blob: d638f700d041e32757f1e3dea3a005085cfd2a12 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher09c32802015-04-27 07:42:05 +02002/*
3 * (C) Copyright 2014
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
Heiko Schocher09c32802015-04-27 07:42:05 +02005 */
6#include <common.h>
Miquel Raynal5db66b32018-09-29 12:58:28 +02007#include <dm/device.h>
8#include <dm/uclass-internal.h>
9#include <jffs2/jffs2.h> /* LEGACY */
Heiko Schocher09c32802015-04-27 07:42:05 +020010#include <linux/mtd/mtd.h>
Miquel Raynal5db66b32018-09-29 12:58:28 +020011#include <linux/mtd/partitions.h>
12#include <mtd.h>
13
14#define MTD_NAME_MAX_LEN 20
15
Boris Brezillon96b06432018-12-02 10:54:26 +010016void board_mtdparts_default(const char **mtdids, const char **mtdparts);
17
18static const char *get_mtdids(void)
19{
20 __maybe_unused const char *mtdparts = NULL;
21 const char *mtdids = env_get("mtdids");
22
23 if (mtdids)
24 return mtdids;
25
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
34 if (mtdids)
35 env_set("mtdids", mtdids);
36
37 return mtdids;
38}
Miquel Raynalff4afa82018-09-29 12:58:26 +020039
40/**
41 * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
42 * the mtdids legacy environment variable.
43 *
44 * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
45 * Check if one of the mtd_id matches mtdname, in this case save dev_id in
46 * altname.
47 *
48 * @mtdname: Current MTD device name
49 * @altname: Alternate name to return
50 * @max_len: Length of the alternate name buffer
51 *
52 * @return 0 on success, an error otherwise.
53 */
54int mtd_search_alternate_name(const char *mtdname, char *altname,
55 unsigned int max_len)
56{
57 const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
58 int dev_id_len, mtd_id_len;
59
Boris Brezillon96b06432018-12-02 10:54:26 +010060 mtdids = get_mtdids();
Miquel Raynalff4afa82018-09-29 12:58:26 +020061 if (!mtdids)
62 return -EINVAL;
63
64 do {
65 /* Find the '=' sign */
66 dev_id = mtdids;
67 equal = strchr(dev_id, '=');
68 if (!equal)
69 break;
70 dev_id_len = equal - mtdids;
71 mtd_id = equal + 1;
72
73 /* Find the end of the tupple */
74 comma = strchr(mtdids, ',');
75 if (comma)
76 mtd_id_len = comma - mtd_id;
77 else
78 mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
79
80 if (!dev_id_len || !mtd_id_len)
81 return -EINVAL;
82
83 if (dev_id_len + 1 > max_len)
84 continue;
85
86 /* Compare the name we search with the current mtd_id */
87 if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
88 strncpy(altname, dev_id, dev_id_len);
89 altname[dev_id_len] = 0;
90
91 return 0;
92 }
93
94 /* Go to the next tupple */
95 mtdids = comma + 1;
96 } while (comma);
97
98 return -EINVAL;
99}
100
Miquel Raynal5db66b32018-09-29 12:58:28 +0200101#if IS_ENABLED(CONFIG_MTD)
102static void mtd_probe_uclass_mtd_devs(void)
103{
104 struct udevice *dev;
105 int idx = 0;
106
107 /* Probe devices with DM compliant drivers */
108 while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
109 mtd_probe(dev);
110 idx++;
111 }
112}
113#else
114static void mtd_probe_uclass_mtd_devs(void) { }
115#endif
116
117#if defined(CONFIG_MTD_PARTITIONS)
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100118
119#define MTDPARTS_MAXLEN 512
120
121static const char *get_mtdparts(void)
122{
123 __maybe_unused const char *mtdids = NULL;
124 static char tmp_parts[MTDPARTS_MAXLEN];
125 static bool use_defaults = true;
126 const char *mtdparts = NULL;
127
128 if (gd->flags & GD_FLG_ENV_READY)
129 mtdparts = env_get("mtdparts");
130 else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1)
131 mtdparts = tmp_parts;
132
133 if (mtdparts || !use_defaults)
134 return mtdparts;
135
136#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
137 board_mtdparts_default(&mtdids, &mtdparts);
138#elif defined(MTDPARTS_DEFAULT)
139 mtdparts = MTDPARTS_DEFAULT;
140#elif defined(CONFIG_MTDPARTS_DEFAULT)
141 mtdparts = CONFIG_MTDPARTS_DEFAULT;
142#endif
143
144 if (mtdparts)
145 env_set("mtdparts", mtdparts);
146
147 use_defaults = false;
148
149 return mtdparts;
150}
151
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100152static int mtd_del_parts(struct mtd_info *mtd, bool quiet)
153{
154 int ret;
155
156 if (!mtd_has_partitions(mtd))
157 return 0;
158
159 /* do not delete partitions if they are in use. */
160 if (mtd_partitions_used(mtd)) {
161 if (!quiet)
162 printf("\"%s\" partitions still in use, can't delete them\n",
163 mtd->name);
164 return -EACCES;
165 }
166
167 ret = del_mtd_partitions(mtd);
168 if (ret)
169 return ret;
170
171 return 1;
172}
173
174static bool mtd_del_all_parts_failed;
175
176static void mtd_del_all_parts(void)
177{
178 struct mtd_info *mtd;
179 int ret = 0;
180
181 mtd_del_all_parts_failed = false;
182
183 /*
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.
188 */
189 do {
190 mtd_for_each_device(mtd) {
191 ret = mtd_del_parts(mtd, false);
192 if (ret > 0)
193 break;
194 else if (ret < 0)
195 mtd_del_all_parts_failed = true;
196 }
197 } while (ret > 0);
198}
199
Miquel Raynal5db66b32018-09-29 12:58:28 +0200200int mtd_probe_devices(void)
201{
202 static char *old_mtdparts;
203 static char *old_mtdids;
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100204 const char *mtdparts = get_mtdparts();
205 const char *mtdids = get_mtdids();
Boris Brezillon2428d912018-12-02 10:54:29 +0100206 const char *mtdparts_next = mtdparts;
Miquel Raynal5db66b32018-09-29 12:58:28 +0200207 struct mtd_info *mtd;
208
209 mtd_probe_uclass_mtd_devs();
210
Boris Brezillon779c9c02018-12-02 10:54:23 +0100211 /*
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100212 * 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.
Boris Brezillon779c9c02018-12-02 10:54:23 +0100216 */
Adam Fordc8602062018-10-08 14:13:03 -0500217 if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
218 (mtdparts && old_mtdparts && mtdids && old_mtdids &&
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100219 !mtd_dev_list_updated() && !mtd_del_all_parts_failed &&
Adam Fordc8602062018-10-08 14:13:03 -0500220 !strcmp(mtdparts, old_mtdparts) &&
221 !strcmp(mtdids, old_mtdids)))
Miquel Raynal5db66b32018-09-29 12:58:28 +0200222 return 0;
223
224 /* Update the local copy of mtdparts */
225 free(old_mtdparts);
226 free(old_mtdids);
227 old_mtdparts = strdup(mtdparts);
228 old_mtdids = strdup(mtdids);
229
Miquel Raynal5db66b32018-09-29 12:58:28 +0200230 /*
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100231 * 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.
Miquel Raynal5db66b32018-09-29 12:58:28 +0200234 */
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100235 mtd_del_all_parts();
Miquel Raynal5db66b32018-09-29 12:58:28 +0200236
Boris Brezillon779c9c02018-12-02 10:54:23 +0100237 /*
238 * Call mtd_dev_list_updated() to clear updates generated by our own
239 * parts removal loop.
240 */
241 mtd_dev_list_updated();
242
Adam Fordc8602062018-10-08 14:13:03 -0500243 /* If either mtdparts or mtdids is empty, then exit */
244 if (!mtdparts || !mtdids)
245 return 0;
246
Miquel Raynal5db66b32018-09-29 12:58:28 +0200247 /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
Boris Brezillon429e0482018-12-02 10:54:27 +0100248 if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1))
Miquel Raynal5db66b32018-09-29 12:58:28 +0200249 mtdparts += 9;
250
251 /* For each MTD device in mtdparts */
Boris Brezillon2428d912018-12-02 10:54:29 +0100252 for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) {
Miquel Raynal5db66b32018-09-29 12:58:28 +0200253 char mtd_name[MTD_NAME_MAX_LEN], *colon;
254 struct mtd_partition *parts;
Boris Brezillon772aa972018-12-02 10:54:28 +0100255 unsigned int mtd_name_len;
256 int nparts, ret;
Miquel Raynal5db66b32018-09-29 12:58:28 +0200257
Boris Brezillon2428d912018-12-02 10:54:29 +0100258 mtdparts_next = strchr(mtdparts, ';');
259 if (!mtdparts_next)
260 mtdparts_next = mtdparts + strlen(mtdparts);
261 else
262 mtdparts_next++;
263
Miquel Raynal5db66b32018-09-29 12:58:28 +0200264 colon = strchr(mtdparts, ':');
Boris Brezillon2428d912018-12-02 10:54:29 +0100265 if (colon > mtdparts_next)
266 colon = NULL;
267
Miquel Raynal5db66b32018-09-29 12:58:28 +0200268 if (!colon) {
269 printf("Wrong mtdparts: %s\n", mtdparts);
270 return -EINVAL;
271 }
272
Boris Brezillon772aa972018-12-02 10:54:28 +0100273 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);
276 return -EINVAL;
277 }
278
Miquel Raynal5db66b32018-09-29 12:58:28 +0200279 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];
286
287 /*
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.
291 */
292 debug("No device named %s\n", mtd_name);
293 ret = mtd_search_alternate_name(mtd_name, linux_name,
294 MTD_NAME_MAX_LEN);
295 if (!ret)
296 mtd = get_mtd_device_nm(linux_name);
297
298 /*
299 * If no device could be found, move the mtdparts
300 * pointer forward until the next set of partitions.
301 */
302 if (ret || IS_ERR_OR_NULL(mtd)) {
303 printf("Could not find a valid device for %s\n",
304 mtd_name);
Boris Brezillon2428d912018-12-02 10:54:29 +0100305 mtdparts = mtdparts_next;
Miquel Raynal5db66b32018-09-29 12:58:28 +0200306 continue;
307 }
308 }
309
310 /*
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100311 * 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.
316 */
317 ret = mtd_del_parts(mtd, true);
318 if (ret < 0)
319 continue;
320
321 /*
Miquel Raynal5db66b32018-09-29 12:58:28 +0200322 * 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.
325 */
326 ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
327 if (ret) {
328 printf("Could not parse device %s\n", mtd->name);
329 put_mtd_device(mtd);
330 return -EINVAL;
331 }
332
333 if (!nparts)
334 continue;
335
336 /* Create the new MTD partitions */
337 add_mtd_partitions(mtd, parts, nparts);
338
339 /* Free the structures allocated during the parsing */
340 mtd_free_parsed_partitions(parts, nparts);
341
342 put_mtd_device(mtd);
343 }
344
Boris Brezillon779c9c02018-12-02 10:54:23 +0100345 /*
346 * Call mtd_dev_list_updated() to clear updates generated by our own
347 * parts registration loop.
348 */
349 mtd_dev_list_updated();
350
Miquel Raynal5db66b32018-09-29 12:58:28 +0200351 return 0;
352}
353#else
354int mtd_probe_devices(void)
355{
356 mtd_probe_uclass_mtd_devs();
357
358 return 0;
359}
360#endif /* defined(CONFIG_MTD_PARTITIONS) */
361
Miquel Raynalff4afa82018-09-29 12:58:26 +0200362/* Legacy */
Heiko Schocher09c32802015-04-27 07:42:05 +0200363
364static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
365 loff_t *maxsize, int devtype)
366{
367#ifdef CONFIG_CMD_MTDPARTS
368 struct mtd_device *dev;
369 struct part_info *part;
370 u8 pnum;
371 int ret;
372
373 ret = mtdparts_init();
374 if (ret)
375 return ret;
376
377 ret = find_dev_and_part(partname, &dev, &pnum, &part);
378 if (ret)
379 return ret;
380
381 if (dev->id->type != devtype) {
382 printf("not same typ %d != %d\n", dev->id->type, devtype);
383 return -1;
384 }
385
386 *off = part->offset;
387 *size = part->size;
388 *maxsize = part->size;
389 *idx = dev->id->num;
390
391 return 0;
392#else
Maxime Ripard10b69712015-10-15 14:34:09 +0200393 puts("mtdparts support missing.\n");
Heiko Schocher09c32802015-04-27 07:42:05 +0200394 return -1;
395#endif
396}
397
398int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
Masahiro Yamadaf18d1112015-07-01 21:35:49 +0900399 loff_t *maxsize, int devtype, uint64_t chipsize)
Heiko Schocher09c32802015-04-27 07:42:05 +0200400{
401 if (!str2off(arg, off))
402 return get_part(arg, idx, off, size, maxsize, devtype);
403
404 if (*off >= chipsize) {
405 puts("Offset exceeds device limit\n");
406 return -1;
407 }
408
409 *maxsize = chipsize - *off;
410 *size = *maxsize;
411 return 0;
412}
413
414int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
Masahiro Yamadaf18d1112015-07-01 21:35:49 +0900415 loff_t *size, loff_t *maxsize, int devtype,
416 uint64_t chipsize)
Heiko Schocher09c32802015-04-27 07:42:05 +0200417{
418 int ret;
419
420 if (argc == 0) {
421 *off = 0;
422 *size = chipsize;
423 *maxsize = *size;
424 goto print;
425 }
426
427 ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
428 chipsize);
429 if (ret)
430 return ret;
431
432 if (argc == 1)
433 goto print;
434
435 if (!str2off(argv[1], size)) {
436 printf("'%s' is not a number\n", argv[1]);
437 return -1;
438 }
439
440 if (*size > *maxsize) {
441 puts("Size exceeds partition or device limit\n");
442 return -1;
443 }
444
445print:
446 printf("device %d ", *idx);
447 if (*size == chipsize)
448 puts("whole chip\n");
449 else
450 printf("offset 0x%llx, size 0x%llx\n",
451 (unsigned long long)*off, (unsigned long long)*size);
452 return 0;
453}