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