gpt.c 21 KB

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