cmd_gpt.c 7.4 KB

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