4 #include <linux/stat.h>
5 #include <linux/time.h>
7 #include <jffs2/jffs2.h>
8 #include <jffs2/jffs2_1pass.h>
11 #include "jffs2_nand_private.h"
13 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
15 /* Debugging switches */
16 #undef DEBUG_DIRENTS /* print directory entry list after scan */
17 #undef DEBUG_FRAGMENTS /* print fragment list after scan */
18 #undef DEBUG /* enable debugging messages */
21 # define DEBUGF(fmt,args...) printf(fmt ,##args)
23 # define DEBUGF(fmt,args...)
26 static struct mtd_info
*mtd
;
28 /* Compression names */
29 static char *compr_names
[] = {
37 #if defined(CONFIG_JFFS2_LZO)
43 static char spinner
[] = { '|', '/', '-', '\\' };
45 /* Memory management */
48 struct mem_block
*next
;
53 free_nodes(struct b_list
*list
)
55 while (list
->listMemBase
!= NULL
) {
56 struct mem_block
*next
= list
->listMemBase
->next
;
57 free(list
->listMemBase
);
58 list
->listMemBase
= next
;
62 static struct b_node
*
63 add_node(struct b_list
*list
, int size
)
66 struct mem_block
*memBase
;
69 memBase
= list
->listMemBase
;
71 index
= memBase
->index
;
73 if (memBase
== NULL
|| index
>= NODE_CHUNK
) {
74 /* we need more space before we continue */
75 memBase
= mmalloc(sizeof(struct mem_block
) + NODE_CHUNK
* size
);
76 if (memBase
== NULL
) {
77 putstr("add_node: malloc failed\n");
80 memBase
->next
= list
->listMemBase
;
83 /* now we have room to add it. */
84 b
= (struct b_node
*)&memBase
->nodes
[size
* index
];
87 memBase
->index
= index
;
88 list
->listMemBase
= memBase
;
93 static struct b_node
*
94 insert_node(struct b_list
*list
, struct b_node
*new)
96 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
97 struct b_node
*b
, *prev
;
99 if (list
->listTail
!= NULL
&& list
->listCompare(new, list
->listTail
))
100 prev
= list
->listTail
;
101 else if (list
->listLast
!= NULL
&& list
->listCompare(new, list
->listLast
))
102 prev
= list
->listLast
;
106 for (b
= (prev
? prev
->next
: list
->listHead
);
107 b
!= NULL
&& list
->listCompare(new, b
);
108 prev
= b
, b
= b
->next
) {
112 list
->listLast
= prev
;
119 list
->listHead
= new;
123 new->next
= (struct b_node
*) NULL
;
124 if (list
->listTail
!= NULL
) {
125 list
->listTail
->next
= new;
126 list
->listTail
= new;
128 list
->listTail
= list
->listHead
= new;
135 static struct b_node
*
136 insert_inode(struct b_list
*list
, struct jffs2_raw_inode
*node
, u32 offset
)
140 if (!(new = (struct b_inode
*)add_node(list
, sizeof(struct b_inode
)))) {
141 putstr("add_node failed!\r\n");
144 new->offset
= offset
;
145 new->version
= node
->version
;
146 new->ino
= node
->ino
;
147 new->isize
= node
->isize
;
148 new->csize
= node
->csize
;
150 return insert_node(list
, (struct b_node
*)new);
153 static struct b_node
*
154 insert_dirent(struct b_list
*list
, struct jffs2_raw_dirent
*node
, u32 offset
)
156 struct b_dirent
*new;
158 if (!(new = (struct b_dirent
*)add_node(list
, sizeof(struct b_dirent
)))) {
159 putstr("add_node failed!\r\n");
162 new->offset
= offset
;
163 new->version
= node
->version
;
164 new->pino
= node
->pino
;
165 new->ino
= node
->ino
;
166 new->nhash
= full_name_hash(node
->name
, node
->nsize
);
167 new->nsize
= node
->nsize
;
168 new->type
= node
->type
;
170 return insert_node(list
, (struct b_node
*)new);
173 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
174 /* Sort data entries with the latest version last, so that if there
175 * is overlapping data the latest version will be used.
177 static int compare_inodes(struct b_node
*new, struct b_node
*old
)
179 struct jffs2_raw_inode ojNew
;
180 struct jffs2_raw_inode ojOld
;
181 struct jffs2_raw_inode
*jNew
=
182 (struct jffs2_raw_inode
*)get_fl_mem(new->offset
, sizeof(ojNew
), &ojNew
);
183 struct jffs2_raw_inode
*jOld
=
184 (struct jffs2_raw_inode
*)get_fl_mem(old
->offset
, sizeof(ojOld
), &ojOld
);
186 return jNew
->version
> jOld
->version
;
189 /* Sort directory entries so all entries in the same directory
190 * with the same name are grouped together, with the latest version
191 * last. This makes it easy to eliminate all but the latest version
192 * by marking the previous version dead by setting the inode to 0.
194 static int compare_dirents(struct b_node
*new, struct b_node
*old
)
196 struct jffs2_raw_dirent ojNew
;
197 struct jffs2_raw_dirent ojOld
;
198 struct jffs2_raw_dirent
*jNew
=
199 (struct jffs2_raw_dirent
*)get_fl_mem(new->offset
, sizeof(ojNew
), &ojNew
);
200 struct jffs2_raw_dirent
*jOld
=
201 (struct jffs2_raw_dirent
*)get_fl_mem(old
->offset
, sizeof(ojOld
), &ojOld
);
204 /* ascending sort by pino */
205 if (jNew
->pino
!= jOld
->pino
)
206 return jNew
->pino
> jOld
->pino
;
208 /* pino is the same, so use ascending sort by nsize, so
209 * we don't do strncmp unless we really must.
211 if (jNew
->nsize
!= jOld
->nsize
)
212 return jNew
->nsize
> jOld
->nsize
;
214 /* length is also the same, so use ascending sort by name
216 cmp
= strncmp(jNew
->name
, jOld
->name
, jNew
->nsize
);
220 /* we have duplicate names in this directory, so use ascending
223 if (jNew
->version
> jOld
->version
) {
224 /* since jNew is newer, we know jOld is not valid, so
225 * mark it with inode 0 and it will not be used
236 jffs_init_1pass_list(struct part_info
*part
)
240 if (part
->jffs2_priv
!= NULL
) {
241 pL
= (struct b_lists
*)part
->jffs2_priv
;
242 free_nodes(&pL
->frag
);
243 free_nodes(&pL
->dir
);
246 if (NULL
!= (part
->jffs2_priv
= malloc(sizeof(struct b_lists
)))) {
247 pL
= (struct b_lists
*)part
->jffs2_priv
;
249 memset(pL
, 0, sizeof(*pL
));
250 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
251 pL
->dir
.listCompare
= compare_dirents
;
252 pL
->frag
.listCompare
= compare_inodes
;
258 /* find the inode from the slashless name given a parent */
260 jffs2_1pass_read_inode(struct b_lists
*pL
, u32 ino
, char *dest
,
263 struct b_inode
*jNode
;
265 u32 latestVersion
= 0;
268 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
269 /* Find file size before loading any data, so fragments that
270 * start past the end of file can be ignored. A fragment
271 * that is partially in the file is loaded, so extra data may
272 * be loaded up to the next 4K boundary above the file size.
273 * This shouldn't cause trouble when loading kernel images, so
274 * we will live with it.
276 for (jNode
= (struct b_inode
*)pL
->frag
.listHead
; jNode
; jNode
= jNode
->next
) {
277 if ((ino
== jNode
->ino
)) {
278 /* get actual file length from the newest node */
279 if (jNode
->version
>= latestVersion
) {
280 totalSize
= jNode
->isize
;
281 latestVersion
= jNode
->version
;
287 for (jNode
= (struct b_inode
*)pL
->frag
.listHead
; jNode
; jNode
= jNode
->next
) {
288 if ((ino
!= jNode
->ino
))
290 #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
291 /* get actual file length from the newest node */
292 if (jNode
->version
>= latestVersion
) {
293 totalSize
= jNode
->isize
;
294 latestVersion
= jNode
->version
;
299 char data
[4096 + sizeof(struct jffs2_raw_inode
)];
300 struct jffs2_raw_inode
*inode
;
303 inode
= (struct jffs2_raw_inode
*)&data
;
304 len
= sizeof(struct jffs2_raw_inode
);
307 nand_read(mtd
, jNode
->offset
, &len
, inode
);
308 /* ignore data behind latest known EOF */
309 if (inode
->offset
> totalSize
)
313 stat
->st_mtime
= inode
->mtime
;
314 stat
->st_mode
= inode
->mode
;
315 stat
->st_ino
= inode
->ino
;
316 stat
->st_size
= totalSize
;
322 src
= ((char *) inode
) + sizeof(struct jffs2_raw_inode
);
323 dst
= (char *) (dest
+ inode
->offset
);
325 switch (inode
->compr
) {
326 case JFFS2_COMPR_NONE
:
328 memcpy(dst
, src
, inode
->dsize
);
330 case JFFS2_COMPR_ZERO
:
332 memset(dst
, 0, inode
->dsize
);
334 case JFFS2_COMPR_RTIME
:
336 rtime_decompress(src
, dst
, inode
->csize
, inode
->dsize
);
338 case JFFS2_COMPR_DYNRUBIN
:
339 /* this is slow but it works */
341 dynrubin_decompress(src
, dst
, inode
->csize
, inode
->dsize
);
343 case JFFS2_COMPR_ZLIB
:
344 ret
= zlib_decompress(src
, dst
, inode
->csize
, inode
->dsize
);
346 #if defined(CONFIG_JFFS2_LZO)
347 case JFFS2_COMPR_LZO
:
348 ret
= lzo_decompress(src
, dst
, inode
->csize
, inode
->dsize
);
353 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", inode
->compr
);
362 /* find the inode from the slashless name given a parent */
364 jffs2_1pass_find_inode(struct b_lists
* pL
, const char *name
, u32 pino
)
366 struct b_dirent
*jDir
;
367 int len
= strlen(name
); /* name is assumed slash free */
368 unsigned int nhash
= full_name_hash(name
, len
);
372 /* we need to search all and return the inode with the highest version */
373 for (jDir
= (struct b_dirent
*)pL
->dir
.listHead
; jDir
; jDir
= jDir
->next
) {
374 if ((pino
== jDir
->pino
) && (jDir
->ino
) && /* 0 for unlink */
375 (len
== jDir
->nsize
) && (nhash
== jDir
->nhash
)) {
376 /* TODO: compare name */
377 if (jDir
->version
< version
)
380 if (jDir
->version
== version
&& inode
!= 0) {
381 /* I'm pretty sure this isn't legal */
382 putstr(" ** ERROR ** ");
383 /* putnstr(jDir->name, jDir->nsize); */
384 /* putLabeledWord(" has dup version =", version); */
387 version
= jDir
->version
;
393 char *mkmodestr(unsigned long mode
, char *str
)
395 static const char *l
= "xwr";
399 switch (mode
& S_IFMT
) {
400 case S_IFDIR
: str
[0] = 'd'; break;
401 case S_IFBLK
: str
[0] = 'b'; break;
402 case S_IFCHR
: str
[0] = 'c'; break;
403 case S_IFIFO
: str
[0] = 'f'; break;
404 case S_IFLNK
: str
[0] = 'l'; break;
405 case S_IFSOCK
: str
[0] = 's'; break;
406 case S_IFREG
: str
[0] = '-'; break;
407 default: str
[0] = '?';
410 for(i
= 0; i
< 9; i
++) {
412 str
[9-i
] = (mode
& mask
)?c
:'-';
416 if(mode
& S_ISUID
) str
[3] = (mode
& S_IXUSR
)?'s':'S';
417 if(mode
& S_ISGID
) str
[6] = (mode
& S_IXGRP
)?'s':'S';
418 if(mode
& S_ISVTX
) str
[9] = (mode
& S_IXOTH
)?'t':'T';
423 static inline void dump_stat(struct stat
*st
, const char *name
)
428 if (st
->st_mtime
== (time_t)(-1)) /* some ctimes really hate -1 */
431 ctime_r(&st
->st_mtime
, s
/*,64*/); /* newlib ctime doesn't have buflen */
433 if ((p
= strchr(s
,'\n')) != NULL
) *p
= '\0';
434 if ((p
= strchr(s
,'\r')) != NULL
) *p
= '\0';
437 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
438 st->st_size, s, name);
441 printf(" %s %8ld %s %s", mkmodestr(st
->st_mode
,str
), st
->st_size
, s
, name
);
445 dump_inode(struct b_lists
*pL
, struct b_dirent
*d
, struct b_inode
*i
)
447 char fname
[JFFS2_MAX_NAME_LEN
+ 1];
451 if(!d
|| !i
) return -1;
453 nand_read(mtd
, d
->offset
+ sizeof(struct jffs2_raw_dirent
),
455 fname
[d
->nsize
] = '\0';
457 memset(&st
, 0, sizeof(st
));
459 jffs2_1pass_read_inode(pL
, i
->ino
, NULL
, &st
);
461 dump_stat(&st
, fname
);
463 if (d->type == DT_LNK) {
464 unsigned char *src = (unsigned char *) (&i[1]);
466 putnstr(src, (int)i->dsize);
474 /* list inodes with the given pino */
476 jffs2_1pass_list_inodes(struct b_lists
* pL
, u32 pino
)
478 struct b_dirent
*jDir
;
481 for (jDir
= (struct b_dirent
*)pL
->dir
.listHead
; jDir
; jDir
= jDir
->next
) {
482 if ((pino
== jDir
->pino
) && (jDir
->ino
)) { /* ino=0 -> unlink */
483 struct b_inode
*jNode
= (struct b_inode
*)pL
->frag
.listHead
;
484 struct b_inode
*i
= NULL
;
487 if (jNode
->ino
== jDir
->ino
&& jNode
->version
>= i_version
) {
488 i_version
= jNode
->version
;
493 dump_inode(pL
, jDir
, i
);
500 jffs2_1pass_search_inode(struct b_lists
* pL
, const char *fname
, u32 pino
)
504 char working_tmp
[256];
507 /* discard any leading slash */
509 while (fname
[i
] == '/')
511 strcpy(tmp
, &fname
[i
]);
513 while ((c
= (char *) strchr(tmp
, '/'))) /* we are still dired searching */
515 strncpy(working_tmp
, tmp
, c
- tmp
);
516 working_tmp
[c
- tmp
] = '\0';
518 putstr("search_inode: tmp = ");
521 putstr("search_inode: wtmp = ");
524 putstr("search_inode: c = ");
528 for (i
= 0; i
< strlen(c
) - 1; i
++)
532 putstr("search_inode: post tmp = ");
537 if (!(pino
= jffs2_1pass_find_inode(pL
, working_tmp
, pino
))) {
538 putstr("find_inode failed for name=");
544 /* this is for the bare filename, directories have already been mapped */
545 if (!(pino
= jffs2_1pass_find_inode(pL
, tmp
, pino
))) {
546 putstr("find_inode failed for name=");
556 jffs2_1pass_resolve_inode(struct b_lists
* pL
, u32 ino
)
558 struct b_dirent
*jDir
;
559 struct b_inode
*jNode
;
560 u8 jDirFoundType
= 0;
561 u32 jDirFoundIno
= 0;
562 u32 jDirFoundPino
= 0;
563 char tmp
[JFFS2_MAX_NAME_LEN
+ 1];
567 /* we need to search all and return the inode with the highest version */
568 for (jDir
= (struct b_dirent
*)pL
->dir
.listHead
; jDir
; jDir
= jDir
->next
) {
569 if (ino
== jDir
->ino
) {
570 if (jDir
->version
< version
)
573 if (jDir
->version
== version
&& jDirFoundType
) {
574 /* I'm pretty sure this isn't legal */
575 putstr(" ** ERROR ** ");
576 /* putnstr(jDir->name, jDir->nsize); */
577 /* putLabeledWord(" has dup version (resolve) = ", */
581 jDirFoundType
= jDir
->type
;
582 jDirFoundIno
= jDir
->ino
;
583 jDirFoundPino
= jDir
->pino
;
584 version
= jDir
->version
;
587 /* now we found the right entry again. (shoulda returned inode*) */
588 if (jDirFoundType
!= DT_LNK
)
591 /* it's a soft link so we follow it again. */
592 for (jNode
= (struct b_inode
*)pL
->frag
.listHead
; jNode
; jNode
= jNode
->next
) {
593 if (jNode
->ino
== jDirFoundIno
) {
594 size_t len
= jNode
->csize
;
596 jNode
->offset
+ sizeof(struct jffs2_raw_inode
),
598 tmp
[jNode
->csize
] = '\0';
602 /* ok so the name of the new file to find is in tmp */
603 /* if it starts with a slash it is root based else shared dirs */
607 pino
= jDirFoundPino
;
609 return jffs2_1pass_search_inode(pL
, tmp
, pino
);
613 jffs2_1pass_search_list_inodes(struct b_lists
* pL
, const char *fname
, u32 pino
)
617 char working_tmp
[256];
620 /* discard any leading slash */
622 while (fname
[i
] == '/')
624 strcpy(tmp
, &fname
[i
]);
625 working_tmp
[0] = '\0';
626 while ((c
= (char *) strchr(tmp
, '/'))) /* we are still dired searching */
628 strncpy(working_tmp
, tmp
, c
- tmp
);
629 working_tmp
[c
- tmp
] = '\0';
630 for (i
= 0; i
< strlen(c
) - 1; i
++)
633 /* only a failure if we arent looking at top level */
634 if (!(pino
= jffs2_1pass_find_inode(pL
, working_tmp
, pino
)) &&
636 putstr("find_inode failed for name=");
643 if (tmp
[0] && !(pino
= jffs2_1pass_find_inode(pL
, tmp
, pino
))) {
644 putstr("find_inode failed for name=");
649 /* this is for the bare filename, directories have already been mapped */
650 if (!(pino
= jffs2_1pass_list_inodes(pL
, pino
))) {
651 putstr("find_inode failed for name=");
661 jffs2_1pass_rescan_needed(struct part_info
*part
)
664 struct jffs2_unknown_node onode
;
665 struct jffs2_unknown_node
*node
;
666 struct b_lists
*pL
= (struct b_lists
*)part
->jffs2_priv
;
668 if (part
->jffs2_priv
== 0){
669 DEBUGF ("rescan: First time in use\n");
672 /* if we have no list, we need to rescan */
673 if (pL
->frag
.listCount
== 0) {
674 DEBUGF ("rescan: fraglist zero\n");
678 /* or if we are scanning a new partition */
679 if (pL
->partOffset
!= part
->offset
) {
680 DEBUGF ("rescan: different partition\n");
686 /* but suppose someone reflashed a partition at the same offset... */
687 b
= pL
->dir
.listHead
;
689 node
= (struct jffs2_unknown_node
*) get_fl_mem(b
->offset
,
690 sizeof(onode
), &onode
);
691 if (node
->nodetype
!= JFFS2_NODETYPE_DIRENT
) {
692 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
693 (unsigned long) b
->offset
);
702 #ifdef DEBUG_FRAGMENTS
704 dump_fragments(struct b_lists
*pL
)
707 struct jffs2_raw_inode ojNode
;
708 struct jffs2_raw_inode
*jNode
;
710 putstr("\r\n\r\n******The fragment Entries******\r\n");
711 b
= pL
->frag
.listHead
;
713 jNode
= (struct jffs2_raw_inode
*) get_fl_mem(b
->offset
,
714 sizeof(ojNode
), &ojNode
);
715 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b
->offset
);
716 putLabeledWord("\tbuild_list: totlen = ", jNode
->totlen
);
717 putLabeledWord("\tbuild_list: inode = ", jNode
->ino
);
718 putLabeledWord("\tbuild_list: version = ", jNode
->version
);
719 putLabeledWord("\tbuild_list: isize = ", jNode
->isize
);
720 putLabeledWord("\tbuild_list: atime = ", jNode
->atime
);
721 putLabeledWord("\tbuild_list: offset = ", jNode
->offset
);
722 putLabeledWord("\tbuild_list: csize = ", jNode
->csize
);
723 putLabeledWord("\tbuild_list: dsize = ", jNode
->dsize
);
724 putLabeledWord("\tbuild_list: compr = ", jNode
->compr
);
725 putLabeledWord("\tbuild_list: usercompr = ", jNode
->usercompr
);
726 putLabeledWord("\tbuild_list: flags = ", jNode
->flags
);
727 putLabeledWord("\tbuild_list: offset = ", b
->offset
); /* FIXME: ? [RS] */
735 dump_dirents(struct b_lists
*pL
)
738 struct jffs2_raw_dirent
*jDir
;
740 putstr("\r\n\r\n******The directory Entries******\r\n");
741 b
= pL
->dir
.listHead
;
743 jDir
= (struct jffs2_raw_dirent
*) get_node_mem(b
->offset
);
745 putnstr(jDir
->name
, jDir
->nsize
);
746 putLabeledWord("\r\n\tbuild_list: magic = ", jDir
->magic
);
747 putLabeledWord("\tbuild_list: nodetype = ", jDir
->nodetype
);
748 putLabeledWord("\tbuild_list: hdr_crc = ", jDir
->hdr_crc
);
749 putLabeledWord("\tbuild_list: pino = ", jDir
->pino
);
750 putLabeledWord("\tbuild_list: version = ", jDir
->version
);
751 putLabeledWord("\tbuild_list: ino = ", jDir
->ino
);
752 putLabeledWord("\tbuild_list: mctime = ", jDir
->mctime
);
753 putLabeledWord("\tbuild_list: nsize = ", jDir
->nsize
);
754 putLabeledWord("\tbuild_list: type = ", jDir
->type
);
755 putLabeledWord("\tbuild_list: node_crc = ", jDir
->node_crc
);
756 putLabeledWord("\tbuild_list: name_crc = ", jDir
->name_crc
);
757 putLabeledWord("\tbuild_list: offset = ", b
->offset
); /* FIXME: ? [RS] */
765 jffs2_fill_scan_buf(struct mtd_info
*mtd
, unsigned char *buf
,
766 unsigned ofs
, unsigned len
)
772 ret
= nand_read(mtd
, ofs
, &olen
, buf
);
774 printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len
, ofs
, ret
);
778 printf("Read at 0x%x gave only 0x%x bytes\n", ofs
, olen
);
784 #define EMPTY_SCAN_SIZE 1024
786 jffs2_1pass_build_lists(struct part_info
* part
)
789 struct jffs2_unknown_node
*node
;
790 unsigned nr_blocks
, sectorsize
, ofs
, offset
;
798 struct mtdids
*id
= part
->dev
->id
;
799 mtd
= get_nand_dev_by_index(id
->num
);
801 pr_err("\nno NAND devices available\n");
805 /* if we are building a list we need to refresh the cache. */
806 jffs_init_1pass_list(part
);
807 pL
= (struct b_lists
*)part
->jffs2_priv
;
808 pL
->partOffset
= part
->offset
;
809 puts ("Scanning JFFS2 FS: ");
811 sectorsize
= mtd
->erasesize
;
812 nr_blocks
= part
->size
/ sectorsize
;
813 buf
= malloc(sectorsize
);
817 for (i
= 0; i
< nr_blocks
; i
++) {
818 printf("\b\b%c ", spinner
[counter
++ % sizeof(spinner
)]);
820 offset
= part
->offset
+ i
* sectorsize
;
822 if (nand_block_isbad(mtd
, offset
))
825 if (jffs2_fill_scan_buf(mtd
, buf
, offset
, EMPTY_SCAN_SIZE
))
829 /* Scan only 4KiB of 0xFF before declaring it's empty */
830 while (ofs
< EMPTY_SCAN_SIZE
&& *(uint32_t *)(&buf
[ofs
]) == 0xFFFFFFFF)
832 if (ofs
== EMPTY_SCAN_SIZE
)
835 if (jffs2_fill_scan_buf(mtd
, buf
+ EMPTY_SCAN_SIZE
, offset
+ EMPTY_SCAN_SIZE
, sectorsize
- EMPTY_SCAN_SIZE
))
839 while (ofs
< sectorsize
- sizeof(struct jffs2_unknown_node
)) {
840 node
= (struct jffs2_unknown_node
*)&buf
[ofs
];
841 if (node
->magic
!= JFFS2_MAGIC_BITMASK
|| !hdr_crc(node
)) {
847 /* if its a fragment add it */
848 if (node
->nodetype
== JFFS2_NODETYPE_INODE
&&
849 inode_crc((struct jffs2_raw_inode
*) node
)) {
850 if (insert_inode(&pL
->frag
, (struct jffs2_raw_inode
*) node
,
854 } else if (node
->nodetype
== JFFS2_NODETYPE_DIRENT
&&
855 dirent_crc((struct jffs2_raw_dirent
*) node
) &&
856 dirent_name_crc((struct jffs2_raw_dirent
*) node
)) {
857 if (! (counterN
%100))
859 if (insert_dirent(&pL
->dir
, (struct jffs2_raw_dirent
*) node
,
864 } else if (node
->nodetype
== JFFS2_NODETYPE_CLEANMARKER
) {
865 if (node
->totlen
!= sizeof(struct jffs2_unknown_node
))
866 printf("OOPS Cleanmarker has bad size "
869 sizeof(struct jffs2_unknown_node
));
870 } else if (node
->nodetype
== JFFS2_NODETYPE_PADDING
) {
871 if (node
->totlen
< sizeof(struct jffs2_unknown_node
))
872 printf("OOPS Padding has bad size "
875 sizeof(struct jffs2_unknown_node
));
877 printf("Unknown node type: %x len %d offset 0x%x\n",
879 node
->totlen
, offset
);
881 offset
+= ((node
->totlen
+ 3) & ~3);
882 ofs
+= ((node
->totlen
+ 3) & ~3);
887 putstr("\b\b done.\r\n"); /* close off the dots */
890 putLabeledWord("dir entries = ", pL
->dir
.listCount
);
891 putLabeledWord("frag entries = ", pL
->frag
.listCount
);
892 putLabeledWord("+4 increments = ", counter4
);
893 putLabeledWord("+file_offset increments = ", counterF
);
900 #ifdef DEBUG_FRAGMENTS
904 /* give visual feedback that we are done scanning the flash */
905 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
913 jffs2_1pass_fill_info(struct b_lists
* pL
, struct b_jffs2_info
* piL
)
916 struct jffs2_raw_inode ojNode
;
917 struct jffs2_raw_inode
*jNode
;
920 for (i
= 0; i
< JFFS2_NUM_COMPR
; i
++) {
921 piL
->compr_info
[i
].num_frags
= 0;
922 piL
->compr_info
[i
].compr_sum
= 0;
923 piL
->compr_info
[i
].decompr_sum
= 0;
926 b = pL->frag.listHead;
928 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
929 sizeof(ojNode), &ojNode);
930 if (jNode->compr < JFFS2_NUM_COMPR) {
931 piL->compr_info[jNode->compr].num_frags++;
932 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
933 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
942 static struct b_lists
*
943 jffs2_get_list(struct part_info
* part
, const char *who
)
945 if (jffs2_1pass_rescan_needed(part
)) {
946 if (!jffs2_1pass_build_lists(part
)) {
947 printf("%s: Failed to scan JFFSv2 file structure\n", who
);
951 return (struct b_lists
*)part
->jffs2_priv
;
955 /* Print directory / file contents */
957 jffs2_1pass_ls(struct part_info
* part
, const char *fname
)
963 if (! (pl
= jffs2_get_list(part
, "ls")))
966 if (! (inode
= jffs2_1pass_search_list_inodes(pl
, fname
, 1))) {
967 putstr("ls: Failed to scan jffs2 file structure\r\n");
972 putLabeledWord("found file at inode = ", inode
);
973 putLabeledWord("read_inode returns = ", ret
);
980 /* Load a file from flash into memory. fname can be a full path */
982 jffs2_1pass_load(char *dest
, struct part_info
* part
, const char *fname
)
989 if (! (pl
= jffs2_get_list(part
, "load")))
992 if (! (inode
= jffs2_1pass_search_inode(pl
, fname
, 1))) {
993 putstr("load: Failed to find inode\r\n");
997 /* Resolve symlinks */
998 if (! (inode
= jffs2_1pass_resolve_inode(pl
, inode
))) {
999 putstr("load: Failed to resolve inode structure\r\n");
1003 if ((ret
= jffs2_1pass_read_inode(pl
, inode
, dest
, NULL
)) < 0) {
1004 putstr("load: Failed to read inode\r\n");
1008 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname
,
1009 (unsigned long) dest
, ret
);
1013 /* Return information about the fs on this partition */
1015 jffs2_1pass_info(struct part_info
* part
)
1017 struct b_jffs2_info info
;
1021 if (! (pl
= jffs2_get_list(part
, "info")))
1024 jffs2_1pass_fill_info(pl
, &info
);
1025 for (i
= 0; i
< JFFS2_NUM_COMPR
; i
++) {
1026 printf ("Compression: %s\n"
1027 "\tfrag count: %d\n"
1028 "\tcompressed sum: %d\n"
1029 "\tuncompressed sum: %d\n",
1031 info
.compr_info
[i
].num_frags
,
1032 info
.compr_info
[i
].compr_sum
,
1033 info
.compr_info
[i
].decompr_sum
);