gpt.c 21 KB

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