]> Git Repo - u-boot.git/blob - fs/jffs2/jffs2_1pass.c
Merge patch series "led: add function naming option from linux"
[u-boot.git] / fs / jffs2 / jffs2_1pass.c
1 /*
2 -------------------------------------------------------------------------
3  * Filename:      jffs2.c
4  * Version:       $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
5  * Copyright:     Copyright (C) 2001, Russ Dill
6  * Author:        Russ Dill <[email protected]>
7  * Description:   Module to load kernel from jffs2
8  *-----------------------------------------------------------------------*/
9 /*
10  * some portions of this code are taken from jffs2, and as such, the
11  * following copyright notice is included.
12  *
13  * JFFS2 -- Journalling Flash File System, Version 2.
14  *
15  * Copyright (C) 2001 Red Hat, Inc.
16  *
17  * Created by David Woodhouse <[email protected]>
18  *
19  * The original JFFS, from which the design for JFFS2 was derived,
20  * was designed and implemented by Axis Communications AB.
21  *
22  * The contents of this file are subject to the Red Hat eCos Public
23  * License Version 1.1 (the "Licence"); you may not use this file
24  * except in compliance with the Licence.  You may obtain a copy of
25  * the Licence at http://www.redhat.com/
26  *
27  * Software distributed under the Licence is distributed on an "AS IS"
28  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
29  * See the Licence for the specific language governing rights and
30  * limitations under the Licence.
31  *
32  * The Original Code is JFFS2 - Journalling Flash File System, version 2
33  *
34  * Alternatively, the contents of this file may be used under the
35  * terms of the GNU General Public License version 2 (the "GPL"), in
36  * which case the provisions of the GPL are applicable instead of the
37  * above.  If you wish to allow the use of your version of this file
38  * only under the terms of the GPL and not to allow others to use your
39  * version of this file under the RHEPL, indicate your decision by
40  * deleting the provisions above and replace them with the notice and
41  * other provisions required by the GPL.  If you do not delete the
42  * provisions above, a recipient may use your version of this file
43  * under either the RHEPL or the GPL.
44  *
45  * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
46  *
47  */
48
49 /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
50  * bag to throw up into before reading this code. I looked through the jffs2
51  * code, the caching scheme is very elegant. I tried to keep the version
52  * for a bootloader as small and simple as possible. Instead of worring about
53  * unneccesary data copies, node scans, etc, I just optimized for the known
54  * common case, a kernel, which looks like:
55  *      (1) most pages are 4096 bytes
56  *      (2) version numbers are somewhat sorted in acsending order
57  *      (3) multiple compressed blocks making up one page is uncommon
58  *
59  * So I create a linked list of decending version numbers (insertions at the
60  * head), and then for each page, walk down the list, until a matching page
61  * with 4096 bytes is found, and then decompress the watching pages in
62  * reverse order.
63  *
64  */
65
66 /*
67  * Adapted by Nye Liu <[email protected]> and
68  * Rex Feany <[email protected]>
69  * on Jan/2002 for U-Boot.
70  *
71  * Clipped out all the non-1pass functions, cleaned up warnings,
72  * wrappers, etc. No major changes to the code.
73  * Please, he really means it when he said have a paper bag
74  * handy. We needed it ;).
75  *
76  */
77
78 /*
79  * Bugfixing by Kai-Uwe Bloem <[email protected]>, (C) Mar/2003
80  *
81  * - overhaul of the memory management. Removed much of the "paper-bagging"
82  *   in that part of the code, fixed several bugs, now frees memory when
83  *   partition is changed.
84  *   It's still ugly :-(
85  * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
86  *   was incorrect. Removed a bit of the paper-bagging as well.
87  * - removed double crc calculation for fragment headers in jffs2_private.h
88  *   for speedup.
89  * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
90  * - spinning wheel now spins depending on how much memory has been scanned
91  * - lots of small changes all over the place to "improve" readability.
92  * - implemented fragment sorting to ensure that the newest data is copied
93  *   if there are multiple copies of fragments for a certain file offset.
94  *
95  * The fragment sorting feature must be enabled by CONFIG_SYS_JFFS2_SORT_FRAGMENTS.
96  * Sorting is done while adding fragments to the lists, which is more or less a
97  * bubble sort. This takes a lot of time, and is most probably not an issue if
98  * the boot filesystem is always mounted readonly.
99  *
100  * You should define it if the boot filesystem is mounted writable, and updates
101  * to the boot files are done by copying files to that filesystem.
102  *
103  *
104  * There's a big issue left: endianess is completely ignored in this code. Duh!
105  *
106  *
107  * You still should have paper bags at hand :-(. The code lacks more or less
108  * any comment, and is still arcane and difficult to read in places. As this
109  * might be incompatible with any new code from the jffs2 maintainers anyway,
110  * it should probably be dumped and replaced by something like jffs2reader!
111  */
112
113 #include <config.h>
114 #include <malloc.h>
115 #include <div64.h>
116 #include <linux/compiler.h>
117 #include <linux/stat.h>
118 #include <linux/time.h>
119 #include <u-boot/crc.h>
120 #include <watchdog.h>
121 #include <jffs2/jffs2.h>
122 #include <jffs2/jffs2_1pass.h>
123 #include <linux/compat.h>
124 #include <linux/errno.h>
125
126 #include "jffs2_private.h"
127
128 #define NODE_CHUNK      1024    /* size of memory allocation chunk in b_nodes */
129 #define SPIN_BLKSIZE    18      /* spin after having scanned 1<<BLKSIZE bytes */
130
131 /* Debugging switches */
132 #undef  DEBUG_DIRENTS           /* print directory entry list after scan */
133 #undef  DEBUG_FRAGMENTS         /* print fragment list after scan */
134 #undef  DEBUG                   /* enable debugging messages */
135
136 #ifdef  DEBUG
137 # define DEBUGF(fmt,args...)    printf(fmt ,##args)
138 #else
139 # define DEBUGF(fmt,args...)
140 #endif
141
142 #include "summary.h"
143
144 /* keeps pointer to currentlu processed partition */
145 static struct part_info *current_part;
146
147 #if (defined(CONFIG_JFFS2_NAND) && \
148      defined(CONFIG_CMD_NAND) )
149 #include <nand.h>
150 /*
151  * Support for jffs2 on top of NAND-flash
152  *
153  * NAND memory isn't mapped in processor's address space,
154  * so data should be fetched from flash before
155  * being processed. This is exactly what functions declared
156  * here do.
157  *
158  */
159
160 #define NAND_PAGE_SIZE 512
161 #define NAND_PAGE_SHIFT 9
162 #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
163
164 #ifndef NAND_CACHE_PAGES
165 #define NAND_CACHE_PAGES 16
166 #endif
167 #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
168
169 static u8* nand_cache = NULL;
170 static u32 nand_cache_off = (u32)-1;
171
172 static int read_nand_cached(u32 off, u32 size, u_char *buf)
173 {
174         struct mtdids *id = current_part->dev->id;
175         struct mtd_info *mtd;
176         u32 bytes_read = 0;
177         size_t retlen;
178         size_t toread;
179         int cpy_bytes;
180
181         mtd = get_nand_dev_by_index(id->num);
182         if (!mtd)
183                 return -1;
184
185         while (bytes_read < size) {
186                 retlen = NAND_CACHE_SIZE;
187                 if( nand_cache_off + retlen > mtd->size )
188                         retlen = mtd->size - nand_cache_off;
189
190                 if ((off + bytes_read < nand_cache_off) ||
191                     (off + bytes_read >= nand_cache_off + retlen)) {
192                         nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
193                         if (!nand_cache) {
194                                 /* This memory never gets freed but 'cause
195                                    it's a bootloader, nobody cares */
196                                 nand_cache = malloc(NAND_CACHE_SIZE);
197                                 if (!nand_cache) {
198                                         printf("read_nand_cached: can't alloc cache size %d bytes\n",
199                                                NAND_CACHE_SIZE);
200                                         return -1;
201                                 }
202                         }
203
204                         toread = NAND_CACHE_SIZE;
205                         if( nand_cache_off + toread > mtd->size )
206                                 toread = mtd->size - nand_cache_off;
207
208                         retlen = toread;
209                         if (nand_read(mtd, nand_cache_off,
210                                       &retlen, nand_cache) < 0 ||
211                                         retlen != toread) {
212                                 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
213                                                 nand_cache_off, toread);
214                                 return -1;
215                         }
216                 }
217                 cpy_bytes = nand_cache_off + retlen - (off + bytes_read);
218                 if (cpy_bytes > size - bytes_read)
219                         cpy_bytes = size - bytes_read;
220                 memcpy(buf + bytes_read,
221                        nand_cache + off + bytes_read - nand_cache_off,
222                        cpy_bytes);
223                 bytes_read += cpy_bytes;
224         }
225         return bytes_read;
226 }
227
228 static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
229 {
230         u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
231
232         if (NULL == buf) {
233                 printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
234                 return NULL;
235         }
236         if (read_nand_cached(off, size, buf) < 0) {
237                 if (!ext_buf)
238                         free(buf);
239                 return NULL;
240         }
241
242         return buf;
243 }
244
245 static void *get_node_mem_nand(u32 off, void *ext_buf)
246 {
247         struct jffs2_unknown_node node;
248         void *ret = NULL;
249
250         if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
251                 return NULL;
252
253         if (!(ret = get_fl_mem_nand(off, node.magic ==
254                                JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
255                                ext_buf))) {
256                 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
257                        off, node.magic, node.nodetype, node.totlen);
258         }
259         return ret;
260 }
261
262 static void put_fl_mem_nand(void *buf)
263 {
264         free(buf);
265 }
266 #endif
267
268 #if defined(CONFIG_CMD_ONENAND)
269
270 #include <linux/mtd/mtd.h>
271 #include <linux/mtd/onenand.h>
272 #include <onenand_uboot.h>
273
274 #define ONENAND_PAGE_SIZE 2048
275 #define ONENAND_PAGE_SHIFT 11
276 #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
277
278 #ifndef ONENAND_CACHE_PAGES
279 #define ONENAND_CACHE_PAGES 4
280 #endif
281 #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
282
283 static u8* onenand_cache;
284 static u32 onenand_cache_off = (u32)-1;
285
286 static int read_onenand_cached(u32 off, u32 size, u_char *buf)
287 {
288         u32 bytes_read = 0;
289         size_t retlen;
290         size_t toread;
291         int cpy_bytes;
292
293         while (bytes_read < size) {
294                 retlen = ONENAND_CACHE_SIZE;
295                 if( onenand_cache_off + retlen > onenand_mtd.size )
296                         retlen = onenand_mtd.size - onenand_cache_off;
297
298                 if ((off + bytes_read < onenand_cache_off) ||
299                     (off + bytes_read >= onenand_cache_off + retlen)) {
300                         onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
301                         if (!onenand_cache) {
302                                 /* This memory never gets freed but 'cause
303                                    it's a bootloader, nobody cares */
304                                 onenand_cache = malloc(ONENAND_CACHE_SIZE);
305                                 if (!onenand_cache) {
306                                         printf("read_onenand_cached: can't alloc cache size %d bytes\n",
307                                                ONENAND_CACHE_SIZE);
308                                         return -1;
309                                 }
310                         }
311
312                         toread = ONENAND_CACHE_SIZE;
313                         if( onenand_cache_off + toread > onenand_mtd.size )
314                                 toread = onenand_mtd.size - onenand_cache_off;
315                         retlen = toread;
316                         if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
317                                                 &retlen, onenand_cache) < 0 ||
318                                         retlen != toread) {
319                                 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
320                                         onenand_cache_off, toread);
321                                 return -1;
322                         }
323                 }
324                 cpy_bytes = onenand_cache_off + retlen - (off + bytes_read);
325                 if (cpy_bytes > size - bytes_read)
326                         cpy_bytes = size - bytes_read;
327                 memcpy(buf + bytes_read,
328                        onenand_cache + off + bytes_read - onenand_cache_off,
329                        cpy_bytes);
330                 bytes_read += cpy_bytes;
331         }
332         return bytes_read;
333 }
334
335 static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
336 {
337         u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
338
339         if (NULL == buf) {
340                 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
341                 return NULL;
342         }
343         if (read_onenand_cached(off, size, buf) < 0) {
344                 if (!ext_buf)
345                         free(buf);
346                 return NULL;
347         }
348
349         return buf;
350 }
351
352 static void *get_node_mem_onenand(u32 off, void *ext_buf)
353 {
354         struct jffs2_unknown_node node;
355         void *ret = NULL;
356
357         if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
358                 return NULL;
359
360         ret = get_fl_mem_onenand(off, node.magic ==
361                         JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
362                         ext_buf);
363         if (!ret) {
364                 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
365                        off, node.magic, node.nodetype, node.totlen);
366         }
367         return ret;
368 }
369
370 static void put_fl_mem_onenand(void *buf)
371 {
372         free(buf);
373 }
374 #endif
375
376 #if defined(CONFIG_CMD_FLASH)
377 #include <flash.h>
378
379 /*
380  * Support for jffs2 on top of NOR-flash
381  *
382  * NOR flash memory is mapped in processor's address space,
383  * just return address.
384  */
385 static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
386 {
387         u32 addr = off;
388         struct mtdids *id = current_part->dev->id;
389
390         flash_info_t *flash = &flash_info[id->num];
391
392         addr += flash->start[0];
393         if (ext_buf) {
394                 memcpy(ext_buf, (void *)addr, size);
395                 return ext_buf;
396         }
397         return (void*)addr;
398 }
399
400 static inline void *get_node_mem_nor(u32 off, void *ext_buf)
401 {
402         struct jffs2_unknown_node *pNode;
403
404         /* pNode will point directly to flash - don't provide external buffer
405            and don't care about size */
406         pNode = get_fl_mem_nor(off, 0, NULL);
407         return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
408                         pNode->totlen : sizeof(*pNode), ext_buf);
409 }
410 #endif
411
412 /*
413  * Generic jffs2 raw memory and node read routines.
414  *
415  */
416 static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
417 {
418         struct mtdids *id = current_part->dev->id;
419
420         switch(id->type) {
421 #if defined(CONFIG_CMD_FLASH)
422         case MTD_DEV_TYPE_NOR:
423                 return get_fl_mem_nor(off, size, ext_buf);
424                 break;
425 #endif
426 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
427         case MTD_DEV_TYPE_NAND:
428                 return get_fl_mem_nand(off, size, ext_buf);
429                 break;
430 #endif
431 #if defined(CONFIG_CMD_ONENAND)
432         case MTD_DEV_TYPE_ONENAND:
433                 return get_fl_mem_onenand(off, size, ext_buf);
434                 break;
435 #endif
436         default:
437                 printf("get_fl_mem: unknown device type, " \
438                         "using raw offset!\n");
439         }
440         return (void*)off;
441 }
442
443 static inline void *get_node_mem(u32 off, void *ext_buf)
444 {
445         struct mtdids *id = current_part->dev->id;
446
447         switch(id->type) {
448 #if defined(CONFIG_CMD_FLASH)
449         case MTD_DEV_TYPE_NOR:
450                 return get_node_mem_nor(off, ext_buf);
451                 break;
452 #endif
453 #if defined(CONFIG_JFFS2_NAND) && \
454     defined(CONFIG_CMD_NAND)
455         case MTD_DEV_TYPE_NAND:
456                 return get_node_mem_nand(off, ext_buf);
457                 break;
458 #endif
459 #if defined(CONFIG_CMD_ONENAND)
460         case MTD_DEV_TYPE_ONENAND:
461                 return get_node_mem_onenand(off, ext_buf);
462                 break;
463 #endif
464         default:
465                 printf("get_fl_mem: unknown device type, " \
466                         "using raw offset!\n");
467         }
468         return (void*)off;
469 }
470
471 static inline void put_fl_mem(void *buf, void *ext_buf)
472 {
473         struct mtdids *id = current_part->dev->id;
474
475         /* If buf is the same as ext_buf, it was provided by the caller -
476            we shouldn't free it then. */
477         if (buf == ext_buf)
478                 return;
479         switch (id->type) {
480 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
481         case MTD_DEV_TYPE_NAND:
482                 return put_fl_mem_nand(buf);
483 #endif
484 #if defined(CONFIG_CMD_ONENAND)
485         case MTD_DEV_TYPE_ONENAND:
486                 return put_fl_mem_onenand(buf);
487 #endif
488         }
489 }
490
491 /* Compression names */
492 static char *compr_names[] = {
493         "NONE",
494         "ZERO",
495         "RTIME",
496         "RUBINMIPS",
497         "COPY",
498         "DYNRUBIN",
499         "ZLIB",
500 #if defined(CONFIG_JFFS2_LZO)
501         "LZO",
502 #endif
503 };
504
505 /* Memory management */
506 struct mem_block {
507         u32     index;
508         struct mem_block *next;
509         struct b_node nodes[NODE_CHUNK];
510 };
511
512 static void
513 free_nodes(struct b_list *list)
514 {
515         while (list->listMemBase != NULL) {
516                 struct mem_block *next = list->listMemBase->next;
517                 free( list->listMemBase );
518                 list->listMemBase = next;
519         }
520 }
521
522 static struct b_node *
523 add_node(struct b_list *list)
524 {
525         u32 index = 0;
526         struct mem_block *memBase;
527         struct b_node *b;
528
529         memBase = list->listMemBase;
530         if (memBase != NULL)
531                 index = memBase->index;
532 #if 0
533         putLabeledWord("add_node: index = ", index);
534         putLabeledWord("add_node: memBase = ", list->listMemBase);
535 #endif
536
537         if (memBase == NULL || index >= NODE_CHUNK) {
538                 /* we need more space before we continue */
539                 memBase = mmalloc(sizeof(struct mem_block));
540                 if (memBase == NULL) {
541                         putstr("add_node: malloc failed\n");
542                         return NULL;
543                 }
544                 memBase->next = list->listMemBase;
545                 index = 0;
546 #if 0
547                 putLabeledWord("add_node: alloced a new membase at ", *memBase);
548 #endif
549
550         }
551         /* now we have room to add it. */
552         b = &memBase->nodes[index];
553         index ++;
554
555         memBase->index = index;
556         list->listMemBase = memBase;
557         list->listCount++;
558         return b;
559 }
560
561 static struct b_node *
562 insert_node(struct b_list *list)
563 {
564         struct b_node *new;
565
566         if (!(new = add_node(list))) {
567                 putstr("add_node failed!\r\n");
568                 return NULL;
569         }
570         new->next = NULL;
571
572         if (list->listTail != NULL)
573                 list->listTail->next = new;
574         else
575                 list->listHead = new;
576         list->listTail = new;
577
578         return new;
579 }
580
581 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
582 /* Sort data entries with the latest version last, so that if there
583  * is overlapping data the latest version will be used.
584  */
585 static int compare_inodes(struct b_node *new, struct b_node *old)
586 {
587         return new->version > old->version;
588 }
589
590 /* Sort directory entries so all entries in the same directory
591  * with the same name are grouped together, with the latest version
592  * last. This makes it easy to eliminate all but the latest version
593  * by marking the previous version dead by setting the inode to 0.
594  */
595 static int compare_dirents(struct b_node *new, struct b_node *old)
596 {
597         /*
598          * Using NULL as the buffer for NOR flash prevents the entire node
599          * being read. This makes most comparisons much quicker as only one
600          * or two entries from the node will be used most of the time.
601          */
602         struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
603         struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
604         int cmp;
605         int ret;
606
607         if (jNew->pino != jOld->pino) {
608                 /* ascending sort by pino */
609                 ret = jNew->pino > jOld->pino;
610         } else if (jNew->nsize != jOld->nsize) {
611                 /*
612                  * pino is the same, so use ascending sort by nsize,
613                  * so we don't do strncmp unless we really must.
614                  */
615                 ret = jNew->nsize > jOld->nsize;
616         } else {
617                 /*
618                  * length is also the same, so use ascending sort by name
619                  */
620                 cmp = strncmp((char *)jNew->name, (char *)jOld->name,
621                         jNew->nsize);
622                 if (cmp != 0) {
623                         ret = cmp > 0;
624                 } else {
625                         /*
626                          * we have duplicate names in this directory,
627                          * so use ascending sort by version
628                          */
629                         ret = jNew->version > jOld->version;
630                 }
631         }
632         put_fl_mem(jNew, NULL);
633         put_fl_mem(jOld, NULL);
634
635         return ret;
636 }
637 #endif
638
639 void
640 jffs2_free_cache(struct part_info *part)
641 {
642         struct b_lists *pL;
643
644         if (part->jffs2_priv != NULL) {
645                 pL = (struct b_lists *)part->jffs2_priv;
646                 free_nodes(&pL->frag);
647                 free_nodes(&pL->dir);
648                 free(pL->readbuf);
649                 free(pL);
650         }
651 }
652
653 static u32
654 jffs_init_1pass_list(struct part_info *part)
655 {
656         struct b_lists *pL;
657
658         jffs2_free_cache(part);
659
660         if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
661                 pL = (struct b_lists *)part->jffs2_priv;
662
663                 memset(pL, 0, sizeof(*pL));
664 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
665                 pL->dir.listCompare = compare_dirents;
666                 pL->frag.listCompare = compare_inodes;
667 #endif
668         }
669         return 0;
670 }
671
672 /* find the inode from the slashless name given a parent */
673 static long
674 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
675 {
676         struct b_node *b;
677         struct jffs2_raw_inode *jNode;
678         u32 totalSize = 0;
679         u32 latestVersion = 0;
680         uchar *lDest;
681         uchar *src;
682         int i;
683         u32 counter = 0;
684
685         /* Find file size before loading any data, so fragments that
686          * start past the end of file can be ignored. A fragment
687          * that is partially in the file is loaded, so extra data may
688          * be loaded up to the next 4K boundary above the file size.
689          * This shouldn't cause trouble when loading kernel images, so
690          * we will live with it.
691          */
692         int latestOffset = -1;
693         for (b = pL->frag.listHead; b != NULL; b = b->next) {
694                 if (inode == b->ino) {
695                         /* get actual file length from the newest node */
696                         if (b->version >= latestVersion) {
697                                 latestVersion = b->version;
698                                 latestOffset = b->offset;
699                         }
700                 }
701         }
702
703         if (latestOffset >= 0) {
704                 jNode = (struct jffs2_raw_inode *)get_fl_mem(latestOffset,
705                         sizeof(struct jffs2_raw_inode), pL->readbuf);
706                 totalSize = jNode->isize;
707                 put_fl_mem(jNode, pL->readbuf);
708         }
709
710         /*
711          * If no destination is provided, we are done.
712          * Just return the total size.
713          */
714         if (!dest)
715                 return totalSize;
716
717         for (b = pL->frag.listHead; b != NULL; b = b->next) {
718                 if (inode == b->ino) {
719                         /*
720                          * Copy just the node and not the data at this point,
721                          * since we don't yet know if we need this data.
722                          */
723                         jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
724                                         sizeof(struct jffs2_raw_inode),
725                                         pL->readbuf);
726 #if 0
727                         putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
728                         putLabeledWord("read_inode: inode = ", jNode->ino);
729                         putLabeledWord("read_inode: version = ", jNode->version);
730                         putLabeledWord("read_inode: isize = ", jNode->isize);
731                         putLabeledWord("read_inode: offset = ", jNode->offset);
732                         putLabeledWord("read_inode: csize = ", jNode->csize);
733                         putLabeledWord("read_inode: dsize = ", jNode->dsize);
734                         putLabeledWord("read_inode: compr = ", jNode->compr);
735                         putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
736                         putLabeledWord("read_inode: flags = ", jNode->flags);
737 #endif
738
739                         if(dest) {
740                                 /*
741                                  * Now that the inode has been checked,
742                                  * read the entire inode, including data.
743                                  */
744                                 put_fl_mem(jNode, pL->readbuf);
745                                 jNode = (struct jffs2_raw_inode *)
746                                         get_node_mem(b->offset, pL->readbuf);
747                                 src = ((uchar *)jNode) +
748                                         sizeof(struct jffs2_raw_inode);
749                                 /* ignore data behind latest known EOF */
750                                 if (jNode->offset > totalSize) {
751                                         put_fl_mem(jNode, pL->readbuf);
752                                         continue;
753                                 }
754                                 if (b->datacrc == CRC_UNKNOWN)
755                                         b->datacrc = data_crc(jNode) ?
756                                                 CRC_OK : CRC_BAD;
757                                 if (b->datacrc == CRC_BAD) {
758                                         put_fl_mem(jNode, pL->readbuf);
759                                         continue;
760                                 }
761
762                                 lDest = (uchar *) (dest + jNode->offset);
763 #if 0
764                                 putLabeledWord("read_inode: src = ", src);
765                                 putLabeledWord("read_inode: dest = ", lDest);
766 #endif
767                                 switch (jNode->compr) {
768                                 case JFFS2_COMPR_NONE:
769                                         ldr_memcpy(lDest, src, jNode->dsize);
770                                         break;
771                                 case JFFS2_COMPR_ZERO:
772                                         for (i = 0; i < jNode->dsize; i++)
773                                                 *(lDest++) = 0;
774                                         break;
775                                 case JFFS2_COMPR_RTIME:
776                                         rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
777                                         break;
778                                 case JFFS2_COMPR_DYNRUBIN:
779                                         /* this is slow but it works */
780                                         dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
781                                         break;
782                                 case JFFS2_COMPR_ZLIB:
783                                         zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
784                                         break;
785 #if defined(CONFIG_JFFS2_LZO)
786                                 case JFFS2_COMPR_LZO:
787                                         lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
788                                         break;
789 #endif
790                                 default:
791                                         /* unknown */
792                                         putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
793                                         put_fl_mem(jNode, pL->readbuf);
794                                         return -1;
795                                         break;
796                                 }
797                         }
798
799 #if 0
800                         putLabeledWord("read_inode: totalSize = ", totalSize);
801 #endif
802                         put_fl_mem(jNode, pL->readbuf);
803                 }
804                 counter++;
805         }
806
807 #if 0
808         putLabeledWord("read_inode: returning = ", totalSize);
809 #endif
810         return totalSize;
811 }
812
813 /* find the inode from the slashless name given a parent */
814 static u32
815 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
816 {
817         struct b_node *b;
818         struct jffs2_raw_dirent *jDir;
819         int len;
820         u32 counter;
821         u32 version = 0;
822         u32 inode = 0;
823
824         /* name is assumed slash free */
825         len = strlen(name);
826
827         counter = 0;
828         /* we need to search all and return the inode with the highest version */
829         for(b = pL->dir.listHead; b; b = b->next, counter++) {
830                 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
831                                                                 pL->readbuf);
832                 if ((pino == jDir->pino) && (len == jDir->nsize) &&
833                     (!strncmp((char *)jDir->name, name, len))) {        /* a match */
834                         if (jDir->version < version) {
835                                 put_fl_mem(jDir, pL->readbuf);
836                                 continue;
837                         }
838
839                         if (jDir->version == version && inode != 0) {
840                                 /* I'm pretty sure this isn't legal */
841                                 putstr(" ** ERROR ** ");
842                                 putnstr(jDir->name, jDir->nsize);
843                                 putLabeledWord(" has dup version =", version);
844                         }
845                         inode = jDir->ino;
846                         version = jDir->version;
847                 }
848 #if 0
849                 putstr("\r\nfind_inode:p&l ->");
850                 putnstr(jDir->name, jDir->nsize);
851                 putstr("\r\n");
852                 putLabeledWord("pino = ", jDir->pino);
853                 putLabeledWord("nsize = ", jDir->nsize);
854                 putLabeledWord("b = ", (u32) b);
855                 putLabeledWord("counter = ", counter);
856 #endif
857                 put_fl_mem(jDir, pL->readbuf);
858         }
859         return inode;
860 }
861
862 char *mkmodestr(unsigned long mode, char *str)
863 {
864         static const char *l = "xwr";
865         int mask = 1, i;
866         char c;
867
868         switch (mode & S_IFMT) {
869                 case S_IFDIR:    str[0] = 'd'; break;
870                 case S_IFBLK:    str[0] = 'b'; break;
871                 case S_IFCHR:    str[0] = 'c'; break;
872                 case S_IFIFO:    str[0] = 'f'; break;
873                 case S_IFLNK:    str[0] = 'l'; break;
874                 case S_IFSOCK:   str[0] = 's'; break;
875                 case S_IFREG:    str[0] = '-'; break;
876                 default:         str[0] = '?';
877         }
878
879         for(i = 0; i < 9; i++) {
880                 c = l[i%3];
881                 str[9-i] = (mode & mask)?c:'-';
882                 mask = mask<<1;
883         }
884
885         if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
886         if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
887         if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
888         str[10] = '\0';
889         return str;
890 }
891
892 static inline void dump_stat(struct stat *st, const char *name)
893 {
894         char str[20];
895         char s[64], *p;
896
897         if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
898                 st->st_mtime = 1;
899
900         ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
901
902         if ((p = strchr(s,'\n')) != NULL) *p = '\0';
903         if ((p = strchr(s,'\r')) != NULL) *p = '\0';
904
905 /*
906         printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
907                 st->st_size, s, name);
908 */
909
910         printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
911 }
912
913 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
914 {
915         char fname[256];
916         struct stat st;
917
918         if(!d || !i) return -1;
919
920         strncpy(fname, (char *)d->name, d->nsize);
921         fname[d->nsize] = '\0';
922
923         memset(&st,0,sizeof(st));
924
925         st.st_mtime = i->mtime;
926         st.st_mode = i->mode;
927         st.st_ino = i->ino;
928         st.st_size = i->isize;
929
930         dump_stat(&st, fname);
931
932         if (d->type == DT_LNK) {
933                 unsigned char *src = (unsigned char *) (&i[1]);
934                 putstr(" -> ");
935                 putnstr(src, (int)i->dsize);
936         }
937
938         putstr("\r\n");
939
940         return 0;
941 }
942
943 /* list inodes with the given pino */
944 static u32
945 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
946 {
947         struct b_node *b;
948         struct jffs2_raw_dirent *jDir;
949
950         for (b = pL->dir.listHead; b; b = b->next) {
951                 if (pino == b->pino) {
952                         u32 i_version = 0;
953                         int i_offset = -1;
954                         struct jffs2_raw_inode *jNode = NULL;
955                         struct b_node *b2;
956
957                         jDir = (struct jffs2_raw_dirent *)
958                                 get_node_mem(b->offset, pL->readbuf);
959 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
960                         /* Check for more recent versions of this file */
961                         int match;
962                         do {
963                                 struct b_node *next = b->next;
964                                 struct jffs2_raw_dirent *jDirNext;
965                                 if (!next)
966                                         break;
967                                 jDirNext = (struct jffs2_raw_dirent *)
968                                         get_node_mem(next->offset, NULL);
969                                 match = jDirNext->pino == jDir->pino &&
970                                         jDirNext->nsize == jDir->nsize &&
971                                         strncmp((char *)jDirNext->name,
972                                                 (char *)jDir->name,
973                                                 jDir->nsize) == 0;
974                                 if (match) {
975                                         /* Use next. It is more recent */
976                                         b = next;
977                                         /* Update buffer with the new info */
978                                         *jDir = *jDirNext;
979                                 }
980                                 put_fl_mem(jDirNext, NULL);
981                         } while (match);
982 #endif
983                         if (jDir->ino == 0) {
984                                 /* Deleted file */
985                                 put_fl_mem(jDir, pL->readbuf);
986                                 continue;
987                         }
988
989                         for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
990                                 if (b2->ino == jDir->ino &&
991                                     b2->version >= i_version) {
992                                         i_version = b2->version;
993                                         i_offset = b2->offset;
994                                 }
995                         }
996
997                         if (i_version >= 0) {
998                                 if (jDir->type == DT_LNK)
999                                         jNode = get_node_mem(i_offset, NULL);
1000                                 else
1001                                         jNode = get_fl_mem(i_offset,
1002                                                            sizeof(*jNode),
1003                                                            NULL);
1004                         }
1005
1006                         dump_inode(pL, jDir, jNode);
1007                         put_fl_mem(jNode, NULL);
1008
1009                         put_fl_mem(jDir, pL->readbuf);
1010                 }
1011         }
1012         return pino;
1013 }
1014
1015 static u32
1016 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1017 {
1018         int i;
1019         char tmp[256];
1020         char working_tmp[256];
1021         char *c;
1022
1023         /* discard any leading slash */
1024         i = 0;
1025         while (fname[i] == '/')
1026                 i++;
1027         strcpy(tmp, &fname[i]);
1028
1029         while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1030         {
1031                 strncpy(working_tmp, tmp, c - tmp);
1032                 working_tmp[c - tmp] = '\0';
1033 #if 0
1034                 putstr("search_inode: tmp = ");
1035                 putstr(tmp);
1036                 putstr("\r\n");
1037                 putstr("search_inode: wtmp = ");
1038                 putstr(working_tmp);
1039                 putstr("\r\n");
1040                 putstr("search_inode: c = ");
1041                 putstr(c);
1042                 putstr("\r\n");
1043 #endif
1044                 for (i = 0; i < strlen(c) - 1; i++)
1045                         tmp[i] = c[i + 1];
1046                 tmp[i] = '\0';
1047 #if 0
1048                 putstr("search_inode: post tmp = ");
1049                 putstr(tmp);
1050                 putstr("\r\n");
1051 #endif
1052
1053                 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1054                         putstr("find_inode failed for name=");
1055                         putstr(working_tmp);
1056                         putstr("\r\n");
1057                         return 0;
1058                 }
1059         }
1060         /* this is for the bare filename, directories have already been mapped */
1061         if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1062                 putstr("find_inode failed for name=");
1063                 putstr(tmp);
1064                 putstr("\r\n");
1065                 return 0;
1066         }
1067         return pino;
1068
1069 }
1070
1071 static u32
1072 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1073 {
1074         struct b_node *b;
1075         struct b_node *b2;
1076         struct jffs2_raw_dirent *jDir;
1077         struct jffs2_raw_inode *jNode;
1078         u8 jDirFoundType = 0;
1079         u32 jDirFoundIno = 0;
1080         u32 jDirFoundPino = 0;
1081         char tmp[256];
1082         u32 version = 0;
1083         u32 pino;
1084         unsigned char *src;
1085
1086         /* we need to search all and return the inode with the highest version */
1087         for(b = pL->dir.listHead; b; b = b->next) {
1088                 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1089                                                                 pL->readbuf);
1090                 if (ino == jDir->ino) {
1091                         if (jDir->version < version) {
1092                                 put_fl_mem(jDir, pL->readbuf);
1093                                 continue;
1094                         }
1095
1096                         if (jDir->version == version && jDirFoundType) {
1097                                 /* I'm pretty sure this isn't legal */
1098                                 putstr(" ** ERROR ** ");
1099                                 putnstr(jDir->name, jDir->nsize);
1100                                 putLabeledWord(" has dup version (resolve) = ",
1101                                         version);
1102                         }
1103
1104                         jDirFoundType = jDir->type;
1105                         jDirFoundIno = jDir->ino;
1106                         jDirFoundPino = jDir->pino;
1107                         version = jDir->version;
1108                 }
1109                 put_fl_mem(jDir, pL->readbuf);
1110         }
1111         /* now we found the right entry again. (shoulda returned inode*) */
1112         if (jDirFoundType != DT_LNK)
1113                 return jDirFoundIno;
1114
1115         /* it's a soft link so we follow it again. */
1116         b2 = pL->frag.listHead;
1117         while (b2) {
1118                 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
1119                                                                 pL->readbuf);
1120                 if (jNode->ino == jDirFoundIno) {
1121                         src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
1122
1123 #if 0
1124                         putLabeledWord("\t\t dsize = ", jNode->dsize);
1125                         putstr("\t\t target = ");
1126                         putnstr(src, jNode->dsize);
1127                         putstr("\r\n");
1128 #endif
1129                         strncpy(tmp, (char *)src, jNode->dsize);
1130                         tmp[jNode->dsize] = '\0';
1131                         put_fl_mem(jNode, pL->readbuf);
1132                         break;
1133                 }
1134                 b2 = b2->next;
1135                 put_fl_mem(jNode, pL->readbuf);
1136         }
1137         /* ok so the name of the new file to find is in tmp */
1138         /* if it starts with a slash it is root based else shared dirs */
1139         if (tmp[0] == '/')
1140                 pino = 1;
1141         else
1142                 pino = jDirFoundPino;
1143
1144         return jffs2_1pass_search_inode(pL, tmp, pino);
1145 }
1146
1147 static u32
1148 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1149 {
1150         int i;
1151         char tmp[256];
1152         char working_tmp[256];
1153         char *c;
1154
1155         /* discard any leading slash */
1156         i = 0;
1157         while (fname[i] == '/')
1158                 i++;
1159         strcpy(tmp, &fname[i]);
1160         working_tmp[0] = '\0';
1161         while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1162         {
1163                 strncpy(working_tmp, tmp, c - tmp);
1164                 working_tmp[c - tmp] = '\0';
1165                 for (i = 0; i < strlen(c) - 1; i++)
1166                         tmp[i] = c[i + 1];
1167                 tmp[i] = '\0';
1168                 /* only a failure if we arent looking at top level */
1169                 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
1170                     (working_tmp[0])) {
1171                         putstr("find_inode failed for name=");
1172                         putstr(working_tmp);
1173                         putstr("\r\n");
1174                         return 0;
1175                 }
1176         }
1177
1178         if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1179                 putstr("find_inode failed for name=");
1180                 putstr(tmp);
1181                 putstr("\r\n");
1182                 return 0;
1183         }
1184         /* this is for the bare filename, directories have already been mapped */
1185         if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1186                 putstr("find_inode failed for name=");
1187                 putstr(tmp);
1188                 putstr("\r\n");
1189                 return 0;
1190         }
1191         return pino;
1192
1193 }
1194
1195 unsigned char
1196 jffs2_1pass_rescan_needed(struct part_info *part)
1197 {
1198         struct b_node *b;
1199         struct jffs2_unknown_node onode;
1200         struct jffs2_unknown_node *node;
1201         struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
1202
1203         if (part->jffs2_priv == 0){
1204                 DEBUGF ("rescan: First time in use\n");
1205                 return 1;
1206         }
1207
1208         /* if we have no list, we need to rescan */
1209         if (pL->frag.listCount == 0) {
1210                 DEBUGF ("rescan: fraglist zero\n");
1211                 return 1;
1212         }
1213
1214         /* but suppose someone reflashed a partition at the same offset... */
1215         b = pL->dir.listHead;
1216         while (b) {
1217                 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1218                         sizeof(onode), &onode);
1219                 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
1220                         DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1221                                         (unsigned long) b->offset);
1222                         return 1;
1223                 }
1224                 b = b->next;
1225         }
1226         return 0;
1227 }
1228
1229 #ifdef CONFIG_JFFS2_SUMMARY
1230 static u32 sum_get_unaligned32(u32 *ptr)
1231 {
1232         u32 val;
1233         u8 *p = (u8 *)ptr;
1234
1235         val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
1236
1237         return __le32_to_cpu(val);
1238 }
1239
1240 static u16 sum_get_unaligned16(u16 *ptr)
1241 {
1242         u16 val;
1243         u8 *p = (u8 *)ptr;
1244
1245         val = *p | (*(p + 1) << 8);
1246
1247         return __le16_to_cpu(val);
1248 }
1249
1250 #define dbg_summary(...) do {} while (0);
1251 /*
1252  * Process the stored summary information - helper function for
1253  * jffs2_sum_scan_sumnode()
1254  */
1255
1256 static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
1257                                 struct jffs2_raw_summary *summary,
1258                                 struct b_lists *pL)
1259 {
1260         void *sp;
1261         int i, pass;
1262         struct b_node *b;
1263
1264         for (pass = 0; pass < 2; pass++) {
1265                 sp = summary->sum;
1266
1267                 for (i = 0; i < summary->sum_num; i++) {
1268                         struct jffs2_sum_unknown_flash *spu = sp;
1269                         dbg_summary("processing summary index %d\n", i);
1270
1271                         switch (sum_get_unaligned16(&spu->nodetype)) {
1272                                 case JFFS2_NODETYPE_INODE: {
1273                                 struct jffs2_sum_inode_flash *spi;
1274                                         if (pass) {
1275                                                 spi = sp;
1276
1277                                                 b = insert_node(&pL->frag);
1278                                                 if (!b)
1279                                                         return -1;
1280                                                 b->offset = (u32)part->offset +
1281                                                         offset +
1282                                                         sum_get_unaligned32(
1283                                                                 &spi->offset);
1284                                                 b->version = sum_get_unaligned32(
1285                                                         &spi->version);
1286                                                 b->ino = sum_get_unaligned32(
1287                                                         &spi->inode);
1288                                                 b->datacrc = CRC_UNKNOWN;
1289                                         }
1290
1291                                         sp += JFFS2_SUMMARY_INODE_SIZE;
1292
1293                                         break;
1294                                 }
1295                                 case JFFS2_NODETYPE_DIRENT: {
1296                                         struct jffs2_sum_dirent_flash *spd;
1297                                         spd = sp;
1298                                         if (pass) {
1299                                                 b = insert_node(&pL->dir);
1300                                                 if (!b)
1301                                                         return -1;
1302                                                 b->offset = (u32)part->offset +
1303                                                         offset +
1304                                                         sum_get_unaligned32(
1305                                                                 &spd->offset);
1306                                                 b->version = sum_get_unaligned32(
1307                                                         &spd->version);
1308                                                 b->pino = sum_get_unaligned32(
1309                                                         &spd->pino);
1310                                                 b->datacrc = CRC_UNKNOWN;
1311                                         }
1312
1313                                         sp += JFFS2_SUMMARY_DIRENT_SIZE(
1314                                                         spd->nsize);
1315
1316                                         break;
1317                                 }
1318                                 default : {
1319                                         uint16_t nodetype = sum_get_unaligned16(
1320                                                                 &spu->nodetype);
1321                                         printf("Unsupported node type %x found"
1322                                                         " in summary!\n",
1323                                                         nodetype);
1324                                         if ((nodetype & JFFS2_COMPAT_MASK) ==
1325                                                         JFFS2_FEATURE_INCOMPAT)
1326                                                 return -EIO;
1327                                         return -EBADMSG;
1328                                 }
1329                         }
1330                 }
1331         }
1332         return 0;
1333 }
1334
1335 /* Process the summary node - called from jffs2_scan_eraseblock() */
1336 int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
1337                            struct jffs2_raw_summary *summary, uint32_t sumsize,
1338                            struct b_lists *pL)
1339 {
1340         struct jffs2_unknown_node crcnode;
1341         int ret, __maybe_unused ofs;
1342         uint32_t crc;
1343
1344         ofs = part->sector_size - sumsize;
1345
1346         dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
1347                     offset, offset + ofs, sumsize);
1348
1349         /* OK, now check for node validity and CRC */
1350         crcnode.magic = JFFS2_MAGIC_BITMASK;
1351         crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
1352         crcnode.totlen = summary->totlen;
1353         crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
1354
1355         if (summary->hdr_crc != crc) {
1356                 dbg_summary("Summary node header is corrupt (bad CRC or "
1357                                 "no summary at all)\n");
1358                 goto crc_err;
1359         }
1360
1361         if (summary->totlen != sumsize) {
1362                 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
1363                 goto crc_err;
1364         }
1365
1366         crc = crc32_no_comp(0, (uchar *)summary,
1367                         sizeof(struct jffs2_raw_summary)-8);
1368
1369         if (summary->node_crc != crc) {
1370                 dbg_summary("Summary node is corrupt (bad CRC)\n");
1371                 goto crc_err;
1372         }
1373
1374         crc = crc32_no_comp(0, (uchar *)summary->sum,
1375                         sumsize - sizeof(struct jffs2_raw_summary));
1376
1377         if (summary->sum_crc != crc) {
1378                 dbg_summary("Summary node data is corrupt (bad CRC)\n");
1379                 goto crc_err;
1380         }
1381
1382         if (summary->cln_mkr)
1383                 dbg_summary("Summary : CLEANMARKER node \n");
1384
1385         ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
1386         if (ret == -EBADMSG)
1387                 return 0;
1388         if (ret)
1389                 return ret;             /* real error */
1390
1391         return 1;
1392
1393 crc_err:
1394         putstr("Summary node crc error, skipping summary information.\n");
1395
1396         return 0;
1397 }
1398 #endif /* CONFIG_JFFS2_SUMMARY */
1399
1400 #ifdef DEBUG_FRAGMENTS
1401 static void
1402 dump_fragments(struct b_lists *pL)
1403 {
1404         struct b_node *b;
1405         struct jffs2_raw_inode ojNode;
1406         struct jffs2_raw_inode *jNode;
1407
1408         putstr("\r\n\r\n******The fragment Entries******\r\n");
1409         b = pL->frag.listHead;
1410         while (b) {
1411                 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1412                         sizeof(ojNode), &ojNode);
1413                 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1414                 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1415                 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1416                 putLabeledWord("\tbuild_list: version = ", jNode->version);
1417                 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1418                 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1419                 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1420                 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1421                 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1422                 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1423                 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1424                 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
1425                 putLabeledWord("\tbuild_list: offset = ", b->offset);   /* FIXME: ? [RS] */
1426                 b = b->next;
1427         }
1428 }
1429 #endif
1430
1431 #ifdef DEBUG_DIRENTS
1432 static void
1433 dump_dirents(struct b_lists *pL)
1434 {
1435         struct b_node *b;
1436         struct jffs2_raw_dirent *jDir;
1437
1438         putstr("\r\n\r\n******The directory Entries******\r\n");
1439         b = pL->dir.listHead;
1440         while (b) {
1441                 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1442                                                                 pL->readbuf);
1443                 putstr("\r\n");
1444                 putnstr(jDir->name, jDir->nsize);
1445                 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1446                 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1447                 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1448                 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1449                 putLabeledWord("\tbuild_list: version = ", jDir->version);
1450                 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1451                 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1452                 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1453                 putLabeledWord("\tbuild_list: type = ", jDir->type);
1454                 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1455                 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
1456                 putLabeledWord("\tbuild_list: offset = ", b->offset);   /* FIXME: ? [RS] */
1457                 b = b->next;
1458                 put_fl_mem(jDir, pL->readbuf);
1459         }
1460 }
1461 #endif
1462
1463 #define DEFAULT_EMPTY_SCAN_SIZE 256
1464
1465 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
1466 {
1467         if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
1468                 return sector_size;
1469         else
1470                 return DEFAULT_EMPTY_SCAN_SIZE;
1471 }
1472
1473 static u32
1474 jffs2_1pass_build_lists(struct part_info * part)
1475 {
1476         struct b_lists *pL;
1477         union jffs2_node_union *node;
1478         u32 nr_sectors;
1479         u32 i;
1480         u32 counter4 = 0;
1481         u32 counterF = 0;
1482         u32 counterN = 0;
1483         u32 max_totlen = 0;
1484         u32 buf_size;
1485         char *buf;
1486
1487         nr_sectors = lldiv(part->size, part->sector_size);
1488         /* turn off the lcd.  Refreshing the lcd adds 50% overhead to the */
1489         /* jffs2 list building enterprise nope.  in newer versions the overhead is */
1490         /* only about 5 %.  not enough to inconvenience people for. */
1491         /* lcd_off(); */
1492
1493         /* if we are building a list we need to refresh the cache. */
1494         jffs_init_1pass_list(part);
1495         pL = (struct b_lists *)part->jffs2_priv;
1496         buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
1497         puts ("Scanning JFFS2 FS:   ");
1498
1499         /* start at the beginning of the partition */
1500         for (i = 0; i < nr_sectors; i++) {
1501                 uint32_t sector_ofs = i * part->sector_size;
1502                 uint32_t buf_ofs = sector_ofs;
1503                 uint32_t buf_len;
1504                 uint32_t ofs, prevofs;
1505 #ifdef CONFIG_JFFS2_SUMMARY
1506                 struct jffs2_sum_marker *sm;
1507                 void *sumptr = NULL;
1508                 uint32_t sumlen;
1509                 int ret;
1510 #endif
1511                 /* Indicates a sector with a CLEANMARKER was found */
1512                 int clean_sector = 0;
1513                 struct jffs2_unknown_node crcnode;
1514                 struct b_node *b;
1515
1516                 /* Set buf_size to maximum length */
1517                 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
1518                 schedule();
1519
1520 #ifdef CONFIG_JFFS2_SUMMARY
1521                 buf_len = sizeof(*sm);
1522
1523                 /* Read as much as we want into the _end_ of the preallocated
1524                  * buffer
1525                  */
1526                 get_fl_mem(part->offset + sector_ofs + part->sector_size -
1527                                 buf_len, buf_len, buf + buf_size - buf_len);
1528
1529                 sm = (void *)buf + buf_size - sizeof(*sm);
1530                 if (sm->magic == JFFS2_SUM_MAGIC) {
1531                         sumlen = part->sector_size - sm->offset;
1532                         sumptr = buf + buf_size - sumlen;
1533
1534                         /* Now, make sure the summary itself is available */
1535                         if (sumlen > buf_size) {
1536                                 /* Need to kmalloc for this. */
1537                                 sumptr = malloc(sumlen);
1538                                 if (!sumptr) {
1539                                         putstr("Can't get memory for summary "
1540                                                         "node!\n");
1541                                         free(buf);
1542                                         jffs2_free_cache(part);
1543                                         return 0;
1544                                 }
1545                                 memcpy(sumptr + sumlen - buf_len, buf +
1546                                                 buf_size - buf_len, buf_len);
1547                         }
1548                         if (buf_len < sumlen) {
1549                                 /* Need to read more so that the entire summary
1550                                  * node is present
1551                                  */
1552                                 get_fl_mem(part->offset + sector_ofs +
1553                                                 part->sector_size - sumlen,
1554                                                 sumlen - buf_len, sumptr);
1555                         }
1556                 }
1557
1558                 if (sumptr) {
1559                         ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
1560                                         sumlen, pL);
1561
1562                         if (buf_size && sumlen > buf_size)
1563                                 free(sumptr);
1564                         if (ret < 0) {
1565                                 free(buf);
1566                                 jffs2_free_cache(part);
1567                                 return 0;
1568                         }
1569                         if (ret)
1570                                 continue;
1571
1572                 }
1573 #endif /* CONFIG_JFFS2_SUMMARY */
1574
1575                 buf_len = EMPTY_SCAN_SIZE(part->sector_size);
1576
1577                 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
1578
1579                 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
1580                 ofs = 0;
1581
1582                 /* Scan only 4KiB of 0xFF before declaring it's empty */
1583                 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
1584                                 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
1585                         ofs += 4;
1586
1587                 if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
1588                         continue;
1589
1590                 ofs += sector_ofs;
1591                 prevofs = ofs - 1;
1592                 /*
1593                  * Set buf_size down to the minimum size required.
1594                  * This prevents reading in chunks of flash data unnecessarily.
1595                  */
1596                 buf_size = sizeof(union jffs2_node_union);
1597
1598         scan_more:
1599                 while (ofs < sector_ofs + part->sector_size) {
1600                         if (ofs == prevofs) {
1601                                 printf("offset %08x already seen, skip\n", ofs);
1602                                 ofs += 4;
1603                                 counter4++;
1604                                 continue;
1605                         }
1606                         prevofs = ofs;
1607                         if (sector_ofs + part->sector_size <
1608                                         ofs + sizeof(struct jffs2_unknown_node))
1609                                 break;
1610                         if (buf_ofs + buf_len <
1611                                         ofs + sizeof(struct jffs2_unknown_node)) {
1612                                 buf_len = min_t(uint32_t, buf_size, sector_ofs
1613                                                 + part->sector_size - ofs);
1614                                 get_fl_mem((u32)part->offset + ofs, buf_len,
1615                                            buf);
1616                                 buf_ofs = ofs;
1617                         }
1618
1619                         node = (union jffs2_node_union *)&buf[ofs - buf_ofs];
1620
1621                         if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
1622                                 uint32_t inbuf_ofs;
1623                                 uint32_t scan_end;
1624
1625                                 ofs += 4;
1626                                 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
1627                                                         part->sector_size)/8,
1628                                                         buf_len);
1629                         more_empty:
1630                                 inbuf_ofs = ofs - buf_ofs;
1631                                 while (inbuf_ofs < scan_end) {
1632                                         if (*(uint32_t *)(&buf[inbuf_ofs]) !=
1633                                                         0xffffffff)
1634                                                 goto scan_more;
1635
1636                                         inbuf_ofs += 4;
1637                                         ofs += 4;
1638                                 }
1639                                 /* Ran off end. */
1640                                 /*
1641                                  * If this sector had a clean marker at the
1642                                  * beginning, and immediately following this
1643                                  * have been a bunch of FF bytes, treat the
1644                                  * entire sector as empty.
1645                                  */
1646                                 if (clean_sector)
1647                                         break;
1648
1649                                 /* See how much more there is to read in this
1650                                  * eraseblock...
1651                                  */
1652                                 buf_len = min_t(uint32_t, buf_size,
1653                                                 sector_ofs +
1654                                                 part->sector_size - ofs);
1655                                 if (!buf_len) {
1656                                         /* No more to read. Break out of main
1657                                          * loop without marking this range of
1658                                          * empty space as dirty (because it's
1659                                          * not)
1660                                          */
1661                                         break;
1662                                 }
1663                                 scan_end = buf_len;
1664                                 get_fl_mem((u32)part->offset + ofs, buf_len,
1665                                            buf);
1666                                 buf_ofs = ofs;
1667                                 goto more_empty;
1668                         }
1669                         /*
1670                          * Found something not erased in the sector, so reset
1671                          * the 'clean_sector' flag.
1672                          */
1673                         clean_sector = 0;
1674                         if (node->u.magic != JFFS2_MAGIC_BITMASK) {
1675                                 ofs += 4;
1676                                 counter4++;
1677                                 continue;
1678                         }
1679
1680                         crcnode.magic = node->u.magic;
1681                         crcnode.nodetype = node->u.nodetype | JFFS2_NODE_ACCURATE;
1682                         crcnode.totlen = node->u.totlen;
1683                         crcnode.hdr_crc = node->u.hdr_crc;
1684                         if (!hdr_crc(&crcnode)) {
1685                                 ofs += 4;
1686                                 counter4++;
1687                                 continue;
1688                         }
1689
1690                         if (ofs + node->u.totlen > sector_ofs + part->sector_size) {
1691                                 ofs += 4;
1692                                 counter4++;
1693                                 continue;
1694                         }
1695
1696                         if (!(node->u.nodetype & JFFS2_NODE_ACCURATE)) {
1697                                 DEBUGF("Obsolete node type: %x len %d offset 0x%x\n",
1698                                        node->u.nodetype, node->u.totlen, ofs);
1699                                 ofs += ((node->u.totlen + 3) & ~3);
1700                                 counterF++;
1701                                 continue;
1702                         }
1703
1704                         /* if its a fragment add it */
1705                         switch (node->u.nodetype) {
1706                         case JFFS2_NODETYPE_INODE:
1707                                 if (buf_ofs + buf_len <
1708                                         ofs + sizeof(struct jffs2_raw_inode)) {
1709                                         buf_len = min_t(uint32_t,
1710                                                         sizeof(struct jffs2_raw_inode),
1711                                                         sector_ofs +
1712                                                         part->sector_size -
1713                                                         ofs);
1714                                         get_fl_mem((u32)part->offset + ofs,
1715                                                    buf_len, buf);
1716                                         buf_ofs = ofs;
1717                                         node = (void *)buf;
1718                                 }
1719                                 if (!inode_crc((struct jffs2_raw_inode *)node))
1720                                         break;
1721
1722                                 b = insert_node(&pL->frag);
1723                                 if (!b) {
1724                                         free(buf);
1725                                         jffs2_free_cache(part);
1726                                         return 0;
1727                                 }
1728                                 b->offset = (u32)part->offset + ofs;
1729                                 b->version = node->i.version;
1730                                 b->ino = node->i.ino;
1731                                 if (max_totlen < node->u.totlen)
1732                                         max_totlen = node->u.totlen;
1733                                 break;
1734                         case JFFS2_NODETYPE_DIRENT:
1735                                 if (buf_ofs + buf_len < ofs + sizeof(struct
1736                                                         jffs2_raw_dirent) +
1737                                                         ((struct
1738                                                          jffs2_raw_dirent *)
1739                                                         node)->nsize) {
1740                                         buf_len = min_t(uint32_t,
1741                                                         node->u.totlen,
1742                                                         sector_ofs +
1743                                                         part->sector_size -
1744                                                         ofs);
1745                                         get_fl_mem((u32)part->offset + ofs,
1746                                                    buf_len, buf);
1747                                         buf_ofs = ofs;
1748                                         node = (void *)buf;
1749                                 }
1750
1751                                 if (!dirent_crc((struct jffs2_raw_dirent *)
1752                                                         node) ||
1753                                                 !dirent_name_crc(
1754                                                         (struct
1755                                                          jffs2_raw_dirent *)
1756                                                         node))
1757                                         break;
1758                                 if (! (counterN%100))
1759                                         puts ("\b\b.  ");
1760                                 b = insert_node(&pL->dir);
1761                                 if (!b) {
1762                                         free(buf);
1763                                         jffs2_free_cache(part);
1764                                         return 0;
1765                                 }
1766                                 b->offset = (u32)part->offset + ofs;
1767                                 b->version = node->d.version;
1768                                 b->pino = node->d.pino;
1769                                 if (max_totlen < node->u.totlen)
1770                                         max_totlen = node->u.totlen;
1771                                 counterN++;
1772                                 break;
1773                         case JFFS2_NODETYPE_CLEANMARKER:
1774                                 if (node->u.totlen != sizeof(struct jffs2_unknown_node))
1775                                         printf("OOPS Cleanmarker has bad size "
1776                                                 "%d != %zu\n",
1777                                                 node->u.totlen,
1778                                                 sizeof(struct jffs2_unknown_node));
1779                                 if (node->u.totlen ==
1780                                      sizeof(struct jffs2_unknown_node) &&
1781                                     ofs == sector_ofs) {
1782                                         /*
1783                                          * Found a CLEANMARKER at the beginning
1784                                          * of the sector. It's in the correct
1785                                          * place with correct size and CRC.
1786                                          */
1787                                         clean_sector = 1;
1788                                 }
1789                                 break;
1790                         case JFFS2_NODETYPE_PADDING:
1791                                 if (node->u.totlen <
1792                                                 sizeof(struct jffs2_unknown_node))
1793                                         printf("OOPS Padding has bad size "
1794                                                 "%d < %zu\n",
1795                                                 node->u.totlen,
1796                                                 sizeof(struct jffs2_unknown_node));
1797                                 break;
1798                         case JFFS2_NODETYPE_SUMMARY:
1799                                 break;
1800                         default:
1801                                 printf("Unknown node type: %x len %d offset 0x%x\n",
1802                                         node->u.nodetype,
1803                                         node->u.totlen, ofs);
1804                         }
1805                         ofs += ((node->u.totlen + 3) & ~3);
1806                         counterF++;
1807                 }
1808         }
1809
1810         free(buf);
1811 #if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
1812         /*
1813          * Sort the lists.
1814          */
1815         sort_list(&pL->frag);
1816         sort_list(&pL->dir);
1817 #endif
1818         putstr("\b\b done.\r\n");               /* close off the dots */
1819
1820         /* We don't care if malloc failed - then each read operation will
1821          * allocate its own buffer as necessary (NAND) or will read directly
1822          * from flash (NOR).
1823          */
1824         pL->readbuf = malloc(max_totlen);
1825
1826         /* turn the lcd back on. */
1827         /* splash(); */
1828
1829 #if 0
1830         putLabeledWord("dir entries = ", pL->dir.listCount);
1831         putLabeledWord("frag entries = ", pL->frag.listCount);
1832         putLabeledWord("+4 increments = ", counter4);
1833         putLabeledWord("+file_offset increments = ", counterF);
1834
1835 #endif
1836
1837 #ifdef DEBUG_DIRENTS
1838         dump_dirents(pL);
1839 #endif
1840
1841 #ifdef DEBUG_FRAGMENTS
1842         dump_fragments(pL);
1843 #endif
1844
1845         /* give visual feedback that we are done scanning the flash */
1846         led_blink(0x0, 0x0, 0x1, 0x1);  /* off, forever, on 100ms, off 100ms */
1847         return 1;
1848 }
1849
1850 static u32
1851 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1852 {
1853         struct b_node *b;
1854         struct jffs2_raw_inode ojNode;
1855         struct jffs2_raw_inode *jNode;
1856         int i;
1857
1858         for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1859                 piL->compr_info[i].num_frags = 0;
1860                 piL->compr_info[i].compr_sum = 0;
1861                 piL->compr_info[i].decompr_sum = 0;
1862         }
1863
1864         b = pL->frag.listHead;
1865         while (b) {
1866                 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1867                         sizeof(ojNode), &ojNode);
1868                 if (jNode->compr < JFFS2_NUM_COMPR) {
1869                         piL->compr_info[jNode->compr].num_frags++;
1870                         piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1871                         piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1872                 }
1873                 b = b->next;
1874         }
1875         return 0;
1876 }
1877
1878 static struct b_lists *
1879 jffs2_get_list(struct part_info * part, const char *who)
1880 {
1881         /* copy requested part_info struct pointer to global location */
1882         current_part = part;
1883
1884         if (jffs2_1pass_rescan_needed(part)) {
1885                 if (!jffs2_1pass_build_lists(part)) {
1886                         printf("%s: Failed to scan JFFSv2 file structure\n", who);
1887                         return NULL;
1888                 }
1889         }
1890         return (struct b_lists *)part->jffs2_priv;
1891 }
1892
1893 /* Print directory / file contents */
1894 u32
1895 jffs2_1pass_ls(struct part_info * part, const char *fname)
1896 {
1897         struct b_lists *pl;
1898         long ret = 1;
1899         u32 inode;
1900
1901         if (! (pl = jffs2_get_list(part, "ls")))
1902                 return 0;
1903
1904         if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1905                 putstr("ls: Failed to scan jffs2 file structure\r\n");
1906                 return 0;
1907         }
1908
1909 #if 0
1910         putLabeledWord("found file at inode = ", inode);
1911         putLabeledWord("read_inode returns = ", ret);
1912 #endif
1913
1914         return ret;
1915 }
1916
1917 /* Load a file from flash into memory. fname can be a full path */
1918 u32
1919 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1920 {
1921
1922         struct b_lists *pl;
1923         long ret = 1;
1924         u32 inode;
1925
1926         if (! (pl  = jffs2_get_list(part, "load")))
1927                 return 0;
1928
1929         if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1930                 putstr("load: Failed to find inode\r\n");
1931                 return 0;
1932         }
1933
1934         /* Resolve symlinks */
1935         if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1936                 putstr("load: Failed to resolve inode structure\r\n");
1937                 return 0;
1938         }
1939
1940         if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1941                 putstr("load: Failed to read inode\r\n");
1942                 return 0;
1943         }
1944
1945         DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1946                                 (unsigned long) dest, ret);
1947         return ret;
1948 }
1949
1950 /* Return information about the fs on this partition */
1951 u32
1952 jffs2_1pass_info(struct part_info * part)
1953 {
1954         struct b_jffs2_info info;
1955         struct b_lists *pl;
1956         int i;
1957
1958         if (! (pl  = jffs2_get_list(part, "info")))
1959                 return 0;
1960
1961         jffs2_1pass_fill_info(pl, &info);
1962         for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1963                 printf ("Compression: %s\n"
1964                         "\tfrag count: %d\n"
1965                         "\tcompressed sum: %d\n"
1966                         "\tuncompressed sum: %d\n",
1967                         compr_names[i],
1968                         info.compr_info[i].num_frags,
1969                         info.compr_info[i].compr_sum,
1970                         info.compr_info[i].decompr_sum);
1971         }
1972         return 1;
1973 }
This page took 0.141177 seconds and 4 git commands to generate.