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