xref: /rk3399_rockchip-uboot/cmd/jffs2.c (revision 8c7c24c01a6f59f93e6564743e0e75c973d73387)
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2002
6  * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
7  *
8  * (C) Copyright 2003
9  * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
10  *
11  * (C) Copyright 2005
12  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13  *
14  *   Added support for reading flash partition table from environment.
15  *   Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
16  *   kernel tree.
17  *
18  *   $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
19  *   Copyright 2002 SYSGO Real-Time Solutions GmbH
20  *
21  * SPDX-License-Identifier:	GPL-2.0+
22  */
23 
24 /*
25  * Three environment variables are used by the parsing routines:
26  *
27  * 'partition' - keeps current partition identifier
28  *
29  * partition  := <part-id>
30  * <part-id>  := <dev-id>,part_num
31  *
32  *
33  * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
34  *
35  * mtdids=<idmap>[,<idmap>,...]
36  *
37  * <idmap>    := <dev-id>=<mtd-id>
38  * <dev-id>   := 'nand'|'nor'|'onenand'<dev-num>
39  * <dev-num>  := mtd device number, 0...
40  * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
41  *
42  *
43  * 'mtdparts' - partition list
44  *
45  * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
46  *
47  * <mtd-def>  := <mtd-id>:<part-def>[,<part-def>...]
48  * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
49  * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
50  * <size>     := standard linux memsize OR '-' to denote all remaining space
51  * <offset>   := partition start offset within the device
52  * <name>     := '(' NAME ')'
53  * <ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)
54  *
55  * Notes:
56  * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
57  * - if the above variables are not set defaults for a given target are used
58  *
59  * Examples:
60  *
61  * 1 NOR Flash, with 1 single writable partition:
62  * mtdids=nor0=edb7312-nor
63  * mtdparts=mtdparts=edb7312-nor:-
64  *
65  * 1 NOR Flash with 2 partitions, 1 NAND with one
66  * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
67  * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
68  *
69  */
70 
71 /*
72  * JFFS2/CRAMFS support
73  */
74 #include <common.h>
75 #include <command.h>
76 #include <malloc.h>
77 #include <jffs2/jffs2.h>
78 #include <linux/list.h>
79 #include <linux/ctype.h>
80 #include <cramfs/cramfs_fs.h>
81 
82 #if defined(CONFIG_CMD_NAND)
83 #include <linux/mtd/rawnand.h>
84 #include <nand.h>
85 #endif
86 
87 #if defined(CONFIG_CMD_ONENAND)
88 #include <linux/mtd/mtd.h>
89 #include <linux/mtd/onenand.h>
90 #include <onenand_uboot.h>
91 #endif
92 
93 /* enable/disable debugging messages */
94 #define	DEBUG_JFFS
95 #undef	DEBUG_JFFS
96 
97 #ifdef  DEBUG_JFFS
98 # define DEBUGF(fmt, args...)	printf(fmt ,##args)
99 #else
100 # define DEBUGF(fmt, args...)
101 #endif
102 
103 /* special size referring to all the remaining space in a partition */
104 #define SIZE_REMAINING		0xFFFFFFFF
105 
106 /* special offset value, it is used when not provided by user
107  *
108  * this value is used temporarily during parsing, later such offests
109  * are recalculated */
110 #define OFFSET_NOT_SPECIFIED	0xFFFFFFFF
111 
112 /* minimum partition size */
113 #define MIN_PART_SIZE		4096
114 
115 /* this flag needs to be set in part_info struct mask_flags
116  * field for read-only partitions */
117 #define MTD_WRITEABLE_CMD		1
118 
119 /* current active device and partition number */
120 #ifdef CONFIG_CMD_MTDPARTS
121 /* Use the ones declared in cmd_mtdparts.c */
122 extern struct mtd_device *current_mtd_dev;
123 extern u8 current_mtd_partnum;
124 #else
125 /* Use local ones */
126 struct mtd_device *current_mtd_dev = NULL;
127 u8 current_mtd_partnum = 0;
128 #endif
129 
130 #if defined(CONFIG_CMD_CRAMFS)
131 extern int cramfs_check (struct part_info *info);
132 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
133 extern int cramfs_ls (struct part_info *info, char *filename);
134 extern int cramfs_info (struct part_info *info);
135 #else
136 /* defining empty macros for function names is ugly but avoids ifdef clutter
137  * all over the code */
138 #define cramfs_check(x)		(0)
139 #define cramfs_load(x,y,z)	(-1)
140 #define cramfs_ls(x,y)		(0)
141 #define cramfs_info(x)		(0)
142 #endif
143 
144 #ifndef CONFIG_CMD_MTDPARTS
145 /**
146  * Check device number to be within valid range for given device type.
147  *
148  * @param dev device to validate
149  * @return 0 if device is valid, 1 otherwise
150  */
151 static int mtd_device_validate(u8 type, u8 num, u32 *size)
152 {
153 	if (type == MTD_DEV_TYPE_NOR) {
154 #if defined(CONFIG_CMD_FLASH)
155 		if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
156 			extern flash_info_t flash_info[];
157 			*size = flash_info[num].size;
158 
159 			return 0;
160 		}
161 
162 		printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
163 				MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
164 #else
165 		struct mtd_info *mtd = get_mtd_device_nm(CONFIG_JFFS2_DEV);
166 		if (mtd) {
167 			*size = mtd->size;
168 			return 0;
169 		}
170 
171 		printf("no such Nor device: %s\n", CONFIG_JFFS2_DEV);
172 #endif
173 	} else if (type == MTD_DEV_TYPE_NAND) {
174 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
175 		struct mtd_info *mtd = get_nand_dev_by_index(num);
176 		if (mtd) {
177 			*size = mtd->size;
178 			return 0;
179 		}
180 
181 		printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
182 				MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
183 #else
184 		printf("support for NAND devices not present\n");
185 #endif
186 	} else if (type == MTD_DEV_TYPE_ONENAND) {
187 #if defined(CONFIG_CMD_ONENAND)
188 		*size = onenand_mtd.size;
189 		return 0;
190 #else
191 		printf("support for OneNAND devices not present\n");
192 #endif
193 	} else
194 		printf("Unknown defice type %d\n", type);
195 
196 	return 1;
197 }
198 
199 /**
200  * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
201  * return device type and number.
202  *
203  * @param id string describing device id
204  * @param ret_id output pointer to next char after parse completes (output)
205  * @param dev_type parsed device type (output)
206  * @param dev_num parsed device number (output)
207  * @return 0 on success, 1 otherwise
208  */
209 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
210 {
211 	const char *p = id;
212 
213 	*dev_type = 0;
214 	if (strncmp(p, "nand", 4) == 0) {
215 		*dev_type = MTD_DEV_TYPE_NAND;
216 		p += 4;
217 	} else if (strncmp(p, "nor", 3) == 0) {
218 		*dev_type = MTD_DEV_TYPE_NOR;
219 		p += 3;
220 	} else if (strncmp(p, "onenand", 7) == 0) {
221 		*dev_type = MTD_DEV_TYPE_ONENAND;
222 		p += 7;
223 	} else {
224 		printf("incorrect device type in %s\n", id);
225 		return 1;
226 	}
227 
228 	if (!isdigit(*p)) {
229 		printf("incorrect device number in %s\n", id);
230 		return 1;
231 	}
232 
233 	*dev_num = simple_strtoul(p, (char **)&p, 0);
234 	if (ret_id)
235 		*ret_id = p;
236 	return 0;
237 }
238 
239 /*
240  * 'Static' version of command line mtdparts_init() routine. Single partition on
241  * a single device configuration.
242  */
243 
244 /**
245  * Calculate sector size.
246  *
247  * @return sector size
248  */
249 static inline u32 get_part_sector_size_nand(struct mtdids *id)
250 {
251 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
252 	struct mtd_info *mtd;
253 
254 	mtd = get_nand_dev_by_index(id->num);
255 
256 	return mtd->erasesize;
257 #else
258 	BUG();
259 	return 0;
260 #endif
261 }
262 
263 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
264 {
265 #if defined(CONFIG_CMD_FLASH)
266 	extern flash_info_t flash_info[];
267 
268 	u32 end_phys, start_phys, sector_size = 0, size = 0;
269 	int i;
270 	flash_info_t *flash;
271 
272 	flash = &flash_info[id->num];
273 
274 	start_phys = flash->start[0] + part->offset;
275 	end_phys = start_phys + part->size - 1;
276 
277 	for (i = 0; i < flash->sector_count; i++) {
278 		if (flash->start[i] >= end_phys)
279 			break;
280 
281 		if (flash->start[i] >= start_phys) {
282 			if (i == flash->sector_count - 1) {
283 				size = flash->start[0] + flash->size - flash->start[i];
284 			} else {
285 				size = flash->start[i+1] - flash->start[i];
286 			}
287 
288 			if (sector_size < size)
289 				sector_size = size;
290 		}
291 	}
292 
293 	return sector_size;
294 #else
295 	struct mtd_info *mtd;
296 
297 	mtd = get_mtd_device_nm(CONFIG_JFFS2_DEV);
298 
299 	return mtd->erasesize;
300 #endif
301 }
302 
303 static inline u32 get_part_sector_size_onenand(void)
304 {
305 #if defined(CONFIG_CMD_ONENAND)
306 	struct mtd_info *mtd;
307 
308 	mtd = &onenand_mtd;
309 
310 	return mtd->erasesize;
311 #else
312 	BUG();
313 	return 0;
314 #endif
315 }
316 
317 static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
318 {
319 	if (id->type == MTD_DEV_TYPE_NAND)
320 		return get_part_sector_size_nand(id);
321 	else if (id->type == MTD_DEV_TYPE_NOR)
322 		return get_part_sector_size_nor(id, part);
323 	else if (id->type == MTD_DEV_TYPE_ONENAND)
324 		return get_part_sector_size_onenand();
325 	else
326 		DEBUGF("Error: Unknown device type.\n");
327 
328 	return 0;
329 }
330 
331 /**
332  * Parse and initialize global mtdids mapping and create global
333  * device/partition list.
334  *
335  * 'Static' version of command line mtdparts_init() routine. Single partition on
336  * a single device configuration.
337  *
338  * @return 0 on success, 1 otherwise
339  */
340 int mtdparts_init(void)
341 {
342 	static int initialized = 0;
343 	u32 size;
344 	char *dev_name;
345 
346 	DEBUGF("\n---mtdparts_init---\n");
347 	if (!initialized) {
348 		struct mtdids *id;
349 		struct part_info *part;
350 
351 		initialized = 1;
352 		current_mtd_dev = (struct mtd_device *)
353 			malloc(sizeof(struct mtd_device) +
354 					sizeof(struct part_info) +
355 					sizeof(struct mtdids));
356 		if (!current_mtd_dev) {
357 			printf("out of memory\n");
358 			return 1;
359 		}
360 		memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
361 		       sizeof(struct part_info) + sizeof(struct mtdids));
362 
363 		id = (struct mtdids *)(current_mtd_dev + 1);
364 		part = (struct part_info *)(id + 1);
365 
366 		/* id */
367 		id->mtd_id = "single part";
368 
369 #if defined(CONFIG_JFFS2_DEV)
370 		dev_name = CONFIG_JFFS2_DEV;
371 #else
372 		dev_name = "nor0";
373 #endif
374 
375 		if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
376 				(mtd_device_validate(id->type, id->num, &size) != 0)) {
377 			printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
378 			free(current_mtd_dev);
379 			return 1;
380 		}
381 		id->size = size;
382 		INIT_LIST_HEAD(&id->link);
383 
384 		DEBUGF("dev id: type = %d, num = %d, size = 0x%08llx, mtd_id = %s\n",
385 				id->type, id->num, id->size, id->mtd_id);
386 
387 		/* partition */
388 		part->name = "static";
389 		part->auto_name = 0;
390 
391 #if defined(CONFIG_JFFS2_PART_SIZE)
392 		part->size = CONFIG_JFFS2_PART_SIZE;
393 #else
394 		part->size = SIZE_REMAINING;
395 #endif
396 
397 #if defined(CONFIG_JFFS2_PART_OFFSET)
398 		part->offset = CONFIG_JFFS2_PART_OFFSET;
399 #else
400 		part->offset = 0x00000000;
401 #endif
402 
403 		part->dev = current_mtd_dev;
404 		INIT_LIST_HEAD(&part->link);
405 
406 		/* recalculate size if needed */
407 		if (part->size == SIZE_REMAINING)
408 			part->size = id->size - part->offset;
409 
410 		part->sector_size = get_part_sector_size(id, part);
411 
412 		DEBUGF("part  : name = %s, size = 0x%08llx, offset = 0x%08llx\n",
413 				part->name, part->size, part->offset);
414 
415 		/* device */
416 		current_mtd_dev->id = id;
417 		INIT_LIST_HEAD(&current_mtd_dev->link);
418 		current_mtd_dev->num_parts = 1;
419 		INIT_LIST_HEAD(&current_mtd_dev->parts);
420 		list_add(&part->link, &current_mtd_dev->parts);
421 	}
422 
423 	return 0;
424 }
425 #endif /* #ifndef CONFIG_CMD_MTDPARTS */
426 
427 /**
428  * Return pointer to the partition of a requested number from a requested
429  * device.
430  *
431  * @param dev device that is to be searched for a partition
432  * @param part_num requested partition number
433  * @return pointer to the part_info, NULL otherwise
434  */
435 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
436 {
437 	struct list_head *entry;
438 	struct part_info *part;
439 	int num;
440 
441 	if (!dev)
442 		return NULL;
443 
444 	DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
445 			part_num, MTD_DEV_TYPE(dev->id->type),
446 			dev->id->num, dev->id->mtd_id);
447 
448 	if (part_num >= dev->num_parts) {
449 		printf("invalid partition number %d for device %s%d (%s)\n",
450 				part_num, MTD_DEV_TYPE(dev->id->type),
451 				dev->id->num, dev->id->mtd_id);
452 		return NULL;
453 	}
454 
455 	/* locate partition number, return it */
456 	num = 0;
457 	list_for_each(entry, &dev->parts) {
458 		part = list_entry(entry, struct part_info, link);
459 
460 		if (part_num == num++) {
461 			return part;
462 		}
463 	}
464 
465 	return NULL;
466 }
467 
468 /***************************************************/
469 /* U-Boot commands				   */
470 /***************************************************/
471 
472 /**
473  * Routine implementing fsload u-boot command. This routine tries to load
474  * a requested file from jffs2/cramfs filesystem on a current partition.
475  *
476  * @param cmdtp command internal data
477  * @param flag command flag
478  * @param argc number of arguments supplied to the command
479  * @param argv arguments list
480  * @return 0 on success, 1 otherwise
481  */
482 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
483 {
484 	char *fsname;
485 	char *filename;
486 	int size;
487 	struct part_info *part;
488 	ulong offset = load_addr;
489 
490 	/* pre-set Boot file name */
491 	filename = env_get("bootfile");
492 	if (!filename)
493 		filename = "uImage";
494 
495 	if (argc == 2) {
496 		filename = argv[1];
497 	}
498 	if (argc == 3) {
499 		offset = simple_strtoul(argv[1], NULL, 16);
500 		load_addr = offset;
501 		filename = argv[2];
502 	}
503 
504 	/* make sure we are in sync with env variables */
505 	if (mtdparts_init() !=0)
506 		return 1;
507 
508 	if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
509 
510 		/* check partition type for cramfs */
511 		fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
512 		printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
513 
514 		if (cramfs_check(part)) {
515 			size = cramfs_load ((char *) offset, part, filename);
516 		} else {
517 			/* if this is not cramfs assume jffs2 */
518 			size = jffs2_1pass_load((char *)offset, part, filename);
519 		}
520 
521 		if (size > 0) {
522 			printf("### %s load complete: %d bytes loaded to 0x%lx\n",
523 				fsname, size, offset);
524 			env_set_hex("filesize", size);
525 		} else {
526 			printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
527 		}
528 
529 		return !(size > 0);
530 	}
531 	return 1;
532 }
533 
534 /**
535  * Routine implementing u-boot ls command which lists content of a given
536  * directory on a current partition.
537  *
538  * @param cmdtp command internal data
539  * @param flag command flag
540  * @param argc number of arguments supplied to the command
541  * @param argv arguments list
542  * @return 0 on success, 1 otherwise
543  */
544 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
545 {
546 	char *filename = "/";
547 	int ret;
548 	struct part_info *part;
549 
550 	if (argc == 2)
551 		filename = argv[1];
552 
553 	/* make sure we are in sync with env variables */
554 	if (mtdparts_init() !=0)
555 		return 1;
556 
557 	if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
558 
559 		/* check partition type for cramfs */
560 		if (cramfs_check(part)) {
561 			ret = cramfs_ls (part, filename);
562 		} else {
563 			/* if this is not cramfs assume jffs2 */
564 			ret = jffs2_1pass_ls(part, filename);
565 		}
566 
567 		return ret ? 0 : 1;
568 	}
569 	return 1;
570 }
571 
572 /**
573  * Routine implementing u-boot fsinfo command. This routine prints out
574  * miscellaneous filesystem informations/statistics.
575  *
576  * @param cmdtp command internal data
577  * @param flag command flag
578  * @param argc number of arguments supplied to the command
579  * @param argv arguments list
580  * @return 0 on success, 1 otherwise
581  */
582 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
583 {
584 	struct part_info *part;
585 	char *fsname;
586 	int ret;
587 
588 	/* make sure we are in sync with env variables */
589 	if (mtdparts_init() !=0)
590 		return 1;
591 
592 	if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
593 
594 		/* check partition type for cramfs */
595 		fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
596 		printf("### filesystem type is %s\n", fsname);
597 
598 		if (cramfs_check(part)) {
599 			ret = cramfs_info (part);
600 		} else {
601 			/* if this is not cramfs assume jffs2 */
602 			ret = jffs2_1pass_info(part);
603 		}
604 
605 		return ret ? 0 : 1;
606 	}
607 	return 1;
608 }
609 
610 /***************************************************/
611 U_BOOT_CMD(
612 	fsload,	3,	0,	do_jffs2_fsload,
613 	"load binary file from a filesystem image",
614 	"[ off ] [ filename ]\n"
615 	"    - load binary file from flash bank\n"
616 	"      with offset 'off'"
617 );
618 U_BOOT_CMD(
619 	fsls,	2,	1,	do_jffs2_ls,
620 	"list files in a directory (default /)",
621 	"[ directory ]"
622 );
623 
624 U_BOOT_CMD(
625 	fsinfo,	1,	1,	do_jffs2_fsinfo,
626 	"print information about filesystems",
627 	""
628 );
629 /***************************************************/
630