blob: 7c6c9ac0bcc80b51b841b06d3687deb0ae414a68 [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>
Simon Glass3a7d5572019-08-01 09:46:42 -06007#include <env.h>
Miquel Raynal5db66b32018-09-29 12:58:28 +02008#include <dm/device.h>
9#include <dm/uclass-internal.h>
10#include <jffs2/jffs2.h> /* LEGACY */
Heiko Schocher09c32802015-04-27 07:42:05 +020011#include <linux/mtd/mtd.h>
Miquel Raynal5db66b32018-09-29 12:58:28 +020012#include <linux/mtd/partitions.h>
13#include <mtd.h>
14
15#define MTD_NAME_MAX_LEN 20
16
Boris Brezillon96b06432018-12-02 10:54:26 +010017void board_mtdparts_default(const char **mtdids, const char **mtdparts);
18
19static const char *get_mtdids(void)
20{
21 __maybe_unused const char *mtdparts = NULL;
22 const char *mtdids = env_get("mtdids");
23
24 if (mtdids)
25 return mtdids;
26
27#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
28 board_mtdparts_default(&mtdids, &mtdparts);
29#elif defined(MTDIDS_DEFAULT)
30 mtdids = MTDIDS_DEFAULT;
31#elif defined(CONFIG_MTDIDS_DEFAULT)
32 mtdids = CONFIG_MTDIDS_DEFAULT;
33#endif
34
35 if (mtdids)
36 env_set("mtdids", mtdids);
37
38 return mtdids;
39}
Miquel Raynalff4afa82018-09-29 12:58:26 +020040
41/**
42 * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
43 * the mtdids legacy environment variable.
44 *
45 * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
46 * Check if one of the mtd_id matches mtdname, in this case save dev_id in
47 * altname.
48 *
49 * @mtdname: Current MTD device name
50 * @altname: Alternate name to return
51 * @max_len: Length of the alternate name buffer
52 *
53 * @return 0 on success, an error otherwise.
54 */
55int mtd_search_alternate_name(const char *mtdname, char *altname,
56 unsigned int max_len)
57{
58 const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
59 int dev_id_len, mtd_id_len;
60
Boris Brezillon96b06432018-12-02 10:54:26 +010061 mtdids = get_mtdids();
Miquel Raynalff4afa82018-09-29 12:58:26 +020062 if (!mtdids)
63 return -EINVAL;
64
65 do {
66 /* Find the '=' sign */
67 dev_id = mtdids;
68 equal = strchr(dev_id, '=');
69 if (!equal)
70 break;
71 dev_id_len = equal - mtdids;
72 mtd_id = equal + 1;
73
74 /* Find the end of the tupple */
75 comma = strchr(mtdids, ',');
76 if (comma)
77 mtd_id_len = comma - mtd_id;
78 else
79 mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
80
81 if (!dev_id_len || !mtd_id_len)
82 return -EINVAL;
83
84 if (dev_id_len + 1 > max_len)
85 continue;
86
87 /* Compare the name we search with the current mtd_id */
88 if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
89 strncpy(altname, dev_id, dev_id_len);
90 altname[dev_id_len] = 0;
91
92 return 0;
93 }
94
95 /* Go to the next tupple */
96 mtdids = comma + 1;
97 } while (comma);
98
99 return -EINVAL;
100}
101
Miquel Raynal1de770d2019-10-03 19:50:04 +0200102#if IS_ENABLED(CONFIG_DM_MTD)
Miquel Raynal5db66b32018-09-29 12:58:28 +0200103static void mtd_probe_uclass_mtd_devs(void)
104{
105 struct udevice *dev;
106 int idx = 0;
107
108 /* Probe devices with DM compliant drivers */
109 while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
110 mtd_probe(dev);
111 idx++;
112 }
113}
114#else
115static void mtd_probe_uclass_mtd_devs(void) { }
116#endif
117
118#if defined(CONFIG_MTD_PARTITIONS)
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100119
120#define MTDPARTS_MAXLEN 512
121
122static const char *get_mtdparts(void)
123{
124 __maybe_unused const char *mtdids = NULL;
125 static char tmp_parts[MTDPARTS_MAXLEN];
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100126 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
Patrice Chotarde6b7afe2019-01-23 18:12:48 +0100133 if (mtdparts)
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100134 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
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100147 return mtdparts;
148}
149
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100150static int mtd_del_parts(struct mtd_info *mtd, bool quiet)
151{
152 int ret;
153
154 if (!mtd_has_partitions(mtd))
155 return 0;
156
157 /* do not delete partitions if they are in use. */
158 if (mtd_partitions_used(mtd)) {
159 if (!quiet)
160 printf("\"%s\" partitions still in use, can't delete them\n",
161 mtd->name);
162 return -EACCES;
163 }
164
165 ret = del_mtd_partitions(mtd);
166 if (ret)
167 return ret;
168
169 return 1;
170}
171
172static bool mtd_del_all_parts_failed;
173
174static void mtd_del_all_parts(void)
175{
176 struct mtd_info *mtd;
177 int ret = 0;
178
179 mtd_del_all_parts_failed = false;
180
181 /*
182 * It is not safe to remove entries from the mtd_for_each_device loop
183 * as it uses idr indexes and the partitions removal is done in bulk
184 * (all partitions of one device at the same time), so break and
185 * iterate from start each time a new partition is found and deleted.
186 */
187 do {
188 mtd_for_each_device(mtd) {
189 ret = mtd_del_parts(mtd, false);
190 if (ret > 0)
191 break;
192 else if (ret < 0)
193 mtd_del_all_parts_failed = true;
194 }
195 } while (ret > 0);
196}
197
Miquel Raynal5db66b32018-09-29 12:58:28 +0200198int mtd_probe_devices(void)
199{
200 static char *old_mtdparts;
201 static char *old_mtdids;
Boris Brezillon5ffcd502018-11-13 12:43:09 +0100202 const char *mtdparts = get_mtdparts();
203 const char *mtdids = get_mtdids();
Boris Brezillon2428d912018-12-02 10:54:29 +0100204 const char *mtdparts_next = mtdparts;
Miquel Raynal5db66b32018-09-29 12:58:28 +0200205 struct mtd_info *mtd;
206
207 mtd_probe_uclass_mtd_devs();
208
Boris Brezillon779c9c02018-12-02 10:54:23 +0100209 /*
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100210 * Check if mtdparts/mtdids changed, if the MTD dev list was updated
211 * or if our previous attempt to delete existing partititions failed.
212 * In any of these cases we want to update the partitions, otherwise,
213 * everything is up-to-date and we can return 0 directly.
Boris Brezillon779c9c02018-12-02 10:54:23 +0100214 */
Adam Fordc8602062018-10-08 14:13:03 -0500215 if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
216 (mtdparts && old_mtdparts && mtdids && old_mtdids &&
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100217 !mtd_dev_list_updated() && !mtd_del_all_parts_failed &&
Adam Fordc8602062018-10-08 14:13:03 -0500218 !strcmp(mtdparts, old_mtdparts) &&
219 !strcmp(mtdids, old_mtdids)))
Miquel Raynal5db66b32018-09-29 12:58:28 +0200220 return 0;
221
222 /* Update the local copy of mtdparts */
223 free(old_mtdparts);
224 free(old_mtdids);
225 old_mtdparts = strdup(mtdparts);
226 old_mtdids = strdup(mtdids);
227
Miquel Raynal5db66b32018-09-29 12:58:28 +0200228 /*
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100229 * Remove all old parts. Note that partition removal can fail in case
230 * one of the partition is still being used by an MTD user, so this
231 * does not guarantee that all old partitions are gone.
Miquel Raynal5db66b32018-09-29 12:58:28 +0200232 */
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100233 mtd_del_all_parts();
Miquel Raynal5db66b32018-09-29 12:58:28 +0200234
Boris Brezillon779c9c02018-12-02 10:54:23 +0100235 /*
236 * Call mtd_dev_list_updated() to clear updates generated by our own
237 * parts removal loop.
238 */
239 mtd_dev_list_updated();
240
Adam Fordc8602062018-10-08 14:13:03 -0500241 /* If either mtdparts or mtdids is empty, then exit */
242 if (!mtdparts || !mtdids)
243 return 0;
244
Miquel Raynal5db66b32018-09-29 12:58:28 +0200245 /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
Boris Brezillon429e0482018-12-02 10:54:27 +0100246 if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1))
Miquel Raynal5db66b32018-09-29 12:58:28 +0200247 mtdparts += 9;
248
249 /* For each MTD device in mtdparts */
Boris Brezillon2428d912018-12-02 10:54:29 +0100250 for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) {
Miquel Raynal5db66b32018-09-29 12:58:28 +0200251 char mtd_name[MTD_NAME_MAX_LEN], *colon;
252 struct mtd_partition *parts;
Boris Brezillon772aa972018-12-02 10:54:28 +0100253 unsigned int mtd_name_len;
254 int nparts, ret;
Miquel Raynal5db66b32018-09-29 12:58:28 +0200255
Boris Brezillon2428d912018-12-02 10:54:29 +0100256 mtdparts_next = strchr(mtdparts, ';');
257 if (!mtdparts_next)
258 mtdparts_next = mtdparts + strlen(mtdparts);
259 else
260 mtdparts_next++;
261
Miquel Raynal5db66b32018-09-29 12:58:28 +0200262 colon = strchr(mtdparts, ':');
Boris Brezillon2428d912018-12-02 10:54:29 +0100263 if (colon > mtdparts_next)
264 colon = NULL;
265
Miquel Raynal5db66b32018-09-29 12:58:28 +0200266 if (!colon) {
267 printf("Wrong mtdparts: %s\n", mtdparts);
268 return -EINVAL;
269 }
270
Boris Brezillon772aa972018-12-02 10:54:28 +0100271 mtd_name_len = (unsigned int)(colon - mtdparts);
272 if (mtd_name_len + 1 > sizeof(mtd_name)) {
273 printf("MTD name too long: %s\n", mtdparts);
274 return -EINVAL;
275 }
276
Miquel Raynal5db66b32018-09-29 12:58:28 +0200277 strncpy(mtd_name, mtdparts, mtd_name_len);
278 mtd_name[mtd_name_len] = '\0';
279 /* Move the pointer forward (including the ':') */
280 mtdparts += mtd_name_len + 1;
281 mtd = get_mtd_device_nm(mtd_name);
282 if (IS_ERR_OR_NULL(mtd)) {
283 char linux_name[MTD_NAME_MAX_LEN];
284
285 /*
286 * The MTD device named "mtd_name" does not exist. Try
287 * to find a correspondance with an MTD device having
288 * the same type and number as defined in the mtdids.
289 */
290 debug("No device named %s\n", mtd_name);
291 ret = mtd_search_alternate_name(mtd_name, linux_name,
292 MTD_NAME_MAX_LEN);
293 if (!ret)
294 mtd = get_mtd_device_nm(linux_name);
295
296 /*
297 * If no device could be found, move the mtdparts
298 * pointer forward until the next set of partitions.
299 */
300 if (ret || IS_ERR_OR_NULL(mtd)) {
301 printf("Could not find a valid device for %s\n",
302 mtd_name);
Boris Brezillon2428d912018-12-02 10:54:29 +0100303 mtdparts = mtdparts_next;
Miquel Raynal5db66b32018-09-29 12:58:28 +0200304 continue;
305 }
306 }
307
308 /*
Boris Brezillon4a5594f2018-12-02 10:54:30 +0100309 * Call mtd_del_parts() again, even if it's already been called
310 * in mtd_del_all_parts(). We need to know if old partitions are
311 * still around (because they are still being used by someone),
312 * and if they are, we shouldn't create new partitions, so just
313 * skip this MTD device and try the next one.
314 */
315 ret = mtd_del_parts(mtd, true);
316 if (ret < 0)
317 continue;
318
319 /*
Miquel Raynal5db66b32018-09-29 12:58:28 +0200320 * Parse the MTD device partitions. It will update the mtdparts
321 * pointer, create an array of parts (that must be freed), and
322 * return the number of partition structures in the array.
323 */
324 ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
325 if (ret) {
326 printf("Could not parse device %s\n", mtd->name);
327 put_mtd_device(mtd);
328 return -EINVAL;
329 }
330
331 if (!nparts)
332 continue;
333
334 /* Create the new MTD partitions */
335 add_mtd_partitions(mtd, parts, nparts);
336
337 /* Free the structures allocated during the parsing */
338 mtd_free_parsed_partitions(parts, nparts);
339
340 put_mtd_device(mtd);
341 }
342
Boris Brezillon779c9c02018-12-02 10:54:23 +0100343 /*
344 * Call mtd_dev_list_updated() to clear updates generated by our own
345 * parts registration loop.
346 */
347 mtd_dev_list_updated();
348
Miquel Raynal5db66b32018-09-29 12:58:28 +0200349 return 0;
350}
351#else
352int mtd_probe_devices(void)
353{
354 mtd_probe_uclass_mtd_devs();
355
356 return 0;
357}
358#endif /* defined(CONFIG_MTD_PARTITIONS) */
359
Miquel Raynalff4afa82018-09-29 12:58:26 +0200360/* Legacy */
Heiko Schocher09c32802015-04-27 07:42:05 +0200361
362static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
363 loff_t *maxsize, int devtype)
364{
365#ifdef CONFIG_CMD_MTDPARTS
366 struct mtd_device *dev;
367 struct part_info *part;
368 u8 pnum;
369 int ret;
370
371 ret = mtdparts_init();
372 if (ret)
373 return ret;
374
375 ret = find_dev_and_part(partname, &dev, &pnum, &part);
376 if (ret)
377 return ret;
378
379 if (dev->id->type != devtype) {
380 printf("not same typ %d != %d\n", dev->id->type, devtype);
381 return -1;
382 }
383
384 *off = part->offset;
385 *size = part->size;
386 *maxsize = part->size;
387 *idx = dev->id->num;
388
389 return 0;
390#else
Maxime Ripard10b69712015-10-15 14:34:09 +0200391 puts("mtdparts support missing.\n");
Heiko Schocher09c32802015-04-27 07:42:05 +0200392 return -1;
393#endif
394}
395
396int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
Masahiro Yamadaf18d1112015-07-01 21:35:49 +0900397 loff_t *maxsize, int devtype, uint64_t chipsize)
Heiko Schocher09c32802015-04-27 07:42:05 +0200398{
399 if (!str2off(arg, off))
400 return get_part(arg, idx, off, size, maxsize, devtype);
401
402 if (*off >= chipsize) {
403 puts("Offset exceeds device limit\n");
404 return -1;
405 }
406
407 *maxsize = chipsize - *off;
408 *size = *maxsize;
409 return 0;
410}
411
412int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
Masahiro Yamadaf18d1112015-07-01 21:35:49 +0900413 loff_t *size, loff_t *maxsize, int devtype,
414 uint64_t chipsize)
Heiko Schocher09c32802015-04-27 07:42:05 +0200415{
416 int ret;
417
418 if (argc == 0) {
419 *off = 0;
420 *size = chipsize;
421 *maxsize = *size;
422 goto print;
423 }
424
425 ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
426 chipsize);
427 if (ret)
428 return ret;
429
430 if (argc == 1)
431 goto print;
432
433 if (!str2off(argv[1], size)) {
434 printf("'%s' is not a number\n", argv[1]);
435 return -1;
436 }
437
438 if (*size > *maxsize) {
439 puts("Size exceeds partition or device limit\n");
440 return -1;
441 }
442
443print:
444 printf("device %d ", *idx);
445 if (*size == chipsize)
446 puts("whole chip\n");
447 else
448 printf("offset 0x%llx, size 0x%llx\n",
449 (unsigned long long)*off, (unsigned long long)*size);
450 return 0;
451}