gpt.c 22 KB

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