gpt.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. /**
  22. * extract_env(): Expand env name from string format '&{env_name}'
  23. * and return pointer to the env (if the env is set)
  24. *
  25. * @param str - pointer to string
  26. * @param env - pointer to pointer to extracted env
  27. *
  28. * @return - zero on successful expand and env is set
  29. */
  30. static int extract_env(const char *str, char **env)
  31. {
  32. int ret = -1;
  33. char *e, *s;
  34. #ifdef CONFIG_RANDOM_UUID
  35. char uuid_str[UUID_STR_LEN + 1];
  36. #endif
  37. if (!str || strlen(str) < 4)
  38. return -1;
  39. if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
  40. return -1;
  41. s = strdup(str);
  42. if (s == NULL)
  43. return -1;
  44. memset(s + strlen(s) - 1, '\0', 1);
  45. memmove(s, s + 2, strlen(s) - 1);
  46. e = getenv(s);
  47. if (e == NULL) {
  48. #ifdef CONFIG_RANDOM_UUID
  49. debug("%s unset. ", str);
  50. gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD);
  51. setenv(s, uuid_str);
  52. e = getenv(s);
  53. if (e) {
  54. debug("Set to random.\n");
  55. ret = 0;
  56. } else {
  57. debug("Can't get random UUID.\n");
  58. }
  59. #else
  60. debug("%s unset.\n", str);
  61. #endif
  62. } else {
  63. debug("%s get from environment.\n", str);
  64. ret = 0;
  65. }
  66. *env = e;
  67. free(s);
  68. return ret;
  69. }
  70. /**
  71. * extract_val(): Extract value from a key=value pair list (comma separated).
  72. * Only value for the given key is returend.
  73. * Function allocates memory for the value, remember to free!
  74. *
  75. * @param str - pointer to string with key=values pairs
  76. * @param key - pointer to the key to search for
  77. *
  78. * @return - pointer to allocated string with the value
  79. */
  80. static char *extract_val(const char *str, const char *key)
  81. {
  82. char *v, *k;
  83. char *s, *strcopy;
  84. char *new = NULL;
  85. strcopy = strdup(str);
  86. if (strcopy == NULL)
  87. return NULL;
  88. s = strcopy;
  89. while (s) {
  90. v = strsep(&s, ",");
  91. if (!v)
  92. break;
  93. k = strsep(&v, "=");
  94. if (!k)
  95. break;
  96. if (strcmp(k, key) == 0) {
  97. new = strdup(v);
  98. break;
  99. }
  100. }
  101. free(strcopy);
  102. return new;
  103. }
  104. /**
  105. * found_key(): Found key without value in parameter list (comma separated).
  106. *
  107. * @param str - pointer to string with key
  108. * @param key - pointer to the key to search for
  109. *
  110. * @return - true on found key
  111. */
  112. static bool found_key(const char *str, const char *key)
  113. {
  114. char *k;
  115. char *s, *strcopy;
  116. bool result = false;
  117. strcopy = strdup(str);
  118. if (!strcopy)
  119. return NULL;
  120. s = strcopy;
  121. while (s) {
  122. k = strsep(&s, ",");
  123. if (!k)
  124. break;
  125. if (strcmp(k, key) == 0) {
  126. result = true;
  127. break;
  128. }
  129. }
  130. free(strcopy);
  131. return result;
  132. }
  133. /**
  134. * set_gpt_info(): Fill partition information from string
  135. * function allocates memory, remember to free!
  136. *
  137. * @param dev_desc - pointer block device descriptor
  138. * @param str_part - pointer to string with partition information
  139. * @param str_disk_guid - pointer to pointer to allocated string with disk guid
  140. * @param partitions - pointer to pointer to allocated partitions array
  141. * @param parts_count - number of partitions
  142. *
  143. * @return - zero on success, otherwise error
  144. *
  145. */
  146. static int set_gpt_info(struct blk_desc *dev_desc,
  147. const char *str_part,
  148. char **str_disk_guid,
  149. disk_partition_t **partitions,
  150. u8 *parts_count)
  151. {
  152. char *tok, *str, *s;
  153. int i;
  154. char *val, *p;
  155. int p_count;
  156. disk_partition_t *parts;
  157. int errno = 0;
  158. uint64_t size_ll, start_ll;
  159. lbaint_t offset = 0;
  160. debug("%s: lba num: 0x%x %d\n", __func__,
  161. (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
  162. if (str_part == NULL)
  163. return -1;
  164. str = strdup(str_part);
  165. /* extract disk guid */
  166. s = str;
  167. val = extract_val(str, "uuid_disk");
  168. if (!val) {
  169. #ifdef CONFIG_RANDOM_UUID
  170. *str_disk_guid = malloc(UUID_STR_LEN + 1);
  171. gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
  172. #else
  173. free(str);
  174. return -2;
  175. #endif
  176. } else {
  177. val = strsep(&val, ";");
  178. if (extract_env(val, &p))
  179. p = val;
  180. *str_disk_guid = strdup(p);
  181. free(val);
  182. /* Move s to first partition */
  183. strsep(&s, ";");
  184. }
  185. if (strlen(s) == 0)
  186. return -3;
  187. i = strlen(s) - 1;
  188. if (s[i] == ';')
  189. s[i] = '\0';
  190. /* calculate expected number of partitions */
  191. p_count = 1;
  192. p = s;
  193. while (*p) {
  194. if (*p++ == ';')
  195. p_count++;
  196. }
  197. /* allocate memory for partitions */
  198. parts = calloc(sizeof(disk_partition_t), p_count);
  199. /* retrieve partitions data from string */
  200. for (i = 0; i < p_count; i++) {
  201. tok = strsep(&s, ";");
  202. if (tok == NULL)
  203. break;
  204. /* uuid */
  205. val = extract_val(tok, "uuid");
  206. if (!val) {
  207. /* 'uuid' is optional if random uuid's are enabled */
  208. #ifdef CONFIG_RANDOM_UUID
  209. gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
  210. #else
  211. errno = -4;
  212. goto err;
  213. #endif
  214. } else {
  215. if (extract_env(val, &p))
  216. p = val;
  217. if (strlen(p) >= sizeof(parts[i].uuid)) {
  218. printf("Wrong uuid format for partition %d\n", i);
  219. errno = -4;
  220. goto err;
  221. }
  222. strcpy((char *)parts[i].uuid, p);
  223. free(val);
  224. }
  225. #ifdef CONFIG_PARTITION_TYPE_GUID
  226. /* guid */
  227. val = extract_val(tok, "type");
  228. if (val) {
  229. /* 'type' is optional */
  230. if (extract_env(val, &p))
  231. p = val;
  232. if (strlen(p) >= sizeof(parts[i].type_guid)) {
  233. printf("Wrong type guid format for partition %d\n",
  234. i);
  235. errno = -4;
  236. goto err;
  237. }
  238. strcpy((char *)parts[i].type_guid, p);
  239. free(val);
  240. }
  241. #endif
  242. /* name */
  243. val = extract_val(tok, "name");
  244. if (!val) { /* name is mandatory */
  245. errno = -4;
  246. goto err;
  247. }
  248. if (extract_env(val, &p))
  249. p = val;
  250. if (strlen(p) >= sizeof(parts[i].name)) {
  251. errno = -4;
  252. goto err;
  253. }
  254. strcpy((char *)parts[i].name, p);
  255. free(val);
  256. /* size */
  257. val = extract_val(tok, "size");
  258. if (!val) { /* 'size' is mandatory */
  259. errno = -4;
  260. goto err;
  261. }
  262. if (extract_env(val, &p))
  263. p = val;
  264. if ((strcmp(p, "-") == 0)) {
  265. /* Let part efi module to auto extend the size */
  266. parts[i].size = 0;
  267. } else {
  268. size_ll = ustrtoull(p, &p, 0);
  269. parts[i].size = lldiv(size_ll, dev_desc->blksz);
  270. }
  271. free(val);
  272. /* start address */
  273. val = extract_val(tok, "start");
  274. if (val) { /* start address is optional */
  275. if (extract_env(val, &p))
  276. p = val;
  277. start_ll = ustrtoull(p, &p, 0);
  278. parts[i].start = lldiv(start_ll, dev_desc->blksz);
  279. free(val);
  280. }
  281. offset += parts[i].size + parts[i].start;
  282. /* bootable */
  283. if (found_key(tok, "bootable"))
  284. parts[i].bootable = 1;
  285. }
  286. *parts_count = p_count;
  287. *partitions = parts;
  288. free(str);
  289. return 0;
  290. err:
  291. free(str);
  292. free(*str_disk_guid);
  293. free(parts);
  294. return errno;
  295. }
  296. static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
  297. {
  298. int ret;
  299. char *str_disk_guid;
  300. u8 part_count = 0;
  301. disk_partition_t *partitions = NULL;
  302. /* fill partitions */
  303. ret = set_gpt_info(blk_dev_desc, str_part,
  304. &str_disk_guid, &partitions, &part_count);
  305. if (ret) {
  306. if (ret == -1)
  307. printf("No partition list provided\n");
  308. if (ret == -2)
  309. printf("Missing disk guid\n");
  310. if ((ret == -3) || (ret == -4))
  311. printf("Partition list incomplete\n");
  312. return -1;
  313. }
  314. /* save partitions layout to disk */
  315. ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
  316. free(str_disk_guid);
  317. free(partitions);
  318. return ret;
  319. }
  320. static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
  321. {
  322. ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
  323. blk_dev_desc->blksz);
  324. disk_partition_t *partitions = NULL;
  325. gpt_entry *gpt_pte = NULL;
  326. char *str_disk_guid;
  327. u8 part_count = 0;
  328. int ret = 0;
  329. /* fill partitions */
  330. ret = set_gpt_info(blk_dev_desc, str_part,
  331. &str_disk_guid, &partitions, &part_count);
  332. if (ret) {
  333. if (ret == -1) {
  334. printf("No partition list provided - only basic check\n");
  335. ret = gpt_verify_headers(blk_dev_desc, gpt_head,
  336. &gpt_pte);
  337. goto out;
  338. }
  339. if (ret == -2)
  340. printf("Missing disk guid\n");
  341. if ((ret == -3) || (ret == -4))
  342. printf("Partition list incomplete\n");
  343. return -1;
  344. }
  345. /* Check partition layout with provided pattern */
  346. ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
  347. gpt_head, &gpt_pte);
  348. free(str_disk_guid);
  349. free(partitions);
  350. out:
  351. free(gpt_pte);
  352. return ret;
  353. }
  354. /**
  355. * do_gpt(): Perform GPT operations
  356. *
  357. * @param cmdtp - command name
  358. * @param flag
  359. * @param argc
  360. * @param argv
  361. *
  362. * @return zero on success; otherwise error
  363. */
  364. static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  365. {
  366. int ret = CMD_RET_SUCCESS;
  367. int dev = 0;
  368. char *ep;
  369. struct blk_desc *blk_dev_desc = NULL;
  370. if (argc < 4 || argc > 5)
  371. return CMD_RET_USAGE;
  372. dev = (int)simple_strtoul(argv[3], &ep, 10);
  373. if (!ep || ep[0] != '\0') {
  374. printf("'%s' is not a number\n", argv[3]);
  375. return CMD_RET_USAGE;
  376. }
  377. blk_dev_desc = blk_get_dev(argv[2], dev);
  378. if (!blk_dev_desc) {
  379. printf("%s: %s dev %d NOT available\n",
  380. __func__, argv[2], dev);
  381. return CMD_RET_FAILURE;
  382. }
  383. if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
  384. printf("Writing GPT: ");
  385. ret = gpt_default(blk_dev_desc, argv[4]);
  386. } else if ((strcmp(argv[1], "verify") == 0)) {
  387. ret = gpt_verify(blk_dev_desc, argv[4]);
  388. printf("Verify GPT: ");
  389. } else {
  390. return CMD_RET_USAGE;
  391. }
  392. if (ret) {
  393. printf("error!\n");
  394. return CMD_RET_FAILURE;
  395. }
  396. printf("success!\n");
  397. return CMD_RET_SUCCESS;
  398. }
  399. U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
  400. "GUID Partition Table",
  401. "<command> <interface> <dev> <partitions_list>\n"
  402. " - GUID partition table restoration and validity check\n"
  403. " Restore or verify GPT information on a device connected\n"
  404. " to interface\n"
  405. " Example usage:\n"
  406. " gpt write mmc 0 $partitions\n"
  407. " gpt verify mmc 0 $partitions\n"
  408. );