mtd_uboot.c 8.7 KB

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