]>
git.ipfire.org Git - people/ms/u-boot.git/blob - cmd/gpt.c
2 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
5 * Lukasz Majewski <l.majewski@majess.pl>
7 * Copyright (C) 2012 Samsung Electronics
8 * author: Lukasz Majewski <l.majewski@samsung.com>
9 * author: Piotr Wilczek <p.wilczek@samsung.com>
11 * SPDX-License-Identifier: GPL-2.0+
19 #include <linux/ctype.h>
22 #include <linux/compat.h>
23 #include <linux/sizes.h>
26 static LIST_HEAD(disk_partitions
);
29 * extract_env(): Expand env name from string format '&{env_name}'
30 * and return pointer to the env (if the env is set)
32 * @param str - pointer to string
33 * @param env - pointer to pointer to extracted env
35 * @return - zero on successful expand and env is set
37 static int extract_env(const char *str
, char **env
)
41 #ifdef CONFIG_RANDOM_UUID
42 char uuid_str
[UUID_STR_LEN
+ 1];
45 if (!str
|| strlen(str
) < 4)
48 if (!((strncmp(str
, "${", 2) == 0) && (str
[strlen(str
) - 1] == '}')))
55 memset(s
+ strlen(s
) - 1, '\0', 1);
56 memmove(s
, s
+ 2, strlen(s
) - 1);
60 #ifdef CONFIG_RANDOM_UUID
61 debug("%s unset. ", str
);
62 gen_rand_uuid_str(uuid_str
, UUID_STR_FORMAT_GUID
);
67 debug("Set to random.\n");
70 debug("Can't get random UUID.\n");
73 debug("%s unset.\n", str
);
76 debug("%s get from environment.\n", str
);
87 * extract_val(): Extract value from a key=value pair list (comma separated).
88 * Only value for the given key is returend.
89 * Function allocates memory for the value, remember to free!
91 * @param str - pointer to string with key=values pairs
92 * @param key - pointer to the key to search for
94 * @return - pointer to allocated string with the value
96 static char *extract_val(const char *str
, const char *key
)
102 strcopy
= strdup(str
);
114 if (strcmp(k
, key
) == 0) {
126 * found_key(): Found key without value in parameter list (comma separated).
128 * @param str - pointer to string with key
129 * @param key - pointer to the key to search for
131 * @return - true on found key
133 static bool found_key(const char *str
, const char *key
)
139 strcopy
= strdup(str
);
148 if (strcmp(k
, key
) == 0) {
159 static int calc_parts_list_len(int numparts
)
161 int partlistlen
= UUID_STR_LEN
+ 1 + strlen("uuid_disk=");
165 /* per-partition additions; numparts starts at 1, so this should be correct */
166 partlistlen
+= numparts
* (strlen("name=,") + PART_NAME_LEN
+ 1);
167 /* see part.h for definition of struct disk_partition */
168 partlistlen
+= numparts
* (strlen("start=MiB,") + sizeof(lbaint_t
) + 1);
169 partlistlen
+= numparts
* (strlen("size=MiB,") + sizeof(lbaint_t
) + 1);
170 partlistlen
+= numparts
* (strlen("uuid=;") + UUID_STR_LEN
+ 1);
171 /* for the terminating null */
173 debug("Length of partitions_list is %d for %d partitions\n", partlistlen
,
178 #ifdef CONFIG_CMD_GPT_RENAME
179 static void del_gpt_info(void)
181 struct list_head
*pos
= &disk_partitions
;
182 struct disk_part
*curr
;
183 while (!list_empty(pos
)) {
184 curr
= list_entry(pos
->next
, struct disk_part
, list
);
190 static struct disk_part
*allocate_disk_part(disk_partition_t
*info
, int partnum
)
192 struct disk_part
*newpart
;
193 newpart
= calloc(1, sizeof(struct disk_part
));
195 return ERR_PTR(-ENOMEM
);
197 newpart
->gpt_part_info
.start
= info
->start
;
198 newpart
->gpt_part_info
.size
= info
->size
;
199 newpart
->gpt_part_info
.blksz
= info
->blksz
;
200 strncpy((char *)newpart
->gpt_part_info
.name
, (const char *)info
->name
,
202 newpart
->gpt_part_info
.name
[PART_NAME_LEN
- 1] = '\0';
203 strncpy((char *)newpart
->gpt_part_info
.type
, (const char *)info
->type
,
205 newpart
->gpt_part_info
.type
[PART_TYPE_LEN
- 1] = '\0';
206 newpart
->gpt_part_info
.bootable
= info
->bootable
;
207 #ifdef CONFIG_PARTITION_UUIDS
208 strncpy(newpart
->gpt_part_info
.uuid
, (const char *)info
->uuid
,
210 /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
211 newpart
->gpt_part_info
.uuid
[UUID_STR_LEN
] = '\0';
213 newpart
->partnum
= partnum
;
218 static void prettyprint_part_size(char *sizestr
, lbaint_t partsize
,
221 unsigned long long partbytes
, partmegabytes
;
223 partbytes
= partsize
* blksize
;
224 partmegabytes
= lldiv(partbytes
, SZ_1M
);
225 snprintf(sizestr
, 16, "%lluMiB", partmegabytes
);
228 static void print_gpt_info(void)
230 struct list_head
*pos
;
231 struct disk_part
*curr
;
232 char partstartstr
[16];
233 char partsizestr
[16];
235 list_for_each(pos
, &disk_partitions
) {
236 curr
= list_entry(pos
, struct disk_part
, list
);
237 prettyprint_part_size(partstartstr
, curr
->gpt_part_info
.start
,
238 curr
->gpt_part_info
.blksz
);
239 prettyprint_part_size(partsizestr
, curr
->gpt_part_info
.size
,
240 curr
->gpt_part_info
.blksz
);
242 printf("Partition %d:\n", curr
->partnum
);
243 printf("Start %s, size %s\n", partstartstr
, partsizestr
);
244 printf("Block size %lu, name %s\n", curr
->gpt_part_info
.blksz
,
245 curr
->gpt_part_info
.name
);
246 printf("Type %s, bootable %d\n", curr
->gpt_part_info
.type
,
247 curr
->gpt_part_info
.bootable
);
248 #ifdef CONFIG_PARTITION_UUIDS
249 printf("UUID %s\n", curr
->gpt_part_info
.uuid
);
256 * create the string that upstream 'gpt write' command will accept as an
259 * From doc/README.gpt, Format of partitions layout:
260 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
261 * name=kernel,size=60MiB,uuid=...;"
262 * The fields 'name' and 'size' are mandatory for every partition.
263 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
264 * are optional if CONFIG_RANDOM_UUID is enabled.
266 static int create_gpt_partitions_list(int numparts
, const char *guid
,
267 char *partitions_list
)
269 struct list_head
*pos
;
270 struct disk_part
*curr
;
271 char partstr
[PART_NAME_LEN
+ 1];
273 if (!partitions_list
)
276 strcpy(partitions_list
, "uuid_disk=");
277 strncat(partitions_list
, guid
, UUID_STR_LEN
+ 1);
278 strcat(partitions_list
, ";");
280 list_for_each(pos
, &disk_partitions
) {
281 curr
= list_entry(pos
, struct disk_part
, list
);
282 strcat(partitions_list
, "name=");
283 strncat(partitions_list
, (const char *)curr
->gpt_part_info
.name
,
285 strcat(partitions_list
, ",start=");
286 prettyprint_part_size(partstr
, (unsigned long)curr
->gpt_part_info
.start
,
287 (unsigned long) curr
->gpt_part_info
.blksz
);
288 /* one extra byte for NULL */
289 strncat(partitions_list
, partstr
, PART_NAME_LEN
+ 1);
290 strcat(partitions_list
, ",size=");
291 prettyprint_part_size(partstr
, curr
->gpt_part_info
.size
,
292 curr
->gpt_part_info
.blksz
);
293 strncat(partitions_list
, partstr
, PART_NAME_LEN
+ 1);
295 strcat(partitions_list
, ",uuid=");
296 strncat(partitions_list
, curr
->gpt_part_info
.uuid
,
298 strcat(partitions_list
, ";");
304 * read partition info into disk_partitions list where
305 * it can be printed or modified
307 static int get_gpt_info(struct blk_desc
*dev_desc
)
309 /* start partition numbering at 1, as U-Boot does */
310 int valid_parts
= 0, p
, ret
;
311 disk_partition_t info
;
312 struct disk_part
*new_disk_part
;
315 * Always re-read partition info from device, in case
318 INIT_LIST_HEAD(&disk_partitions
);
320 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
321 ret
= part_get_info(dev_desc
, p
, &info
);
325 /* Add 1 here because counter is zero-based but p1 is
326 the first partition */
327 new_disk_part
= allocate_disk_part(&info
, valid_parts
+1);
328 if (IS_ERR(new_disk_part
))
331 list_add_tail(&new_disk_part
->list
, &disk_partitions
);
334 if (valid_parts
== 0) {
335 printf("** No valid partitions found **\n");
340 if (valid_parts
>= 1)
345 /* a wrapper to test get_gpt_info */
346 static int do_get_gpt_info(struct blk_desc
*dev_desc
)
350 ret
= get_gpt_info(dev_desc
);
361 * set_gpt_info(): Fill partition information from string
362 * function allocates memory, remember to free!
364 * @param dev_desc - pointer block device descriptor
365 * @param str_part - pointer to string with partition information
366 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
367 * @param partitions - pointer to pointer to allocated partitions array
368 * @param parts_count - number of partitions
370 * @return - zero on success, otherwise error
373 static int set_gpt_info(struct blk_desc
*dev_desc
,
374 const char *str_part
,
375 char **str_disk_guid
,
376 disk_partition_t
**partitions
,
383 disk_partition_t
*parts
;
385 uint64_t size_ll
, start_ll
;
387 int max_str_part
= calc_parts_list_len(MAX_SEARCH_PARTITIONS
);
389 debug("%s: lba num: 0x%x %d\n", __func__
,
390 (unsigned int)dev_desc
->lba
, (unsigned int)dev_desc
->lba
);
392 if (str_part
== NULL
)
395 str
= strdup(str_part
);
399 /* extract disk guid */
401 val
= extract_val(str
, "uuid_disk");
403 #ifdef CONFIG_RANDOM_UUID
404 *str_disk_guid
= malloc(UUID_STR_LEN
+ 1);
405 if (str_disk_guid
== NULL
)
407 gen_rand_uuid_str(*str_disk_guid
, UUID_STR_FORMAT_STD
);
413 val
= strsep(&val
, ";");
414 if (extract_env(val
, &p
))
416 *str_disk_guid
= strdup(p
);
418 /* Move s to first partition */
422 printf("Error: is the partitions string NULL-terminated?\n");
425 if (strnlen(s
, max_str_part
) == 0)
428 i
= strnlen(s
, max_str_part
) - 1;
432 /* calculate expected number of partitions */
440 /* allocate memory for partitions */
441 parts
= calloc(sizeof(disk_partition_t
), p_count
);
445 /* retrieve partitions data from string */
446 for (i
= 0; i
< p_count
; i
++) {
447 tok
= strsep(&s
, ";");
453 val
= extract_val(tok
, "uuid");
455 /* 'uuid' is optional if random uuid's are enabled */
456 #ifdef CONFIG_RANDOM_UUID
457 gen_rand_uuid_str(parts
[i
].uuid
, UUID_STR_FORMAT_STD
);
463 if (extract_env(val
, &p
))
465 if (strnlen(p
, max_str_part
) >= sizeof(parts
[i
].uuid
)) {
466 printf("Wrong uuid format for partition %d\n", i
);
470 strncpy((char *)parts
[i
].uuid
, p
, max_str_part
);
473 #ifdef CONFIG_PARTITION_TYPE_GUID
475 val
= extract_val(tok
, "type");
477 /* 'type' is optional */
478 if (extract_env(val
, &p
))
480 if (strnlen(p
, max_str_part
) >= sizeof(parts
[i
].type_guid
)) {
481 printf("Wrong type guid format for partition %d\n",
486 strncpy((char *)parts
[i
].type_guid
, p
, max_str_part
);
491 val
= extract_val(tok
, "name");
492 if (!val
) { /* name is mandatory */
496 if (extract_env(val
, &p
))
498 if (strnlen(p
, max_str_part
) >= sizeof(parts
[i
].name
)) {
502 strncpy((char *)parts
[i
].name
, p
, max_str_part
);
506 val
= extract_val(tok
, "size");
507 if (!val
) { /* 'size' is mandatory */
511 if (extract_env(val
, &p
))
513 if ((strcmp(p
, "-") == 0)) {
514 /* Let part efi module to auto extend the size */
517 size_ll
= ustrtoull(p
, &p
, 0);
518 parts
[i
].size
= lldiv(size_ll
, dev_desc
->blksz
);
524 val
= extract_val(tok
, "start");
525 if (val
) { /* start address is optional */
526 if (extract_env(val
, &p
))
528 start_ll
= ustrtoull(p
, &p
, 0);
529 parts
[i
].start
= lldiv(start_ll
, dev_desc
->blksz
);
533 offset
+= parts
[i
].size
+ parts
[i
].start
;
536 if (found_key(tok
, "bootable"))
537 parts
[i
].bootable
= 1;
540 *parts_count
= p_count
;
547 free(*str_disk_guid
);
553 static int gpt_default(struct blk_desc
*blk_dev_desc
, const char *str_part
)
558 disk_partition_t
*partitions
= NULL
;
560 /* fill partitions */
561 ret
= set_gpt_info(blk_dev_desc
, str_part
,
562 &str_disk_guid
, &partitions
, &part_count
);
565 printf("No partition list provided\n");
567 printf("Missing disk guid\n");
568 if ((ret
== -3) || (ret
== -4))
569 printf("Partition list incomplete\n");
573 /* save partitions layout to disk */
574 ret
= gpt_restore(blk_dev_desc
, str_disk_guid
, partitions
, part_count
);
581 static int gpt_verify(struct blk_desc
*blk_dev_desc
, const char *str_part
)
583 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header
, gpt_head
, 1,
584 blk_dev_desc
->blksz
);
585 disk_partition_t
*partitions
= NULL
;
586 gpt_entry
*gpt_pte
= NULL
;
591 /* fill partitions */
592 ret
= set_gpt_info(blk_dev_desc
, str_part
,
593 &str_disk_guid
, &partitions
, &part_count
);
596 printf("No partition list provided - only basic check\n");
597 ret
= gpt_verify_headers(blk_dev_desc
, gpt_head
,
602 printf("Missing disk guid\n");
603 if ((ret
== -3) || (ret
== -4))
604 printf("Partition list incomplete\n");
608 /* Check partition layout with provided pattern */
609 ret
= gpt_verify_partitions(blk_dev_desc
, partitions
, part_count
,
618 static int do_disk_guid(struct blk_desc
*dev_desc
, char * const namestr
)
621 char disk_guid
[UUID_STR_LEN
+ 1];
623 ret
= get_disk_guid(dev_desc
, disk_guid
);
625 return CMD_RET_FAILURE
;
628 env_set(namestr
, disk_guid
);
630 printf("%s\n", disk_guid
);
635 #ifdef CONFIG_CMD_GPT_RENAME
637 * There are 3 malloc() calls in set_gpt_info() and there is no info about which
640 static void set_gpt_cleanup(char **str_disk_guid
,
641 disk_partition_t
**partitions
)
643 #ifdef CONFIG_RANDOM_UUID
651 static int do_rename_gpt_parts(struct blk_desc
*dev_desc
, char *subcomm
,
652 char *name1
, char *name2
)
654 struct list_head
*pos
;
655 struct disk_part
*curr
;
656 disk_partition_t
*new_partitions
= NULL
;
657 char disk_guid
[UUID_STR_LEN
+ 1];
658 char *partitions_list
, *str_disk_guid
;
660 int partlistlen
, ret
, numparts
= 0, partnum
, i
= 1, ctr1
= 0, ctr2
= 0;
662 if ((subcomm
== NULL
) || (name1
== NULL
) || (name2
== NULL
) ||
663 (strcmp(subcomm
, "swap") && (strcmp(subcomm
, "rename"))))
666 ret
= get_disk_guid(dev_desc
, disk_guid
);
670 * Allocates disk_partitions, requiring matching call to del_gpt_info()
673 numparts
= get_gpt_info(dev_desc
);
675 return numparts
? numparts
: -ENODEV
;
677 partlistlen
= calc_parts_list_len(numparts
);
678 partitions_list
= malloc(partlistlen
);
679 if (!partitions_list
) {
683 memset(partitions_list
, '\0', partlistlen
);
685 ret
= create_gpt_partitions_list(numparts
, disk_guid
, partitions_list
);
687 free(partitions_list
);
691 * Uncomment the following line to print a string that 'gpt write'
692 * or 'gpt verify' will accept as input.
694 debug("OLD partitions_list is %s with %u chars\n", partitions_list
,
695 (unsigned)strlen(partitions_list
));
697 /* set_gpt_info allocates new_partitions and str_disk_guid */
698 ret
= set_gpt_info(dev_desc
, partitions_list
, &str_disk_guid
,
699 &new_partitions
, &part_count
);
702 free(partitions_list
);
704 set_gpt_cleanup(&str_disk_guid
, &new_partitions
);
709 if (!strcmp(subcomm
, "swap")) {
710 if ((strlen(name1
) > PART_NAME_LEN
) || (strlen(name2
) > PART_NAME_LEN
)) {
711 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN
);
715 list_for_each(pos
, &disk_partitions
) {
716 curr
= list_entry(pos
, struct disk_part
, list
);
717 if (!strcmp((char *)curr
->gpt_part_info
.name
, name1
)) {
718 strcpy((char *)curr
->gpt_part_info
.name
, name2
);
720 } else if (!strcmp((char *)curr
->gpt_part_info
.name
, name2
)) {
721 strcpy((char *)curr
->gpt_part_info
.name
, name1
);
725 if ((ctr1
+ ctr2
< 2) || (ctr1
!= ctr2
)) {
726 printf("Cannot swap partition names except in pairs.\n");
730 } else { /* rename */
731 if (strlen(name2
) > PART_NAME_LEN
) {
732 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN
);
736 partnum
= (int)simple_strtol(name1
, NULL
, 10);
737 if ((partnum
< 0) || (partnum
> numparts
)) {
738 printf("Illegal partition number %s\n", name1
);
742 ret
= part_get_info(dev_desc
, partnum
, new_partitions
);
746 /* U-Boot partition numbering starts at 1 */
747 list_for_each(pos
, &disk_partitions
) {
748 curr
= list_entry(pos
, struct disk_part
, list
);
750 strcpy((char *)curr
->gpt_part_info
.name
, name2
);
757 ret
= create_gpt_partitions_list(numparts
, disk_guid
, partitions_list
);
760 debug("NEW partitions_list is %s with %u chars\n", partitions_list
,
761 (unsigned)strlen(partitions_list
));
763 ret
= set_gpt_info(dev_desc
, partitions_list
, &str_disk_guid
,
764 &new_partitions
, &part_count
);
766 * Even though valid pointers are here passed into set_gpt_info(),
767 * it mallocs again, and there's no way to tell which failed.
771 free(partitions_list
);
773 set_gpt_cleanup(&str_disk_guid
, &new_partitions
);
778 debug("Writing new partition table\n");
779 ret
= gpt_restore(dev_desc
, disk_guid
, new_partitions
, numparts
);
781 printf("Writing new partition table failed\n");
785 debug("Reading back new partition table\n");
787 * Empty the existing disk_partitions list, as otherwise the memory in
788 * the original list is unreachable.
791 numparts
= get_gpt_info(dev_desc
);
793 ret
= numparts
? numparts
: -ENODEV
;
796 printf("new partition table with %d partitions is:\n", numparts
);
800 free(new_partitions
);
802 free(partitions_list
);
808 * do_gpt(): Perform GPT operations
810 * @param cmdtp - command name
815 * @return zero on success; otherwise error
817 static int do_gpt(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
819 int ret
= CMD_RET_SUCCESS
;
822 struct blk_desc
*blk_dev_desc
= NULL
;
824 #ifndef CONFIG_CMD_GPT_RENAME
825 if (argc
< 4 || argc
> 5)
827 if (argc
< 4 || argc
> 6)
829 return CMD_RET_USAGE
;
831 dev
= (int)simple_strtoul(argv
[3], &ep
, 10);
832 if (!ep
|| ep
[0] != '\0') {
833 printf("'%s' is not a number\n", argv
[3]);
834 return CMD_RET_USAGE
;
836 blk_dev_desc
= blk_get_dev(argv
[2], dev
);
838 printf("%s: %s dev %d NOT available\n",
839 __func__
, argv
[2], dev
);
840 return CMD_RET_FAILURE
;
843 if ((strcmp(argv
[1], "write") == 0) && (argc
== 5)) {
844 printf("Writing GPT: ");
845 ret
= gpt_default(blk_dev_desc
, argv
[4]);
846 } else if ((strcmp(argv
[1], "verify") == 0)) {
847 ret
= gpt_verify(blk_dev_desc
, argv
[4]);
848 printf("Verify GPT: ");
849 } else if (strcmp(argv
[1], "guid") == 0) {
850 ret
= do_disk_guid(blk_dev_desc
, argv
[4]);
851 #ifdef CONFIG_CMD_GPT_RENAME
852 } else if (strcmp(argv
[1], "read") == 0) {
853 ret
= do_get_gpt_info(blk_dev_desc
);
854 } else if ((strcmp(argv
[1], "swap") == 0) ||
855 (strcmp(argv
[1], "rename") == 0)) {
856 ret
= do_rename_gpt_parts(blk_dev_desc
, argv
[1], argv
[4], argv
[5]);
859 return CMD_RET_USAGE
;
864 return CMD_RET_FAILURE
;
867 printf("success!\n");
868 return CMD_RET_SUCCESS
;
871 U_BOOT_CMD(gpt
, CONFIG_SYS_MAXARGS
, 1, do_gpt
,
872 "GUID Partition Table",
873 "<command> <interface> <dev> <partitions_list>\n"
874 " - GUID partition table restoration and validity check\n"
875 " Restore or verify GPT information on a device connected\n"
878 " gpt write mmc 0 $partitions\n"
879 " gpt verify mmc 0 $partitions\n"
880 " read <interface> <dev>\n"
881 " - read GPT into a data structure for manipulation\n"
882 " guid <interface> <dev>\n"
883 " - print disk GUID\n"
884 " guid <interface> <dev> <varname>\n"
885 " - set environment variable to disk GUID\n"
888 " gpt guid mmc 0 varname\n"
889 #ifdef CONFIG_CMD_GPT_RENAME
890 "gpt partition renaming commands:\n"
891 "gpt swap <interface> <dev> <name1> <name2>\n"
892 " - change all partitions named name1 to name2\n"
894 "gpt rename <interface> <dev> <part> <name>\n"
895 " - rename the specified partition\n"
897 " gpt swap mmc 0 foo bar\n"
898 " gpt rename mmc 0 3 foo\n"