]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/gpt.c
98c788e75793f306dd615e52c8de9925c531b25e
1 // SPDX-License-Identifier: GPL-2.0+
3 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
6 * Lukasz Majewski <l.majewski@majess.pl>
8 * Copyright (C) 2012 Samsung Electronics
9 * author: Lukasz Majewski <l.majewski@samsung.com>
10 * author: Piotr Wilczek <p.wilczek@samsung.com>
20 #include <linux/ctype.h>
23 #include <linux/compat.h>
24 #include <linux/err.h>
25 #include <linux/sizes.h>
28 static LIST_HEAD(disk_partitions
);
31 * extract_env(): Expand env name from string format '&{env_name}'
32 * and return pointer to the env (if the env is set)
34 * @param str - pointer to string
35 * @param env - pointer to pointer to extracted env
37 * @return - zero on successful expand and env is set
39 static int extract_env(const char *str
, char **env
)
43 #ifdef CONFIG_RANDOM_UUID
44 char uuid_str
[UUID_STR_LEN
+ 1];
47 if (!str
|| strlen(str
) < 4)
50 if (!((strncmp(str
, "${", 2) == 0) && (str
[strlen(str
) - 1] == '}')))
57 memset(s
+ strlen(s
) - 1, '\0', 1);
58 memmove(s
, s
+ 2, strlen(s
) - 1);
62 #ifdef CONFIG_RANDOM_UUID
63 debug("%s unset. ", str
);
64 gen_rand_uuid_str(uuid_str
, UUID_STR_FORMAT_GUID
);
69 debug("Set to random.\n");
72 debug("Can't get random UUID.\n");
75 debug("%s unset.\n", str
);
78 debug("%s get from environment.\n", str
);
89 * extract_val(): Extract value from a key=value pair list (comma separated).
90 * Only value for the given key is returend.
91 * Function allocates memory for the value, remember to free!
93 * @param str - pointer to string with key=values pairs
94 * @param key - pointer to the key to search for
96 * @return - pointer to allocated string with the value
98 static char *extract_val(const char *str
, const char *key
)
104 strcopy
= strdup(str
);
116 if (strcmp(k
, key
) == 0) {
128 * found_key(): Found key without value in parameter list (comma separated).
130 * @param str - pointer to string with key
131 * @param key - pointer to the key to search for
133 * @return - true on found key
135 static bool found_key(const char *str
, const char *key
)
141 strcopy
= strdup(str
);
150 if (strcmp(k
, key
) == 0) {
161 static int calc_parts_list_len(int numparts
)
163 int partlistlen
= UUID_STR_LEN
+ 1 + strlen("uuid_disk=");
167 /* per-partition additions; numparts starts at 1, so this should be correct */
168 partlistlen
+= numparts
* (strlen("name=,") + PART_NAME_LEN
+ 1);
169 /* see part.h for definition of struct disk_partition */
170 partlistlen
+= numparts
* (strlen("start=MiB,") + sizeof(lbaint_t
) + 1);
171 partlistlen
+= numparts
* (strlen("size=MiB,") + sizeof(lbaint_t
) + 1);
172 partlistlen
+= numparts
* (strlen("uuid=;") + UUID_STR_LEN
+ 1);
173 /* for the terminating null */
175 debug("Length of partitions_list is %d for %d partitions\n", partlistlen
,
180 #ifdef CONFIG_CMD_GPT_RENAME
181 static void del_gpt_info(void)
183 struct list_head
*pos
= &disk_partitions
;
184 struct disk_part
*curr
;
185 while (!list_empty(pos
)) {
186 curr
= list_entry(pos
->next
, struct disk_part
, list
);
192 static struct disk_part
*allocate_disk_part(disk_partition_t
*info
, int partnum
)
194 struct disk_part
*newpart
;
195 newpart
= calloc(1, sizeof(struct disk_part
));
197 return ERR_PTR(-ENOMEM
);
199 newpart
->gpt_part_info
.start
= info
->start
;
200 newpart
->gpt_part_info
.size
= info
->size
;
201 newpart
->gpt_part_info
.blksz
= info
->blksz
;
202 strncpy((char *)newpart
->gpt_part_info
.name
, (const char *)info
->name
,
204 newpart
->gpt_part_info
.name
[PART_NAME_LEN
- 1] = '\0';
205 strncpy((char *)newpart
->gpt_part_info
.type
, (const char *)info
->type
,
207 newpart
->gpt_part_info
.type
[PART_TYPE_LEN
- 1] = '\0';
208 newpart
->gpt_part_info
.bootable
= info
->bootable
;
209 #ifdef CONFIG_PARTITION_UUIDS
210 strncpy(newpart
->gpt_part_info
.uuid
, (const char *)info
->uuid
,
212 /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
213 newpart
->gpt_part_info
.uuid
[UUID_STR_LEN
] = '\0';
215 newpart
->partnum
= partnum
;
220 static void prettyprint_part_size(char *sizestr
, lbaint_t partsize
,
223 unsigned long long partbytes
, partmegabytes
;
225 partbytes
= partsize
* blksize
;
226 partmegabytes
= lldiv(partbytes
, SZ_1M
);
227 snprintf(sizestr
, 16, "%lluMiB", partmegabytes
);
230 static void print_gpt_info(void)
232 struct list_head
*pos
;
233 struct disk_part
*curr
;
234 char partstartstr
[16];
235 char partsizestr
[16];
237 list_for_each(pos
, &disk_partitions
) {
238 curr
= list_entry(pos
, struct disk_part
, list
);
239 prettyprint_part_size(partstartstr
, curr
->gpt_part_info
.start
,
240 curr
->gpt_part_info
.blksz
);
241 prettyprint_part_size(partsizestr
, curr
->gpt_part_info
.size
,
242 curr
->gpt_part_info
.blksz
);
244 printf("Partition %d:\n", curr
->partnum
);
245 printf("Start %s, size %s\n", partstartstr
, partsizestr
);
246 printf("Block size %lu, name %s\n", curr
->gpt_part_info
.blksz
,
247 curr
->gpt_part_info
.name
);
248 printf("Type %s, bootable %d\n", curr
->gpt_part_info
.type
,
249 curr
->gpt_part_info
.bootable
& PART_BOOTABLE
);
250 #ifdef CONFIG_PARTITION_UUIDS
251 printf("UUID %s\n", curr
->gpt_part_info
.uuid
);
258 * create the string that upstream 'gpt write' command will accept as an
261 * From doc/README.gpt, Format of partitions layout:
262 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
263 * name=kernel,size=60MiB,uuid=...;"
264 * The fields 'name' and 'size' are mandatory for every partition.
265 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
266 * are optional if CONFIG_RANDOM_UUID is enabled.
268 static int create_gpt_partitions_list(int numparts
, const char *guid
,
269 char *partitions_list
)
271 struct list_head
*pos
;
272 struct disk_part
*curr
;
273 char partstr
[PART_NAME_LEN
+ 1];
275 if (!partitions_list
)
278 strcpy(partitions_list
, "uuid_disk=");
279 strncat(partitions_list
, guid
, UUID_STR_LEN
+ 1);
280 strcat(partitions_list
, ";");
282 list_for_each(pos
, &disk_partitions
) {
283 curr
= list_entry(pos
, struct disk_part
, list
);
284 strcat(partitions_list
, "name=");
285 strncat(partitions_list
, (const char *)curr
->gpt_part_info
.name
,
287 sprintf(partstr
, ",start=0x%llx",
288 (unsigned long long)curr
->gpt_part_info
.start
*
289 curr
->gpt_part_info
.blksz
);
290 /* one extra byte for NULL */
291 strncat(partitions_list
, partstr
, PART_NAME_LEN
+ 1);
292 sprintf(partstr
, ",size=0x%llx",
293 (unsigned long long)curr
->gpt_part_info
.size
*
294 curr
->gpt_part_info
.blksz
);
295 strncat(partitions_list
, partstr
, PART_NAME_LEN
+ 1);
297 strcat(partitions_list
, ",uuid=");
298 strncat(partitions_list
, curr
->gpt_part_info
.uuid
,
300 strcat(partitions_list
, ";");
306 * read partition info into disk_partitions list where
307 * it can be printed or modified
309 static int get_gpt_info(struct blk_desc
*dev_desc
)
311 /* start partition numbering at 1, as U-Boot does */
312 int valid_parts
= 0, p
, ret
;
313 disk_partition_t info
;
314 struct disk_part
*new_disk_part
;
317 * Always re-read partition info from device, in case
320 INIT_LIST_HEAD(&disk_partitions
);
322 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
323 ret
= part_get_info(dev_desc
, p
, &info
);
327 /* Add 1 here because counter is zero-based but p1 is
328 the first partition */
329 new_disk_part
= allocate_disk_part(&info
, valid_parts
+1);
330 if (IS_ERR(new_disk_part
))
333 list_add_tail(&new_disk_part
->list
, &disk_partitions
);
336 if (valid_parts
== 0) {
337 printf("** No valid partitions found **\n");
342 if (valid_parts
>= 1)
347 /* a wrapper to test get_gpt_info */
348 static int do_get_gpt_info(struct blk_desc
*dev_desc
)
352 ret
= get_gpt_info(dev_desc
);
363 * set_gpt_info(): Fill partition information from string
364 * function allocates memory, remember to free!
366 * @param dev_desc - pointer block device descriptor
367 * @param str_part - pointer to string with partition information
368 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
369 * @param partitions - pointer to pointer to allocated partitions array
370 * @param parts_count - number of partitions
372 * @return - zero on success, otherwise error
375 static int set_gpt_info(struct blk_desc
*dev_desc
,
376 const char *str_part
,
377 char **str_disk_guid
,
378 disk_partition_t
**partitions
,
385 disk_partition_t
*parts
;
387 uint64_t size_ll
, start_ll
;
389 int max_str_part
= calc_parts_list_len(MAX_SEARCH_PARTITIONS
);
391 debug("%s: lba num: 0x%x %d\n", __func__
,
392 (unsigned int)dev_desc
->lba
, (unsigned int)dev_desc
->lba
);
394 if (str_part
== NULL
)
397 str
= strdup(str_part
);
401 /* extract disk guid */
403 val
= extract_val(str
, "uuid_disk");
405 #ifdef CONFIG_RANDOM_UUID
406 *str_disk_guid
= malloc(UUID_STR_LEN
+ 1);
407 if (*str_disk_guid
== NULL
)
409 gen_rand_uuid_str(*str_disk_guid
, UUID_STR_FORMAT_STD
);
415 val
= strsep(&val
, ";");
416 if (extract_env(val
, &p
))
418 *str_disk_guid
= strdup(p
);
420 /* Move s to first partition */
424 printf("Error: is the partitions string NULL-terminated?\n");
427 if (strnlen(s
, max_str_part
) == 0)
430 i
= strnlen(s
, max_str_part
) - 1;
434 /* calculate expected number of partitions */
442 /* allocate memory for partitions */
443 parts
= calloc(sizeof(disk_partition_t
), p_count
);
447 /* retrieve partitions data from string */
448 for (i
= 0; i
< p_count
; i
++) {
449 tok
= strsep(&s
, ";");
455 val
= extract_val(tok
, "uuid");
457 /* 'uuid' is optional if random uuid's are enabled */
458 #ifdef CONFIG_RANDOM_UUID
459 gen_rand_uuid_str(parts
[i
].uuid
, UUID_STR_FORMAT_STD
);
465 if (extract_env(val
, &p
))
467 if (strnlen(p
, max_str_part
) >= sizeof(parts
[i
].uuid
)) {
468 printf("Wrong uuid format for partition %d\n", i
);
472 strncpy((char *)parts
[i
].uuid
, p
, max_str_part
);
475 #ifdef CONFIG_PARTITION_TYPE_GUID
477 val
= extract_val(tok
, "type");
479 /* 'type' is optional */
480 if (extract_env(val
, &p
))
482 if (strnlen(p
, max_str_part
) >= sizeof(parts
[i
].type_guid
)) {
483 printf("Wrong type guid format for partition %d\n",
488 strncpy((char *)parts
[i
].type_guid
, p
, max_str_part
);
493 val
= extract_val(tok
, "name");
494 if (!val
) { /* name is mandatory */
498 if (extract_env(val
, &p
))
500 if (strnlen(p
, max_str_part
) >= sizeof(parts
[i
].name
)) {
504 strncpy((char *)parts
[i
].name
, p
, max_str_part
);
508 val
= extract_val(tok
, "size");
509 if (!val
) { /* 'size' is mandatory */
513 if (extract_env(val
, &p
))
515 if ((strcmp(p
, "-") == 0)) {
516 /* Let part efi module to auto extend the size */
519 size_ll
= ustrtoull(p
, &p
, 0);
520 parts
[i
].size
= lldiv(size_ll
, dev_desc
->blksz
);
526 val
= extract_val(tok
, "start");
527 if (val
) { /* start address is optional */
528 if (extract_env(val
, &p
))
530 start_ll
= ustrtoull(p
, &p
, 0);
531 parts
[i
].start
= lldiv(start_ll
, dev_desc
->blksz
);
535 offset
+= parts
[i
].size
+ parts
[i
].start
;
538 if (found_key(tok
, "bootable"))
539 parts
[i
].bootable
= PART_BOOTABLE
;
542 *parts_count
= p_count
;
549 free(*str_disk_guid
);
555 static int gpt_default(struct blk_desc
*blk_dev_desc
, const char *str_part
)
560 disk_partition_t
*partitions
= NULL
;
562 /* fill partitions */
563 ret
= set_gpt_info(blk_dev_desc
, str_part
,
564 &str_disk_guid
, &partitions
, &part_count
);
567 printf("No partition list provided\n");
569 printf("Missing disk guid\n");
570 if ((ret
== -3) || (ret
== -4))
571 printf("Partition list incomplete\n");
575 /* save partitions layout to disk */
576 ret
= gpt_restore(blk_dev_desc
, str_disk_guid
, partitions
, part_count
);
583 static int gpt_verify(struct blk_desc
*blk_dev_desc
, const char *str_part
)
585 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header
, gpt_head
, 1,
586 blk_dev_desc
->blksz
);
587 disk_partition_t
*partitions
= NULL
;
588 gpt_entry
*gpt_pte
= NULL
;
593 /* fill partitions */
594 ret
= set_gpt_info(blk_dev_desc
, str_part
,
595 &str_disk_guid
, &partitions
, &part_count
);
598 printf("No partition list provided - only basic check\n");
599 ret
= gpt_verify_headers(blk_dev_desc
, gpt_head
,
604 printf("Missing disk guid\n");
605 if ((ret
== -3) || (ret
== -4))
606 printf("Partition list incomplete\n");
610 /* Check partition layout with provided pattern */
611 ret
= gpt_verify_partitions(blk_dev_desc
, partitions
, part_count
,
620 static int do_disk_guid(struct blk_desc
*dev_desc
, char * const namestr
)
623 char disk_guid
[UUID_STR_LEN
+ 1];
625 ret
= get_disk_guid(dev_desc
, disk_guid
);
627 return CMD_RET_FAILURE
;
630 env_set(namestr
, disk_guid
);
632 printf("%s\n", disk_guid
);
637 #ifdef CONFIG_CMD_GPT_RENAME
638 static int do_rename_gpt_parts(struct blk_desc
*dev_desc
, char *subcomm
,
639 char *name1
, char *name2
)
641 struct list_head
*pos
;
642 struct disk_part
*curr
;
643 disk_partition_t
*new_partitions
= NULL
;
644 char disk_guid
[UUID_STR_LEN
+ 1];
645 char *partitions_list
, *str_disk_guid
= NULL
;
647 int partlistlen
, ret
, numparts
= 0, partnum
, i
= 1, ctr1
= 0, ctr2
= 0;
649 if ((subcomm
== NULL
) || (name1
== NULL
) || (name2
== NULL
) ||
650 (strcmp(subcomm
, "swap") && (strcmp(subcomm
, "rename"))))
653 ret
= get_disk_guid(dev_desc
, disk_guid
);
657 * Allocates disk_partitions, requiring matching call to del_gpt_info()
660 numparts
= get_gpt_info(dev_desc
);
662 return numparts
? numparts
: -ENODEV
;
664 partlistlen
= calc_parts_list_len(numparts
);
665 partitions_list
= malloc(partlistlen
);
666 if (!partitions_list
) {
670 memset(partitions_list
, '\0', partlistlen
);
672 ret
= create_gpt_partitions_list(numparts
, disk_guid
, partitions_list
);
674 free(partitions_list
);
678 * Uncomment the following line to print a string that 'gpt write'
679 * or 'gpt verify' will accept as input.
681 debug("OLD partitions_list is %s with %u chars\n", partitions_list
,
682 (unsigned)strlen(partitions_list
));
684 /* set_gpt_info allocates new_partitions and str_disk_guid */
685 ret
= set_gpt_info(dev_desc
, partitions_list
, &str_disk_guid
,
686 &new_partitions
, &part_count
);
690 if (!strcmp(subcomm
, "swap")) {
691 if ((strlen(name1
) > PART_NAME_LEN
) || (strlen(name2
) > PART_NAME_LEN
)) {
692 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN
);
696 list_for_each(pos
, &disk_partitions
) {
697 curr
= list_entry(pos
, struct disk_part
, list
);
698 if (!strcmp((char *)curr
->gpt_part_info
.name
, name1
)) {
699 strcpy((char *)curr
->gpt_part_info
.name
, name2
);
701 } else if (!strcmp((char *)curr
->gpt_part_info
.name
, name2
)) {
702 strcpy((char *)curr
->gpt_part_info
.name
, name1
);
706 if ((ctr1
+ ctr2
< 2) || (ctr1
!= ctr2
)) {
707 printf("Cannot swap partition names except in pairs.\n");
711 } else { /* rename */
712 if (strlen(name2
) > PART_NAME_LEN
) {
713 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN
);
717 partnum
= (int)simple_strtol(name1
, NULL
, 10);
718 if ((partnum
< 0) || (partnum
> numparts
)) {
719 printf("Illegal partition number %s\n", name1
);
723 ret
= part_get_info(dev_desc
, partnum
, new_partitions
);
727 /* U-Boot partition numbering starts at 1 */
728 list_for_each(pos
, &disk_partitions
) {
729 curr
= list_entry(pos
, struct disk_part
, list
);
731 strcpy((char *)curr
->gpt_part_info
.name
, name2
);
738 ret
= create_gpt_partitions_list(numparts
, disk_guid
, partitions_list
);
741 debug("NEW partitions_list is %s with %u chars\n", partitions_list
,
742 (unsigned)strlen(partitions_list
));
744 ret
= set_gpt_info(dev_desc
, partitions_list
, &str_disk_guid
,
745 &new_partitions
, &part_count
);
747 * Even though valid pointers are here passed into set_gpt_info(),
748 * it mallocs again, and there's no way to tell which failed.
753 debug("Writing new partition table\n");
754 ret
= gpt_restore(dev_desc
, disk_guid
, new_partitions
, numparts
);
756 printf("Writing new partition table failed\n");
760 debug("Reading back new partition table\n");
762 * Empty the existing disk_partitions list, as otherwise the memory in
763 * the original list is unreachable.
766 numparts
= get_gpt_info(dev_desc
);
768 ret
= numparts
? numparts
: -ENODEV
;
771 printf("new partition table with %d partitions is:\n", numparts
);
775 #ifdef CONFIG_RANDOM_UUID
778 free(new_partitions
);
779 free(partitions_list
);
785 * do_gpt(): Perform GPT operations
787 * @param cmdtp - command name
792 * @return zero on success; otherwise error
794 static int do_gpt(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
796 int ret
= CMD_RET_SUCCESS
;
799 struct blk_desc
*blk_dev_desc
= NULL
;
801 #ifndef CONFIG_CMD_GPT_RENAME
802 if (argc
< 4 || argc
> 5)
804 if (argc
< 4 || argc
> 6)
806 return CMD_RET_USAGE
;
808 dev
= (int)simple_strtoul(argv
[3], &ep
, 10);
809 if (!ep
|| ep
[0] != '\0') {
810 printf("'%s' is not a number\n", argv
[3]);
811 return CMD_RET_USAGE
;
813 blk_dev_desc
= blk_get_dev(argv
[2], dev
);
815 printf("%s: %s dev %d NOT available\n",
816 __func__
, argv
[2], dev
);
817 return CMD_RET_FAILURE
;
820 if ((strcmp(argv
[1], "write") == 0) && (argc
== 5)) {
821 printf("Writing GPT: ");
822 ret
= gpt_default(blk_dev_desc
, argv
[4]);
823 } else if ((strcmp(argv
[1], "verify") == 0)) {
824 ret
= gpt_verify(blk_dev_desc
, argv
[4]);
825 printf("Verify GPT: ");
826 } else if (strcmp(argv
[1], "guid") == 0) {
827 ret
= do_disk_guid(blk_dev_desc
, argv
[4]);
828 #ifdef CONFIG_CMD_GPT_RENAME
829 } else if (strcmp(argv
[1], "read") == 0) {
830 ret
= do_get_gpt_info(blk_dev_desc
);
831 } else if ((strcmp(argv
[1], "swap") == 0) ||
832 (strcmp(argv
[1], "rename") == 0)) {
833 ret
= do_rename_gpt_parts(blk_dev_desc
, argv
[1], argv
[4], argv
[5]);
836 return CMD_RET_USAGE
;
841 return CMD_RET_FAILURE
;
844 printf("success!\n");
845 return CMD_RET_SUCCESS
;
848 U_BOOT_CMD(gpt
, CONFIG_SYS_MAXARGS
, 1, do_gpt
,
849 "GUID Partition Table",
850 "<command> <interface> <dev> <partitions_list>\n"
851 " - GUID partition table restoration and validity check\n"
852 " Restore or verify GPT information on a device connected\n"
855 " gpt write mmc 0 $partitions\n"
856 " gpt verify mmc 0 $partitions\n"
857 " gpt guid <interface> <dev>\n"
858 " - print disk GUID\n"
859 " gpt guid <interface> <dev> <varname>\n"
860 " - set environment variable to disk GUID\n"
863 " gpt guid mmc 0 varname\n"
864 #ifdef CONFIG_CMD_GPT_RENAME
865 "gpt partition renaming commands:\n"
866 " gpt read <interface> <dev>\n"
867 " - read GPT into a data structure for manipulation\n"
868 " gpt swap <interface> <dev> <name1> <name2>\n"
869 " - change all partitions named name1 to name2\n"
871 " gpt rename <interface> <dev> <part> <name>\n"
872 " - rename the specified partition\n"
874 " gpt swap mmc 0 foo bar\n"
875 " gpt rename mmc 0 3 foo\n"