2012-12-11 10:09:47 +00:00
|
|
|
/*
|
|
|
|
* cmd_gpt.c -- GPT (GUID Partition Table) handling command
|
|
|
|
*
|
2015-11-20 07:06:17 +00:00
|
|
|
* Copyright (C) 2015
|
|
|
|
* Lukasz Majewski <l.majewski@majess.pl>
|
|
|
|
*
|
2012-12-11 10:09:47 +00:00
|
|
|
* Copyright (C) 2012 Samsung Electronics
|
|
|
|
* author: Lukasz Majewski <l.majewski@samsung.com>
|
|
|
|
* author: Piotr Wilczek <p.wilczek@samsung.com>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-12-11 10:09:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <part_efi.h>
|
|
|
|
#include <exports.h>
|
|
|
|
#include <linux/ctype.h>
|
2013-01-27 22:59:25 +00:00
|
|
|
#include <div64.h>
|
2015-11-20 07:06:17 +00:00
|
|
|
#include <memalign.h>
|
2012-12-11 10:09:47 +00:00
|
|
|
|
|
|
|
#ifndef CONFIG_PARTITION_UUIDS
|
|
|
|
#error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extract_env(): Expand env name from string format '&{env_name}'
|
|
|
|
* and return pointer to the env (if the env is set)
|
|
|
|
*
|
|
|
|
* @param str - pointer to string
|
|
|
|
* @param env - pointer to pointer to extracted env
|
|
|
|
*
|
|
|
|
* @return - zero on successful expand and env is set
|
|
|
|
*/
|
2014-04-02 08:20:06 +00:00
|
|
|
static int extract_env(const char *str, char **env)
|
2012-12-11 10:09:47 +00:00
|
|
|
{
|
2014-04-02 08:20:06 +00:00
|
|
|
int ret = -1;
|
2012-12-11 10:09:47 +00:00
|
|
|
char *e, *s;
|
2014-04-02 08:20:06 +00:00
|
|
|
#ifdef CONFIG_RANDOM_UUID
|
|
|
|
char uuid_str[UUID_STR_LEN + 1];
|
|
|
|
#endif
|
2012-12-11 10:09:47 +00:00
|
|
|
|
|
|
|
if (!str || strlen(str) < 4)
|
|
|
|
return -1;
|
|
|
|
|
2014-04-02 08:20:06 +00:00
|
|
|
if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s = strdup(str);
|
|
|
|
if (s == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(s + strlen(s) - 1, '\0', 1);
|
|
|
|
memmove(s, s + 2, strlen(s) - 1);
|
|
|
|
|
|
|
|
e = getenv(s);
|
|
|
|
if (e == NULL) {
|
|
|
|
#ifdef CONFIG_RANDOM_UUID
|
|
|
|
debug("%s unset. ", str);
|
|
|
|
gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD);
|
|
|
|
setenv(s, uuid_str);
|
|
|
|
|
2012-12-11 10:09:47 +00:00
|
|
|
e = getenv(s);
|
2014-04-02 08:20:06 +00:00
|
|
|
if (e) {
|
|
|
|
debug("Set to random.\n");
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
debug("Can't get random UUID.\n");
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
2014-04-02 08:20:06 +00:00
|
|
|
#else
|
|
|
|
debug("%s unset.\n", str);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
debug("%s get from environment.\n", str);
|
|
|
|
ret = 0;
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 08:20:06 +00:00
|
|
|
*env = e;
|
|
|
|
free(s);
|
|
|
|
|
|
|
|
return ret;
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extract_val(): Extract value from a key=value pair list (comma separated).
|
|
|
|
* Only value for the given key is returend.
|
|
|
|
* Function allocates memory for the value, remember to free!
|
|
|
|
*
|
|
|
|
* @param str - pointer to string with key=values pairs
|
|
|
|
* @param key - pointer to the key to search for
|
|
|
|
*
|
|
|
|
* @return - pointer to allocated string with the value
|
|
|
|
*/
|
|
|
|
static char *extract_val(const char *str, const char *key)
|
|
|
|
{
|
|
|
|
char *v, *k;
|
|
|
|
char *s, *strcopy;
|
|
|
|
char *new = NULL;
|
|
|
|
|
|
|
|
strcopy = strdup(str);
|
|
|
|
if (strcopy == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = strcopy;
|
|
|
|
while (s) {
|
|
|
|
v = strsep(&s, ",");
|
|
|
|
if (!v)
|
|
|
|
break;
|
|
|
|
k = strsep(&v, "=");
|
|
|
|
if (!k)
|
|
|
|
break;
|
|
|
|
if (strcmp(k, key) == 0) {
|
|
|
|
new = strdup(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(strcopy);
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_gpt_info(): Fill partition information from string
|
|
|
|
* function allocates memory, remember to free!
|
|
|
|
*
|
|
|
|
* @param dev_desc - pointer block device descriptor
|
|
|
|
* @param str_part - pointer to string with partition information
|
|
|
|
* @param str_disk_guid - pointer to pointer to allocated string with disk guid
|
|
|
|
* @param partitions - pointer to pointer to allocated partitions array
|
|
|
|
* @param parts_count - number of partitions
|
|
|
|
*
|
|
|
|
* @return - zero on success, otherwise error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int set_gpt_info(block_dev_desc_t *dev_desc,
|
|
|
|
const char *str_part,
|
|
|
|
char **str_disk_guid,
|
|
|
|
disk_partition_t **partitions,
|
|
|
|
u8 *parts_count)
|
|
|
|
{
|
|
|
|
char *tok, *str, *s;
|
|
|
|
int i;
|
|
|
|
char *val, *p;
|
|
|
|
int p_count;
|
|
|
|
disk_partition_t *parts;
|
|
|
|
int errno = 0;
|
2013-01-27 22:59:25 +00:00
|
|
|
uint64_t size_ll, start_ll;
|
2012-12-11 10:09:47 +00:00
|
|
|
|
2013-10-04 16:53:04 +00:00
|
|
|
debug("%s: lba num: 0x%x %d\n", __func__,
|
2012-12-11 10:09:47 +00:00
|
|
|
(unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
|
|
|
|
|
|
|
|
if (str_part == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
str = strdup(str_part);
|
|
|
|
|
|
|
|
/* extract disk guid */
|
|
|
|
s = str;
|
2015-01-26 15:44:18 +00:00
|
|
|
val = extract_val(str, "uuid_disk");
|
2012-12-11 10:09:47 +00:00
|
|
|
if (!val) {
|
2015-01-26 15:44:18 +00:00
|
|
|
#ifdef CONFIG_RANDOM_UUID
|
|
|
|
*str_disk_guid = malloc(UUID_STR_LEN + 1);
|
|
|
|
gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
|
|
|
|
#else
|
2012-12-11 10:09:47 +00:00
|
|
|
free(str);
|
|
|
|
return -2;
|
2015-01-26 15:44:18 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
val = strsep(&val, ";");
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
|
|
|
*str_disk_guid = strdup(p);
|
|
|
|
free(val);
|
|
|
|
/* Move s to first partition */
|
|
|
|
strsep(&s, ";");
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
|
|
|
if (strlen(s) == 0)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
i = strlen(s) - 1;
|
|
|
|
if (s[i] == ';')
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
/* calculate expected number of partitions */
|
|
|
|
p_count = 1;
|
|
|
|
p = s;
|
|
|
|
while (*p) {
|
|
|
|
if (*p++ == ';')
|
|
|
|
p_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate memory for partitions */
|
|
|
|
parts = calloc(sizeof(disk_partition_t), p_count);
|
|
|
|
|
2013-08-21 15:39:19 +00:00
|
|
|
/* retrieve partitions data from string */
|
2012-12-11 10:09:47 +00:00
|
|
|
for (i = 0; i < p_count; i++) {
|
|
|
|
tok = strsep(&s, ";");
|
|
|
|
|
|
|
|
if (tok == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* uuid */
|
|
|
|
val = extract_val(tok, "uuid");
|
2015-01-26 15:44:18 +00:00
|
|
|
if (!val) {
|
|
|
|
/* 'uuid' is optional if random uuid's are enabled */
|
|
|
|
#ifdef CONFIG_RANDOM_UUID
|
|
|
|
gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
|
|
|
|
#else
|
2012-12-11 10:09:47 +00:00
|
|
|
errno = -4;
|
|
|
|
goto err;
|
2015-01-26 15:44:18 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
|
|
|
if (strlen(p) >= sizeof(parts[i].uuid)) {
|
|
|
|
printf("Wrong uuid format for partition %d\n", i);
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strcpy((char *)parts[i].uuid, p);
|
|
|
|
free(val);
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
2015-10-27 10:00:27 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
|
|
|
/* guid */
|
|
|
|
val = extract_val(tok, "type");
|
|
|
|
if (val) {
|
|
|
|
/* 'type' is optional */
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
|
|
|
if (strlen(p) >= sizeof(parts[i].type_guid)) {
|
|
|
|
printf("Wrong type guid format for partition %d\n",
|
|
|
|
i);
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strcpy((char *)parts[i].type_guid, p);
|
|
|
|
free(val);
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-11 10:09:47 +00:00
|
|
|
/* name */
|
|
|
|
val = extract_val(tok, "name");
|
|
|
|
if (!val) { /* name is mandatory */
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
|
|
|
if (strlen(p) >= sizeof(parts[i].name)) {
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strcpy((char *)parts[i].name, p);
|
|
|
|
free(val);
|
|
|
|
|
|
|
|
/* size */
|
|
|
|
val = extract_val(tok, "size");
|
|
|
|
if (!val) { /* 'size' is mandatory */
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
2013-01-27 22:59:25 +00:00
|
|
|
size_ll = ustrtoull(p, &p, 0);
|
|
|
|
parts[i].size = lldiv(size_ll, dev_desc->blksz);
|
2012-12-11 10:09:47 +00:00
|
|
|
free(val);
|
|
|
|
|
|
|
|
/* start address */
|
|
|
|
val = extract_val(tok, "start");
|
|
|
|
if (val) { /* start address is optional */
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
2013-01-27 22:59:25 +00:00
|
|
|
start_ll = ustrtoull(p, &p, 0);
|
|
|
|
parts[i].start = lldiv(start_ll, dev_desc->blksz);
|
2012-12-11 10:09:47 +00:00
|
|
|
free(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*parts_count = p_count;
|
|
|
|
*partitions = parts;
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
free(str);
|
|
|
|
free(*str_disk_guid);
|
|
|
|
free(parts);
|
|
|
|
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:53:04 +00:00
|
|
|
static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part)
|
2012-12-11 10:09:47 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *str_disk_guid;
|
|
|
|
u8 part_count = 0;
|
|
|
|
disk_partition_t *partitions = NULL;
|
|
|
|
|
|
|
|
/* fill partitions */
|
2013-10-04 16:53:04 +00:00
|
|
|
ret = set_gpt_info(blk_dev_desc, str_part,
|
2012-12-11 10:09:47 +00:00
|
|
|
&str_disk_guid, &partitions, &part_count);
|
|
|
|
if (ret) {
|
|
|
|
if (ret == -1)
|
|
|
|
printf("No partition list provided\n");
|
|
|
|
if (ret == -2)
|
|
|
|
printf("Missing disk guid\n");
|
|
|
|
if ((ret == -3) || (ret == -4))
|
|
|
|
printf("Partition list incomplete\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save partitions layout to disk */
|
2015-01-26 15:43:15 +00:00
|
|
|
ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
|
2012-12-11 10:09:47 +00:00
|
|
|
free(str_disk_guid);
|
|
|
|
free(partitions);
|
|
|
|
|
2015-01-26 15:43:15 +00:00
|
|
|
return ret;
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 07:06:17 +00:00
|
|
|
static int gpt_verify(block_dev_desc_t *blk_dev_desc, const char *str_part)
|
|
|
|
{
|
|
|
|
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
|
|
|
|
blk_dev_desc->blksz);
|
|
|
|
disk_partition_t *partitions = NULL;
|
|
|
|
gpt_entry *gpt_pte = NULL;
|
|
|
|
char *str_disk_guid;
|
|
|
|
u8 part_count = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* fill partitions */
|
|
|
|
ret = set_gpt_info(blk_dev_desc, str_part,
|
|
|
|
&str_disk_guid, &partitions, &part_count);
|
|
|
|
if (ret) {
|
|
|
|
if (ret == -1) {
|
|
|
|
printf("No partition list provided - only basic check\n");
|
|
|
|
ret = gpt_verify_headers(blk_dev_desc, gpt_head,
|
|
|
|
&gpt_pte);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ret == -2)
|
|
|
|
printf("Missing disk guid\n");
|
|
|
|
if ((ret == -3) || (ret == -4))
|
|
|
|
printf("Partition list incomplete\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check partition layout with provided pattern */
|
|
|
|
ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
|
|
|
|
gpt_head, &gpt_pte);
|
|
|
|
free(str_disk_guid);
|
|
|
|
free(partitions);
|
|
|
|
out:
|
|
|
|
free(gpt_pte);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-11 10:09:47 +00:00
|
|
|
/**
|
|
|
|
* do_gpt(): Perform GPT operations
|
|
|
|
*
|
|
|
|
* @param cmdtp - command name
|
|
|
|
* @param flag
|
|
|
|
* @param argc
|
|
|
|
* @param argv
|
|
|
|
*
|
|
|
|
* @return zero on success; otherwise error
|
|
|
|
*/
|
|
|
|
static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
int ret = CMD_RET_SUCCESS;
|
|
|
|
int dev = 0;
|
2013-10-04 16:53:04 +00:00
|
|
|
char *ep;
|
2015-11-20 07:06:17 +00:00
|
|
|
block_dev_desc_t *blk_dev_desc = NULL;
|
2012-12-11 10:09:47 +00:00
|
|
|
|
2015-11-20 07:06:17 +00:00
|
|
|
if (argc < 4 || argc > 5)
|
2012-12-11 10:09:47 +00:00
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
2015-11-20 07:06:17 +00:00
|
|
|
dev = (int)simple_strtoul(argv[3], &ep, 10);
|
|
|
|
if (!ep || ep[0] != '\0') {
|
|
|
|
printf("'%s' is not a number\n", argv[3]);
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
blk_dev_desc = get_dev(argv[2], dev);
|
|
|
|
if (!blk_dev_desc) {
|
|
|
|
printf("%s: %s dev %d NOT available\n",
|
|
|
|
__func__, argv[2], dev);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
2014-04-02 08:20:06 +00:00
|
|
|
|
2015-11-20 07:06:17 +00:00
|
|
|
if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
|
|
|
|
printf("Writing GPT: ");
|
2014-04-02 08:20:06 +00:00
|
|
|
ret = gpt_default(blk_dev_desc, argv[4]);
|
2015-11-20 07:06:17 +00:00
|
|
|
} else if ((strcmp(argv[1], "verify") == 0)) {
|
|
|
|
ret = gpt_verify(blk_dev_desc, argv[4]);
|
|
|
|
printf("Verify GPT: ");
|
2012-12-11 10:09:47 +00:00
|
|
|
} else {
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
2015-11-20 07:06:17 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
printf("error!\n");
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("success!\n");
|
|
|
|
return CMD_RET_SUCCESS;
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
|
|
|
|
"GUID Partition Table",
|
2013-08-21 15:39:19 +00:00
|
|
|
"<command> <interface> <dev> <partitions_list>\n"
|
2015-11-13 06:42:10 +00:00
|
|
|
" - GUID partition table restoration and validity check\n"
|
|
|
|
" Restore or verify GPT information on a device connected\n"
|
2012-12-11 10:09:47 +00:00
|
|
|
" to interface\n"
|
2015-11-13 06:42:10 +00:00
|
|
|
" Example usage:\n"
|
|
|
|
" gpt write mmc 0 $partitions\n"
|
|
|
|
" gpt verify mmc 0 $partitions\n"
|
2012-12-11 10:09:47 +00:00
|
|
|
);
|