gpt.c 22 KB

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