X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=cmd%2Fgpt.c;h=05f5f943a8e11e0131a8d16cf7aff4652f07eff5;hb=09140113108541b95d340f3c7b6ee597d31ccc73;hp=d94d5530bc6fab6194a92cdd4b089dbbcf1f24af;hpb=82d72a1b9967cff4908f22c57536c3660f794401;p=oweals%2Fu-boot.git diff --git a/cmd/gpt.c b/cmd/gpt.c index d94d5530bc..05f5f943a8 100644 --- a/cmd/gpt.c +++ b/cmd/gpt.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0+ /* * cmd_gpt.c -- GPT (GUID Partition Table) handling command * @@ -7,22 +8,26 @@ * Copyright (C) 2012 Samsung Electronics * author: Lukasz Majewski * author: Piotr Wilczek - * - * SPDX-License-Identifier: GPL-2.0+ */ #include +#include +#include #include #include +#include #include #include +#include #include #include #include +#include +#include +#include +#include -#ifndef CONFIG_PARTITION_UUIDS -#error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled -#endif +static LIST_HEAD(disk_partitions); /** * extract_env(): Expand env name from string format '&{env_name}' @@ -54,14 +59,14 @@ static int extract_env(const char *str, char **env) memset(s + strlen(s) - 1, '\0', 1); memmove(s, s + 2, strlen(s) - 1); - e = getenv(s); + e = env_get(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); + gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID); + env_set(s, uuid_str); - e = getenv(s); + e = env_get(s); if (e) { debug("Set to random.\n"); ret = 0; @@ -155,6 +160,208 @@ static bool found_key(const char *str, const char *key) return result; } +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); + 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; +} + +#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); + } +} + +static struct disk_part *allocate_disk_part(struct disk_partition *info, + int partnum) +{ + struct disk_part *newpart; + newpart = calloc(1, sizeof(struct disk_part)); + 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; +#ifdef CONFIG_PARTITION_UUIDS + strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid, + UUID_STR_LEN); + /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */ + newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0'; +#endif + newpart->partnum = partnum; + + return newpart; +} + +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); +} + +static void print_gpt_info(void) +{ + struct list_head *pos; + struct disk_part *curr; + char partstartstr[16]; + char partsizestr[16]; + + list_for_each(pos, &disk_partitions) { + curr = list_entry(pos, struct disk_part, list); + 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); + + printf("Partition %d:\n", curr->partnum); + printf("Start %s, size %s\n", partstartstr, partsizestr); + 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, + curr->gpt_part_info.bootable & PART_BOOTABLE); +#ifdef CONFIG_PARTITION_UUIDS + printf("UUID %s\n", curr->gpt_part_info.uuid); +#endif + printf("\n"); + } +} + +/* + * 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); + sprintf(partstr, ",start=0x%llx", + (unsigned long long)curr->gpt_part_info.start * + curr->gpt_part_info.blksz); + /* one extra byte for NULL */ + strncat(partitions_list, partstr, PART_NAME_LEN + 1); + sprintf(partstr, ",size=0x%llx", + (unsigned long long)curr->gpt_part_info.size * + curr->gpt_part_info.blksz); + strncat(partitions_list, partstr, PART_NAME_LEN + 1); + + strcat(partitions_list, ",uuid="); + strncat(partitions_list, curr->gpt_part_info.uuid, + UUID_STR_LEN + 1); + strcat(partitions_list, ";"); + } + return 0; +} + +/* + * 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; + struct disk_partition info; + struct disk_part *new_disk_part; + + /* + * Always re-read partition info from device, in case + * it has changed + */ + INIT_LIST_HEAD(&disk_partitions); + + 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 */ +static int do_get_gpt_info(struct blk_desc *dev_desc) +{ + int ret; + + ret = get_gpt_info(dev_desc); + if (ret > 0) { + print_gpt_info(); + del_gpt_info(); + return 0; + } + return ret; +} +#endif + /** * set_gpt_info(): Fill partition information from string * function allocates memory, remember to free! @@ -168,19 +375,21 @@ static bool found_key(const char *str, const char *key) * @return - zero on success, otherwise error * */ -static int set_gpt_info(block_dev_desc_t *dev_desc, +static int set_gpt_info(struct blk_desc *dev_desc, const char *str_part, char **str_disk_guid, - disk_partition_t **partitions, + struct disk_partition **partitions, u8 *parts_count) { char *tok, *str, *s; int i; char *val, *p; int p_count; - disk_partition_t *parts; + struct disk_partition *parts; int errno = 0; uint64_t size_ll, start_ll; + lbaint_t offset = 0; + int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS); debug("%s: lba num: 0x%x %d\n", __func__, (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba); @@ -189,6 +398,8 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, return -1; str = strdup(str_part); + if (str == NULL) + return -ENOMEM; /* extract disk guid */ s = str; @@ -196,6 +407,8 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, if (!val) { #ifdef CONFIG_RANDOM_UUID *str_disk_guid = malloc(UUID_STR_LEN + 1); + if (*str_disk_guid == NULL) + return -ENOMEM; gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD); #else free(str); @@ -210,10 +423,14 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, /* Move s to first partition */ strsep(&s, ";"); } - if (strlen(s) == 0) + if (s == NULL) { + printf("Error: is the partitions string NULL-terminated?\n"); + return -EINVAL; + } + if (strnlen(s, max_str_part) == 0) return -3; - i = strlen(s) - 1; + i = strnlen(s, max_str_part) - 1; if (s[i] == ';') s[i] = '\0'; @@ -226,7 +443,9 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, } /* allocate memory for partitions */ - parts = calloc(sizeof(disk_partition_t), p_count); + parts = calloc(sizeof(struct disk_partition), p_count); + if (parts == NULL) + return -ENOMEM; /* retrieve partitions data from string */ for (i = 0; i < p_count; i++) { @@ -248,12 +467,12 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, } else { if (extract_env(val, &p)) p = val; - if (strlen(p) >= sizeof(parts[i].uuid)) { + if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) { printf("Wrong uuid format for partition %d\n", i); errno = -4; goto err; } - strcpy((char *)parts[i].uuid, p); + strncpy((char *)parts[i].uuid, p, max_str_part); free(val); } #ifdef CONFIG_PARTITION_TYPE_GUID @@ -263,13 +482,13 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, /* 'type' is optional */ if (extract_env(val, &p)) p = val; - if (strlen(p) >= sizeof(parts[i].type_guid)) { + if (strnlen(p, max_str_part) >= 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); + strncpy((char *)parts[i].type_guid, p, max_str_part); free(val); } #endif @@ -281,11 +500,11 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, } if (extract_env(val, &p)) p = val; - if (strlen(p) >= sizeof(parts[i].name)) { + if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) { errno = -4; goto err; } - strcpy((char *)parts[i].name, p); + strncpy((char *)parts[i].name, p, max_str_part); free(val); /* size */ @@ -296,8 +515,14 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, } if (extract_env(val, &p)) p = val; - size_ll = ustrtoull(p, &p, 0); - parts[i].size = lldiv(size_ll, dev_desc->blksz); + if ((strcmp(p, "-") == 0)) { + /* Let part efi module to auto extend the size */ + parts[i].size = 0; + } else { + size_ll = ustrtoull(p, &p, 0); + parts[i].size = lldiv(size_ll, dev_desc->blksz); + } + free(val); /* start address */ @@ -310,9 +535,11 @@ static int set_gpt_info(block_dev_desc_t *dev_desc, free(val); } + offset += parts[i].size + parts[i].start; + /* bootable */ if (found_key(tok, "bootable")) - parts[i].bootable = 1; + parts[i].bootable = PART_BOOTABLE; } *parts_count = p_count; @@ -328,12 +555,12 @@ err: return errno; } -static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part) +static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part) { int ret; char *str_disk_guid; u8 part_count = 0; - disk_partition_t *partitions = NULL; + struct disk_partition *partitions = NULL; /* fill partitions */ ret = set_gpt_info(blk_dev_desc, str_part, @@ -356,11 +583,11 @@ static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part) return ret; } -static int gpt_verify(block_dev_desc_t *blk_dev_desc, const char *str_part) +static int gpt_verify(struct blk_desc *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; + struct disk_partition *partitions = NULL; gpt_entry *gpt_pte = NULL; char *str_disk_guid; u8 part_count = 0; @@ -393,6 +620,170 @@ static int gpt_verify(block_dev_desc_t *blk_dev_desc, const char *str_part) return ret; } +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) + env_set(namestr, disk_guid); + else + printf("%s\n", disk_guid); + + return ret; +} + +#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; + struct disk_partition *new_partitions = NULL; + char disk_guid[UUID_STR_LEN + 1]; + char *partitions_list, *str_disk_guid = NULL; + 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; + /* + * Allocates disk_partitions, requiring matching call to del_gpt_info() + * if successful. + */ + numparts = get_gpt_info(dev_desc); + if (numparts <= 0) + return numparts ? numparts : -ENODEV; + + 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) { + free(partitions_list); + return ret; + } + /* + * 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)); + + /* set_gpt_info allocates new_partitions and str_disk_guid */ + ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, + &new_partitions, &part_count); + if (ret < 0) + goto out; + + 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); + ret = -EINVAL; + goto out; + } + 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"); + ret = -EINVAL; + goto out; + } + } else { /* rename */ + if (strlen(name2) > PART_NAME_LEN) { + printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); + ret = -EINVAL; + goto out; + } + partnum = (int)simple_strtol(name1, NULL, 10); + if ((partnum < 0) || (partnum > numparts)) { + printf("Illegal partition number %s\n", name1); + ret = -EINVAL; + goto out; + } + ret = part_get_info(dev_desc, partnum, new_partitions); + if (ret < 0) + goto out; + + /* 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) + goto out; + 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); + /* + * Even though valid pointers are here passed into set_gpt_info(), + * it mallocs again, and there's no way to tell which failed. + */ + if (ret < 0) + goto out; + + 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"); + goto out; + } + + debug("Reading back new partition table\n"); + /* + * Empty the existing disk_partitions list, as otherwise the memory in + * the original list is unreachable. + */ + del_gpt_info(); + numparts = get_gpt_info(dev_desc); + if (numparts <= 0) { + ret = numparts ? numparts : -ENODEV; + goto out; + } + printf("new partition table with %d partitions is:\n", numparts); + print_gpt_info(); + out: + del_gpt_info(); +#ifdef CONFIG_RANDOM_UUID + free(str_disk_guid); +#endif + free(new_partitions); + free(partitions_list); + return ret; +} +#endif + /** * do_gpt(): Perform GPT operations * @@ -403,14 +794,18 @@ static int gpt_verify(block_dev_desc_t *blk_dev_desc, const char *str_part) * * @return zero on success; otherwise error */ -static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { int ret = CMD_RET_SUCCESS; int dev = 0; char *ep; - block_dev_desc_t *blk_dev_desc = NULL; + struct blk_desc *blk_dev_desc = NULL; +#ifndef CONFIG_CMD_GPT_RENAME if (argc < 4 || argc > 5) +#else + if (argc < 4 || argc > 6) +#endif return CMD_RET_USAGE; dev = (int)simple_strtoul(argv[3], &ep, 10); @@ -418,7 +813,7 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) printf("'%s' is not a number\n", argv[3]); return CMD_RET_USAGE; } - blk_dev_desc = get_dev(argv[2], dev); + blk_dev_desc = blk_get_dev(argv[2], dev); if (!blk_dev_desc) { printf("%s: %s dev %d NOT available\n", __func__, argv[2], dev); @@ -431,6 +826,15 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) } else if ((strcmp(argv[1], "verify") == 0)) { ret = gpt_verify(blk_dev_desc, argv[4]); printf("Verify GPT: "); + } else if (strcmp(argv[1], "guid") == 0) { + ret = do_disk_guid(blk_dev_desc, argv[4]); +#ifdef CONFIG_CMD_GPT_RENAME + } else if (strcmp(argv[1], "read") == 0) { + ret = do_get_gpt_info(blk_dev_desc); + } 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]); +#endif } else { return CMD_RET_USAGE; } @@ -453,4 +857,24 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, " Example usage:\n" " gpt write mmc 0 $partitions\n" " gpt verify mmc 0 $partitions\n" + " gpt guid \n" + " - print disk GUID\n" + " gpt guid \n" + " - set environment variable to disk GUID\n" + " Example usage:\n" + " gpt guid mmc 0\n" + " gpt guid mmc 0 varname\n" +#ifdef CONFIG_CMD_GPT_RENAME + "gpt partition renaming commands:\n" + " gpt read \n" + " - read GPT into a data structure for manipulation\n" + " gpt swap \n" + " - change all partitions named name1 to name2\n" + " and vice-versa\n" + " gpt rename \n" + " - rename the specified partition\n" + " Example usage:\n" + " gpt swap mmc 0 foo bar\n" + " gpt rename mmc 0 3 foo\n" +#endif );