X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=cmd%2Fgpt.c;h=964702bad43e91281d4e0a937143babdd5250dd0;hb=4e5c4683b7a54090323043ab9a67772baeecb1b1;hp=d4406e3120b50dbac4c77312fa3f3e914d4105e9;hpb=f66bc0e0beeff7fa27c29f740d2f314d63b3346d;p=oweals%2Fu-boot.git diff --git a/cmd/gpt.c b/cmd/gpt.c index d4406e3120..964702bad4 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,11 +8,10 @@ * Copyright (C) 2012 Samsung Electronics * author: Lukasz Majewski * author: Piotr Wilczek - * - * SPDX-License-Identifier: GPL-2.0+ */ #include +#include #include #include #include @@ -282,14 +282,14 @@ static int create_gpt_partitions_list(int numparts, const char *guid, strcat(partitions_list, "name="); strncat(partitions_list, (const char *)curr->gpt_part_info.name, PART_NAME_LEN + 1); - strcat(partitions_list, ",start="); - prettyprint_part_size(partstr, (unsigned long)curr->gpt_part_info.start, - (unsigned long) curr->gpt_part_info.blksz); + 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); - strcat(partitions_list, ",size="); - prettyprint_part_size(partstr, curr->gpt_part_info.size, - curr->gpt_part_info.blksz); + 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="); @@ -402,7 +402,7 @@ static int set_gpt_info(struct blk_desc *dev_desc, if (!val) { #ifdef CONFIG_RANDOM_UUID *str_disk_guid = malloc(UUID_STR_LEN + 1); - if (str_disk_guid == NULL) + if (*str_disk_guid == NULL) return -ENOMEM; gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD); #else @@ -640,7 +640,7 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, struct disk_part *curr; disk_partition_t *new_partitions = NULL; char disk_guid[UUID_STR_LEN + 1]; - char *partitions_list, *str_disk_guid; + char *partitions_list, *str_disk_guid = NULL; u8 part_count = 0; int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0; @@ -651,19 +651,27 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, 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 == NULL) + 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) + 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. @@ -671,15 +679,17 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, 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) - return ret; + 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); - return -EINVAL; + ret = -EINVAL; + goto out; } list_for_each(pos, &disk_partitions) { curr = list_entry(pos, struct disk_part, list); @@ -693,21 +703,24 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, } if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) { printf("Cannot swap partition names except in pairs.\n"); - return -EINVAL; + ret = -EINVAL; + goto out; } } else { /* rename */ if (strlen(name2) > PART_NAME_LEN) { printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); - return -EINVAL; + ret = -EINVAL; + goto out; } partnum = (int)simple_strtol(name1, NULL, 10); if ((partnum < 0) || (partnum > numparts)) { printf("Illegal partition number %s\n", name1); - return -EINVAL; + ret = -EINVAL; + goto out; } ret = part_get_info(dev_desc, partnum, new_partitions); if (ret < 0) - return ret; + goto out; /* U-Boot partition numbering starts at 1 */ list_for_each(pos, &disk_partitions) { @@ -722,33 +735,48 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); if (ret < 0) - return ret; + 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) - return ret; + 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"); - return ret; + 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) - return numparts ? numparts : -ENODEV; + 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 + if (str_disk_guid) + free(str_disk_guid); +#endif + if (new_partitions) + free(new_partitions); free(partitions_list); - free(str_disk_guid); - free(new_partitions); return ret; } #endif @@ -826,21 +854,21 @@ 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" - " read \n" - " - read GPT into a data structure for manipulation\n" - " guid \n" + " gpt guid \n" " - print disk GUID\n" - " 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 swap \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" + " gpt rename \n" " - rename the specified partition\n" " Example usage:\n" " gpt swap mmc 0 foo bar\n"