mtd_uboot.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <dm/device.h>
  8. #include <dm/uclass-internal.h>
  9. #include <jffs2/jffs2.h> /* LEGACY */
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/partitions.h>
  12. #include <mtd.h>
  13. #define MTD_NAME_MAX_LEN 20
  14. /**
  15. * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
  16. * the mtdids legacy environment variable.
  17. *
  18. * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
  19. * Check if one of the mtd_id matches mtdname, in this case save dev_id in
  20. * altname.
  21. *
  22. * @mtdname: Current MTD device name
  23. * @altname: Alternate name to return
  24. * @max_len: Length of the alternate name buffer
  25. *
  26. * @return 0 on success, an error otherwise.
  27. */
  28. int mtd_search_alternate_name(const char *mtdname, char *altname,
  29. unsigned int max_len)
  30. {
  31. const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
  32. int dev_id_len, mtd_id_len;
  33. mtdids = env_get("mtdids");
  34. if (!mtdids)
  35. return -EINVAL;
  36. do {
  37. /* Find the '=' sign */
  38. dev_id = mtdids;
  39. equal = strchr(dev_id, '=');
  40. if (!equal)
  41. break;
  42. dev_id_len = equal - mtdids;
  43. mtd_id = equal + 1;
  44. /* Find the end of the tupple */
  45. comma = strchr(mtdids, ',');
  46. if (comma)
  47. mtd_id_len = comma - mtd_id;
  48. else
  49. mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
  50. if (!dev_id_len || !mtd_id_len)
  51. return -EINVAL;
  52. if (dev_id_len + 1 > max_len)
  53. continue;
  54. /* Compare the name we search with the current mtd_id */
  55. if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
  56. strncpy(altname, dev_id, dev_id_len);
  57. altname[dev_id_len] = 0;
  58. return 0;
  59. }
  60. /* Go to the next tupple */
  61. mtdids = comma + 1;
  62. } while (comma);
  63. return -EINVAL;
  64. }
  65. #if IS_ENABLED(CONFIG_MTD)
  66. static void mtd_probe_uclass_mtd_devs(void)
  67. {
  68. struct udevice *dev;
  69. int idx = 0;
  70. /* Probe devices with DM compliant drivers */
  71. while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
  72. mtd_probe(dev);
  73. idx++;
  74. }
  75. }
  76. #else
  77. static void mtd_probe_uclass_mtd_devs(void) { }
  78. #endif
  79. #if defined(CONFIG_MTD_PARTITIONS)
  80. int mtd_probe_devices(void)
  81. {
  82. static char *old_mtdparts;
  83. static char *old_mtdids;
  84. const char *mtdparts = env_get("mtdparts");
  85. const char *mtdids = env_get("mtdids");
  86. bool remaining_partitions = true;
  87. struct mtd_info *mtd;
  88. mtd_probe_uclass_mtd_devs();
  89. /* Check if mtdparts/mtdids changed since last call, otherwise: exit */
  90. if (!strcmp(mtdparts, old_mtdparts) && !strcmp(mtdids, old_mtdids))
  91. return 0;
  92. /* Update the local copy of mtdparts */
  93. free(old_mtdparts);
  94. free(old_mtdids);
  95. old_mtdparts = strdup(mtdparts);
  96. old_mtdids = strdup(mtdids);
  97. /* If at least one partition is still in use, do not delete anything */
  98. mtd_for_each_device(mtd) {
  99. if (mtd->usecount) {
  100. printf("Partition \"%s\" already in use, aborting\n",
  101. mtd->name);
  102. return -EACCES;
  103. }
  104. }
  105. /*
  106. * Everything looks clear, remove all partitions. It is not safe to
  107. * remove entries from the mtd_for_each_device loop as it uses idr
  108. * indexes and the partitions removal is done in bulk (all partitions of
  109. * one device at the same time), so break and iterate from start each
  110. * time a new partition is found and deleted.
  111. */
  112. while (remaining_partitions) {
  113. remaining_partitions = false;
  114. mtd_for_each_device(mtd) {
  115. if (!mtd_is_partition(mtd) && mtd_has_partitions(mtd)) {
  116. del_mtd_partitions(mtd);
  117. remaining_partitions = true;
  118. break;
  119. }
  120. }
  121. }
  122. /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
  123. if (strstr(mtdparts, "mtdparts="))
  124. mtdparts += 9;
  125. /* For each MTD device in mtdparts */
  126. while (mtdparts[0] != '\0') {
  127. char mtd_name[MTD_NAME_MAX_LEN], *colon;
  128. struct mtd_partition *parts;
  129. int mtd_name_len, nparts;
  130. int ret;
  131. colon = strchr(mtdparts, ':');
  132. if (!colon) {
  133. printf("Wrong mtdparts: %s\n", mtdparts);
  134. return -EINVAL;
  135. }
  136. mtd_name_len = colon - mtdparts;
  137. strncpy(mtd_name, mtdparts, mtd_name_len);
  138. mtd_name[mtd_name_len] = '\0';
  139. /* Move the pointer forward (including the ':') */
  140. mtdparts += mtd_name_len + 1;
  141. mtd = get_mtd_device_nm(mtd_name);
  142. if (IS_ERR_OR_NULL(mtd)) {
  143. char linux_name[MTD_NAME_MAX_LEN];
  144. /*
  145. * The MTD device named "mtd_name" does not exist. Try
  146. * to find a correspondance with an MTD device having
  147. * the same type and number as defined in the mtdids.
  148. */
  149. debug("No device named %s\n", mtd_name);
  150. ret = mtd_search_alternate_name(mtd_name, linux_name,
  151. MTD_NAME_MAX_LEN);
  152. if (!ret)
  153. mtd = get_mtd_device_nm(linux_name);
  154. /*
  155. * If no device could be found, move the mtdparts
  156. * pointer forward until the next set of partitions.
  157. */
  158. if (ret || IS_ERR_OR_NULL(mtd)) {
  159. printf("Could not find a valid device for %s\n",
  160. mtd_name);
  161. mtdparts = strchr(mtdparts, ';');
  162. if (mtdparts)
  163. mtdparts++;
  164. continue;
  165. }
  166. }
  167. /*
  168. * Parse the MTD device partitions. It will update the mtdparts
  169. * pointer, create an array of parts (that must be freed), and
  170. * return the number of partition structures in the array.
  171. */
  172. ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
  173. if (ret) {
  174. printf("Could not parse device %s\n", mtd->name);
  175. put_mtd_device(mtd);
  176. return -EINVAL;
  177. }
  178. if (!nparts)
  179. continue;
  180. /* Create the new MTD partitions */
  181. add_mtd_partitions(mtd, parts, nparts);
  182. /* Free the structures allocated during the parsing */
  183. mtd_free_parsed_partitions(parts, nparts);
  184. put_mtd_device(mtd);
  185. }
  186. return 0;
  187. }
  188. #else
  189. int mtd_probe_devices(void)
  190. {
  191. mtd_probe_uclass_mtd_devs();
  192. return 0;
  193. }
  194. #endif /* defined(CONFIG_MTD_PARTITIONS) */
  195. /* Legacy */
  196. static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
  197. loff_t *maxsize, int devtype)
  198. {
  199. #ifdef CONFIG_CMD_MTDPARTS
  200. struct mtd_device *dev;
  201. struct part_info *part;
  202. u8 pnum;
  203. int ret;
  204. ret = mtdparts_init();
  205. if (ret)
  206. return ret;
  207. ret = find_dev_and_part(partname, &dev, &pnum, &part);
  208. if (ret)
  209. return ret;
  210. if (dev->id->type != devtype) {
  211. printf("not same typ %d != %d\n", dev->id->type, devtype);
  212. return -1;
  213. }
  214. *off = part->offset;
  215. *size = part->size;
  216. *maxsize = part->size;
  217. *idx = dev->id->num;
  218. return 0;
  219. #else
  220. puts("mtdparts support missing.\n");
  221. return -1;
  222. #endif
  223. }
  224. int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
  225. loff_t *maxsize, int devtype, uint64_t chipsize)
  226. {
  227. if (!str2off(arg, off))
  228. return get_part(arg, idx, off, size, maxsize, devtype);
  229. if (*off >= chipsize) {
  230. puts("Offset exceeds device limit\n");
  231. return -1;
  232. }
  233. *maxsize = chipsize - *off;
  234. *size = *maxsize;
  235. return 0;
  236. }
  237. int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
  238. loff_t *size, loff_t *maxsize, int devtype,
  239. uint64_t chipsize)
  240. {
  241. int ret;
  242. if (argc == 0) {
  243. *off = 0;
  244. *size = chipsize;
  245. *maxsize = *size;
  246. goto print;
  247. }
  248. ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
  249. chipsize);
  250. if (ret)
  251. return ret;
  252. if (argc == 1)
  253. goto print;
  254. if (!str2off(argv[1], size)) {
  255. printf("'%s' is not a number\n", argv[1]);
  256. return -1;
  257. }
  258. if (*size > *maxsize) {
  259. puts("Size exceeds partition or device limit\n");
  260. return -1;
  261. }
  262. print:
  263. printf("device %d ", *idx);
  264. if (*size == chipsize)
  265. puts("whole chip\n");
  266. else
  267. printf("offset 0x%llx, size 0x%llx\n",
  268. (unsigned long long)*off, (unsigned long long)*size);
  269. return 0;
  270. }