1 // SPDX-License-Identifier: GPL-2.0+
3 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
8 * Copyright (C) 2012 Samsung Electronics
24 #include <linux/ctype.h>
27 #include <linux/compat.h>
28 #include <linux/err.h>
29 #include <linux/sizes.h>
32 static LIST_HEAD(disk_partitions);
35 * extract_env(): Expand env name from string format '&{env_name}'
36 * and return pointer to the env (if the env is set)
38 * @param str - pointer to string
39 * @param env - pointer to pointer to extracted env
41 * Return: - zero on successful expand and env is set
43 static int extract_env(const char *str, char **env)
47 #ifdef CONFIG_RANDOM_UUID
48 char uuid_str[UUID_STR_LEN + 1];
51 if (!str || strlen(str) < 4)
54 if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
61 memset(s + strlen(s) - 1, '\0', 1);
62 memmove(s, s + 2, strlen(s) - 1);
66 #ifdef CONFIG_RANDOM_UUID
67 debug("%s unset. ", str);
68 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
73 debug("Set to random.\n");
76 debug("Can't get random UUID.\n");
79 debug("%s unset.\n", str);
82 debug("%s get from environment.\n", str);
93 * extract_val(): Extract value from a key=value pair list (comma separated).
94 * Only value for the given key is returend.
95 * Function allocates memory for the value, remember to free!
97 * @param str - pointer to string with key=values pairs
98 * @param key - pointer to the key to search for
100 * Return: - pointer to allocated string with the value
102 static char *extract_val(const char *str, const char *key)
108 strcopy = strdup(str);
120 if (strcmp(k, key) == 0) {
132 * found_key(): Found key without value in parameter list (comma separated).
134 * @param str - pointer to string with key
135 * @param key - pointer to the key to search for
137 * Return: - true on found key
139 static bool found_key(const char *str, const char *key)
145 strcopy = strdup(str);
154 if (strcmp(k, key) == 0) {
166 * calc_parts_list_len() - get size of partition table description
168 * @numparts: number of partitions
169 * Return: string size including terminating NUL
171 static int calc_parts_list_len(int numparts)
173 /* number of hexadecimal digits of the lbaint_t representation */
174 const int lbaint_size = 2 * sizeof(lbaint_t);
177 /* media description including terminating NUL */
178 partlistlen = strlen("uuid_disk=;") + UUID_STR_LEN + 1;
179 /* per-partition descriptions; numparts */
180 partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN);
181 /* see part.h for definition of struct disk_partition */
182 partlistlen += numparts * (strlen("start=0x,") + lbaint_size);
183 partlistlen += numparts * (strlen("size=0x,") + lbaint_size);
184 if (IS_ENABLED(CONFIG_PARTITION_UUIDS))
185 partlistlen += numparts * (strlen("uuid=,") + UUID_STR_LEN);
186 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
187 partlistlen += numparts * (strlen("type=;") + UUID_STR_LEN);
188 debug("Length of partitions_list is %d for %d partitions\n",
189 partlistlen, numparts);
193 #ifdef CONFIG_CMD_GPT_RENAME
194 static void del_gpt_info(void)
196 struct list_head *pos = &disk_partitions;
197 struct disk_part *curr;
198 while (!list_empty(pos)) {
199 curr = list_entry(pos->next, struct disk_part, list);
205 static struct disk_part *allocate_disk_part(struct disk_partition *info,
208 struct disk_part *newpart;
209 newpart = calloc(1, sizeof(struct disk_part));
211 return ERR_PTR(-ENOMEM);
213 newpart->gpt_part_info.start = info->start;
214 newpart->gpt_part_info.size = info->size;
215 newpart->gpt_part_info.blksz = info->blksz;
216 strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
218 newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
219 strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
221 newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
222 newpart->gpt_part_info.bootable = info->bootable;
223 if (IS_ENABLED(CONFIG_PARTITION_UUIDS))
224 disk_partition_set_uuid(&newpart->gpt_part_info,
225 disk_partition_uuid(info));
226 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
227 disk_partition_set_type_guid(&newpart->gpt_part_info,
228 disk_partition_type_guid(info));
229 newpart->partnum = partnum;
234 static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
237 unsigned long long partbytes, partmegabytes;
239 partbytes = partsize * blksize;
240 partmegabytes = lldiv(partbytes, SZ_1M);
241 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
244 static void print_gpt_info(void)
246 struct list_head *pos;
247 struct disk_part *curr;
248 char partstartstr[16];
249 char partsizestr[16];
251 list_for_each(pos, &disk_partitions) {
252 curr = list_entry(pos, struct disk_part, list);
253 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
254 curr->gpt_part_info.blksz);
255 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
256 curr->gpt_part_info.blksz);
258 printf("Partition %d:\n", curr->partnum);
259 printf("Start %s, size %s\n", partstartstr, partsizestr);
260 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
261 curr->gpt_part_info.name);
262 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
263 curr->gpt_part_info.bootable & PART_BOOTABLE);
264 if (CONFIG_IS_ENABLED(PARTITION_UUIDS))
266 disk_partition_uuid(&curr->gpt_part_info));
267 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
268 printf("Type GUID %s\n",
269 disk_partition_type_guid(&curr->gpt_part_info));
275 * create the string that upstream 'gpt write' command will accept as an
278 * From doc/README.gpt, Format of partitions layout:
279 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
280 * name=kernel,size=60MiB,uuid=...;"
281 * The fields 'name' and 'size' are mandatory for every partition.
282 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
283 * are optional if CONFIG_RANDOM_UUID is enabled.
285 static int create_gpt_partitions_list(int numparts, const char *guid,
286 char *partitions_list)
288 struct list_head *pos;
289 struct disk_part *curr;
290 char partstr[PART_NAME_LEN + 1];
292 if (!partitions_list)
295 strcpy(partitions_list, "uuid_disk=");
296 strncat(partitions_list, guid, UUID_STR_LEN + 1);
297 strcat(partitions_list, ";");
299 list_for_each(pos, &disk_partitions) {
300 curr = list_entry(pos, struct disk_part, list);
301 strcat(partitions_list, "name=");
302 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
304 sprintf(partstr, ",start=0x%llx",
305 (unsigned long long)curr->gpt_part_info.start *
306 curr->gpt_part_info.blksz);
307 /* one extra byte for NULL */
308 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
309 sprintf(partstr, ",size=0x%llx",
310 (unsigned long long)curr->gpt_part_info.size *
311 curr->gpt_part_info.blksz);
312 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
314 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) {
315 strcat(partitions_list, ",type=");
316 strncat(partitions_list,
317 disk_partition_type_guid(&curr->gpt_part_info),
320 if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
321 strcat(partitions_list, ",uuid=");
322 strncat(partitions_list,
323 disk_partition_uuid(&curr->gpt_part_info),
326 if (curr->gpt_part_info.bootable & PART_BOOTABLE)
327 strcat(partitions_list, ",bootable");
328 strcat(partitions_list, ";");
334 * read partition info into disk_partitions list where
335 * it can be printed or modified
337 static int get_gpt_info(struct blk_desc *dev_desc)
339 /* start partition numbering at 1, as U-Boot does */
340 int valid_parts = 0, p, ret;
341 struct disk_partition info;
342 struct disk_part *new_disk_part;
345 * Always re-read partition info from device, in case
348 INIT_LIST_HEAD(&disk_partitions);
350 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
351 ret = part_get_info(dev_desc, p, &info);
355 /* Add 1 here because counter is zero-based but p1 is
356 the first partition */
357 new_disk_part = allocate_disk_part(&info, valid_parts+1);
358 if (IS_ERR(new_disk_part))
361 list_add_tail(&new_disk_part->list, &disk_partitions);
364 if (valid_parts == 0) {
365 printf("** No valid partitions found **\n");
370 if (valid_parts >= 1)
375 /* a wrapper to test get_gpt_info */
376 static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr)
380 numparts = get_gpt_info(dev_desc);
384 char disk_guid[UUID_STR_LEN + 1];
385 char *partitions_list;
389 ret = get_disk_guid(dev_desc, disk_guid);
393 partlistlen = calc_parts_list_len(numparts);
394 partitions_list = malloc(partlistlen);
395 if (!partitions_list) {
399 memset(partitions_list, '\0', partlistlen);
401 ret = create_gpt_partitions_list(numparts, disk_guid,
404 printf("Error: Could not create partition list string!\n");
406 env_set(namestr, partitions_list);
408 free(partitions_list);
420 * set_gpt_info(): Fill partition information from string
421 * function allocates memory, remember to free!
423 * @param dev_desc - pointer block device descriptor
424 * @param str_part - pointer to string with partition information
425 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
426 * @param partitions - pointer to pointer to allocated partitions array
427 * @param parts_count - number of partitions
429 * Return: - zero on success, otherwise error
432 static int set_gpt_info(struct blk_desc *dev_desc,
433 const char *str_part,
434 char **str_disk_guid,
435 struct disk_partition **partitions,
442 struct disk_partition *parts;
444 uint64_t size_ll, start_ll;
446 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
448 debug("%s: lba num: 0x%x %d\n", __func__,
449 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
451 if (str_part == NULL)
454 str = strdup(str_part);
458 /* extract disk guid */
460 val = extract_val(str, "uuid_disk");
462 #ifdef CONFIG_RANDOM_UUID
463 *str_disk_guid = malloc(UUID_STR_LEN + 1);
464 if (*str_disk_guid == NULL)
466 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
472 val = strsep(&val, ";");
473 if (extract_env(val, &p))
475 *str_disk_guid = strdup(p);
477 /* Move s to first partition */
481 printf("Error: is the partitions string NULL-terminated?\n");
484 if (strnlen(s, max_str_part) == 0)
487 i = strnlen(s, max_str_part) - 1;
491 /* calculate expected number of partitions */
499 /* allocate memory for partitions */
500 parts = calloc(sizeof(struct disk_partition), p_count);
504 /* retrieve partitions data from string */
505 for (i = 0; i < p_count; i++) {
506 tok = strsep(&s, ";");
512 val = extract_val(tok, "uuid");
514 /* 'uuid' is optional if random uuid's are enabled */
515 #ifdef CONFIG_RANDOM_UUID
516 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
522 if (extract_env(val, &p))
524 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
525 printf("Wrong uuid format for partition %d\n", i);
529 strncpy((char *)parts[i].uuid, p, max_str_part);
532 #ifdef CONFIG_PARTITION_TYPE_GUID
534 val = extract_val(tok, "type");
536 /* 'type' is optional */
537 if (extract_env(val, &p))
539 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
540 printf("Wrong type guid format for partition %d\n",
545 strncpy((char *)parts[i].type_guid, p, max_str_part);
550 val = extract_val(tok, "name");
551 if (!val) { /* name is mandatory */
555 if (extract_env(val, &p))
557 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
561 strncpy((char *)parts[i].name, p, max_str_part);
565 val = extract_val(tok, "size");
566 if (!val) { /* 'size' is mandatory */
570 if (extract_env(val, &p))
572 if ((strcmp(p, "-") == 0)) {
573 /* Let part efi module to auto extend the size */
576 size_ll = ustrtoull(p, &p, 0);
577 parts[i].size = lldiv(size_ll, dev_desc->blksz);
583 val = extract_val(tok, "start");
584 if (val) { /* start address is optional */
585 if (extract_env(val, &p))
587 start_ll = ustrtoull(p, &p, 0);
588 parts[i].start = lldiv(start_ll, dev_desc->blksz);
592 offset += parts[i].size + parts[i].start;
595 if (found_key(tok, "bootable"))
596 parts[i].bootable = PART_BOOTABLE;
599 *parts_count = p_count;
606 free(*str_disk_guid);
612 static int gpt_repair(struct blk_desc *blk_dev_desc)
616 ret = gpt_repair_headers(blk_dev_desc);
621 static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
626 struct disk_partition *partitions = NULL;
628 /* fill partitions */
629 ret = set_gpt_info(blk_dev_desc, str_part,
630 &str_disk_guid, &partitions, &part_count);
633 printf("No partition list provided\n");
635 printf("Missing disk guid\n");
636 if ((ret == -3) || (ret == -4))
637 printf("Partition list incomplete\n");
641 /* save partitions layout to disk */
642 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
649 static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
651 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
652 blk_dev_desc->blksz);
653 struct disk_partition *partitions = NULL;
654 gpt_entry *gpt_pte = NULL;
659 /* fill partitions */
660 ret = set_gpt_info(blk_dev_desc, str_part,
661 &str_disk_guid, &partitions, &part_count);
664 printf("No partition list provided - only basic check\n");
665 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
670 printf("Missing disk guid\n");
671 if ((ret == -3) || (ret == -4))
672 printf("Partition list incomplete\n");
676 /* Check partition layout with provided pattern */
677 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
687 * gpt_enumerate() - Enumerate partition names into environment variable.
689 * Enumerate partition names. Partition names are stored in gpt_partition_list
690 * environment variable. Each partition name is delimited by space.
692 * @desc: block device descriptor
694 * @Return: '0' on success and -ve error on failure
696 static int gpt_enumerate(struct blk_desc *desc)
698 struct part_driver *first_drv, *part_drv;
699 int str_len = 0, tmp_len;
700 char part_list[2048];
705 n_drvs = part_driver_get_count();
707 printf("Failed to get partition driver count\n");
711 first_drv = part_driver_get_first();
712 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
713 struct disk_partition pinfo;
717 if (part_drv->test(desc))
720 for (i = 1; i < part_drv->max_entries; i++) {
721 ret = part_drv->get_info(desc, i, &pinfo);
725 ptr = &part_list[str_len];
726 tmp_len = strlen((const char *)pinfo.name);
730 if (str_len > sizeof(part_list)) {
731 printf("Error insufficient memory\n");
734 strcpy(ptr, (const char *)pinfo.name);
735 /* One byte for space(" ") delimiter */
739 part_list[strlen(part_list) - 1] = 0;
742 debug("setenv gpt_partition_list %s\n", part_list);
744 return env_set("gpt_partition_list", part_list);
748 * gpt_setenv_part_variables() - setup partition environmental variables
750 * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
751 * and gpt_partition_size, gpt_partition_bootable environment variables.
753 * @pinfo: pointer to disk partition
754 * @i: partition entry
756 * @Return: '0' on success and -ENOENT on failure
758 static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
762 ret = env_set_hex("gpt_partition_addr", pinfo->start);
766 ret = env_set_hex("gpt_partition_size", pinfo->size);
770 ret = env_set_hex("gpt_partition_entry", i);
774 ret = env_set("gpt_partition_name", (const char *)pinfo->name);
778 ret = env_set_ulong("gpt_partition_bootable", !!(pinfo->bootable & PART_BOOTABLE));
789 * gpt_setenv() - Dynamically setup environment variables.
791 * Dynamically setup environment variables for name, index, offset and size
792 * for partition in GPT table after running "gpt setenv" for a partition name.
794 * @desc: block device descriptor
795 * @name: partition name
797 * @Return: '0' on success and -ve err on failure
799 static int gpt_setenv(struct blk_desc *desc, const char *name)
801 struct part_driver *first_drv, *part_drv;
805 n_drvs = part_driver_get_count();
807 printf("Failed to get partition driver count\n");
811 first_drv = part_driver_get_first();
812 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
813 struct disk_partition pinfo;
816 for (i = 1; i < part_drv->max_entries; i++) {
817 ret = part_drv->get_info(desc, i, &pinfo);
821 if (!strcmp(name, (const char *)pinfo.name)) {
822 /* match found, setup environment variables */
823 ret = gpt_setenv_part_variables(&pinfo, i);
836 static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
839 char disk_guid[UUID_STR_LEN + 1];
841 ret = get_disk_guid(dev_desc, disk_guid);
843 return CMD_RET_FAILURE;
846 env_set(namestr, disk_guid);
848 printf("%s\n", disk_guid);
853 #ifdef CONFIG_CMD_GPT_RENAME
854 static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
855 char *name1, char *name2)
857 struct list_head *pos;
858 struct disk_part *curr;
859 struct disk_partition *new_partitions = NULL;
860 char disk_guid[UUID_STR_LEN + 1];
861 char *partitions_list, *str_disk_guid = NULL;
863 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
865 if (!subcomm || !name1 || !name2 ||
866 (strcmp(subcomm, "swap") && strcmp(subcomm, "rename") &&
867 strcmp(subcomm, "transpose")))
870 ret = get_disk_guid(dev_desc, disk_guid);
874 * Allocates disk_partitions, requiring matching call to del_gpt_info()
877 numparts = get_gpt_info(dev_desc);
879 return numparts ? numparts : -ENODEV;
881 partlistlen = calc_parts_list_len(numparts);
882 partitions_list = malloc(partlistlen);
883 if (!partitions_list) {
887 memset(partitions_list, '\0', partlistlen);
889 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
891 free(partitions_list);
895 * Uncomment the following line to print a string that 'gpt write'
896 * or 'gpt verify' will accept as input.
898 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
899 (unsigned)strlen(partitions_list));
901 /* set_gpt_info allocates new_partitions and str_disk_guid */
902 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
903 &new_partitions, &part_count);
907 if (!strcmp(subcomm, "swap")) {
908 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
909 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
913 list_for_each(pos, &disk_partitions) {
914 curr = list_entry(pos, struct disk_part, list);
915 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
916 strcpy((char *)curr->gpt_part_info.name, name2);
918 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
919 strcpy((char *)curr->gpt_part_info.name, name1);
923 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
924 printf("Cannot swap partition names except in pairs.\n");
928 } else if (!strcmp(subcomm, "transpose")) {
930 struct disk_partition* first = NULL;
931 struct disk_partition* second= NULL;
932 struct disk_partition tmp_part;
934 idx1 = simple_strtoul(name1, NULL, 10);
935 idx2 = simple_strtoul(name2, NULL, 10);
937 printf("Cannot swap partition with itself\n");
942 list_for_each(pos, &disk_partitions) {
943 curr = list_entry(pos, struct disk_part, list);
944 if (curr->partnum == idx1)
945 first = &curr->gpt_part_info;
946 else if (curr->partnum == idx2)
947 second = &curr->gpt_part_info;
950 printf("Illegal partition number %s\n", name1);
955 printf("Illegal partition number %s\n", name2);
963 } else { /* rename */
964 if (strlen(name2) > PART_NAME_LEN) {
965 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
969 partnum = (int)simple_strtol(name1, NULL, 10);
970 if ((partnum < 0) || (partnum > numparts)) {
971 printf("Illegal partition number %s\n", name1);
975 ret = part_get_info(dev_desc, partnum, new_partitions);
979 /* U-Boot partition numbering starts at 1 */
980 list_for_each(pos, &disk_partitions) {
981 curr = list_entry(pos, struct disk_part, list);
983 strcpy((char *)curr->gpt_part_info.name, name2);
990 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
993 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
994 (unsigned)strlen(partitions_list));
996 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
997 &new_partitions, &part_count);
999 * Even though valid pointers are here passed into set_gpt_info(),
1000 * it mallocs again, and there's no way to tell which failed.
1005 debug("Writing new partition table\n");
1006 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
1008 printf("Writing new partition table failed\n");
1012 debug("Reading back new partition table\n");
1014 * Empty the existing disk_partitions list, as otherwise the memory in
1015 * the original list is unreachable.
1018 numparts = get_gpt_info(dev_desc);
1019 if (numparts <= 0) {
1020 ret = numparts ? numparts : -ENODEV;
1023 printf("new partition table with %d partitions is:\n", numparts);
1027 #ifdef CONFIG_RANDOM_UUID
1028 free(str_disk_guid);
1030 free(new_partitions);
1031 free(partitions_list);
1036 * gpt_set_bootable() - Set bootable flags for partitions
1038 * Sets the bootable flag for any partition names in the comma separated list of
1039 * partition names. Any partitions not in the list have their bootable flag
1042 * @desc: block device descriptor
1043 * @name: Comma separated list of partition names
1045 * @Return: '0' on success and -ve error on failure
1047 static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list)
1050 char disk_guid[UUID_STR_LEN + 1];
1051 struct list_head *pos;
1052 struct disk_part *curr;
1053 struct disk_partition *partitions = NULL;
1055 int ret = get_disk_guid(blk_dev_desc, disk_guid);
1060 ret = get_gpt_info(blk_dev_desc);
1065 partitions = malloc(sizeof(*partitions) * part_count);
1071 /* Copy partitions and clear bootable flag */
1073 list_for_each(pos, &disk_partitions) {
1074 curr = list_entry(pos, struct disk_part, list);
1075 partitions[part_count] = curr->gpt_part_info;
1076 partitions[part_count].bootable &= ~PART_BOOTABLE;
1080 name = strtok(part_list, ",");
1084 for (int i = 0; i < part_count; i++) {
1085 if (strcmp((char *)partitions[i].name, name) == 0) {
1086 partitions[i].bootable |= PART_BOOTABLE;
1092 printf("Warning: No partition matching '%s' found\n",
1096 name = strtok(NULL, ",");
1099 ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count);
1112 * do_gpt(): Perform GPT operations
1114 * @param cmdtp - command name
1119 * Return: zero on success; otherwise error
1121 static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1123 int ret = CMD_RET_SUCCESS;
1126 struct blk_desc *blk_dev_desc = NULL;
1128 #ifndef CONFIG_CMD_GPT_RENAME
1129 if (argc < 4 || argc > 5)
1131 if (argc < 4 || argc > 6)
1133 return CMD_RET_USAGE;
1135 dev = (int)dectoul(argv[3], &ep);
1136 if (!ep || ep[0] != '\0') {
1137 printf("'%s' is not a number\n", argv[3]);
1138 return CMD_RET_USAGE;
1140 blk_dev_desc = blk_get_dev(argv[2], dev);
1141 if (!blk_dev_desc) {
1142 printf("%s: %s dev %d NOT available\n",
1143 __func__, argv[2], dev);
1144 return CMD_RET_FAILURE;
1147 if (strcmp(argv[1], "repair") == 0) {
1148 printf("Repairing GPT: ");
1149 ret = gpt_repair(blk_dev_desc);
1150 } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
1151 printf("Writing GPT: ");
1152 ret = gpt_default(blk_dev_desc, argv[4]);
1153 } else if ((strcmp(argv[1], "verify") == 0)) {
1154 ret = gpt_verify(blk_dev_desc, argv[4]);
1155 printf("Verify GPT: ");
1156 } else if ((strcmp(argv[1], "setenv") == 0)) {
1157 ret = gpt_setenv(blk_dev_desc, argv[4]);
1158 } else if ((strcmp(argv[1], "enumerate") == 0)) {
1159 ret = gpt_enumerate(blk_dev_desc);
1160 } else if (strcmp(argv[1], "guid") == 0) {
1161 ret = do_disk_guid(blk_dev_desc, argv[4]);
1162 #ifdef CONFIG_CMD_GPT_RENAME
1163 } else if (strcmp(argv[1], "read") == 0) {
1164 ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
1165 } else if ((strcmp(argv[1], "swap") == 0) ||
1166 (strcmp(argv[1], "rename") == 0) ||
1167 (strcmp(argv[1], "transpose") == 0)) {
1168 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
1169 } else if ((strcmp(argv[1], "set-bootable") == 0)) {
1170 ret = gpt_set_bootable(blk_dev_desc, argv[4]);
1173 return CMD_RET_USAGE;
1178 return CMD_RET_FAILURE;
1181 printf("success!\n");
1182 return CMD_RET_SUCCESS;
1185 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
1186 "GUID Partition Table",
1187 "<command> <interface> <dev> <partitions_list>\n"
1188 " - GUID partition table restoration and validity check\n"
1189 " Restore or verify GPT information on a device connected\n"
1192 " gpt repair mmc 0\n"
1193 " - repair the GPT on the device\n"
1194 " gpt write mmc 0 $partitions\n"
1195 " - write the GPT to device\n"
1196 " gpt verify mmc 0 $partitions\n"
1197 " - verify the GPT on device against $partitions\n"
1198 " gpt setenv mmc 0 $name\n"
1199 " - setup environment variables for partition $name:\n"
1200 " gpt_partition_addr, gpt_partition_size,\n"
1201 " gpt_partition_name, gpt_partition_entry,\n"
1202 " gpt_partition_bootable\n"
1203 " gpt enumerate mmc 0\n"
1204 " - store list of partitions to gpt_partition_list environment variable\n"
1205 " gpt guid <interface> <dev>\n"
1206 " - print disk GUID\n"
1207 " gpt guid <interface> <dev> <varname>\n"
1208 " - set environment variable to disk GUID\n"
1211 " gpt guid mmc 0 varname\n"
1212 #ifdef CONFIG_CMD_GPT_RENAME
1213 "gpt partition renaming commands:\n"
1214 " gpt read <interface> <dev> [<varname>]\n"
1215 " - read GPT into a data structure for manipulation\n"
1216 " - read GPT partitions into environment variable\n"
1217 " gpt swap <interface> <dev> <name1> <name2>\n"
1218 " - change all partitions named name1 to name2\n"
1220 " gpt transpose <interface> <dev> <part1> <part2>\n"
1221 " - Swap the order of the entries for part1 and part2 in the partition table\n"
1222 " gpt rename <interface> <dev> <part> <name>\n"
1223 " - rename the specified partition\n"
1224 " gpt set-bootable <interface> <dev> <list>\n"
1225 " - make partition names in list bootable\n"
1227 " gpt swap mmc 0 foo bar\n"
1228 " gpt rename mmc 0 3 foo\n"
1229 " gpt set-bootable mmc 0 boot_a,boot_b\n"
1230 " gpt transpose mmc 0 1 2\n"