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