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 * might be incompatible with any new code from the jffs2 maintainers anyway,
110 * it should probably be dumped and replaced by something like jffs2reader!
117 #include <watchdog.h>
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 20 /* 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...)
144 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
147 * Support for jffs2 on top of NAND-flash
149 * NAND memory isn't mapped in processor's address space,
150 * so data should be fetched from flash before
151 * being processed. This is exactly what functions declared
156 /* this one defined in cmd_nand.c */
157 int read_jffs2_nand(size_t start, size_t len,
158 size_t * retlen, u_char * buf, int nanddev);
160 #define NAND_PAGE_SIZE 512
161 #define NAND_PAGE_SHIFT 9
162 #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
164 #ifndef NAND_CACHE_PAGES
165 #define NAND_CACHE_PAGES 16
167 #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
169 static u8* nand_cache = NULL;
170 static u32 nand_cache_off = (u32)-1;
171 static int nanddev = 0; /* nand device of current partition */
173 static int read_nand_cached(u32 off, u32 size, u_char *buf)
179 while (bytes_read < size) {
180 if ((off + bytes_read < nand_cache_off) ||
181 (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
182 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
184 /* This memory never gets freed but 'cause
185 it's a bootloader, nobody cares */
186 nand_cache = malloc(NAND_CACHE_SIZE);
188 printf("read_nand_cached: can't alloc cache size %d bytes\n",
193 if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE,
194 &retlen, nand_cache, nanddev) < 0 ||
195 retlen != NAND_CACHE_SIZE) {
196 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
197 nand_cache_off, NAND_CACHE_SIZE);
201 cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
202 if (cpy_bytes > size - bytes_read)
203 cpy_bytes = size - bytes_read;
204 memcpy(buf + bytes_read,
205 nand_cache + off + bytes_read - nand_cache_off,
207 bytes_read += cpy_bytes;
212 static void *get_fl_mem(u32 off, u32 size, void *ext_buf)
214 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
217 printf("get_fl_mem: can't alloc %d bytes\n", size);
220 if (read_nand_cached(off, size, buf) < 0) {
228 static void *get_node_mem(u32 off)
230 struct jffs2_unknown_node node;
233 if (NULL == get_fl_mem(off, sizeof(node), &node))
236 if (!(ret = get_fl_mem(off, node.magic ==
237 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
239 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
240 off, node.magic, node.nodetype, node.totlen);
245 static void put_fl_mem(void *buf)
250 #else /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
252 static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
257 static inline void *get_node_mem(u32 off)
262 static inline void put_fl_mem(void *buf)
266 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
269 /* Compression names */
270 static char *compr_names[] = {
280 #if 0 /* Use spinning wheel */
282 static char spinner[] = { '|', '/', '-', '\\' };
285 /* Memory management */
288 struct mem_block *next;
289 struct b_node nodes[NODE_CHUNK];
294 free_nodes(struct b_list *list)
296 while (list->listMemBase != NULL) {
297 struct mem_block *next = list->listMemBase->next;
298 free( list->listMemBase );
299 list->listMemBase = next;
303 static struct b_node *
304 add_node(struct b_list *list)
307 struct mem_block *memBase;
310 memBase = list->listMemBase;
312 index = memBase->index;
314 putLabeledWord("add_node: index = ", index);
315 putLabeledWord("add_node: memBase = ", list->listMemBase);
318 if (memBase == NULL || index >= NODE_CHUNK) {
319 /* we need more space before we continue */
320 memBase = mmalloc(sizeof(struct mem_block));
321 if (memBase == NULL) {
322 putstr("add_node: malloc failed\n");
325 memBase->next = list->listMemBase;
328 putLabeledWord("add_node: alloced a new membase at ", *memBase);
332 /* now we have room to add it. */
333 b = &memBase->nodes[index];
336 memBase->index = index;
337 list->listMemBase = memBase;
342 static struct b_node *
343 insert_node(struct b_list *list, u32 offset)
346 #ifdef CFG_JFFS2_SORT_FRAGMENTS
347 struct b_node *b, *prev;
350 if (!(new = add_node(list))) {
351 putstr("add_node failed!\r\n");
354 new->offset = offset;
356 #ifdef CFG_JFFS2_SORT_FRAGMENTS
357 if (list->listTail != NULL && list->listCompare(new, list->listTail))
358 prev = list->listTail;
359 else if (list->listLast != NULL && list->listCompare(new, list->listLast))
360 prev = list->listLast;
364 for (b = (prev ? prev->next : list->listHead);
365 b != NULL && list->listCompare(new, b);
366 prev = b, b = b->next) {
370 list->listLast = prev;
377 list->listHead = new;
381 new->next = (struct b_node *) NULL;
382 if (list->listTail != NULL) {
383 list->listTail->next = new;
384 list->listTail = new;
386 list->listTail = list->listHead = new;
393 #ifdef CFG_JFFS2_SORT_FRAGMENTS
394 /* Sort data entries with the latest version last, so that if there
395 * is overlapping data the latest version will be used.
397 static int compare_inodes(struct b_node *new, struct b_node *old)
399 struct jffs2_raw_inode ojNew;
400 struct jffs2_raw_inode ojOld;
401 struct jffs2_raw_inode *jNew =
402 (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
403 struct jffs2_raw_inode *jOld =
404 (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
406 return jNew->version > jOld->version;
409 /* Sort directory entries so all entries in the same directory
410 * with the same name are grouped together, with the latest version
411 * last. This makes it easy to eliminate all but the latest version
412 * by marking the previous version dead by setting the inode to 0.
414 static int compare_dirents(struct b_node *new, struct b_node *old)
416 struct jffs2_raw_dirent ojNew;
417 struct jffs2_raw_dirent ojOld;
418 struct jffs2_raw_dirent *jNew =
419 (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
420 struct jffs2_raw_dirent *jOld =
421 (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
424 /* ascending sort by pino */
425 if (jNew->pino != jOld->pino)
426 return jNew->pino > jOld->pino;
428 /* pino is the same, so use ascending sort by nsize, so
429 * we don't do strncmp unless we really must.
431 if (jNew->nsize != jOld->nsize)
432 return jNew->nsize > jOld->nsize;
434 /* length is also the same, so use ascending sort by name
436 cmp = strncmp(jNew->name, jOld->name, jNew->nsize);
440 /* we have duplicate names in this directory, so use ascending
443 if (jNew->version > jOld->version) {
444 /* since jNew is newer, we know jOld is not valid, so
445 * mark it with inode 0 and it will not be used
456 jffs2_scan_empty(u32 start_offset, struct part_info *part)
458 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
459 char *offset = part->offset + start_offset;
463 while (offset < max &&
464 *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) {
465 offset += sizeof(u32);
467 #if 0 /* Use spinning wheel */
468 /* return if spinning is due */
469 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
471 if (cntr > 1024 && part->erasesize > 0) { /* 4k */
472 /* round up to next erase block border */
473 (u32)offset |= part->erasesize-1;
478 return offset - part->offset;
482 jffs_init_1pass_list(struct part_info *part)
486 if (part->jffs2_priv != NULL) {
487 pL = (struct b_lists *)part->jffs2_priv;
488 free_nodes(&pL->frag);
489 free_nodes(&pL->dir);
492 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
493 pL = (struct b_lists *)part->jffs2_priv;
495 memset(pL, 0, sizeof(*pL));
496 #ifdef CFG_JFFS2_SORT_FRAGMENTS
497 pL->dir.listCompare = compare_dirents;
498 pL->frag.listCompare = compare_inodes;
504 /* find the inode from the slashless name given a parent */
506 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
509 struct jffs2_raw_inode *jNode;
511 u32 latestVersion = 0;
517 #ifdef CFG_JFFS2_SORT_FRAGMENTS
518 /* Find file size before loading any data, so fragments that
519 * start past the end of file can be ignored. A fragment
520 * that is partially in the file is loaded, so extra data may
521 * be loaded up to the next 4K boundary above the file size.
522 * This shouldn't cause trouble when loading kernel images, so
523 * we will live with it.
525 for (b = pL->frag.listHead; b != NULL; b = b->next) {
526 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
527 sizeof(struct jffs2_raw_inode), NULL);
528 if ((inode == jNode->ino)) {
529 /* get actual file length from the newest node */
530 if (jNode->version >= latestVersion) {
531 totalSize = jNode->isize;
532 latestVersion = jNode->version;
539 for (b = pL->frag.listHead; b != NULL; b = b->next) {
540 jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset);
541 if ((inode == jNode->ino)) {
543 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
544 putLabeledWord("read_inode: inode = ", jNode->ino);
545 putLabeledWord("read_inode: version = ", jNode->version);
546 putLabeledWord("read_inode: isize = ", jNode->isize);
547 putLabeledWord("read_inode: offset = ", jNode->offset);
548 putLabeledWord("read_inode: csize = ", jNode->csize);
549 putLabeledWord("read_inode: dsize = ", jNode->dsize);
550 putLabeledWord("read_inode: compr = ", jNode->compr);
551 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
552 putLabeledWord("read_inode: flags = ", jNode->flags);
555 #ifndef CFG_JFFS2_SORT_FRAGMENTS
556 /* get actual file length from the newest node */
557 if (jNode->version >= latestVersion) {
558 totalSize = jNode->isize;
559 latestVersion = jNode->version;
564 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
565 /* ignore data behind latest known EOF */
566 if (jNode->offset > totalSize) {
571 lDest = (char *) (dest + jNode->offset);
573 putLabeledWord("read_inode: src = ", src);
574 putLabeledWord("read_inode: dest = ", lDest);
576 switch (jNode->compr) {
577 case JFFS2_COMPR_NONE:
578 ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
580 case JFFS2_COMPR_ZERO:
582 for (i = 0; i < jNode->dsize; i++)
585 case JFFS2_COMPR_RTIME:
587 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
589 case JFFS2_COMPR_DYNRUBIN:
590 /* this is slow but it works */
592 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
594 case JFFS2_COMPR_ZLIB:
595 ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
599 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
607 putLabeledWord("read_inode: totalSize = ", totalSize);
608 putLabeledWord("read_inode: compr ret = ", ret);
616 putLabeledWord("read_inode: returning = ", totalSize);
621 /* find the inode from the slashless name given a parent */
623 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
626 struct jffs2_raw_dirent *jDir;
632 /* name is assumed slash free */
636 /* we need to search all and return the inode with the highest version */
637 for(b = pL->dir.listHead; b; b = b->next, counter++) {
638 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
639 if ((pino == jDir->pino) && (len == jDir->nsize) &&
640 (jDir->ino) && /* 0 for unlink */
641 (!strncmp(jDir->name, name, len))) { /* a match */
642 if (jDir->version < version) {
647 if (jDir->version == version && inode != 0) {
648 /* I'm pretty sure this isn't legal */
649 putstr(" ** ERROR ** ");
650 putnstr(jDir->name, jDir->nsize);
651 putLabeledWord(" has dup version =", version);
654 version = jDir->version;
657 putstr("\r\nfind_inode:p&l ->");
658 putnstr(jDir->name, jDir->nsize);
660 putLabeledWord("pino = ", jDir->pino);
661 putLabeledWord("nsize = ", jDir->nsize);
662 putLabeledWord("b = ", (u32) b);
663 putLabeledWord("counter = ", counter);
670 char *mkmodestr(unsigned long mode, char *str)
672 static const char *l = "xwr";
676 switch (mode & S_IFMT) {
677 case S_IFDIR: str[0] = 'd'; break;
678 case S_IFBLK: str[0] = 'b'; break;
679 case S_IFCHR: str[0] = 'c'; break;
680 case S_IFIFO: str[0] = 'f'; break;
681 case S_IFLNK: str[0] = 'l'; break;
682 case S_IFSOCK: str[0] = 's'; break;
683 case S_IFREG: str[0] = '-'; break;
684 default: str[0] = '?';
687 for(i = 0; i < 9; i++) {
689 str[9-i] = (mode & mask)?c:'-';
693 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
694 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
695 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
700 static inline void dump_stat(struct stat *st, const char *name)
705 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
708 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
710 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
711 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
714 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
715 st->st_size, s, name);
718 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
721 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
726 if(!d || !i) return -1;
728 strncpy(fname, d->name, d->nsize);
729 fname[d->nsize] = '\0';
731 memset(&st,0,sizeof(st));
733 st.st_mtime = i->mtime;
734 st.st_mode = i->mode;
737 /* neither dsize nor isize help us.. do it the long way */
738 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
740 dump_stat(&st, fname);
742 if (d->type == DT_LNK) {
743 unsigned char *src = (unsigned char *) (&i[1]);
745 putnstr(src, (int)i->dsize);
753 /* list inodes with the given pino */
755 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
758 struct jffs2_raw_dirent *jDir;
760 for (b = pL->dir.listHead; b; b = b->next) {
761 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
762 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
764 struct jffs2_raw_inode ojNode;
765 struct jffs2_raw_inode *jNode, *i = NULL;
766 struct b_node *b2 = pL->frag.listHead;
769 jNode = (struct jffs2_raw_inode *)
770 get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
771 if (jNode->ino == jDir->ino
772 && jNode->version >= i_version)
773 i = get_fl_mem(b2->offset, sizeof(*i), NULL);
777 dump_inode(pL, jDir, i);
786 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
790 char working_tmp[256];
793 /* discard any leading slash */
795 while (fname[i] == '/')
797 strcpy(tmp, &fname[i]);
799 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
801 strncpy(working_tmp, tmp, c - tmp);
802 working_tmp[c - tmp] = '\0';
804 putstr("search_inode: tmp = ");
807 putstr("search_inode: wtmp = ");
810 putstr("search_inode: c = ");
814 for (i = 0; i < strlen(c) - 1; i++)
818 putstr("search_inode: post tmp = ");
823 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
824 putstr("find_inode failed for name=");
830 /* this is for the bare filename, directories have already been mapped */
831 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
832 putstr("find_inode failed for name=");
842 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
846 struct jffs2_raw_dirent *jDir;
847 struct jffs2_raw_inode *jNode;
848 u8 jDirFoundType = 0;
849 u32 jDirFoundIno = 0;
850 u32 jDirFoundPino = 0;
856 /* we need to search all and return the inode with the highest version */
857 for(b = pL->dir.listHead; b; b = b->next) {
858 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
859 if (ino == jDir->ino) {
860 if (jDir->version < version) {
865 if (jDir->version == version && jDirFoundType) {
866 /* I'm pretty sure this isn't legal */
867 putstr(" ** ERROR ** ");
868 putnstr(jDir->name, jDir->nsize);
869 putLabeledWord(" has dup version (resolve) = ",
873 jDirFoundType = jDir->type;
874 jDirFoundIno = jDir->ino;
875 jDirFoundPino = jDir->pino;
876 version = jDir->version;
880 /* now we found the right entry again. (shoulda returned inode*) */
881 if (jDirFoundType != DT_LNK)
884 /* it's a soft link so we follow it again. */
885 b2 = pL->frag.listHead;
887 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset);
888 if (jNode->ino == jDirFoundIno) {
889 src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode));
892 putLabeledWord("\t\t dsize = ", jNode->dsize);
893 putstr("\t\t target = ");
894 putnstr(src, jNode->dsize);
897 strncpy(tmp, src, jNode->dsize);
898 tmp[jNode->dsize] = '\0';
905 /* ok so the name of the new file to find is in tmp */
906 /* if it starts with a slash it is root based else shared dirs */
910 pino = jDirFoundPino;
912 return jffs2_1pass_search_inode(pL, tmp, pino);
916 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
920 char working_tmp[256];
923 /* discard any leading slash */
925 while (fname[i] == '/')
927 strcpy(tmp, &fname[i]);
928 working_tmp[0] = '\0';
929 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
931 strncpy(working_tmp, tmp, c - tmp);
932 working_tmp[c - tmp] = '\0';
933 for (i = 0; i < strlen(c) - 1; i++)
939 /* only a failure if we arent looking at top level */
940 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
942 putstr("find_inode failed for name=");
949 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
950 putstr("find_inode failed for name=");
955 /* this is for the bare filename, directories have already been mapped */
956 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
957 putstr("find_inode failed for name=");
967 jffs2_1pass_rescan_needed(struct part_info *part)
970 struct jffs2_unknown_node onode;
971 struct jffs2_unknown_node *node;
972 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
974 if (part->jffs2_priv == 0){
975 DEBUGF ("rescan: First time in use\n");
978 /* if we have no list, we need to rescan */
979 if (pL->frag.listCount == 0) {
980 DEBUGF ("rescan: fraglist zero\n");
984 /* or if we are scanning a new partition */
985 if (pL->partOffset != part->offset) {
986 DEBUGF ("rescan: different partition\n");
990 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
991 if (nanddev != (int)part->usr_priv - 1) {
992 DEBUGF ("rescan: nand device changed\n");
995 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
997 /* but suppose someone reflashed a partition at the same offset... */
998 b = pL->dir.listHead;
1000 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1001 sizeof(onode), &onode);
1002 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
1003 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1004 (unsigned long) b->offset);
1012 #ifdef DEBUG_FRAGMENTS
1014 dump_fragments(struct b_lists *pL)
1017 struct jffs2_raw_inode ojNode;
1018 struct jffs2_raw_inode *jNode;
1020 putstr("\r\n\r\n******The fragment Entries******\r\n");
1021 b = pL->frag.listHead;
1023 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1024 sizeof(ojNode), &ojNode);
1025 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1026 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1027 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1028 putLabeledWord("\tbuild_list: version = ", jNode->version);
1029 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1030 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1031 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1032 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1033 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1034 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1035 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1036 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
1037 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
1043 #ifdef DEBUG_DIRENTS
1045 dump_dirents(struct b_lists *pL)
1048 struct jffs2_raw_dirent *jDir;
1050 putstr("\r\n\r\n******The directory Entries******\r\n");
1051 b = pL->dir.listHead;
1053 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
1055 putnstr(jDir->name, jDir->nsize);
1056 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1057 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1058 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1059 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1060 putLabeledWord("\tbuild_list: version = ", jDir->version);
1061 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1062 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1063 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1064 putLabeledWord("\tbuild_list: type = ", jDir->type);
1065 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1066 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
1067 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
1075 jffs2_1pass_build_lists(struct part_info * part)
1078 struct jffs2_unknown_node *node;
1079 struct jffs2_unknown_node crcnode;
1080 u32 offset, oldoffset = 0;
1081 u32 max = part->size - sizeof(struct jffs2_raw_inode);
1089 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
1090 nanddev = (int)part->usr_priv - 1;
1091 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
1093 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
1094 /* jffs2 list building enterprise nope. in newer versions the overhead is */
1095 /* only about 5 %. not enough to inconvenience people for. */
1098 /* if we are building a list we need to refresh the cache. */
1099 jffs_init_1pass_list(part);
1100 pL = (struct b_lists *)part->jffs2_priv;
1101 pL->partOffset = part->offset;
1103 puts ("Scanning JFFS2 FS: ");
1105 /* start at the beginning of the partition */
1106 while (offset < max) {
1107 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
1109 #if 0 /* Use spinning wheel */
1110 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
1114 node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset);
1115 if (node->magic == JFFS2_MAGIC_BITMASK) {
1116 /* if its a fragment add it */
1117 /* check crc by readding a JFFS2_NODE_ACCURATE */
1118 crcnode.magic = node->magic;
1119 crcnode.nodetype = node->nodetype | JFFS2_NODE_ACCURATE;
1120 crcnode.totlen = node->totlen;
1121 crcnode.hdr_crc = node->hdr_crc;
1123 if (!hdr_crc(&crcnode)) {
1127 } else if (node->nodetype == JFFS2_NODETYPE_INODE &&
1128 inode_crc((struct jffs2_raw_inode *) node)) {
1129 if (insert_node(&pL->frag, (u32) part->offset +
1134 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
1135 dirent_crc((struct jffs2_raw_dirent *) node) &&
1136 dirent_name_crc((struct jffs2_raw_dirent *) node)) {
1137 if (! (counterN%128))
1138 #if 0 /* Use spinning wheel */
1143 if (insert_node(&pL->dir, (u32) part->offset +
1149 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
1150 if (node->totlen != sizeof(struct jffs2_unknown_node))
1151 printf("OOPS Cleanmarker has bad size "
1152 "%d != %d\n", node->totlen,
1153 sizeof(struct jffs2_unknown_node));
1154 } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
1155 if (node->totlen < sizeof(struct jffs2_unknown_node))
1156 printf("OOPS Padding has bad size "
1157 "%d < %d\n", node->totlen,
1158 sizeof(struct jffs2_unknown_node));
1162 offset += ((node->totlen + 3) & ~3);
1164 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
1165 node->nodetype == JFFS2_EMPTY_BITMASK) {
1166 offset = jffs2_scan_empty(offset, part);
1167 } else { /* if we know nothing, we just step and look. */
1171 /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
1175 putstr("\b\b done.\r\n"); /* close off the dots */
1176 /* turn the lcd back on. */
1180 putLabeledWord("dir entries = ", pL->dir.listCount);
1181 putLabeledWord("frag entries = ", pL->frag.listCount);
1182 putLabeledWord("+4 increments = ", counter4);
1183 putLabeledWord("+file_offset increments = ", counterF);
1184 putLabeledWord("Unknown node types = ", counterUNK);
1185 putLabeledWord("Bad hdr_crc = ", counterCRC);
1189 #ifdef DEBUG_DIRENTS
1193 #ifdef DEBUG_FRAGMENTS
1197 /* give visual feedback that we are done scanning the flash */
1198 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
1204 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1207 struct jffs2_raw_inode ojNode;
1208 struct jffs2_raw_inode *jNode;
1211 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1212 piL->compr_info[i].num_frags = 0;
1213 piL->compr_info[i].compr_sum = 0;
1214 piL->compr_info[i].decompr_sum = 0;
1217 b = pL->frag.listHead;
1219 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1220 sizeof(ojNode), &ojNode);
1221 if (jNode->compr < JFFS2_NUM_COMPR) {
1222 piL->compr_info[jNode->compr].num_frags++;
1223 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1224 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1232 static struct b_lists *
1233 jffs2_get_list(struct part_info * part, const char *who)
1235 if (jffs2_1pass_rescan_needed(part)) {
1236 if (!jffs2_1pass_build_lists(part)) {
1237 printf("%s: Failed to scan JFFSv2 file structure\n", who);
1242 return (struct b_lists *)part->jffs2_priv;
1246 /* Print directory / file contents */
1248 jffs2_1pass_ls(struct part_info * part, const char *fname)
1253 if (! (pl = jffs2_get_list(part, "ls")))
1257 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1258 putstr("ls: Failed to scan jffs2 file structure\r\n");
1262 putLabeledWord("found file at inode = ", inode);
1263 putLabeledWord("read_inode returns = ", ret);
1269 /* Load a file from flash into memory. fname can be a full path */
1271 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1278 if (! (pl = jffs2_get_list(part, "load")))
1281 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1282 putstr("load: Failed to find inode\r\n");
1286 /* Resolve symlinks */
1287 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1288 putstr("load: Failed to resolve inode structure\r\n");
1292 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1293 putstr("load: Failed to read inode\r\n");
1297 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1298 (unsigned long) dest, ret);
1302 /* Return information about the fs on this partition */
1304 jffs2_1pass_info(struct part_info * part)
1306 struct b_jffs2_info info;
1310 if (! (pl = jffs2_get_list(part, "info")))
1313 jffs2_1pass_fill_info(pl, &info);
1314 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1315 printf ("Compression: %s\n"
1316 "\tfrag count: %d\n"
1317 "\tcompressed sum: %d\n"
1318 "\tuncompressed sum: %d\n",
1320 info.compr_info[i].num_frags,
1321 info.compr_info[i].compr_sum,
1322 info.compr_info[i].decompr_sum);
1327 #endif /* CFG_CMD_JFFS2 */