cmd_gpt.c 7.3 KB

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