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
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.
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
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 ;).
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 CONFIG_SYS_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!
116 #include <linux/compiler.h>
117 #include <linux/stat.h>
118 #include <linux/time.h>
119 #include <u-boot/crc.h>
120 #include <watchdog.h>
121 #include <jffs2/jffs2.h>
122 #include <jffs2/jffs2_1pass.h>
123 #include <linux/compat.h>
124 #include <linux/errno.h>
126 #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 */
137 # define DEBUGF(fmt,args...) printf(fmt ,##args)
139 # define DEBUGF(fmt,args...)
144 /* keeps pointer to currentlu processed partition */
145 static struct part_info *current_part;
147 #if (defined(CONFIG_JFFS2_NAND) && \
148 defined(CONFIG_CMD_NAND) )
151 * Support for jffs2 on top of NAND-flash
153 * NAND memory isn't mapped in processor's address space,
154 * so data should be fetched from flash before
155 * being processed. This is exactly what functions declared
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;
172 static int read_nand_cached(u32 off, u32 size, u_char *buf)
174 struct mtdids *id = current_part->dev->id;
175 struct mtd_info *mtd;
181 mtd = get_nand_dev_by_index(id->num);
185 while (bytes_read < size) {
186 retlen = NAND_CACHE_SIZE;
187 if( nand_cache_off + retlen > mtd->size )
188 retlen = mtd->size - nand_cache_off;
190 if ((off + bytes_read < nand_cache_off) ||
191 (off + bytes_read >= nand_cache_off + retlen)) {
192 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
194 /* This memory never gets freed but 'cause
195 it's a bootloader, nobody cares */
196 nand_cache = malloc(NAND_CACHE_SIZE);
198 printf("read_nand_cached: can't alloc cache size %d bytes\n",
204 toread = NAND_CACHE_SIZE;
205 if( nand_cache_off + toread > mtd->size )
206 toread = mtd->size - nand_cache_off;
209 if (nand_read(mtd, nand_cache_off,
210 &retlen, nand_cache) < 0 ||
212 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
213 nand_cache_off, toread);
217 cpy_bytes = nand_cache_off + retlen - (off + bytes_read);
218 if (cpy_bytes > size - bytes_read)
219 cpy_bytes = size - bytes_read;
220 memcpy(buf + bytes_read,
221 nand_cache + off + bytes_read - nand_cache_off,
223 bytes_read += cpy_bytes;
228 static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
230 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
233 printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
236 if (read_nand_cached(off, size, buf) < 0) {
245 static void *get_node_mem_nand(u32 off, void *ext_buf)
247 struct jffs2_unknown_node node;
250 if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
253 if (!(ret = get_fl_mem_nand(off, node.magic ==
254 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
256 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
257 off, node.magic, node.nodetype, node.totlen);
262 static void put_fl_mem_nand(void *buf)
268 #if defined(CONFIG_CMD_ONENAND)
270 #include <linux/mtd/mtd.h>
271 #include <linux/mtd/onenand.h>
272 #include <onenand_uboot.h>
274 #define ONENAND_PAGE_SIZE 2048
275 #define ONENAND_PAGE_SHIFT 11
276 #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
278 #ifndef ONENAND_CACHE_PAGES
279 #define ONENAND_CACHE_PAGES 4
281 #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
283 static u8* onenand_cache;
284 static u32 onenand_cache_off = (u32)-1;
286 static int read_onenand_cached(u32 off, u32 size, u_char *buf)
293 while (bytes_read < size) {
294 retlen = ONENAND_CACHE_SIZE;
295 if( onenand_cache_off + retlen > onenand_mtd.size )
296 retlen = onenand_mtd.size - onenand_cache_off;
298 if ((off + bytes_read < onenand_cache_off) ||
299 (off + bytes_read >= onenand_cache_off + retlen)) {
300 onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
301 if (!onenand_cache) {
302 /* This memory never gets freed but 'cause
303 it's a bootloader, nobody cares */
304 onenand_cache = malloc(ONENAND_CACHE_SIZE);
305 if (!onenand_cache) {
306 printf("read_onenand_cached: can't alloc cache size %d bytes\n",
312 toread = ONENAND_CACHE_SIZE;
313 if( onenand_cache_off + toread > onenand_mtd.size )
314 toread = onenand_mtd.size - onenand_cache_off;
316 if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
317 &retlen, onenand_cache) < 0 ||
319 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
320 onenand_cache_off, toread);
324 cpy_bytes = onenand_cache_off + retlen - (off + bytes_read);
325 if (cpy_bytes > size - bytes_read)
326 cpy_bytes = size - bytes_read;
327 memcpy(buf + bytes_read,
328 onenand_cache + off + bytes_read - onenand_cache_off,
330 bytes_read += cpy_bytes;
335 static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
337 u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
340 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
343 if (read_onenand_cached(off, size, buf) < 0) {
352 static void *get_node_mem_onenand(u32 off, void *ext_buf)
354 struct jffs2_unknown_node node;
357 if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
360 ret = get_fl_mem_onenand(off, node.magic ==
361 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
364 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
365 off, node.magic, node.nodetype, node.totlen);
370 static void put_fl_mem_onenand(void *buf)
376 #if defined(CONFIG_CMD_FLASH)
380 * Support for jffs2 on top of NOR-flash
382 * NOR flash memory is mapped in processor's address space,
383 * just return address.
385 static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
388 struct mtdids *id = current_part->dev->id;
390 flash_info_t *flash = &flash_info[id->num];
392 addr += flash->start[0];
394 memcpy(ext_buf, (void *)addr, size);
400 static inline void *get_node_mem_nor(u32 off, void *ext_buf)
402 struct jffs2_unknown_node *pNode;
404 /* pNode will point directly to flash - don't provide external buffer
405 and don't care about size */
406 pNode = get_fl_mem_nor(off, 0, NULL);
407 return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
408 pNode->totlen : sizeof(*pNode), ext_buf);
413 * Generic jffs2 raw memory and node read routines.
416 static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
418 struct mtdids *id = current_part->dev->id;
421 #if defined(CONFIG_CMD_FLASH)
422 case MTD_DEV_TYPE_NOR:
423 return get_fl_mem_nor(off, size, ext_buf);
426 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
427 case MTD_DEV_TYPE_NAND:
428 return get_fl_mem_nand(off, size, ext_buf);
431 #if defined(CONFIG_CMD_ONENAND)
432 case MTD_DEV_TYPE_ONENAND:
433 return get_fl_mem_onenand(off, size, ext_buf);
437 printf("get_fl_mem: unknown device type, " \
438 "using raw offset!\n");
443 static inline void *get_node_mem(u32 off, void *ext_buf)
445 struct mtdids *id = current_part->dev->id;
448 #if defined(CONFIG_CMD_FLASH)
449 case MTD_DEV_TYPE_NOR:
450 return get_node_mem_nor(off, ext_buf);
453 #if defined(CONFIG_JFFS2_NAND) && \
454 defined(CONFIG_CMD_NAND)
455 case MTD_DEV_TYPE_NAND:
456 return get_node_mem_nand(off, ext_buf);
459 #if defined(CONFIG_CMD_ONENAND)
460 case MTD_DEV_TYPE_ONENAND:
461 return get_node_mem_onenand(off, ext_buf);
465 printf("get_fl_mem: unknown device type, " \
466 "using raw offset!\n");
471 static inline void put_fl_mem(void *buf, void *ext_buf)
473 struct mtdids *id = current_part->dev->id;
475 /* If buf is the same as ext_buf, it was provided by the caller -
476 we shouldn't free it then. */
480 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
481 case MTD_DEV_TYPE_NAND:
482 return put_fl_mem_nand(buf);
484 #if defined(CONFIG_CMD_ONENAND)
485 case MTD_DEV_TYPE_ONENAND:
486 return put_fl_mem_onenand(buf);
491 /* Compression names */
492 static char *compr_names[] = {
500 #if defined(CONFIG_JFFS2_LZO)
505 /* Memory management */
508 struct mem_block *next;
509 struct b_node nodes[NODE_CHUNK];
513 free_nodes(struct b_list *list)
515 while (list->listMemBase != NULL) {
516 struct mem_block *next = list->listMemBase->next;
517 free( list->listMemBase );
518 list->listMemBase = next;
522 static struct b_node *
523 add_node(struct b_list *list)
526 struct mem_block *memBase;
529 memBase = list->listMemBase;
531 index = memBase->index;
533 putLabeledWord("add_node: index = ", index);
534 putLabeledWord("add_node: memBase = ", list->listMemBase);
537 if (memBase == NULL || index >= NODE_CHUNK) {
538 /* we need more space before we continue */
539 memBase = mmalloc(sizeof(struct mem_block));
540 if (memBase == NULL) {
541 putstr("add_node: malloc failed\n");
544 memBase->next = list->listMemBase;
547 putLabeledWord("add_node: alloced a new membase at ", *memBase);
551 /* now we have room to add it. */
552 b = &memBase->nodes[index];
555 memBase->index = index;
556 list->listMemBase = memBase;
561 static struct b_node *
562 insert_node(struct b_list *list)
566 if (!(new = add_node(list))) {
567 putstr("add_node failed!\r\n");
572 if (list->listTail != NULL)
573 list->listTail->next = new;
575 list->listHead = new;
576 list->listTail = new;
581 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
582 /* Sort data entries with the latest version last, so that if there
583 * is overlapping data the latest version will be used.
585 static int compare_inodes(struct b_node *new, struct b_node *old)
587 return new->version > old->version;
590 /* Sort directory entries so all entries in the same directory
591 * with the same name are grouped together, with the latest version
592 * last. This makes it easy to eliminate all but the latest version
593 * by marking the previous version dead by setting the inode to 0.
595 static int compare_dirents(struct b_node *new, struct b_node *old)
598 * Using NULL as the buffer for NOR flash prevents the entire node
599 * being read. This makes most comparisons much quicker as only one
600 * or two entries from the node will be used most of the time.
602 struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
603 struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
607 if (jNew->pino != jOld->pino) {
608 /* ascending sort by pino */
609 ret = jNew->pino > jOld->pino;
610 } else if (jNew->nsize != jOld->nsize) {
612 * pino is the same, so use ascending sort by nsize,
613 * so we don't do strncmp unless we really must.
615 ret = jNew->nsize > jOld->nsize;
618 * length is also the same, so use ascending sort by name
620 cmp = strncmp((char *)jNew->name, (char *)jOld->name,
626 * we have duplicate names in this directory,
627 * so use ascending sort by version
629 ret = jNew->version > jOld->version;
632 put_fl_mem(jNew, NULL);
633 put_fl_mem(jOld, NULL);
640 jffs2_free_cache(struct part_info *part)
644 if (part->jffs2_priv != NULL) {
645 pL = (struct b_lists *)part->jffs2_priv;
646 free_nodes(&pL->frag);
647 free_nodes(&pL->dir);
654 jffs_init_1pass_list(struct part_info *part)
658 jffs2_free_cache(part);
660 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
661 pL = (struct b_lists *)part->jffs2_priv;
663 memset(pL, 0, sizeof(*pL));
664 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
665 pL->dir.listCompare = compare_dirents;
666 pL->frag.listCompare = compare_inodes;
672 /* find the inode from the slashless name given a parent */
674 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
677 struct jffs2_raw_inode *jNode;
679 u32 latestVersion = 0;
685 /* Find file size before loading any data, so fragments that
686 * start past the end of file can be ignored. A fragment
687 * that is partially in the file is loaded, so extra data may
688 * be loaded up to the next 4K boundary above the file size.
689 * This shouldn't cause trouble when loading kernel images, so
690 * we will live with it.
692 int latestOffset = -1;
693 for (b = pL->frag.listHead; b != NULL; b = b->next) {
694 if (inode == b->ino) {
695 /* get actual file length from the newest node */
696 if (b->version >= latestVersion) {
697 latestVersion = b->version;
698 latestOffset = b->offset;
703 if (latestOffset >= 0) {
704 jNode = (struct jffs2_raw_inode *)get_fl_mem(latestOffset,
705 sizeof(struct jffs2_raw_inode), pL->readbuf);
706 totalSize = jNode->isize;
707 put_fl_mem(jNode, pL->readbuf);
711 * If no destination is provided, we are done.
712 * Just return the total size.
717 for (b = pL->frag.listHead; b != NULL; b = b->next) {
718 if (inode == b->ino) {
720 * Copy just the node and not the data at this point,
721 * since we don't yet know if we need this data.
723 jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
724 sizeof(struct jffs2_raw_inode),
727 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
728 putLabeledWord("read_inode: inode = ", jNode->ino);
729 putLabeledWord("read_inode: version = ", jNode->version);
730 putLabeledWord("read_inode: isize = ", jNode->isize);
731 putLabeledWord("read_inode: offset = ", jNode->offset);
732 putLabeledWord("read_inode: csize = ", jNode->csize);
733 putLabeledWord("read_inode: dsize = ", jNode->dsize);
734 putLabeledWord("read_inode: compr = ", jNode->compr);
735 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
736 putLabeledWord("read_inode: flags = ", jNode->flags);
741 * Now that the inode has been checked,
742 * read the entire inode, including data.
744 put_fl_mem(jNode, pL->readbuf);
745 jNode = (struct jffs2_raw_inode *)
746 get_node_mem(b->offset, pL->readbuf);
747 src = ((uchar *)jNode) +
748 sizeof(struct jffs2_raw_inode);
749 /* ignore data behind latest known EOF */
750 if (jNode->offset > totalSize) {
751 put_fl_mem(jNode, pL->readbuf);
754 if (b->datacrc == CRC_UNKNOWN)
755 b->datacrc = data_crc(jNode) ?
757 if (b->datacrc == CRC_BAD) {
758 put_fl_mem(jNode, pL->readbuf);
762 lDest = (uchar *) (dest + jNode->offset);
764 putLabeledWord("read_inode: src = ", src);
765 putLabeledWord("read_inode: dest = ", lDest);
767 switch (jNode->compr) {
768 case JFFS2_COMPR_NONE:
769 ldr_memcpy(lDest, src, jNode->dsize);
771 case JFFS2_COMPR_ZERO:
772 for (i = 0; i < jNode->dsize; i++)
775 case JFFS2_COMPR_RTIME:
776 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
778 case JFFS2_COMPR_DYNRUBIN:
779 /* this is slow but it works */
780 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
782 case JFFS2_COMPR_ZLIB:
783 zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
785 #if defined(CONFIG_JFFS2_LZO)
786 case JFFS2_COMPR_LZO:
787 lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
792 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
793 put_fl_mem(jNode, pL->readbuf);
800 putLabeledWord("read_inode: totalSize = ", totalSize);
802 put_fl_mem(jNode, pL->readbuf);
808 putLabeledWord("read_inode: returning = ", totalSize);
813 /* find the inode from the slashless name given a parent */
815 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
818 struct jffs2_raw_dirent *jDir;
824 /* name is assumed slash free */
828 /* we need to search all and return the inode with the highest version */
829 for(b = pL->dir.listHead; b; b = b->next, counter++) {
830 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
832 if ((pino == jDir->pino) && (len == jDir->nsize) &&
833 (!strncmp((char *)jDir->name, name, len))) { /* a match */
834 if (jDir->version < version) {
835 put_fl_mem(jDir, pL->readbuf);
839 if (jDir->version == version && inode != 0) {
840 /* I'm pretty sure this isn't legal */
841 putstr(" ** ERROR ** ");
842 putnstr(jDir->name, jDir->nsize);
843 putLabeledWord(" has dup version =", version);
846 version = jDir->version;
849 putstr("\r\nfind_inode:p&l ->");
850 putnstr(jDir->name, jDir->nsize);
852 putLabeledWord("pino = ", jDir->pino);
853 putLabeledWord("nsize = ", jDir->nsize);
854 putLabeledWord("b = ", (u32) b);
855 putLabeledWord("counter = ", counter);
857 put_fl_mem(jDir, pL->readbuf);
862 char *mkmodestr(unsigned long mode, char *str)
864 static const char *l = "xwr";
868 switch (mode & S_IFMT) {
869 case S_IFDIR: str[0] = 'd'; break;
870 case S_IFBLK: str[0] = 'b'; break;
871 case S_IFCHR: str[0] = 'c'; break;
872 case S_IFIFO: str[0] = 'f'; break;
873 case S_IFLNK: str[0] = 'l'; break;
874 case S_IFSOCK: str[0] = 's'; break;
875 case S_IFREG: str[0] = '-'; break;
876 default: str[0] = '?';
879 for(i = 0; i < 9; i++) {
881 str[9-i] = (mode & mask)?c:'-';
885 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
886 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
887 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
892 static inline void dump_stat(struct stat *st, const char *name)
897 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
900 ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
902 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
903 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
906 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
907 st->st_size, s, name);
910 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
913 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
918 if(!d || !i) return -1;
920 strncpy(fname, (char *)d->name, d->nsize);
921 fname[d->nsize] = '\0';
923 memset(&st,0,sizeof(st));
925 st.st_mtime = i->mtime;
926 st.st_mode = i->mode;
928 st.st_size = i->isize;
930 dump_stat(&st, fname);
932 if (d->type == DT_LNK) {
933 unsigned char *src = (unsigned char *) (&i[1]);
935 putnstr(src, (int)i->dsize);
943 /* list inodes with the given pino */
945 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
948 struct jffs2_raw_dirent *jDir;
950 for (b = pL->dir.listHead; b; b = b->next) {
951 if (pino == b->pino) {
954 struct jffs2_raw_inode *jNode = NULL;
957 jDir = (struct jffs2_raw_dirent *)
958 get_node_mem(b->offset, pL->readbuf);
959 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
960 /* Check for more recent versions of this file */
963 struct b_node *next = b->next;
964 struct jffs2_raw_dirent *jDirNext;
967 jDirNext = (struct jffs2_raw_dirent *)
968 get_node_mem(next->offset, NULL);
969 match = jDirNext->pino == jDir->pino &&
970 jDirNext->nsize == jDir->nsize &&
971 strncmp((char *)jDirNext->name,
975 /* Use next. It is more recent */
977 /* Update buffer with the new info */
980 put_fl_mem(jDirNext, NULL);
983 if (jDir->ino == 0) {
985 put_fl_mem(jDir, pL->readbuf);
989 for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
990 if (b2->ino == jDir->ino &&
991 b2->version >= i_version) {
992 i_version = b2->version;
993 i_offset = b2->offset;
997 if (i_version >= 0) {
998 if (jDir->type == DT_LNK)
999 jNode = get_node_mem(i_offset, NULL);
1001 jNode = get_fl_mem(i_offset,
1006 dump_inode(pL, jDir, jNode);
1007 put_fl_mem(jNode, NULL);
1009 put_fl_mem(jDir, pL->readbuf);
1016 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1020 char working_tmp[256];
1023 /* discard any leading slash */
1025 while (fname[i] == '/')
1027 strcpy(tmp, &fname[i]);
1029 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1031 strncpy(working_tmp, tmp, c - tmp);
1032 working_tmp[c - tmp] = '\0';
1034 putstr("search_inode: tmp = ");
1037 putstr("search_inode: wtmp = ");
1038 putstr(working_tmp);
1040 putstr("search_inode: c = ");
1044 for (i = 0; i < strlen(c) - 1; i++)
1048 putstr("search_inode: post tmp = ");
1053 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1054 putstr("find_inode failed for name=");
1055 putstr(working_tmp);
1060 /* this is for the bare filename, directories have already been mapped */
1061 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1062 putstr("find_inode failed for name=");
1072 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1076 struct jffs2_raw_dirent *jDir;
1077 struct jffs2_raw_inode *jNode;
1078 u8 jDirFoundType = 0;
1079 u32 jDirFoundIno = 0;
1080 u32 jDirFoundPino = 0;
1086 /* we need to search all and return the inode with the highest version */
1087 for(b = pL->dir.listHead; b; b = b->next) {
1088 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1090 if (ino == jDir->ino) {
1091 if (jDir->version < version) {
1092 put_fl_mem(jDir, pL->readbuf);
1096 if (jDir->version == version && jDirFoundType) {
1097 /* I'm pretty sure this isn't legal */
1098 putstr(" ** ERROR ** ");
1099 putnstr(jDir->name, jDir->nsize);
1100 putLabeledWord(" has dup version (resolve) = ",
1104 jDirFoundType = jDir->type;
1105 jDirFoundIno = jDir->ino;
1106 jDirFoundPino = jDir->pino;
1107 version = jDir->version;
1109 put_fl_mem(jDir, pL->readbuf);
1111 /* now we found the right entry again. (shoulda returned inode*) */
1112 if (jDirFoundType != DT_LNK)
1113 return jDirFoundIno;
1115 /* it's a soft link so we follow it again. */
1116 b2 = pL->frag.listHead;
1118 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
1120 if (jNode->ino == jDirFoundIno) {
1121 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
1124 putLabeledWord("\t\t dsize = ", jNode->dsize);
1125 putstr("\t\t target = ");
1126 putnstr(src, jNode->dsize);
1129 strncpy(tmp, (char *)src, jNode->dsize);
1130 tmp[jNode->dsize] = '\0';
1131 put_fl_mem(jNode, pL->readbuf);
1135 put_fl_mem(jNode, pL->readbuf);
1137 /* ok so the name of the new file to find is in tmp */
1138 /* if it starts with a slash it is root based else shared dirs */
1142 pino = jDirFoundPino;
1144 return jffs2_1pass_search_inode(pL, tmp, pino);
1148 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1152 char working_tmp[256];
1155 /* discard any leading slash */
1157 while (fname[i] == '/')
1159 strcpy(tmp, &fname[i]);
1160 working_tmp[0] = '\0';
1161 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1163 strncpy(working_tmp, tmp, c - tmp);
1164 working_tmp[c - tmp] = '\0';
1165 for (i = 0; i < strlen(c) - 1; i++)
1168 /* only a failure if we arent looking at top level */
1169 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
1171 putstr("find_inode failed for name=");
1172 putstr(working_tmp);
1178 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1179 putstr("find_inode failed for name=");
1184 /* this is for the bare filename, directories have already been mapped */
1185 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1186 putstr("find_inode failed for name=");
1196 jffs2_1pass_rescan_needed(struct part_info *part)
1199 struct jffs2_unknown_node onode;
1200 struct jffs2_unknown_node *node;
1201 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
1203 if (part->jffs2_priv == 0){
1204 DEBUGF ("rescan: First time in use\n");
1208 /* if we have no list, we need to rescan */
1209 if (pL->frag.listCount == 0) {
1210 DEBUGF ("rescan: fraglist zero\n");
1214 /* but suppose someone reflashed a partition at the same offset... */
1215 b = pL->dir.listHead;
1217 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1218 sizeof(onode), &onode);
1219 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
1220 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1221 (unsigned long) b->offset);
1229 #ifdef CONFIG_JFFS2_SUMMARY
1230 static u32 sum_get_unaligned32(u32 *ptr)
1235 val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
1237 return __le32_to_cpu(val);
1240 static u16 sum_get_unaligned16(u16 *ptr)
1245 val = *p | (*(p + 1) << 8);
1247 return __le16_to_cpu(val);
1250 #define dbg_summary(...) do {} while (0);
1252 * Process the stored summary information - helper function for
1253 * jffs2_sum_scan_sumnode()
1256 static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
1257 struct jffs2_raw_summary *summary,
1264 for (pass = 0; pass < 2; pass++) {
1267 for (i = 0; i < summary->sum_num; i++) {
1268 struct jffs2_sum_unknown_flash *spu = sp;
1269 dbg_summary("processing summary index %d\n", i);
1271 switch (sum_get_unaligned16(&spu->nodetype)) {
1272 case JFFS2_NODETYPE_INODE: {
1273 struct jffs2_sum_inode_flash *spi;
1277 b = insert_node(&pL->frag);
1280 b->offset = (u32)part->offset +
1282 sum_get_unaligned32(
1284 b->version = sum_get_unaligned32(
1286 b->ino = sum_get_unaligned32(
1288 b->datacrc = CRC_UNKNOWN;
1291 sp += JFFS2_SUMMARY_INODE_SIZE;
1295 case JFFS2_NODETYPE_DIRENT: {
1296 struct jffs2_sum_dirent_flash *spd;
1299 b = insert_node(&pL->dir);
1302 b->offset = (u32)part->offset +
1304 sum_get_unaligned32(
1306 b->version = sum_get_unaligned32(
1308 b->pino = sum_get_unaligned32(
1310 b->datacrc = CRC_UNKNOWN;
1313 sp += JFFS2_SUMMARY_DIRENT_SIZE(
1319 uint16_t nodetype = sum_get_unaligned16(
1321 printf("Unsupported node type %x found"
1324 if ((nodetype & JFFS2_COMPAT_MASK) ==
1325 JFFS2_FEATURE_INCOMPAT)
1335 /* Process the summary node - called from jffs2_scan_eraseblock() */
1336 int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
1337 struct jffs2_raw_summary *summary, uint32_t sumsize,
1340 struct jffs2_unknown_node crcnode;
1341 int ret, __maybe_unused ofs;
1344 ofs = part->sector_size - sumsize;
1346 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
1347 offset, offset + ofs, sumsize);
1349 /* OK, now check for node validity and CRC */
1350 crcnode.magic = JFFS2_MAGIC_BITMASK;
1351 crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
1352 crcnode.totlen = summary->totlen;
1353 crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
1355 if (summary->hdr_crc != crc) {
1356 dbg_summary("Summary node header is corrupt (bad CRC or "
1357 "no summary at all)\n");
1361 if (summary->totlen != sumsize) {
1362 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
1366 crc = crc32_no_comp(0, (uchar *)summary,
1367 sizeof(struct jffs2_raw_summary)-8);
1369 if (summary->node_crc != crc) {
1370 dbg_summary("Summary node is corrupt (bad CRC)\n");
1374 crc = crc32_no_comp(0, (uchar *)summary->sum,
1375 sumsize - sizeof(struct jffs2_raw_summary));
1377 if (summary->sum_crc != crc) {
1378 dbg_summary("Summary node data is corrupt (bad CRC)\n");
1382 if (summary->cln_mkr)
1383 dbg_summary("Summary : CLEANMARKER node \n");
1385 ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
1386 if (ret == -EBADMSG)
1389 return ret; /* real error */
1394 putstr("Summary node crc error, skipping summary information.\n");
1398 #endif /* CONFIG_JFFS2_SUMMARY */
1400 #ifdef DEBUG_FRAGMENTS
1402 dump_fragments(struct b_lists *pL)
1405 struct jffs2_raw_inode ojNode;
1406 struct jffs2_raw_inode *jNode;
1408 putstr("\r\n\r\n******The fragment Entries******\r\n");
1409 b = pL->frag.listHead;
1411 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1412 sizeof(ojNode), &ojNode);
1413 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1414 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1415 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1416 putLabeledWord("\tbuild_list: version = ", jNode->version);
1417 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1418 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1419 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1420 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1421 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1422 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1423 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1424 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
1425 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
1431 #ifdef DEBUG_DIRENTS
1433 dump_dirents(struct b_lists *pL)
1436 struct jffs2_raw_dirent *jDir;
1438 putstr("\r\n\r\n******The directory Entries******\r\n");
1439 b = pL->dir.listHead;
1441 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1444 putnstr(jDir->name, jDir->nsize);
1445 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1446 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1447 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1448 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1449 putLabeledWord("\tbuild_list: version = ", jDir->version);
1450 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1451 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1452 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1453 putLabeledWord("\tbuild_list: type = ", jDir->type);
1454 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1455 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
1456 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
1458 put_fl_mem(jDir, pL->readbuf);
1463 #define DEFAULT_EMPTY_SCAN_SIZE 256
1465 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
1467 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
1470 return DEFAULT_EMPTY_SCAN_SIZE;
1474 jffs2_1pass_build_lists(struct part_info * part)
1477 union jffs2_node_union *node;
1487 nr_sectors = lldiv(part->size, part->sector_size);
1488 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
1489 /* jffs2 list building enterprise nope. in newer versions the overhead is */
1490 /* only about 5 %. not enough to inconvenience people for. */
1493 /* if we are building a list we need to refresh the cache. */
1494 jffs_init_1pass_list(part);
1495 pL = (struct b_lists *)part->jffs2_priv;
1496 buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
1497 puts ("Scanning JFFS2 FS: ");
1499 /* start at the beginning of the partition */
1500 for (i = 0; i < nr_sectors; i++) {
1501 uint32_t sector_ofs = i * part->sector_size;
1502 uint32_t buf_ofs = sector_ofs;
1504 uint32_t ofs, prevofs;
1505 #ifdef CONFIG_JFFS2_SUMMARY
1506 struct jffs2_sum_marker *sm;
1507 void *sumptr = NULL;
1511 /* Indicates a sector with a CLEANMARKER was found */
1512 int clean_sector = 0;
1513 struct jffs2_unknown_node crcnode;
1516 /* Set buf_size to maximum length */
1517 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
1520 #ifdef CONFIG_JFFS2_SUMMARY
1521 buf_len = sizeof(*sm);
1523 /* Read as much as we want into the _end_ of the preallocated
1526 get_fl_mem(part->offset + sector_ofs + part->sector_size -
1527 buf_len, buf_len, buf + buf_size - buf_len);
1529 sm = (void *)buf + buf_size - sizeof(*sm);
1530 if (sm->magic == JFFS2_SUM_MAGIC) {
1531 sumlen = part->sector_size - sm->offset;
1532 sumptr = buf + buf_size - sumlen;
1534 /* Now, make sure the summary itself is available */
1535 if (sumlen > buf_size) {
1536 /* Need to kmalloc for this. */
1537 sumptr = malloc(sumlen);
1539 putstr("Can't get memory for summary "
1542 jffs2_free_cache(part);
1545 memcpy(sumptr + sumlen - buf_len, buf +
1546 buf_size - buf_len, buf_len);
1548 if (buf_len < sumlen) {
1549 /* Need to read more so that the entire summary
1552 get_fl_mem(part->offset + sector_ofs +
1553 part->sector_size - sumlen,
1554 sumlen - buf_len, sumptr);
1559 ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
1562 if (buf_size && sumlen > buf_size)
1566 jffs2_free_cache(part);
1573 #endif /* CONFIG_JFFS2_SUMMARY */
1575 buf_len = EMPTY_SCAN_SIZE(part->sector_size);
1577 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
1579 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
1582 /* Scan only 4KiB of 0xFF before declaring it's empty */
1583 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
1584 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
1587 if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
1593 * Set buf_size down to the minimum size required.
1594 * This prevents reading in chunks of flash data unnecessarily.
1596 buf_size = sizeof(union jffs2_node_union);
1599 while (ofs < sector_ofs + part->sector_size) {
1600 if (ofs == prevofs) {
1601 printf("offset %08x already seen, skip\n", ofs);
1607 if (sector_ofs + part->sector_size <
1608 ofs + sizeof(struct jffs2_unknown_node))
1610 if (buf_ofs + buf_len <
1611 ofs + sizeof(struct jffs2_unknown_node)) {
1612 buf_len = min_t(uint32_t, buf_size, sector_ofs
1613 + part->sector_size - ofs);
1614 get_fl_mem((u32)part->offset + ofs, buf_len,
1619 node = (union jffs2_node_union *)&buf[ofs - buf_ofs];
1621 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
1626 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
1627 part->sector_size)/8,
1630 inbuf_ofs = ofs - buf_ofs;
1631 while (inbuf_ofs < scan_end) {
1632 if (*(uint32_t *)(&buf[inbuf_ofs]) !=
1641 * If this sector had a clean marker at the
1642 * beginning, and immediately following this
1643 * have been a bunch of FF bytes, treat the
1644 * entire sector as empty.
1649 /* See how much more there is to read in this
1652 buf_len = min_t(uint32_t, buf_size,
1654 part->sector_size - ofs);
1656 /* No more to read. Break out of main
1657 * loop without marking this range of
1658 * empty space as dirty (because it's
1664 get_fl_mem((u32)part->offset + ofs, buf_len,
1670 * Found something not erased in the sector, so reset
1671 * the 'clean_sector' flag.
1674 if (node->u.magic != JFFS2_MAGIC_BITMASK) {
1680 crcnode.magic = node->u.magic;
1681 crcnode.nodetype = node->u.nodetype | JFFS2_NODE_ACCURATE;
1682 crcnode.totlen = node->u.totlen;
1683 crcnode.hdr_crc = node->u.hdr_crc;
1684 if (!hdr_crc(&crcnode)) {
1690 if (ofs + node->u.totlen > sector_ofs + part->sector_size) {
1696 if (!(node->u.nodetype & JFFS2_NODE_ACCURATE)) {
1697 DEBUGF("Obsolete node type: %x len %d offset 0x%x\n",
1698 node->u.nodetype, node->u.totlen, ofs);
1699 ofs += ((node->u.totlen + 3) & ~3);
1704 /* if its a fragment add it */
1705 switch (node->u.nodetype) {
1706 case JFFS2_NODETYPE_INODE:
1707 if (buf_ofs + buf_len <
1708 ofs + sizeof(struct jffs2_raw_inode)) {
1709 buf_len = min_t(uint32_t,
1710 sizeof(struct jffs2_raw_inode),
1714 get_fl_mem((u32)part->offset + ofs,
1719 if (!inode_crc((struct jffs2_raw_inode *)node))
1722 b = insert_node(&pL->frag);
1725 jffs2_free_cache(part);
1728 b->offset = (u32)part->offset + ofs;
1729 b->version = node->i.version;
1730 b->ino = node->i.ino;
1731 if (max_totlen < node->u.totlen)
1732 max_totlen = node->u.totlen;
1734 case JFFS2_NODETYPE_DIRENT:
1735 if (buf_ofs + buf_len < ofs + sizeof(struct
1740 buf_len = min_t(uint32_t,
1745 get_fl_mem((u32)part->offset + ofs,
1751 if (!dirent_crc((struct jffs2_raw_dirent *)
1758 if (! (counterN%100))
1760 b = insert_node(&pL->dir);
1763 jffs2_free_cache(part);
1766 b->offset = (u32)part->offset + ofs;
1767 b->version = node->d.version;
1768 b->pino = node->d.pino;
1769 if (max_totlen < node->u.totlen)
1770 max_totlen = node->u.totlen;
1773 case JFFS2_NODETYPE_CLEANMARKER:
1774 if (node->u.totlen != sizeof(struct jffs2_unknown_node))
1775 printf("OOPS Cleanmarker has bad size "
1778 sizeof(struct jffs2_unknown_node));
1779 if (node->u.totlen ==
1780 sizeof(struct jffs2_unknown_node) &&
1781 ofs == sector_ofs) {
1783 * Found a CLEANMARKER at the beginning
1784 * of the sector. It's in the correct
1785 * place with correct size and CRC.
1790 case JFFS2_NODETYPE_PADDING:
1791 if (node->u.totlen <
1792 sizeof(struct jffs2_unknown_node))
1793 printf("OOPS Padding has bad size "
1796 sizeof(struct jffs2_unknown_node));
1798 case JFFS2_NODETYPE_SUMMARY:
1801 printf("Unknown node type: %x len %d offset 0x%x\n",
1803 node->u.totlen, ofs);
1805 ofs += ((node->u.totlen + 3) & ~3);
1811 #if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
1815 sort_list(&pL->frag);
1816 sort_list(&pL->dir);
1818 putstr("\b\b done.\r\n"); /* close off the dots */
1820 /* We don't care if malloc failed - then each read operation will
1821 * allocate its own buffer as necessary (NAND) or will read directly
1824 pL->readbuf = malloc(max_totlen);
1826 /* turn the lcd back on. */
1830 putLabeledWord("dir entries = ", pL->dir.listCount);
1831 putLabeledWord("frag entries = ", pL->frag.listCount);
1832 putLabeledWord("+4 increments = ", counter4);
1833 putLabeledWord("+file_offset increments = ", counterF);
1837 #ifdef DEBUG_DIRENTS
1841 #ifdef DEBUG_FRAGMENTS
1845 /* give visual feedback that we are done scanning the flash */
1846 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
1851 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1854 struct jffs2_raw_inode ojNode;
1855 struct jffs2_raw_inode *jNode;
1858 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1859 piL->compr_info[i].num_frags = 0;
1860 piL->compr_info[i].compr_sum = 0;
1861 piL->compr_info[i].decompr_sum = 0;
1864 b = pL->frag.listHead;
1866 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1867 sizeof(ojNode), &ojNode);
1868 if (jNode->compr < JFFS2_NUM_COMPR) {
1869 piL->compr_info[jNode->compr].num_frags++;
1870 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1871 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1878 static struct b_lists *
1879 jffs2_get_list(struct part_info * part, const char *who)
1881 /* copy requested part_info struct pointer to global location */
1882 current_part = part;
1884 if (jffs2_1pass_rescan_needed(part)) {
1885 if (!jffs2_1pass_build_lists(part)) {
1886 printf("%s: Failed to scan JFFSv2 file structure\n", who);
1890 return (struct b_lists *)part->jffs2_priv;
1893 /* Print directory / file contents */
1895 jffs2_1pass_ls(struct part_info * part, const char *fname)
1901 if (! (pl = jffs2_get_list(part, "ls")))
1904 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1905 putstr("ls: Failed to scan jffs2 file structure\r\n");
1910 putLabeledWord("found file at inode = ", inode);
1911 putLabeledWord("read_inode returns = ", ret);
1917 /* Load a file from flash into memory. fname can be a full path */
1919 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1926 if (! (pl = jffs2_get_list(part, "load")))
1929 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1930 putstr("load: Failed to find inode\r\n");
1934 /* Resolve symlinks */
1935 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1936 putstr("load: Failed to resolve inode structure\r\n");
1940 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1941 putstr("load: Failed to read inode\r\n");
1945 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1946 (unsigned long) dest, ret);
1950 /* Return information about the fs on this partition */
1952 jffs2_1pass_info(struct part_info * part)
1954 struct b_jffs2_info info;
1958 if (! (pl = jffs2_get_list(part, "info")))
1961 jffs2_1pass_fill_info(pl, &info);
1962 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1963 printf ("Compression: %s\n"
1964 "\tfrag count: %d\n"
1965 "\tcompressed sum: %d\n"
1966 "\tuncompressed sum: %d\n",
1968 info.compr_info[i].num_frags,
1969 info.compr_info[i].compr_sum,
1970 info.compr_info[i].decompr_sum);