gpt.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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_repair(struct blk_desc *blk_dev_desc)
  521. {
  522. int ret = 0;
  523. ret = gpt_repair_headers(blk_dev_desc);
  524. return ret;
  525. }
  526. static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
  527. {
  528. int ret;
  529. char *str_disk_guid;
  530. u8 part_count = 0;
  531. struct disk_partition *partitions = NULL;
  532. /* fill partitions */
  533. ret = set_gpt_info(blk_dev_desc, str_part,
  534. &str_disk_guid, &partitions, &part_count);
  535. if (ret) {
  536. if (ret == -1)
  537. printf("No partition list provided\n");
  538. if (ret == -2)
  539. printf("Missing disk guid\n");
  540. if ((ret == -3) || (ret == -4))
  541. printf("Partition list incomplete\n");
  542. return -1;
  543. }
  544. /* save partitions layout to disk */
  545. ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
  546. free(str_disk_guid);
  547. free(partitions);
  548. return ret;
  549. }
  550. static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
  551. {
  552. ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
  553. blk_dev_desc->blksz);
  554. struct disk_partition *partitions = NULL;
  555. gpt_entry *gpt_pte = NULL;
  556. char *str_disk_guid;
  557. u8 part_count = 0;
  558. int ret = 0;
  559. /* fill partitions */
  560. ret = set_gpt_info(blk_dev_desc, str_part,
  561. &str_disk_guid, &partitions, &part_count);
  562. if (ret) {
  563. if (ret == -1) {
  564. printf("No partition list provided - only basic check\n");
  565. ret = gpt_verify_headers(blk_dev_desc, gpt_head,
  566. &gpt_pte);
  567. goto out;
  568. }
  569. if (ret == -2)
  570. printf("Missing disk guid\n");
  571. if ((ret == -3) || (ret == -4))
  572. printf("Partition list incomplete\n");
  573. return -1;
  574. }
  575. /* Check partition layout with provided pattern */
  576. ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
  577. gpt_head, &gpt_pte);
  578. free(str_disk_guid);
  579. free(partitions);
  580. out:
  581. free(gpt_pte);
  582. return ret;
  583. }
  584. /**
  585. * gpt_enumerate() - Enumerate partition names into environment variable.
  586. *
  587. * Enumerate partition names. Partition names are stored in gpt_partition_list
  588. * environment variable. Each partition name is delimited by space.
  589. *
  590. * @desc: block device descriptor
  591. *
  592. * @Return: '0' on success and -ve error on failure
  593. */
  594. static int gpt_enumerate(struct blk_desc *desc)
  595. {
  596. struct part_driver *first_drv, *part_drv;
  597. int str_len = 0, tmp_len;
  598. char part_list[2048];
  599. int n_drvs;
  600. char *ptr;
  601. part_list[0] = 0;
  602. n_drvs = part_driver_get_count();
  603. if (!n_drvs) {
  604. printf("Failed to get partition driver count\n");
  605. return -ENOENT;
  606. }
  607. first_drv = part_driver_get_first();
  608. for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
  609. struct disk_partition pinfo;
  610. int ret;
  611. int i;
  612. if (part_drv->test(desc))
  613. continue;
  614. for (i = 1; i < part_drv->max_entries; i++) {
  615. ret = part_drv->get_info(desc, i, &pinfo);
  616. if (ret)
  617. continue;
  618. ptr = &part_list[str_len];
  619. tmp_len = strlen((const char *)pinfo.name);
  620. str_len += tmp_len;
  621. /* +1 for space */
  622. str_len++;
  623. if (str_len > sizeof(part_list)) {
  624. printf("Error insufficient memory\n");
  625. return -ENOMEM;
  626. }
  627. strcpy(ptr, (const char *)pinfo.name);
  628. /* One byte for space(" ") delimiter */
  629. ptr[tmp_len] = ' ';
  630. }
  631. if (*part_list)
  632. part_list[strlen(part_list) - 1] = 0;
  633. break;
  634. }
  635. debug("setenv gpt_partition_list %s\n", part_list);
  636. return env_set("gpt_partition_list", part_list);
  637. }
  638. /**
  639. * gpt_setenv_part_variables() - setup partition environmental variables
  640. *
  641. * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
  642. * and gpt_partition_size environment variables.
  643. *
  644. * @pinfo: pointer to disk partition
  645. * @i: partition entry
  646. *
  647. * @Return: '0' on success and -ENOENT on failure
  648. */
  649. static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
  650. {
  651. int ret;
  652. ret = env_set_hex("gpt_partition_addr", pinfo->start);
  653. if (ret)
  654. goto fail;
  655. ret = env_set_hex("gpt_partition_size", pinfo->size);
  656. if (ret)
  657. goto fail;
  658. ret = env_set_hex("gpt_partition_entry", i);
  659. if (ret)
  660. goto fail;
  661. ret = env_set("gpt_partition_name", (const char *)pinfo->name);
  662. if (ret)
  663. goto fail;
  664. return 0;
  665. fail:
  666. return -ENOENT;
  667. }
  668. /**
  669. * gpt_setenv() - Dynamically setup environment variables.
  670. *
  671. * Dynamically setup environment variables for name, index, offset and size
  672. * for partition in GPT table after running "gpt setenv" for a partition name.
  673. *
  674. * @desc: block device descriptor
  675. * @name: partition name
  676. *
  677. * @Return: '0' on success and -ve err on failure
  678. */
  679. static int gpt_setenv(struct blk_desc *desc, const char *name)
  680. {
  681. struct part_driver *first_drv, *part_drv;
  682. int n_drvs;
  683. int ret = -1;
  684. n_drvs = part_driver_get_count();
  685. if (!n_drvs) {
  686. printf("Failed to get partition driver count\n");
  687. goto fail;
  688. }
  689. first_drv = part_driver_get_first();
  690. for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
  691. struct disk_partition pinfo;
  692. int i;
  693. for (i = 1; i < part_drv->max_entries; i++) {
  694. ret = part_drv->get_info(desc, i, &pinfo);
  695. if (ret)
  696. continue;
  697. if (!strcmp(name, (const char *)pinfo.name)) {
  698. /* match found, setup environment variables */
  699. ret = gpt_setenv_part_variables(&pinfo, i);
  700. if (ret)
  701. goto fail;
  702. return 0;
  703. }
  704. }
  705. }
  706. fail:
  707. return ret;
  708. }
  709. static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
  710. {
  711. int ret;
  712. char disk_guid[UUID_STR_LEN + 1];
  713. ret = get_disk_guid(dev_desc, disk_guid);
  714. if (ret < 0)
  715. return CMD_RET_FAILURE;
  716. if (namestr)
  717. env_set(namestr, disk_guid);
  718. else
  719. printf("%s\n", disk_guid);
  720. return ret;
  721. }
  722. #ifdef CONFIG_CMD_GPT_RENAME
  723. static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
  724. char *name1, char *name2)
  725. {
  726. struct list_head *pos;
  727. struct disk_part *curr;
  728. struct disk_partition *new_partitions = NULL;
  729. char disk_guid[UUID_STR_LEN + 1];
  730. char *partitions_list, *str_disk_guid = NULL;
  731. u8 part_count = 0;
  732. int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
  733. if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
  734. (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
  735. return -EINVAL;
  736. ret = get_disk_guid(dev_desc, disk_guid);
  737. if (ret < 0)
  738. return ret;
  739. /*
  740. * Allocates disk_partitions, requiring matching call to del_gpt_info()
  741. * if successful.
  742. */
  743. numparts = get_gpt_info(dev_desc);
  744. if (numparts <= 0)
  745. return numparts ? numparts : -ENODEV;
  746. partlistlen = calc_parts_list_len(numparts);
  747. partitions_list = malloc(partlistlen);
  748. if (!partitions_list) {
  749. del_gpt_info();
  750. return -ENOMEM;
  751. }
  752. memset(partitions_list, '\0', partlistlen);
  753. ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
  754. if (ret < 0) {
  755. free(partitions_list);
  756. return ret;
  757. }
  758. /*
  759. * Uncomment the following line to print a string that 'gpt write'
  760. * or 'gpt verify' will accept as input.
  761. */
  762. debug("OLD partitions_list is %s with %u chars\n", partitions_list,
  763. (unsigned)strlen(partitions_list));
  764. /* set_gpt_info allocates new_partitions and str_disk_guid */
  765. ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
  766. &new_partitions, &part_count);
  767. if (ret < 0)
  768. goto out;
  769. if (!strcmp(subcomm, "swap")) {
  770. if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
  771. printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
  772. ret = -EINVAL;
  773. goto out;
  774. }
  775. list_for_each(pos, &disk_partitions) {
  776. curr = list_entry(pos, struct disk_part, list);
  777. if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
  778. strcpy((char *)curr->gpt_part_info.name, name2);
  779. ctr1++;
  780. } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
  781. strcpy((char *)curr->gpt_part_info.name, name1);
  782. ctr2++;
  783. }
  784. }
  785. if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
  786. printf("Cannot swap partition names except in pairs.\n");
  787. ret = -EINVAL;
  788. goto out;
  789. }
  790. } else { /* rename */
  791. if (strlen(name2) > PART_NAME_LEN) {
  792. printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
  793. ret = -EINVAL;
  794. goto out;
  795. }
  796. partnum = (int)simple_strtol(name1, NULL, 10);
  797. if ((partnum < 0) || (partnum > numparts)) {
  798. printf("Illegal partition number %s\n", name1);
  799. ret = -EINVAL;
  800. goto out;
  801. }
  802. ret = part_get_info(dev_desc, partnum, new_partitions);
  803. if (ret < 0)
  804. goto out;
  805. /* U-Boot partition numbering starts at 1 */
  806. list_for_each(pos, &disk_partitions) {
  807. curr = list_entry(pos, struct disk_part, list);
  808. if (i == partnum) {
  809. strcpy((char *)curr->gpt_part_info.name, name2);
  810. break;
  811. }
  812. i++;
  813. }
  814. }
  815. ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
  816. if (ret < 0)
  817. goto out;
  818. debug("NEW partitions_list is %s with %u chars\n", partitions_list,
  819. (unsigned)strlen(partitions_list));
  820. ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
  821. &new_partitions, &part_count);
  822. /*
  823. * Even though valid pointers are here passed into set_gpt_info(),
  824. * it mallocs again, and there's no way to tell which failed.
  825. */
  826. if (ret < 0)
  827. goto out;
  828. debug("Writing new partition table\n");
  829. ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
  830. if (ret < 0) {
  831. printf("Writing new partition table failed\n");
  832. goto out;
  833. }
  834. debug("Reading back new partition table\n");
  835. /*
  836. * Empty the existing disk_partitions list, as otherwise the memory in
  837. * the original list is unreachable.
  838. */
  839. del_gpt_info();
  840. numparts = get_gpt_info(dev_desc);
  841. if (numparts <= 0) {
  842. ret = numparts ? numparts : -ENODEV;
  843. goto out;
  844. }
  845. printf("new partition table with %d partitions is:\n", numparts);
  846. print_gpt_info();
  847. out:
  848. del_gpt_info();
  849. #ifdef CONFIG_RANDOM_UUID
  850. free(str_disk_guid);
  851. #endif
  852. free(new_partitions);
  853. free(partitions_list);
  854. return ret;
  855. }
  856. #endif
  857. /**
  858. * do_gpt(): Perform GPT operations
  859. *
  860. * @param cmdtp - command name
  861. * @param flag
  862. * @param argc
  863. * @param argv
  864. *
  865. * Return: zero on success; otherwise error
  866. */
  867. static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  868. {
  869. int ret = CMD_RET_SUCCESS;
  870. int dev = 0;
  871. char *ep;
  872. struct blk_desc *blk_dev_desc = NULL;
  873. #ifndef CONFIG_CMD_GPT_RENAME
  874. if (argc < 4 || argc > 5)
  875. #else
  876. if (argc < 4 || argc > 6)
  877. #endif
  878. return CMD_RET_USAGE;
  879. dev = (int)dectoul(argv[3], &ep);
  880. if (!ep || ep[0] != '\0') {
  881. printf("'%s' is not a number\n", argv[3]);
  882. return CMD_RET_USAGE;
  883. }
  884. blk_dev_desc = blk_get_dev(argv[2], dev);
  885. if (!blk_dev_desc) {
  886. printf("%s: %s dev %d NOT available\n",
  887. __func__, argv[2], dev);
  888. return CMD_RET_FAILURE;
  889. }
  890. if (strcmp(argv[1], "repair") == 0) {
  891. printf("Repairing GPT: ");
  892. ret = gpt_repair(blk_dev_desc);
  893. } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
  894. printf("Writing GPT: ");
  895. ret = gpt_default(blk_dev_desc, argv[4]);
  896. } else if ((strcmp(argv[1], "verify") == 0)) {
  897. ret = gpt_verify(blk_dev_desc, argv[4]);
  898. printf("Verify GPT: ");
  899. } else if ((strcmp(argv[1], "setenv") == 0)) {
  900. ret = gpt_setenv(blk_dev_desc, argv[4]);
  901. } else if ((strcmp(argv[1], "enumerate") == 0)) {
  902. ret = gpt_enumerate(blk_dev_desc);
  903. } else if (strcmp(argv[1], "guid") == 0) {
  904. ret = do_disk_guid(blk_dev_desc, argv[4]);
  905. #ifdef CONFIG_CMD_GPT_RENAME
  906. } else if (strcmp(argv[1], "read") == 0) {
  907. ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
  908. } else if ((strcmp(argv[1], "swap") == 0) ||
  909. (strcmp(argv[1], "rename") == 0)) {
  910. ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
  911. #endif
  912. } else {
  913. return CMD_RET_USAGE;
  914. }
  915. if (ret) {
  916. printf("error!\n");
  917. return CMD_RET_FAILURE;
  918. }
  919. printf("success!\n");
  920. return CMD_RET_SUCCESS;
  921. }
  922. U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
  923. "GUID Partition Table",
  924. "<command> <interface> <dev> <partitions_list>\n"
  925. " - GUID partition table restoration and validity check\n"
  926. " Restore or verify GPT information on a device connected\n"
  927. " to interface\n"
  928. " Example usage:\n"
  929. " gpt repair mmc 0\n"
  930. " - repair the GPT on the device\n"
  931. " gpt write mmc 0 $partitions\n"
  932. " - write the GPT to device\n"
  933. " gpt verify mmc 0 $partitions\n"
  934. " - verify the GPT on device against $partitions\n"
  935. " gpt setenv mmc 0 $name\n"
  936. " - setup environment variables for partition $name:\n"
  937. " gpt_partition_addr, gpt_partition_size,\n"
  938. " gpt_partition_name, gpt_partition_entry\n"
  939. " gpt enumerate mmc 0\n"
  940. " - store list of partitions to gpt_partition_list environment variable\n"
  941. " gpt guid <interface> <dev>\n"
  942. " - print disk GUID\n"
  943. " gpt guid <interface> <dev> <varname>\n"
  944. " - set environment variable to disk GUID\n"
  945. " Example usage:\n"
  946. " gpt guid mmc 0\n"
  947. " gpt guid mmc 0 varname\n"
  948. #ifdef CONFIG_CMD_GPT_RENAME
  949. "gpt partition renaming commands:\n"
  950. " gpt read <interface> <dev> [<varname>]\n"
  951. " - read GPT into a data structure for manipulation\n"
  952. " - read GPT partitions into environment variable\n"
  953. " gpt swap <interface> <dev> <name1> <name2>\n"
  954. " - change all partitions named name1 to name2\n"
  955. " and vice-versa\n"
  956. " gpt rename <interface> <dev> <part> <name>\n"
  957. " - rename the specified partition\n"
  958. " Example usage:\n"
  959. " gpt swap mmc 0 foo bar\n"
  960. " gpt rename mmc 0 3 foo\n"
  961. #endif
  962. );