gpt.c 9.6 KB

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