xref: /rk3399_rockchip-uboot/drivers/mtd/mtd_uboot.c (revision a38bf0a9b17c4255faece9db47065cc02b839f9a)
1 /*
2  * (C) Copyright 2014
3  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <dm/device.h>
9 #include <dm/uclass-internal.h>
10 #include <jffs2/jffs2.h> /* LEGACY */
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/partitions.h>
13 #include <mtd.h>
14 
15 #define MTD_NAME_MAX_LEN 20
16 
17 void board_mtdparts_default(const char **mtdids, const char **mtdparts);
18 
19 static 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 }
40 
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  */
55 int 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 
61 	mtdids = get_mtdids();
62 	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 
102 #if IS_ENABLED(CONFIG_MTD)
103 static 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
115 static void mtd_probe_uclass_mtd_devs(void) { }
116 #endif
117 
118 #if defined(CONFIG_MTD_PARTITIONS)
119 
120 #define MTDPARTS_MAXLEN         512
121 
122 static const char *get_mtdparts(void)
123 {
124 	__maybe_unused const char *mtdids = NULL;
125 	static char tmp_parts[MTDPARTS_MAXLEN];
126 	static bool use_defaults = true;
127 	const char *mtdparts = NULL;
128 
129 	if (gd->flags & GD_FLG_ENV_READY)
130 		mtdparts = env_get("mtdparts");
131 	else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1)
132 		mtdparts = tmp_parts;
133 
134 	if (mtdparts || !use_defaults)
135 		return mtdparts;
136 
137 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
138 	board_mtdparts_default(&mtdids, &mtdparts);
139 #elif defined(MTDPARTS_DEFAULT)
140 	mtdparts = MTDPARTS_DEFAULT;
141 #elif defined(CONFIG_MTDPARTS_DEFAULT)
142 	mtdparts = CONFIG_MTDPARTS_DEFAULT;
143 #endif
144 
145 	if (mtdparts)
146 		env_set("mtdparts", mtdparts);
147 
148 	use_defaults = false;
149 
150 	return mtdparts;
151 }
152 
153 int mtd_probe_devices(void)
154 {
155 	static char *old_mtdparts;
156 	static char *old_mtdids;
157 	const char *mtdparts = get_mtdparts();
158 	const char *mtdids = get_mtdids();
159 	const char *mtdparts_next = mtdparts;
160 	bool remaining_partitions = true;
161 	struct mtd_info *mtd;
162 
163 	mtd_probe_uclass_mtd_devs();
164 
165 	/*
166 	 * Check if mtdparts/mtdids changed or if the MTD dev list was updated
167 	 * since last call, otherwise: exit
168 	 */
169 	if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
170 	    (mtdparts && old_mtdparts && mtdids && old_mtdids &&
171 	     !mtd_dev_list_updated() &&
172 	     !strcmp(mtdparts, old_mtdparts) &&
173 	     !strcmp(mtdids, old_mtdids)))
174 		return 0;
175 
176 	/* Update the local copy of mtdparts */
177 	free(old_mtdparts);
178 	free(old_mtdids);
179 	old_mtdparts = strdup(mtdparts);
180 	old_mtdids = strdup(mtdids);
181 
182 	/* If at least one partition is still in use, do not delete anything */
183 	mtd_for_each_device(mtd) {
184 		if (mtd->usecount) {
185 			printf("Partition \"%s\" already in use, aborting\n",
186 			       mtd->name);
187 			return -EACCES;
188 		}
189 	}
190 
191 	/*
192 	 * Everything looks clear, remove all partitions. It is not safe to
193 	 * remove entries from the mtd_for_each_device loop as it uses idr
194 	 * indexes and the partitions removal is done in bulk (all partitions of
195 	 * one device at the same time), so break and iterate from start each
196 	 * time a new partition is found and deleted.
197 	 */
198 	while (remaining_partitions) {
199 		remaining_partitions = false;
200 		mtd_for_each_device(mtd) {
201 			if (!mtd_is_partition(mtd) && mtd_has_partitions(mtd)) {
202 				del_mtd_partitions(mtd);
203 				remaining_partitions = true;
204 				break;
205 			}
206 		}
207 	}
208 
209 	/*
210 	 * Call mtd_dev_list_updated() to clear updates generated by our own
211 	 * parts removal loop.
212 	 */
213 	mtd_dev_list_updated();
214 
215 	/* If either mtdparts or mtdids is empty, then exit */
216 	if (!mtdparts || !mtdids)
217 		return 0;
218 
219 	/* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
220 	if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1))
221 		mtdparts += 9;
222 
223 	/* For each MTD device in mtdparts */
224 	for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) {
225 		char mtd_name[MTD_NAME_MAX_LEN], *colon;
226 		struct mtd_partition *parts;
227 		unsigned int mtd_name_len;
228 		int nparts, ret;
229 
230 		mtdparts_next = strchr(mtdparts, ';');
231 		if (!mtdparts_next)
232 			mtdparts_next = mtdparts + strlen(mtdparts);
233 		else
234 			mtdparts_next++;
235 
236 		colon = strchr(mtdparts, ':');
237 		if (colon > mtdparts_next)
238 			colon = NULL;
239 
240 		if (!colon) {
241 			printf("Wrong mtdparts: %s\n", mtdparts);
242 			return -EINVAL;
243 		}
244 
245 		mtd_name_len = (unsigned int)(colon - mtdparts);
246 		if (mtd_name_len + 1 > sizeof(mtd_name)) {
247 			printf("MTD name too long: %s\n", mtdparts);
248 			return -EINVAL;
249 		}
250 
251 		strncpy(mtd_name, mtdparts, mtd_name_len);
252 		mtd_name[mtd_name_len] = '\0';
253 		/* Move the pointer forward (including the ':') */
254 		mtdparts += mtd_name_len + 1;
255 		mtd = get_mtd_device_nm(mtd_name);
256 		if (IS_ERR_OR_NULL(mtd)) {
257 			char linux_name[MTD_NAME_MAX_LEN];
258 
259 			/*
260 			 * The MTD device named "mtd_name" does not exist. Try
261 			 * to find a correspondance with an MTD device having
262 			 * the same type and number as defined in the mtdids.
263 			 */
264 			debug("No device named %s\n", mtd_name);
265 			ret = mtd_search_alternate_name(mtd_name, linux_name,
266 							MTD_NAME_MAX_LEN);
267 			if (!ret)
268 				mtd = get_mtd_device_nm(linux_name);
269 
270 			/*
271 			 * If no device could be found, move the mtdparts
272 			 * pointer forward until the next set of partitions.
273 			 */
274 			if (ret || IS_ERR_OR_NULL(mtd)) {
275 				printf("Could not find a valid device for %s\n",
276 				       mtd_name);
277 				mtdparts = mtdparts_next;
278 				continue;
279 			}
280 		}
281 
282 		/*
283 		 * Parse the MTD device partitions. It will update the mtdparts
284 		 * pointer, create an array of parts (that must be freed), and
285 		 * return the number of partition structures in the array.
286 		 */
287 		ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
288 		if (ret) {
289 			printf("Could not parse device %s\n", mtd->name);
290 			put_mtd_device(mtd);
291 			return -EINVAL;
292 		}
293 
294 		if (!nparts)
295 			continue;
296 
297 		/* Create the new MTD partitions */
298 		add_mtd_partitions(mtd, parts, nparts);
299 
300 		/* Free the structures allocated during the parsing */
301 		mtd_free_parsed_partitions(parts, nparts);
302 
303 		put_mtd_device(mtd);
304 	}
305 
306 	/*
307 	 * Call mtd_dev_list_updated() to clear updates generated by our own
308 	 * parts registration loop.
309 	 */
310 	mtd_dev_list_updated();
311 
312 	return 0;
313 }
314 #else
315 int mtd_probe_devices(void)
316 {
317 	mtd_probe_uclass_mtd_devs();
318 
319 	return 0;
320 }
321 #endif /* defined(CONFIG_MTD_PARTITIONS) */
322 
323 /* Legacy */
324 
325 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
326 		loff_t *maxsize, int devtype)
327 {
328 #ifdef CONFIG_CMD_MTDPARTS
329 	struct mtd_device *dev;
330 	struct part_info *part;
331 	u8 pnum;
332 	int ret;
333 
334 	ret = mtdparts_init();
335 	if (ret)
336 		return ret;
337 
338 	ret = find_dev_and_part(partname, &dev, &pnum, &part);
339 	if (ret)
340 		return ret;
341 
342 	if (dev->id->type != devtype) {
343 		printf("not same typ %d != %d\n", dev->id->type, devtype);
344 		return -1;
345 	}
346 
347 	*off = part->offset;
348 	*size = part->size;
349 	*maxsize = part->size;
350 	*idx = dev->id->num;
351 
352 	return 0;
353 #else
354 	puts("mtdparts support missing.\n");
355 	return -1;
356 #endif
357 }
358 
359 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
360 		loff_t *maxsize, int devtype, uint64_t chipsize)
361 {
362 	if (!str2off(arg, off))
363 		return get_part(arg, idx, off, size, maxsize, devtype);
364 
365 	if (*off >= chipsize) {
366 		puts("Offset exceeds device limit\n");
367 		return -1;
368 	}
369 
370 	*maxsize = chipsize - *off;
371 	*size = *maxsize;
372 	return 0;
373 }
374 
375 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
376 		     loff_t *size, loff_t *maxsize, int devtype,
377 		     uint64_t chipsize)
378 {
379 	int ret;
380 
381 	if (argc == 0) {
382 		*off = 0;
383 		*size = chipsize;
384 		*maxsize = *size;
385 		goto print;
386 	}
387 
388 	ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
389 			  chipsize);
390 	if (ret)
391 		return ret;
392 
393 	if (argc == 1)
394 		goto print;
395 
396 	if (!str2off(argv[1], size)) {
397 		printf("'%s' is not a number\n", argv[1]);
398 		return -1;
399 	}
400 
401 	if (*size > *maxsize) {
402 		puts("Size exceeds partition or device limit\n");
403 		return -1;
404 	}
405 
406 print:
407 	printf("device %d ", *idx);
408 	if (*size == chipsize)
409 		puts("whole chip\n");
410 	else
411 		printf("offset 0x%llx, size 0x%llx\n",
412 		       (unsigned long long)*off, (unsigned long long)*size);
413 	return 0;
414 }
415