]> git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/gpt.c
Merge tag 'efi-2020-07-rc3-2' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[thirdparty/u-boot.git] / cmd / gpt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
4 *
5 * Copyright (C) 2015
6 * Lukasz Majewski <l.majewski@majess.pl>
7 *
8 * Copyright (C) 2012 Samsung Electronics
9 * author: Lukasz Majewski <l.majewski@samsung.com>
10 * author: Piotr Wilczek <p.wilczek@samsung.com>
11 */
12
13 #include <common.h>
14 #include <blk.h>
15 #include <env.h>
16 #include <log.h>
17 #include <malloc.h>
18 #include <command.h>
19 #include <part.h>
20 #include <part_efi.h>
21 #include <exports.h>
22 #include <uuid.h>
23 #include <linux/ctype.h>
24 #include <div64.h>
25 #include <memalign.h>
26 #include <linux/compat.h>
27 #include <linux/err.h>
28 #include <linux/sizes.h>
29 #include <stdlib.h>
30
31 static LIST_HEAD(disk_partitions);
32
33 /**
34 * extract_env(): Expand env name from string format '&{env_name}'
35 * and return pointer to the env (if the env is set)
36 *
37 * @param str - pointer to string
38 * @param env - pointer to pointer to extracted env
39 *
40 * @return - zero on successful expand and env is set
41 */
42 static int extract_env(const char *str, char **env)
43 {
44 int ret = -1;
45 char *e, *s;
46 #ifdef CONFIG_RANDOM_UUID
47 char uuid_str[UUID_STR_LEN + 1];
48 #endif
49
50 if (!str || strlen(str) < 4)
51 return -1;
52
53 if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
54 return -1;
55
56 s = strdup(str);
57 if (s == NULL)
58 return -1;
59
60 memset(s + strlen(s) - 1, '\0', 1);
61 memmove(s, s + 2, strlen(s) - 1);
62
63 e = env_get(s);
64 if (e == NULL) {
65 #ifdef CONFIG_RANDOM_UUID
66 debug("%s unset. ", str);
67 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
68 env_set(s, uuid_str);
69
70 e = env_get(s);
71 if (e) {
72 debug("Set to random.\n");
73 ret = 0;
74 } else {
75 debug("Can't get random UUID.\n");
76 }
77 #else
78 debug("%s unset.\n", str);
79 #endif
80 } else {
81 debug("%s get from environment.\n", str);
82 ret = 0;
83 }
84
85 *env = e;
86 free(s);
87
88 return ret;
89 }
90
91 /**
92 * extract_val(): Extract value from a key=value pair list (comma separated).
93 * Only value for the given key is returend.
94 * Function allocates memory for the value, remember to free!
95 *
96 * @param str - pointer to string with key=values pairs
97 * @param key - pointer to the key to search for
98 *
99 * @return - pointer to allocated string with the value
100 */
101 static char *extract_val(const char *str, const char *key)
102 {
103 char *v, *k;
104 char *s, *strcopy;
105 char *new = NULL;
106
107 strcopy = strdup(str);
108 if (strcopy == NULL)
109 return NULL;
110
111 s = strcopy;
112 while (s) {
113 v = strsep(&s, ",");
114 if (!v)
115 break;
116 k = strsep(&v, "=");
117 if (!k)
118 break;
119 if (strcmp(k, key) == 0) {
120 new = strdup(v);
121 break;
122 }
123 }
124
125 free(strcopy);
126
127 return new;
128 }
129
130 /**
131 * found_key(): Found key without value in parameter list (comma separated).
132 *
133 * @param str - pointer to string with key
134 * @param key - pointer to the key to search for
135 *
136 * @return - true on found key
137 */
138 static bool found_key(const char *str, const char *key)
139 {
140 char *k;
141 char *s, *strcopy;
142 bool result = false;
143
144 strcopy = strdup(str);
145 if (!strcopy)
146 return NULL;
147
148 s = strcopy;
149 while (s) {
150 k = strsep(&s, ",");
151 if (!k)
152 break;
153 if (strcmp(k, key) == 0) {
154 result = true;
155 break;
156 }
157 }
158
159 free(strcopy);
160
161 return result;
162 }
163
164 static int calc_parts_list_len(int numparts)
165 {
166 int partlistlen = UUID_STR_LEN + 1 + strlen("uuid_disk=");
167 /* for the comma */
168 partlistlen++;
169
170 /* per-partition additions; numparts starts at 1, so this should be correct */
171 partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN + 1);
172 /* see part.h for definition of struct disk_partition */
173 partlistlen += numparts * (strlen("start=MiB,") + sizeof(lbaint_t) + 1);
174 partlistlen += numparts * (strlen("size=MiB,") + sizeof(lbaint_t) + 1);
175 partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN + 1);
176 /* for the terminating null */
177 partlistlen++;
178 debug("Length of partitions_list is %d for %d partitions\n", partlistlen,
179 numparts);
180 return partlistlen;
181 }
182
183 #ifdef CONFIG_CMD_GPT_RENAME
184 static void del_gpt_info(void)
185 {
186 struct list_head *pos = &disk_partitions;
187 struct disk_part *curr;
188 while (!list_empty(pos)) {
189 curr = list_entry(pos->next, struct disk_part, list);
190 list_del(pos->next);
191 free(curr);
192 }
193 }
194
195 static struct disk_part *allocate_disk_part(struct disk_partition *info,
196 int partnum)
197 {
198 struct disk_part *newpart;
199 newpart = calloc(1, sizeof(struct disk_part));
200 if (!newpart)
201 return ERR_PTR(-ENOMEM);
202
203 newpart->gpt_part_info.start = info->start;
204 newpart->gpt_part_info.size = info->size;
205 newpart->gpt_part_info.blksz = info->blksz;
206 strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
207 PART_NAME_LEN);
208 newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
209 strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
210 PART_TYPE_LEN);
211 newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
212 newpart->gpt_part_info.bootable = info->bootable;
213 #ifdef CONFIG_PARTITION_UUIDS
214 strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid,
215 UUID_STR_LEN);
216 /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
217 newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0';
218 #endif
219 newpart->partnum = partnum;
220
221 return newpart;
222 }
223
224 static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
225 lbaint_t blksize)
226 {
227 unsigned long long partbytes, partmegabytes;
228
229 partbytes = partsize * blksize;
230 partmegabytes = lldiv(partbytes, SZ_1M);
231 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
232 }
233
234 static void print_gpt_info(void)
235 {
236 struct list_head *pos;
237 struct disk_part *curr;
238 char partstartstr[16];
239 char partsizestr[16];
240
241 list_for_each(pos, &disk_partitions) {
242 curr = list_entry(pos, struct disk_part, list);
243 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
244 curr->gpt_part_info.blksz);
245 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
246 curr->gpt_part_info.blksz);
247
248 printf("Partition %d:\n", curr->partnum);
249 printf("Start %s, size %s\n", partstartstr, partsizestr);
250 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
251 curr->gpt_part_info.name);
252 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
253 curr->gpt_part_info.bootable & PART_BOOTABLE);
254 #ifdef CONFIG_PARTITION_UUIDS
255 printf("UUID %s\n", curr->gpt_part_info.uuid);
256 #endif
257 printf("\n");
258 }
259 }
260
261 /*
262 * create the string that upstream 'gpt write' command will accept as an
263 * argument
264 *
265 * From doc/README.gpt, Format of partitions layout:
266 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
267 * name=kernel,size=60MiB,uuid=...;"
268 * The fields 'name' and 'size' are mandatory for every partition.
269 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
270 * are optional if CONFIG_RANDOM_UUID is enabled.
271 */
272 static int create_gpt_partitions_list(int numparts, const char *guid,
273 char *partitions_list)
274 {
275 struct list_head *pos;
276 struct disk_part *curr;
277 char partstr[PART_NAME_LEN + 1];
278
279 if (!partitions_list)
280 return -EINVAL;
281
282 strcpy(partitions_list, "uuid_disk=");
283 strncat(partitions_list, guid, UUID_STR_LEN + 1);
284 strcat(partitions_list, ";");
285
286 list_for_each(pos, &disk_partitions) {
287 curr = list_entry(pos, struct disk_part, list);
288 strcat(partitions_list, "name=");
289 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
290 PART_NAME_LEN + 1);
291 sprintf(partstr, ",start=0x%llx",
292 (unsigned long long)curr->gpt_part_info.start *
293 curr->gpt_part_info.blksz);
294 /* one extra byte for NULL */
295 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
296 sprintf(partstr, ",size=0x%llx",
297 (unsigned long long)curr->gpt_part_info.size *
298 curr->gpt_part_info.blksz);
299 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
300
301 strcat(partitions_list, ",uuid=");
302 strncat(partitions_list, curr->gpt_part_info.uuid,
303 UUID_STR_LEN + 1);
304 strcat(partitions_list, ";");
305 }
306 return 0;
307 }
308
309 /*
310 * read partition info into disk_partitions list where
311 * it can be printed or modified
312 */
313 static int get_gpt_info(struct blk_desc *dev_desc)
314 {
315 /* start partition numbering at 1, as U-Boot does */
316 int valid_parts = 0, p, ret;
317 struct disk_partition info;
318 struct disk_part *new_disk_part;
319
320 /*
321 * Always re-read partition info from device, in case
322 * it has changed
323 */
324 INIT_LIST_HEAD(&disk_partitions);
325
326 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
327 ret = part_get_info(dev_desc, p, &info);
328 if (ret)
329 continue;
330
331 /* Add 1 here because counter is zero-based but p1 is
332 the first partition */
333 new_disk_part = allocate_disk_part(&info, valid_parts+1);
334 if (IS_ERR(new_disk_part))
335 goto out;
336
337 list_add_tail(&new_disk_part->list, &disk_partitions);
338 valid_parts++;
339 }
340 if (valid_parts == 0) {
341 printf("** No valid partitions found **\n");
342 goto out;
343 }
344 return valid_parts;
345 out:
346 if (valid_parts >= 1)
347 del_gpt_info();
348 return -ENODEV;
349 }
350
351 /* a wrapper to test get_gpt_info */
352 static int do_get_gpt_info(struct blk_desc *dev_desc)
353 {
354 int ret;
355
356 ret = get_gpt_info(dev_desc);
357 if (ret > 0) {
358 print_gpt_info();
359 del_gpt_info();
360 return 0;
361 }
362 return ret;
363 }
364 #endif
365
366 /**
367 * set_gpt_info(): Fill partition information from string
368 * function allocates memory, remember to free!
369 *
370 * @param dev_desc - pointer block device descriptor
371 * @param str_part - pointer to string with partition information
372 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
373 * @param partitions - pointer to pointer to allocated partitions array
374 * @param parts_count - number of partitions
375 *
376 * @return - zero on success, otherwise error
377 *
378 */
379 static int set_gpt_info(struct blk_desc *dev_desc,
380 const char *str_part,
381 char **str_disk_guid,
382 struct disk_partition **partitions,
383 u8 *parts_count)
384 {
385 char *tok, *str, *s;
386 int i;
387 char *val, *p;
388 int p_count;
389 struct disk_partition *parts;
390 int errno = 0;
391 uint64_t size_ll, start_ll;
392 lbaint_t offset = 0;
393 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
394
395 debug("%s: lba num: 0x%x %d\n", __func__,
396 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
397
398 if (str_part == NULL)
399 return -1;
400
401 str = strdup(str_part);
402 if (str == NULL)
403 return -ENOMEM;
404
405 /* extract disk guid */
406 s = str;
407 val = extract_val(str, "uuid_disk");
408 if (!val) {
409 #ifdef CONFIG_RANDOM_UUID
410 *str_disk_guid = malloc(UUID_STR_LEN + 1);
411 if (*str_disk_guid == NULL)
412 return -ENOMEM;
413 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
414 #else
415 free(str);
416 return -2;
417 #endif
418 } else {
419 val = strsep(&val, ";");
420 if (extract_env(val, &p))
421 p = val;
422 *str_disk_guid = strdup(p);
423 free(val);
424 /* Move s to first partition */
425 strsep(&s, ";");
426 }
427 if (s == NULL) {
428 printf("Error: is the partitions string NULL-terminated?\n");
429 return -EINVAL;
430 }
431 if (strnlen(s, max_str_part) == 0)
432 return -3;
433
434 i = strnlen(s, max_str_part) - 1;
435 if (s[i] == ';')
436 s[i] = '\0';
437
438 /* calculate expected number of partitions */
439 p_count = 1;
440 p = s;
441 while (*p) {
442 if (*p++ == ';')
443 p_count++;
444 }
445
446 /* allocate memory for partitions */
447 parts = calloc(sizeof(struct disk_partition), p_count);
448 if (parts == NULL)
449 return -ENOMEM;
450
451 /* retrieve partitions data from string */
452 for (i = 0; i < p_count; i++) {
453 tok = strsep(&s, ";");
454
455 if (tok == NULL)
456 break;
457
458 /* uuid */
459 val = extract_val(tok, "uuid");
460 if (!val) {
461 /* 'uuid' is optional if random uuid's are enabled */
462 #ifdef CONFIG_RANDOM_UUID
463 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
464 #else
465 errno = -4;
466 goto err;
467 #endif
468 } else {
469 if (extract_env(val, &p))
470 p = val;
471 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
472 printf("Wrong uuid format for partition %d\n", i);
473 errno = -4;
474 goto err;
475 }
476 strncpy((char *)parts[i].uuid, p, max_str_part);
477 free(val);
478 }
479 #ifdef CONFIG_PARTITION_TYPE_GUID
480 /* guid */
481 val = extract_val(tok, "type");
482 if (val) {
483 /* 'type' is optional */
484 if (extract_env(val, &p))
485 p = val;
486 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
487 printf("Wrong type guid format for partition %d\n",
488 i);
489 errno = -4;
490 goto err;
491 }
492 strncpy((char *)parts[i].type_guid, p, max_str_part);
493 free(val);
494 }
495 #endif
496 /* name */
497 val = extract_val(tok, "name");
498 if (!val) { /* name is mandatory */
499 errno = -4;
500 goto err;
501 }
502 if (extract_env(val, &p))
503 p = val;
504 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
505 errno = -4;
506 goto err;
507 }
508 strncpy((char *)parts[i].name, p, max_str_part);
509 free(val);
510
511 /* size */
512 val = extract_val(tok, "size");
513 if (!val) { /* 'size' is mandatory */
514 errno = -4;
515 goto err;
516 }
517 if (extract_env(val, &p))
518 p = val;
519 if ((strcmp(p, "-") == 0)) {
520 /* Let part efi module to auto extend the size */
521 parts[i].size = 0;
522 } else {
523 size_ll = ustrtoull(p, &p, 0);
524 parts[i].size = lldiv(size_ll, dev_desc->blksz);
525 }
526
527 free(val);
528
529 /* start address */
530 val = extract_val(tok, "start");
531 if (val) { /* start address is optional */
532 if (extract_env(val, &p))
533 p = val;
534 start_ll = ustrtoull(p, &p, 0);
535 parts[i].start = lldiv(start_ll, dev_desc->blksz);
536 free(val);
537 }
538
539 offset += parts[i].size + parts[i].start;
540
541 /* bootable */
542 if (found_key(tok, "bootable"))
543 parts[i].bootable = PART_BOOTABLE;
544 }
545
546 *parts_count = p_count;
547 *partitions = parts;
548 free(str);
549
550 return 0;
551 err:
552 free(str);
553 free(*str_disk_guid);
554 free(parts);
555
556 return errno;
557 }
558
559 static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
560 {
561 int ret;
562 char *str_disk_guid;
563 u8 part_count = 0;
564 struct disk_partition *partitions = NULL;
565
566 /* fill partitions */
567 ret = set_gpt_info(blk_dev_desc, str_part,
568 &str_disk_guid, &partitions, &part_count);
569 if (ret) {
570 if (ret == -1)
571 printf("No partition list provided\n");
572 if (ret == -2)
573 printf("Missing disk guid\n");
574 if ((ret == -3) || (ret == -4))
575 printf("Partition list incomplete\n");
576 return -1;
577 }
578
579 /* save partitions layout to disk */
580 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
581 free(str_disk_guid);
582 free(partitions);
583
584 return ret;
585 }
586
587 static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
588 {
589 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
590 blk_dev_desc->blksz);
591 struct disk_partition *partitions = NULL;
592 gpt_entry *gpt_pte = NULL;
593 char *str_disk_guid;
594 u8 part_count = 0;
595 int ret = 0;
596
597 /* fill partitions */
598 ret = set_gpt_info(blk_dev_desc, str_part,
599 &str_disk_guid, &partitions, &part_count);
600 if (ret) {
601 if (ret == -1) {
602 printf("No partition list provided - only basic check\n");
603 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
604 &gpt_pte);
605 goto out;
606 }
607 if (ret == -2)
608 printf("Missing disk guid\n");
609 if ((ret == -3) || (ret == -4))
610 printf("Partition list incomplete\n");
611 return -1;
612 }
613
614 /* Check partition layout with provided pattern */
615 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
616 gpt_head, &gpt_pte);
617 free(str_disk_guid);
618 free(partitions);
619 out:
620 free(gpt_pte);
621 return ret;
622 }
623
624 static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
625 {
626 int ret;
627 char disk_guid[UUID_STR_LEN + 1];
628
629 ret = get_disk_guid(dev_desc, disk_guid);
630 if (ret < 0)
631 return CMD_RET_FAILURE;
632
633 if (namestr)
634 env_set(namestr, disk_guid);
635 else
636 printf("%s\n", disk_guid);
637
638 return ret;
639 }
640
641 #ifdef CONFIG_CMD_GPT_RENAME
642 static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
643 char *name1, char *name2)
644 {
645 struct list_head *pos;
646 struct disk_part *curr;
647 struct disk_partition *new_partitions = NULL;
648 char disk_guid[UUID_STR_LEN + 1];
649 char *partitions_list, *str_disk_guid = NULL;
650 u8 part_count = 0;
651 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
652
653 if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
654 (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
655 return -EINVAL;
656
657 ret = get_disk_guid(dev_desc, disk_guid);
658 if (ret < 0)
659 return ret;
660 /*
661 * Allocates disk_partitions, requiring matching call to del_gpt_info()
662 * if successful.
663 */
664 numparts = get_gpt_info(dev_desc);
665 if (numparts <= 0)
666 return numparts ? numparts : -ENODEV;
667
668 partlistlen = calc_parts_list_len(numparts);
669 partitions_list = malloc(partlistlen);
670 if (!partitions_list) {
671 del_gpt_info();
672 return -ENOMEM;
673 }
674 memset(partitions_list, '\0', partlistlen);
675
676 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
677 if (ret < 0) {
678 free(partitions_list);
679 return ret;
680 }
681 /*
682 * Uncomment the following line to print a string that 'gpt write'
683 * or 'gpt verify' will accept as input.
684 */
685 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
686 (unsigned)strlen(partitions_list));
687
688 /* set_gpt_info allocates new_partitions and str_disk_guid */
689 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
690 &new_partitions, &part_count);
691 if (ret < 0)
692 goto out;
693
694 if (!strcmp(subcomm, "swap")) {
695 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
696 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
697 ret = -EINVAL;
698 goto out;
699 }
700 list_for_each(pos, &disk_partitions) {
701 curr = list_entry(pos, struct disk_part, list);
702 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
703 strcpy((char *)curr->gpt_part_info.name, name2);
704 ctr1++;
705 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
706 strcpy((char *)curr->gpt_part_info.name, name1);
707 ctr2++;
708 }
709 }
710 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
711 printf("Cannot swap partition names except in pairs.\n");
712 ret = -EINVAL;
713 goto out;
714 }
715 } else { /* rename */
716 if (strlen(name2) > PART_NAME_LEN) {
717 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
718 ret = -EINVAL;
719 goto out;
720 }
721 partnum = (int)simple_strtol(name1, NULL, 10);
722 if ((partnum < 0) || (partnum > numparts)) {
723 printf("Illegal partition number %s\n", name1);
724 ret = -EINVAL;
725 goto out;
726 }
727 ret = part_get_info(dev_desc, partnum, new_partitions);
728 if (ret < 0)
729 goto out;
730
731 /* U-Boot partition numbering starts at 1 */
732 list_for_each(pos, &disk_partitions) {
733 curr = list_entry(pos, struct disk_part, list);
734 if (i == partnum) {
735 strcpy((char *)curr->gpt_part_info.name, name2);
736 break;
737 }
738 i++;
739 }
740 }
741
742 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
743 if (ret < 0)
744 goto out;
745 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
746 (unsigned)strlen(partitions_list));
747
748 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
749 &new_partitions, &part_count);
750 /*
751 * Even though valid pointers are here passed into set_gpt_info(),
752 * it mallocs again, and there's no way to tell which failed.
753 */
754 if (ret < 0)
755 goto out;
756
757 debug("Writing new partition table\n");
758 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
759 if (ret < 0) {
760 printf("Writing new partition table failed\n");
761 goto out;
762 }
763
764 debug("Reading back new partition table\n");
765 /*
766 * Empty the existing disk_partitions list, as otherwise the memory in
767 * the original list is unreachable.
768 */
769 del_gpt_info();
770 numparts = get_gpt_info(dev_desc);
771 if (numparts <= 0) {
772 ret = numparts ? numparts : -ENODEV;
773 goto out;
774 }
775 printf("new partition table with %d partitions is:\n", numparts);
776 print_gpt_info();
777 out:
778 del_gpt_info();
779 #ifdef CONFIG_RANDOM_UUID
780 free(str_disk_guid);
781 #endif
782 free(new_partitions);
783 free(partitions_list);
784 return ret;
785 }
786 #endif
787
788 /**
789 * do_gpt(): Perform GPT operations
790 *
791 * @param cmdtp - command name
792 * @param flag
793 * @param argc
794 * @param argv
795 *
796 * @return zero on success; otherwise error
797 */
798 static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
799 {
800 int ret = CMD_RET_SUCCESS;
801 int dev = 0;
802 char *ep;
803 struct blk_desc *blk_dev_desc = NULL;
804
805 #ifndef CONFIG_CMD_GPT_RENAME
806 if (argc < 4 || argc > 5)
807 #else
808 if (argc < 4 || argc > 6)
809 #endif
810 return CMD_RET_USAGE;
811
812 dev = (int)simple_strtoul(argv[3], &ep, 10);
813 if (!ep || ep[0] != '\0') {
814 printf("'%s' is not a number\n", argv[3]);
815 return CMD_RET_USAGE;
816 }
817 blk_dev_desc = blk_get_dev(argv[2], dev);
818 if (!blk_dev_desc) {
819 printf("%s: %s dev %d NOT available\n",
820 __func__, argv[2], dev);
821 return CMD_RET_FAILURE;
822 }
823
824 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
825 printf("Writing GPT: ");
826 ret = gpt_default(blk_dev_desc, argv[4]);
827 } else if ((strcmp(argv[1], "verify") == 0)) {
828 ret = gpt_verify(blk_dev_desc, argv[4]);
829 printf("Verify GPT: ");
830 } else if (strcmp(argv[1], "guid") == 0) {
831 ret = do_disk_guid(blk_dev_desc, argv[4]);
832 #ifdef CONFIG_CMD_GPT_RENAME
833 } else if (strcmp(argv[1], "read") == 0) {
834 ret = do_get_gpt_info(blk_dev_desc);
835 } else if ((strcmp(argv[1], "swap") == 0) ||
836 (strcmp(argv[1], "rename") == 0)) {
837 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
838 #endif
839 } else {
840 return CMD_RET_USAGE;
841 }
842
843 if (ret) {
844 printf("error!\n");
845 return CMD_RET_FAILURE;
846 }
847
848 printf("success!\n");
849 return CMD_RET_SUCCESS;
850 }
851
852 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
853 "GUID Partition Table",
854 "<command> <interface> <dev> <partitions_list>\n"
855 " - GUID partition table restoration and validity check\n"
856 " Restore or verify GPT information on a device connected\n"
857 " to interface\n"
858 " Example usage:\n"
859 " gpt write mmc 0 $partitions\n"
860 " gpt verify mmc 0 $partitions\n"
861 " gpt guid <interface> <dev>\n"
862 " - print disk GUID\n"
863 " gpt guid <interface> <dev> <varname>\n"
864 " - set environment variable to disk GUID\n"
865 " Example usage:\n"
866 " gpt guid mmc 0\n"
867 " gpt guid mmc 0 varname\n"
868 #ifdef CONFIG_CMD_GPT_RENAME
869 "gpt partition renaming commands:\n"
870 " gpt read <interface> <dev>\n"
871 " - read GPT into a data structure for manipulation\n"
872 " gpt swap <interface> <dev> <name1> <name2>\n"
873 " - change all partitions named name1 to name2\n"
874 " and vice-versa\n"
875 " gpt rename <interface> <dev> <part> <name>\n"
876 " - rename the specified partition\n"
877 " Example usage:\n"
878 " gpt swap mmc 0 foo bar\n"
879 " gpt rename mmc 0 3 foo\n"
880 #endif
881 );