1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
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>
17 #define MTD_NAME_MAX_LEN 20
19 void board_mtdparts_default(const char **mtdids, const char **mtdparts);
21 static const char *get_mtdids(void)
23 __maybe_unused const char *mtdparts = NULL;
24 const char *mtdids = env_get("mtdids");
29 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
30 board_mtdparts_default(&mtdids, &mtdparts);
31 #elif defined(MTDIDS_DEFAULT)
32 mtdids = MTDIDS_DEFAULT;
33 #elif defined(CONFIG_MTDIDS_DEFAULT)
34 mtdids = CONFIG_MTDIDS_DEFAULT;
38 env_set("mtdids", mtdids);
44 * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
45 * the mtdids legacy environment variable.
47 * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
48 * Check if one of the mtd_id matches mtdname, in this case save dev_id in
51 * @mtdname: Current MTD device name
52 * @altname: Alternate name to return
53 * @max_len: Length of the alternate name buffer
55 * @return 0 on success, an error otherwise.
57 int mtd_search_alternate_name(const char *mtdname, char *altname,
60 const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
61 int dev_id_len, mtd_id_len;
63 mtdids = get_mtdids();
68 /* Find the '=' sign */
70 equal = strchr(dev_id, '=');
73 dev_id_len = equal - mtdids;
76 /* Find the end of the tupple */
77 comma = strchr(mtdids, ',');
79 mtd_id_len = comma - mtd_id;
81 mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
83 if (!dev_id_len || !mtd_id_len)
86 if (dev_id_len + 1 > max_len)
89 /* Compare the name we search with the current mtd_id */
90 if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
91 strncpy(altname, dev_id, dev_id_len);
92 altname[dev_id_len] = 0;
97 /* Go to the next tupple */
104 #if IS_ENABLED(CONFIG_DM_MTD)
105 static void mtd_probe_uclass_mtd_devs(void)
110 /* Probe devices with DM compliant drivers */
111 while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
117 static void mtd_probe_uclass_mtd_devs(void) { }
120 #if defined(CONFIG_MTD_PARTITIONS)
122 #define MTDPARTS_MAXLEN 512
124 static const char *get_mtdparts(void)
126 __maybe_unused const char *mtdids = NULL;
127 static char tmp_parts[MTDPARTS_MAXLEN];
128 const char *mtdparts = NULL;
130 if (gd->flags & GD_FLG_ENV_READY)
131 mtdparts = env_get("mtdparts");
132 else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1)
133 mtdparts = tmp_parts;
138 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
139 board_mtdparts_default(&mtdids, &mtdparts);
140 #elif defined(MTDPARTS_DEFAULT)
141 mtdparts = MTDPARTS_DEFAULT;
142 #elif defined(CONFIG_MTDPARTS_DEFAULT)
143 mtdparts = CONFIG_MTDPARTS_DEFAULT;
147 env_set("mtdparts", mtdparts);
152 static int mtd_del_parts(struct mtd_info *mtd, bool quiet)
156 if (!mtd_has_partitions(mtd))
159 /* do not delete partitions if they are in use. */
160 if (mtd_partitions_used(mtd)) {
162 printf("\"%s\" partitions still in use, can't delete them\n",
167 ret = del_mtd_partitions(mtd);
174 static bool mtd_del_all_parts_failed;
176 static void mtd_del_all_parts(void)
178 struct mtd_info *mtd;
181 mtd_del_all_parts_failed = false;
184 * It is not safe to remove entries from the mtd_for_each_device loop
185 * as it uses idr indexes and the partitions removal is done in bulk
186 * (all partitions of one device at the same time), so break and
187 * iterate from start each time a new partition is found and deleted.
190 mtd_for_each_device(mtd) {
191 ret = mtd_del_parts(mtd, false);
195 mtd_del_all_parts_failed = true;
200 int mtd_probe_devices(void)
202 static char *old_mtdparts;
203 static char *old_mtdids;
204 const char *mtdparts = get_mtdparts();
205 const char *mtdids = get_mtdids();
206 const char *mtdparts_next = mtdparts;
207 struct mtd_info *mtd;
209 mtd_probe_uclass_mtd_devs();
212 * Check if mtdparts/mtdids changed, if the MTD dev list was updated
213 * or if our previous attempt to delete existing partititions failed.
214 * In any of these cases we want to update the partitions, otherwise,
215 * everything is up-to-date and we can return 0 directly.
217 if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
218 (mtdparts && old_mtdparts && mtdids && old_mtdids &&
219 !mtd_dev_list_updated() && !mtd_del_all_parts_failed &&
220 !strcmp(mtdparts, old_mtdparts) &&
221 !strcmp(mtdids, old_mtdids)))
224 /* Update the local copy of mtdparts */
227 old_mtdparts = strdup(mtdparts);
228 old_mtdids = strdup(mtdids);
231 * Remove all old parts. Note that partition removal can fail in case
232 * one of the partition is still being used by an MTD user, so this
233 * does not guarantee that all old partitions are gone.
238 * Call mtd_dev_list_updated() to clear updates generated by our own
239 * parts removal loop.
241 mtd_dev_list_updated();
243 /* If either mtdparts or mtdids is empty, then exit */
244 if (!mtdparts || !mtdids)
247 /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
248 if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1))
251 /* For each MTD device in mtdparts */
252 for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) {
253 char mtd_name[MTD_NAME_MAX_LEN], *colon;
254 struct mtd_partition *parts;
255 unsigned int mtd_name_len;
258 mtdparts_next = strchr(mtdparts, ';');
260 mtdparts_next = mtdparts + strlen(mtdparts);
264 colon = strchr(mtdparts, ':');
265 if (colon > mtdparts_next)
269 printf("Wrong mtdparts: %s\n", mtdparts);
273 mtd_name_len = (unsigned int)(colon - mtdparts);
274 if (mtd_name_len + 1 > sizeof(mtd_name)) {
275 printf("MTD name too long: %s\n", mtdparts);
279 strncpy(mtd_name, mtdparts, mtd_name_len);
280 mtd_name[mtd_name_len] = '\0';
281 /* Move the pointer forward (including the ':') */
282 mtdparts += mtd_name_len + 1;
283 mtd = get_mtd_device_nm(mtd_name);
284 if (IS_ERR_OR_NULL(mtd)) {
285 char linux_name[MTD_NAME_MAX_LEN];
288 * The MTD device named "mtd_name" does not exist. Try
289 * to find a correspondance with an MTD device having
290 * the same type and number as defined in the mtdids.
292 debug("No device named %s\n", mtd_name);
293 ret = mtd_search_alternate_name(mtd_name, linux_name,
296 mtd = get_mtd_device_nm(linux_name);
299 * If no device could be found, move the mtdparts
300 * pointer forward until the next set of partitions.
302 if (ret || IS_ERR_OR_NULL(mtd)) {
303 printf("Could not find a valid device for %s\n",
305 mtdparts = mtdparts_next;
311 * Call mtd_del_parts() again, even if it's already been called
312 * in mtd_del_all_parts(). We need to know if old partitions are
313 * still around (because they are still being used by someone),
314 * and if they are, we shouldn't create new partitions, so just
315 * skip this MTD device and try the next one.
317 ret = mtd_del_parts(mtd, true);
322 * Parse the MTD device partitions. It will update the mtdparts
323 * pointer, create an array of parts (that must be freed), and
324 * return the number of partition structures in the array.
326 ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
328 printf("Could not parse device %s\n", mtd->name);
336 /* Create the new MTD partitions */
337 add_mtd_partitions(mtd, parts, nparts);
339 /* Free the structures allocated during the parsing */
340 mtd_free_parsed_partitions(parts, nparts);
346 * Call mtd_dev_list_updated() to clear updates generated by our own
347 * parts registration loop.
349 mtd_dev_list_updated();
354 int mtd_probe_devices(void)
356 mtd_probe_uclass_mtd_devs();
360 #endif /* defined(CONFIG_MTD_PARTITIONS) */