2 -------------------------------------------------------------------------
4 * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
5 * Copyright: Copyright (C) 2001, Russ Dill
6 * Author: Russ Dill <Russ.Dill@asu.edu>
7 * Description: Module to load kernel from jffs2
8 *-----------------------------------------------------------------------*/
10 * some portions of this code are taken from jffs2, and as such, the
11 * following copyright notice is included.
13 * JFFS2 -- Journalling Flash File System, Version 2.
15 * Copyright (C) 2001 Red Hat, Inc.
17 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
19 * The original JFFS, from which the design for JFFS2 was derived,
20 * was designed and implemented by Axis Communications AB.
22 * The contents of this file are subject to the Red Hat eCos Public
23 * License Version 1.1 (the "Licence"); you may not use this file
24 * except in compliance with the Licence. You may obtain a copy of
25 * the Licence at http://www.redhat.com/
27 * Software distributed under the Licence is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
29 * See the Licence for the specific language governing rights and
30 * limitations under the Licence.
32 * The Original Code is JFFS2 - Journalling Flash File System, version 2
34 * Alternatively, the contents of this file may be used under the
35 * terms of the GNU General Public License version 2 (the "GPL"), in
36 * which case the provisions of the GPL are applicable instead of the
37 * above. If you wish to allow the use of your version of this file
38 * only under the terms of the GPL and not to allow others to use your
39 * version of this file under the RHEPL, indicate your decision by
40 * deleting the provisions above and replace them with the notice and
41 * other provisions required by the GPL. If you do not delete the
42 * provisions above, a recipient may use your version of this file
43 * under either the RHEPL or the GPL.
45 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
49 /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
50 * bag to throw up into before reading this code. I looked through the jffs2
51 * code, the caching scheme is very elegant. I tried to keep the version
52 * for a bootloader as small and simple as possible. Instead of worring about
53 * unneccesary data copies, node scans, etc, I just optimized for the known
54 * common case, a kernel, which looks like:
55 * (1) most pages are 4096 bytes
56 * (2) version numbers are somewhat sorted in acsending order
57 * (3) multiple compressed blocks making up one page is uncommon
59 * So I create a linked list of decending version numbers (insertions at the
60 * head), and then for each page, walk down the list, until a matching page
61 * with 4096 bytes is found, and then decompress the watching pages in
67 * Adapted by Nye Liu <nyet@zumanetworks.com> and
68 * Rex Feany <rfeany@zumanetworks.com>
69 * on Jan/2002 for U-Boot.
71 * Clipped out all the non-1pass functions, cleaned up warnings,
72 * wrappers, etc. No major changes to the code.
73 * Please, he really means it when he said have a paper bag
74 * handy. We needed it ;).
79 * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
81 * - overhaul of the memory management. Removed much of the "paper-bagging"
82 * in that part of the code, fixed several bugs, now frees memory when
83 * partition is changed.
85 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
86 * was incorrect. Removed a bit of the paper-bagging as well.
87 * - removed double crc calculation for fragment headers in jffs2_private.h
89 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
90 * - spinning wheel now spins depending on how much memory has been scanned
91 * - lots of small changes all over the place to "improve" readability.
92 * - implemented fragment sorting to ensure that the newest data is copied
93 * if there are multiple copies of fragments for a certain file offset.
95 * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS.
96 * Sorting is done while adding fragments to the lists, which is more or less a
97 * bubble sort. This takes a lot of time, and is most probably not an issue if
98 * the boot filesystem is always mounted readonly.
100 * You should define it if the boot filesystem is mounted writable, and updates
101 * to the boot files are done by copying files to that filesystem.
104 * There's a big issue left: endianess is completely ignored in this code. Duh!
107 * You still should have paper bags at hand :-(. The code lacks more or less
108 * any comment, and is still arcane and difficult to read in places. As this
109 * is incompatible with any new code from the jffs2 maintainers anyway, it
110 * should probably be dumped and replaced by something like jffs2reader!
117 #include <linux/stat.h>
118 #include <linux/time.h>
120 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
122 #include <jffs2/jffs2.h>
123 #include <jffs2/jffs2_1pass.h>
125 #include "jffs2_private.h"
128 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
129 #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
131 /* Debugging switches */
132 #undef DEBUG_DIRENTS /* print directory entry list after scan */
133 #undef DEBUG_FRAGMENTS /* print fragment list after scan */
134 #undef DEBUG /* enable debugging messages */
138 # define DEBUGF(fmt,args...) printf(fmt ,##args)
140 # define DEBUGF(fmt,args...)
144 /* Compression names */
145 static char *compr_names
[] = {
156 static char spinner
[] = { '|', '/', '-', '\\' };
158 /* Memory management */
161 struct mem_block
*next
;
162 struct b_node nodes
[NODE_CHUNK
];
167 free_nodes(struct b_list
*list
)
169 while (list
->listMemBase
!= NULL
) {
170 struct mem_block
*next
= list
->listMemBase
->next
;
171 free( list
->listMemBase
);
172 list
->listMemBase
= next
;
176 static struct b_node
*
177 add_node(struct b_list
*list
)
180 struct mem_block
*memBase
;
183 memBase
= list
->listMemBase
;
185 index
= memBase
->index
;
187 putLabeledWord("add_node: index = ", index
);
188 putLabeledWord("add_node: memBase = ", list
->listMemBase
);
191 if (memBase
== NULL
|| index
>= NODE_CHUNK
) {
192 /* we need more space before we continue */
193 memBase
= mmalloc(sizeof(struct mem_block
));
194 if (memBase
== NULL
) {
195 putstr("add_node: malloc failed\n");
198 memBase
->next
= list
->listMemBase
;
201 putLabeledWord("add_node: alloced a new membase at ", *memBase
);
205 /* now we have room to add it. */
206 b
= &memBase
->nodes
[index
];
209 memBase
->index
= index
;
210 list
->listMemBase
= memBase
;
215 static struct b_node
*
216 insert_node(struct b_list
*list
, u32 offset
)
219 #ifdef CFG_JFFS2_SORT_FRAGMENTS
220 struct b_node
*b
, *prev
;
223 if (!(new = add_node(list
))) {
224 putstr("add_node failed!\r\n");
227 new->offset
= offset
;
229 #ifdef CFG_JFFS2_SORT_FRAGMENTS
230 if (list
->listTail
!= NULL
&& list
->listCompare(new, list
->listTail
))
231 prev
= list
->listTail
;
232 else if (list
->listLast
!= NULL
&& list
->listCompare(new, list
->listLast
))
233 prev
= list
->listLast
;
237 for (b
= (prev
? prev
->next
: list
->listHead
);
238 b
!= NULL
&& list
->listCompare(new, b
);
239 prev
= b
, b
= b
->next
) {
243 list
->listLast
= prev
;
250 list
->listHead
= new;
254 new->next
= (struct b_node
*) NULL
;
255 if (list
->listTail
!= NULL
) {
256 list
->listTail
->next
= new;
257 list
->listTail
= new;
259 list
->listTail
= list
->listHead
= new;
266 #ifdef CFG_JFFS2_SORT_FRAGMENTS
267 /* Sort data entries with the latest version last, so that if there
268 * is overlapping data the latest version will be used.
270 static int compare_inodes(struct b_node
*new, struct b_node
*old
)
272 struct jffs2_raw_inode
*jNew
= (struct jffs2_raw_inode
*)new->offset
;
273 struct jffs2_raw_inode
*jOld
= (struct jffs2_raw_inode
*)old
->offset
;
275 return jNew
->version
> jOld
->version
;
278 /* Sort directory entries so all entries in the same directory
279 * with the same name are grouped together, with the latest version
280 * last. This makes it easy to eliminate all but the latest version
281 * by marking the previous version dead by setting the inode to 0.
283 static int compare_dirents(struct b_node
*new, struct b_node
*old
)
285 struct jffs2_raw_dirent
*jNew
= (struct jffs2_raw_dirent
*)new->offset
;
286 struct jffs2_raw_dirent
*jOld
= (struct jffs2_raw_dirent
*)old
->offset
;
289 /* ascending sort by pino */
290 if (jNew
->pino
!= jOld
->pino
)
291 return jNew
->pino
> jOld
->pino
;
293 /* pino is the same, so use ascending sort by nsize, so
294 * we don't do strncmp unless we really must.
296 if (jNew
->nsize
!= jOld
->nsize
)
297 return jNew
->nsize
> jOld
->nsize
;
299 /* length is also the same, so use ascending sort by name
301 cmp
= strncmp(jNew
->name
, jOld
->name
, jNew
->nsize
);
305 /* we have duplicate names in this directory, so use ascending
308 if (jNew
->version
> jOld
->version
) {
309 /* since jNew is newer, we know jOld is not valid, so
310 * mark it with inode 0 and it will not be used
321 jffs2_scan_empty(u32 start_offset
, struct part_info
*part
)
323 char *max
= part
->offset
+ part
->size
- sizeof(struct jffs2_raw_inode
);
324 char *offset
= part
->offset
+ start_offset
;
326 while (offset
< max
&& *(u32
*)offset
== 0xFFFFFFFF) {
327 offset
+= sizeof(u32
);
328 /* return if spinning is due */
329 if (((u32
)offset
& ((1 << SPIN_BLKSIZE
)-1)) == 0) break;
332 return offset
- part
->offset
;
336 jffs_init_1pass_list(struct part_info
*part
)
340 if (part
->jffs2_priv
!= NULL
) {
341 pL
= (struct b_lists
*)part
->jffs2_priv
;
342 free_nodes(&pL
->frag
);
343 free_nodes(&pL
->dir
);
346 if (NULL
!= (part
->jffs2_priv
= malloc(sizeof(struct b_lists
)))) {
347 pL
= (struct b_lists
*)part
->jffs2_priv
;
349 memset(pL
, 0, sizeof(*pL
));
350 #ifdef CFG_JFFS2_SORT_FRAGMENTS
351 pL
->dir
.listCompare
= compare_dirents
;
352 pL
->frag
.listCompare
= compare_inodes
;
358 /* find the inode from the slashless name given a parent */
360 jffs2_1pass_read_inode(struct b_lists
*pL
, u32 inode
, char *dest
)
363 struct jffs2_raw_inode
*jNode
;
365 u32 latestVersion
= 0;
371 #ifdef CFG_JFFS2_SORT_FRAGMENTS
372 /* Find file size before loading any data, so fragments that
373 * start past the end of file can be ignored. A fragment
374 * that is partially in the file is loaded, so extra data may
375 * be loaded up to the next 4K boundary above the file size.
376 * This shouldn't cause trouble when loading kernel images, so
377 * we will live with it.
379 for (b
= pL
->frag
.listHead
; b
!= NULL
; b
= b
->next
) {
380 jNode
= (struct jffs2_raw_inode
*) (b
->offset
);
381 if ((inode
== jNode
->ino
)) {
382 /* get actual file length from the newest node */
383 if (jNode
->version
>= latestVersion
) {
384 totalSize
= jNode
->isize
;
385 latestVersion
= jNode
->version
;
391 for (b
= pL
->frag
.listHead
; b
!= NULL
; b
= b
->next
) {
392 jNode
= (struct jffs2_raw_inode
*) (b
->offset
);
393 if ((inode
== jNode
->ino
)) {
395 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode
->totlen
);
396 putLabeledWord("read_inode: inode = ", jNode
->ino
);
397 putLabeledWord("read_inode: version = ", jNode
->version
);
398 putLabeledWord("read_inode: isize = ", jNode
->isize
);
399 putLabeledWord("read_inode: offset = ", jNode
->offset
);
400 putLabeledWord("read_inode: csize = ", jNode
->csize
);
401 putLabeledWord("read_inode: dsize = ", jNode
->dsize
);
402 putLabeledWord("read_inode: compr = ", jNode
->compr
);
403 putLabeledWord("read_inode: usercompr = ", jNode
->usercompr
);
404 putLabeledWord("read_inode: flags = ", jNode
->flags
);
407 #ifndef CFG_JFFS2_SORT_FRAGMENTS
408 /* get actual file length from the newest node */
409 if (jNode
->version
>= latestVersion
) {
410 totalSize
= jNode
->isize
;
411 latestVersion
= jNode
->version
;
416 src
= ((char *) jNode
) + sizeof(struct jffs2_raw_inode
);
417 /* ignore data behind latest known EOF */
418 if (jNode
->offset
> totalSize
)
421 lDest
= (char *) (dest
+ jNode
->offset
);
423 putLabeledWord("read_inode: src = ", src
);
424 putLabeledWord("read_inode: dest = ", lDest
);
426 switch (jNode
->compr
) {
427 case JFFS2_COMPR_NONE
:
428 ret
= (unsigned long) ldr_memcpy(lDest
, src
, jNode
->dsize
);
430 case JFFS2_COMPR_ZERO
:
432 for (i
= 0; i
< jNode
->dsize
; i
++)
435 case JFFS2_COMPR_RTIME
:
437 rtime_decompress(src
, lDest
, jNode
->csize
, jNode
->dsize
);
439 case JFFS2_COMPR_DYNRUBIN
:
440 /* this is slow but it works */
442 dynrubin_decompress(src
, lDest
, jNode
->csize
, jNode
->dsize
);
444 case JFFS2_COMPR_ZLIB
:
445 ret
= zlib_decompress(src
, lDest
, jNode
->csize
, jNode
->dsize
);
449 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode
->compr
);
456 putLabeledWord("read_inode: totalSize = ", totalSize
);
457 putLabeledWord("read_inode: compr ret = ", ret
);
464 putLabeledWord("read_inode: returning = ", totalSize
);
469 /* find the inode from the slashless name given a parent */
471 jffs2_1pass_find_inode(struct b_lists
* pL
, const char *name
, u32 pino
)
474 struct jffs2_raw_dirent
*jDir
;
480 /* name is assumed slash free */
484 /* we need to search all and return the inode with the highest version */
485 for(b
= pL
->dir
.listHead
; b
; b
= b
->next
, counter
++) {
486 jDir
= (struct jffs2_raw_dirent
*) (b
->offset
);
487 if ((pino
== jDir
->pino
) && (len
== jDir
->nsize
) &&
488 (jDir
->ino
) && /* 0 for unlink */
489 (!strncmp(jDir
->name
, name
, len
))) { /* a match */
490 if (jDir
->version
< version
)
493 if (jDir
->version
== version
&& inode
!= 0) {
494 /* I'm pretty sure this isn't legal */
495 putstr(" ** ERROR ** ");
496 putnstr(jDir
->name
, jDir
->nsize
);
497 putLabeledWord(" has dup version =", version
);
500 version
= jDir
->version
;
503 putstr("\r\nfind_inode:p&l ->");
504 putnstr(jDir
->name
, jDir
->nsize
);
506 putLabeledWord("pino = ", jDir
->pino
);
507 putLabeledWord("nsize = ", jDir
->nsize
);
508 putLabeledWord("b = ", (u32
) b
);
509 putLabeledWord("counter = ", counter
);
515 static char *mkmodestr(unsigned long mode
, char *str
)
517 static const char *l
= "xwr";
521 switch (mode
& S_IFMT
) {
522 case S_IFDIR
: str
[0] = 'd'; break;
523 case S_IFBLK
: str
[0] = 'b'; break;
524 case S_IFCHR
: str
[0] = 'c'; break;
525 case S_IFIFO
: str
[0] = 'f'; break;
526 case S_IFLNK
: str
[0] = 'l'; break;
527 case S_IFSOCK
: str
[0] = 's'; break;
528 case S_IFREG
: str
[0] = '-'; break;
529 default: str
[0] = '?';
532 for(i
= 0; i
< 9; i
++) {
534 str
[9-i
] = (mode
& mask
)?c
:'-';
538 if(mode
& S_ISUID
) str
[3] = (mode
& S_IXUSR
)?'s':'S';
539 if(mode
& S_ISGID
) str
[6] = (mode
& S_IXGRP
)?'s':'S';
540 if(mode
& S_ISVTX
) str
[9] = (mode
& S_IXOTH
)?'t':'T';
545 static inline void dump_stat(struct stat
*st
, const char *name
)
550 if (st
->st_mtime
== (time_t)(-1)) /* some ctimes really hate -1 */
553 ctime_r(&st
->st_mtime
, s
/*,64*/); /* newlib ctime doesn't have buflen */
555 if ((p
= strchr(s
,'\n')) != NULL
) *p
= '\0';
556 if ((p
= strchr(s
,'\r')) != NULL
) *p
= '\0';
559 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
560 st->st_size, s, name);
563 printf(" %s %8ld %s %s", mkmodestr(st
->st_mode
,str
), st
->st_size
, s
, name
);
566 static inline u32
dump_inode(struct b_lists
* pL
, struct jffs2_raw_dirent
*d
, struct jffs2_raw_inode
*i
)
571 if(!d
|| !i
) return -1;
573 strncpy(fname
, d
->name
, d
->nsize
);
574 fname
[d
->nsize
] = '\0';
576 memset(&st
,0,sizeof(st
));
578 st
.st_mtime
= i
->mtime
;
579 st
.st_mode
= i
->mode
;
582 /* neither dsize nor isize help us.. do it the long way */
583 st
.st_size
= jffs2_1pass_read_inode(pL
, i
->ino
, NULL
);
585 dump_stat(&st
, fname
);
587 if (d
->type
== DT_LNK
) {
588 unsigned char *src
= (unsigned char *) (&i
[1]);
590 putnstr(src
, (int)i
->dsize
);
598 /* list inodes with the given pino */
600 jffs2_1pass_list_inodes(struct b_lists
* pL
, u32 pino
)
603 struct jffs2_raw_dirent
*jDir
;
605 for (b
= pL
->dir
.listHead
; b
; b
= b
->next
) {
606 jDir
= (struct jffs2_raw_dirent
*) (b
->offset
);
607 if ((pino
== jDir
->pino
) && (jDir
->ino
)) { /* ino=0 -> unlink */
609 struct jffs2_raw_inode
*jNode
, *i
= NULL
;
610 struct b_node
*b2
= pL
->frag
.listHead
;
613 jNode
= (struct jffs2_raw_inode
*) (b2
->offset
);
614 if (jNode
->ino
== jDir
->ino
615 && jNode
->version
>= i_version
)
620 dump_inode(pL
, jDir
, i
);
627 jffs2_1pass_search_inode(struct b_lists
* pL
, const char *fname
, u32 pino
)
631 char working_tmp
[256];
634 /* discard any leading slash */
636 while (fname
[i
] == '/')
638 strcpy(tmp
, &fname
[i
]);
640 while ((c
= (char *) strchr(tmp
, '/'))) /* we are still dired searching */
642 strncpy(working_tmp
, tmp
, c
- tmp
);
643 working_tmp
[c
- tmp
] = '\0';
645 putstr("search_inode: tmp = ");
648 putstr("search_inode: wtmp = ");
651 putstr("search_inode: c = ");
655 for (i
= 0; i
< strlen(c
) - 1; i
++)
659 putstr("search_inode: post tmp = ");
664 if (!(pino
= jffs2_1pass_find_inode(pL
, working_tmp
, pino
))) {
665 putstr("find_inode failed for name=");
671 /* this is for the bare filename, directories have already been mapped */
672 if (!(pino
= jffs2_1pass_find_inode(pL
, tmp
, pino
))) {
673 putstr("find_inode failed for name=");
683 jffs2_1pass_resolve_inode(struct b_lists
* pL
, u32 ino
)
687 struct jffs2_raw_dirent
*jDir
;
688 struct jffs2_raw_inode
*jNode
;
689 struct jffs2_raw_dirent
*jDirFound
= NULL
;
695 /* we need to search all and return the inode with the highest version */
696 for(b
= pL
->dir
.listHead
; b
; b
= b
->next
) {
697 jDir
= (struct jffs2_raw_dirent
*) (b
->offset
);
698 if (ino
== jDir
->ino
) {
699 if (jDir
->version
< version
)
702 if (jDir
->version
== version
&& jDirFound
) {
703 /* I'm pretty sure this isn't legal */
704 putstr(" ** ERROR ** ");
705 putnstr(jDir
->name
, jDir
->nsize
);
706 putLabeledWord(" has dup version (resolve) = ",
711 version
= jDir
->version
;
714 /* now we found the right entry again. (shoulda returned inode*) */
715 if (jDirFound
->type
!= DT_LNK
)
716 return jDirFound
->ino
;
718 /* it's a soft link so we follow it again. */
719 b2
= pL
->frag
.listHead
;
721 jNode
= (struct jffs2_raw_inode
*) (b2
->offset
);
722 if (jNode
->ino
== jDirFound
->ino
) {
723 src
= (unsigned char *) (b2
->offset
+ sizeof(struct jffs2_raw_inode
));
726 putLabeledWord("\t\t dsize = ", jNode
->dsize
);
727 putstr("\t\t target = ");
728 putnstr(src
, jNode
->dsize
);
731 strncpy(tmp
, src
, jNode
->dsize
);
732 tmp
[jNode
->dsize
] = '\0';
737 /* ok so the name of the new file to find is in tmp */
738 /* if it starts with a slash it is root based else shared dirs */
742 pino
= jDirFound
->pino
;
744 return jffs2_1pass_search_inode(pL
, tmp
, pino
);
748 jffs2_1pass_search_list_inodes(struct b_lists
* pL
, const char *fname
, u32 pino
)
752 char working_tmp
[256];
755 /* discard any leading slash */
757 while (fname
[i
] == '/')
759 strcpy(tmp
, &fname
[i
]);
760 working_tmp
[0] = '\0';
761 while ((c
= (char *) strchr(tmp
, '/'))) /* we are still dired searching */
763 strncpy(working_tmp
, tmp
, c
- tmp
);
764 working_tmp
[c
- tmp
] = '\0';
765 for (i
= 0; i
< strlen(c
) - 1; i
++)
768 /* only a failure if we arent looking at top level */
769 if (!(pino
= jffs2_1pass_find_inode(pL
, working_tmp
, pino
)) &&
771 putstr("find_inode failed for name=");
778 if (tmp
[0] && !(pino
= jffs2_1pass_find_inode(pL
, tmp
, pino
))) {
779 putstr("find_inode failed for name=");
784 /* this is for the bare filename, directories have already been mapped */
785 if (!(pino
= jffs2_1pass_list_inodes(pL
, pino
))) {
786 putstr("find_inode failed for name=");
796 jffs2_1pass_rescan_needed(struct part_info
*part
)
799 struct jffs2_unknown_node
*node
;
800 struct b_lists
*pL
= (struct b_lists
*)part
->jffs2_priv
;
802 if (part
->jffs2_priv
== 0){
803 DEBUGF ("rescan: First time in use\n");
806 /* if we have no list, we need to rescan */
807 if (pL
->frag
.listCount
== 0) {
808 DEBUGF ("rescan: fraglist zero\n");
812 /* or if we are scanning a new partition */
813 if (pL
->partOffset
!= part
->offset
) {
814 DEBUGF ("rescan: different partition\n");
817 /* but suppose someone reflashed a partition at the same offset... */
818 b
= pL
->dir
.listHead
;
820 node
= (struct jffs2_unknown_node
*) (b
->offset
);
821 if (node
->nodetype
!= JFFS2_NODETYPE_DIRENT
) {
822 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
823 (unsigned long) b
->offset
);
831 #ifdef DEBUG_FRAGMENTS
833 dump_fragments(struct b_lists
*pL
)
836 struct jffs2_raw_inode
*jNode
;
838 putstr("\r\n\r\n******The fragment Entries******\r\n");
839 b
= pL
->frag
.listHead
;
841 jNode
= (struct jffs2_raw_inode
*) (b
->offset
);
842 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b
->offset
);
843 putLabeledWord("\tbuild_list: totlen = ", jNode
->totlen
);
844 putLabeledWord("\tbuild_list: inode = ", jNode
->ino
);
845 putLabeledWord("\tbuild_list: version = ", jNode
->version
);
846 putLabeledWord("\tbuild_list: isize = ", jNode
->isize
);
847 putLabeledWord("\tbuild_list: atime = ", jNode
->atime
);
848 putLabeledWord("\tbuild_list: offset = ", jNode
->offset
);
849 putLabeledWord("\tbuild_list: csize = ", jNode
->csize
);
850 putLabeledWord("\tbuild_list: dsize = ", jNode
->dsize
);
851 putLabeledWord("\tbuild_list: compr = ", jNode
->compr
);
852 putLabeledWord("\tbuild_list: usercompr = ", jNode
->usercompr
);
853 putLabeledWord("\tbuild_list: flags = ", jNode
->flags
);
854 putLabeledWord("\tbuild_list: offset = ", b
->offset
); /* FIXME: ? [RS] */
862 dump_dirents(struct b_lists
*pL
)
865 struct jffs2_raw_dirent
*jDir
;
867 putstr("\r\n\r\n******The directory Entries******\r\n");
868 b
= pL
->dir
.listHead
;
870 jDir
= (struct jffs2_raw_dirent
*) (b
->offset
);
872 putnstr(jDir
->name
, jDir
->nsize
);
873 putLabeledWord("\r\n\tbuild_list: magic = ", jDir
->magic
);
874 putLabeledWord("\tbuild_list: nodetype = ", jDir
->nodetype
);
875 putLabeledWord("\tbuild_list: hdr_crc = ", jDir
->hdr_crc
);
876 putLabeledWord("\tbuild_list: pino = ", jDir
->pino
);
877 putLabeledWord("\tbuild_list: version = ", jDir
->version
);
878 putLabeledWord("\tbuild_list: ino = ", jDir
->ino
);
879 putLabeledWord("\tbuild_list: mctime = ", jDir
->mctime
);
880 putLabeledWord("\tbuild_list: nsize = ", jDir
->nsize
);
881 putLabeledWord("\tbuild_list: type = ", jDir
->type
);
882 putLabeledWord("\tbuild_list: node_crc = ", jDir
->node_crc
);
883 putLabeledWord("\tbuild_list: name_crc = ", jDir
->name_crc
);
884 putLabeledWord("\tbuild_list: offset = ", b
->offset
); /* FIXME: ? [RS] */
891 jffs2_1pass_build_lists(struct part_info
* part
)
894 struct jffs2_unknown_node
*node
;
895 u32 offset
, oldoffset
= 0;
896 u32 max
= part
->size
- sizeof(struct jffs2_raw_inode
);
902 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
903 /* jffs2 list building enterprise nope. in newer versions the overhead is */
904 /* only about 5 %. not enough to inconvenience people for. */
907 /* if we are building a list we need to refresh the cache. */
908 jffs_init_1pass_list(part
);
909 pL
= (struct b_lists
*)part
->jffs2_priv
;
910 pL
->partOffset
= part
->offset
;
912 printf("Scanning JFFS2 FS: ");
914 /* start at the beginning of the partition */
915 while (offset
< max
) {
916 if ((oldoffset
>> SPIN_BLKSIZE
) != (offset
>> SPIN_BLKSIZE
)) {
917 printf("\b\b%c ", spinner
[counter
++ % sizeof(spinner
)]);
921 node
= (struct jffs2_unknown_node
*) (part
->offset
+ offset
);
922 if (node
->magic
== JFFS2_MAGIC_BITMASK
&& hdr_crc(node
)) {
923 /* if its a fragment add it */
924 if (node
->nodetype
== JFFS2_NODETYPE_INODE
&&
925 inode_crc((struct jffs2_raw_inode
*) node
)) {
926 if (insert_node(&pL
->frag
, (u32
) part
->offset
+
929 } else if (node
->nodetype
== JFFS2_NODETYPE_DIRENT
&&
930 dirent_crc((struct jffs2_raw_dirent
*) node
) &&
931 dirent_name_crc((struct jffs2_raw_dirent
*) node
)) {
932 if (! (counterN
%100))
934 if (insert_node(&pL
->dir
, (u32
) part
->offset
+
938 } else if (node
->nodetype
== JFFS2_NODETYPE_CLEANMARKER
) {
939 if (node
->totlen
!= sizeof(struct jffs2_unknown_node
))
940 printf("OOPS Cleanmarker has bad size "
941 "%d != %d\n", node
->totlen
,
942 sizeof(struct jffs2_unknown_node
));
943 } else if (node
->nodetype
== JFFS2_NODETYPE_PADDING
) {
944 if (node
->totlen
< sizeof(struct jffs2_unknown_node
))
945 printf("OOPS Padding has bad size "
946 "%d < %d\n", node
->totlen
,
947 sizeof(struct jffs2_unknown_node
));
949 printf("Unknown node type: %x len %d "
950 "offset 0x%x\n", node
->nodetype
,
951 node
->totlen
, offset
);
953 offset
+= ((node
->totlen
+ 3) & ~3);
955 } else if (node
->magic
== JFFS2_EMPTY_BITMASK
&&
956 node
->nodetype
== JFFS2_EMPTY_BITMASK
) {
957 offset
= jffs2_scan_empty(offset
, part
);
958 } else { /* if we know nothing, we just step and look. */
962 /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
966 putstr("\b\b done.\r\n"); /* close off the dots */
967 /* turn the lcd back on. */
971 putLabeledWord("dir entries = ", pL
->dir
.listCount
);
972 putLabeledWord("frag entries = ", pL
->frag
.listCount
);
973 putLabeledWord("+4 increments = ", counter4
);
974 putLabeledWord("+file_offset increments = ", counterF
);
982 #ifdef DEBUG_FRAGMENTS
986 /* give visual feedback that we are done scanning the flash */
987 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
993 jffs2_1pass_fill_info(struct b_lists
* pL
, struct b_jffs2_info
* piL
)
996 struct jffs2_raw_inode
*jNode
;
999 for (i
= 0; i
< JFFS2_NUM_COMPR
; i
++) {
1000 piL
->compr_info
[i
].num_frags
= 0;
1001 piL
->compr_info
[i
].compr_sum
= 0;
1002 piL
->compr_info
[i
].decompr_sum
= 0;
1005 b
= pL
->frag
.listHead
;
1007 jNode
= (struct jffs2_raw_inode
*) (b
->offset
);
1008 if (jNode
->compr
< JFFS2_NUM_COMPR
) {
1009 piL
->compr_info
[jNode
->compr
].num_frags
++;
1010 piL
->compr_info
[jNode
->compr
].compr_sum
+= jNode
->csize
;
1011 piL
->compr_info
[jNode
->compr
].decompr_sum
+= jNode
->dsize
;
1019 static struct b_lists
*
1020 jffs2_get_list(struct part_info
* part
, const char *who
)
1022 if (jffs2_1pass_rescan_needed(part
)) {
1023 if (!jffs2_1pass_build_lists(part
)) {
1024 printf("%s: Failed to scan JFFSv2 file structure\n", who
);
1028 return (struct b_lists
*)part
->jffs2_priv
;
1032 /* Print directory / file contents */
1034 jffs2_1pass_ls(struct part_info
* part
, const char *fname
)
1040 if (! (pl
= jffs2_get_list(part
, "ls")))
1043 if (! (inode
= jffs2_1pass_search_list_inodes(pl
, fname
, 1))) {
1044 putstr("ls: Failed to scan jffs2 file structure\r\n");
1050 putLabeledWord("found file at inode = ", inode
);
1051 putLabeledWord("read_inode returns = ", ret
);
1058 /* Load a file from flash into memory. fname can be a full path */
1060 jffs2_1pass_load(char *dest
, struct part_info
* part
, const char *fname
)
1067 if (! (pl
= jffs2_get_list(part
, "load")))
1070 if (! (inode
= jffs2_1pass_search_inode(pl
, fname
, 1))) {
1071 putstr("load: Failed to find inode\r\n");
1075 /* Resolve symlinks */
1076 if (! (inode
= jffs2_1pass_resolve_inode(pl
, inode
))) {
1077 putstr("load: Failed to resolve inode structure\r\n");
1081 if ((ret
= jffs2_1pass_read_inode(pl
, inode
, dest
)) < 0) {
1082 putstr("load: Failed to read inode\r\n");
1086 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname
,
1087 (unsigned long) dest
, ret
);
1091 /* Return information about the fs on this partition */
1093 jffs2_1pass_info(struct part_info
* part
)
1095 struct b_jffs2_info info
;
1099 if (! (pl
= jffs2_get_list(part
, "info")))
1102 jffs2_1pass_fill_info(pl
, &info
);
1103 for (i
= 0; i
< JFFS2_NUM_COMPR
; i
++) {
1104 printf("Compression: %s\n", compr_names
[i
]);
1105 printf("\tfrag count: %d\n", info
.compr_info
[i
].num_frags
);
1106 printf("\tcompressed sum: %d\n", info
.compr_info
[i
].compr_sum
);
1107 printf("\tuncompressed sum: %d\n", info
.compr_info
[i
].decompr_sum
);
1112 #endif /* CFG_CMD_JFFS2 */