3 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
6 #include <linux/stat.h>
7 #include <linux/time.h>
9 #include <jffs2/jffs2.h>
10 #include <jffs2/jffs2_1pass.h>
13 #include "jffs2_nand_private.h"
15 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
17 /* Debugging switches */
18 #undef DEBUG_DIRENTS /* print directory entry list after scan */
19 #undef DEBUG_FRAGMENTS /* print fragment list after scan */
20 #undef DEBUG /* enable debugging messages */
23 # define DEBUGF(fmt,args...) printf(fmt ,##args)
25 # define DEBUGF(fmt,args...)
28 static int nanddev = -1; /* nand device of current partition */
29 static nand_info_t *nand;
31 /* Compression names */
32 static char *compr_names[] = {
40 #if defined(CONFIG_JFFS2_LZO_LZARI)
47 static char spinner[] = { '|', '/', '-', '\\' };
49 /* Memory management */
52 struct mem_block *next;
57 free_nodes(struct b_list *list)
59 while (list->listMemBase != NULL) {
60 struct mem_block *next = list->listMemBase->next;
61 free(list->listMemBase);
62 list->listMemBase = next;
66 static struct b_node *
67 add_node(struct b_list *list, int size)
70 struct mem_block *memBase;
73 memBase = list->listMemBase;
75 index = memBase->index;
77 if (memBase == NULL || index >= NODE_CHUNK) {
78 /* we need more space before we continue */
79 memBase = mmalloc(sizeof(struct mem_block) + NODE_CHUNK * size);
80 if (memBase == NULL) {
81 putstr("add_node: malloc failed\n");
84 memBase->next = list->listMemBase;
87 /* now we have room to add it. */
88 b = (struct b_node *)&memBase->nodes[size * index];
91 memBase->index = index;
92 list->listMemBase = memBase;
97 static struct b_node *
98 insert_node(struct b_list *list, struct b_node *new)
100 #ifdef CFG_JFFS2_SORT_FRAGMENTS
101 struct b_node *b, *prev;
103 if (list->listTail != NULL && list->listCompare(new, list->listTail))
104 prev = list->listTail;
105 else if (list->listLast != NULL && list->listCompare(new, list->listLast))
106 prev = list->listLast;
110 for (b = (prev ? prev->next : list->listHead);
111 b != NULL && list->listCompare(new, b);
112 prev = b, b = b->next) {
116 list->listLast = prev;
123 list->listHead = new;
127 new->next = (struct b_node *) NULL;
128 if (list->listTail != NULL) {
129 list->listTail->next = new;
130 list->listTail = new;
132 list->listTail = list->listHead = new;
139 static struct b_node *
140 insert_inode(struct b_list *list, struct jffs2_raw_inode *node, u32 offset)
144 if (!(new = (struct b_inode *)add_node(list, sizeof(struct b_inode)))) {
145 putstr("add_node failed!\r\n");
148 new->offset = offset;
149 new->version = node->version;
150 new->ino = node->ino;
151 new->isize = node->isize;
152 new->csize = node->csize;
154 return insert_node(list, (struct b_node *)new);
157 static struct b_node *
158 insert_dirent(struct b_list *list, struct jffs2_raw_dirent *node, u32 offset)
160 struct b_dirent *new;
162 if (!(new = (struct b_dirent *)add_node(list, sizeof(struct b_dirent)))) {
163 putstr("add_node failed!\r\n");
166 new->offset = offset;
167 new->version = node->version;
168 new->pino = node->pino;
169 new->ino = node->ino;
170 new->nhash = full_name_hash(node->name, node->nsize);
171 new->nsize = node->nsize;
172 new->type = node->type;
174 return insert_node(list, (struct b_node *)new);
177 #ifdef CFG_JFFS2_SORT_FRAGMENTS
178 /* Sort data entries with the latest version last, so that if there
179 * is overlapping data the latest version will be used.
181 static int compare_inodes(struct b_node *new, struct b_node *old)
183 struct jffs2_raw_inode ojNew;
184 struct jffs2_raw_inode ojOld;
185 struct jffs2_raw_inode *jNew =
186 (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
187 struct jffs2_raw_inode *jOld =
188 (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
190 return jNew->version > jOld->version;
193 /* Sort directory entries so all entries in the same directory
194 * with the same name are grouped together, with the latest version
195 * last. This makes it easy to eliminate all but the latest version
196 * by marking the previous version dead by setting the inode to 0.
198 static int compare_dirents(struct b_node *new, struct b_node *old)
200 struct jffs2_raw_dirent ojNew;
201 struct jffs2_raw_dirent ojOld;
202 struct jffs2_raw_dirent *jNew =
203 (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
204 struct jffs2_raw_dirent *jOld =
205 (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
208 /* ascending sort by pino */
209 if (jNew->pino != jOld->pino)
210 return jNew->pino > jOld->pino;
212 /* pino is the same, so use ascending sort by nsize, so
213 * we don't do strncmp unless we really must.
215 if (jNew->nsize != jOld->nsize)
216 return jNew->nsize > jOld->nsize;
218 /* length is also the same, so use ascending sort by name
220 cmp = strncmp(jNew->name, jOld->name, jNew->nsize);
224 /* we have duplicate names in this directory, so use ascending
227 if (jNew->version > jOld->version) {
228 /* since jNew is newer, we know jOld is not valid, so
229 * mark it with inode 0 and it will not be used
240 jffs_init_1pass_list(struct part_info *part)
244 if (part->jffs2_priv != NULL) {
245 pL = (struct b_lists *)part->jffs2_priv;
246 free_nodes(&pL->frag);
247 free_nodes(&pL->dir);
250 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
251 pL = (struct b_lists *)part->jffs2_priv;
253 memset(pL, 0, sizeof(*pL));
254 #ifdef CFG_JFFS2_SORT_FRAGMENTS
255 pL->dir.listCompare = compare_dirents;
256 pL->frag.listCompare = compare_inodes;
262 /* find the inode from the slashless name given a parent */
264 jffs2_1pass_read_inode(struct b_lists *pL, u32 ino, char *dest,
267 struct b_inode *jNode;
269 u32 latestVersion = 0;
272 #ifdef CFG_JFFS2_SORT_FRAGMENTS
273 /* Find file size before loading any data, so fragments that
274 * start past the end of file can be ignored. A fragment
275 * that is partially in the file is loaded, so extra data may
276 * be loaded up to the next 4K boundary above the file size.
277 * This shouldn't cause trouble when loading kernel images, so
278 * we will live with it.
280 for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
281 if ((ino == jNode->ino)) {
282 /* get actual file length from the newest node */
283 if (jNode->version >= latestVersion) {
284 totalSize = jNode->isize;
285 latestVersion = jNode->version;
291 for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
292 if ((ino != jNode->ino))
294 #ifndef CFG_JFFS2_SORT_FRAGMENTS
295 /* get actual file length from the newest node */
296 if (jNode->version >= latestVersion) {
297 totalSize = jNode->isize;
298 latestVersion = jNode->version;
303 char data[4096 + sizeof(struct jffs2_raw_inode)];
304 struct jffs2_raw_inode *inode;
307 inode = (struct jffs2_raw_inode *)&data;
308 len = sizeof(struct jffs2_raw_inode);
311 nand_read(nand, jNode->offset, &len, inode);
312 /* ignore data behind latest known EOF */
313 if (inode->offset > totalSize)
317 stat->st_mtime = inode->mtime;
318 stat->st_mode = inode->mode;
319 stat->st_ino = inode->ino;
320 stat->st_size = totalSize;
326 src = ((char *) inode) + sizeof(struct jffs2_raw_inode);
327 dst = (char *) (dest + inode->offset);
329 switch (inode->compr) {
330 case JFFS2_COMPR_NONE:
332 memcpy(dst, src, inode->dsize);
334 case JFFS2_COMPR_ZERO:
336 memset(dst, 0, inode->dsize);
338 case JFFS2_COMPR_RTIME:
340 rtime_decompress(src, dst, inode->csize, inode->dsize);
342 case JFFS2_COMPR_DYNRUBIN:
343 /* this is slow but it works */
345 dynrubin_decompress(src, dst, inode->csize, inode->dsize);
347 case JFFS2_COMPR_ZLIB:
348 ret = zlib_decompress(src, dst, inode->csize, inode->dsize);
350 #if defined(CONFIG_JFFS2_LZO_LZARI)
351 case JFFS2_COMPR_LZO:
352 ret = lzo_decompress(src, dst, inode->csize, inode->dsize);
354 case JFFS2_COMPR_LZARI:
355 ret = lzari_decompress(src, dst, inode->csize, inode->dsize);
360 putLabeledWord("UNKOWN COMPRESSION METHOD = ", inode->compr);
369 /* find the inode from the slashless name given a parent */
371 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
373 struct b_dirent *jDir;
374 int len = strlen(name); /* name is assumed slash free */
375 unsigned int nhash = full_name_hash(name, len);
379 /* we need to search all and return the inode with the highest version */
380 for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
381 if ((pino == jDir->pino) && (jDir->ino) && /* 0 for unlink */
382 (len == jDir->nsize) && (nhash == jDir->nhash)) {
383 /* TODO: compare name */
384 if (jDir->version < version)
387 if (jDir->version == version && inode != 0) {
388 /* I'm pretty sure this isn't legal */
389 putstr(" ** ERROR ** ");
390 // putnstr(jDir->name, jDir->nsize);
391 // putLabeledWord(" has dup version =", version);
394 version = jDir->version;
400 char *mkmodestr(unsigned long mode, char *str)
402 static const char *l = "xwr";
406 switch (mode & S_IFMT) {
407 case S_IFDIR: str[0] = 'd'; break;
408 case S_IFBLK: str[0] = 'b'; break;
409 case S_IFCHR: str[0] = 'c'; break;
410 case S_IFIFO: str[0] = 'f'; break;
411 case S_IFLNK: str[0] = 'l'; break;
412 case S_IFSOCK: str[0] = 's'; break;
413 case S_IFREG: str[0] = '-'; break;
414 default: str[0] = '?';
417 for(i = 0; i < 9; i++) {
419 str[9-i] = (mode & mask)?c:'-';
423 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
424 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
425 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
430 static inline void dump_stat(struct stat *st, const char *name)
435 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
438 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
440 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
441 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
444 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
445 st->st_size, s, name);
448 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
452 dump_inode(struct b_lists *pL, struct b_dirent *d, struct b_inode *i)
454 char fname[JFFS2_MAX_NAME_LEN + 1];
458 if(!d || !i) return -1;
460 nand_read(nand, d->offset + sizeof(struct jffs2_raw_dirent),
462 fname[d->nsize] = '\0';
464 memset(&st, 0, sizeof(st));
466 jffs2_1pass_read_inode(pL, i->ino, NULL, &st);
468 dump_stat(&st, fname);
470 if (d->type == DT_LNK) {
471 unsigned char *src = (unsigned char *) (&i[1]);
473 putnstr(src, (int)i->dsize);
481 /* list inodes with the given pino */
483 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
485 struct b_dirent *jDir;
488 for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
489 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
490 struct b_inode *jNode = (struct b_inode *)pL->frag.listHead;
491 struct b_inode *i = NULL;
494 if (jNode->ino == jDir->ino && jNode->version >= i_version) {
495 i_version = jNode->version;
500 dump_inode(pL, jDir, i);
507 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
511 char working_tmp[256];
514 /* discard any leading slash */
516 while (fname[i] == '/')
518 strcpy(tmp, &fname[i]);
520 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
522 strncpy(working_tmp, tmp, c - tmp);
523 working_tmp[c - tmp] = '\0';
525 putstr("search_inode: tmp = ");
528 putstr("search_inode: wtmp = ");
531 putstr("search_inode: c = ");
535 for (i = 0; i < strlen(c) - 1; i++)
539 putstr("search_inode: post tmp = ");
544 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
545 putstr("find_inode failed for name=");
551 /* this is for the bare filename, directories have already been mapped */
552 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
553 putstr("find_inode failed for name=");
563 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
565 struct b_dirent *jDir;
566 struct b_inode *jNode;
567 u8 jDirFoundType = 0;
568 u32 jDirFoundIno = 0;
569 u32 jDirFoundPino = 0;
570 char tmp[JFFS2_MAX_NAME_LEN + 1];
574 /* we need to search all and return the inode with the highest version */
575 for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
576 if (ino == jDir->ino) {
577 if (jDir->version < version)
580 if (jDir->version == version && jDirFoundType) {
581 /* I'm pretty sure this isn't legal */
582 putstr(" ** ERROR ** ");
583 // putnstr(jDir->name, jDir->nsize);
584 // putLabeledWord(" has dup version (resolve) = ",
588 jDirFoundType = jDir->type;
589 jDirFoundIno = jDir->ino;
590 jDirFoundPino = jDir->pino;
591 version = jDir->version;
594 /* now we found the right entry again. (shoulda returned inode*) */
595 if (jDirFoundType != DT_LNK)
598 /* it's a soft link so we follow it again. */
599 for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
600 if (jNode->ino == jDirFoundIno) {
601 size_t len = jNode->csize;
602 nand_read(nand, jNode->offset + sizeof(struct jffs2_raw_inode), &len, &tmp);
603 tmp[jNode->csize] = '\0';
607 /* ok so the name of the new file to find is in tmp */
608 /* if it starts with a slash it is root based else shared dirs */
612 pino = jDirFoundPino;
614 return jffs2_1pass_search_inode(pL, tmp, pino);
618 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
622 char working_tmp[256];
625 /* discard any leading slash */
627 while (fname[i] == '/')
629 strcpy(tmp, &fname[i]);
630 working_tmp[0] = '\0';
631 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
633 strncpy(working_tmp, tmp, c - tmp);
634 working_tmp[c - tmp] = '\0';
635 for (i = 0; i < strlen(c) - 1; i++)
638 /* only a failure if we arent looking at top level */
639 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
641 putstr("find_inode failed for name=");
648 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
649 putstr("find_inode failed for name=");
654 /* this is for the bare filename, directories have already been mapped */
655 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
656 putstr("find_inode failed for name=");
666 jffs2_1pass_rescan_needed(struct part_info *part)
669 struct jffs2_unknown_node onode;
670 struct jffs2_unknown_node *node;
671 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
673 if (part->jffs2_priv == 0){
674 DEBUGF ("rescan: First time in use\n");
677 /* if we have no list, we need to rescan */
678 if (pL->frag.listCount == 0) {
679 DEBUGF ("rescan: fraglist zero\n");
683 /* or if we are scanning a new partition */
684 if (pL->partOffset != part->offset) {
685 DEBUGF ("rescan: different partition\n");
689 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
690 if (nanddev != (int)part->usr_priv - 1) {
691 DEBUGF ("rescan: nand device changed\n");
694 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
697 /* but suppose someone reflashed a partition at the same offset... */
698 b = pL->dir.listHead;
700 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
701 sizeof(onode), &onode);
702 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
703 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
704 (unsigned long) b->offset);
713 #ifdef DEBUG_FRAGMENTS
715 dump_fragments(struct b_lists *pL)
718 struct jffs2_raw_inode ojNode;
719 struct jffs2_raw_inode *jNode;
721 putstr("\r\n\r\n******The fragment Entries******\r\n");
722 b = pL->frag.listHead;
724 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
725 sizeof(ojNode), &ojNode);
726 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
727 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
728 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
729 putLabeledWord("\tbuild_list: version = ", jNode->version);
730 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
731 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
732 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
733 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
734 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
735 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
736 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
737 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
738 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
746 dump_dirents(struct b_lists *pL)
749 struct jffs2_raw_dirent *jDir;
751 putstr("\r\n\r\n******The directory Entries******\r\n");
752 b = pL->dir.listHead;
754 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
756 putnstr(jDir->name, jDir->nsize);
757 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
758 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
759 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
760 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
761 putLabeledWord("\tbuild_list: version = ", jDir->version);
762 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
763 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
764 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
765 putLabeledWord("\tbuild_list: type = ", jDir->type);
766 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
767 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
768 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
776 jffs2_fill_scan_buf(nand_info_t *nand, unsigned char *buf,
777 unsigned ofs, unsigned len)
783 ret = nand_read(nand, ofs, &olen, buf);
785 printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret);
789 printf("Read at 0x%x gave only 0x%x bytes\n", ofs, olen);
795 #define EMPTY_SCAN_SIZE 1024
797 jffs2_1pass_build_lists(struct part_info * part)
800 struct jffs2_unknown_node *node;
801 unsigned nr_blocks, sectorsize, ofs, offset;
809 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
810 nanddev = (int)part->usr_priv - 1;
811 nand = &nand_info[nanddev];
812 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
814 /* if we are building a list we need to refresh the cache. */
815 jffs_init_1pass_list(part);
816 pL = (struct b_lists *)part->jffs2_priv;
817 pL->partOffset = part->offset;
818 puts ("Scanning JFFS2 FS: ");
820 sectorsize = nand->erasesize;
821 nr_blocks = part->size / sectorsize;
822 buf = malloc(sectorsize);
826 for (i = 0; i < nr_blocks; i++) {
827 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
829 offset = part->offset + i * sectorsize;
831 if (nand_block_isbad(nand, offset))
834 if (jffs2_fill_scan_buf(nand, buf, offset, EMPTY_SCAN_SIZE))
838 /* Scan only 4KiB of 0xFF before declaring it's empty */
839 while (ofs < EMPTY_SCAN_SIZE && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
841 if (ofs == EMPTY_SCAN_SIZE)
844 if (jffs2_fill_scan_buf(nand, buf + EMPTY_SCAN_SIZE, offset + EMPTY_SCAN_SIZE, sectorsize - EMPTY_SCAN_SIZE))
848 while (ofs < sectorsize - sizeof(struct jffs2_unknown_node)) {
849 node = (struct jffs2_unknown_node *)&buf[ofs];
850 if (node->magic != JFFS2_MAGIC_BITMASK || !hdr_crc(node)) {
856 /* if its a fragment add it */
857 if (node->nodetype == JFFS2_NODETYPE_INODE &&
858 inode_crc((struct jffs2_raw_inode *) node)) {
859 if (insert_inode(&pL->frag, (struct jffs2_raw_inode *) node,
863 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
864 dirent_crc((struct jffs2_raw_dirent *) node) &&
865 dirent_name_crc((struct jffs2_raw_dirent *) node)) {
866 if (! (counterN%100))
868 if (insert_dirent(&pL->dir, (struct jffs2_raw_dirent *) node,
873 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
874 if (node->totlen != sizeof(struct jffs2_unknown_node))
875 printf("OOPS Cleanmarker has bad size "
876 "%d != %d\n", node->totlen,
877 sizeof(struct jffs2_unknown_node));
878 } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
879 if (node->totlen < sizeof(struct jffs2_unknown_node))
880 printf("OOPS Padding has bad size "
881 "%d < %d\n", node->totlen,
882 sizeof(struct jffs2_unknown_node));
884 printf("Unknown node type: %x len %d "
885 "offset 0x%x\n", node->nodetype,
886 node->totlen, offset);
888 offset += ((node->totlen + 3) & ~3);
889 ofs += ((node->totlen + 3) & ~3);
894 putstr("\b\b done.\r\n"); /* close off the dots */
897 putLabeledWord("dir entries = ", pL->dir.listCount);
898 putLabeledWord("frag entries = ", pL->frag.listCount);
899 putLabeledWord("+4 increments = ", counter4);
900 putLabeledWord("+file_offset increments = ", counterF);
907 #ifdef DEBUG_FRAGMENTS
911 /* give visual feedback that we are done scanning the flash */
912 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
920 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
923 struct jffs2_raw_inode ojNode;
924 struct jffs2_raw_inode *jNode;
927 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
928 piL->compr_info[i].num_frags = 0;
929 piL->compr_info[i].compr_sum = 0;
930 piL->compr_info[i].decompr_sum = 0;
933 b = pL->frag.listHead;
935 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
936 sizeof(ojNode), &ojNode);
937 if (jNode->compr < JFFS2_NUM_COMPR) {
938 piL->compr_info[jNode->compr].num_frags++;
939 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
940 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
949 static struct b_lists *
950 jffs2_get_list(struct part_info * part, const char *who)
952 if (jffs2_1pass_rescan_needed(part)) {
953 if (!jffs2_1pass_build_lists(part)) {
954 printf("%s: Failed to scan JFFSv2 file structure\n", who);
958 return (struct b_lists *)part->jffs2_priv;
962 /* Print directory / file contents */
964 jffs2_1pass_ls(struct part_info * part, const char *fname)
970 if (! (pl = jffs2_get_list(part, "ls")))
973 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
974 putstr("ls: Failed to scan jffs2 file structure\r\n");
979 putLabeledWord("found file at inode = ", inode);
980 putLabeledWord("read_inode returns = ", ret);
987 /* Load a file from flash into memory. fname can be a full path */
989 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
996 if (! (pl = jffs2_get_list(part, "load")))
999 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1000 putstr("load: Failed to find inode\r\n");
1004 /* Resolve symlinks */
1005 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1006 putstr("load: Failed to resolve inode structure\r\n");
1010 if ((ret = jffs2_1pass_read_inode(pl, inode, dest, NULL)) < 0) {
1011 putstr("load: Failed to read inode\r\n");
1015 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1016 (unsigned long) dest, ret);
1020 /* Return information about the fs on this partition */
1022 jffs2_1pass_info(struct part_info * part)
1024 struct b_jffs2_info info;
1028 if (! (pl = jffs2_get_list(part, "info")))
1031 jffs2_1pass_fill_info(pl, &info);
1032 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1033 printf ("Compression: %s\n"
1034 "\tfrag count: %d\n"
1035 "\tcompressed sum: %d\n"
1036 "\tuncompressed sum: %d\n",
1038 info.compr_info[i].num_frags,
1039 info.compr_info[i].compr_sum,
1040 info.compr_info[i].decompr_sum);
1045 #endif /* CFG_CMD_JFFS2 */