1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90#include <common.h>
91#include <command.h>
92#include <malloc.h>
93#include <jffs2/jffs2.h>
94#include <linux/list.h>
95#include <linux/ctype.h>
96#include <cramfs/cramfs_fs.h>
97
98#if defined(CONFIG_CMD_NAND)
99#ifdef CONFIG_NAND_LEGACY
100#include <linux/mtd/nand_legacy.h>
101#else
102#include <linux/mtd/nand.h>
103#include <nand.h>
104#endif
105#endif
106
107#if defined(CONFIG_CMD_ONENAND)
108#include <linux/mtd/mtd.h>
109#include <linux/mtd/onenand.h>
110#include <onenand_uboot.h>
111#endif
112
113
114#define DEBUG_JFFS
115#undef DEBUG_JFFS
116
117#ifdef DEBUG_JFFS
118# define DEBUGF(fmt, args...) printf(fmt ,##args)
119#else
120# define DEBUGF(fmt, args...)
121#endif
122
123
124#define SIZE_REMAINING 0xFFFFFFFF
125
126
127
128
129
130#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
131
132
133#define MIN_PART_SIZE 4096
134
135
136
137#define MTD_WRITEABLE_CMD 1
138
139
140#ifdef CONFIG_CMD_MTDPARTS
141
142extern struct mtd_device *current_mtd_dev;
143extern u8 current_mtd_partnum;
144#else
145
146struct mtd_device *current_mtd_dev = NULL;
147u8 current_mtd_partnum = 0;
148#endif
149
150#if defined(CONFIG_CMD_CRAMFS)
151extern int cramfs_check (struct part_info *info);
152extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
153extern int cramfs_ls (struct part_info *info, char *filename);
154extern int cramfs_info (struct part_info *info);
155#else
156
157
158#define cramfs_check(x) (0)
159#define cramfs_load(x,y,z) (-1)
160#define cramfs_ls(x,y) (0)
161#define cramfs_info(x) (0)
162#endif
163
164#ifndef CONFIG_CMD_MTDPARTS
165
166
167
168
169
170
171static int mtd_device_validate(u8 type, u8 num, u32 *size)
172{
173 if (type == MTD_DEV_TYPE_NOR) {
174#if defined(CONFIG_CMD_FLASH)
175 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
176 extern flash_info_t flash_info[];
177 *size = flash_info[num].size;
178
179 return 0;
180 }
181
182 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
183 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
184#else
185 printf("support for FLASH devices not present\n");
186#endif
187 } else if (type == MTD_DEV_TYPE_NAND) {
188#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
189 if (num < CONFIG_SYS_MAX_NAND_DEVICE) {
190#ifndef CONFIG_NAND_LEGACY
191 *size = nand_info[num].size;
192#else
193 extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
194 *size = nand_dev_desc[num].totlen;
195#endif
196 return 0;
197 }
198
199 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
200 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
201#else
202 printf("support for NAND devices not present\n");
203#endif
204 } else if (type == MTD_DEV_TYPE_ONENAND) {
205#if defined(CONFIG_CMD_ONENAND)
206 *size = onenand_mtd.size;
207 return 0;
208#else
209 printf("support for OneNAND devices not present\n");
210#endif
211 } else
212 printf("Unknown defice type %d\n", type);
213
214 return 1;
215}
216
217
218
219
220
221
222
223
224
225
226
227static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
228{
229 const char *p = id;
230
231 *dev_type = 0;
232 if (strncmp(p, "nand", 4) == 0) {
233 *dev_type = MTD_DEV_TYPE_NAND;
234 p += 4;
235 } else if (strncmp(p, "nor", 3) == 0) {
236 *dev_type = MTD_DEV_TYPE_NOR;
237 p += 3;
238 } else if (strncmp(p, "onenand", 7) == 0) {
239 *dev_type = MTD_DEV_TYPE_ONENAND;
240 p += 7;
241 } else {
242 printf("incorrect device type in %s\n", id);
243 return 1;
244 }
245
246 if (!isdigit(*p)) {
247 printf("incorrect device number in %s\n", id);
248 return 1;
249 }
250
251 *dev_num = simple_strtoul(p, (char **)&p, 0);
252 if (ret_id)
253 *ret_id = p;
254 return 0;
255}
256
257
258
259
260
261
262
263
264
265
266
267static inline u32 get_part_sector_size_nand(struct mtdids *id)
268{
269#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
270#if defined(CONFIG_NAND_LEGACY)
271 extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
272
273 return nand_dev_desc[id->num].erasesize;
274#else
275 nand_info_t *nand;
276
277 nand = &nand_info[id->num];
278
279 return nand->erasesize;
280#endif
281#else
282 BUG();
283 return 0;
284#endif
285}
286
287static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
288{
289#if defined(CONFIG_CMD_FLASH)
290 extern flash_info_t flash_info[];
291
292 u32 end_phys, start_phys, sector_size = 0, size = 0;
293 int i;
294 flash_info_t *flash;
295
296 flash = &flash_info[id->num];
297
298 start_phys = flash->start[0] + part->offset;
299 end_phys = start_phys + part->size;
300
301 for (i = 0; i < flash->sector_count; i++) {
302 if (flash->start[i] >= end_phys)
303 break;
304
305 if (flash->start[i] >= start_phys) {
306 if (i == flash->sector_count - 1) {
307 size = flash->start[0] + flash->size - flash->start[i];
308 } else {
309 size = flash->start[i+1] - flash->start[i];
310 }
311
312 if (sector_size < size)
313 sector_size = size;
314 }
315 }
316
317 return sector_size;
318#else
319 BUG();
320 return 0;
321#endif
322}
323
324static inline u32 get_part_sector_size_onenand(void)
325{
326#if defined(CONFIG_CMD_ONENAND)
327 struct mtd_info *mtd;
328
329 mtd = &onenand_mtd;
330
331 return mtd->erasesize;
332#else
333 BUG();
334 return 0;
335#endif
336}
337
338static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
339{
340 if (id->type == MTD_DEV_TYPE_NAND)
341 return get_part_sector_size_nand(id);
342 else if (id->type == MTD_DEV_TYPE_NOR)
343 return get_part_sector_size_nor(id, part);
344 else if (id->type == MTD_DEV_TYPE_ONENAND)
345 return get_part_sector_size_onenand();
346 else
347 DEBUGF("Error: Unknown device type.\n");
348
349 return 0;
350}
351
352
353
354
355
356
357
358
359
360
361int mtdparts_init(void)
362{
363 static int initialized = 0;
364 u32 size;
365 char *dev_name;
366
367 DEBUGF("\n---mtdparts_init---\n");
368 if (!initialized) {
369 struct mtdids *id;
370 struct part_info *part;
371
372 initialized = 1;
373 current_mtd_dev = (struct mtd_device *)
374 malloc(sizeof(struct mtd_device) +
375 sizeof(struct part_info) +
376 sizeof(struct mtdids));
377 if (!current_mtd_dev) {
378 printf("out of memory\n");
379 return 1;
380 }
381 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
382 sizeof(struct part_info) + sizeof(struct mtdids));
383
384 id = (struct mtdids *)(current_mtd_dev + 1);
385 part = (struct part_info *)(id + 1);
386
387
388 id->mtd_id = "single part";
389
390#if defined(CONFIG_JFFS2_DEV)
391 dev_name = CONFIG_JFFS2_DEV;
392#else
393 dev_name = "nor0";
394#endif
395
396 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
397 (mtd_device_validate(id->type, id->num, &size) != 0)) {
398 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
399 free(current_mtd_dev);
400 return 1;
401 }
402 id->size = size;
403 INIT_LIST_HEAD(&id->link);
404
405 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
406 id->type, id->num, id->size, id->mtd_id);
407
408
409 part->name = "static";
410 part->auto_name = 0;
411
412#if defined(CONFIG_JFFS2_PART_SIZE)
413 part->size = CONFIG_JFFS2_PART_SIZE;
414#else
415 part->size = SIZE_REMAINING;
416#endif
417
418#if defined(CONFIG_JFFS2_PART_OFFSET)
419 part->offset = CONFIG_JFFS2_PART_OFFSET;
420#else
421 part->offset = 0x00000000;
422#endif
423
424 part->sector_size = get_part_sector_size(id, part);
425
426 part->dev = current_mtd_dev;
427 INIT_LIST_HEAD(&part->link);
428
429
430 if (part->size == SIZE_REMAINING)
431 part->size = id->size - part->offset;
432
433 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
434 part->name, part->size, part->offset);
435
436
437 current_mtd_dev->id = id;
438 INIT_LIST_HEAD(¤t_mtd_dev->link);
439 current_mtd_dev->num_parts = 1;
440 INIT_LIST_HEAD(¤t_mtd_dev->parts);
441 list_add(&part->link, ¤t_mtd_dev->parts);
442 }
443
444 return 0;
445}
446#endif
447
448
449
450
451
452
453
454
455
456static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
457{
458 struct list_head *entry;
459 struct part_info *part;
460 int num;
461
462 if (!dev)
463 return NULL;
464
465 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
466 part_num, MTD_DEV_TYPE(dev->id->type),
467 dev->id->num, dev->id->mtd_id);
468
469 if (part_num >= dev->num_parts) {
470 printf("invalid partition number %d for device %s%d (%s)\n",
471 part_num, MTD_DEV_TYPE(dev->id->type),
472 dev->id->num, dev->id->mtd_id);
473 return NULL;
474 }
475
476
477 num = 0;
478 list_for_each(entry, &dev->parts) {
479 part = list_entry(entry, struct part_info, link);
480
481 if (part_num == num++) {
482 return part;
483 }
484 }
485
486 return NULL;
487}
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
504{
505 char *fsname;
506 char *filename;
507 int size;
508 struct part_info *part;
509 ulong offset = load_addr;
510
511
512 if ((filename = getenv("bootfile")) == NULL) {
513 filename = "uImage";
514 }
515
516 if (argc == 2) {
517 filename = argv[1];
518 }
519 if (argc == 3) {
520 offset = simple_strtoul(argv[1], NULL, 16);
521 load_addr = offset;
522 filename = argv[2];
523 }
524
525
526 if (mtdparts_init() !=0)
527 return 1;
528
529 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
530
531
532 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
533 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
534
535 if (cramfs_check(part)) {
536 size = cramfs_load ((char *) offset, part, filename);
537 } else {
538
539 size = jffs2_1pass_load((char *)offset, part, filename);
540 }
541
542 if (size > 0) {
543 char buf[10];
544 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
545 fsname, size, offset);
546 sprintf(buf, "%x", size);
547 setenv("filesize", buf);
548 } else {
549 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
550 }
551
552 return !(size > 0);
553 }
554 return 1;
555}
556
557
558
559
560
561
562
563
564
565
566
567int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
568{
569 char *filename = "/";
570 int ret;
571 struct part_info *part;
572
573 if (argc == 2)
574 filename = argv[1];
575
576
577 if (mtdparts_init() !=0)
578 return 1;
579
580 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
581
582
583 if (cramfs_check(part)) {
584 ret = cramfs_ls (part, filename);
585 } else {
586
587 ret = jffs2_1pass_ls(part, filename);
588 }
589
590 return ret ? 0 : 1;
591 }
592 return 1;
593}
594
595
596
597
598
599
600
601
602
603
604
605int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
606{
607 struct part_info *part;
608 char *fsname;
609 int ret;
610
611
612 if (mtdparts_init() !=0)
613 return 1;
614
615 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
616
617
618 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
619 printf("### filesystem type is %s\n", fsname);
620
621 if (cramfs_check(part)) {
622 ret = cramfs_info (part);
623 } else {
624
625 ret = jffs2_1pass_info(part);
626 }
627
628 return ret ? 0 : 1;
629 }
630 return 1;
631}
632
633
634U_BOOT_CMD(
635 fsload, 3, 0, do_jffs2_fsload,
636 "load binary file from a filesystem image",
637 "[ off ] [ filename ]\n"
638 " - load binary file from flash bank\n"
639 " with offset 'off'\n"
640);
641U_BOOT_CMD(
642 ls, 2, 1, do_jffs2_ls,
643 "list files in a directory (default /)",
644 "[ directory ]\n"
645 " - list files in a directory.\n"
646);
647
648U_BOOT_CMD(
649 fsinfo, 1, 1, do_jffs2_fsinfo,
650 "print information about filesystems",
651 " - print information about filesystems\n"
652);
653
654