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