]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - fs/jffs2/jffs2_nand_1pass.c
Merge branch 'next' of git://git.denx.de/u-boot-coldfire
[karo-tx-uboot.git] / fs / jffs2 / jffs2_nand_1pass.c
1 #include <common.h>
2
3 #include <malloc.h>
4 #include <linux/stat.h>
5 #include <linux/time.h>
6
7 #include <jffs2/jffs2.h>
8 #include <jffs2/jffs2_1pass.h>
9 #include <nand.h>
10
11 #include "jffs2_nand_private.h"
12
13 #define NODE_CHUNK      1024    /* size of memory allocation chunk in b_nodes */
14
15 /* Debugging switches */
16 #undef  DEBUG_DIRENTS           /* print directory entry list after scan */
17 #undef  DEBUG_FRAGMENTS         /* print fragment list after scan */
18 #undef  DEBUG                   /* enable debugging messages */
19
20 #ifdef  DEBUG
21 # define DEBUGF(fmt,args...)    printf(fmt ,##args)
22 #else
23 # define DEBUGF(fmt,args...)
24 #endif
25
26 static nand_info_t *nand;
27
28 /* Compression names */
29 static char *compr_names[] = {
30         "NONE",
31         "ZERO",
32         "RTIME",
33         "RUBINMIPS",
34         "COPY",
35         "DYNRUBIN",
36         "ZLIB",
37 #if defined(CONFIG_JFFS2_LZO_LZARI)
38         "LZO",
39         "LZARI",
40 #endif
41 };
42
43 /* Spinning wheel */
44 static char spinner[] = { '|', '/', '-', '\\' };
45
46 /* Memory management */
47 struct mem_block {
48         unsigned index;
49         struct mem_block *next;
50         char nodes[0];
51 };
52
53 static void
54 free_nodes(struct b_list *list)
55 {
56         while (list->listMemBase != NULL) {
57                 struct mem_block *next = list->listMemBase->next;
58                 free(list->listMemBase);
59                 list->listMemBase = next;
60         }
61 }
62
63 static struct b_node *
64 add_node(struct b_list *list, int size)
65 {
66         u32 index = 0;
67         struct mem_block *memBase;
68         struct b_node *b;
69
70         memBase = list->listMemBase;
71         if (memBase != NULL)
72                 index = memBase->index;
73
74         if (memBase == NULL || index >= NODE_CHUNK) {
75                 /* we need more space before we continue */
76                 memBase = mmalloc(sizeof(struct mem_block) + NODE_CHUNK * size);
77                 if (memBase == NULL) {
78                         putstr("add_node: malloc failed\n");
79                         return NULL;
80                 }
81                 memBase->next = list->listMemBase;
82                 index = 0;
83         }
84         /* now we have room to add it. */
85         b = (struct b_node *)&memBase->nodes[size * index];
86         index ++;
87
88         memBase->index = index;
89         list->listMemBase = memBase;
90         list->listCount++;
91         return b;
92 }
93
94 static struct b_node *
95 insert_node(struct b_list *list, struct b_node *new)
96 {
97 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
98         struct b_node *b, *prev;
99
100         if (list->listTail != NULL && list->listCompare(new, list->listTail))
101                 prev = list->listTail;
102         else if (list->listLast != NULL && list->listCompare(new, list->listLast))
103                 prev = list->listLast;
104         else
105                 prev = NULL;
106
107         for (b = (prev ? prev->next : list->listHead);
108              b != NULL && list->listCompare(new, b);
109              prev = b, b = b->next) {
110                 list->listLoops++;
111         }
112         if (b != NULL)
113                 list->listLast = prev;
114
115         if (b != NULL) {
116                 new->next = b;
117                 if (prev != NULL)
118                         prev->next = new;
119                 else
120                         list->listHead = new;
121         } else
122 #endif
123         {
124                 new->next = (struct b_node *) NULL;
125                 if (list->listTail != NULL) {
126                         list->listTail->next = new;
127                         list->listTail = new;
128                 } else {
129                         list->listTail = list->listHead = new;
130                 }
131         }
132
133         return new;
134 }
135
136 static struct b_node *
137 insert_inode(struct b_list *list, struct jffs2_raw_inode *node, u32 offset)
138 {
139         struct b_inode *new;
140
141         if (!(new = (struct b_inode *)add_node(list, sizeof(struct b_inode)))) {
142                 putstr("add_node failed!\r\n");
143                 return NULL;
144         }
145         new->offset = offset;
146         new->version = node->version;
147         new->ino = node->ino;
148         new->isize = node->isize;
149         new->csize = node->csize;
150
151         return insert_node(list, (struct b_node *)new);
152 }
153
154 static struct b_node *
155 insert_dirent(struct b_list *list, struct jffs2_raw_dirent *node, u32 offset)
156 {
157         struct b_dirent *new;
158
159         if (!(new = (struct b_dirent *)add_node(list, sizeof(struct b_dirent)))) {
160                 putstr("add_node failed!\r\n");
161                 return NULL;
162         }
163         new->offset = offset;
164         new->version = node->version;
165         new->pino = node->pino;
166         new->ino = node->ino;
167         new->nhash = full_name_hash(node->name, node->nsize);
168         new->nsize = node->nsize;
169         new->type = node->type;
170
171         return insert_node(list, (struct b_node *)new);
172 }
173
174 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
175 /* Sort data entries with the latest version last, so that if there
176  * is overlapping data the latest version will be used.
177  */
178 static int compare_inodes(struct b_node *new, struct b_node *old)
179 {
180         struct jffs2_raw_inode ojNew;
181         struct jffs2_raw_inode ojOld;
182         struct jffs2_raw_inode *jNew =
183                 (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
184         struct jffs2_raw_inode *jOld =
185                 (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
186
187         return jNew->version > jOld->version;
188 }
189
190 /* Sort directory entries so all entries in the same directory
191  * with the same name are grouped together, with the latest version
192  * last. This makes it easy to eliminate all but the latest version
193  * by marking the previous version dead by setting the inode to 0.
194  */
195 static int compare_dirents(struct b_node *new, struct b_node *old)
196 {
197         struct jffs2_raw_dirent ojNew;
198         struct jffs2_raw_dirent ojOld;
199         struct jffs2_raw_dirent *jNew =
200                 (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
201         struct jffs2_raw_dirent *jOld =
202                 (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
203         int cmp;
204
205         /* ascending sort by pino */
206         if (jNew->pino != jOld->pino)
207                 return jNew->pino > jOld->pino;
208
209         /* pino is the same, so use ascending sort by nsize, so
210          * we don't do strncmp unless we really must.
211          */
212         if (jNew->nsize != jOld->nsize)
213                 return jNew->nsize > jOld->nsize;
214
215         /* length is also the same, so use ascending sort by name
216          */
217         cmp = strncmp(jNew->name, jOld->name, jNew->nsize);
218         if (cmp != 0)
219                 return cmp > 0;
220
221         /* we have duplicate names in this directory, so use ascending
222          * sort by version
223          */
224         if (jNew->version > jOld->version) {
225                 /* since jNew is newer, we know jOld is not valid, so
226                  * mark it with inode 0 and it will not be used
227                  */
228                 jOld->ino = 0;
229                 return 1;
230         }
231
232         return 0;
233 }
234 #endif
235
236 static u32
237 jffs_init_1pass_list(struct part_info *part)
238 {
239         struct b_lists *pL;
240
241         if (part->jffs2_priv != NULL) {
242                 pL = (struct b_lists *)part->jffs2_priv;
243                 free_nodes(&pL->frag);
244                 free_nodes(&pL->dir);
245                 free(pL);
246         }
247         if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
248                 pL = (struct b_lists *)part->jffs2_priv;
249
250                 memset(pL, 0, sizeof(*pL));
251 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
252                 pL->dir.listCompare = compare_dirents;
253                 pL->frag.listCompare = compare_inodes;
254 #endif
255         }
256         return 0;
257 }
258
259 /* find the inode from the slashless name given a parent */
260 static long
261 jffs2_1pass_read_inode(struct b_lists *pL, u32 ino, char *dest,
262                        struct stat *stat)
263 {
264         struct b_inode *jNode;
265         u32 totalSize = 0;
266         u32 latestVersion = 0;
267         long ret;
268
269 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
270         /* Find file size before loading any data, so fragments that
271          * start past the end of file can be ignored. A fragment
272          * that is partially in the file is loaded, so extra data may
273          * be loaded up to the next 4K boundary above the file size.
274          * This shouldn't cause trouble when loading kernel images, so
275          * we will live with it.
276          */
277         for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
278                 if ((ino == jNode->ino)) {
279                         /* get actual file length from the newest node */
280                         if (jNode->version >= latestVersion) {
281                                 totalSize = jNode->isize;
282                                 latestVersion = jNode->version;
283                         }
284                 }
285         }
286 #endif
287
288         for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
289                 if ((ino != jNode->ino))
290                         continue;
291 #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
292                 /* get actual file length from the newest node */
293                 if (jNode->version >= latestVersion) {
294                         totalSize = jNode->isize;
295                         latestVersion = jNode->version;
296                 }
297 #endif
298                 if (dest || stat) {
299                         char *src, *dst;
300                         char data[4096 + sizeof(struct jffs2_raw_inode)];
301                         struct jffs2_raw_inode *inode;
302                         size_t len;
303
304                         inode = (struct jffs2_raw_inode *)&data;
305                         len = sizeof(struct jffs2_raw_inode);
306                         if (dest)
307                                 len += jNode->csize;
308                         nand_read(nand, jNode->offset, &len, inode);
309                         /* ignore data behind latest known EOF */
310                         if (inode->offset > totalSize)
311                                 continue;
312
313                         if (stat) {
314                                 stat->st_mtime = inode->mtime;
315                                 stat->st_mode = inode->mode;
316                                 stat->st_ino = inode->ino;
317                                 stat->st_size = totalSize;
318                         }
319
320                         if (!dest)
321                                 continue;
322
323                         src = ((char *) inode) + sizeof(struct jffs2_raw_inode);
324                         dst = (char *) (dest + inode->offset);
325
326                         switch (inode->compr) {
327                         case JFFS2_COMPR_NONE:
328                                 ret = 0;
329                                 memcpy(dst, src, inode->dsize);
330                                 break;
331                         case JFFS2_COMPR_ZERO:
332                                 ret = 0;
333                                 memset(dst, 0, inode->dsize);
334                                 break;
335                         case JFFS2_COMPR_RTIME:
336                                 ret = 0;
337                                 rtime_decompress(src, dst, inode->csize, inode->dsize);
338                                 break;
339                         case JFFS2_COMPR_DYNRUBIN:
340                                 /* this is slow but it works */
341                                 ret = 0;
342                                 dynrubin_decompress(src, dst, inode->csize, inode->dsize);
343                                 break;
344                         case JFFS2_COMPR_ZLIB:
345                                 ret = zlib_decompress(src, dst, inode->csize, inode->dsize);
346                                 break;
347 #if defined(CONFIG_JFFS2_LZO_LZARI)
348                         case JFFS2_COMPR_LZO:
349                                 ret = lzo_decompress(src, dst, inode->csize, inode->dsize);
350                                 break;
351                         case JFFS2_COMPR_LZARI:
352                                 ret = lzari_decompress(src, dst, inode->csize, inode->dsize);
353                                 break;
354 #endif
355                         default:
356                                 /* unknown */
357                                 putLabeledWord("UNKOWN COMPRESSION METHOD = ", inode->compr);
358                                 return -1;
359                         }
360                 }
361         }
362
363         return totalSize;
364 }
365
366 /* find the inode from the slashless name given a parent */
367 static u32
368 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
369 {
370         struct b_dirent *jDir;
371         int len = strlen(name); /* name is assumed slash free */
372         unsigned int nhash = full_name_hash(name, len);
373         u32 version = 0;
374         u32 inode = 0;
375
376         /* we need to search all and return the inode with the highest version */
377         for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
378                 if ((pino == jDir->pino) && (jDir->ino) &&      /* 0 for unlink */
379                     (len == jDir->nsize) && (nhash == jDir->nhash)) {
380                         /* TODO: compare name */
381                         if (jDir->version < version)
382                                 continue;
383
384                         if (jDir->version == version && inode != 0) {
385                                 /* I'm pretty sure this isn't legal */
386                                 putstr(" ** ERROR ** ");
387 /*                              putnstr(jDir->name, jDir->nsize); */
388 /*                              putLabeledWord(" has dup version =", version); */
389                         }
390                         inode = jDir->ino;
391                         version = jDir->version;
392                 }
393         }
394         return inode;
395 }
396
397 char *mkmodestr(unsigned long mode, char *str)
398 {
399         static const char *l = "xwr";
400         int mask = 1, i;
401         char c;
402
403         switch (mode & S_IFMT) {
404                 case S_IFDIR:    str[0] = 'd'; break;
405                 case S_IFBLK:    str[0] = 'b'; break;
406                 case S_IFCHR:    str[0] = 'c'; break;
407                 case S_IFIFO:    str[0] = 'f'; break;
408                 case S_IFLNK:    str[0] = 'l'; break;
409                 case S_IFSOCK:   str[0] = 's'; break;
410                 case S_IFREG:    str[0] = '-'; break;
411                 default:         str[0] = '?';
412         }
413
414         for(i = 0; i < 9; i++) {
415                 c = l[i%3];
416                 str[9-i] = (mode & mask)?c:'-';
417                 mask = mask<<1;
418         }
419
420         if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
421         if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
422         if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
423         str[10] = '\0';
424         return str;
425 }
426
427 static inline void dump_stat(struct stat *st, const char *name)
428 {
429         char str[20];
430         char s[64], *p;
431
432         if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
433                 st->st_mtime = 1;
434
435         ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
436
437         if ((p = strchr(s,'\n')) != NULL) *p = '\0';
438         if ((p = strchr(s,'\r')) != NULL) *p = '\0';
439
440 /*
441         printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
442                 st->st_size, s, name);
443 */
444
445         printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
446 }
447
448 static inline int
449 dump_inode(struct b_lists *pL, struct b_dirent *d, struct b_inode *i)
450 {
451         char fname[JFFS2_MAX_NAME_LEN + 1];
452         struct stat st;
453         size_t len;
454
455         if(!d || !i) return -1;
456         len = d->nsize;
457         nand_read(nand, d->offset + sizeof(struct jffs2_raw_dirent),
458                   &len, &fname);
459         fname[d->nsize] = '\0';
460
461         memset(&st, 0, sizeof(st));
462
463         jffs2_1pass_read_inode(pL, i->ino, NULL, &st);
464
465         dump_stat(&st, fname);
466 /* FIXME
467         if (d->type == DT_LNK) {
468                 unsigned char *src = (unsigned char *) (&i[1]);
469                 putstr(" -> ");
470                 putnstr(src, (int)i->dsize);
471         }
472 */
473         putstr("\r\n");
474
475         return 0;
476 }
477
478 /* list inodes with the given pino */
479 static u32
480 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
481 {
482         struct b_dirent *jDir;
483         u32 i_version = 0;
484
485         for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
486                 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
487                         struct b_inode *jNode = (struct b_inode *)pL->frag.listHead;
488                         struct b_inode *i = NULL;
489
490                         while (jNode) {
491                                 if (jNode->ino == jDir->ino && jNode->version >= i_version) {
492                                         i_version = jNode->version;
493                                         i = jNode;
494                                 }
495                                 jNode = jNode->next;
496                         }
497                         dump_inode(pL, jDir, i);
498                 }
499         }
500         return pino;
501 }
502
503 static u32
504 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
505 {
506         int i;
507         char tmp[256];
508         char working_tmp[256];
509         char *c;
510
511         /* discard any leading slash */
512         i = 0;
513         while (fname[i] == '/')
514                 i++;
515         strcpy(tmp, &fname[i]);
516
517         while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
518         {
519                 strncpy(working_tmp, tmp, c - tmp);
520                 working_tmp[c - tmp] = '\0';
521 #if 0
522                 putstr("search_inode: tmp = ");
523                 putstr(tmp);
524                 putstr("\r\n");
525                 putstr("search_inode: wtmp = ");
526                 putstr(working_tmp);
527                 putstr("\r\n");
528                 putstr("search_inode: c = ");
529                 putstr(c);
530                 putstr("\r\n");
531 #endif
532                 for (i = 0; i < strlen(c) - 1; i++)
533                         tmp[i] = c[i + 1];
534                 tmp[i] = '\0';
535 #if 0
536                 putstr("search_inode: post tmp = ");
537                 putstr(tmp);
538                 putstr("\r\n");
539 #endif
540
541                 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
542                         putstr("find_inode failed for name=");
543                         putstr(working_tmp);
544                         putstr("\r\n");
545                         return 0;
546                 }
547         }
548         /* this is for the bare filename, directories have already been mapped */
549         if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
550                 putstr("find_inode failed for name=");
551                 putstr(tmp);
552                 putstr("\r\n");
553                 return 0;
554         }
555         return pino;
556
557 }
558
559 static u32
560 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
561 {
562         struct b_dirent *jDir;
563         struct b_inode *jNode;
564         u8 jDirFoundType = 0;
565         u32 jDirFoundIno = 0;
566         u32 jDirFoundPino = 0;
567         char tmp[JFFS2_MAX_NAME_LEN + 1];
568         u32 version = 0;
569         u32 pino;
570
571         /* we need to search all and return the inode with the highest version */
572         for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
573                 if (ino == jDir->ino) {
574                         if (jDir->version < version)
575                                 continue;
576
577                         if (jDir->version == version && jDirFoundType) {
578                                 /* I'm pretty sure this isn't legal */
579                                 putstr(" ** ERROR ** ");
580 /*                              putnstr(jDir->name, jDir->nsize); */
581 /*                              putLabeledWord(" has dup version (resolve) = ", */
582 /*                                      version); */
583                         }
584
585                         jDirFoundType = jDir->type;
586                         jDirFoundIno = jDir->ino;
587                         jDirFoundPino = jDir->pino;
588                         version = jDir->version;
589                 }
590         }
591         /* now we found the right entry again. (shoulda returned inode*) */
592         if (jDirFoundType != DT_LNK)
593                 return jDirFoundIno;
594
595         /* it's a soft link so we follow it again. */
596         for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
597                 if (jNode->ino == jDirFoundIno) {
598                         size_t len = jNode->csize;
599                         nand_read(nand, jNode->offset + sizeof(struct jffs2_raw_inode), &len, &tmp);
600                         tmp[jNode->csize] = '\0';
601                         break;
602                 }
603         }
604         /* ok so the name of the new file to find is in tmp */
605         /* if it starts with a slash it is root based else shared dirs */
606         if (tmp[0] == '/')
607                 pino = 1;
608         else
609                 pino = jDirFoundPino;
610
611         return jffs2_1pass_search_inode(pL, tmp, pino);
612 }
613
614 static u32
615 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
616 {
617         int i;
618         char tmp[256];
619         char working_tmp[256];
620         char *c;
621
622         /* discard any leading slash */
623         i = 0;
624         while (fname[i] == '/')
625                 i++;
626         strcpy(tmp, &fname[i]);
627         working_tmp[0] = '\0';
628         while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
629         {
630                 strncpy(working_tmp, tmp, c - tmp);
631                 working_tmp[c - tmp] = '\0';
632                 for (i = 0; i < strlen(c) - 1; i++)
633                         tmp[i] = c[i + 1];
634                 tmp[i] = '\0';
635                 /* only a failure if we arent looking at top level */
636                 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
637                     (working_tmp[0])) {
638                         putstr("find_inode failed for name=");
639                         putstr(working_tmp);
640                         putstr("\r\n");
641                         return 0;
642                 }
643         }
644
645         if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
646                 putstr("find_inode failed for name=");
647                 putstr(tmp);
648                 putstr("\r\n");
649                 return 0;
650         }
651         /* this is for the bare filename, directories have already been mapped */
652         if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
653                 putstr("find_inode failed for name=");
654                 putstr(tmp);
655                 putstr("\r\n");
656                 return 0;
657         }
658         return pino;
659
660 }
661
662 unsigned char
663 jffs2_1pass_rescan_needed(struct part_info *part)
664 {
665         struct b_node *b;
666         struct jffs2_unknown_node onode;
667         struct jffs2_unknown_node *node;
668         struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
669
670         if (part->jffs2_priv == 0){
671                 DEBUGF ("rescan: First time in use\n");
672                 return 1;
673         }
674         /* if we have no list, we need to rescan */
675         if (pL->frag.listCount == 0) {
676                 DEBUGF ("rescan: fraglist zero\n");
677                 return 1;
678         }
679
680         /* or if we are scanning a new partition */
681         if (pL->partOffset != part->offset) {
682                 DEBUGF ("rescan: different partition\n");
683                 return 1;
684         }
685
686         /* FIXME */
687 #if 0
688         /* but suppose someone reflashed a partition at the same offset... */
689         b = pL->dir.listHead;
690         while (b) {
691                 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
692                         sizeof(onode), &onode);
693                 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
694                         DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
695                                         (unsigned long) b->offset);
696                         return 1;
697                 }
698                 b = b->next;
699         }
700 #endif
701         return 0;
702 }
703
704 #ifdef DEBUG_FRAGMENTS
705 static void
706 dump_fragments(struct b_lists *pL)
707 {
708         struct b_node *b;
709         struct jffs2_raw_inode ojNode;
710         struct jffs2_raw_inode *jNode;
711
712         putstr("\r\n\r\n******The fragment Entries******\r\n");
713         b = pL->frag.listHead;
714         while (b) {
715                 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
716                         sizeof(ojNode), &ojNode);
717                 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
718                 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
719                 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
720                 putLabeledWord("\tbuild_list: version = ", jNode->version);
721                 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
722                 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
723                 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
724                 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
725                 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
726                 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
727                 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
728                 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
729                 putLabeledWord("\tbuild_list: offset = ", b->offset);   /* FIXME: ? [RS] */
730                 b = b->next;
731         }
732 }
733 #endif
734
735 #ifdef DEBUG_DIRENTS
736 static void
737 dump_dirents(struct b_lists *pL)
738 {
739         struct b_node *b;
740         struct jffs2_raw_dirent *jDir;
741
742         putstr("\r\n\r\n******The directory Entries******\r\n");
743         b = pL->dir.listHead;
744         while (b) {
745                 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
746                 putstr("\r\n");
747                 putnstr(jDir->name, jDir->nsize);
748                 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
749                 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
750                 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
751                 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
752                 putLabeledWord("\tbuild_list: version = ", jDir->version);
753                 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
754                 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
755                 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
756                 putLabeledWord("\tbuild_list: type = ", jDir->type);
757                 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
758                 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
759                 putLabeledWord("\tbuild_list: offset = ", b->offset);   /* FIXME: ? [RS] */
760                 b = b->next;
761                 put_fl_mem(jDir);
762         }
763 }
764 #endif
765
766 static int
767 jffs2_fill_scan_buf(nand_info_t *nand, unsigned char *buf,
768                     unsigned ofs, unsigned len)
769 {
770         int ret;
771         unsigned olen;
772
773         olen = len;
774         ret = nand_read(nand, ofs, &olen, buf);
775         if (ret) {
776                 printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret);
777                 return ret;
778         }
779         if (olen < len) {
780                 printf("Read at 0x%x gave only 0x%x bytes\n", ofs, olen);
781                 return -1;
782         }
783         return 0;
784 }
785
786 #define EMPTY_SCAN_SIZE 1024
787 static u32
788 jffs2_1pass_build_lists(struct part_info * part)
789 {
790         struct b_lists *pL;
791         struct jffs2_unknown_node *node;
792         unsigned nr_blocks, sectorsize, ofs, offset;
793         char *buf;
794         int i;
795         u32 counter = 0;
796         u32 counter4 = 0;
797         u32 counterF = 0;
798         u32 counterN = 0;
799
800         struct mtdids *id = part->dev->id;
801         nand = nand_info + id->num;
802
803         /* if we are building a list we need to refresh the cache. */
804         jffs_init_1pass_list(part);
805         pL = (struct b_lists *)part->jffs2_priv;
806         pL->partOffset = part->offset;
807         puts ("Scanning JFFS2 FS:   ");
808
809         sectorsize = nand->erasesize;
810         nr_blocks = part->size / sectorsize;
811         buf = malloc(sectorsize);
812         if (!buf)
813                 return 0;
814
815         for (i = 0; i < nr_blocks; i++) {
816                 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
817
818                 offset = part->offset + i * sectorsize;
819
820                 if (nand_block_isbad(nand, offset))
821                         continue;
822
823                 if (jffs2_fill_scan_buf(nand, buf, offset, EMPTY_SCAN_SIZE))
824                         return 0;
825
826                 ofs = 0;
827                 /* Scan only 4KiB of 0xFF before declaring it's empty */
828                 while (ofs < EMPTY_SCAN_SIZE && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
829                         ofs += 4;
830                 if (ofs == EMPTY_SCAN_SIZE)
831                         continue;
832
833                 if (jffs2_fill_scan_buf(nand, buf + EMPTY_SCAN_SIZE, offset + EMPTY_SCAN_SIZE, sectorsize - EMPTY_SCAN_SIZE))
834                         return 0;
835                 offset += ofs;
836
837                 while (ofs < sectorsize - sizeof(struct jffs2_unknown_node)) {
838                         node = (struct jffs2_unknown_node *)&buf[ofs];
839                         if (node->magic != JFFS2_MAGIC_BITMASK || !hdr_crc(node)) {
840                                 offset += 4;
841                                 ofs += 4;
842                                 counter4++;
843                                 continue;
844                         }
845                         /* if its a fragment add it */
846                         if (node->nodetype == JFFS2_NODETYPE_INODE &&
847                                     inode_crc((struct jffs2_raw_inode *) node)) {
848                                 if (insert_inode(&pL->frag, (struct jffs2_raw_inode *) node,
849                                                  offset) == NULL) {
850                                         return 0;
851                                 }
852                         } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
853                                    dirent_crc((struct jffs2_raw_dirent *) node)  &&
854                                    dirent_name_crc((struct jffs2_raw_dirent *) node)) {
855                                 if (! (counterN%100))
856                                         puts ("\b\b.  ");
857                                 if (insert_dirent(&pL->dir, (struct jffs2_raw_dirent *) node,
858                                                   offset) == NULL) {
859                                         return 0;
860                                 }
861                                 counterN++;
862                         } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
863                                 if (node->totlen != sizeof(struct jffs2_unknown_node))
864                                         printf("OOPS Cleanmarker has bad size "
865                                                 "%d != %zu\n",
866                                                 node->totlen,
867                                                 sizeof(struct jffs2_unknown_node));
868                         } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
869                                 if (node->totlen < sizeof(struct jffs2_unknown_node))
870                                         printf("OOPS Padding has bad size "
871                                                 "%d < %zu\n",
872                                                 node->totlen,
873                                                 sizeof(struct jffs2_unknown_node));
874                         } else {
875                                 printf("Unknown node type: %x len %d offset 0x%x\n",
876                                         node->nodetype,
877                                         node->totlen, offset);
878                         }
879                         offset += ((node->totlen + 3) & ~3);
880                         ofs += ((node->totlen + 3) & ~3);
881                         counterF++;
882                 }
883         }
884
885         putstr("\b\b done.\r\n");               /* close off the dots */
886
887 #if 0
888         putLabeledWord("dir entries = ", pL->dir.listCount);
889         putLabeledWord("frag entries = ", pL->frag.listCount);
890         putLabeledWord("+4 increments = ", counter4);
891         putLabeledWord("+file_offset increments = ", counterF);
892 #endif
893
894 #ifdef DEBUG_DIRENTS
895         dump_dirents(pL);
896 #endif
897
898 #ifdef DEBUG_FRAGMENTS
899         dump_fragments(pL);
900 #endif
901
902         /* give visual feedback that we are done scanning the flash */
903         led_blink(0x0, 0x0, 0x1, 0x1);  /* off, forever, on 100ms, off 100ms */
904         free(buf);
905
906         return 1;
907 }
908
909
910 static u32
911 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
912 {
913         struct b_node *b;
914         struct jffs2_raw_inode ojNode;
915         struct jffs2_raw_inode *jNode;
916         int i;
917
918         for (i = 0; i < JFFS2_NUM_COMPR; i++) {
919                 piL->compr_info[i].num_frags = 0;
920                 piL->compr_info[i].compr_sum = 0;
921                 piL->compr_info[i].decompr_sum = 0;
922         }
923 /*      FIXME
924         b = pL->frag.listHead;
925         while (b) {
926                 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
927                         sizeof(ojNode), &ojNode);
928                 if (jNode->compr < JFFS2_NUM_COMPR) {
929                         piL->compr_info[jNode->compr].num_frags++;
930                         piL->compr_info[jNode->compr].compr_sum += jNode->csize;
931                         piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
932                 }
933                 b = b->next;
934         }
935 */
936         return 0;
937 }
938
939
940 static struct b_lists *
941 jffs2_get_list(struct part_info * part, const char *who)
942 {
943         if (jffs2_1pass_rescan_needed(part)) {
944                 if (!jffs2_1pass_build_lists(part)) {
945                         printf("%s: Failed to scan JFFSv2 file structure\n", who);
946                         return NULL;
947                 }
948         }
949         return (struct b_lists *)part->jffs2_priv;
950 }
951
952
953 /* Print directory / file contents */
954 u32
955 jffs2_1pass_ls(struct part_info * part, const char *fname)
956 {
957         struct b_lists *pl;
958         long ret = 0;
959         u32 inode;
960
961         if (! (pl = jffs2_get_list(part, "ls")))
962                 return 0;
963
964         if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
965                 putstr("ls: Failed to scan jffs2 file structure\r\n");
966                 return 0;
967         }
968
969 #if 0
970         putLabeledWord("found file at inode = ", inode);
971         putLabeledWord("read_inode returns = ", ret);
972 #endif
973
974         return ret;
975 }
976
977
978 /* Load a file from flash into memory. fname can be a full path */
979 u32
980 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
981 {
982
983         struct b_lists *pl;
984         long ret = 0;
985         u32 inode;
986
987         if (! (pl = jffs2_get_list(part, "load")))
988                 return 0;
989
990         if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
991                 putstr("load: Failed to find inode\r\n");
992                 return 0;
993         }
994
995         /* Resolve symlinks */
996         if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
997                 putstr("load: Failed to resolve inode structure\r\n");
998                 return 0;
999         }
1000
1001         if ((ret = jffs2_1pass_read_inode(pl, inode, dest, NULL)) < 0) {
1002                 putstr("load: Failed to read inode\r\n");
1003                 return 0;
1004         }
1005
1006         DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1007                                 (unsigned long) dest, ret);
1008         return ret;
1009 }
1010
1011 /* Return information about the fs on this partition */
1012 u32
1013 jffs2_1pass_info(struct part_info * part)
1014 {
1015         struct b_jffs2_info info;
1016         struct b_lists *pl;
1017         int i;
1018
1019         if (! (pl = jffs2_get_list(part, "info")))
1020                 return 0;
1021
1022         jffs2_1pass_fill_info(pl, &info);
1023         for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1024                 printf ("Compression: %s\n"
1025                         "\tfrag count: %d\n"
1026                         "\tcompressed sum: %d\n"
1027                         "\tuncompressed sum: %d\n",
1028                         compr_names[i],
1029                         info.compr_info[i].num_frags,
1030                         info.compr_info[i].compr_sum,
1031                         info.compr_info[i].decompr_sum);
1032         }
1033         return 1;
1034 }