gpt.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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, char * const namestr)
  311. {
  312. int numparts;
  313. numparts = get_gpt_info(dev_desc);
  314. if (numparts > 0) {
  315. if (namestr) {
  316. char disk_guid[UUID_STR_LEN + 1];
  317. char *partitions_list;
  318. int partlistlen;
  319. int ret = -1;
  320. ret = get_disk_guid(dev_desc, disk_guid);
  321. if (ret < 0)
  322. return ret;
  323. partlistlen = calc_parts_list_len(numparts);
  324. partitions_list = malloc(partlistlen);
  325. if (!partitions_list) {
  326. del_gpt_info();
  327. return -ENOMEM;
  328. }
  329. memset(partitions_list, '\0', partlistlen);
  330. ret = create_gpt_partitions_list(numparts, disk_guid,
  331. partitions_list);
  332. if (ret < 0)
  333. printf("Error: Could not create partition list string!\n");
  334. else
  335. env_set(namestr, partitions_list);
  336. free(partitions_list);
  337. } else {
  338. print_gpt_info();
  339. }
  340. del_gpt_info();
  341. return 0;
  342. }
  343. return numparts;
  344. }
  345. #endif
  346. /**
  347. * set_gpt_info(): Fill partition information from string
  348. * function allocates memory, remember to free!
  349. *
  350. * @param dev_desc - pointer block device descriptor
  351. * @param str_part - pointer to string with partition information
  352. * @param str_disk_guid - pointer to pointer to allocated string with disk guid
  353. * @param partitions - pointer to pointer to allocated partitions array
  354. * @param parts_count - number of partitions
  355. *
  356. * @return - zero on success, otherwise error
  357. *
  358. */
  359. static int set_gpt_info(struct blk_desc *dev_desc,
  360. const char *str_part,
  361. char **str_disk_guid,
  362. struct disk_partition **partitions,
  363. u8 *parts_count)
  364. {
  365. char *tok, *str, *s;
  366. int i;
  367. char *val, *p;
  368. int p_count;
  369. struct disk_partition *parts;
  370. int errno = 0;
  371. uint64_t size_ll, start_ll;
  372. lbaint_t offset = 0;
  373. int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
  374. debug("%s: lba num: 0x%x %d\n", __func__,
  375. (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
  376. if (str_part == NULL)
  377. return -1;
  378. str = strdup(str_part);
  379. if (str == NULL)
  380. return -ENOMEM;
  381. /* extract disk guid */
  382. s = str;
  383. val = extract_val(str, "uuid_disk");
  384. if (!val) {
  385. #ifdef CONFIG_RANDOM_UUID
  386. *str_disk_guid = malloc(UUID_STR_LEN + 1);
  387. if (*str_disk_guid == NULL)
  388. return -ENOMEM;
  389. gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
  390. #else
  391. free(str);
  392. return -2;
  393. #endif
  394. } else {
  395. val = strsep(&val, ";");
  396. if (extract_env(val, &p))
  397. p = val;
  398. *str_disk_guid = strdup(p);
  399. free(val);
  400. /* Move s to first partition */
  401. strsep(&s, ";");
  402. }
  403. if (s == NULL) {
  404. printf("Error: is the partitions string NULL-terminated?\n");
  405. return -EINVAL;
  406. }
  407. if (strnlen(s, max_str_part) == 0)
  408. return -3;
  409. i = strnlen(s, max_str_part) - 1;
  410. if (s[i] == ';')
  411. s[i] = '\0';
  412. /* calculate expected number of partitions */
  413. p_count = 1;
  414. p = s;
  415. while (*p) {
  416. if (*p++ == ';')
  417. p_count++;
  418. }
  419. /* allocate memory for partitions */
  420. parts = calloc(sizeof(struct disk_partition), p_count);
  421. if (parts == NULL)
  422. return -ENOMEM;
  423. /* retrieve partitions data from string */
  424. for (i = 0; i < p_count; i++) {
  425. tok = strsep(&s, ";");
  426. if (tok == NULL)
  427. break;
  428. /* uuid */
  429. val = extract_val(tok, "uuid");
  430. if (!val) {
  431. /* 'uuid' is optional if random uuid's are enabled */
  432. #ifdef CONFIG_RANDOM_UUID
  433. gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
  434. #else
  435. errno = -4;
  436. goto err;
  437. #endif
  438. } else {
  439. if (extract_env(val, &p))
  440. p = val;
  441. if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
  442. printf("Wrong uuid format for partition %d\n", i);
  443. errno = -4;
  444. goto err;
  445. }
  446. strncpy((char *)parts[i].uuid, p, max_str_part);
  447. free(val);
  448. }
  449. #ifdef CONFIG_PARTITION_TYPE_GUID
  450. /* guid */
  451. val = extract_val(tok, "type");
  452. if (val) {
  453. /* 'type' is optional */
  454. if (extract_env(val, &p))
  455. p = val;
  456. if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
  457. printf("Wrong type guid format for partition %d\n",
  458. i);
  459. errno = -4;
  460. goto err;
  461. }
  462. strncpy((char *)parts[i].type_guid, p, max_str_part);
  463. free(val);
  464. }
  465. #endif
  466. /* name */
  467. val = extract_val(tok, "name");
  468. if (!val) { /* name is mandatory */
  469. errno = -4;
  470. goto err;
  471. }
  472. if (extract_env(val, &p))
  473. p = val;
  474. if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
  475. errno = -4;
  476. goto err;
  477. }
  478. strncpy((char *)parts[i].name, p, max_str_part);
  479. free(val);
  480. /* size */
  481. val = extract_val(tok, "size");
  482. if (!val) { /* 'size' is mandatory */
  483. errno = -4;
  484. goto err;
  485. }
  486. if (extract_env(val, &p))
  487. p = val;
  488. if ((strcmp(p, "-") == 0)) {
  489. /* Let part efi module to auto extend the size */
  490. parts[i].size = 0;
  491. } else {
  492. size_ll = ustrtoull(p, &p, 0);
  493. parts[i].size = lldiv(size_ll, dev_desc->blksz);
  494. }
  495. free(val);
  496. /* start address */
  497. val = extract_val(tok, "start");
  498. if (val) { /* start address is optional */
  499. if (extract_env(val, &p))
  500. p = val;
  501. start_ll = ustrtoull(p, &p, 0);
  502. parts[i].start = lldiv(start_ll, dev_desc->blksz);
  503. free(val);
  504. }
  505. offset += parts[i].size + parts[i].start;
  506. /* bootable */
  507. if (found_key(tok, "bootable"))
  508. parts[i].bootable = PART_BOOTABLE;
  509. }
  510. *parts_count = p_count;
  511. *partitions = parts;
  512. free(str);
  513. return 0;
  514. err:
  515. free(str);
  516. free(*str_disk_guid);
  517. free(parts);
  518. return errno;
  519. }
  520. static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
  521. {
  522. int ret;
  523. char *str_disk_guid;
  524. u8 part_count = 0;
  525. struct disk_partition *partitions = NULL;
  526. /* fill partitions */
  527. ret = set_gpt_info(blk_dev_desc, str_part,
  528. &str_disk_guid, &partitions, &part_count);
  529. if (ret) {
  530. if (ret == -1)
  531. printf("No partition list provided\n");
  532. if (ret == -2)
  533. printf("Missing disk guid\n");
  534. if ((ret == -3) || (ret == -4))
  535. printf("Partition list incomplete\n");
  536. return -1;
  537. }
  538. /* save partitions layout to disk */
  539. ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
  540. free(str_disk_guid);
  541. free(partitions);
  542. return ret;
  543. }
  544. static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
  545. {
  546. ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
  547. blk_dev_desc->blksz);
  548. struct disk_partition *partitions = NULL;
  549. gpt_entry *gpt_pte = NULL;
  550. char *str_disk_guid;
  551. u8 part_count = 0;
  552. int ret = 0;
  553. /* fill partitions */
  554. ret = set_gpt_info(blk_dev_desc, str_part,
  555. &str_disk_guid, &partitions, &part_count);
  556. if (ret) {
  557. if (ret == -1) {
  558. printf("No partition list provided - only basic check\n");
  559. ret = gpt_verify_headers(blk_dev_desc, gpt_head,
  560. &gpt_pte);
  561. goto out;
  562. }
  563. if (ret == -2)
  564. printf("Missing disk guid\n");
  565. if ((ret == -3) || (ret == -4))
  566. printf("Partition list incomplete\n");
  567. return -1;
  568. }
  569. /* Check partition layout with provided pattern */
  570. ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
  571. gpt_head, &gpt_pte);
  572. free(str_disk_guid);
  573. free(partitions);
  574. out:
  575. free(gpt_pte);
  576. return ret;
  577. }
  578. /**
  579. * gpt_enumerate() - Enumerate partition names into environment variable.
  580. *
  581. * Enumerate partition names. Partition names are stored in gpt_partition_list
  582. * environment variable. Each partition name is delimited by space.
  583. *
  584. * @desc: block device descriptor
  585. *
  586. * @Return: '0' on success and -ve error on failure
  587. */
  588. static int gpt_enumerate(struct blk_desc *desc)
  589. {
  590. struct part_driver *first_drv, *part_drv;
  591. int str_len = 0, tmp_len;
  592. char part_list[2048];
  593. int n_drvs;
  594. char *ptr;
  595. part_list[0] = 0;
  596. n_drvs = part_driver_get_count();
  597. if (!n_drvs) {
  598. printf("Failed to get partition driver count\n");
  599. return -ENOENT;
  600. }
  601. first_drv = part_driver_get_first();
  602. for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
  603. struct disk_partition pinfo;
  604. int ret;
  605. int i;
  606. for (i = 1; i < part_drv->max_entries; i++) {
  607. ret = part_drv->get_info(desc, i, &pinfo);
  608. if (ret) {
  609. /* no more entries in table */
  610. break;
  611. }
  612. ptr = &part_list[str_len];
  613. tmp_len = strlen((const char *)pinfo.name);
  614. str_len += tmp_len;
  615. /* +1 for space */
  616. str_len++;
  617. if (str_len > sizeof(part_list)) {
  618. printf("Error insufficient memory\n");
  619. return -ENOMEM;
  620. }
  621. strcpy(ptr, (const char *)pinfo.name);
  622. /* One byte for space(" ") delimiter */
  623. ptr[tmp_len] = ' ';
  624. }
  625. }
  626. if (*part_list)
  627. part_list[strlen(part_list) - 1] = 0;
  628. debug("setenv gpt_partition_list %s\n", part_list);
  629. return env_set("gpt_partition_list", part_list);
  630. }
  631. /**
  632. * gpt_setenv_part_variables() - setup partition environmental variables
  633. *
  634. * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
  635. * and gpt_partition_size environment variables.
  636. *
  637. * @pinfo: pointer to disk partition
  638. * @i: partition entry
  639. *
  640. * @Return: '0' on success and -ENOENT on failure
  641. */
  642. static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
  643. {
  644. int ret;
  645. ret = env_set_hex("gpt_partition_addr", pinfo->start);
  646. if (ret)
  647. goto fail;
  648. ret = env_set_hex("gpt_partition_size", pinfo->size);
  649. if (ret)
  650. goto fail;
  651. ret = env_set_ulong("gpt_partition_entry", i);
  652. if (ret)
  653. goto fail;
  654. ret = env_set("gpt_partition_name", (const char *)pinfo->name);
  655. if (ret)
  656. goto fail;
  657. return 0;
  658. fail:
  659. return -ENOENT;
  660. }
  661. /**
  662. * gpt_setenv() - Dynamically setup environment variables.
  663. *
  664. * Dynamically setup environment variables for name, index, offset and size
  665. * for partition in GPT table after running "gpt setenv" for a partition name.
  666. *
  667. * @desc: block device descriptor
  668. * @name: partition name
  669. *
  670. * @Return: '0' on success and -ve err on failure
  671. */
  672. static int gpt_setenv(struct blk_desc *desc, const char *name)
  673. {
  674. struct part_driver *first_drv, *part_drv;
  675. int n_drvs;
  676. int ret = -1;
  677. n_drvs = part_driver_get_count();
  678. if (!n_drvs) {
  679. printf("Failed to get partition driver count\n");
  680. goto fail;
  681. }
  682. first_drv = part_driver_get_first();
  683. for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
  684. struct disk_partition pinfo;
  685. int i;
  686. for (i = 1; i < part_drv->max_entries; i++) {
  687. ret = part_drv->get_info(desc, i, &pinfo);
  688. if (ret) {
  689. /* no more entries in table */
  690. break;
  691. }
  692. if (!strcmp(name, (const char *)pinfo.name)) {
  693. /* match found, setup environment variables */
  694. ret = gpt_setenv_part_variables(&pinfo, i);
  695. if (ret)
  696. goto fail;
  697. return 0;
  698. }
  699. }
  700. }
  701. fail:
  702. return ret;
  703. }
  704. static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
  705. {
  706. int ret;
  707. char disk_guid[UUID_STR_LEN + 1];
  708. ret = get_disk_guid(dev_desc, disk_guid);
  709. if (ret < 0)
  710. return CMD_RET_FAILURE;
  711. if (namestr)
  712. env_set(namestr, disk_guid);
  713. else
  714. printf("%s\n", disk_guid);
  715. return ret;
  716. }
  717. #ifdef CONFIG_CMD_GPT_RENAME
  718. static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
  719. char *name1, char *name2)
  720. {
  721. struct list_head *pos;
  722. struct disk_part *curr;
  723. struct disk_partition *new_partitions = NULL;
  724. char disk_guid[UUID_STR_LEN + 1];
  725. char *partitions_list, *str_disk_guid = NULL;
  726. u8 part_count = 0;
  727. int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
  728. if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
  729. (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
  730. return -EINVAL;
  731. ret = get_disk_guid(dev_desc, disk_guid);
  732. if (ret < 0)
  733. return ret;
  734. /*
  735. * Allocates disk_partitions, requiring matching call to del_gpt_info()
  736. * if successful.
  737. */
  738. numparts = get_gpt_info(dev_desc);
  739. if (numparts <= 0)
  740. return numparts ? numparts : -ENODEV;
  741. partlistlen = calc_parts_list_len(numparts);
  742. partitions_list = malloc(partlistlen);
  743. if (!partitions_list) {
  744. del_gpt_info();
  745. return -ENOMEM;
  746. }
  747. memset(partitions_list, '\0', partlistlen);
  748. ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
  749. if (ret < 0) {
  750. free(partitions_list);
  751. return ret;
  752. }
  753. /*
  754. * Uncomment the following line to print a string that 'gpt write'
  755. * or 'gpt verify' will accept as input.
  756. */
  757. debug("OLD partitions_list is %s with %u chars\n", partitions_list,
  758. (unsigned)strlen(partitions_list));
  759. /* set_gpt_info allocates new_partitions and str_disk_guid */
  760. ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
  761. &new_partitions, &part_count);
  762. if (ret < 0)
  763. goto out;
  764. if (!strcmp(subcomm, "swap")) {
  765. if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
  766. printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
  767. ret = -EINVAL;
  768. goto out;
  769. }
  770. list_for_each(pos, &disk_partitions) {
  771. curr = list_entry(pos, struct disk_part, list);
  772. if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
  773. strcpy((char *)curr->gpt_part_info.name, name2);
  774. ctr1++;
  775. } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
  776. strcpy((char *)curr->gpt_part_info.name, name1);
  777. ctr2++;
  778. }
  779. }
  780. if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
  781. printf("Cannot swap partition names except in pairs.\n");
  782. ret = -EINVAL;
  783. goto out;
  784. }
  785. } else { /* rename */
  786. if (strlen(name2) > PART_NAME_LEN) {
  787. printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
  788. ret = -EINVAL;
  789. goto out;
  790. }
  791. partnum = (int)simple_strtol(name1, NULL, 10);
  792. if ((partnum < 0) || (partnum > numparts)) {
  793. printf("Illegal partition number %s\n", name1);
  794. ret = -EINVAL;
  795. goto out;
  796. }
  797. ret = part_get_info(dev_desc, partnum, new_partitions);
  798. if (ret < 0)
  799. goto out;
  800. /* U-Boot partition numbering starts at 1 */
  801. list_for_each(pos, &disk_partitions) {
  802. curr = list_entry(pos, struct disk_part, list);
  803. if (i == partnum) {
  804. strcpy((char *)curr->gpt_part_info.name, name2);
  805. break;
  806. }
  807. i++;
  808. }
  809. }
  810. ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
  811. if (ret < 0)
  812. goto out;
  813. debug("NEW partitions_list is %s with %u chars\n", partitions_list,
  814. (unsigned)strlen(partitions_list));
  815. ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
  816. &new_partitions, &part_count);
  817. /*
  818. * Even though valid pointers are here passed into set_gpt_info(),
  819. * it mallocs again, and there's no way to tell which failed.
  820. */
  821. if (ret < 0)
  822. goto out;
  823. debug("Writing new partition table\n");
  824. ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
  825. if (ret < 0) {
  826. printf("Writing new partition table failed\n");
  827. goto out;
  828. }
  829. debug("Reading back new partition table\n");
  830. /*
  831. * Empty the existing disk_partitions list, as otherwise the memory in
  832. * the original list is unreachable.
  833. */
  834. del_gpt_info();
  835. numparts = get_gpt_info(dev_desc);
  836. if (numparts <= 0) {
  837. ret = numparts ? numparts : -ENODEV;
  838. goto out;
  839. }
  840. printf("new partition table with %d partitions is:\n", numparts);
  841. print_gpt_info();
  842. out:
  843. del_gpt_info();
  844. #ifdef CONFIG_RANDOM_UUID
  845. free(str_disk_guid);
  846. #endif
  847. free(new_partitions);
  848. free(partitions_list);
  849. return ret;
  850. }
  851. #endif
  852. /**
  853. * do_gpt(): Perform GPT operations
  854. *
  855. * @param cmdtp - command name
  856. * @param flag
  857. * @param argc
  858. * @param argv
  859. *
  860. * @return zero on success; otherwise error
  861. */
  862. static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  863. {
  864. int ret = CMD_RET_SUCCESS;
  865. int dev = 0;
  866. char *ep;
  867. struct blk_desc *blk_dev_desc = NULL;
  868. #ifndef CONFIG_CMD_GPT_RENAME
  869. if (argc < 4 || argc > 5)
  870. #else
  871. if (argc < 4 || argc > 6)
  872. #endif
  873. return CMD_RET_USAGE;
  874. dev = (int)dectoul(argv[3], &ep);
  875. if (!ep || ep[0] != '\0') {
  876. printf("'%s' is not a number\n", argv[3]);
  877. return CMD_RET_USAGE;
  878. }
  879. blk_dev_desc = blk_get_dev(argv[2], dev);
  880. if (!blk_dev_desc) {
  881. printf("%s: %s dev %d NOT available\n",
  882. __func__, argv[2], dev);
  883. return CMD_RET_FAILURE;
  884. }
  885. if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
  886. printf("Writing GPT: ");
  887. ret = gpt_default(blk_dev_desc, argv[4]);
  888. } else if ((strcmp(argv[1], "verify") == 0)) {
  889. ret = gpt_verify(blk_dev_desc, argv[4]);
  890. printf("Verify GPT: ");
  891. } else if ((strcmp(argv[1], "setenv") == 0)) {
  892. ret = gpt_setenv(blk_dev_desc, argv[4]);
  893. } else if ((strcmp(argv[1], "enumerate") == 0)) {
  894. ret = gpt_enumerate(blk_dev_desc);
  895. } else if (strcmp(argv[1], "guid") == 0) {
  896. ret = do_disk_guid(blk_dev_desc, argv[4]);
  897. #ifdef CONFIG_CMD_GPT_RENAME
  898. } else if (strcmp(argv[1], "read") == 0) {
  899. ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
  900. } else if ((strcmp(argv[1], "swap") == 0) ||
  901. (strcmp(argv[1], "rename") == 0)) {
  902. ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
  903. #endif
  904. } else {
  905. return CMD_RET_USAGE;
  906. }
  907. if (ret) {
  908. printf("error!\n");
  909. return CMD_RET_FAILURE;
  910. }
  911. printf("success!\n");
  912. return CMD_RET_SUCCESS;
  913. }
  914. U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
  915. "GUID Partition Table",
  916. "<command> <interface> <dev> <partitions_list>\n"
  917. " - GUID partition table restoration and validity check\n"
  918. " Restore or verify GPT information on a device connected\n"
  919. " to interface\n"
  920. " Example usage:\n"
  921. " gpt write mmc 0 $partitions\n"
  922. " - write the GPT to device\n"
  923. " gpt verify mmc 0 $partitions\n"
  924. " - verify the GPT on device against $partitions\n"
  925. " gpt setenv mmc 0 $name\n"
  926. " - setup environment variables for partition $name:\n"
  927. " gpt_partition_addr, gpt_partition_size,\n"
  928. " gpt_partition_name, gpt_partition_entry\n"
  929. " gpt enumerate mmc 0\n"
  930. " - store list of partitions to gpt_partition_list environment variable\n"
  931. " read <interface> <dev>\n"
  932. " - read GPT into a data structure for manipulation\n"
  933. " gpt guid <interface> <dev>\n"
  934. " - print disk GUID\n"
  935. " gpt guid <interface> <dev> <varname>\n"
  936. " - set environment variable to disk GUID\n"
  937. " Example usage:\n"
  938. " gpt guid mmc 0\n"
  939. " gpt guid mmc 0 varname\n"
  940. #ifdef CONFIG_CMD_GPT_RENAME
  941. "gpt partition renaming commands:\n"
  942. " gpt read <interface> <dev> [<varname>]\n"
  943. " - read GPT into a data structure for manipulation\n"
  944. " - read GPT partitions into environment variable\n"
  945. " gpt swap <interface> <dev> <name1> <name2>\n"
  946. " - change all partitions named name1 to name2\n"
  947. " and vice-versa\n"
  948. " gpt rename <interface> <dev> <part> <name>\n"
  949. " - rename the specified partition\n"
  950. " Example usage:\n"
  951. " gpt swap mmc 0 foo bar\n"
  952. " gpt rename mmc 0 3 foo\n"
  953. #endif
  954. );