]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - fs/jffs2/jffs2_1pass.c
* Avoid flicker on the TRAB's VFD by synchronizing the enable with
[karo-tx-uboot.git] / fs / jffs2 / jffs2_1pass.c
1 /* vi: set sw=4 ts=4: */
2 /*
3 -------------------------------------------------------------------------
4  * Filename:      jffs2.c
5  * Version:       $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
6  * Copyright:     Copyright (C) 2001, Russ Dill
7  * Author:        Russ Dill <Russ.Dill@asu.edu>
8  * Description:   Module to load kernel from jffs2
9  *-----------------------------------------------------------------------*/
10 /*
11  * some portions of this code are taken from jffs2, and as such, the
12  * following copyright notice is included.
13  *
14  * JFFS2 -- Journalling Flash File System, Version 2.
15  *
16  * Copyright (C) 2001 Red Hat, Inc.
17  *
18  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
19  *
20  * The original JFFS, from which the design for JFFS2 was derived,
21  * was designed and implemented by Axis Communications AB.
22  *
23  * The contents of this file are subject to the Red Hat eCos Public
24  * License Version 1.1 (the "Licence"); you may not use this file
25  * except in compliance with the Licence.  You may obtain a copy of
26  * the Licence at http://www.redhat.com/
27  *
28  * Software distributed under the Licence is distributed on an "AS IS"
29  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
30  * See the Licence for the specific language governing rights and
31  * limitations under the Licence.
32  *
33  * The Original Code is JFFS2 - Journalling Flash File System, version 2
34  *
35  * Alternatively, the contents of this file may be used under the
36  * terms of the GNU General Public License version 2 (the "GPL"), in
37  * which case the provisions of the GPL are applicable instead of the
38  * above.  If you wish to allow the use of your version of this file
39  * only under the terms of the GPL and not to allow others to use your
40  * version of this file under the RHEPL, indicate your decision by
41  * deleting the provisions above and replace them with the notice and
42  * other provisions required by the GPL.  If you do not delete the
43  * provisions above, a recipient may use your version of this file
44  * under either the RHEPL or the GPL.
45  *
46  * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
47  *
48  */
49
50 /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
51  * bag to throw up into before reading this code. I looked through the jffs2
52  * code, the caching scheme is very elegant. I tried to keep the version
53  * for a bootloader as small and simple as possible. Instead of worring about
54  * unneccesary data copies, node scans, etc, I just optimized for the known
55  * common case, a kernel, which looks like:
56  *      (1) most pages are 4096 bytes
57  *      (2) version numbers are somewhat sorted in acsending order
58  *      (3) multiple compressed blocks making up one page is uncommon
59  *
60  * So I create a linked list of decending version numbers (insertions at the
61  * head), and then for each page, walk down the list, until a matching page
62  * with 4096 bytes is found, and then decompress the watching pages in
63  * reverse order.
64  *
65  */
66
67 /*
68  * Adapted by Nye Liu <nyet@zumanetworks.com> and
69  * Rex Feany <rfeany@zumanetworks.com>
70  * on Jan/2002 for U-Boot.
71  *
72  * Clipped out all the non-1pass functions, cleaned up warnings,
73  * wrappers, etc. No major changes to the code.
74  * Please, he really means it when he said have a paper bag
75  * handy. We needed it ;).
76  *
77  */
78
79 /*
80  * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
81  *
82  * - overhaul of the memory management. Removed much of the "paper-bagging"
83  *   in that part of the code, fixed several bugs, now frees memory when
84  *   partition is changed.
85  *   It's still ugly :-(
86  * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
87  *   was incorrect. Removed a bit of the paper-bagging as well.
88  * - removed double crc calculation for fragment headers in jffs2_private.h
89  *   for speedup.
90  * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
91  * - spinning wheel now spins depending on how much memory has been scanned
92  * - lots of small changes all over the place to "improve" readability.
93  * - implemented fragment sorting to ensure that the newest data is copied
94  *   if there are multiple copies of fragments for a certain file offset.
95  *
96  * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS.
97  * Sorting is done while adding fragments to the lists, which is more or less a
98  * bubble sort. This takes a lot of time, and is most probably not an issue if
99  * the boot filesystem is always mounted readonly.
100  *
101  * You should define it if the boot filesystem is mounted writable, and updates
102  * to the boot files are done by copying files to that filesystem.
103  *
104  *
105  * There's a big issue left: endianess is completely ignored in this code. Duh!
106  *
107  *
108  * You still should have paper bags at hand :-(. The code lacks more or less
109  * any comment, and is still arcane and difficult to read in places. As this
110  * is incompatible with any new code from the jffs2 maintainers anyway, it
111  * should probably be dumped and replaced by something like jffs2reader!
112  */
113
114
115 #include <common.h>
116 #include <config.h>
117 #include <malloc.h>
118 #include <linux/stat.h>
119 #include <linux/time.h>
120
121 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
122
123 #include <jffs2/jffs2.h>
124 #include <jffs2/jffs2_1pass.h>
125
126 #include "jffs2_private.h"
127
128
129 #define NODE_CHUNK      1024    /* size of memory allocation chunk in b_nodes */
130 #define SPIN_BLKSIZE    18      /* spin after having scanned 1<<BLKSIZE bytes */
131
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 */
136
137
138 #ifdef  DEBUG
139 # define DEBUGF(fmt,args...)    printf(fmt ,##args)
140 #else
141 # define DEBUGF(fmt,args...)
142 #endif
143
144
145 /* Compression names */
146 static char *compr_names[] = {
147         "NONE",
148         "ZERO",
149         "RTIME",
150         "RUBINMIPS",
151         "COPY",
152         "DYNRUBIN",
153         "ZLIB"
154 };
155
156 /* Spinning wheel */
157 static char spinner[] = { '|', '/', '-', '\\' };
158
159 /* Memory management */
160 struct mem_block {
161         u32     index;
162         struct mem_block *next;
163         struct b_node nodes[NODE_CHUNK];
164 };
165
166
167 static void
168 free_nodes(struct b_list *list)
169 {
170         while (list->listMemBase != NULL) {
171                 struct mem_block *next = list->listMemBase->next;
172                 free( list->listMemBase );
173                 list->listMemBase = next;
174         }
175 }
176
177 static struct b_node *
178 add_node(struct b_list *list)
179 {
180         u32 index = 0;
181         struct mem_block *memBase;
182         struct b_node *b;
183
184         memBase = list->listMemBase;
185         if (memBase != NULL)
186                 index = memBase->index;
187 #if 0
188         putLabeledWord("add_node: index = ", index);
189         putLabeledWord("add_node: memBase = ", list->listMemBase);
190 #endif
191
192         if (memBase == NULL || index >= NODE_CHUNK) {
193                 /* we need more space before we continue */
194                 memBase = mmalloc(sizeof(struct mem_block));
195                 if (memBase == NULL) {
196                         putstr("add_node: malloc failed\n");
197                         return NULL;
198                 }
199                 memBase->next = list->listMemBase;
200                 index = 0;
201 #if 0
202                 putLabeledWord("add_node: alloced a new membase at ", *memBase);
203 #endif
204
205         }
206         /* now we have room to add it. */
207         b = &memBase->nodes[index];
208         index ++;
209
210         memBase->index = index;
211         list->listMemBase = memBase;
212         list->listCount++;
213         return b;
214 }
215
216 static struct b_node *
217 insert_node(struct b_list *list, u32 offset)
218 {
219         struct b_node *new;
220 #ifdef CFG_JFFS2_SORT_FRAGMENTS
221         struct b_node *b, *prev;
222 #endif
223
224         if (!(new = add_node(list))) {
225                 putstr("add_node failed!\r\n");
226                 return NULL;
227         }
228         new->offset = offset;
229
230 #ifdef CFG_JFFS2_SORT_FRAGMENTS
231         if (list->listTail != NULL && list->listCompare(new, list->listTail))
232                 prev = list->listTail;
233         else if (list->listLast != NULL && list->listCompare(new, list->listLast))
234                 prev = list->listLast;
235         else
236                 prev = NULL;
237
238         for (b = (prev ? prev->next : list->listHead);
239              b != NULL && list->listCompare(new, b);
240              prev = b, b = b->next) {
241                 list->listLoops++;
242         }
243         if (b != NULL)
244                 list->listLast = prev;
245
246         if (b != NULL) {
247                 new->next = b;
248                 if (prev != NULL)
249                         prev->next = new;
250                 else
251                         list->listHead = new;
252         } else
253 #endif
254         {
255                 new->next = (struct b_node *) NULL;
256                 if (list->listTail != NULL) {
257                         list->listTail->next = new;
258                         list->listTail = new;
259                 } else {
260                         list->listTail = list->listHead = new;
261                 }
262         }
263
264         return new;
265 }
266
267 #ifdef CFG_JFFS2_SORT_FRAGMENTS
268 static int compare_inodes(struct b_node *new, struct b_node *old)
269 {
270         struct jffs2_raw_inode *jNew = (struct jffs2_raw_inode *)new->offset;
271         struct jffs2_raw_inode *jOld = (struct jffs2_raw_inode *)old->offset;
272
273         return jNew->version < jOld->version;
274 }
275
276 static int compare_dirents(struct b_node *new, struct b_node *old)
277 {
278         struct jffs2_raw_dirent *jNew = (struct jffs2_raw_dirent *)new->offset;
279         struct jffs2_raw_dirent *jOld = (struct jffs2_raw_dirent *)old->offset;
280
281         return jNew->version > jOld->version;
282 }
283 #endif
284
285 static u32
286 jffs2_scan_empty(u32 start_offset, struct part_info *part)
287 {
288         char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
289         char *offset = part->offset + start_offset;
290
291         while (offset < max && *(u32 *)offset == 0xFFFFFFFF) {
292                 offset += sizeof(u32);
293                 /* return if spinning is due */
294                 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
295         }
296
297         return offset - part->offset;
298 }
299
300 static u32
301 jffs_init_1pass_list(struct part_info *part)
302 {
303         struct b_lists *pL;
304
305         if (part->jffs2_priv != NULL) {
306                 pL = (struct b_lists *)part->jffs2_priv;
307                 free_nodes(&pL->frag);
308                 free_nodes(&pL->dir);
309                 free(pL);
310         }
311         if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
312                 pL = (struct b_lists *)part->jffs2_priv;
313
314                 memset(pL, 0, sizeof(*pL));
315 #ifdef CFG_JFFS2_SORT_FRAGMENTS
316                 pL->dir.listCompare = compare_dirents;
317                 pL->frag.listCompare = compare_inodes;
318 #endif
319         }
320         return 0;
321 }
322
323 /* find the inode from the slashless name given a parent */
324 static long
325 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
326 {
327         struct b_node *b;
328         struct jffs2_raw_inode *jNode;
329         u32 totalSize = 0;
330         u16 latestVersion = 0;
331         char *lDest;
332         char *src;
333         long ret;
334         int i;
335         u32 counter = 0;
336
337         for (b = pL->frag.listHead; b != NULL; b = b->next) {
338                 jNode = (struct jffs2_raw_inode *) (b->offset);
339                 if ((inode == jNode->ino)) {
340 #if 0
341                         putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
342                         putLabeledWord("read_inode: inode = ", jNode->ino);
343                         putLabeledWord("read_inode: version = ", jNode->version);
344                         putLabeledWord("read_inode: isize = ", jNode->isize);
345                         putLabeledWord("read_inode: offset = ", jNode->offset);
346                         putLabeledWord("read_inode: csize = ", jNode->csize);
347                         putLabeledWord("read_inode: dsize = ", jNode->dsize);
348                         putLabeledWord("read_inode: compr = ", jNode->compr);
349                         putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
350                         putLabeledWord("read_inode: flags = ", jNode->flags);
351 #endif
352                         /* get actual file length from the newest node */
353                         if (jNode->version >= latestVersion) {
354                                 totalSize = jNode->isize;
355                                 latestVersion = jNode->version;
356                         }
357
358                         if(dest) {
359                                 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
360                                 /* ignore data behind latest known EOF */
361                                 if (jNode->offset > totalSize)
362                                         continue;
363
364                                 lDest = (char *) (dest + jNode->offset);
365 #if 0
366                                 putLabeledWord("read_inode: src = ", src);
367                                 putLabeledWord("read_inode: dest = ", lDest);
368 #endif
369                                 switch (jNode->compr) {
370                                 case JFFS2_COMPR_NONE:
371                                         ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
372                                         break;
373                                 case JFFS2_COMPR_ZERO:
374                                         ret = 0;
375                                         for (i = 0; i < jNode->dsize; i++)
376                                                 *(lDest++) = 0;
377                                         break;
378                                 case JFFS2_COMPR_RTIME:
379                                         ret = 0;
380                                         rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
381                                         break;
382                                 case JFFS2_COMPR_DYNRUBIN:
383                                         /* this is slow but it works */
384                                         ret = 0;
385                                         dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
386                                         break;
387                                 case JFFS2_COMPR_ZLIB:
388                                         ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
389                                         break;
390                                 default:
391                                         /* unknown */
392                                         putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
393                                         return -1;
394                                         break;
395                                 }
396                         }
397
398 #if 0
399                         putLabeledWord("read_inode: totalSize = ", totalSize);
400                         putLabeledWord("read_inode: compr ret = ", ret);
401 #endif
402                 }
403                 counter++;
404         }
405
406 #if 0
407         putLabeledWord("read_inode: returning = ", totalSize);
408 #endif
409         return totalSize;
410 }
411
412 /* find the inode from the slashless name given a parent */
413 static u32
414 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
415 {
416         struct b_node *b;
417         struct jffs2_raw_dirent *jDir;
418         int len;
419         u32 counter;
420         u32 version = 0;
421         u32 inode = 0;
422
423         /* name is assumed slash free */
424         len = strlen(name);
425
426         counter = 0;
427         /* we need to search all and return the inode with the highest version */
428         for(b = pL->dir.listHead; b; b = b->next, counter++) {
429                 jDir = (struct jffs2_raw_dirent *) (b->offset);
430                 if ((pino == jDir->pino) && (len == jDir->nsize) &&
431                     (jDir->ino) &&      /* 0 for unlink */
432                     (!strncmp(jDir->name, name, len))) {        /* a match */
433                         if (jDir->version < version) continue;
434
435                         if(jDir->version == 0) {
436                                 /* Is this legal? */
437                                 putstr(" ** WARNING ** ");
438                                 putnstr(jDir->name, jDir->nsize);
439                                 putstr(" is version 0 (in find, ignoring)\r\n");
440                         } else if(jDir->version == version) {
441                                 /* Im pretty sure this isn't ... */
442                                 putstr(" ** ERROR ** ");
443                                 putnstr(jDir->name, jDir->nsize);
444                                 putLabeledWord(" has dup version =", version);
445                         }
446                         inode = jDir->ino;
447                         version = jDir->version;
448                 }
449 #if 0
450                 putstr("\r\nfind_inode:p&l ->");
451                 putnstr(jDir->name, jDir->nsize);
452                 putstr("\r\n");
453                 putLabeledWord("pino = ", jDir->pino);
454                 putLabeledWord("nsize = ", jDir->nsize);
455                 putLabeledWord("b = ", (u32) b);
456                 putLabeledWord("counter = ", counter);
457 #endif
458         }
459         return inode;
460 }
461
462 static char *mkmodestr(unsigned long mode, char *str)
463 {
464         static const char *l = "xwr";
465         int mask = 1, i;
466         char c;
467
468         switch (mode & S_IFMT) {
469                 case S_IFDIR:    str[0] = 'd'; break;
470                 case S_IFBLK:    str[0] = 'b'; break;
471                 case S_IFCHR:    str[0] = 'c'; break;
472                 case S_IFIFO:    str[0] = 'f'; break;
473                 case S_IFLNK:    str[0] = 'l'; break;
474                 case S_IFSOCK:   str[0] = 's'; break;
475                 case S_IFREG:    str[0] = '-'; break;
476                 default:         str[0] = '?';
477         }
478
479         for(i = 0; i < 9; i++) {
480                 c = l[i%3];
481                 str[9-i] = (mode & mask)?c:'-';
482                 mask = mask<<1;
483         }
484
485         if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
486         if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
487         if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
488         str[10] = '\0';
489         return str;
490 }
491
492 static inline void dump_stat(struct stat *st, const char *name)
493 {
494         char str[20];
495         char s[64], *p;
496
497         if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
498                 st->st_mtime = 1;
499
500         ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
501
502         if ((p = strchr(s,'\n')) != NULL) *p = '\0';
503         if ((p = strchr(s,'\r')) != NULL) *p = '\0';
504
505 /*
506         printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
507                 st->st_size, s, name);
508 */
509
510         printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
511 }
512
513 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
514 {
515         char fname[256];
516         struct stat st;
517
518         if(!d || !i) return -1;
519
520         strncpy(fname, d->name, d->nsize);
521         fname[d->nsize] = '\0';
522
523         memset(&st,0,sizeof(st));
524
525         st.st_mtime = i->mtime;
526         st.st_mode = i->mode;
527         st.st_ino = i->ino;
528
529         /* neither dsize nor isize help us.. do it the long way */
530         st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
531
532         dump_stat(&st, fname);
533
534         if (d->type == DT_LNK) {
535                 unsigned char *src = (unsigned char *) (&i[1]);
536                 putstr(" -> ");
537                 putnstr(src, (int)i->dsize);
538         }
539
540         putstr("\r\n");
541
542         return 0;
543 }
544
545 /* list inodes with the given pino */
546 static u32
547 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
548 {
549         struct b_node *b;
550         struct jffs2_raw_dirent *jDir;
551
552         for (b = pL->dir.listHead; b; b = b->next) {
553                 jDir = (struct jffs2_raw_dirent *) (b->offset);
554                 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
555                         u32 i_version = 0;
556                         struct jffs2_raw_inode *jNode, *i = NULL;
557                         struct b_node *b2 = pL->frag.listHead;
558
559                         while (b2) {
560                                 jNode = (struct jffs2_raw_inode *) (b2->offset);
561                                 if (jNode->ino == jDir->ino
562                                     && jNode->version >= i_version)
563                                         i = jNode;
564                                 b2 = b2->next;
565                         }
566
567                         dump_inode(pL, jDir, i);
568                 }
569         }
570         return pino;
571 }
572
573 static u32
574 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
575 {
576         int i;
577         char tmp[256];
578         char working_tmp[256];
579         char *c;
580
581         /* discard any leading slash */
582         i = 0;
583         while (fname[i] == '/')
584                 i++;
585         strcpy(tmp, &fname[i]);
586
587         while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
588         {
589                 strncpy(working_tmp, tmp, c - tmp);
590                 working_tmp[c - tmp] = '\0';
591 #if 0
592                 putstr("search_inode: tmp = ");
593                 putstr(tmp);
594                 putstr("\r\n");
595                 putstr("search_inode: wtmp = ");
596                 putstr(working_tmp);
597                 putstr("\r\n");
598                 putstr("search_inode: c = ");
599                 putstr(c);
600                 putstr("\r\n");
601 #endif
602                 for (i = 0; i < strlen(c) - 1; i++)
603                         tmp[i] = c[i + 1];
604                 tmp[i] = '\0';
605 #if 0
606                 putstr("search_inode: post tmp = ");
607                 putstr(tmp);
608                 putstr("\r\n");
609 #endif
610
611                 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
612                         putstr("find_inode failed for name=");
613                         putstr(working_tmp);
614                         putstr("\r\n");
615                         return 0;
616                 }
617         }
618         /* this is for the bare filename, directories have already been mapped */
619         if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
620                 putstr("find_inode failed for name=");
621                 putstr(tmp);
622                 putstr("\r\n");
623                 return 0;
624         }
625         return pino;
626
627 }
628
629 static u32
630 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
631 {
632         struct b_node *b;
633         struct b_node *b2;
634         struct jffs2_raw_dirent *jDir;
635         struct jffs2_raw_inode *jNode;
636         struct jffs2_raw_dirent *jDirFound = NULL;
637         char tmp[256];
638         u32 version = 0;
639         u32 pino;
640         unsigned char *src;
641
642         /* we need to search all and return the inode with the highest version */
643         for(b = pL->dir.listHead; b; b = b->next) {
644                 jDir = (struct jffs2_raw_dirent *) (b->offset);
645                 if (ino == jDir->ino) {
646                         if(jDir->version < version) continue;
647
648                         if(jDir->version == 0) {
649                                 /* Is this legal? */
650                                 putstr(" ** WARNING ** ");
651                                 putnstr(jDir->name, jDir->nsize);
652                                 putstr(" is version 0 (in resolve, ignoring)\r\n");
653                         } else if(jDir->version == version) {
654                                 /* Im pretty sure this isn't ... */
655                                 putstr(" ** ERROR ** ");
656                                 putnstr(jDir->name, jDir->nsize);
657                                 putLabeledWord(" has dup version (resolve) = ",
658                                         version);
659                         }
660
661                         jDirFound = jDir;
662                         version = jDir->version;
663                 }
664         }
665         /* now we found the right entry again. (shoulda returned inode*) */
666         if (jDirFound->type != DT_LNK)
667                 return jDirFound->ino;
668
669         /* it's a soft link so we follow it again. */
670         b2 = pL->frag.listHead;
671         while (b2) {
672                 jNode = (struct jffs2_raw_inode *) (b2->offset);
673                 if (jNode->ino == jDirFound->ino) {
674                         src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode));
675
676 #if 0
677                         putLabeledWord("\t\t dsize = ", jNode->dsize);
678                         putstr("\t\t target = ");
679                         putnstr(src, jNode->dsize);
680                         putstr("\r\n");
681 #endif
682                         strncpy(tmp, src, jNode->dsize);
683                         tmp[jNode->dsize] = '\0';
684                         break;
685                 }
686                 b2 = b2->next;
687         }
688         /* ok so the name of the new file to find is in tmp */
689         /* if it starts with a slash it is root based else shared dirs */
690         if (tmp[0] == '/')
691                 pino = 1;
692         else
693                 pino = jDirFound->pino;
694
695         return jffs2_1pass_search_inode(pL, tmp, pino);
696 }
697
698 static u32
699 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
700 {
701         int i;
702         char tmp[256];
703         char working_tmp[256];
704         char *c;
705
706         /* discard any leading slash */
707         i = 0;
708         while (fname[i] == '/')
709                 i++;
710         strcpy(tmp, &fname[i]);
711         working_tmp[0] = '\0';
712         while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
713         {
714                 strncpy(working_tmp, tmp, c - tmp);
715                 working_tmp[c - tmp] = '\0';
716                 for (i = 0; i < strlen(c) - 1; i++)
717                         tmp[i] = c[i + 1];
718                 tmp[i] = '\0';
719                 /* only a failure if we arent looking at top level */
720                 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
721                     (working_tmp[0])) {
722                         putstr("find_inode failed for name=");
723                         putstr(working_tmp);
724                         putstr("\r\n");
725                         return 0;
726                 }
727         }
728
729         if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
730                 putstr("find_inode failed for name=");
731                 putstr(tmp);
732                 putstr("\r\n");
733                 return 0;
734         }
735         /* this is for the bare filename, directories have already been mapped */
736         if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
737                 putstr("find_inode failed for name=");
738                 putstr(tmp);
739                 putstr("\r\n");
740                 return 0;
741         }
742         return pino;
743
744 }
745
746 unsigned char
747 jffs2_1pass_rescan_needed(struct part_info *part)
748 {
749         struct b_node *b;
750         struct jffs2_unknown_node *node;
751         struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
752
753         if (part->jffs2_priv == 0){
754                 DEBUGF ("rescan: First time in use\n");
755                 return 1;
756         }
757         /* if we have no list, we need to rescan */
758         if (pL->frag.listCount == 0) {
759                 DEBUGF ("rescan: fraglist zero\n");
760                 return 1;
761         }
762
763         /* or if we are scanning a new partition */
764         if (pL->partOffset != part->offset) {
765                 DEBUGF ("rescan: different partition\n");
766                 return 1;
767         }
768         /* but suppose someone reflashed a partition at the same offset... */
769         b = pL->dir.listHead;
770         while (b) {
771                 node = (struct jffs2_unknown_node *) (b->offset);
772                 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
773                         DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
774                                         (unsigned long) b->offset);
775                         return 1;
776                 }
777                 b = b->next;
778         }
779         return 0;
780 }
781
782 #ifdef DEBUG_FRAGMENTS
783 static void
784 dump_fragments(struct b_lists *pL)
785 {
786         struct b_node *b;
787         struct jffs2_raw_inode *jNode;
788
789         putstr("\r\n\r\n******The fragment Entries******\r\n");
790         b = pL->frag.listHead;
791         while (b) {
792                 jNode = (struct jffs2_raw_inode *) (b->offset);
793                 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
794                 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
795                 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
796                 putLabeledWord("\tbuild_list: version = ", jNode->version);
797                 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
798                 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
799                 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
800                 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
801                 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
802                 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
803                 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
804                 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
805                 putLabeledWord("\tbuild_list: offset = ", b->offset);   // FIXME: ? [RS]
806                 b = b->next;
807         }
808 }
809 #endif
810
811 #ifdef DEBUG_DIRENTS
812 static void
813 dump_dirents(struct b_lists *pL)
814 {
815         struct b_node *b;
816         struct jffs2_raw_dirent *jDir;
817
818         putstr("\r\n\r\n******The directory Entries******\r\n");
819         b = pL->dir.listHead;
820         while (b) {
821                 jDir = (struct jffs2_raw_dirent *) (b->offset);
822                 putstr("\r\n");
823                 putnstr(jDir->name, jDir->nsize);
824                 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
825                 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
826                 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
827                 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
828                 putLabeledWord("\tbuild_list: version = ", jDir->version);
829                 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
830                 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
831                 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
832                 putLabeledWord("\tbuild_list: type = ", jDir->type);
833                 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
834                 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
835                 putLabeledWord("\tbuild_list: offset = ", b->offset);   // FIXME: ? [RS]
836                 b = b->next;
837         }
838 }
839 #endif
840
841 static u32
842 jffs2_1pass_build_lists(struct part_info * part)
843 {
844         struct b_lists *pL;
845         struct jffs2_unknown_node *node;
846         u32 offset, oldoffset = 0;
847         u32 max = part->size - sizeof(struct jffs2_raw_inode);
848         u32 counter = 0;
849         u32 counter4 = 0;
850         u32 counterF = 0;
851         u32 counterN = 0;
852
853         /* turn off the lcd.  Refreshing the lcd adds 50% overhead to the */
854         /* jffs2 list building enterprise nope.  in newer versions the overhead is */
855         /* only about 5 %.  not enough to inconvenience people for. */
856         /* lcd_off(); */
857
858         /* if we are building a list we need to refresh the cache. */
859         jffs_init_1pass_list(part);
860         pL = (struct b_lists *)part->jffs2_priv;
861         pL->partOffset = part->offset;
862         offset = 0;
863         printf("Scanning JFFS2 FS:   ");
864
865         /* start at the beginning of the partition */
866         while (offset < max) {
867                 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
868                         printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
869                         oldoffset = offset;
870                 }
871
872                 node = (struct jffs2_unknown_node *) (part->offset + offset);
873                 if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
874                         /* if its a fragment add it */
875                         if (node->nodetype == JFFS2_NODETYPE_INODE &&
876                                     inode_crc((struct jffs2_raw_inode *) node)) {
877                                 if (insert_node(&pL->frag, (u32) part->offset +
878                                                                 offset) == NULL)
879                                         return 0;
880                         } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
881                                    dirent_crc((struct jffs2_raw_dirent *) node)  &&
882                                    dirent_name_crc((struct jffs2_raw_dirent *) node)) {
883                                 if (! (counterN%100))
884                                         printf("\b\b.  ");
885                                 if (insert_node(&pL->dir, (u32) part->offset +
886                                                                 offset) == NULL)
887                                         return 0;
888                                 counterN++;
889                         } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
890                                 if (node->totlen != sizeof(struct jffs2_unknown_node))
891                                         printf("OOPS Cleanmarker has bad size "
892                                                 "%d != %d\n", node->totlen,
893                                                 sizeof(struct jffs2_unknown_node));
894                         } else {
895                                 printf("Unknown node type: %x len %d "
896                                         "offset 0x%x\n", node->nodetype,
897                                         node->totlen, offset);
898                         }
899                         offset += ((node->totlen + 3) & ~3);
900                         counterF++;
901                 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
902                            node->nodetype == JFFS2_EMPTY_BITMASK) {
903                         offset = jffs2_scan_empty(offset, part);
904                 } else {        /* if we know nothing, we just step and look. */
905                         offset += 4;
906                         counter4++;
907                 }
908 /*             printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
909
910         }
911
912         putstr("\b\b done.\r\n");               /* close off the dots */
913         /* turn the lcd back on. */
914         /* splash(); */
915
916 #if 0
917         putLabeledWord("dir entries = ", pL->dir.listCount);
918         putLabeledWord("frag entries = ", pL->frag.listCount);
919         putLabeledWord("+4 increments = ", counter4);
920         putLabeledWord("+file_offset increments = ", counterF);
921
922 #endif
923
924 #ifdef DEBUG_DIRENTS
925         dump_dirents(pL);
926 #endif
927
928 #ifdef DEBUG_FRAGMENTS
929         dump_fragments(pL);
930 #endif
931
932         /* give visual feedback that we are done scanning the flash */
933         led_blink(0x0, 0x0, 0x1, 0x1);  /* off, forever, on 100ms, off 100ms */
934         return 1;
935 }
936
937
938
939
940
941 static u32
942 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
943 {
944         struct b_node *b;
945         struct jffs2_raw_inode *jNode;
946         int i;
947
948         for (i = 0; i < JFFS2_NUM_COMPR; i++) {
949                 piL->compr_info[i].num_frags = 0;
950                 piL->compr_info[i].compr_sum = 0;
951                 piL->compr_info[i].decompr_sum = 0;
952         }
953
954         b = pL->frag.listHead;
955         while (b) {
956                 jNode = (struct jffs2_raw_inode *) (b->offset);
957                 if (jNode->compr < JFFS2_NUM_COMPR) {
958                         piL->compr_info[jNode->compr].num_frags++;
959                         piL->compr_info[jNode->compr].compr_sum += jNode->csize;
960                         piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
961                 }
962                 b = b->next;
963         }
964         return 0;
965 }
966
967
968
969 static struct b_lists *
970 jffs2_get_list(struct part_info * part, const char *who)
971 {
972         if (jffs2_1pass_rescan_needed(part)) {
973                 if (!jffs2_1pass_build_lists(part)) {
974                         printf("%s: Failed to scan JFFSv2 file structure\n", who);
975                         return NULL;
976                 }
977         }
978         return (struct b_lists *)part->jffs2_priv;
979 }
980
981
982 /* Print directory / file contents */
983 u32
984 jffs2_1pass_ls(struct part_info * part, const char *fname)
985 {
986         struct b_lists *pl;
987         long ret = 0;
988         u32 inode;
989
990         if (! (pl = jffs2_get_list(part, "ls")))
991                 return 0;
992
993         if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
994                 putstr("ls: Failed to scan jffs2 file structure\r\n");
995                 return 0;
996         }
997
998
999 #if 0
1000         putLabeledWord("found file at inode = ", inode);
1001         putLabeledWord("read_inode returns = ", ret);
1002 #endif
1003
1004         return ret;
1005 }
1006
1007
1008
1009
1010
1011 /* Load a file from flash into memory. fname can be a full path */
1012 u32
1013 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1014 {
1015
1016         struct b_lists *pl;
1017         long ret = 0;
1018         u32 inode;
1019
1020         if (! (pl  = jffs2_get_list(part, "load")))
1021                 return 0;
1022
1023         if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1024                 putstr("load: Failed to find inode\r\n");
1025                 return 0;
1026         }
1027
1028         /* Resolve symlinks */
1029         if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1030                 putstr("load: Failed to resolve inode structure\r\n");
1031                 return 0;
1032         }
1033
1034         if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1035                 putstr("load: Failed to read inode\r\n");
1036                 return 0;
1037         }
1038
1039         DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1040                                 (unsigned long) dest, ret);
1041         return ret;
1042 }
1043
1044 /* Return information about the fs on this partition */
1045 u32
1046 jffs2_1pass_info(struct part_info * part)
1047 {
1048         struct b_jffs2_info info;
1049         struct b_lists *pl;
1050         int i;
1051
1052         if (! (pl  = jffs2_get_list(part, "info")))
1053                 return 0;
1054
1055         jffs2_1pass_fill_info(pl, &info);
1056         for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1057                 printf("Compression: %s\n", compr_names[i]);
1058                 printf("\tfrag count: %d\n", info.compr_info[i].num_frags);
1059                 printf("\tcompressed sum: %d\n", info.compr_info[i].compr_sum);
1060                 printf("\tuncompressed sum: %d\n", info.compr_info[i].decompr_sum);
1061         }
1062         return 1;
1063 }
1064
1065 #endif /* CFG_CMD_JFFS2 */