gpt.c 25 KB

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