xref: /rk3399_rockchip-uboot/drivers/mtd/mtd_uboot.c (revision b95c8f6caece637d742719c07d80b56f80c89caa)
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 
18 /**
19  * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
20  *                             the mtdids legacy environment variable.
21  *
22  * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
23  * Check if one of the mtd_id matches mtdname, in this case save dev_id in
24  * altname.
25  *
26  * @mtdname: Current MTD device name
27  * @altname: Alternate name to return
28  * @max_len: Length of the alternate name buffer
29  *
30  * @return 0 on success, an error otherwise.
31  */
32 int mtd_search_alternate_name(const char *mtdname, char *altname,
33 			      unsigned int max_len)
34 {
35 	const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
36 	int dev_id_len, mtd_id_len;
37 
38 	mtdids = env_get("mtdids");
39 	if (!mtdids)
40 		return -EINVAL;
41 
42 	do {
43 		/* Find the '=' sign */
44 		dev_id = mtdids;
45 		equal = strchr(dev_id, '=');
46 		if (!equal)
47 			break;
48 		dev_id_len = equal - mtdids;
49 		mtd_id = equal + 1;
50 
51 		/* Find the end of the tupple */
52 		comma = strchr(mtdids, ',');
53 		if (comma)
54 			mtd_id_len = comma - mtd_id;
55 		else
56 			mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
57 
58 		if (!dev_id_len || !mtd_id_len)
59 			return -EINVAL;
60 
61 		if (dev_id_len + 1 > max_len)
62 			continue;
63 
64 		/* Compare the name we search with the current mtd_id */
65 		if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
66 			strncpy(altname, dev_id, dev_id_len);
67 			altname[dev_id_len] = 0;
68 
69 			return 0;
70 		}
71 
72 		/* Go to the next tupple */
73 		mtdids = comma + 1;
74 	} while (comma);
75 
76 	return -EINVAL;
77 }
78 
79 #if IS_ENABLED(CONFIG_MTD)
80 static void mtd_probe_uclass_mtd_devs(void)
81 {
82 	struct udevice *dev;
83 	int idx = 0;
84 
85 	/* Probe devices with DM compliant drivers */
86 	while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
87 		mtd_probe(dev);
88 		idx++;
89 	}
90 }
91 #else
92 static void mtd_probe_uclass_mtd_devs(void) { }
93 #endif
94 
95 #if defined(CONFIG_MTD_PARTITIONS)
96 extern void board_mtdparts_default(const char **mtdids,
97 				   const char **mtdparts);
98 
99 static const char *get_mtdids(void)
100 {
101 	__maybe_unused const char *mtdparts = NULL;
102 	const char *mtdids = env_get("mtdids");
103 
104 	if (mtdids)
105 		return mtdids;
106 
107 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
108 	board_mtdparts_default(&mtdids, &mtdparts);
109 #elif defined(MTDIDS_DEFAULT)
110 	mtdids = MTDIDS_DEFAULT;
111 #elif defined(CONFIG_MTDIDS_DEFAULT)
112 	mtdids = CONFIG_MTDIDS_DEFAULT;
113 #endif
114 
115 	if (mtdids)
116 		env_set("mtdids", mtdids);
117 
118 	return mtdids;
119 }
120 
121 #define MTDPARTS_MAXLEN         512
122 
123 static const char *get_mtdparts(void)
124 {
125 	__maybe_unused const char *mtdids = NULL;
126 	static char tmp_parts[MTDPARTS_MAXLEN];
127 	static bool use_defaults = true;
128 	const char *mtdparts = NULL;
129 
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;
134 
135 	if (mtdparts || !use_defaults)
136 		return mtdparts;
137 
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;
144 #endif
145 
146 	if (mtdparts)
147 		env_set("mtdparts", mtdparts);
148 
149 	use_defaults = false;
150 
151 	return mtdparts;
152 }
153 
154 int mtd_probe_devices(void)
155 {
156 	static char *old_mtdparts;
157 	static char *old_mtdids;
158 	const char *mtdparts = get_mtdparts();
159 	const char *mtdids = get_mtdids();
160 	bool remaining_partitions = true;
161 	struct mtd_info *mtd;
162 
163 	mtd_probe_uclass_mtd_devs();
164 
165 	/* Check if mtdparts/mtdids changed since last call, otherwise: exit */
166 	if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
167 	    (mtdparts && old_mtdparts && mtdids && old_mtdids &&
168 	     !strcmp(mtdparts, old_mtdparts) &&
169 	     !strcmp(mtdids, old_mtdids)))
170 		return 0;
171 
172 	/* Update the local copy of mtdparts */
173 	free(old_mtdparts);
174 	free(old_mtdids);
175 	old_mtdparts = strdup(mtdparts);
176 	old_mtdids = strdup(mtdids);
177 
178 	/* If at least one partition is still in use, do not delete anything */
179 	mtd_for_each_device(mtd) {
180 		if (mtd->usecount) {
181 			printf("Partition \"%s\" already in use, aborting\n",
182 			       mtd->name);
183 			return -EACCES;
184 		}
185 	}
186 
187 	/*
188 	 * Everything looks clear, remove all partitions. It is not safe to
189 	 * remove entries from the mtd_for_each_device loop as it uses idr
190 	 * indexes and the partitions removal is done in bulk (all partitions of
191 	 * one device at the same time), so break and iterate from start each
192 	 * time a new partition is found and deleted.
193 	 */
194 	while (remaining_partitions) {
195 		remaining_partitions = false;
196 		mtd_for_each_device(mtd) {
197 			if (!mtd_is_partition(mtd) && mtd_has_partitions(mtd)) {
198 				del_mtd_partitions(mtd);
199 				remaining_partitions = true;
200 				break;
201 			}
202 		}
203 	}
204 
205 	/* If either mtdparts or mtdids is empty, then exit */
206 	if (!mtdparts || !mtdids)
207 		return 0;
208 
209 	/* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
210 	if (strstr(mtdparts, "mtdparts="))
211 		mtdparts += 9;
212 
213 	/* For each MTD device in mtdparts */
214 	while (mtdparts[0] != '\0') {
215 		char mtd_name[MTD_NAME_MAX_LEN], *colon;
216 		struct mtd_partition *parts;
217 		int mtd_name_len, nparts;
218 		int ret;
219 
220 		colon = strchr(mtdparts, ':');
221 		if (!colon) {
222 			printf("Wrong mtdparts: %s\n", mtdparts);
223 			return -EINVAL;
224 		}
225 
226 		mtd_name_len = colon - mtdparts;
227 		strncpy(mtd_name, mtdparts, mtd_name_len);
228 		mtd_name[mtd_name_len] = '\0';
229 		/* Move the pointer forward (including the ':') */
230 		mtdparts += mtd_name_len + 1;
231 		mtd = get_mtd_device_nm(mtd_name);
232 		if (IS_ERR_OR_NULL(mtd)) {
233 			char linux_name[MTD_NAME_MAX_LEN];
234 
235 			/*
236 			 * The MTD device named "mtd_name" does not exist. Try
237 			 * to find a correspondance with an MTD device having
238 			 * the same type and number as defined in the mtdids.
239 			 */
240 			debug("No device named %s\n", mtd_name);
241 			ret = mtd_search_alternate_name(mtd_name, linux_name,
242 							MTD_NAME_MAX_LEN);
243 			if (!ret)
244 				mtd = get_mtd_device_nm(linux_name);
245 
246 			/*
247 			 * If no device could be found, move the mtdparts
248 			 * pointer forward until the next set of partitions.
249 			 */
250 			if (ret || IS_ERR_OR_NULL(mtd)) {
251 				printf("Could not find a valid device for %s\n",
252 				       mtd_name);
253 				mtdparts = strchr(mtdparts, ';');
254 				if (mtdparts)
255 					mtdparts++;
256 
257 				continue;
258 			}
259 		}
260 
261 		/*
262 		 * Parse the MTD device partitions. It will update the mtdparts
263 		 * pointer, create an array of parts (that must be freed), and
264 		 * return the number of partition structures in the array.
265 		 */
266 		ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
267 		if (ret) {
268 			printf("Could not parse device %s\n", mtd->name);
269 			put_mtd_device(mtd);
270 			return -EINVAL;
271 		}
272 
273 		if (!nparts)
274 			continue;
275 
276 		/* Create the new MTD partitions */
277 		add_mtd_partitions(mtd, parts, nparts);
278 
279 		/* Free the structures allocated during the parsing */
280 		mtd_free_parsed_partitions(parts, nparts);
281 
282 		put_mtd_device(mtd);
283 	}
284 
285 	return 0;
286 }
287 #else
288 int mtd_probe_devices(void)
289 {
290 	mtd_probe_uclass_mtd_devs();
291 
292 	return 0;
293 }
294 #endif /* defined(CONFIG_MTD_PARTITIONS) */
295 
296 /* Legacy */
297 
298 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
299 		loff_t *maxsize, int devtype)
300 {
301 #ifdef CONFIG_CMD_MTDPARTS
302 	struct mtd_device *dev;
303 	struct part_info *part;
304 	u8 pnum;
305 	int ret;
306 
307 	ret = mtdparts_init();
308 	if (ret)
309 		return ret;
310 
311 	ret = find_dev_and_part(partname, &dev, &pnum, &part);
312 	if (ret)
313 		return ret;
314 
315 	if (dev->id->type != devtype) {
316 		printf("not same typ %d != %d\n", dev->id->type, devtype);
317 		return -1;
318 	}
319 
320 	*off = part->offset;
321 	*size = part->size;
322 	*maxsize = part->size;
323 	*idx = dev->id->num;
324 
325 	return 0;
326 #else
327 	puts("mtdparts support missing.\n");
328 	return -1;
329 #endif
330 }
331 
332 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
333 		loff_t *maxsize, int devtype, uint64_t chipsize)
334 {
335 	if (!str2off(arg, off))
336 		return get_part(arg, idx, off, size, maxsize, devtype);
337 
338 	if (*off >= chipsize) {
339 		puts("Offset exceeds device limit\n");
340 		return -1;
341 	}
342 
343 	*maxsize = chipsize - *off;
344 	*size = *maxsize;
345 	return 0;
346 }
347 
348 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
349 		     loff_t *size, loff_t *maxsize, int devtype,
350 		     uint64_t chipsize)
351 {
352 	int ret;
353 
354 	if (argc == 0) {
355 		*off = 0;
356 		*size = chipsize;
357 		*maxsize = *size;
358 		goto print;
359 	}
360 
361 	ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
362 			  chipsize);
363 	if (ret)
364 		return ret;
365 
366 	if (argc == 1)
367 		goto print;
368 
369 	if (!str2off(argv[1], size)) {
370 		printf("'%s' is not a number\n", argv[1]);
371 		return -1;
372 	}
373 
374 	if (*size > *maxsize) {
375 		puts("Size exceeds partition or device limit\n");
376 		return -1;
377 	}
378 
379 print:
380 	printf("device %d ", *idx);
381 	if (*size == chipsize)
382 		puts("whole chip\n");
383 	else
384 		printf("offset 0x%llx, size 0x%llx\n",
385 		       (unsigned long long)*off, (unsigned long long)*size);
386 	return 0;
387 }
388