2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <blk.h>
|
2019-08-01 15:46:51 +00:00
|
|
|
#include <env.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2012-12-11 10:09:47 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <command.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <part.h>
|
2012-12-11 10:09:47 +00:00
|
|
|
#include <part_efi.h>
|
2020-11-09 06:19:16 +00:00
|
|
|
#include <part.h>
|
2012-12-11 10:09:47 +00:00
|
|
|
#include <exports.h>
|
2020-05-10 17:39:52 +00:00
|
|
|
#include <uuid.h>
|
2012-12-11 10:09:47 +00:00
|
|
|
#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>
|
2017-07-04 18:18:50 +00:00
|
|
|
#include <linux/compat.h>
|
2020-02-03 14:36:15 +00:00
|
|
|
#include <linux/err.h>
|
2017-07-04 18:19:18 +00:00
|
|
|
#include <linux/sizes.h>
|
|
|
|
#include <stdlib.h>
|
2017-07-04 18:18:50 +00:00
|
|
|
|
|
|
|
static LIST_HEAD(disk_partitions);
|
2012-12-11 10:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: - zero on successful expand and env is set
|
2012-12-11 10:09:47 +00:00
|
|
|
*/
|
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);
|
|
|
|
|
2017-08-03 18:22:12 +00:00
|
|
|
e = env_get(s);
|
2014-04-02 08:20:06 +00:00
|
|
|
if (e == NULL) {
|
|
|
|
#ifdef CONFIG_RANDOM_UUID
|
|
|
|
debug("%s unset. ", str);
|
2017-02-27 14:11:15 +00:00
|
|
|
gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
|
2017-08-03 18:22:09 +00:00
|
|
|
env_set(s, uuid_str);
|
2014-04-02 08:20:06 +00:00
|
|
|
|
2017-08-03 18:22:12 +00:00
|
|
|
e = env_get(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
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: - pointer to allocated string with the value
|
2012-12-11 10:09:47 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-17 10:36:52 +00:00
|
|
|
/**
|
|
|
|
* found_key(): Found key without value in parameter list (comma separated).
|
|
|
|
*
|
|
|
|
* @param str - pointer to string with key
|
|
|
|
* @param key - pointer to the key to search for
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: - true on found key
|
2015-11-17 10:36:52 +00:00
|
|
|
*/
|
|
|
|
static bool found_key(const char *str, const char *key)
|
|
|
|
{
|
|
|
|
char *k;
|
|
|
|
char *s, *strcopy;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
strcopy = strdup(str);
|
|
|
|
if (!strcopy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = strcopy;
|
|
|
|
while (s) {
|
|
|
|
k = strsep(&s, ",");
|
|
|
|
if (!k)
|
|
|
|
break;
|
|
|
|
if (strcmp(k, key) == 0) {
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(strcopy);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
static int calc_parts_list_len(int numparts)
|
|
|
|
{
|
|
|
|
int partlistlen = UUID_STR_LEN + 1 + strlen("uuid_disk=");
|
|
|
|
/* for the comma */
|
|
|
|
partlistlen++;
|
|
|
|
|
|
|
|
/* per-partition additions; numparts starts at 1, so this should be correct */
|
|
|
|
partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN + 1);
|
|
|
|
/* see part.h for definition of struct disk_partition */
|
|
|
|
partlistlen += numparts * (strlen("start=MiB,") + sizeof(lbaint_t) + 1);
|
|
|
|
partlistlen += numparts * (strlen("size=MiB,") + sizeof(lbaint_t) + 1);
|
2023-08-31 16:51:39 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
|
|
|
partlistlen += numparts * (strlen("type=,") + UUID_STR_LEN + 1);
|
|
|
|
#endif
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN + 1);
|
|
|
|
/* for the terminating null */
|
|
|
|
partlistlen++;
|
|
|
|
debug("Length of partitions_list is %d for %d partitions\n", partlistlen,
|
|
|
|
numparts);
|
|
|
|
return partlistlen;
|
|
|
|
}
|
|
|
|
|
2017-07-04 18:18:50 +00:00
|
|
|
#ifdef CONFIG_CMD_GPT_RENAME
|
|
|
|
static void del_gpt_info(void)
|
|
|
|
{
|
|
|
|
struct list_head *pos = &disk_partitions;
|
|
|
|
struct disk_part *curr;
|
|
|
|
while (!list_empty(pos)) {
|
|
|
|
curr = list_entry(pos->next, struct disk_part, list);
|
|
|
|
list_del(pos->next);
|
|
|
|
free(curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:39:57 +00:00
|
|
|
static struct disk_part *allocate_disk_part(struct disk_partition *info,
|
|
|
|
int partnum)
|
2017-07-04 18:18:50 +00:00
|
|
|
{
|
|
|
|
struct disk_part *newpart;
|
2017-09-21 17:03:06 +00:00
|
|
|
newpart = calloc(1, sizeof(struct disk_part));
|
2017-07-04 18:18:50 +00:00
|
|
|
if (!newpart)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
newpart->gpt_part_info.start = info->start;
|
|
|
|
newpart->gpt_part_info.size = info->size;
|
|
|
|
newpart->gpt_part_info.blksz = info->blksz;
|
|
|
|
strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
|
|
|
|
PART_NAME_LEN);
|
|
|
|
newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
|
|
|
|
strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
|
|
|
|
PART_TYPE_LEN);
|
|
|
|
newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
|
|
|
|
newpart->gpt_part_info.bootable = info->bootable;
|
2023-08-31 16:51:39 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
|
|
|
strncpy(newpart->gpt_part_info.type_guid, (const char *)info->type_guid,
|
|
|
|
UUID_STR_LEN);
|
|
|
|
newpart->gpt_part_info.type_guid[UUID_STR_LEN] = '\0';
|
|
|
|
#endif
|
2023-08-24 19:55:31 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) {
|
|
|
|
strlcpy(newpart->gpt_part_info.uuid, disk_partition_uuid(info),
|
|
|
|
UUID_STR_LEN + 1);
|
|
|
|
}
|
2017-07-04 18:18:50 +00:00
|
|
|
newpart->partnum = partnum;
|
|
|
|
|
|
|
|
return newpart;
|
|
|
|
}
|
|
|
|
|
2017-07-04 18:19:18 +00:00
|
|
|
static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
|
|
|
|
lbaint_t blksize)
|
|
|
|
{
|
|
|
|
unsigned long long partbytes, partmegabytes;
|
|
|
|
|
|
|
|
partbytes = partsize * blksize;
|
|
|
|
partmegabytes = lldiv(partbytes, SZ_1M);
|
|
|
|
snprintf(sizestr, 16, "%lluMiB", partmegabytes);
|
|
|
|
}
|
|
|
|
|
2017-07-04 18:18:50 +00:00
|
|
|
static void print_gpt_info(void)
|
|
|
|
{
|
|
|
|
struct list_head *pos;
|
|
|
|
struct disk_part *curr;
|
2017-07-04 18:19:18 +00:00
|
|
|
char partstartstr[16];
|
|
|
|
char partsizestr[16];
|
2017-07-04 18:18:50 +00:00
|
|
|
|
|
|
|
list_for_each(pos, &disk_partitions) {
|
|
|
|
curr = list_entry(pos, struct disk_part, list);
|
2017-07-04 18:19:18 +00:00
|
|
|
prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
|
|
|
|
curr->gpt_part_info.blksz);
|
|
|
|
prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
|
|
|
|
curr->gpt_part_info.blksz);
|
|
|
|
|
2017-07-04 18:18:50 +00:00
|
|
|
printf("Partition %d:\n", curr->partnum);
|
2017-07-04 18:19:18 +00:00
|
|
|
printf("Start %s, size %s\n", partstartstr, partsizestr);
|
2017-07-04 18:18:50 +00:00
|
|
|
printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
|
|
|
|
curr->gpt_part_info.name);
|
|
|
|
printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
|
2020-03-19 12:49:34 +00:00
|
|
|
curr->gpt_part_info.bootable & PART_BOOTABLE);
|
2023-08-31 16:51:39 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
|
|
|
printf("Type GUID %s\n", curr->gpt_part_info.type_guid);
|
|
|
|
#endif
|
2017-07-04 18:18:50 +00:00
|
|
|
#ifdef CONFIG_PARTITION_UUIDS
|
|
|
|
printf("UUID %s\n", curr->gpt_part_info.uuid);
|
|
|
|
#endif
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 18:19:18 +00:00
|
|
|
/*
|
|
|
|
* create the string that upstream 'gpt write' command will accept as an
|
|
|
|
* argument
|
|
|
|
*
|
|
|
|
* From doc/README.gpt, Format of partitions layout:
|
|
|
|
* "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
|
|
|
|
* name=kernel,size=60MiB,uuid=...;"
|
|
|
|
* The fields 'name' and 'size' are mandatory for every partition.
|
|
|
|
* The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
|
|
|
|
* are optional if CONFIG_RANDOM_UUID is enabled.
|
|
|
|
*/
|
|
|
|
static int create_gpt_partitions_list(int numparts, const char *guid,
|
|
|
|
char *partitions_list)
|
|
|
|
{
|
|
|
|
struct list_head *pos;
|
|
|
|
struct disk_part *curr;
|
|
|
|
char partstr[PART_NAME_LEN + 1];
|
|
|
|
|
|
|
|
if (!partitions_list)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
strcpy(partitions_list, "uuid_disk=");
|
|
|
|
strncat(partitions_list, guid, UUID_STR_LEN + 1);
|
|
|
|
strcat(partitions_list, ";");
|
|
|
|
|
|
|
|
list_for_each(pos, &disk_partitions) {
|
|
|
|
curr = list_entry(pos, struct disk_part, list);
|
|
|
|
strcat(partitions_list, "name=");
|
|
|
|
strncat(partitions_list, (const char *)curr->gpt_part_info.name,
|
|
|
|
PART_NAME_LEN + 1);
|
2017-10-18 13:11:08 +00:00
|
|
|
sprintf(partstr, ",start=0x%llx",
|
|
|
|
(unsigned long long)curr->gpt_part_info.start *
|
|
|
|
curr->gpt_part_info.blksz);
|
2017-07-04 18:19:18 +00:00
|
|
|
/* one extra byte for NULL */
|
|
|
|
strncat(partitions_list, partstr, PART_NAME_LEN + 1);
|
2017-10-18 13:11:08 +00:00
|
|
|
sprintf(partstr, ",size=0x%llx",
|
|
|
|
(unsigned long long)curr->gpt_part_info.size *
|
|
|
|
curr->gpt_part_info.blksz);
|
2017-07-04 18:19:18 +00:00
|
|
|
strncat(partitions_list, partstr, PART_NAME_LEN + 1);
|
|
|
|
|
2023-08-31 16:51:39 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
|
|
|
strcat(partitions_list, ",type=");
|
|
|
|
strncat(partitions_list, curr->gpt_part_info.type_guid,
|
|
|
|
UUID_STR_LEN + 1);
|
|
|
|
#endif
|
2017-07-04 18:19:18 +00:00
|
|
|
strcat(partitions_list, ",uuid=");
|
|
|
|
strncat(partitions_list, curr->gpt_part_info.uuid,
|
|
|
|
UUID_STR_LEN + 1);
|
|
|
|
strcat(partitions_list, ";");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-04 18:18:50 +00:00
|
|
|
/*
|
|
|
|
* read partition info into disk_partitions list where
|
|
|
|
* it can be printed or modified
|
|
|
|
*/
|
|
|
|
static int get_gpt_info(struct blk_desc *dev_desc)
|
|
|
|
{
|
|
|
|
/* start partition numbering at 1, as U-Boot does */
|
|
|
|
int valid_parts = 0, p, ret;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition info;
|
2017-07-04 18:18:50 +00:00
|
|
|
struct disk_part *new_disk_part;
|
|
|
|
|
2017-07-04 18:19:18 +00:00
|
|
|
/*
|
|
|
|
* Always re-read partition info from device, in case
|
|
|
|
* it has changed
|
|
|
|
*/
|
|
|
|
INIT_LIST_HEAD(&disk_partitions);
|
2017-07-04 18:18:50 +00:00
|
|
|
|
|
|
|
for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
|
|
|
|
ret = part_get_info(dev_desc, p, &info);
|
|
|
|
if (ret)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Add 1 here because counter is zero-based but p1 is
|
|
|
|
the first partition */
|
|
|
|
new_disk_part = allocate_disk_part(&info, valid_parts+1);
|
|
|
|
if (IS_ERR(new_disk_part))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
list_add_tail(&new_disk_part->list, &disk_partitions);
|
|
|
|
valid_parts++;
|
|
|
|
}
|
|
|
|
if (valid_parts == 0) {
|
|
|
|
printf("** No valid partitions found **\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
return valid_parts;
|
|
|
|
out:
|
|
|
|
if (valid_parts >= 1)
|
|
|
|
del_gpt_info();
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a wrapper to test get_gpt_info */
|
2021-02-26 18:17:33 +00:00
|
|
|
static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr)
|
2017-07-04 18:18:50 +00:00
|
|
|
{
|
2021-02-26 18:17:33 +00:00
|
|
|
int numparts;
|
|
|
|
|
|
|
|
numparts = get_gpt_info(dev_desc);
|
|
|
|
|
|
|
|
if (numparts > 0) {
|
|
|
|
if (namestr) {
|
|
|
|
char disk_guid[UUID_STR_LEN + 1];
|
|
|
|
char *partitions_list;
|
|
|
|
int partlistlen;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
ret = get_disk_guid(dev_desc, disk_guid);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
partlistlen = calc_parts_list_len(numparts);
|
|
|
|
partitions_list = malloc(partlistlen);
|
|
|
|
if (!partitions_list) {
|
|
|
|
del_gpt_info();
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
memset(partitions_list, '\0', partlistlen);
|
|
|
|
|
|
|
|
ret = create_gpt_partitions_list(numparts, disk_guid,
|
|
|
|
partitions_list);
|
|
|
|
if (ret < 0)
|
|
|
|
printf("Error: Could not create partition list string!\n");
|
|
|
|
else
|
|
|
|
env_set(namestr, partitions_list);
|
2017-07-04 18:18:50 +00:00
|
|
|
|
2021-02-26 18:17:33 +00:00
|
|
|
free(partitions_list);
|
|
|
|
} else {
|
|
|
|
print_gpt_info();
|
|
|
|
}
|
2017-07-04 18:18:50 +00:00
|
|
|
del_gpt_info();
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-26 18:17:33 +00:00
|
|
|
return numparts;
|
2017-07-04 18:18:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-11 10:09:47 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: - zero on success, otherwise error
|
2012-12-11 10:09:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-02-29 22:25:34 +00:00
|
|
|
static int set_gpt_info(struct blk_desc *dev_desc,
|
2012-12-11 10:09:47 +00:00
|
|
|
const char *str_part,
|
|
|
|
char **str_disk_guid,
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition **partitions,
|
2012-12-11 10:09:47 +00:00
|
|
|
u8 *parts_count)
|
|
|
|
{
|
|
|
|
char *tok, *str, *s;
|
|
|
|
int i;
|
|
|
|
char *val, *p;
|
|
|
|
int p_count;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition *parts;
|
2012-12-11 10:09:47 +00:00
|
|
|
int errno = 0;
|
2013-01-27 22:59:25 +00:00
|
|
|
uint64_t size_ll, start_ll;
|
2016-06-08 08:18:16 +00:00
|
|
|
lbaint_t offset = 0;
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
|
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);
|
2017-07-04 18:19:18 +00:00
|
|
|
if (str == NULL)
|
|
|
|
return -ENOMEM;
|
2012-12-11 10:09:47 +00:00
|
|
|
|
|
|
|
/* 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);
|
2017-10-07 15:27:59 +00:00
|
|
|
if (*str_disk_guid == NULL)
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
return -ENOMEM;
|
2015-01-26 15:44:18 +00:00
|
|
|
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
|
|
|
}
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
printf("Error: is the partitions string NULL-terminated?\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (strnlen(s, max_str_part) == 0)
|
2012-12-11 10:09:47 +00:00
|
|
|
return -3;
|
|
|
|
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
i = strnlen(s, max_str_part) - 1;
|
2012-12-11 10:09:47 +00:00
|
|
|
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 */
|
2020-05-10 17:39:57 +00:00
|
|
|
parts = calloc(sizeof(struct disk_partition), p_count);
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
if (parts == NULL)
|
|
|
|
return -ENOMEM;
|
2012-12-11 10:09:47 +00:00
|
|
|
|
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;
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
|
2015-01-26 15:44:18 +00:00
|
|
|
printf("Wrong uuid format for partition %d\n", i);
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
strncpy((char *)parts[i].uuid, p, max_str_part);
|
2015-01-26 15:44:18 +00:00
|
|
|
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;
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
|
2015-10-27 10:00:27 +00:00
|
|
|
printf("Wrong type guid format for partition %d\n",
|
|
|
|
i);
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
strncpy((char *)parts[i].type_guid, p, max_str_part);
|
2015-10-27 10:00:27 +00:00
|
|
|
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;
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
|
2012-12-11 10:09:47 +00:00
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
gpt: harden set_gpt_info() against non NULL-terminated strings
Strings read from devices may sometimes fail to be
NULL-terminated. The functions in lib/string.c are subject to
failure in this case. Protect against observed failures in
set_gpt_info() by switching to length-checking variants with a length
limit of the maximum possible partition table length. At the same
time, add a few checks for NULL string pointers.
Here is an example as observed in sandbox under GDB:
=> gpt verify host 0 $partitions
Program received signal SIGSEGV, Segmentation fault.
0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
267 for (sc = s; *sc != '\0'; ++sc)
(gdb) bt
#0 0x0000000000477747 in strlen (s=0x0) at lib/string.c:267
#1 0x00000000004140b2 in set_gpt_info (str_part=<optimized out>,
str_disk_guid=str_disk_guid@entry=0x7fffffffdbe8, partitions=partitions@entry=0x7fffffffdbd8,
parts_count=parts_count@entry=0x7fffffffdbcf "", dev_desc=<optimized out>) at cmd/gpt.c:415
#2 0x00000000004145b9 in gpt_verify (str_part=<optimized out>, blk_dev_desc=0x7fffef09a9d0) at cmd/gpt.c:580
#3 do_gpt (cmdtp=<optimized out>, flag=<optimized out>, argc=<optimized out>, argv=0x7fffef09a8f0)
at cmd/gpt.c:783
#4 0x00000000004295b0 in cmd_call (argv=0x7fffef09a8f0, argc=0x5, flag=<optimized out>,
cmdtp=0x714e20 <_u_boot_list_2_cmd_2_gpt>) at common/command.c:500
#5 cmd_process (flag=<optimized out>, argc=0x5, argv=0x7fffef09a8f0,
repeatable=repeatable@entry=0x726c04 <flag_repeat>, ticks=ticks@entry=0x0) at common/command.c:539
Suggested-by: Lothar Waßmann <LW@karo-electronics.de>
Signed-off-by: Alison Chaiken <alison@peloton-tech.com>
2017-07-04 18:19:46 +00:00
|
|
|
strncpy((char *)parts[i].name, p, max_str_part);
|
2012-12-11 10:09:47 +00:00
|
|
|
free(val);
|
|
|
|
|
|
|
|
/* size */
|
|
|
|
val = extract_val(tok, "size");
|
|
|
|
if (!val) { /* 'size' is mandatory */
|
|
|
|
errno = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (extract_env(val, &p))
|
|
|
|
p = val;
|
2016-06-08 08:18:16 +00:00
|
|
|
if ((strcmp(p, "-") == 0)) {
|
2016-07-29 03:12:18 +00:00
|
|
|
/* Let part efi module to auto extend the size */
|
|
|
|
parts[i].size = 0;
|
2016-06-08 08:18:16 +00:00
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
2015-11-17 10:36:52 +00:00
|
|
|
|
2016-06-08 08:18:16 +00:00
|
|
|
offset += parts[i].size + parts[i].start;
|
|
|
|
|
2015-11-17 10:36:52 +00:00
|
|
|
/* bootable */
|
|
|
|
if (found_key(tok, "bootable"))
|
2020-03-19 12:49:34 +00:00
|
|
|
parts[i].bootable = PART_BOOTABLE;
|
2012-12-11 10:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*parts_count = p_count;
|
|
|
|
*partitions = parts;
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
free(str);
|
|
|
|
free(*str_disk_guid);
|
|
|
|
free(parts);
|
|
|
|
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:46:49 +00:00
|
|
|
static int gpt_repair(struct blk_desc *blk_dev_desc)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = gpt_repair_headers(blk_dev_desc);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:25:34 +00:00
|
|
|
static int gpt_default(struct blk_desc *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;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition *partitions = NULL;
|
2012-12-11 10:09:47 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2016-02-29 22:25:34 +00:00
|
|
|
static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
|
2015-11-20 07:06:17 +00:00
|
|
|
{
|
|
|
|
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
|
|
|
|
blk_dev_desc->blksz);
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition *partitions = NULL;
|
2015-11-20 07:06:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-09 06:19:16 +00:00
|
|
|
/**
|
|
|
|
* gpt_enumerate() - Enumerate partition names into environment variable.
|
|
|
|
*
|
|
|
|
* Enumerate partition names. Partition names are stored in gpt_partition_list
|
|
|
|
* environment variable. Each partition name is delimited by space.
|
|
|
|
*
|
|
|
|
* @desc: block device descriptor
|
|
|
|
*
|
|
|
|
* @Return: '0' on success and -ve error on failure
|
|
|
|
*/
|
|
|
|
static int gpt_enumerate(struct blk_desc *desc)
|
|
|
|
{
|
|
|
|
struct part_driver *first_drv, *part_drv;
|
|
|
|
int str_len = 0, tmp_len;
|
|
|
|
char part_list[2048];
|
|
|
|
int n_drvs;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
part_list[0] = 0;
|
|
|
|
n_drvs = part_driver_get_count();
|
|
|
|
if (!n_drvs) {
|
|
|
|
printf("Failed to get partition driver count\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
first_drv = part_driver_get_first();
|
|
|
|
for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
|
|
|
|
struct disk_partition pinfo;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2023-08-26 01:53:42 +00:00
|
|
|
if (part_drv->test(desc))
|
|
|
|
continue;
|
|
|
|
|
2020-11-09 06:19:16 +00:00
|
|
|
for (i = 1; i < part_drv->max_entries; i++) {
|
|
|
|
ret = part_drv->get_info(desc, i, &pinfo);
|
2023-08-26 01:53:42 +00:00
|
|
|
if (ret)
|
|
|
|
continue;
|
2020-11-09 06:19:16 +00:00
|
|
|
|
|
|
|
ptr = &part_list[str_len];
|
|
|
|
tmp_len = strlen((const char *)pinfo.name);
|
|
|
|
str_len += tmp_len;
|
|
|
|
/* +1 for space */
|
|
|
|
str_len++;
|
|
|
|
if (str_len > sizeof(part_list)) {
|
|
|
|
printf("Error insufficient memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
strcpy(ptr, (const char *)pinfo.name);
|
|
|
|
/* One byte for space(" ") delimiter */
|
|
|
|
ptr[tmp_len] = ' ';
|
|
|
|
}
|
2023-08-26 01:53:42 +00:00
|
|
|
if (*part_list)
|
|
|
|
part_list[strlen(part_list) - 1] = 0;
|
|
|
|
break;
|
2020-11-09 06:19:16 +00:00
|
|
|
}
|
|
|
|
debug("setenv gpt_partition_list %s\n", part_list);
|
|
|
|
|
|
|
|
return env_set("gpt_partition_list", part_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpt_setenv_part_variables() - setup partition environmental variables
|
|
|
|
*
|
|
|
|
* Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
|
2023-08-31 16:51:37 +00:00
|
|
|
* and gpt_partition_size, gpt_partition_bootable environment variables.
|
2020-11-09 06:19:16 +00:00
|
|
|
*
|
|
|
|
* @pinfo: pointer to disk partition
|
|
|
|
* @i: partition entry
|
|
|
|
*
|
|
|
|
* @Return: '0' on success and -ENOENT on failure
|
|
|
|
*/
|
|
|
|
static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = env_set_hex("gpt_partition_addr", pinfo->start);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = env_set_hex("gpt_partition_size", pinfo->size);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2023-08-26 01:53:43 +00:00
|
|
|
ret = env_set_hex("gpt_partition_entry", i);
|
2020-11-09 06:19:16 +00:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = env_set("gpt_partition_name", (const char *)pinfo->name);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2023-08-31 16:51:37 +00:00
|
|
|
ret = env_set_ulong("gpt_partition_bootable", !!(pinfo->bootable & PART_BOOTABLE));
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2020-11-09 06:19:16 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpt_setenv() - Dynamically setup environment variables.
|
|
|
|
*
|
|
|
|
* Dynamically setup environment variables for name, index, offset and size
|
|
|
|
* for partition in GPT table after running "gpt setenv" for a partition name.
|
|
|
|
*
|
|
|
|
* @desc: block device descriptor
|
|
|
|
* @name: partition name
|
|
|
|
*
|
|
|
|
* @Return: '0' on success and -ve err on failure
|
|
|
|
*/
|
|
|
|
static int gpt_setenv(struct blk_desc *desc, const char *name)
|
|
|
|
{
|
|
|
|
struct part_driver *first_drv, *part_drv;
|
|
|
|
int n_drvs;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
n_drvs = part_driver_get_count();
|
|
|
|
if (!n_drvs) {
|
|
|
|
printf("Failed to get partition driver count\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
first_drv = part_driver_get_first();
|
|
|
|
for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
|
|
|
|
struct disk_partition pinfo;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < part_drv->max_entries; i++) {
|
|
|
|
ret = part_drv->get_info(desc, i, &pinfo);
|
2023-08-26 01:53:41 +00:00
|
|
|
if (ret)
|
|
|
|
continue;
|
2020-11-09 06:19:16 +00:00
|
|
|
|
|
|
|
if (!strcmp(name, (const char *)pinfo.name)) {
|
|
|
|
/* match found, setup environment variables */
|
|
|
|
ret = gpt_setenv_part_variables(&pinfo, i);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:43:23 +00:00
|
|
|
static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char disk_guid[UUID_STR_LEN + 1];
|
|
|
|
|
|
|
|
ret = get_disk_guid(dev_desc, disk_guid);
|
|
|
|
if (ret < 0)
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
|
|
|
if (namestr)
|
2017-08-03 18:22:09 +00:00
|
|
|
env_set(namestr, disk_guid);
|
2017-06-25 23:43:23 +00:00
|
|
|
else
|
|
|
|
printf("%s\n", disk_guid);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-04 18:19:18 +00:00
|
|
|
#ifdef CONFIG_CMD_GPT_RENAME
|
|
|
|
static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
|
|
|
|
char *name1, char *name2)
|
|
|
|
{
|
|
|
|
struct list_head *pos;
|
|
|
|
struct disk_part *curr;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition *new_partitions = NULL;
|
2017-07-04 18:19:18 +00:00
|
|
|
char disk_guid[UUID_STR_LEN + 1];
|
2020-01-21 16:53:38 +00:00
|
|
|
char *partitions_list, *str_disk_guid = NULL;
|
2017-07-04 18:19:18 +00:00
|
|
|
u8 part_count = 0;
|
|
|
|
int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
|
|
|
|
|
|
|
|
if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
|
|
|
|
(strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = get_disk_guid(dev_desc, disk_guid);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-09-26 14:42:28 +00:00
|
|
|
/*
|
|
|
|
* Allocates disk_partitions, requiring matching call to del_gpt_info()
|
|
|
|
* if successful.
|
|
|
|
*/
|
2017-07-04 18:19:18 +00:00
|
|
|
numparts = get_gpt_info(dev_desc);
|
|
|
|
if (numparts <= 0)
|
|
|
|
return numparts ? numparts : -ENODEV;
|
|
|
|
|
|
|
|
partlistlen = calc_parts_list_len(numparts);
|
|
|
|
partitions_list = malloc(partlistlen);
|
2017-09-26 14:42:28 +00:00
|
|
|
if (!partitions_list) {
|
|
|
|
del_gpt_info();
|
2017-07-04 18:19:18 +00:00
|
|
|
return -ENOMEM;
|
2017-09-26 14:42:28 +00:00
|
|
|
}
|
2017-07-04 18:19:18 +00:00
|
|
|
memset(partitions_list, '\0', partlistlen);
|
|
|
|
|
|
|
|
ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
|
2017-09-26 14:42:28 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
free(partitions_list);
|
2017-07-04 18:19:18 +00:00
|
|
|
return ret;
|
2017-09-26 14:42:28 +00:00
|
|
|
}
|
2017-07-04 18:19:18 +00:00
|
|
|
/*
|
|
|
|
* Uncomment the following line to print a string that 'gpt write'
|
|
|
|
* or 'gpt verify' will accept as input.
|
|
|
|
*/
|
|
|
|
debug("OLD partitions_list is %s with %u chars\n", partitions_list,
|
|
|
|
(unsigned)strlen(partitions_list));
|
|
|
|
|
2017-09-26 14:42:28 +00:00
|
|
|
/* set_gpt_info allocates new_partitions and str_disk_guid */
|
2017-07-04 18:19:18 +00:00
|
|
|
ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
|
|
|
|
&new_partitions, &part_count);
|
2020-01-21 16:53:38 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
|
|
|
|
if (!strcmp(subcomm, "swap")) {
|
|
|
|
if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
|
|
|
|
printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
|
2017-09-26 14:42:28 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
}
|
|
|
|
list_for_each(pos, &disk_partitions) {
|
|
|
|
curr = list_entry(pos, struct disk_part, list);
|
|
|
|
if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
|
|
|
|
strcpy((char *)curr->gpt_part_info.name, name2);
|
|
|
|
ctr1++;
|
|
|
|
} else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
|
|
|
|
strcpy((char *)curr->gpt_part_info.name, name1);
|
|
|
|
ctr2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
|
|
|
|
printf("Cannot swap partition names except in pairs.\n");
|
2017-09-26 14:42:28 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
}
|
|
|
|
} else { /* rename */
|
|
|
|
if (strlen(name2) > PART_NAME_LEN) {
|
|
|
|
printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
|
2017-09-26 14:42:28 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
}
|
|
|
|
partnum = (int)simple_strtol(name1, NULL, 10);
|
|
|
|
if ((partnum < 0) || (partnum > numparts)) {
|
|
|
|
printf("Illegal partition number %s\n", name1);
|
2017-09-26 14:42:28 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
}
|
|
|
|
ret = part_get_info(dev_desc, partnum, new_partitions);
|
|
|
|
if (ret < 0)
|
2017-09-26 14:42:28 +00:00
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
|
|
|
|
/* U-Boot partition numbering starts at 1 */
|
|
|
|
list_for_each(pos, &disk_partitions) {
|
|
|
|
curr = list_entry(pos, struct disk_part, list);
|
|
|
|
if (i == partnum) {
|
|
|
|
strcpy((char *)curr->gpt_part_info.name, name2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
|
|
|
|
if (ret < 0)
|
2017-09-26 14:42:28 +00:00
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
debug("NEW partitions_list is %s with %u chars\n", partitions_list,
|
|
|
|
(unsigned)strlen(partitions_list));
|
|
|
|
|
|
|
|
ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
|
|
|
|
&new_partitions, &part_count);
|
2017-09-26 14:42:28 +00:00
|
|
|
/*
|
|
|
|
* Even though valid pointers are here passed into set_gpt_info(),
|
|
|
|
* it mallocs again, and there's no way to tell which failed.
|
|
|
|
*/
|
2020-01-21 16:53:38 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
|
|
|
|
debug("Writing new partition table\n");
|
|
|
|
ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("Writing new partition table failed\n");
|
2017-09-26 14:42:28 +00:00
|
|
|
goto out;
|
2017-07-04 18:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug("Reading back new partition table\n");
|
2017-09-26 14:42:28 +00:00
|
|
|
/*
|
|
|
|
* Empty the existing disk_partitions list, as otherwise the memory in
|
|
|
|
* the original list is unreachable.
|
|
|
|
*/
|
|
|
|
del_gpt_info();
|
2017-07-04 18:19:18 +00:00
|
|
|
numparts = get_gpt_info(dev_desc);
|
2017-09-26 14:42:28 +00:00
|
|
|
if (numparts <= 0) {
|
|
|
|
ret = numparts ? numparts : -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-07-04 18:19:18 +00:00
|
|
|
printf("new partition table with %d partitions is:\n", numparts);
|
|
|
|
print_gpt_info();
|
2017-09-26 14:42:28 +00:00
|
|
|
out:
|
2020-01-21 16:53:38 +00:00
|
|
|
del_gpt_info();
|
|
|
|
#ifdef CONFIG_RANDOM_UUID
|
2020-04-28 19:40:13 +00:00
|
|
|
free(str_disk_guid);
|
2020-01-21 16:53:38 +00:00
|
|
|
#endif
|
2020-04-28 19:40:13 +00:00
|
|
|
free(new_partitions);
|
2017-09-26 14:42:28 +00:00
|
|
|
free(partitions_list);
|
2017-07-04 18:19:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2023-08-31 16:51:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gpt_set_bootable() - Set bootable flags for partitions
|
|
|
|
*
|
|
|
|
* Sets the bootable flag for any partition names in the comma separated list of
|
|
|
|
* partition names. Any partitions not in the list have their bootable flag
|
|
|
|
* cleared
|
|
|
|
*
|
|
|
|
* @desc: block device descriptor
|
|
|
|
* @name: Comma separated list of partition names
|
|
|
|
*
|
|
|
|
* @Return: '0' on success and -ve error on failure
|
|
|
|
*/
|
|
|
|
static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char disk_guid[UUID_STR_LEN + 1];
|
|
|
|
struct list_head *pos;
|
|
|
|
struct disk_part *curr;
|
|
|
|
struct disk_partition *partitions = NULL;
|
|
|
|
int part_count = 0;
|
|
|
|
int ret = get_disk_guid(blk_dev_desc, disk_guid);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = get_gpt_info(blk_dev_desc);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
part_count = ret;
|
|
|
|
partitions = malloc(sizeof(*partitions) * part_count);
|
|
|
|
if (!partitions) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy partitions and clear bootable flag */
|
|
|
|
part_count = 0;
|
|
|
|
list_for_each(pos, &disk_partitions) {
|
|
|
|
curr = list_entry(pos, struct disk_part, list);
|
|
|
|
partitions[part_count] = curr->gpt_part_info;
|
|
|
|
partitions[part_count].bootable &= ~PART_BOOTABLE;
|
|
|
|
part_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strtok(part_list, ",");
|
|
|
|
while (name) {
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < part_count; i++) {
|
|
|
|
if (strcmp((char *)partitions[i].name, name) == 0) {
|
|
|
|
partitions[i].bootable |= PART_BOOTABLE;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
printf("Warning: No partition matching '%s' found\n",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count);
|
|
|
|
|
|
|
|
out:
|
|
|
|
del_gpt_info();
|
|
|
|
|
|
|
|
if (partitions)
|
|
|
|
free(partitions);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-04 18:19:18 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-11 10:09:47 +00:00
|
|
|
/**
|
|
|
|
* do_gpt(): Perform GPT operations
|
|
|
|
*
|
|
|
|
* @param cmdtp - command name
|
|
|
|
* @param flag
|
|
|
|
* @param argc
|
|
|
|
* @param argv
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: zero on success; otherwise error
|
2012-12-11 10:09:47 +00:00
|
|
|
*/
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
|
2012-12-11 10:09:47 +00:00
|
|
|
{
|
|
|
|
int ret = CMD_RET_SUCCESS;
|
|
|
|
int dev = 0;
|
2013-10-04 16:53:04 +00:00
|
|
|
char *ep;
|
2016-02-29 22:25:34 +00:00
|
|
|
struct blk_desc *blk_dev_desc = NULL;
|
2012-12-11 10:09:47 +00:00
|
|
|
|
2017-07-04 18:19:18 +00:00
|
|
|
#ifndef CONFIG_CMD_GPT_RENAME
|
2015-11-20 07:06:17 +00:00
|
|
|
if (argc < 4 || argc > 5)
|
2017-07-04 18:19:18 +00:00
|
|
|
#else
|
|
|
|
if (argc < 4 || argc > 6)
|
|
|
|
#endif
|
2012-12-11 10:09:47 +00:00
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
2021-07-24 15:03:30 +00:00
|
|
|
dev = (int)dectoul(argv[3], &ep);
|
2015-11-20 07:06:17 +00:00
|
|
|
if (!ep || ep[0] != '\0') {
|
|
|
|
printf("'%s' is not a number\n", argv[3]);
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
2016-02-29 22:25:42 +00:00
|
|
|
blk_dev_desc = blk_get_dev(argv[2], dev);
|
2015-11-20 07:06:17 +00:00
|
|
|
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
|
|
|
|
2022-04-22 15:46:49 +00:00
|
|
|
if (strcmp(argv[1], "repair") == 0) {
|
|
|
|
printf("Repairing GPT: ");
|
|
|
|
ret = gpt_repair(blk_dev_desc);
|
|
|
|
} else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
|
2015-11-20 07:06:17 +00:00
|
|
|
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: ");
|
2020-11-09 06:19:16 +00:00
|
|
|
} else if ((strcmp(argv[1], "setenv") == 0)) {
|
|
|
|
ret = gpt_setenv(blk_dev_desc, argv[4]);
|
|
|
|
} else if ((strcmp(argv[1], "enumerate") == 0)) {
|
|
|
|
ret = gpt_enumerate(blk_dev_desc);
|
2017-06-25 23:43:23 +00:00
|
|
|
} else if (strcmp(argv[1], "guid") == 0) {
|
|
|
|
ret = do_disk_guid(blk_dev_desc, argv[4]);
|
2017-07-04 18:18:50 +00:00
|
|
|
#ifdef CONFIG_CMD_GPT_RENAME
|
|
|
|
} else if (strcmp(argv[1], "read") == 0) {
|
2021-02-26 18:17:33 +00:00
|
|
|
ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
|
2017-07-04 18:19:18 +00:00
|
|
|
} else if ((strcmp(argv[1], "swap") == 0) ||
|
|
|
|
(strcmp(argv[1], "rename") == 0)) {
|
|
|
|
ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
|
2023-08-31 16:51:38 +00:00
|
|
|
} else if ((strcmp(argv[1], "set-bootable") == 0)) {
|
|
|
|
ret = gpt_set_bootable(blk_dev_desc, argv[4]);
|
2017-07-04 18:18:50 +00:00
|
|
|
#endif
|
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"
|
2022-04-22 15:46:49 +00:00
|
|
|
" gpt repair mmc 0\n"
|
|
|
|
" - repair the GPT on the device\n"
|
2015-11-13 06:42:10 +00:00
|
|
|
" gpt write mmc 0 $partitions\n"
|
2020-11-09 06:19:16 +00:00
|
|
|
" - write the GPT to device\n"
|
2015-11-13 06:42:10 +00:00
|
|
|
" gpt verify mmc 0 $partitions\n"
|
2020-11-09 06:19:16 +00:00
|
|
|
" - verify the GPT on device against $partitions\n"
|
|
|
|
" gpt setenv mmc 0 $name\n"
|
|
|
|
" - setup environment variables for partition $name:\n"
|
|
|
|
" gpt_partition_addr, gpt_partition_size,\n"
|
2023-08-31 16:51:37 +00:00
|
|
|
" gpt_partition_name, gpt_partition_entry,\n"
|
|
|
|
" gpt_partition_bootable\n"
|
2020-11-09 06:19:16 +00:00
|
|
|
" gpt enumerate mmc 0\n"
|
|
|
|
" - store list of partitions to gpt_partition_list environment variable\n"
|
2019-04-30 02:53:46 +00:00
|
|
|
" gpt guid <interface> <dev>\n"
|
2017-06-25 23:43:23 +00:00
|
|
|
" - print disk GUID\n"
|
2019-04-30 02:53:46 +00:00
|
|
|
" gpt guid <interface> <dev> <varname>\n"
|
2017-06-25 23:43:23 +00:00
|
|
|
" - set environment variable to disk GUID\n"
|
|
|
|
" Example usage:\n"
|
|
|
|
" gpt guid mmc 0\n"
|
|
|
|
" gpt guid mmc 0 varname\n"
|
2017-07-04 18:19:18 +00:00
|
|
|
#ifdef CONFIG_CMD_GPT_RENAME
|
|
|
|
"gpt partition renaming commands:\n"
|
2021-02-26 18:17:33 +00:00
|
|
|
" gpt read <interface> <dev> [<varname>]\n"
|
2019-04-30 02:53:46 +00:00
|
|
|
" - read GPT into a data structure for manipulation\n"
|
2021-02-26 18:17:33 +00:00
|
|
|
" - read GPT partitions into environment variable\n"
|
2019-04-30 02:53:46 +00:00
|
|
|
" gpt swap <interface> <dev> <name1> <name2>\n"
|
2017-07-04 18:19:18 +00:00
|
|
|
" - change all partitions named name1 to name2\n"
|
|
|
|
" and vice-versa\n"
|
2019-04-30 02:53:46 +00:00
|
|
|
" gpt rename <interface> <dev> <part> <name>\n"
|
2017-07-04 18:19:18 +00:00
|
|
|
" - rename the specified partition\n"
|
2023-08-31 16:51:38 +00:00
|
|
|
" gpt set-bootable <interface> <dev> <list>\n"
|
|
|
|
" - make partition names in list bootable\n"
|
2017-07-04 18:19:18 +00:00
|
|
|
" Example usage:\n"
|
|
|
|
" gpt swap mmc 0 foo bar\n"
|
|
|
|
" gpt rename mmc 0 3 foo\n"
|
2023-08-31 16:51:38 +00:00
|
|
|
" gpt set-bootable mmc 0 boot_a,boot_b\n"
|
2017-07-04 18:19:18 +00:00
|
|
|
#endif
|
2012-12-11 10:09:47 +00:00
|
|
|
);
|