cmd_gpt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * cmd_gpt.c -- GPT (GUID Partition Table) handling command
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * author: Lukasz Majewski <l.majewski@samsung.com>
  6. * author: Piotr Wilczek <p.wilczek@samsung.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <command.h>
  13. #include <mmc.h>
  14. #include <part_efi.h>
  15. #include <exports.h>
  16. #include <linux/ctype.h>
  17. #include <div64.h>
  18. #ifndef CONFIG_PARTITION_UUIDS
  19. #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
  20. #endif
  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 char extract_env(const char *str, char **env)
  31. {
  32. char *e, *s;
  33. if (!str || strlen(str) < 4)
  34. return -1;
  35. if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
  36. s = strdup(str);
  37. if (s == NULL)
  38. return -1;
  39. memset(s + strlen(s) - 1, '\0', 1);
  40. memmove(s, s + 2, strlen(s) - 1);
  41. e = getenv(s);
  42. free(s);
  43. if (e == NULL) {
  44. printf("Environmental '%s' not set\n", str);
  45. return -1; /* env not set */
  46. }
  47. *env = e;
  48. return 0;
  49. }
  50. return -1;
  51. }
  52. /**
  53. * extract_val(): Extract value from a key=value pair list (comma separated).
  54. * Only value for the given key is returend.
  55. * Function allocates memory for the value, remember to free!
  56. *
  57. * @param str - pointer to string with key=values pairs
  58. * @param key - pointer to the key to search for
  59. *
  60. * @return - pointer to allocated string with the value
  61. */
  62. static char *extract_val(const char *str, const char *key)
  63. {
  64. char *v, *k;
  65. char *s, *strcopy;
  66. char *new = NULL;
  67. strcopy = strdup(str);
  68. if (strcopy == NULL)
  69. return NULL;
  70. s = strcopy;
  71. while (s) {
  72. v = strsep(&s, ",");
  73. if (!v)
  74. break;
  75. k = strsep(&v, "=");
  76. if (!k)
  77. break;
  78. if (strcmp(k, key) == 0) {
  79. new = strdup(v);
  80. break;
  81. }
  82. }
  83. free(strcopy);
  84. return new;
  85. }
  86. /**
  87. * set_gpt_info(): Fill partition information from string
  88. * function allocates memory, remember to free!
  89. *
  90. * @param dev_desc - pointer block device descriptor
  91. * @param str_part - pointer to string with partition information
  92. * @param str_disk_guid - pointer to pointer to allocated string with disk guid
  93. * @param partitions - pointer to pointer to allocated partitions array
  94. * @param parts_count - number of partitions
  95. *
  96. * @return - zero on success, otherwise error
  97. *
  98. */
  99. static int set_gpt_info(block_dev_desc_t *dev_desc,
  100. const char *str_part,
  101. char **str_disk_guid,
  102. disk_partition_t **partitions,
  103. u8 *parts_count)
  104. {
  105. char *tok, *str, *s;
  106. int i;
  107. char *val, *p;
  108. int p_count;
  109. disk_partition_t *parts;
  110. int errno = 0;
  111. uint64_t size_ll, start_ll;
  112. debug("%s: MMC lba num: 0x%x %d\n", __func__,
  113. (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
  114. if (str_part == NULL)
  115. return -1;
  116. str = strdup(str_part);
  117. /* extract disk guid */
  118. s = str;
  119. tok = strsep(&s, ";");
  120. val = extract_val(tok, "uuid_disk");
  121. if (!val) {
  122. free(str);
  123. return -2;
  124. }
  125. if (extract_env(val, &p))
  126. p = val;
  127. *str_disk_guid = strdup(p);
  128. free(val);
  129. if (strlen(s) == 0)
  130. return -3;
  131. i = strlen(s) - 1;
  132. if (s[i] == ';')
  133. s[i] = '\0';
  134. /* calculate expected number of partitions */
  135. p_count = 1;
  136. p = s;
  137. while (*p) {
  138. if (*p++ == ';')
  139. p_count++;
  140. }
  141. /* allocate memory for partitions */
  142. parts = calloc(sizeof(disk_partition_t), p_count);
  143. /* retrive partions data from string */
  144. for (i = 0; i < p_count; i++) {
  145. tok = strsep(&s, ";");
  146. if (tok == NULL)
  147. break;
  148. /* uuid */
  149. val = extract_val(tok, "uuid");
  150. if (!val) { /* 'uuid' is mandatory */
  151. errno = -4;
  152. goto err;
  153. }
  154. if (extract_env(val, &p))
  155. p = val;
  156. if (strlen(p) >= sizeof(parts[i].uuid)) {
  157. printf("Wrong uuid format for partition %d\n", i);
  158. errno = -4;
  159. goto err;
  160. }
  161. strcpy((char *)parts[i].uuid, p);
  162. free(val);
  163. /* name */
  164. val = extract_val(tok, "name");
  165. if (!val) { /* name is mandatory */
  166. errno = -4;
  167. goto err;
  168. }
  169. if (extract_env(val, &p))
  170. p = val;
  171. if (strlen(p) >= sizeof(parts[i].name)) {
  172. errno = -4;
  173. goto err;
  174. }
  175. strcpy((char *)parts[i].name, p);
  176. free(val);
  177. /* size */
  178. val = extract_val(tok, "size");
  179. if (!val) { /* 'size' is mandatory */
  180. errno = -4;
  181. goto err;
  182. }
  183. if (extract_env(val, &p))
  184. p = val;
  185. size_ll = ustrtoull(p, &p, 0);
  186. parts[i].size = lldiv(size_ll, dev_desc->blksz);
  187. free(val);
  188. /* start address */
  189. val = extract_val(tok, "start");
  190. if (val) { /* start address is optional */
  191. if (extract_env(val, &p))
  192. p = val;
  193. start_ll = ustrtoull(p, &p, 0);
  194. parts[i].start = lldiv(start_ll, dev_desc->blksz);
  195. free(val);
  196. }
  197. }
  198. *parts_count = p_count;
  199. *partitions = parts;
  200. free(str);
  201. return 0;
  202. err:
  203. free(str);
  204. free(*str_disk_guid);
  205. free(parts);
  206. return errno;
  207. }
  208. static int gpt_mmc_default(int dev, const char *str_part)
  209. {
  210. int ret;
  211. char *str_disk_guid;
  212. u8 part_count = 0;
  213. disk_partition_t *partitions = NULL;
  214. struct mmc *mmc = find_mmc_device(dev);
  215. if (mmc == NULL) {
  216. printf("%s: mmc dev %d NOT available\n", __func__, dev);
  217. return CMD_RET_FAILURE;
  218. }
  219. if (!str_part)
  220. return -1;
  221. /* fill partitions */
  222. ret = set_gpt_info(&mmc->block_dev, str_part,
  223. &str_disk_guid, &partitions, &part_count);
  224. if (ret) {
  225. if (ret == -1)
  226. printf("No partition list provided\n");
  227. if (ret == -2)
  228. printf("Missing disk guid\n");
  229. if ((ret == -3) || (ret == -4))
  230. printf("Partition list incomplete\n");
  231. return -1;
  232. }
  233. /* save partitions layout to disk */
  234. gpt_restore(&mmc->block_dev, str_disk_guid, partitions, part_count);
  235. free(str_disk_guid);
  236. free(partitions);
  237. return 0;
  238. }
  239. /**
  240. * do_gpt(): Perform GPT operations
  241. *
  242. * @param cmdtp - command name
  243. * @param flag
  244. * @param argc
  245. * @param argv
  246. *
  247. * @return zero on success; otherwise error
  248. */
  249. static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  250. {
  251. int ret = CMD_RET_SUCCESS;
  252. int dev = 0;
  253. char *pstr;
  254. if (argc < 5)
  255. return CMD_RET_USAGE;
  256. /* command: 'write' */
  257. if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
  258. /* device: 'mmc' */
  259. if (strcmp(argv[2], "mmc") == 0) {
  260. /* check if 'dev' is a number */
  261. for (pstr = argv[3]; *pstr != '\0'; pstr++)
  262. if (!isdigit(*pstr)) {
  263. printf("'%s' is not a number\n",
  264. argv[3]);
  265. return CMD_RET_USAGE;
  266. }
  267. dev = (int)simple_strtoul(argv[3], NULL, 10);
  268. /* write to mmc */
  269. if (gpt_mmc_default(dev, argv[4]))
  270. return CMD_RET_FAILURE;
  271. }
  272. } else {
  273. return CMD_RET_USAGE;
  274. }
  275. return ret;
  276. }
  277. U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
  278. "GUID Partition Table",
  279. "<command> <interface> <dev> <partions_list>\n"
  280. " - GUID partition table restoration\n"
  281. " Restore GPT information on a device connected\n"
  282. " to interface\n"
  283. );