gpt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * cmd_gpt.c -- GPT (GUID Partition Table) handling command
  3. *
  4. * Copyright (C) 2015
  5. * Lukasz Majewski <l.majewski@majess.pl>
  6. *
  7. * Copyright (C) 2012 Samsung Electronics
  8. * author: Lukasz Majewski <l.majewski@samsung.com>
  9. * author: Piotr Wilczek <p.wilczek@samsung.com>
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <malloc.h>
  15. #include <command.h>
  16. #include <part_efi.h>
  17. #include <exports.h>
  18. #include <linux/ctype.h>
  19. #include <div64.h>
  20. #include <memalign.h>
  21. #include <linux/compat.h>
  22. static LIST_HEAD(disk_partitions);
  23. /**
  24. * extract_env(): Expand env name from string format '&{env_name}'
  25. * and return pointer to the env (if the env is set)
  26. *
  27. * @param str - pointer to string
  28. * @param env - pointer to pointer to extracted env
  29. *
  30. * @return - zero on successful expand and env is set
  31. */
  32. static int extract_env(const char *str, char **env)
  33. {
  34. int ret = -1;
  35. char *e, *s;
  36. #ifdef CONFIG_RANDOM_UUID
  37. char uuid_str[UUID_STR_LEN + 1];
  38. #endif
  39. if (!str || strlen(str) < 4)
  40. return -1;
  41. if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
  42. return -1;
  43. s = strdup(str);
  44. if (s == NULL)
  45. return -1;
  46. memset(s + strlen(s) - 1, '\0', 1);
  47. memmove(s, s + 2, strlen(s) - 1);
  48. e = getenv(s);
  49. if (e == NULL) {
  50. #ifdef CONFIG_RANDOM_UUID
  51. debug("%s unset. ", str);
  52. gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
  53. setenv(s, uuid_str);
  54. e = getenv(s);
  55. if (e) {
  56. debug("Set to random.\n");
  57. ret = 0;
  58. } else {
  59. debug("Can't get random UUID.\n");
  60. }
  61. #else
  62. debug("%s unset.\n", str);
  63. #endif
  64. } else {
  65. debug("%s get from environment.\n", str);
  66. ret = 0;
  67. }
  68. *env = e;
  69. free(s);
  70. return ret;
  71. }
  72. /**
  73. * extract_val(): Extract value from a key=value pair list (comma separated).
  74. * Only value for the given key is returend.
  75. * Function allocates memory for the value, remember to free!
  76. *
  77. * @param str - pointer to string with key=values pairs
  78. * @param key - pointer to the key to search for
  79. *
  80. * @return - pointer to allocated string with the value
  81. */
  82. static char *extract_val(const char *str, const char *key)
  83. {
  84. char *v, *k;
  85. char *s, *strcopy;
  86. char *new = NULL;
  87. strcopy = strdup(str);
  88. if (strcopy == NULL)
  89. return NULL;
  90. s = strcopy;
  91. while (s) {
  92. v = strsep(&s, ",");
  93. if (!v)
  94. break;
  95. k = strsep(&v, "=");
  96. if (!k)
  97. break;
  98. if (strcmp(k, key) == 0) {
  99. new = strdup(v);
  100. break;
  101. }
  102. }
  103. free(strcopy);
  104. return new;
  105. }
  106. /**
  107. * found_key(): Found key without value in parameter list (comma separated).
  108. *
  109. * @param str - pointer to string with key
  110. * @param key - pointer to the key to search for
  111. *
  112. * @return - true on found key
  113. */
  114. static bool found_key(const char *str, const char *key)
  115. {
  116. char *k;
  117. char *s, *strcopy;
  118. bool result = false;
  119. strcopy = strdup(str);
  120. if (!strcopy)
  121. return NULL;
  122. s = strcopy;
  123. while (s) {
  124. k = strsep(&s, ",");
  125. if (!k)
  126. break;
  127. if (strcmp(k, key) == 0) {
  128. result = true;
  129. break;
  130. }
  131. }
  132. free(strcopy);
  133. return result;
  134. }
  135. #ifdef CONFIG_CMD_GPT_RENAME
  136. static void del_gpt_info(void)
  137. {
  138. struct list_head *pos = &disk_partitions;
  139. struct disk_part *curr;
  140. while (!list_empty(pos)) {
  141. curr = list_entry(pos->next, struct disk_part, list);
  142. list_del(pos->next);
  143. free(curr);
  144. }
  145. }
  146. static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
  147. {
  148. struct disk_part *newpart;
  149. newpart = malloc(sizeof(*newpart));
  150. if (!newpart)
  151. return ERR_PTR(-ENOMEM);
  152. memset(newpart, '\0', sizeof(newpart));
  153. newpart->gpt_part_info.start = info->start;
  154. newpart->gpt_part_info.size = info->size;
  155. newpart->gpt_part_info.blksz = info->blksz;
  156. strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
  157. PART_NAME_LEN);
  158. newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
  159. strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
  160. PART_TYPE_LEN);
  161. newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
  162. newpart->gpt_part_info.bootable = info->bootable;
  163. #ifdef CONFIG_PARTITION_UUIDS
  164. strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid,
  165. UUID_STR_LEN);
  166. /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
  167. newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0';
  168. #endif
  169. newpart->partnum = partnum;
  170. return newpart;
  171. }
  172. static void print_gpt_info(void)
  173. {
  174. struct list_head *pos;
  175. struct disk_part *curr;
  176. list_for_each(pos, &disk_partitions) {
  177. curr = list_entry(pos, struct disk_part, list);
  178. printf("Partition %d:\n", curr->partnum);
  179. printf("1st block %x, size %x\n", (unsigned)curr->gpt_part_info.start,
  180. (unsigned)curr->gpt_part_info.size);
  181. printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
  182. curr->gpt_part_info.name);
  183. printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
  184. curr->gpt_part_info.bootable);
  185. #ifdef CONFIG_PARTITION_UUIDS
  186. printf("UUID %s\n", curr->gpt_part_info.uuid);
  187. #endif
  188. printf("\n");
  189. }
  190. }
  191. /*
  192. * read partition info into disk_partitions list where
  193. * it can be printed or modified
  194. */
  195. static int get_gpt_info(struct blk_desc *dev_desc)
  196. {
  197. /* start partition numbering at 1, as U-Boot does */
  198. int valid_parts = 0, p, ret;
  199. disk_partition_t info;
  200. struct disk_part *new_disk_part;
  201. if (disk_partitions.next == NULL)
  202. INIT_LIST_HEAD(&disk_partitions);
  203. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  204. ret = part_get_info(dev_desc, p, &info);
  205. if (ret)
  206. continue;
  207. /* Add 1 here because counter is zero-based but p1 is
  208. the first partition */
  209. new_disk_part = allocate_disk_part(&info, valid_parts+1);
  210. if (IS_ERR(new_disk_part))
  211. goto out;
  212. list_add_tail(&new_disk_part->list, &disk_partitions);
  213. valid_parts++;
  214. }
  215. if (valid_parts == 0) {
  216. printf("** No valid partitions found **\n");
  217. goto out;
  218. }
  219. return valid_parts;
  220. out:
  221. if (valid_parts >= 1)
  222. del_gpt_info();
  223. return -ENODEV;
  224. }
  225. /* a wrapper to test get_gpt_info */
  226. static int do_get_gpt_info(struct blk_desc *dev_desc)
  227. {
  228. int ret;
  229. ret = get_gpt_info(dev_desc);
  230. if (ret > 0) {
  231. print_gpt_info();
  232. del_gpt_info();
  233. return 0;
  234. }
  235. return ret;
  236. }
  237. #endif
  238. /**
  239. * set_gpt_info(): Fill partition information from string
  240. * function allocates memory, remember to free!
  241. *
  242. * @param dev_desc - pointer block device descriptor
  243. * @param str_part - pointer to string with partition information
  244. * @param str_disk_guid - pointer to pointer to allocated string with disk guid
  245. * @param partitions - pointer to pointer to allocated partitions array
  246. * @param parts_count - number of partitions
  247. *
  248. * @return - zero on success, otherwise error
  249. *
  250. */
  251. static int set_gpt_info(struct blk_desc *dev_desc,
  252. const char *str_part,
  253. char **str_disk_guid,
  254. disk_partition_t **partitions,
  255. u8 *parts_count)
  256. {
  257. char *tok, *str, *s;
  258. int i;
  259. char *val, *p;
  260. int p_count;
  261. disk_partition_t *parts;
  262. int errno = 0;
  263. uint64_t size_ll, start_ll;
  264. lbaint_t offset = 0;
  265. debug("%s: lba num: 0x%x %d\n", __func__,
  266. (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
  267. if (str_part == NULL)
  268. return -1;
  269. str = strdup(str_part);
  270. /* extract disk guid */
  271. s = str;
  272. val = extract_val(str, "uuid_disk");
  273. if (!val) {
  274. #ifdef CONFIG_RANDOM_UUID
  275. *str_disk_guid = malloc(UUID_STR_LEN + 1);
  276. gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
  277. #else
  278. free(str);
  279. return -2;
  280. #endif
  281. } else {
  282. val = strsep(&val, ";");
  283. if (extract_env(val, &p))
  284. p = val;
  285. *str_disk_guid = strdup(p);
  286. free(val);
  287. /* Move s to first partition */
  288. strsep(&s, ";");
  289. }
  290. if (strlen(s) == 0)
  291. return -3;
  292. i = strlen(s) - 1;
  293. if (s[i] == ';')
  294. s[i] = '\0';
  295. /* calculate expected number of partitions */
  296. p_count = 1;
  297. p = s;
  298. while (*p) {
  299. if (*p++ == ';')
  300. p_count++;
  301. }
  302. /* allocate memory for partitions */
  303. parts = calloc(sizeof(disk_partition_t), p_count);
  304. /* retrieve partitions data from string */
  305. for (i = 0; i < p_count; i++) {
  306. tok = strsep(&s, ";");
  307. if (tok == NULL)
  308. break;
  309. /* uuid */
  310. val = extract_val(tok, "uuid");
  311. if (!val) {
  312. /* 'uuid' is optional if random uuid's are enabled */
  313. #ifdef CONFIG_RANDOM_UUID
  314. gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
  315. #else
  316. errno = -4;
  317. goto err;
  318. #endif
  319. } else {
  320. if (extract_env(val, &p))
  321. p = val;
  322. if (strlen(p) >= sizeof(parts[i].uuid)) {
  323. printf("Wrong uuid format for partition %d\n", i);
  324. errno = -4;
  325. goto err;
  326. }
  327. strcpy((char *)parts[i].uuid, p);
  328. free(val);
  329. }
  330. #ifdef CONFIG_PARTITION_TYPE_GUID
  331. /* guid */
  332. val = extract_val(tok, "type");
  333. if (val) {
  334. /* 'type' is optional */
  335. if (extract_env(val, &p))
  336. p = val;
  337. if (strlen(p) >= sizeof(parts[i].type_guid)) {
  338. printf("Wrong type guid format for partition %d\n",
  339. i);
  340. errno = -4;
  341. goto err;
  342. }
  343. strcpy((char *)parts[i].type_guid, p);
  344. free(val);
  345. }
  346. #endif
  347. /* name */
  348. val = extract_val(tok, "name");
  349. if (!val) { /* name is mandatory */
  350. errno = -4;
  351. goto err;
  352. }
  353. if (extract_env(val, &p))
  354. p = val;
  355. if (strlen(p) >= sizeof(parts[i].name)) {
  356. errno = -4;
  357. goto err;
  358. }
  359. strcpy((char *)parts[i].name, p);
  360. free(val);
  361. /* size */
  362. val = extract_val(tok, "size");
  363. if (!val) { /* 'size' is mandatory */
  364. errno = -4;
  365. goto err;
  366. }
  367. if (extract_env(val, &p))
  368. p = val;
  369. if ((strcmp(p, "-") == 0)) {
  370. /* Let part efi module to auto extend the size */
  371. parts[i].size = 0;
  372. } else {
  373. size_ll = ustrtoull(p, &p, 0);
  374. parts[i].size = lldiv(size_ll, dev_desc->blksz);
  375. }
  376. free(val);
  377. /* start address */
  378. val = extract_val(tok, "start");
  379. if (val) { /* start address is optional */
  380. if (extract_env(val, &p))
  381. p = val;
  382. start_ll = ustrtoull(p, &p, 0);
  383. parts[i].start = lldiv(start_ll, dev_desc->blksz);
  384. free(val);
  385. }
  386. offset += parts[i].size + parts[i].start;
  387. /* bootable */
  388. if (found_key(tok, "bootable"))
  389. parts[i].bootable = 1;
  390. }
  391. *parts_count = p_count;
  392. *partitions = parts;
  393. free(str);
  394. return 0;
  395. err:
  396. free(str);
  397. free(*str_disk_guid);
  398. free(parts);
  399. return errno;
  400. }
  401. static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
  402. {
  403. int ret;
  404. char *str_disk_guid;
  405. u8 part_count = 0;
  406. disk_partition_t *partitions = NULL;
  407. /* fill partitions */
  408. ret = set_gpt_info(blk_dev_desc, str_part,
  409. &str_disk_guid, &partitions, &part_count);
  410. if (ret) {
  411. if (ret == -1)
  412. printf("No partition list provided\n");
  413. if (ret == -2)
  414. printf("Missing disk guid\n");
  415. if ((ret == -3) || (ret == -4))
  416. printf("Partition list incomplete\n");
  417. return -1;
  418. }
  419. /* save partitions layout to disk */
  420. ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
  421. free(str_disk_guid);
  422. free(partitions);
  423. return ret;
  424. }
  425. static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
  426. {
  427. ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
  428. blk_dev_desc->blksz);
  429. disk_partition_t *partitions = NULL;
  430. gpt_entry *gpt_pte = NULL;
  431. char *str_disk_guid;
  432. u8 part_count = 0;
  433. int ret = 0;
  434. /* fill partitions */
  435. ret = set_gpt_info(blk_dev_desc, str_part,
  436. &str_disk_guid, &partitions, &part_count);
  437. if (ret) {
  438. if (ret == -1) {
  439. printf("No partition list provided - only basic check\n");
  440. ret = gpt_verify_headers(blk_dev_desc, gpt_head,
  441. &gpt_pte);
  442. goto out;
  443. }
  444. if (ret == -2)
  445. printf("Missing disk guid\n");
  446. if ((ret == -3) || (ret == -4))
  447. printf("Partition list incomplete\n");
  448. return -1;
  449. }
  450. /* Check partition layout with provided pattern */
  451. ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
  452. gpt_head, &gpt_pte);
  453. free(str_disk_guid);
  454. free(partitions);
  455. out:
  456. free(gpt_pte);
  457. return ret;
  458. }
  459. static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
  460. {
  461. int ret;
  462. char disk_guid[UUID_STR_LEN + 1];
  463. ret = get_disk_guid(dev_desc, disk_guid);
  464. if (ret < 0)
  465. return CMD_RET_FAILURE;
  466. if (namestr)
  467. setenv(namestr, disk_guid);
  468. else
  469. printf("%s\n", disk_guid);
  470. return ret;
  471. }
  472. /**
  473. * do_gpt(): Perform GPT operations
  474. *
  475. * @param cmdtp - command name
  476. * @param flag
  477. * @param argc
  478. * @param argv
  479. *
  480. * @return zero on success; otherwise error
  481. */
  482. static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  483. {
  484. int ret = CMD_RET_SUCCESS;
  485. int dev = 0;
  486. char *ep;
  487. struct blk_desc *blk_dev_desc = NULL;
  488. if (argc < 4 || argc > 5)
  489. return CMD_RET_USAGE;
  490. dev = (int)simple_strtoul(argv[3], &ep, 10);
  491. if (!ep || ep[0] != '\0') {
  492. printf("'%s' is not a number\n", argv[3]);
  493. return CMD_RET_USAGE;
  494. }
  495. blk_dev_desc = blk_get_dev(argv[2], dev);
  496. if (!blk_dev_desc) {
  497. printf("%s: %s dev %d NOT available\n",
  498. __func__, argv[2], dev);
  499. return CMD_RET_FAILURE;
  500. }
  501. if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
  502. printf("Writing GPT: ");
  503. ret = gpt_default(blk_dev_desc, argv[4]);
  504. } else if ((strcmp(argv[1], "verify") == 0)) {
  505. ret = gpt_verify(blk_dev_desc, argv[4]);
  506. printf("Verify GPT: ");
  507. } else if (strcmp(argv[1], "guid") == 0) {
  508. ret = do_disk_guid(blk_dev_desc, argv[4]);
  509. #ifdef CONFIG_CMD_GPT_RENAME
  510. } else if (strcmp(argv[1], "read") == 0) {
  511. ret = do_get_gpt_info(blk_dev_desc);
  512. #endif
  513. } else {
  514. return CMD_RET_USAGE;
  515. }
  516. if (ret) {
  517. printf("error!\n");
  518. return CMD_RET_FAILURE;
  519. }
  520. printf("success!\n");
  521. return CMD_RET_SUCCESS;
  522. }
  523. U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
  524. "GUID Partition Table",
  525. "<command> <interface> <dev> <partitions_list>\n"
  526. " - GUID partition table restoration and validity check\n"
  527. " Restore or verify GPT information on a device connected\n"
  528. " to interface\n"
  529. " Example usage:\n"
  530. " gpt write mmc 0 $partitions\n"
  531. " gpt verify mmc 0 $partitions\n"
  532. " read <interface> <dev>\n"
  533. " - read GPT into a data structure for manipulation\n"
  534. " guid <interface> <dev>\n"
  535. " - print disk GUID\n"
  536. " guid <interface> <dev> <varname>\n"
  537. " - set environment variable to disk GUID\n"
  538. " Example usage:\n"
  539. " gpt guid mmc 0\n"
  540. " gpt guid mmc 0 varname\n"
  541. );