gpt.c 22 KB

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