]> Git Repo - J-u-boot.git/blame - cmd/gpt.c
patman: Suppress empty changelog entries
[J-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 <[email protected]>
7 *
8b096237
PW
8 * Copyright (C) 2012 Samsung Electronics
9 * author: Lukasz Majewski <[email protected]>
10 * author: Piotr Wilczek <[email protected]>
8b096237
PW
11 */
12
13#include <common.h>
e6f6f9e6 14#include <blk.h>
9fb625ce 15#include <env.h>
f7ae49fc 16#include <log.h>
8b096237
PW
17#include <malloc.h>
18#include <command.h>
e6f6f9e6 19#include <part.h>
8b096237
PW
20#include <part_efi.h>
21#include <exports.h>
ba06b3c5 22#include <uuid.h>
8b096237 23#include <linux/ctype.h>
3e34cf7b 24#include <div64.h>
bbb9ffac 25#include <memalign.h>
09a49930 26#include <linux/compat.h>
61b29b82 27#include <linux/err.h>
203f9b48
AC
28#include <linux/sizes.h>
29#include <stdlib.h>
09a49930
AC
30
31static LIST_HEAD(disk_partitions);
8b096237 32
8b096237
PW
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 */
39206382 42static int extract_env(const char *str, char **env)
8b096237 43{
39206382 44 int ret = -1;
8b096237 45 char *e, *s;
39206382
PM
46#ifdef CONFIG_RANDOM_UUID
47 char uuid_str[UUID_STR_LEN + 1];
48#endif
8b096237
PW
49
50 if (!str || strlen(str) < 4)
51 return -1;
52
39206382
PM
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
00caae6d 63 e = env_get(s);
39206382
PM
64 if (e == NULL) {
65#ifdef CONFIG_RANDOM_UUID
66 debug("%s unset. ", str);
9da52f8f 67 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
382bee57 68 env_set(s, uuid_str);
39206382 69
00caae6d 70 e = env_get(s);
39206382
PM
71 if (e) {
72 debug("Set to random.\n");
73 ret = 0;
74 } else {
75 debug("Can't get random UUID.\n");
8b096237 76 }
39206382
PM
77#else
78 debug("%s unset.\n", str);
79#endif
80 } else {
81 debug("%s get from environment.\n", str);
82 ret = 0;
8b096237
PW
83 }
84
39206382
PM
85 *env = e;
86 free(s);
87
88 return ret;
8b096237
PW
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 */
101static 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
cfdaf4ca
PD
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 */
138static 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
2fcaa413
AC
164static 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
09a49930
AC
183#ifdef CONFIG_CMD_GPT_RENAME
184static 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
0528979f
SG
195static struct disk_part *allocate_disk_part(struct disk_partition *info,
196 int partnum)
09a49930
AC
197{
198 struct disk_part *newpart;
f66bc0e0 199 newpart = calloc(1, sizeof(struct disk_part));
09a49930
AC
200 if (!newpart)
201 return ERR_PTR(-ENOMEM);
09a49930
AC
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
203f9b48
AC
224static 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
09a49930
AC
234static void print_gpt_info(void)
235{
236 struct list_head *pos;
237 struct disk_part *curr;
203f9b48
AC
238 char partstartstr[16];
239 char partsizestr[16];
09a49930
AC
240
241 list_for_each(pos, &disk_partitions) {
242 curr = list_entry(pos, struct disk_part, list);
203f9b48
AC
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
09a49930 248 printf("Partition %d:\n", curr->partnum);
203f9b48 249 printf("Start %s, size %s\n", partstartstr, partsizestr);
09a49930
AC
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,
25801acc 253 curr->gpt_part_info.bootable & PART_BOOTABLE);
09a49930
AC
254#ifdef CONFIG_PARTITION_UUIDS
255 printf("UUID %s\n", curr->gpt_part_info.uuid);
256#endif
257 printf("\n");
258 }
259}
260
203f9b48
AC
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 */
272static 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);
3a2605fa
PD
291 sprintf(partstr, ",start=0x%llx",
292 (unsigned long long)curr->gpt_part_info.start *
293 curr->gpt_part_info.blksz);
203f9b48
AC
294 /* one extra byte for NULL */
295 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
3a2605fa
PD
296 sprintf(partstr, ",size=0x%llx",
297 (unsigned long long)curr->gpt_part_info.size *
298 curr->gpt_part_info.blksz);
203f9b48
AC
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
09a49930
AC
309/*
310 * read partition info into disk_partitions list where
311 * it can be printed or modified
312 */
313static 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;
0528979f 317 struct disk_partition info;
09a49930
AC
318 struct disk_part *new_disk_part;
319
203f9b48
AC
320 /*
321 * Always re-read partition info from device, in case
322 * it has changed
323 */
324 INIT_LIST_HEAD(&disk_partitions);
09a49930
AC
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 */
352static 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
8b096237
PW
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 */
4101f687 379static int set_gpt_info(struct blk_desc *dev_desc,
8b096237
PW
380 const char *str_part,
381 char **str_disk_guid,
0528979f 382 struct disk_partition **partitions,
8b096237
PW
383 u8 *parts_count)
384{
385 char *tok, *str, *s;
386 int i;
387 char *val, *p;
388 int p_count;
0528979f 389 struct disk_partition *parts;
8b096237 390 int errno = 0;
3e34cf7b 391 uint64_t size_ll, start_ll;
66636235 392 lbaint_t offset = 0;
2fcaa413 393 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
8b096237 394
619f0fdf 395 debug("%s: lba num: 0x%x %d\n", __func__,
8b096237
PW
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);
203f9b48
AC
402 if (str == NULL)
403 return -ENOMEM;
8b096237
PW
404
405 /* extract disk guid */
406 s = str;
0c7e8d13 407 val = extract_val(str, "uuid_disk");
8b096237 408 if (!val) {
0c7e8d13
RH
409#ifdef CONFIG_RANDOM_UUID
410 *str_disk_guid = malloc(UUID_STR_LEN + 1);
bf52fcde 411 if (*str_disk_guid == NULL)
2fcaa413 412 return -ENOMEM;
0c7e8d13
RH
413 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
414#else
8b096237
PW
415 free(str);
416 return -2;
0c7e8d13
RH
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, ";");
8b096237 426 }
2fcaa413
AC
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)
8b096237
PW
432 return -3;
433
2fcaa413 434 i = strnlen(s, max_str_part) - 1;
8b096237
PW
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 */
0528979f 447 parts = calloc(sizeof(struct disk_partition), p_count);
2fcaa413
AC
448 if (parts == NULL)
449 return -ENOMEM;
8b096237 450
1f8b546f 451 /* retrieve partitions data from string */
8b096237
PW
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");
0c7e8d13
RH
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
8b096237
PW
465 errno = -4;
466 goto err;
0c7e8d13
RH
467#endif
468 } else {
469 if (extract_env(val, &p))
470 p = val;
2fcaa413 471 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
0c7e8d13
RH
472 printf("Wrong uuid format for partition %d\n", i);
473 errno = -4;
474 goto err;
475 }
2fcaa413 476 strncpy((char *)parts[i].uuid, p, max_str_part);
0c7e8d13 477 free(val);
8b096237 478 }
7561b258
PD
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;
2fcaa413 486 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
7561b258
PD
487 printf("Wrong type guid format for partition %d\n",
488 i);
489 errno = -4;
490 goto err;
491 }
2fcaa413 492 strncpy((char *)parts[i].type_guid, p, max_str_part);
7561b258
PD
493 free(val);
494 }
495#endif
8b096237
PW
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;
2fcaa413 504 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
8b096237
PW
505 errno = -4;
506 goto err;
507 }
2fcaa413 508 strncpy((char *)parts[i].name, p, max_str_part);
8b096237
PW
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;
66636235 519 if ((strcmp(p, "-") == 0)) {
c2fdd345
KY
520 /* Let part efi module to auto extend the size */
521 parts[i].size = 0;
66636235
MT
522 } else {
523 size_ll = ustrtoull(p, &p, 0);
524 parts[i].size = lldiv(size_ll, dev_desc->blksz);
525 }
526
8b096237
PW
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;
3e34cf7b
PW
534 start_ll = ustrtoull(p, &p, 0);
535 parts[i].start = lldiv(start_ll, dev_desc->blksz);
8b096237
PW
536 free(val);
537 }
cfdaf4ca 538
66636235
MT
539 offset += parts[i].size + parts[i].start;
540
cfdaf4ca
PD
541 /* bootable */
542 if (found_key(tok, "bootable"))
25801acc 543 parts[i].bootable = PART_BOOTABLE;
8b096237
PW
544 }
545
546 *parts_count = p_count;
547 *partitions = parts;
548 free(str);
549
550 return 0;
551err:
552 free(str);
553 free(*str_disk_guid);
554 free(parts);
555
556 return errno;
557}
558
4101f687 559static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
8b096237
PW
560{
561 int ret;
562 char *str_disk_guid;
563 u8 part_count = 0;
0528979f 564 struct disk_partition *partitions = NULL;
8b096237 565
8b096237 566 /* fill partitions */
619f0fdf 567 ret = set_gpt_info(blk_dev_desc, str_part,
8b096237
PW
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 */
a150e6c9 580 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
8b096237
PW
581 free(str_disk_guid);
582 free(partitions);
583
a150e6c9 584 return ret;
8b096237
PW
585}
586
4101f687 587static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
bbb9ffac
LM
588{
589 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
590 blk_dev_desc->blksz);
0528979f 591 struct disk_partition *partitions = NULL;
bbb9ffac
LM
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
73d6d18b
AC
624static 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)
382bee57 634 env_set(namestr, disk_guid);
73d6d18b
AC
635 else
636 printf("%s\n", disk_guid);
637
638 return ret;
639}
640
203f9b48
AC
641#ifdef CONFIG_CMD_GPT_RENAME
642static 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;
0528979f 647 struct disk_partition *new_partitions = NULL;
203f9b48 648 char disk_guid[UUID_STR_LEN + 1];
5749faa3 649 char *partitions_list, *str_disk_guid = NULL;
203f9b48
AC
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;
18030d04
AC
660 /*
661 * Allocates disk_partitions, requiring matching call to del_gpt_info()
662 * if successful.
663 */
203f9b48
AC
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);
18030d04
AC
670 if (!partitions_list) {
671 del_gpt_info();
203f9b48 672 return -ENOMEM;
18030d04 673 }
203f9b48
AC
674 memset(partitions_list, '\0', partlistlen);
675
676 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
18030d04
AC
677 if (ret < 0) {
678 free(partitions_list);
203f9b48 679 return ret;
18030d04 680 }
203f9b48
AC
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
18030d04 688 /* set_gpt_info allocates new_partitions and str_disk_guid */
203f9b48
AC
689 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
690 &new_partitions, &part_count);
5749faa3
TR
691 if (ret < 0)
692 goto out;
203f9b48
AC
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);
18030d04
AC
697 ret = -EINVAL;
698 goto out;
203f9b48
AC
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");
18030d04
AC
712 ret = -EINVAL;
713 goto out;
203f9b48
AC
714 }
715 } else { /* rename */
716 if (strlen(name2) > PART_NAME_LEN) {
717 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
18030d04
AC
718 ret = -EINVAL;
719 goto out;
203f9b48
AC
720 }
721 partnum = (int)simple_strtol(name1, NULL, 10);
722 if ((partnum < 0) || (partnum > numparts)) {
723 printf("Illegal partition number %s\n", name1);
18030d04
AC
724 ret = -EINVAL;
725 goto out;
203f9b48
AC
726 }
727 ret = part_get_info(dev_desc, partnum, new_partitions);
728 if (ret < 0)
18030d04 729 goto out;
203f9b48
AC
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)
18030d04 744 goto out;
203f9b48
AC
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);
18030d04
AC
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 */
5749faa3
TR
754 if (ret < 0)
755 goto out;
203f9b48
AC
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");
18030d04 761 goto out;
203f9b48
AC
762 }
763
764 debug("Reading back new partition table\n");
18030d04
AC
765 /*
766 * Empty the existing disk_partitions list, as otherwise the memory in
767 * the original list is unreachable.
768 */
769 del_gpt_info();
203f9b48 770 numparts = get_gpt_info(dev_desc);
18030d04
AC
771 if (numparts <= 0) {
772 ret = numparts ? numparts : -ENODEV;
773 goto out;
774 }
203f9b48
AC
775 printf("new partition table with %d partitions is:\n", numparts);
776 print_gpt_info();
18030d04 777 out:
5749faa3
TR
778 del_gpt_info();
779#ifdef CONFIG_RANDOM_UUID
b142d0ac 780 free(str_disk_guid);
5749faa3 781#endif
b142d0ac 782 free(new_partitions);
18030d04 783 free(partitions_list);
203f9b48
AC
784 return ret;
785}
786#endif
787
8b096237
PW
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 */
09140113 798static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
8b096237
PW
799{
800 int ret = CMD_RET_SUCCESS;
801 int dev = 0;
619f0fdf 802 char *ep;
4101f687 803 struct blk_desc *blk_dev_desc = NULL;
8b096237 804
203f9b48 805#ifndef CONFIG_CMD_GPT_RENAME
bbb9ffac 806 if (argc < 4 || argc > 5)
203f9b48
AC
807#else
808 if (argc < 4 || argc > 6)
809#endif
8b096237
PW
810 return CMD_RET_USAGE;
811
bbb9ffac
LM
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 }
db1d9e78 817 blk_dev_desc = blk_get_dev(argv[2], dev);
bbb9ffac
LM
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 }
39206382 823
bbb9ffac
LM
824 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
825 printf("Writing GPT: ");
39206382 826 ret = gpt_default(blk_dev_desc, argv[4]);
bbb9ffac
LM
827 } else if ((strcmp(argv[1], "verify") == 0)) {
828 ret = gpt_verify(blk_dev_desc, argv[4]);
829 printf("Verify GPT: ");
73d6d18b
AC
830 } else if (strcmp(argv[1], "guid") == 0) {
831 ret = do_disk_guid(blk_dev_desc, argv[4]);
09a49930
AC
832#ifdef CONFIG_CMD_GPT_RENAME
833 } else if (strcmp(argv[1], "read") == 0) {
834 ret = do_get_gpt_info(blk_dev_desc);
203f9b48
AC
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]);
09a49930 838#endif
8b096237
PW
839 } else {
840 return CMD_RET_USAGE;
841 }
bbb9ffac
LM
842
843 if (ret) {
844 printf("error!\n");
845 return CMD_RET_FAILURE;
846 }
847
848 printf("success!\n");
849 return CMD_RET_SUCCESS;
8b096237
PW
850}
851
852U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
853 "GUID Partition Table",
1f8b546f 854 "<command> <interface> <dev> <partitions_list>\n"
74f889b0
LM
855 " - GUID partition table restoration and validity check\n"
856 " Restore or verify GPT information on a device connected\n"
8b096237 857 " to interface\n"
74f889b0
LM
858 " Example usage:\n"
859 " gpt write mmc 0 $partitions\n"
860 " gpt verify mmc 0 $partitions\n"
d0266096 861 " gpt guid <interface> <dev>\n"
73d6d18b 862 " - print disk GUID\n"
d0266096 863 " gpt guid <interface> <dev> <varname>\n"
73d6d18b
AC
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"
203f9b48
AC
868#ifdef CONFIG_CMD_GPT_RENAME
869 "gpt partition renaming commands:\n"
d0266096
ER
870 " gpt read <interface> <dev>\n"
871 " - read GPT into a data structure for manipulation\n"
872 " gpt swap <interface> <dev> <name1> <name2>\n"
203f9b48
AC
873 " - change all partitions named name1 to name2\n"
874 " and vice-versa\n"
d0266096 875 " gpt rename <interface> <dev> <part> <name>\n"
203f9b48
AC
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
8b096237 881);
This page took 0.469119 seconds and 4 git commands to generate.