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#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/nand.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
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
104#define SIZE_REMAINING 0xFFFFFFFF
105
106
107
108
109
110#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
111
112
113#define MIN_PART_SIZE 4096
114
115
116
117#define MTD_WRITEABLE_CMD 1
118
119
120#ifdef CONFIG_CMD_MTDPARTS
121
122extern struct mtd_device *current_mtd_dev;
123extern u8 current_mtd_partnum;
124#else
125
126struct mtd_device *current_mtd_dev = NULL;
127u8 current_mtd_partnum = 0;
128#endif
129
130#if defined(CONFIG_CMD_CRAMFS)
131extern int cramfs_check (struct part_info *info);
132extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
133extern int cramfs_ls (struct part_info *info, char *filename);
134extern int cramfs_info (struct part_info *info);
135#else
136
137
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
147
148
149
150
151static 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 printf("support for FLASH devices not present\n");
166#endif
167 } else if (type == MTD_DEV_TYPE_NAND) {
168#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
169 if (num < CONFIG_SYS_MAX_NAND_DEVICE) {
170 *size = nand_info[num]->size;
171 return 0;
172 }
173
174 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
175 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
176#else
177 printf("support for NAND devices not present\n");
178#endif
179 } else if (type == MTD_DEV_TYPE_ONENAND) {
180#if defined(CONFIG_CMD_ONENAND)
181 *size = onenand_mtd.size;
182 return 0;
183#else
184 printf("support for OneNAND devices not present\n");
185#endif
186 } else
187 printf("Unknown defice type %d\n", type);
188
189 return 1;
190}
191
192
193
194
195
196
197
198
199
200
201
202static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
203{
204 const char *p = id;
205
206 *dev_type = 0;
207 if (strncmp(p, "nand", 4) == 0) {
208 *dev_type = MTD_DEV_TYPE_NAND;
209 p += 4;
210 } else if (strncmp(p, "nor", 3) == 0) {
211 *dev_type = MTD_DEV_TYPE_NOR;
212 p += 3;
213 } else if (strncmp(p, "onenand", 7) == 0) {
214 *dev_type = MTD_DEV_TYPE_ONENAND;
215 p += 7;
216 } else {
217 printf("incorrect device type in %s\n", id);
218 return 1;
219 }
220
221 if (!isdigit(*p)) {
222 printf("incorrect device number in %s\n", id);
223 return 1;
224 }
225
226 *dev_num = simple_strtoul(p, (char **)&p, 0);
227 if (ret_id)
228 *ret_id = p;
229 return 0;
230}
231
232
233
234
235
236
237
238
239
240
241
242static inline u32 get_part_sector_size_nand(struct mtdids *id)
243{
244#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
245 struct mtd_info *mtd;
246
247 mtd = nand_info[id->num];
248
249 return mtd->erasesize;
250#else
251 BUG();
252 return 0;
253#endif
254}
255
256static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
257{
258#if defined(CONFIG_CMD_FLASH)
259 extern flash_info_t flash_info[];
260
261 u32 end_phys, start_phys, sector_size = 0, size = 0;
262 int i;
263 flash_info_t *flash;
264
265 flash = &flash_info[id->num];
266
267 start_phys = flash->start[0] + part->offset;
268 end_phys = start_phys + part->size - 1;
269
270 for (i = 0; i < flash->sector_count; i++) {
271 if (flash->start[i] >= end_phys)
272 break;
273
274 if (flash->start[i] >= start_phys) {
275 if (i == flash->sector_count - 1) {
276 size = flash->start[0] + flash->size - flash->start[i];
277 } else {
278 size = flash->start[i+1] - flash->start[i];
279 }
280
281 if (sector_size < size)
282 sector_size = size;
283 }
284 }
285
286 return sector_size;
287#else
288 BUG();
289 return 0;
290#endif
291}
292
293static inline u32 get_part_sector_size_onenand(void)
294{
295#if defined(CONFIG_CMD_ONENAND)
296 struct mtd_info *mtd;
297
298 mtd = &onenand_mtd;
299
300 return mtd->erasesize;
301#else
302 BUG();
303 return 0;
304#endif
305}
306
307static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
308{
309 if (id->type == MTD_DEV_TYPE_NAND)
310 return get_part_sector_size_nand(id);
311 else if (id->type == MTD_DEV_TYPE_NOR)
312 return get_part_sector_size_nor(id, part);
313 else if (id->type == MTD_DEV_TYPE_ONENAND)
314 return get_part_sector_size_onenand();
315 else
316 DEBUGF("Error: Unknown device type.\n");
317
318 return 0;
319}
320
321
322
323
324
325
326
327
328
329
330int mtdparts_init(void)
331{
332 static int initialized = 0;
333 u32 size;
334 char *dev_name;
335
336 DEBUGF("\n---mtdparts_init---\n");
337 if (!initialized) {
338 struct mtdids *id;
339 struct part_info *part;
340
341 initialized = 1;
342 current_mtd_dev = (struct mtd_device *)
343 malloc(sizeof(struct mtd_device) +
344 sizeof(struct part_info) +
345 sizeof(struct mtdids));
346 if (!current_mtd_dev) {
347 printf("out of memory\n");
348 return 1;
349 }
350 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
351 sizeof(struct part_info) + sizeof(struct mtdids));
352
353 id = (struct mtdids *)(current_mtd_dev + 1);
354 part = (struct part_info *)(id + 1);
355
356
357 id->mtd_id = "single part";
358
359#if defined(CONFIG_JFFS2_DEV)
360 dev_name = CONFIG_JFFS2_DEV;
361#else
362 dev_name = "nor0";
363#endif
364
365 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
366 (mtd_device_validate(id->type, id->num, &size) != 0)) {
367 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
368 free(current_mtd_dev);
369 return 1;
370 }
371 id->size = size;
372 INIT_LIST_HEAD(&id->link);
373
374 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
375 id->type, id->num, id->size, id->mtd_id);
376
377
378 part->name = "static";
379 part->auto_name = 0;
380
381#if defined(CONFIG_JFFS2_PART_SIZE)
382 part->size = CONFIG_JFFS2_PART_SIZE;
383#else
384 part->size = SIZE_REMAINING;
385#endif
386
387#if defined(CONFIG_JFFS2_PART_OFFSET)
388 part->offset = CONFIG_JFFS2_PART_OFFSET;
389#else
390 part->offset = 0x00000000;
391#endif
392
393 part->dev = current_mtd_dev;
394 INIT_LIST_HEAD(&part->link);
395
396
397 if (part->size == SIZE_REMAINING)
398 part->size = id->size - part->offset;
399
400 part->sector_size = get_part_sector_size(id, part);
401
402 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
403 part->name, part->size, part->offset);
404
405
406 current_mtd_dev->id = id;
407 INIT_LIST_HEAD(¤t_mtd_dev->link);
408 current_mtd_dev->num_parts = 1;
409 INIT_LIST_HEAD(¤t_mtd_dev->parts);
410 list_add(&part->link, ¤t_mtd_dev->parts);
411 }
412
413 return 0;
414}
415#endif
416
417
418
419
420
421
422
423
424
425static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
426{
427 struct list_head *entry;
428 struct part_info *part;
429 int num;
430
431 if (!dev)
432 return NULL;
433
434 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
435 part_num, MTD_DEV_TYPE(dev->id->type),
436 dev->id->num, dev->id->mtd_id);
437
438 if (part_num >= dev->num_parts) {
439 printf("invalid partition number %d for device %s%d (%s)\n",
440 part_num, MTD_DEV_TYPE(dev->id->type),
441 dev->id->num, dev->id->mtd_id);
442 return NULL;
443 }
444
445
446 num = 0;
447 list_for_each(entry, &dev->parts) {
448 part = list_entry(entry, struct part_info, link);
449
450 if (part_num == num++) {
451 return part;
452 }
453 }
454
455 return NULL;
456}
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
473{
474 char *fsname;
475 char *filename;
476 int size;
477 struct part_info *part;
478 ulong offset = load_addr;
479
480
481 if ((filename = getenv("bootfile")) == NULL) {
482 filename = "uImage";
483 }
484
485 if (argc == 2) {
486 filename = argv[1];
487 }
488 if (argc == 3) {
489 offset = simple_strtoul(argv[1], NULL, 16);
490 load_addr = offset;
491 filename = argv[2];
492 }
493
494
495 if (mtdparts_init() !=0)
496 return 1;
497
498 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
499
500
501 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
502 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
503
504 if (cramfs_check(part)) {
505 size = cramfs_load ((char *) offset, part, filename);
506 } else {
507
508 size = jffs2_1pass_load((char *)offset, part, filename);
509 }
510
511 if (size > 0) {
512 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
513 fsname, size, offset);
514 setenv_hex("filesize", size);
515 } else {
516 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
517 }
518
519 return !(size > 0);
520 }
521 return 1;
522}
523
524
525
526
527
528
529
530
531
532
533
534int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
535{
536 char *filename = "/";
537 int ret;
538 struct part_info *part;
539
540 if (argc == 2)
541 filename = argv[1];
542
543
544 if (mtdparts_init() !=0)
545 return 1;
546
547 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
548
549
550 if (cramfs_check(part)) {
551 ret = cramfs_ls (part, filename);
552 } else {
553
554 ret = jffs2_1pass_ls(part, filename);
555 }
556
557 return ret ? 0 : 1;
558 }
559 return 1;
560}
561
562
563
564
565
566
567
568
569
570
571
572int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
573{
574 struct part_info *part;
575 char *fsname;
576 int ret;
577
578
579 if (mtdparts_init() !=0)
580 return 1;
581
582 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
583
584
585 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
586 printf("### filesystem type is %s\n", fsname);
587
588 if (cramfs_check(part)) {
589 ret = cramfs_info (part);
590 } else {
591
592 ret = jffs2_1pass_info(part);
593 }
594
595 return ret ? 0 : 1;
596 }
597 return 1;
598}
599
600
601U_BOOT_CMD(
602 fsload, 3, 0, do_jffs2_fsload,
603 "load binary file from a filesystem image",
604 "[ off ] [ filename ]\n"
605 " - load binary file from flash bank\n"
606 " with offset 'off'"
607);
608U_BOOT_CMD(
609 fsls, 2, 1, do_jffs2_ls,
610 "list files in a directory (default /)",
611 "[ directory ]"
612);
613
614U_BOOT_CMD(
615 fsinfo, 1, 1, do_jffs2_fsinfo,
616 "print information about filesystems",
617 ""
618);
619
620