1 /* vi: set sw=4 ts=4: */
3 -------------------------------------------------------------------------
5 * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
6 * Copyright: Copyright (C) 2001, Russ Dill
8 * Description: Module to load kernel from jffs2
9 *-----------------------------------------------------------------------*/
11 * some portions of this code are taken from jffs2, and as such, the
12 * following copyright notice is included.
14 * JFFS2 -- Journalling Flash File System, Version 2.
16 * Copyright (C) 2001 Red Hat, Inc.
20 * The original JFFS, from which the design for JFFS2 was derived,
21 * was designed and implemented by Axis Communications AB.
23 * The contents of this file are subject to the Red Hat eCos Public
24 * License Version 1.1 (the "Licence"); you may not use this file
25 * except in compliance with the Licence. You may obtain a copy of
26 * the Licence at http://www.redhat.com/
28 * Software distributed under the Licence is distributed on an "AS IS"
29 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
30 * See the Licence for the specific language governing rights and
31 * limitations under the Licence.
33 * The Original Code is JFFS2 - Journalling Flash File System, version 2
35 * Alternatively, the contents of this file may be used under the
36 * terms of the GNU General Public License version 2 (the "GPL"), in
37 * which case the provisions of the GPL are applicable instead of the
38 * above. If you wish to allow the use of your version of this file
39 * only under the terms of the GPL and not to allow others to use your
40 * version of this file under the RHEPL, indicate your decision by
41 * deleting the provisions above and replace them with the notice and
42 * other provisions required by the GPL. If you do not delete the
43 * provisions above, a recipient may use your version of this file
44 * under either the RHEPL or the GPL.
46 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
50 /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
51 * bag to throw up into before reading this code. I looked through the jffs2
52 * code, the caching scheme is very elegant. I tried to keep the version
53 * for a bootloader as small and simple as possible. Instead of worring about
54 * unneccesary data copies, node scans, etc, I just optimized for the known
55 * common case, a kernel, which looks like:
56 * (1) most pages are 4096 bytes
57 * (2) version numbers are somewhat sorted in acsending order
58 * (3) multiple compressed blocks making up one page is uncommon
60 * So I create a linked list of decending version numbers (insertions at the
61 * head), and then for each page, walk down the list, until a matching page
62 * with 4096 bytes is found, and then decompress the watching pages in
70 * on Jan/2002 for U-Boot.
72 * Clipped out all the non-1pass functions, cleaned up warnings,
73 * wrappers, etc. No major changes to the code.
74 * Please, he really means it when he said have a paper bag
75 * handy. We needed it ;).
82 * - overhaul of the memory management. Removed much of the "paper-bagging"
83 * in that part of the code, fixed several bugs, now frees memory when
84 * partition is changed.
86 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
87 * was incorrect. Removed a bit of the paper-bagging as well.
88 * - removed double crc calculation for fragment headers in jffs2_private.h
90 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
91 * - spinning wheel now spins depending on how much memory has been scanned
92 * - lots of small changes all over the place to "improve" readability.
93 * - implemented fragment sorting to ensure that the newest data is copied
94 * if there are multiple copies of fragments for a certain file offset.
96 * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS.
97 * Sorting is done while adding fragments to the lists, which is more or less a
98 * bubble sort. This takes a lot of time, and is most probably not an issue if
99 * the boot filesystem is always mounted readonly.
101 * You should define it if the boot filesystem is mounted writable, and updates
102 * to the boot files are done by copying files to that filesystem.
105 * There's a big issue left: endianess is completely ignored in this code. Duh!
108 * You still should have paper bags at hand :-(. The code lacks more or less
109 * any comment, and is still arcane and difficult to read in places. As this
110 * is incompatible with any new code from the jffs2 maintainers anyway, it
111 * should probably be dumped and replaced by something like jffs2reader!
118 #include <linux/stat.h>
119 #include <linux/time.h>
121 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
123 #include <jffs2/jffs2.h>
124 #include <jffs2/jffs2_1pass.h>
126 #include "jffs2_private.h"
129 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
130 #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
132 /* Debugging switches */
133 #undef DEBUG_DIRENTS /* print directory entry list after scan */
134 #undef DEBUG_FRAGMENTS /* print fragment list after scan */
135 #undef DEBUG /* enable debugging messages */
139 # define DEBUGF(fmt,args...) printf(fmt ,##args)
141 # define DEBUGF(fmt,args...)
145 /* Compression names */
146 static char *compr_names[] = {
157 static char spinner[] = { '|', '/', '-', '\\' };
159 /* Memory management */
162 struct mem_block *next;
163 struct b_node nodes[NODE_CHUNK];
168 free_nodes(struct b_list *list)
170 while (list->listMemBase != NULL) {
171 struct mem_block *next = list->listMemBase->next;
172 free( list->listMemBase );
173 list->listMemBase = next;
177 static struct b_node *
178 add_node(struct b_list *list)
181 struct mem_block *memBase;
184 memBase = list->listMemBase;
186 index = memBase->index;
188 putLabeledWord("add_node: index = ", index);
189 putLabeledWord("add_node: memBase = ", list->listMemBase);
192 if (memBase == NULL || index >= NODE_CHUNK) {
193 /* we need more space before we continue */
194 memBase = mmalloc(sizeof(struct mem_block));
195 if (memBase == NULL) {
196 putstr("add_node: malloc failed\n");
199 memBase->next = list->listMemBase;
202 putLabeledWord("add_node: alloced a new membase at ", *memBase);
206 /* now we have room to add it. */
207 b = &memBase->nodes[index];
210 memBase->index = index;
211 list->listMemBase = memBase;
216 static struct b_node *
217 insert_node(struct b_list *list, u32 offset)
220 #ifdef CFG_JFFS2_SORT_FRAGMENTS
221 struct b_node *b, *prev;
224 if (!(new = add_node(list))) {
225 putstr("add_node failed!\r\n");
228 new->offset = offset;
230 #ifdef CFG_JFFS2_SORT_FRAGMENTS
231 if (list->listTail != NULL && list->listCompare(new, list->listTail))
232 prev = list->listTail;
233 else if (list->listLast != NULL && list->listCompare(new, list->listLast))
234 prev = list->listLast;
238 for (b = (prev ? prev->next : list->listHead);
239 b != NULL && list->listCompare(new, b);
240 prev = b, b = b->next) {
244 list->listLast = prev;
251 list->listHead = new;
255 new->next = (struct b_node *) NULL;
256 if (list->listTail != NULL) {
257 list->listTail->next = new;
258 list->listTail = new;
260 list->listTail = list->listHead = new;
267 #ifdef CFG_JFFS2_SORT_FRAGMENTS
268 static int compare_inodes(struct b_node *new, struct b_node *old)
270 struct jffs2_raw_inode *jNew = (struct jffs2_raw_inode *)new->offset;
271 struct jffs2_raw_inode *jOld = (struct jffs2_raw_inode *)old->offset;
273 return jNew->version < jOld->version;
276 static int compare_dirents(struct b_node *new, struct b_node *old)
278 struct jffs2_raw_dirent *jNew = (struct jffs2_raw_dirent *)new->offset;
279 struct jffs2_raw_dirent *jOld = (struct jffs2_raw_dirent *)old->offset;
281 return jNew->version > jOld->version;
286 jffs2_scan_empty(u32 start_offset, struct part_info *part)
288 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
289 char *offset = part->offset + start_offset;
291 while (offset < max && *(u32 *)offset == 0xFFFFFFFF) {
292 offset += sizeof(u32);
293 /* return if spinning is due */
294 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
297 return offset - part->offset;
301 jffs_init_1pass_list(struct part_info *part)
305 if (part->jffs2_priv != NULL) {
306 pL = (struct b_lists *)part->jffs2_priv;
307 free_nodes(&pL->frag);
308 free_nodes(&pL->dir);
311 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
312 pL = (struct b_lists *)part->jffs2_priv;
314 memset(pL, 0, sizeof(*pL));
315 #ifdef CFG_JFFS2_SORT_FRAGMENTS
316 pL->dir.listCompare = compare_dirents;
317 pL->frag.listCompare = compare_inodes;
323 /* find the inode from the slashless name given a parent */
325 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
328 struct jffs2_raw_inode *jNode;
330 u16 latestVersion = 0;
337 for (b = pL->frag.listHead; b != NULL; b = b->next) {
338 jNode = (struct jffs2_raw_inode *) (b->offset);
339 if ((inode == jNode->ino)) {
341 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
342 putLabeledWord("read_inode: inode = ", jNode->ino);
343 putLabeledWord("read_inode: version = ", jNode->version);
344 putLabeledWord("read_inode: isize = ", jNode->isize);
345 putLabeledWord("read_inode: offset = ", jNode->offset);
346 putLabeledWord("read_inode: csize = ", jNode->csize);
347 putLabeledWord("read_inode: dsize = ", jNode->dsize);
348 putLabeledWord("read_inode: compr = ", jNode->compr);
349 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
350 putLabeledWord("read_inode: flags = ", jNode->flags);
352 /* get actual file length from the newest node */
353 if (jNode->version >= latestVersion) {
354 totalSize = jNode->isize;
355 latestVersion = jNode->version;
359 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
360 /* ignore data behind latest known EOF */
361 if (jNode->offset > totalSize)
364 lDest = (char *) (dest + jNode->offset);
366 putLabeledWord("read_inode: src = ", src);
367 putLabeledWord("read_inode: dest = ", lDest);
369 switch (jNode->compr) {
370 case JFFS2_COMPR_NONE:
371 ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
373 case JFFS2_COMPR_ZERO:
375 for (i = 0; i < jNode->dsize; i++)
378 case JFFS2_COMPR_RTIME:
380 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
382 case JFFS2_COMPR_DYNRUBIN:
383 /* this is slow but it works */
385 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
387 case JFFS2_COMPR_ZLIB:
388 ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
392 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
399 putLabeledWord("read_inode: totalSize = ", totalSize);
400 putLabeledWord("read_inode: compr ret = ", ret);
407 putLabeledWord("read_inode: returning = ", totalSize);
412 /* find the inode from the slashless name given a parent */
414 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
417 struct jffs2_raw_dirent *jDir;
423 /* name is assumed slash free */
427 /* we need to search all and return the inode with the highest version */
428 for(b = pL->dir.listHead; b; b = b->next, counter++) {
429 jDir = (struct jffs2_raw_dirent *) (b->offset);
430 if ((pino == jDir->pino) && (len == jDir->nsize) &&
431 (jDir->ino) && /* 0 for unlink */
432 (!strncmp(jDir->name, name, len))) { /* a match */
433 if (jDir->version < version) continue;
435 if(jDir->version == 0) {
437 putstr(" ** WARNING ** ");
438 putnstr(jDir->name, jDir->nsize);
439 putstr(" is version 0 (in find, ignoring)\r\n");
440 } else if(jDir->version == version) {
441 /* Im pretty sure this isn't ... */
442 putstr(" ** ERROR ** ");
443 putnstr(jDir->name, jDir->nsize);
444 putLabeledWord(" has dup version =", version);
447 version = jDir->version;
450 putstr("\r\nfind_inode:p&l ->");
451 putnstr(jDir->name, jDir->nsize);
453 putLabeledWord("pino = ", jDir->pino);
454 putLabeledWord("nsize = ", jDir->nsize);
455 putLabeledWord("b = ", (u32) b);
456 putLabeledWord("counter = ", counter);
462 static char *mkmodestr(unsigned long mode, char *str)
464 static const char *l = "xwr";
468 switch (mode & S_IFMT) {
469 case S_IFDIR: str[0] = 'd'; break;
470 case S_IFBLK: str[0] = 'b'; break;
471 case S_IFCHR: str[0] = 'c'; break;
472 case S_IFIFO: str[0] = 'f'; break;
473 case S_IFLNK: str[0] = 'l'; break;
474 case S_IFSOCK: str[0] = 's'; break;
475 case S_IFREG: str[0] = '-'; break;
476 default: str[0] = '?';
479 for(i = 0; i < 9; i++) {
481 str[9-i] = (mode & mask)?c:'-';
485 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
486 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
487 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
492 static inline void dump_stat(struct stat *st, const char *name)
497 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
500 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
502 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
503 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
506 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
507 st->st_size, s, name);
510 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
513 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
518 if(!d || !i) return -1;
520 strncpy(fname, d->name, d->nsize);
521 fname[d->nsize] = '\0';
523 memset(&st,0,sizeof(st));
525 st.st_mtime = i->mtime;
526 st.st_mode = i->mode;
529 /* neither dsize nor isize help us.. do it the long way */
530 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
532 dump_stat(&st, fname);
534 if (d->type == DT_LNK) {
535 unsigned char *src = (unsigned char *) (&i[1]);
537 putnstr(src, (int)i->dsize);
545 /* list inodes with the given pino */
547 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
550 struct jffs2_raw_dirent *jDir;
552 for (b = pL->dir.listHead; b; b = b->next) {
553 jDir = (struct jffs2_raw_dirent *) (b->offset);
554 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
556 struct jffs2_raw_inode *jNode, *i = NULL;
557 struct b_node *b2 = pL->frag.listHead;
560 jNode = (struct jffs2_raw_inode *) (b2->offset);
561 if (jNode->ino == jDir->ino
562 && jNode->version >= i_version)
567 dump_inode(pL, jDir, i);
574 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
578 char working_tmp[256];
581 /* discard any leading slash */
583 while (fname[i] == '/')
585 strcpy(tmp, &fname[i]);
587 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
589 strncpy(working_tmp, tmp, c - tmp);
590 working_tmp[c - tmp] = '\0';
592 putstr("search_inode: tmp = ");
595 putstr("search_inode: wtmp = ");
598 putstr("search_inode: c = ");
602 for (i = 0; i < strlen(c) - 1; i++)
606 putstr("search_inode: post tmp = ");
611 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
612 putstr("find_inode failed for name=");
618 /* this is for the bare filename, directories have already been mapped */
619 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
620 putstr("find_inode failed for name=");
630 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
634 struct jffs2_raw_dirent *jDir;
635 struct jffs2_raw_inode *jNode;
636 struct jffs2_raw_dirent *jDirFound = NULL;
642 /* we need to search all and return the inode with the highest version */
643 for(b = pL->dir.listHead; b; b = b->next) {
644 jDir = (struct jffs2_raw_dirent *) (b->offset);
645 if (ino == jDir->ino) {
646 if(jDir->version < version) continue;
648 if(jDir->version == 0) {
650 putstr(" ** WARNING ** ");
651 putnstr(jDir->name, jDir->nsize);
652 putstr(" is version 0 (in resolve, ignoring)\r\n");
653 } else if(jDir->version == version) {
654 /* Im pretty sure this isn't ... */
655 putstr(" ** ERROR ** ");
656 putnstr(jDir->name, jDir->nsize);
657 putLabeledWord(" has dup version (resolve) = ",
662 version = jDir->version;
665 /* now we found the right entry again. (shoulda returned inode*) */
666 if (jDirFound->type != DT_LNK)
667 return jDirFound->ino;
669 /* it's a soft link so we follow it again. */
670 b2 = pL->frag.listHead;
672 jNode = (struct jffs2_raw_inode *) (b2->offset);
673 if (jNode->ino == jDirFound->ino) {
674 src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode));
677 putLabeledWord("\t\t dsize = ", jNode->dsize);
678 putstr("\t\t target = ");
679 putnstr(src, jNode->dsize);
682 strncpy(tmp, src, jNode->dsize);
683 tmp[jNode->dsize] = '\0';
688 /* ok so the name of the new file to find is in tmp */
689 /* if it starts with a slash it is root based else shared dirs */
693 pino = jDirFound->pino;
695 return jffs2_1pass_search_inode(pL, tmp, pino);
699 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
703 char working_tmp[256];
706 /* discard any leading slash */
708 while (fname[i] == '/')
710 strcpy(tmp, &fname[i]);
711 working_tmp[0] = '\0';
712 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
714 strncpy(working_tmp, tmp, c - tmp);
715 working_tmp[c - tmp] = '\0';
716 for (i = 0; i < strlen(c) - 1; i++)
719 /* only a failure if we arent looking at top level */
720 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
722 putstr("find_inode failed for name=");
729 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
730 putstr("find_inode failed for name=");
735 /* this is for the bare filename, directories have already been mapped */
736 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
737 putstr("find_inode failed for name=");
747 jffs2_1pass_rescan_needed(struct part_info *part)
750 struct jffs2_unknown_node *node;
751 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
753 if (part->jffs2_priv == 0){
754 DEBUGF ("rescan: First time in use\n");
757 /* if we have no list, we need to rescan */
758 if (pL->frag.listCount == 0) {
759 DEBUGF ("rescan: fraglist zero\n");
763 /* or if we are scanning a new partition */
764 if (pL->partOffset != part->offset) {
765 DEBUGF ("rescan: different partition\n");
768 /* but suppose someone reflashed a partition at the same offset... */
769 b = pL->dir.listHead;
771 node = (struct jffs2_unknown_node *) (b->offset);
772 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
773 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
774 (unsigned long) b->offset);
782 #ifdef DEBUG_FRAGMENTS
784 dump_fragments(struct b_lists *pL)
787 struct jffs2_raw_inode *jNode;
789 putstr("\r\n\r\n******The fragment Entries******\r\n");
790 b = pL->frag.listHead;
792 jNode = (struct jffs2_raw_inode *) (b->offset);
793 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
794 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
795 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
796 putLabeledWord("\tbuild_list: version = ", jNode->version);
797 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
798 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
799 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
800 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
801 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
802 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
803 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
804 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
805 putLabeledWord("\tbuild_list: offset = ", b->offset); // FIXME: ? [RS]
813 dump_dirents(struct b_lists *pL)
816 struct jffs2_raw_dirent *jDir;
818 putstr("\r\n\r\n******The directory Entries******\r\n");
819 b = pL->dir.listHead;
821 jDir = (struct jffs2_raw_dirent *) (b->offset);
823 putnstr(jDir->name, jDir->nsize);
824 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
825 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
826 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
827 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
828 putLabeledWord("\tbuild_list: version = ", jDir->version);
829 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
830 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
831 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
832 putLabeledWord("\tbuild_list: type = ", jDir->type);
833 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
834 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
835 putLabeledWord("\tbuild_list: offset = ", b->offset); // FIXME: ? [RS]
842 jffs2_1pass_build_lists(struct part_info * part)
845 struct jffs2_unknown_node *node;
846 u32 offset, oldoffset = 0;
847 u32 max = part->size - sizeof(struct jffs2_raw_inode);
853 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
854 /* jffs2 list building enterprise nope. in newer versions the overhead is */
855 /* only about 5 %. not enough to inconvenience people for. */
858 /* if we are building a list we need to refresh the cache. */
859 jffs_init_1pass_list(part);
860 pL = (struct b_lists *)part->jffs2_priv;
861 pL->partOffset = part->offset;
863 printf("Scanning JFFS2 FS: ");
865 /* start at the beginning of the partition */
866 while (offset < max) {
867 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
868 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
872 node = (struct jffs2_unknown_node *) (part->offset + offset);
873 if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
874 /* if its a fragment add it */
875 if (node->nodetype == JFFS2_NODETYPE_INODE &&
876 inode_crc((struct jffs2_raw_inode *) node)) {
877 if (insert_node(&pL->frag, (u32) part->offset +
880 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
881 dirent_crc((struct jffs2_raw_dirent *) node) &&
882 dirent_name_crc((struct jffs2_raw_dirent *) node)) {
883 if (! (counterN%100))
885 if (insert_node(&pL->dir, (u32) part->offset +
889 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
890 if (node->totlen != sizeof(struct jffs2_unknown_node))
891 printf("OOPS Cleanmarker has bad size "
892 "%d != %d\n", node->totlen,
893 sizeof(struct jffs2_unknown_node));
895 printf("Unknown node type: %x len %d "
896 "offset 0x%x\n", node->nodetype,
897 node->totlen, offset);
899 offset += ((node->totlen + 3) & ~3);
901 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
902 node->nodetype == JFFS2_EMPTY_BITMASK) {
903 offset = jffs2_scan_empty(offset, part);
904 } else { /* if we know nothing, we just step and look. */
908 /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
912 putstr("\b\b done.\r\n"); /* close off the dots */
913 /* turn the lcd back on. */
917 putLabeledWord("dir entries = ", pL->dir.listCount);
918 putLabeledWord("frag entries = ", pL->frag.listCount);
919 putLabeledWord("+4 increments = ", counter4);
920 putLabeledWord("+file_offset increments = ", counterF);
928 #ifdef DEBUG_FRAGMENTS
932 /* give visual feedback that we are done scanning the flash */
933 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
942 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
945 struct jffs2_raw_inode *jNode;
948 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
949 piL->compr_info[i].num_frags = 0;
950 piL->compr_info[i].compr_sum = 0;
951 piL->compr_info[i].decompr_sum = 0;
954 b = pL->frag.listHead;
956 jNode = (struct jffs2_raw_inode *) (b->offset);
957 if (jNode->compr < JFFS2_NUM_COMPR) {
958 piL->compr_info[jNode->compr].num_frags++;
959 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
960 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
969 static struct b_lists *
970 jffs2_get_list(struct part_info * part, const char *who)
972 if (jffs2_1pass_rescan_needed(part)) {
973 if (!jffs2_1pass_build_lists(part)) {
974 printf("%s: Failed to scan JFFSv2 file structure\n", who);
978 return (struct b_lists *)part->jffs2_priv;
982 /* Print directory / file contents */
984 jffs2_1pass_ls(struct part_info * part, const char *fname)
990 if (! (pl = jffs2_get_list(part, "ls")))
993 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
994 putstr("ls: Failed to scan jffs2 file structure\r\n");
1000 putLabeledWord("found file at inode = ", inode);
1001 putLabeledWord("read_inode returns = ", ret);
1011 /* Load a file from flash into memory. fname can be a full path */
1013 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1020 if (! (pl = jffs2_get_list(part, "load")))
1023 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1024 putstr("load: Failed to find inode\r\n");
1028 /* Resolve symlinks */
1029 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1030 putstr("load: Failed to resolve inode structure\r\n");
1034 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1035 putstr("load: Failed to read inode\r\n");
1039 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1040 (unsigned long) dest, ret);
1044 /* Return information about the fs on this partition */
1046 jffs2_1pass_info(struct part_info * part)
1048 struct b_jffs2_info info;
1052 if (! (pl = jffs2_get_list(part, "info")))
1055 jffs2_1pass_fill_info(pl, &info);
1056 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1057 printf("Compression: %s\n", compr_names[i]);
1058 printf("\tfrag count: %d\n", info.compr_info[i].num_frags);
1059 printf("\tcompressed sum: %d\n", info.compr_info[i].compr_sum);
1060 printf("\tuncompressed sum: %d\n", info.compr_info[i].decompr_sum);
1065 #endif /* CFG_CMD_JFFS2 */