gpt.c 21 KB

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