mtd_uboot.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014
  4. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  5. */
  6. #include <common.h>
  7. #include <env.h>
  8. #include <log.h>
  9. #include <malloc.h>
  10. #include <dm/device.h>
  11. #include <dm/uclass-internal.h>
  12. #include <dm/uclass.h>
  13. #include <linux/err.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <asm/global_data.h>
  17. #include <mtd.h>
  18. #define MTD_NAME_MAX_LEN 20
  19. void board_mtdparts_default(const char **mtdids, const char **mtdparts);
  20. static const char *get_mtdids(void)
  21. {
  22. __maybe_unused const char *mtdparts = NULL;
  23. const char *mtdids = env_get("mtdids");
  24. if (mtdids)
  25. return mtdids;
  26. #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
  27. board_mtdparts_default(&mtdids, &mtdparts);
  28. #elif defined(MTDIDS_DEFAULT)
  29. mtdids = MTDIDS_DEFAULT;
  30. #elif defined(CONFIG_MTDIDS_DEFAULT)
  31. mtdids = CONFIG_MTDIDS_DEFAULT;
  32. #endif
  33. if (mtdids)
  34. env_set("mtdids", mtdids);
  35. return mtdids;
  36. }
  37. /**
  38. * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
  39. * the mtdids legacy environment variable.
  40. *
  41. * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
  42. * Check if one of the mtd_id matches mtdname, in this case save dev_id in
  43. * altname.
  44. *
  45. * @mtdname: Current MTD device name
  46. * @altname: Alternate name to return
  47. * @max_len: Length of the alternate name buffer
  48. *
  49. * @return 0 on success, an error otherwise.
  50. */
  51. int mtd_search_alternate_name(const char *mtdname, char *altname,
  52. unsigned int max_len)
  53. {
  54. const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
  55. int dev_id_len, mtd_id_len;
  56. mtdids = get_mtdids();
  57. if (!mtdids)
  58. return -EINVAL;
  59. do {
  60. /* Find the '=' sign */
  61. dev_id = mtdids;
  62. equal = strchr(dev_id, '=');
  63. if (!equal)
  64. break;
  65. dev_id_len = equal - mtdids;
  66. mtd_id = equal + 1;
  67. /* Find the end of the tupple */
  68. comma = strchr(mtdids, ',');
  69. if (comma)
  70. mtd_id_len = comma - mtd_id;
  71. else
  72. mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
  73. if (!dev_id_len || !mtd_id_len)
  74. return -EINVAL;
  75. if (dev_id_len + 1 > max_len)
  76. continue;
  77. /* Compare the name we search with the current mtd_id */
  78. if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
  79. strncpy(altname, dev_id, dev_id_len);
  80. altname[dev_id_len] = 0;
  81. return 0;
  82. }
  83. /* Go to the next tupple */
  84. mtdids = comma + 1;
  85. } while (comma);
  86. return -EINVAL;
  87. }
  88. #if IS_ENABLED(CONFIG_DM_MTD)
  89. static void mtd_probe_uclass_mtd_devs(void)
  90. {
  91. struct udevice *dev;
  92. uclass_foreach_dev_probe(UCLASS_MTD, dev)
  93. ;
  94. }
  95. #else
  96. static void mtd_probe_uclass_mtd_devs(void) { }
  97. #endif
  98. #if IS_ENABLED(CONFIG_DM_SPI_FLASH) && IS_ENABLED(CONFIG_SPI_FLASH_MTD)
  99. static void mtd_probe_uclass_spi_nor_devs(void)
  100. {
  101. struct udevice *dev;
  102. uclass_foreach_dev_probe(UCLASS_SPI_FLASH, dev)
  103. ;
  104. }
  105. #else
  106. static void mtd_probe_uclass_spi_nor_devs(void) { }
  107. #endif
  108. #if defined(CONFIG_MTD_PARTITIONS)
  109. #define MTDPARTS_MAXLEN 512
  110. static const char *get_mtdparts(void)
  111. {
  112. __maybe_unused const char *mtdids = NULL;
  113. static char tmp_parts[MTDPARTS_MAXLEN];
  114. const char *mtdparts = NULL;
  115. if (gd->flags & GD_FLG_ENV_READY)
  116. mtdparts = env_get("mtdparts");
  117. else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1)
  118. mtdparts = tmp_parts;
  119. if (mtdparts)
  120. return mtdparts;
  121. #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
  122. board_mtdparts_default(&mtdids, &mtdparts);
  123. #elif defined(MTDPARTS_DEFAULT)
  124. mtdparts = MTDPARTS_DEFAULT;
  125. #elif defined(CONFIG_MTDPARTS_DEFAULT)
  126. mtdparts = CONFIG_MTDPARTS_DEFAULT;
  127. #endif
  128. if (mtdparts)
  129. env_set("mtdparts", mtdparts);
  130. return mtdparts;
  131. }
  132. static int mtd_del_parts(struct mtd_info *mtd, bool quiet)
  133. {
  134. int ret;
  135. if (!mtd_has_partitions(mtd))
  136. return 0;
  137. /* do not delete partitions if they are in use. */
  138. if (mtd_partitions_used(mtd)) {
  139. if (!quiet)
  140. printf("\"%s\" partitions still in use, can't delete them\n",
  141. mtd->name);
  142. return -EACCES;
  143. }
  144. ret = del_mtd_partitions(mtd);
  145. if (ret)
  146. return ret;
  147. return 1;
  148. }
  149. static bool mtd_del_all_parts_failed;
  150. static void mtd_del_all_parts(void)
  151. {
  152. struct mtd_info *mtd;
  153. int ret = 0;
  154. mtd_del_all_parts_failed = false;
  155. /*
  156. * It is not safe to remove entries from the mtd_for_each_device loop
  157. * as it uses idr indexes and the partitions removal is done in bulk
  158. * (all partitions of one device at the same time), so break and
  159. * iterate from start each time a new partition is found and deleted.
  160. */
  161. do {
  162. mtd_for_each_device(mtd) {
  163. ret = mtd_del_parts(mtd, false);
  164. if (ret > 0)
  165. break;
  166. else if (ret < 0)
  167. mtd_del_all_parts_failed = true;
  168. }
  169. } while (ret > 0);
  170. }
  171. static int parse_mtdparts(const char *mtdparts, const char *mtdids)
  172. {
  173. const char *mtdparts_next;
  174. struct mtd_info *mtd;
  175. /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
  176. if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1))
  177. mtdparts += 9;
  178. /* For each MTD device in mtdparts */
  179. for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) {
  180. char mtd_name[MTD_NAME_MAX_LEN], *colon;
  181. struct mtd_partition *parts;
  182. unsigned int mtd_name_len;
  183. int nparts, ret;
  184. mtdparts_next = strchr(mtdparts, ';');
  185. if (!mtdparts_next)
  186. mtdparts_next = mtdparts + strlen(mtdparts);
  187. else
  188. mtdparts_next++;
  189. colon = strchr(mtdparts, ':');
  190. if (colon > mtdparts_next)
  191. colon = NULL;
  192. if (!colon) {
  193. printf("Wrong mtdparts: %s\n", mtdparts);
  194. return -EINVAL;
  195. }
  196. mtd_name_len = (unsigned int)(colon - mtdparts);
  197. if (mtd_name_len + 1 > sizeof(mtd_name)) {
  198. printf("MTD name too long: %s\n", mtdparts);
  199. return -EINVAL;
  200. }
  201. strncpy(mtd_name, mtdparts, mtd_name_len);
  202. mtd_name[mtd_name_len] = '\0';
  203. /* Move the pointer forward (including the ':') */
  204. mtdparts += mtd_name_len + 1;
  205. mtd = get_mtd_device_nm(mtd_name);
  206. if (IS_ERR_OR_NULL(mtd)) {
  207. char linux_name[MTD_NAME_MAX_LEN];
  208. /*
  209. * The MTD device named "mtd_name" does not exist. Try
  210. * to find a correspondance with an MTD device having
  211. * the same type and number as defined in the mtdids.
  212. */
  213. debug("No device named %s\n", mtd_name);
  214. ret = mtd_search_alternate_name(mtd_name, linux_name,
  215. MTD_NAME_MAX_LEN);
  216. if (!ret)
  217. mtd = get_mtd_device_nm(linux_name);
  218. /*
  219. * If no device could be found, move the mtdparts
  220. * pointer forward until the next set of partitions.
  221. */
  222. if (ret || IS_ERR_OR_NULL(mtd)) {
  223. printf("Could not find a valid device for %s\n",
  224. mtd_name);
  225. mtdparts = mtdparts_next;
  226. continue;
  227. }
  228. }
  229. /*
  230. * Call mtd_del_parts() again, even if it's already been called
  231. * in mtd_del_all_parts(). We need to know if old partitions are
  232. * still around (because they are still being used by someone),
  233. * and if they are, we shouldn't create new partitions, so just
  234. * skip this MTD device and try the next one.
  235. */
  236. ret = mtd_del_parts(mtd, true);
  237. if (ret < 0)
  238. continue;
  239. /*
  240. * Parse the MTD device partitions. It will update the mtdparts
  241. * pointer, create an array of parts (that must be freed), and
  242. * return the number of partition structures in the array.
  243. */
  244. ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
  245. if (ret) {
  246. printf("Could not parse device %s\n", mtd->name);
  247. put_mtd_device(mtd);
  248. return -EINVAL;
  249. }
  250. if (!nparts)
  251. continue;
  252. /* Create the new MTD partitions */
  253. add_mtd_partitions(mtd, parts, nparts);
  254. /* Free the structures allocated during the parsing */
  255. mtd_free_parsed_partitions(parts, nparts);
  256. put_mtd_device(mtd);
  257. }
  258. return 0;
  259. }
  260. int mtd_probe_devices(void)
  261. {
  262. static char *old_mtdparts;
  263. static char *old_mtdids;
  264. const char *mtdparts = get_mtdparts();
  265. const char *mtdids = get_mtdids();
  266. struct mtd_info *mtd;
  267. mtd_probe_uclass_mtd_devs();
  268. mtd_probe_uclass_spi_nor_devs();
  269. /*
  270. * Check if mtdparts/mtdids changed, if the MTD dev list was updated
  271. * or if our previous attempt to delete existing partititions failed.
  272. * In any of these cases we want to update the partitions, otherwise,
  273. * everything is up-to-date and we can return 0 directly.
  274. */
  275. if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
  276. (mtdparts && old_mtdparts && mtdids && old_mtdids &&
  277. !mtd_dev_list_updated() && !mtd_del_all_parts_failed &&
  278. !strcmp(mtdparts, old_mtdparts) &&
  279. !strcmp(mtdids, old_mtdids)))
  280. return 0;
  281. /* Update the local copy of mtdparts */
  282. free(old_mtdparts);
  283. free(old_mtdids);
  284. old_mtdparts = strdup(mtdparts);
  285. old_mtdids = strdup(mtdids);
  286. /*
  287. * Remove all old parts. Note that partition removal can fail in case
  288. * one of the partition is still being used by an MTD user, so this
  289. * does not guarantee that all old partitions are gone.
  290. */
  291. mtd_del_all_parts();
  292. /*
  293. * Call mtd_dev_list_updated() to clear updates generated by our own
  294. * parts removal loop.
  295. */
  296. mtd_dev_list_updated();
  297. /* If both mtdparts and mtdids are non-empty, parse */
  298. if (mtdparts && mtdids) {
  299. if (parse_mtdparts(mtdparts, mtdids) < 0)
  300. printf("Failed parsing MTD partitions from mtdparts!\n");
  301. }
  302. /* Fallback to OF partitions */
  303. mtd_for_each_device(mtd) {
  304. if (list_empty(&mtd->partitions)) {
  305. if (add_mtd_partitions_of(mtd) < 0)
  306. printf("Failed parsing MTD %s OF partitions!\n",
  307. mtd->name);
  308. }
  309. }
  310. /*
  311. * Call mtd_dev_list_updated() to clear updates generated by our own
  312. * parts registration loop.
  313. */
  314. mtd_dev_list_updated();
  315. return 0;
  316. }
  317. #else
  318. int mtd_probe_devices(void)
  319. {
  320. mtd_probe_uclass_mtd_devs();
  321. mtd_probe_uclass_spi_nor_devs();
  322. return 0;
  323. }
  324. #endif /* defined(CONFIG_MTD_PARTITIONS) */