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