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 nand_info_t
*nand
;
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(nand
, 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(nand
, 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
;
595 nand_read(nand
, jNode
->offset
+ sizeof(struct jffs2_raw_inode
), &len
, &tmp
);
596 tmp
[jNode
->csize
] = '\0';
600 /* ok so the name of the new file to find is in tmp */
601 /* if it starts with a slash it is root based else shared dirs */
605 pino
= jDirFoundPino
;
607 return jffs2_1pass_search_inode(pL
, tmp
, pino
);
611 jffs2_1pass_search_list_inodes(struct b_lists
* pL
, const char *fname
, u32 pino
)
615 char working_tmp
[256];
618 /* discard any leading slash */
620 while (fname
[i
] == '/')
622 strcpy(tmp
, &fname
[i
]);
623 working_tmp
[0] = '\0';
624 while ((c
= (char *) strchr(tmp
, '/'))) /* we are still dired searching */
626 strncpy(working_tmp
, tmp
, c
- tmp
);
627 working_tmp
[c
- tmp
] = '\0';
628 for (i
= 0; i
< strlen(c
) - 1; i
++)
631 /* only a failure if we arent looking at top level */
632 if (!(pino
= jffs2_1pass_find_inode(pL
, working_tmp
, pino
)) &&
634 putstr("find_inode failed for name=");
641 if (tmp
[0] && !(pino
= jffs2_1pass_find_inode(pL
, tmp
, pino
))) {
642 putstr("find_inode failed for name=");
647 /* this is for the bare filename, directories have already been mapped */
648 if (!(pino
= jffs2_1pass_list_inodes(pL
, pino
))) {
649 putstr("find_inode failed for name=");
659 jffs2_1pass_rescan_needed(struct part_info
*part
)
662 struct jffs2_unknown_node onode
;
663 struct jffs2_unknown_node
*node
;
664 struct b_lists
*pL
= (struct b_lists
*)part
->jffs2_priv
;
666 if (part
->jffs2_priv
== 0){
667 DEBUGF ("rescan: First time in use\n");
670 /* if we have no list, we need to rescan */
671 if (pL
->frag
.listCount
== 0) {
672 DEBUGF ("rescan: fraglist zero\n");
676 /* or if we are scanning a new partition */
677 if (pL
->partOffset
!= part
->offset
) {
678 DEBUGF ("rescan: different partition\n");
684 /* but suppose someone reflashed a partition at the same offset... */
685 b
= pL
->dir
.listHead
;
687 node
= (struct jffs2_unknown_node
*) get_fl_mem(b
->offset
,
688 sizeof(onode
), &onode
);
689 if (node
->nodetype
!= JFFS2_NODETYPE_DIRENT
) {
690 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
691 (unsigned long) b
->offset
);
700 #ifdef DEBUG_FRAGMENTS
702 dump_fragments(struct b_lists
*pL
)
705 struct jffs2_raw_inode ojNode
;
706 struct jffs2_raw_inode
*jNode
;
708 putstr("\r\n\r\n******The fragment Entries******\r\n");
709 b
= pL
->frag
.listHead
;
711 jNode
= (struct jffs2_raw_inode
*) get_fl_mem(b
->offset
,
712 sizeof(ojNode
), &ojNode
);
713 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b
->offset
);
714 putLabeledWord("\tbuild_list: totlen = ", jNode
->totlen
);
715 putLabeledWord("\tbuild_list: inode = ", jNode
->ino
);
716 putLabeledWord("\tbuild_list: version = ", jNode
->version
);
717 putLabeledWord("\tbuild_list: isize = ", jNode
->isize
);
718 putLabeledWord("\tbuild_list: atime = ", jNode
->atime
);
719 putLabeledWord("\tbuild_list: offset = ", jNode
->offset
);
720 putLabeledWord("\tbuild_list: csize = ", jNode
->csize
);
721 putLabeledWord("\tbuild_list: dsize = ", jNode
->dsize
);
722 putLabeledWord("\tbuild_list: compr = ", jNode
->compr
);
723 putLabeledWord("\tbuild_list: usercompr = ", jNode
->usercompr
);
724 putLabeledWord("\tbuild_list: flags = ", jNode
->flags
);
725 putLabeledWord("\tbuild_list: offset = ", b
->offset
); /* FIXME: ? [RS] */
733 dump_dirents(struct b_lists
*pL
)
736 struct jffs2_raw_dirent
*jDir
;
738 putstr("\r\n\r\n******The directory Entries******\r\n");
739 b
= pL
->dir
.listHead
;
741 jDir
= (struct jffs2_raw_dirent
*) get_node_mem(b
->offset
);
743 putnstr(jDir
->name
, jDir
->nsize
);
744 putLabeledWord("\r\n\tbuild_list: magic = ", jDir
->magic
);
745 putLabeledWord("\tbuild_list: nodetype = ", jDir
->nodetype
);
746 putLabeledWord("\tbuild_list: hdr_crc = ", jDir
->hdr_crc
);
747 putLabeledWord("\tbuild_list: pino = ", jDir
->pino
);
748 putLabeledWord("\tbuild_list: version = ", jDir
->version
);
749 putLabeledWord("\tbuild_list: ino = ", jDir
->ino
);
750 putLabeledWord("\tbuild_list: mctime = ", jDir
->mctime
);
751 putLabeledWord("\tbuild_list: nsize = ", jDir
->nsize
);
752 putLabeledWord("\tbuild_list: type = ", jDir
->type
);
753 putLabeledWord("\tbuild_list: node_crc = ", jDir
->node_crc
);
754 putLabeledWord("\tbuild_list: name_crc = ", jDir
->name_crc
);
755 putLabeledWord("\tbuild_list: offset = ", b
->offset
); /* FIXME: ? [RS] */
763 jffs2_fill_scan_buf(nand_info_t
*nand
, unsigned char *buf
,
764 unsigned ofs
, unsigned len
)
770 ret
= nand_read(nand
, ofs
, &olen
, buf
);
772 printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len
, ofs
, ret
);
776 printf("Read at 0x%x gave only 0x%x bytes\n", ofs
, olen
);
782 #define EMPTY_SCAN_SIZE 1024
784 jffs2_1pass_build_lists(struct part_info
* part
)
787 struct jffs2_unknown_node
*node
;
788 unsigned nr_blocks
, sectorsize
, ofs
, offset
;
796 struct mtdids
*id
= part
->dev
->id
;
797 nand
= nand_info
+ id
->num
;
799 /* if we are building a list we need to refresh the cache. */
800 jffs_init_1pass_list(part
);
801 pL
= (struct b_lists
*)part
->jffs2_priv
;
802 pL
->partOffset
= part
->offset
;
803 puts ("Scanning JFFS2 FS: ");
805 sectorsize
= nand
->erasesize
;
806 nr_blocks
= part
->size
/ sectorsize
;
807 buf
= malloc(sectorsize
);
811 for (i
= 0; i
< nr_blocks
; i
++) {
812 printf("\b\b%c ", spinner
[counter
++ % sizeof(spinner
)]);
814 offset
= part
->offset
+ i
* sectorsize
;
816 if (nand_block_isbad(nand
, offset
))
819 if (jffs2_fill_scan_buf(nand
, buf
, offset
, EMPTY_SCAN_SIZE
))
823 /* Scan only 4KiB of 0xFF before declaring it's empty */
824 while (ofs
< EMPTY_SCAN_SIZE
&& *(uint32_t *)(&buf
[ofs
]) == 0xFFFFFFFF)
826 if (ofs
== EMPTY_SCAN_SIZE
)
829 if (jffs2_fill_scan_buf(nand
, buf
+ EMPTY_SCAN_SIZE
, offset
+ EMPTY_SCAN_SIZE
, sectorsize
- EMPTY_SCAN_SIZE
))
833 while (ofs
< sectorsize
- sizeof(struct jffs2_unknown_node
)) {
834 node
= (struct jffs2_unknown_node
*)&buf
[ofs
];
835 if (node
->magic
!= JFFS2_MAGIC_BITMASK
|| !hdr_crc(node
)) {
841 /* if its a fragment add it */
842 if (node
->nodetype
== JFFS2_NODETYPE_INODE
&&
843 inode_crc((struct jffs2_raw_inode
*) node
)) {
844 if (insert_inode(&pL
->frag
, (struct jffs2_raw_inode
*) node
,
848 } else if (node
->nodetype
== JFFS2_NODETYPE_DIRENT
&&
849 dirent_crc((struct jffs2_raw_dirent
*) node
) &&
850 dirent_name_crc((struct jffs2_raw_dirent
*) node
)) {
851 if (! (counterN
%100))
853 if (insert_dirent(&pL
->dir
, (struct jffs2_raw_dirent
*) node
,
858 } else if (node
->nodetype
== JFFS2_NODETYPE_CLEANMARKER
) {
859 if (node
->totlen
!= sizeof(struct jffs2_unknown_node
))
860 printf("OOPS Cleanmarker has bad size "
863 sizeof(struct jffs2_unknown_node
));
864 } else if (node
->nodetype
== JFFS2_NODETYPE_PADDING
) {
865 if (node
->totlen
< sizeof(struct jffs2_unknown_node
))
866 printf("OOPS Padding has bad size "
869 sizeof(struct jffs2_unknown_node
));
871 printf("Unknown node type: %x len %d offset 0x%x\n",
873 node
->totlen
, offset
);
875 offset
+= ((node
->totlen
+ 3) & ~3);
876 ofs
+= ((node
->totlen
+ 3) & ~3);
881 putstr("\b\b done.\r\n"); /* close off the dots */
884 putLabeledWord("dir entries = ", pL
->dir
.listCount
);
885 putLabeledWord("frag entries = ", pL
->frag
.listCount
);
886 putLabeledWord("+4 increments = ", counter4
);
887 putLabeledWord("+file_offset increments = ", counterF
);
894 #ifdef DEBUG_FRAGMENTS
898 /* give visual feedback that we are done scanning the flash */
899 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
907 jffs2_1pass_fill_info(struct b_lists
* pL
, struct b_jffs2_info
* piL
)
910 struct jffs2_raw_inode ojNode
;
911 struct jffs2_raw_inode
*jNode
;
914 for (i
= 0; i
< JFFS2_NUM_COMPR
; i
++) {
915 piL
->compr_info
[i
].num_frags
= 0;
916 piL
->compr_info
[i
].compr_sum
= 0;
917 piL
->compr_info
[i
].decompr_sum
= 0;
920 b = pL->frag.listHead;
922 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
923 sizeof(ojNode), &ojNode);
924 if (jNode->compr < JFFS2_NUM_COMPR) {
925 piL->compr_info[jNode->compr].num_frags++;
926 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
927 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
936 static struct b_lists
*
937 jffs2_get_list(struct part_info
* part
, const char *who
)
939 if (jffs2_1pass_rescan_needed(part
)) {
940 if (!jffs2_1pass_build_lists(part
)) {
941 printf("%s: Failed to scan JFFSv2 file structure\n", who
);
945 return (struct b_lists
*)part
->jffs2_priv
;
949 /* Print directory / file contents */
951 jffs2_1pass_ls(struct part_info
* part
, const char *fname
)
957 if (! (pl
= jffs2_get_list(part
, "ls")))
960 if (! (inode
= jffs2_1pass_search_list_inodes(pl
, fname
, 1))) {
961 putstr("ls: Failed to scan jffs2 file structure\r\n");
966 putLabeledWord("found file at inode = ", inode
);
967 putLabeledWord("read_inode returns = ", ret
);
974 /* Load a file from flash into memory. fname can be a full path */
976 jffs2_1pass_load(char *dest
, struct part_info
* part
, const char *fname
)
983 if (! (pl
= jffs2_get_list(part
, "load")))
986 if (! (inode
= jffs2_1pass_search_inode(pl
, fname
, 1))) {
987 putstr("load: Failed to find inode\r\n");
991 /* Resolve symlinks */
992 if (! (inode
= jffs2_1pass_resolve_inode(pl
, inode
))) {
993 putstr("load: Failed to resolve inode structure\r\n");
997 if ((ret
= jffs2_1pass_read_inode(pl
, inode
, dest
, NULL
)) < 0) {
998 putstr("load: Failed to read inode\r\n");
1002 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname
,
1003 (unsigned long) dest
, ret
);
1007 /* Return information about the fs on this partition */
1009 jffs2_1pass_info(struct part_info
* part
)
1011 struct b_jffs2_info info
;
1015 if (! (pl
= jffs2_get_list(part
, "info")))
1018 jffs2_1pass_fill_info(pl
, &info
);
1019 for (i
= 0; i
< JFFS2_NUM_COMPR
; i
++) {
1020 printf ("Compression: %s\n"
1021 "\tfrag count: %d\n"
1022 "\tcompressed sum: %d\n"
1023 "\tuncompressed sum: %d\n",
1025 info
.compr_info
[i
].num_frags
,
1026 info
.compr_info
[i
].compr_sum
,
1027 info
.compr_info
[i
].decompr_sum
);