]> git.sur5r.net Git - u-boot/blobdiff - cmd/gpt.c
ARM: socfpga: Pull DRAM size from DT
[u-boot] / cmd / gpt.c
index dab47c71f9624540fed7dba2a4725221cb091899..638870352f406d11f5738f6739a2d6731c26468b 100644 (file)
--- 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,8 +8,6 @@
  * Copyright (C) 2012 Samsung Electronics
  * author: Lukasz Majewski <l.majewski@samsung.com>
  * author: Piotr Wilczek <p.wilczek@samsung.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -55,14 +54,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_GUID);
-               setenv(s, uuid_str);
+               env_set(s, uuid_str);
 
-               e = getenv(s);
+               e = env_get(s);
                if (e) {
                        debug("Set to random.\n");
                        ret = 0;
@@ -156,6 +155,25 @@ 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)
 {
@@ -171,10 +189,9 @@ static void del_gpt_info(void)
 static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
 {
        struct disk_part *newpart;
-       newpart = malloc(sizeof(*newpart));
+       newpart = calloc(1, sizeof(struct disk_part));
        if (!newpart)
                return ERR_PTR(-ENOMEM);
-       memset(newpart, '\0', sizeof(newpart));
 
        newpart->gpt_part_info.start = info->start;
        newpart->gpt_part_info.size = info->size;
@@ -234,25 +251,6 @@ static void print_gpt_info(void)
        }
 }
 
-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;
-}
-
 /*
  * create the string that upstream 'gpt write' command will accept as an
  * argument
@@ -283,14 +281,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=");
@@ -385,6 +383,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
        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);
@@ -402,6 +401,8 @@ 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)
+                       return -ENOMEM;
                gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
 #else
                free(str);
@@ -416,10 +417,14 @@ static int set_gpt_info(struct blk_desc *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';
 
@@ -433,6 +438,8 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 
        /* allocate memory for partitions */
        parts = calloc(sizeof(disk_partition_t), p_count);
+       if (parts == NULL)
+               return -ENOMEM;
 
        /* retrieve partitions data from string */
        for (i = 0; i < p_count; i++) {
@@ -454,12 +461,12 @@ static int set_gpt_info(struct blk_desc *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
@@ -469,13 +476,13 @@ static int set_gpt_info(struct blk_desc *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
@@ -487,11 +494,11 @@ static int set_gpt_info(struct blk_desc *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 */
@@ -617,7 +624,7 @@ static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
                return CMD_RET_FAILURE;
 
        if (namestr)
-               setenv(namestr, disk_guid);
+               env_set(namestr, disk_guid);
        else
                printf("%s\n", disk_guid);
 
@@ -625,6 +632,21 @@ static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
 }
 
 #ifdef CONFIG_CMD_GPT_RENAME
+/*
+ * There are 3 malloc() calls in set_gpt_info() and there is no info about which
+ * failed.
+ */
+static void set_gpt_cleanup(char **str_disk_guid,
+                           disk_partition_t **partitions)
+{
+#ifdef CONFIG_RANDOM_UUID
+       if (str_disk_guid)
+               free(str_disk_guid);
+#endif
+       if (partitions)
+               free(partitions);
+}
+
 static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
                               char *name1, char *name2)
 {
@@ -643,19 +665,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.
@@ -663,15 +693,23 @@ 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;
+       if (ret < 0) {
+               del_gpt_info();
+               free(partitions_list);
+               if (ret == -ENOMEM)
+                       set_gpt_cleanup(&str_disk_guid, &new_partitions);
+               else
+                       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);
@@ -685,21 +723,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) {
@@ -714,33 +755,50 @@ 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);
-       if (ret < 0)
-               return ret;
+       /*
+        * 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) {
+               del_gpt_info();
+               free(partitions_list);
+               if (ret == -ENOMEM)
+                       set_gpt_cleanup(&str_disk_guid, &new_partitions);
+               else
+                       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();
-
        del_gpt_info();
-       free(partitions_list);
-       free(str_disk_guid);
+ out:
        free(new_partitions);
+       free(str_disk_guid);
+       free(partitions_list);
        return ret;
 }
 #endif