gpt.c 22 KB

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