blob: 4518a2eaae2cbe925f88ffe7940193cd9013fcdd [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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 <Russ.Dill@asu.edu>
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 <dwmw2@cambridge.redhat.com>
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 <nyet@zumanetworks.com> and
68 * Rex Feany <rfeany@zumanetworks.com>
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 <kai-uwe.bloem@auerswald.de>, (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
114#include <common.h>
115#include <config.h>
116#include <malloc.h>
117#include <div64.h>
118#include <linux/compiler.h>
119#include <linux/stat.h>
120#include <linux/time.h>
121#include <watchdog.h>
122#include <jffs2/jffs2.h>
123#include <jffs2/jffs2_1pass.h>
124#include <linux/mtd/compat.h>
125#include <asm/errno.h>
126
127#include "jffs2_private.h"
128
129
130#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
131#define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
132
133/* Debugging switches */
134#undef DEBUG_DIRENTS /* print directory entry list after scan */
135#undef DEBUG_FRAGMENTS /* print fragment list after scan */
136#undef DEBUG /* enable debugging messages */
137
138
139#ifdef DEBUG
140# define DEBUGF(fmt,args...) printf(fmt ,##args)
141#else
142# define DEBUGF(fmt,args...)
143#endif
144
145#include "summary.h"
146
147/* keeps pointer to currentlu processed partition */
148static struct part_info *current_part;
149
150#if (defined(CONFIG_JFFS2_NAND) && \
151 defined(CONFIG_CMD_NAND) )
152#include <nand.h>
153/*
154 * Support for jffs2 on top of NAND-flash
155 *
156 * NAND memory isn't mapped in processor's address space,
157 * so data should be fetched from flash before
158 * being processed. This is exactly what functions declared
159 * here do.
160 *
161 */
162
163#define NAND_PAGE_SIZE 2048
164#define NAND_PAGE_SHIFT 11
165#define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
166
167#ifndef NAND_CACHE_PAGES
168#define NAND_CACHE_PAGES 16
169#endif
170#define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
171
172static u8* nand_cache = NULL;
173static u32 nand_cache_off = (u32)-1;
174
175int nand_read_ddr(unsigned ofs, unsigned len, unsigned char *buf)
176{
177 memcpy(buf, ofs, len);
178
179 return 0;
180}
181
182
183static int read_nand_cached(u32 off, u32 size, u_char *buf)
184{
185 struct mtdids *id = current_part->dev->id;
186 u32 bytes_read = 0;
187 size_t retlen;
188 int cpy_bytes;
189
190 while (bytes_read < size) {
191 if ((off + bytes_read < nand_cache_off) ||
192 (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
193 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
194 if (!nand_cache) {
195 /* This memory never gets freed but 'cause
196 it's a bootloader, nobody cares */
197 nand_cache = malloc(NAND_CACHE_SIZE);
198 if (!nand_cache) {
199 printf("read_nand_cached: can't alloc cache size %d bytes\n",
200 NAND_CACHE_SIZE);
201 return -1;
202 }
203 }
204
205 retlen = NAND_CACHE_SIZE;
206 nand_read_ddr(nand_cache_off, retlen, nand_cache);
207 }
208 cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
209 if (cpy_bytes > size - bytes_read)
210 cpy_bytes = size - bytes_read;
211 memcpy(buf + bytes_read,
212 nand_cache + off + bytes_read - nand_cache_off,
213 cpy_bytes);
214 bytes_read += cpy_bytes;
215 }
216 return bytes_read;
217}
218
219static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
220{
221 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
222
223 if (NULL == buf) {
224 printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
225 return NULL;
226 }
227 if (read_nand_cached(off, size, buf) < 0) {
228 if (!ext_buf)
229 free(buf);
230 return NULL;
231 }
232
233 return buf;
234}
235
236static void *get_node_mem_nand(u32 off, void *ext_buf)
237{
238 struct jffs2_unknown_node node;
239 void *ret = NULL;
240
241 if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
242 return NULL;
243
244 if (!(ret = get_fl_mem_nand(off, node.magic ==
245 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
246 ext_buf))) {
247 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
248 off, node.magic, node.nodetype, node.totlen);
249 }
250 return ret;
251}
252
253static void put_fl_mem_nand(void *buf)
254{
255 free(buf);
256}
257#endif
258
259#if defined(CONFIG_CMD_ONENAND)
260
261#include <linux/mtd/mtd.h>
262#include <linux/mtd/onenand.h>
263#include <onenand_uboot.h>
264
265#define ONENAND_PAGE_SIZE 2048
266#define ONENAND_PAGE_SHIFT 11
267#define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
268
269#ifndef ONENAND_CACHE_PAGES
270#define ONENAND_CACHE_PAGES 4
271#endif
272#define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
273
274static u8* onenand_cache;
275static u32 onenand_cache_off = (u32)-1;
276
277static int read_onenand_cached(u32 off, u32 size, u_char *buf)
278{
279 u32 bytes_read = 0;
280 size_t retlen;
281 int cpy_bytes;
282
283 while (bytes_read < size) {
284 if ((off + bytes_read < onenand_cache_off) ||
285 (off + bytes_read >= onenand_cache_off + ONENAND_CACHE_SIZE)) {
286 onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
287 if (!onenand_cache) {
288 /* This memory never gets freed but 'cause
289 it's a bootloader, nobody cares */
290 onenand_cache = malloc(ONENAND_CACHE_SIZE);
291 if (!onenand_cache) {
292 printf("read_onenand_cached: can't alloc cache size %d bytes\n",
293 ONENAND_CACHE_SIZE);
294 return -1;
295 }
296 }
297
298 retlen = ONENAND_CACHE_SIZE;
299 if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
300 &retlen, onenand_cache) != 0 ||
301 retlen != ONENAND_CACHE_SIZE) {
302 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
303 onenand_cache_off, ONENAND_CACHE_SIZE);
304 return -1;
305 }
306 }
307 cpy_bytes = onenand_cache_off + ONENAND_CACHE_SIZE - (off + bytes_read);
308 if (cpy_bytes > size - bytes_read)
309 cpy_bytes = size - bytes_read;
310 memcpy(buf + bytes_read,
311 onenand_cache + off + bytes_read - onenand_cache_off,
312 cpy_bytes);
313 bytes_read += cpy_bytes;
314 }
315 return bytes_read;
316}
317
318static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
319{
320 u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
321
322 if (NULL == buf) {
323 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
324 return NULL;
325 }
326 if (read_onenand_cached(off, size, buf) < 0) {
327 if (!ext_buf)
328 free(buf);
329 return NULL;
330 }
331
332 return buf;
333}
334
335static void *get_node_mem_onenand(u32 off, void *ext_buf)
336{
337 struct jffs2_unknown_node node;
338 void *ret = NULL;
339
340 if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
341 return NULL;
342
343 ret = get_fl_mem_onenand(off, node.magic ==
344 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
345 ext_buf);
346 if (!ret) {
347 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
348 off, node.magic, node.nodetype, node.totlen);
349 }
350 return ret;
351}
352
353
354static void put_fl_mem_onenand(void *buf)
355{
356 free(buf);
357}
358#endif
359
360
361#if defined(CONFIG_CMD_FLASH)
362/*
363 * Support for jffs2 on top of NOR-flash
364 *
365 * NOR flash memory is mapped in processor's address space,
366 * just return address.
367 */
368static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
369{
370 u32 addr = off;
371 struct mtdids *id = current_part->dev->id;
372
373 extern flash_info_t flash_info[];
374 flash_info_t *flash = &flash_info[id->num];
375
376 addr += flash->start[0];
377 if (ext_buf) {
378 memcpy(ext_buf, (void *)addr, size);
379 return ext_buf;
380 }
381 return (void*)addr;
382}
383
384static inline void *get_node_mem_nor(u32 off, void *ext_buf)
385{
386 struct jffs2_unknown_node *pNode;
387
388 /* pNode will point directly to flash - don't provide external buffer
389 and don't care about size */
390 pNode = get_fl_mem_nor(off, 0, NULL);
391 return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
392 pNode->totlen : sizeof(*pNode), ext_buf);
393}
394#endif
395
396
397/*
398 * Generic jffs2 raw memory and node read routines.
399 *
400 */
401static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
402{
403 struct mtdids *id = current_part->dev->id;
404
405 switch(id->type) {
406#if defined(CONFIG_CMD_FLASH)
407 case MTD_DEV_TYPE_NOR:
408 return get_fl_mem_nor(off, size, ext_buf);
409 break;
410#endif
411#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
412 case MTD_DEV_TYPE_NAND:
413 return get_fl_mem_nand(off, size, ext_buf);
414 break;
415#endif
416#if defined(CONFIG_CMD_ONENAND)
417 case MTD_DEV_TYPE_ONENAND:
418 return get_fl_mem_onenand(off, size, ext_buf);
419 break;
420#endif
421 default:
422 printf("get_fl_mem: unknown device type, " \
423 "using raw offset!\n");
424 }
425 return (void*)off;
426}
427
428static inline void *get_node_mem(u32 off, void *ext_buf)
429{
430 struct mtdids *id = current_part->dev->id;
431
432 switch(id->type) {
433#if defined(CONFIG_CMD_FLASH)
434 case MTD_DEV_TYPE_NOR:
435 return get_node_mem_nor(off, ext_buf);
436 break;
437#endif
438#if defined(CONFIG_JFFS2_NAND) && \
439 defined(CONFIG_CMD_NAND)
440 case MTD_DEV_TYPE_NAND:
441 return get_node_mem_nand(off, ext_buf);
442 break;
443#endif
444#if defined(CONFIG_CMD_ONENAND)
445 case MTD_DEV_TYPE_ONENAND:
446 return get_node_mem_onenand(off, ext_buf);
447 break;
448#endif
449 default:
450 printf("get_fl_mem: unknown device type, " \
451 "using raw offset!\n");
452 }
453 return (void*)off;
454}
455
456static inline void put_fl_mem(void *buf, void *ext_buf)
457{
458 struct mtdids *id = current_part->dev->id;
459
460 /* If buf is the same as ext_buf, it was provided by the caller -
461 we shouldn't free it then. */
462 if (buf == ext_buf)
463 return;
464 switch (id->type) {
465#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
466 case MTD_DEV_TYPE_NAND:
467 return put_fl_mem_nand(buf);
468#endif
469#if defined(CONFIG_CMD_ONENAND)
470 case MTD_DEV_TYPE_ONENAND:
471 return put_fl_mem_onenand(buf);
472#endif
473 }
474}
475
476/* Compression names */
477static char *compr_names[] = {
478 "NONE",
479 "ZERO",
480 "RTIME",
481 "RUBINMIPS",
482 "COPY",
483 "DYNRUBIN",
484 "ZLIB",
485#if defined(CONFIG_JFFS2_LZO)
486 "LZO",
487#endif
488};
489
490/* Memory management */
491struct mem_block {
492 u32 index;
493 struct mem_block *next;
494 struct b_node nodes[NODE_CHUNK];
495};
496
497
498static void
499free_nodes(struct b_list *list)
500{
501 while (list->listMemBase != NULL) {
502 struct mem_block *next = list->listMemBase->next;
503 free( list->listMemBase );
504 list->listMemBase = next;
505 }
506}
507
508static struct b_node *
509add_node(struct b_list *list)
510{
511 u32 index = 0;
512 struct mem_block *memBase;
513 struct b_node *b;
514
515 memBase = list->listMemBase;
516 if (memBase != NULL)
517 index = memBase->index;
518#if 0
519 putLabeledWord("add_node: index = ", index);
520 putLabeledWord("add_node: memBase = ", list->listMemBase);
521#endif
522
523 if (memBase == NULL || index >= NODE_CHUNK) {
524 /* we need more space before we continue */
525 memBase = mmalloc(sizeof(struct mem_block));
526 if (memBase == NULL) {
527 putstr("add_node: malloc failed\n");
528 return NULL;
529 }
530 memBase->next = list->listMemBase;
531 index = 0;
532#if 0
533 putLabeledWord("add_node: alloced a new membase at ", *memBase);
534#endif
535
536 }
537 /* now we have room to add it. */
538 b = &memBase->nodes[index];
539 index ++;
540
541 memBase->index = index;
542 list->listMemBase = memBase;
543 list->listCount++;
544 b->datacrc = CRC_UNKNOWN;
545 return b;
546}
547
548static struct b_node *
549insert_node(struct b_list *list, u32 offset)
550{
551 struct b_node *new;
552
553 if (!(new = add_node(list))) {
554 putstr("add_node failed!\r\n");
555 return NULL;
556 }
557 new->offset = offset;
558 new->next = NULL;
559
560 if (list->listTail != NULL)
561 list->listTail->next = new;
562 else
563 list->listHead = new;
564 list->listTail = new;
565
566 return new;
567}
568
569#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
570/* Sort data entries with the latest version last, so that if there
571 * is overlapping data the latest version will be used.
572 */
573#if CONFIG_MUTUAL_DEBUG
574static int g_compare_inode_count = 0;
575#endif
576static int compare_inodes(struct b_node *new, struct b_node *old)
577{
578 /*
579 * Only read in the version info from flash, not the entire inode.
580 * This can make a big difference to speed if flash is slow.
581 */
582 u32 new_version;
583 u32 old_version;
584 get_fl_mem(new->offset + offsetof(struct jffs2_raw_inode, version),
585 sizeof(new_version), &new_version);
586 get_fl_mem(old->offset + offsetof(struct jffs2_raw_inode, version),
587 sizeof(old_version), &old_version);
588#if CONFIG_MUTUAL_DEBUG
589 g_compare_inode_count++;
590#endif
591 return new_version > old_version;
592}
593
594#if 0
595static int compare_inodes2(struct b_node **new, struct b_node **old)
596{
597 /*
598 * Only read in the version info from flash, not the entire inode.
599 * This can make a big difference to speed if flash is slow.
600 */
601 u32 new_version;
602 u32 old_version;
603 get_fl_mem((*new)->offset + offsetof(struct jffs2_raw_inode, version),
604 sizeof(new_version), &new_version);
605 get_fl_mem((*old)->offset + offsetof(struct jffs2_raw_inode, version),
606 sizeof(old_version), &old_version);
607#if CONFIG_MUTUAL_DEBUG
608 g_compare_inode_count++;
609#endif
610
611 return new_version - old_version;
612}
613#endif
614
615#endif /*CONFIG_SYS_JFFS2_SORT_FRAGMENTS*/
616
617#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS_DIR) || defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
618/* Sort directory entries so all entries in the same directory
619 * with the same name are grouped together, with the latest version
620 * last. This makes it easy to eliminate all but the latest version
621 * by marking the previous version dead by setting the inode to 0.
622 */
623static int compare_dirents(struct b_node *new, struct b_node *old)
624{
625 /*
626 * Using NULL as the buffer for NOR flash prevents the entire node
627 * being read. This makes most comparisons much quicker as only one
628 * or two entries from the node will be used most of the time.
629 */
630 struct jffs2_raw_dirent *jNew = NULL;
631 struct jffs2_raw_dirent *jOld = NULL;
632 int cmp;
633 int ret;
634
635 jNew = get_node_mem(new->offset, NULL);
636 if(jNew == NULL)
637 {
638 return 0;
639 }
640 jOld = get_node_mem(old->offset, NULL);
641 if(jOld == NULL)
642 {
643 put_fl_mem(jNew, NULL);
644 return 0;
645 }
646
647 if (jNew->pino != jOld->pino) {
648 /* ascending sort by pino */
649 ret = jNew->pino > jOld->pino;
650 } else if (jNew->nsize != jOld->nsize) {
651 /*
652 * pino is the same, so use ascending sort by nsize,
653 * so we don't do strncmp unless we really must.
654 */
655 ret = jNew->nsize > jOld->nsize;
656 } else {
657 /*
658 * length is also the same, so use ascending sort by name
659 */
660 cmp = strncmp((char *)jNew->name, (char *)jOld->name,
661 jNew->nsize);
662 if (cmp != 0) {
663 ret = cmp > 0;
664 } else {
665 /*
666 * we have duplicate names in this directory,
667 * so use ascending sort by version
668 */
669 ret = jNew->version > jOld->version;
670 }
671 }
672 put_fl_mem(jNew, NULL);
673 put_fl_mem(jOld, NULL);
674
675 return ret;
676}
677#endif
678
679void
680jffs2_free_cache(struct part_info *part)
681{
682 struct b_lists *pL;
683
684 if (part->jffs2_priv != NULL) {
685 pL = (struct b_lists *)part->jffs2_priv;
686 free_nodes(&pL->frag);
687 free_nodes(&pL->dir);
688 free(pL->readbuf);
689 free(pL);
690 }
691}
692
693static u32
694jffs_init_1pass_list(struct part_info *part)
695{
696 struct b_lists *pL;
697
698 jffs2_free_cache(part);
699 nand_cache_off = (u32)-1;
700
701 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
702 pL = (struct b_lists *)part->jffs2_priv;
703
704 memset(pL, 0, sizeof(*pL));
705#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS) || defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS_DIR)
706 pL->dir.listCompare = compare_dirents;
707#endif
708#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
709 pL->frag.listCompare = compare_inodes;
710#endif
711 }
712 return 0;
713}
714
715/* find the inode from the slashless name given a parent */
716static long
717jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
718{
719 struct b_node *b;
720 struct jffs2_raw_inode *jNode;
721 u32 totalSize = 0;
722 u32 latestVersion = 0;
723 uchar *lDest;
724 uchar *src;
725 int i;
726 u32 counter = 0;
727#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
728 /* Find file size before loading any data, so fragments that
729 * start past the end of file can be ignored. A fragment
730 * that is partially in the file is loaded, so extra data may
731 * be loaded up to the next 4K boundary above the file size.
732 * This shouldn't cause trouble when loading kernel images, so
733 * we will live with it.
734 */
735 for (b = pL->frag.listHead; b != NULL; b = b->next) {
736 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
737 sizeof(struct jffs2_raw_inode), pL->readbuf);
738 if ((inode == jNode->ino)) {
739 /* get actual file length from the newest node */
740 if (jNode->version >= latestVersion) {
741 totalSize = jNode->isize;
742 latestVersion = jNode->version;
743 }
744 }
745 put_fl_mem(jNode, pL->readbuf);
746 }
747 /*
748 * If no destination is provided, we are done.
749 * Just return the total size.
750 */
751 if (!dest)
752 return totalSize;
753#endif
754
755 for (b = pL->frag.listHead; b != NULL; b = b->next) {
756 /*
757 * Copy just the node and not the data at this point,
758 * since we don't yet know if we need this data.
759 */
760 jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
761 sizeof(struct jffs2_raw_inode),
762 pL->readbuf);
763 if(jNode == NULL)
764 {
765 return -1;
766 }
767
768 if (inode == jNode->ino) {
769#if 0
770 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
771 putLabeledWord("read_inode: inode = ", jNode->ino);
772 putLabeledWord("read_inode: version = ", jNode->version);
773 putLabeledWord("read_inode: isize = ", jNode->isize);
774 putLabeledWord("read_inode: offset = ", jNode->offset);
775 putLabeledWord("read_inode: csize = ", jNode->csize);
776 putLabeledWord("read_inode: dsize = ", jNode->dsize);
777 putLabeledWord("read_inode: compr = ", jNode->compr);
778 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
779 putLabeledWord("read_inode: flags = ", jNode->flags);
780#endif
781
782#ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
783 /* get actual file length from the newest node */
784 if (jNode->version >= latestVersion) {
785 totalSize = jNode->isize;
786 latestVersion = jNode->version;
787 }
788#endif
789
790 if(dest) {
791 /*
792 * Now that the inode has been checked,
793 * read the entire inode, including data.
794 */
795 put_fl_mem(jNode, pL->readbuf);
796 jNode = (struct jffs2_raw_inode *)
797 get_node_mem(b->offset, pL->readbuf);
798 if(jNode == NULL)
799 {
800 return -1;
801 }
802
803 src = ((uchar *)jNode) +
804 sizeof(struct jffs2_raw_inode);
805 /* ignore data behind latest known EOF */
806 if (jNode->offset > totalSize) {
807 put_fl_mem(jNode, pL->readbuf);
808 continue;
809 }
810 if (b->datacrc == CRC_UNKNOWN)
811 b->datacrc = data_crc(jNode) ?
812 CRC_OK : CRC_BAD;
813 if (b->datacrc == CRC_BAD) {
814 put_fl_mem(jNode, pL->readbuf);
815 continue;
816 }
817
818 lDest = (uchar *) (dest + jNode->offset);
819 switch (jNode->compr) {
820 case JFFS2_COMPR_NONE:
821 ldr_memcpy(lDest, src, jNode->dsize);
822 break;
823 case JFFS2_COMPR_ZERO:
824 for (i = 0; i < jNode->dsize; i++)
825 *(lDest++) = 0;
826 break;
827 case JFFS2_COMPR_RTIME:
828 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
829 break;
830 case JFFS2_COMPR_DYNRUBIN:
831 /* this is slow but it works */
832 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
833 break;
834 case JFFS2_COMPR_ZLIB:
835 zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
836 break;
837#if defined(CONFIG_JFFS2_LZO)
838 case JFFS2_COMPR_LZO:
839 lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
840 break;
841#endif
842
843 case JFFS2_COMPR_LZMA:
844 jffs2_lzma_decompress(src, lDest, jNode->csize, jNode->dsize);
845 break;
846
847
848 default:
849 /* unknown */
850 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
851 put_fl_mem(jNode, pL->readbuf);
852 return -1;
853 break;
854 }
855 }
856 }
857 counter++;
858 put_fl_mem(jNode, pL->readbuf);
859 }
860
861 return totalSize;
862}
863
864/* find the inode from the slashless name given a parent */
865static u32
866jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
867{
868 struct b_node *b;
869 struct jffs2_raw_dirent *jDir;
870 int len;
871 u32 counter;
872 u32 version = 0;
873 u32 inode = 0;
874
875 /* name is assumed slash free */
876 len = strlen(name);
877
878 counter = 0;
879 /* we need to search all and return the inode with the highest version */
880 for(b = pL->dir.listHead; b; b = b->next, counter++) {
881 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
882 pL->readbuf);
883 if(jDir == NULL)
884 {
885 return 0;
886 }
887 if ((pino == jDir->pino) && (len == jDir->nsize) &&
888 (!strncmp((char *)jDir->name, name, len))) { /* a match */
889 if (jDir->version < version) {
890 put_fl_mem(jDir, pL->readbuf);
891 continue;
892 }
893
894 if (jDir->version == version && inode != 0) {
895 /* I'm pretty sure this isn't legal */
896 putstr(" ** ERROR ** ");
897 putnstr(jDir->name, jDir->nsize);
898 putLabeledWord(" has dup version =", version);
899 }
900 inode = jDir->ino;
901 version = jDir->version;
902 }
903 put_fl_mem(jDir, pL->readbuf);
904 }
905 return inode;
906}
907
908char *mkmodestr(unsigned long mode, char *str)
909{
910 static const char *l = "xwr";
911 int mask = 1, i;
912 char c;
913
914 switch (mode & S_IFMT) {
915 case S_IFDIR: str[0] = 'd'; break;
916 case S_IFBLK: str[0] = 'b'; break;
917 case S_IFCHR: str[0] = 'c'; break;
918 case S_IFIFO: str[0] = 'f'; break;
919 case S_IFLNK: str[0] = 'l'; break;
920 case S_IFSOCK: str[0] = 's'; break;
921 case S_IFREG: str[0] = '-'; break;
922 default: str[0] = '?';
923 }
924
925 for(i = 0; i < 9; i++) {
926 c = l[i%3];
927 str[9-i] = (mode & mask)?c:'-';
928 mask = mask<<1;
929 }
930
931 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
932 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
933 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
934 str[10] = '\0';
935 return str;
936}
937
938static inline void dump_stat(struct stat *st, const char *name)
939{
940 char str[20];
941 char s[64], *p;
942
943 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
944 st->st_mtime = 1;
945
946 ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
947
948 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
949 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
950
951/*
952 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
953 st->st_size, s, name);
954*/
955
956 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
957}
958
959static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
960{
961 char fname[256];
962 struct stat st;
963
964 if(!d || !i) return -1;
965
966 strncpy(fname, (char *)d->name, sizeof(fname) - 1);
967 fname[sizeof(fname) - 1] = '\0';
968
969 memset(&st,0,sizeof(st));
970
971 st.st_mtime = i->mtime;
972 st.st_mode = i->mode;
973 st.st_ino = i->ino;
974 st.st_size = i->isize;
975
976 dump_stat(&st, fname);
977
978 if (d->type == DT_LNK) {
979 unsigned char *src = (unsigned char *) (&i[1]);
980 putstr(" -> ");
981 putnstr(src, (int)i->dsize);
982 }
983
984 putstr("\r\n");
985
986 return 0;
987}
988
989/* list inodes with the given pino */
990static u32
991jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
992{
993 struct b_node *b;
994 struct jffs2_raw_dirent *jDir;
995
996 for (b = pL->dir.listHead; b; b = b->next) {
997 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
998 pL->readbuf);
999 if(jDir == NULL)
1000 {
1001 return 0;
1002 }
1003 if (pino == jDir->pino) {
1004 u32 i_version = 0;
1005 struct jffs2_raw_inode *jNode, *i = NULL;
1006 struct b_node *b2;
1007
1008#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS) || defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS_DIR)
1009 /* Check for more recent versions of this file */
1010 int match;
1011 do {
1012 struct b_node *next = b->next;
1013 struct jffs2_raw_dirent *jDirNext;
1014 if (!next)
1015 break;
1016 jDirNext = (struct jffs2_raw_dirent *)
1017 get_node_mem(next->offset, NULL);
1018 if(jDirNext == NULL)
1019 {
1020 put_fl_mem(jDir, pL->readbuf);
1021 return 0;
1022 }
1023
1024 match = jDirNext->pino == jDir->pino &&
1025 jDirNext->nsize == jDir->nsize &&
1026 strncmp((char *)jDirNext->name,
1027 (char *)jDir->name,
1028 jDir->nsize) == 0;
1029 if (match) {
1030 /* Use next. It is more recent */
1031 b = next;
1032 /* Update buffer with the new info */
1033 *jDir = *jDirNext;
1034 }
1035 put_fl_mem(jDirNext, NULL);
1036 } while (match);
1037#endif
1038 if (jDir->ino == 0) {
1039 /* Deleted file */
1040 put_fl_mem(jDir, pL->readbuf);
1041 continue;
1042 }
1043
1044 for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
1045 jNode = (struct jffs2_raw_inode *)
1046 get_fl_mem(b2->offset, sizeof(*jNode), NULL);
1047 if(jNode == NULL)
1048 {
1049 put_fl_mem(jDir, pL->readbuf);
1050 put_fl_mem(i, NULL);
1051 return 0;
1052 }
1053
1054 if (jNode->ino == jDir->ino &&
1055 jNode->version >= i_version) {
1056 i_version = jNode->version;
1057 if (i)
1058 put_fl_mem(i, NULL);
1059
1060 if (jDir->type == DT_LNK)
1061 i = get_node_mem(b2->offset,
1062 NULL);
1063 else
1064 i = get_fl_mem(b2->offset,
1065 sizeof(*i),
1066 NULL);
1067 }
1068 put_fl_mem(jNode, NULL);
1069 }
1070
1071 dump_inode(pL, jDir, i);
1072 put_fl_mem(i, NULL);
1073 }
1074 put_fl_mem(jDir, pL->readbuf);
1075 }
1076 return pino;
1077}
1078
1079static u32
1080jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1081{
1082 int i;
1083 char tmp[256];
1084 char working_tmp[256];
1085 char *c;
1086
1087 /* discard any leading slash */
1088 i = 0;
1089 while (fname[i] == '/')
1090 i++;
1091 strncpy(tmp, &fname[i], sizeof(tmp) - 1);
1092 tmp[sizeof(tmp) - 1] = '\0';
1093
1094 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1095 {
1096 strncpy(working_tmp, tmp, c - tmp);
1097 working_tmp[c - tmp] = '\0';
1098
1099 for (i = 0; i < strlen(c) - 1; i++)
1100 tmp[i] = c[i + 1];
1101 tmp[i] = '\0';
1102
1103 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1104 putstr("find_inode failed for name=");
1105 putstr(working_tmp);
1106 putstr("\r\n");
1107 return 0;
1108 }
1109 }
1110 /* this is for the bare filename, directories have already been mapped */
1111 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1112 putstr("find_inode failed for name=");
1113 putstr(tmp);
1114 putstr("\r\n");
1115 return 0;
1116 }
1117 return pino;
1118
1119}
1120
1121static u32
1122jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1123{
1124 struct b_node *b;
1125 struct b_node *b2;
1126 struct jffs2_raw_dirent *jDir;
1127 struct jffs2_raw_inode *jNode;
1128 u8 jDirFoundType = 0;
1129 u32 jDirFoundIno = 0;
1130 u32 jDirFoundPino = 0;
1131 char tmp[256]= {0};
1132 u32 version = 0;
1133 u32 pino;
1134 unsigned char *src;
1135
1136 /* we need to search all and return the inode with the highest version */
1137 for(b = pL->dir.listHead; b; b = b->next) {
1138 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1139 pL->readbuf);
1140 if(jDir == NULL)
1141 {
1142 return 0;
1143 }
1144
1145 if (ino == jDir->ino) {
1146 if (jDir->version < version) {
1147 put_fl_mem(jDir, pL->readbuf);
1148 continue;
1149 }
1150
1151 if (jDir->version == version && jDirFoundType) {
1152 /* I'm pretty sure this isn't legal */
1153 putstr(" ** ERROR ** ");
1154 putnstr(jDir->name, jDir->nsize);
1155 putLabeledWord(" has dup version (resolve) = ",
1156 version);
1157 }
1158
1159 jDirFoundType = jDir->type;
1160 jDirFoundIno = jDir->ino;
1161 jDirFoundPino = jDir->pino;
1162 version = jDir->version;
1163 }
1164 put_fl_mem(jDir, pL->readbuf);
1165 }
1166 /* now we found the right entry again. (shoulda returned inode*) */
1167 if (jDirFoundType != DT_LNK)
1168 return jDirFoundIno;
1169
1170 /* it's a soft link so we follow it again. */
1171 b2 = pL->frag.listHead;
1172 while (b2) {
1173 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
1174 pL->readbuf);
1175 if(jNode == NULL)
1176 {
1177 return 0;
1178 }
1179 if (jNode->ino == jDirFoundIno) {
1180 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
1181 strncpy(tmp, (char *)src, sizeof(tmp) - 1);
1182 tmp[sizeof(tmp) - 1] = '\0';
1183 put_fl_mem(jNode, pL->readbuf);
1184 break;
1185 }
1186 b2 = b2->next;
1187 put_fl_mem(jNode, pL->readbuf);
1188 }
1189 /* ok so the name of the new file to find is in tmp */
1190 /* if it starts with a slash it is root based else shared dirs */
1191 if (tmp[0] == '/')
1192 pino = 1;
1193 else
1194 pino = jDirFoundPino;
1195
1196 return jffs2_1pass_search_inode(pL, tmp, pino);
1197}
1198
1199static u32
1200jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1201{
1202 int i;
1203 char tmp[256];
1204 char working_tmp[256];
1205 char *c;
1206
1207 /* discard any leading slash */
1208 i = 0;
1209 while (fname[i] == '/')
1210 i++;
1211
1212 strncpy(tmp, &fname[i], sizeof(tmp) - 1);
1213 tmp[sizeof(tmp) - 1] = '\0';
1214
1215 working_tmp[0] = '\0';
1216 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1217 {
1218 strncpy(working_tmp, tmp, c - tmp);
1219 working_tmp[c - tmp] = '\0';
1220 for (i = 0; i < strlen(c) - 1; i++)
1221 tmp[i] = c[i + 1];
1222 tmp[i] = '\0';
1223 /* only a failure if we arent looking at top level */
1224 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
1225 (working_tmp[0])) {
1226 putstr("find_inode failed for name=");
1227 putstr(working_tmp);
1228 putstr("\r\n");
1229 return 0;
1230 }
1231 }
1232
1233 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1234 putstr("find_inode failed for name=");
1235 putstr(tmp);
1236 putstr("\r\n");
1237 return 0;
1238 }
1239 /* this is for the bare filename, directories have already been mapped */
1240 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1241 putstr("find_inode failed for name=");
1242 putstr(tmp);
1243 putstr("\r\n");
1244 return 0;
1245 }
1246 return pino;
1247
1248}
1249
1250unsigned char
1251jffs2_1pass_rescan_needed(struct part_info *part)
1252{
1253 struct b_node *b;
1254 struct jffs2_unknown_node onode;
1255 struct jffs2_unknown_node *node;
1256 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
1257
1258 if (part->jffs2_priv == 0){
1259 DEBUGF ("rescan: First time in use\n");
1260 return 1;
1261 }
1262
1263 /* if we have no list, we need to rescan */
1264 if (pL->frag.listCount == 0) {
1265 DEBUGF ("rescan: fraglist zero\n");
1266 return 1;
1267 }
1268
1269 /* but suppose someone reflashed a partition at the same offset... */
1270 b = pL->dir.listHead;
1271 while (b) {
1272 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1273 sizeof(onode), &onode);
1274 if(node == NULL)
1275 {
1276 return 0;
1277 }
1278 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
1279 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1280 (unsigned long) b->offset);
1281 return 1;
1282 }
1283 b = b->next;
1284 }
1285 return 0;
1286}
1287
1288#ifdef CONFIG_JFFS2_SUMMARY
1289static u32 sum_get_unaligned32(u32 *ptr)
1290{
1291 u32 val;
1292 u8 *p = (u8 *)ptr;
1293
1294 val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
1295
1296 return __le32_to_cpu(val);
1297}
1298
1299static u16 sum_get_unaligned16(u16 *ptr)
1300{
1301 u16 val;
1302 u8 *p = (u8 *)ptr;
1303
1304 val = *p | (*(p + 1) << 8);
1305
1306 return __le16_to_cpu(val);
1307}
1308
1309#define dbg_summary(...) do {} while (0);
1310/*
1311 * Process the stored summary information - helper function for
1312 * jffs2_sum_scan_sumnode()
1313 */
1314
1315static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
1316 struct jffs2_raw_summary *summary,
1317 struct b_lists *pL)
1318{
1319 void *sp;
1320 int i, pass;
1321 void *ret;
1322
1323 for (pass = 0; pass < 2; pass++) {
1324 sp = summary->sum;
1325
1326 for (i = 0; i < summary->sum_num; i++) {
1327 struct jffs2_sum_unknown_flash *spu = sp;
1328 dbg_summary("processing summary index %d\n", i);
1329
1330 switch (sum_get_unaligned16(&spu->nodetype)) {
1331 case JFFS2_NODETYPE_INODE: {
1332 struct jffs2_sum_inode_flash *spi;
1333 if (pass) {
1334 spi = sp;
1335
1336 ret = insert_node(&pL->frag,
1337 (u32)part->offset +
1338 offset +
1339 sum_get_unaligned32(
1340 &spi->offset));
1341 if (ret == NULL)
1342 return -1;
1343 }
1344
1345 sp += JFFS2_SUMMARY_INODE_SIZE;
1346
1347 break;
1348 }
1349 case JFFS2_NODETYPE_DIRENT: {
1350 struct jffs2_sum_dirent_flash *spd;
1351 spd = sp;
1352 if (pass) {
1353 ret = insert_node(&pL->dir,
1354 (u32) part->offset +
1355 offset +
1356 sum_get_unaligned32(
1357 &spd->offset));
1358 if (ret == NULL)
1359 return -1;
1360 }
1361
1362 sp += JFFS2_SUMMARY_DIRENT_SIZE(
1363 spd->nsize);
1364
1365 break;
1366 }
1367 default : {
1368 uint16_t nodetype = sum_get_unaligned16(
1369 &spu->nodetype);
1370 printf("Unsupported node type %x found"
1371 " in summary!\n",
1372 nodetype);
1373 if ((nodetype & JFFS2_COMPAT_MASK) ==
1374 JFFS2_FEATURE_INCOMPAT)
1375 return -EIO;
1376 return -EBADMSG;
1377 }
1378 }
1379 }
1380 }
1381 return 0;
1382}
1383
1384/* Process the summary node - called from jffs2_scan_eraseblock() */
1385int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
1386 struct jffs2_raw_summary *summary, uint32_t sumsize,
1387 struct b_lists *pL)
1388{
1389 struct jffs2_unknown_node crcnode;
1390 int ret, __maybe_unused ofs;
1391 uint32_t crc;
1392
1393 ofs = part->sector_size - sumsize;
1394
1395 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
1396 offset, offset + ofs, sumsize);
1397
1398 /* OK, now check for node validity and CRC */
1399 crcnode.magic = JFFS2_MAGIC_BITMASK;
1400 crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
1401 crcnode.totlen = summary->totlen;
1402 crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
1403
1404 if (summary->hdr_crc != crc) {
1405 dbg_summary("Summary node header is corrupt (bad CRC or "
1406 "no summary at all)\n");
1407 goto crc_err;
1408 }
1409
1410 if (summary->totlen != sumsize) {
1411 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
1412 goto crc_err;
1413 }
1414
1415 crc = crc32_no_comp(0, (uchar *)summary,
1416 sizeof(struct jffs2_raw_summary)-8);
1417
1418 if (summary->node_crc != crc) {
1419 dbg_summary("Summary node is corrupt (bad CRC)\n");
1420 goto crc_err;
1421 }
1422
1423 crc = crc32_no_comp(0, (uchar *)summary->sum,
1424 sumsize - sizeof(struct jffs2_raw_summary));
1425
1426 if (summary->sum_crc != crc) {
1427 dbg_summary("Summary node data is corrupt (bad CRC)\n");
1428 goto crc_err;
1429 }
1430
1431 if (summary->cln_mkr)
1432 dbg_summary("Summary : CLEANMARKER node \n");
1433
1434 ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
1435 if (ret == -EBADMSG)
1436 return 0;
1437 if (ret)
1438 return ret; /* real error */
1439
1440 return 1;
1441
1442crc_err:
1443 putstr("Summary node crc error, skipping summary information.\n");
1444
1445 return 0;
1446}
1447#endif /* CONFIG_JFFS2_SUMMARY */
1448
1449#ifdef DEBUG_FRAGMENTS
1450static void
1451dump_fragments(struct b_lists *pL)
1452{
1453 struct b_node *b;
1454 struct jffs2_raw_inode ojNode;
1455 struct jffs2_raw_inode *jNode;
1456
1457 putstr("\r\n\r\n******The fragment Entries******\r\n");
1458 b = pL->frag.listHead;
1459 while (b) {
1460 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1461 sizeof(ojNode), &ojNode);
1462 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1463 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1464 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1465 putLabeledWord("\tbuild_list: version = ", jNode->version);
1466 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1467 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1468 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1469 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1470 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1471 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1472 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1473 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
1474 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
1475 b = b->next;
1476 }
1477}
1478#endif
1479
1480#ifdef DEBUG_DIRENTS
1481static void
1482dump_dirents(struct b_lists *pL)
1483{
1484 struct b_node *b;
1485 struct jffs2_raw_dirent *jDir;
1486
1487 putstr("\r\n\r\n******The directory Entries******\r\n");
1488 b = pL->dir.listHead;
1489 while (b) {
1490 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1491 pL->readbuf);
1492 putstr("\r\n");
1493 putnstr(jDir->name, jDir->nsize);
1494 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1495 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1496 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1497 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1498 putLabeledWord("\tbuild_list: version = ", jDir->version);
1499 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1500 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1501 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1502 putLabeledWord("\tbuild_list: type = ", jDir->type);
1503 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1504 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
1505 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
1506 b = b->next;
1507 put_fl_mem(jDir, pL->readbuf);
1508 }
1509}
1510#endif
1511
1512#define DEFAULT_EMPTY_SCAN_SIZE 256
1513
1514static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
1515{
1516 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
1517 return sector_size;
1518 else
1519 return DEFAULT_EMPTY_SCAN_SIZE;
1520}
1521
1522#if CONFIG_MUTUAL_DEBUG
1523void list_inode_print(struct b_list *list, int start, int count)
1524{
1525 u32 new_version;
1526 struct b_node *item;
1527 int i = 0;
1528
1529 item = list->listHead;
1530 while(item)
1531 {
1532 if(i < start)
1533 {
1534 item = item->next;
1535 i++;
1536 continue;
1537 }
1538 if(i - start >= count)
1539 break;
1540
1541 get_fl_mem(item->offset + offsetof(struct jffs2_raw_inode, version),
1542 sizeof(new_version), &new_version);
1543
1544 printf("inode %d %d\n", i, new_version);
1545
1546 item = item->next;
1547 i++;
1548
1549 }
1550}
1551#endif
1552
1553#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
1554unsigned int SDBMHash(char *str, int len)
1555{
1556 unsigned int hash = 0;
1557 int i;
1558 //while (*str)
1559 for(i=0; i<len; i++)
1560 {
1561 // equivalent to: hash = 65599*hash + (*str++);
1562 hash = (*str++) + (hash << 6) + (hash << 16) - hash;
1563 }
1564 return (hash & 0x7FFFFFFF);
1565}
1566
1567// RS Hash Function
1568unsigned int RSHash(char *str, int len)
1569{
1570 unsigned int b = 378551;
1571 unsigned int a = 63689;
1572 unsigned int hash = 0;
1573 int i;
1574
1575 //while (*str)
1576 for (i=0; i<len; i++)
1577 {
1578 hash = hash * a + (*str++);
1579 a *= b;
1580 }
1581
1582 return (hash & 0x7FFFFFFF);
1583}
1584
1585#define HASH_FUNC SDBMHash
1586#define LIST_HASH_INDEX_MASK 0xfff
1587#define LIST_HASH_INDEX_MAX (LIST_HASH_INDEX_MASK + 1)
1588//struct b_node * g_hash_table_array[LIST_HASH_INDEX_MAX];
1589static struct b_node **g_hash_table_array;
1590
1591static void get_inode_info2(struct b_node *pNode, unsigned int *ino, unsigned int *offset, unsigned int *version)
1592{
1593 unsigned int _ino, _offset, _version;
1594
1595 get_fl_mem(pNode->offset + offsetof(struct jffs2_raw_inode, ino),
1596 sizeof(_ino), &_ino);
1597 //get_fl_mem(pNode->offset + offsetof(struct jffs2_raw_inode, offset),
1598 // sizeof(_offset), &_offset);
1599 get_fl_mem(pNode->offset + offsetof(struct jffs2_raw_inode, version),
1600 sizeof(_version), &_version);
1601
1602 *ino = _ino;
1603 //*offset = _offset;
1604 *version = _version;
1605}
1606
1607static void get_inode_info(struct b_node *pNode, unsigned int *ino, unsigned int *offset, unsigned int *version)
1608{
1609 unsigned int _ino, _offset, _version;
1610
1611 get_fl_mem(pNode->offset + offsetof(struct jffs2_raw_inode, ino),
1612 sizeof(_ino), &_ino);
1613 get_fl_mem(pNode->offset + offsetof(struct jffs2_raw_inode, offset),
1614 sizeof(_offset), &_offset);
1615 get_fl_mem(pNode->offset + offsetof(struct jffs2_raw_inode, version),
1616 sizeof(_version), &_version);
1617
1618 *ino = _ino;
1619 *offset = _offset;
1620 *version = _version;
1621}
1622
1623static void list_sort3_init(struct b_list *pList)
1624{
1625 struct b_node *pNode;
1626 struct b_node *tmp_prev = NULL;
1627
1628 pNode = pList->listHead;
1629 while (pNode)
1630 {
1631 pNode->prev = tmp_prev;
1632 pNode->hash_next = NULL;
1633
1634 tmp_prev = pNode;
1635 pNode = pNode->next;
1636 }
1637}
1638
1639static void list_sort3_del(struct b_list *pList, struct b_node *pNode)
1640{
1641 if (pNode->prev != NULL)
1642 {
1643 pNode->prev->next = pNode->next;
1644 }
1645 else
1646 {
1647 pList->listHead = pNode->next;
1648 }
1649
1650 if (pNode->next != NULL)
1651 {
1652 pNode->next->prev = pNode->prev;
1653 }
1654 else
1655 {
1656 pList->listTail = pNode->prev;
1657 }
1658
1659 pList->listCount--;
1660}
1661
1662static void list_sort3_ListInsert(struct b_list *pList, struct b_node *pPrev, struct b_node *pNode)
1663{
1664 struct b_node *pNext = NULL;
1665
1666 if (pPrev != NULL)
1667 {
1668 pNext = pPrev->next; /* make prev node point fwd to new */
1669 pPrev->next = pNode;
1670 }
1671 else
1672 {
1673 pNext = pList->listHead; /* new node is to be first in list */
1674 pList->listHead = pNode;
1675 }
1676
1677 if (pNext != NULL)
1678 {
1679 pNext->prev = pNode; /* make next node point back to new */
1680 }
1681 else
1682 {
1683 pList->listTail = pNode; /* new node is to be last in list */
1684 }
1685
1686 /* set pointers in new node, and update node count */
1687 pNode->next = pNext;
1688 pNode->prev = pPrev;
1689
1690 pList->listCount++;
1691}
1692
1693static void list_sort3_add (struct b_list *pList, struct b_node *pNode)
1694{
1695 list_sort3_ListInsert(pList, pList->listTail, pNode);
1696}
1697
1698static void list_sort3(struct b_list *pList)
1699{
1700 struct b_node *pNode; /* list loop */
1701 struct b_node *pNode2; /* hash list loop*/
1702 struct b_node *node_tmp;
1703 int i = 0;
1704 unsigned int hash_input[2];
1705 unsigned int index;
1706 unsigned int ino, offset, version;
1707 unsigned int ino2, offset2, version2;
1708 int hash_list_add_flag;
1709 int hash_conflict_cnt = 0, hash_mov_cnt = 0;
1710
1711 if (g_hash_table_array == NULL)
1712 g_hash_table_array = malloc(LIST_HASH_INDEX_MAX * sizeof(struct b_node *));
1713 if (g_hash_table_array == NULL)
1714 {
1715 printf("error:g_hash_table_array malloc failed!\n");
1716 return;
1717 }
1718 memset(g_hash_table_array, 0, LIST_HASH_INDEX_MAX * sizeof(struct b_node *));
1719
1720 pNode = pList->listHead;
1721 while(pNode)
1722 {
1723 get_inode_info(pNode, &ino, &offset, &version);
1724 hash_input[0] = ino;
1725 hash_input[1] = offset;
1726 index = HASH_FUNC((char *)&hash_input, sizeof(hash_input)) & LIST_HASH_INDEX_MASK;
1727
1728 if (g_hash_table_array[index] == NULL)
1729 {
1730 g_hash_table_array[index] = pNode;
1731 pNode = pNode->next;
1732 continue;
1733 }
1734 else
1735 {
1736 hash_list_add_flag = 0;
1737 pNode2 = g_hash_table_array[index];
1738 node_tmp = pNode2;
1739 while (pNode2)
1740 {
1741 get_inode_info(pNode2, &ino2, &offset2, &version2);
1742 if ((ino != ino2) || (offset != offset2))
1743 {
1744 /* different file inode and hash conflict */
1745 hash_conflict_cnt++;
1746 }
1747
1748 /* pNode2 new version > pNode old version*/
1749 if (pList->listCompare(pNode2, pNode))
1750 {
1751 /* pNode older and add */
1752 pNode->hash_next = pNode2;
1753 if (pNode2 == g_hash_table_array[index])
1754 {
1755 g_hash_table_array[index] = pNode;
1756 }
1757 else
1758 {
1759 node_tmp->hash_next = pNode;
1760 }
1761 hash_list_add_flag = 1;
1762 break;
1763 }
1764 node_tmp = pNode2;
1765 pNode2 = pNode2->hash_next;
1766 }
1767 if (hash_list_add_flag == 0)
1768 {
1769 /* add to tail */
1770 //printf("hash list add tail pNode ino:%d offset:%x version:%d\n", ino, offset, version);
1771 node_tmp->hash_next = pNode;
1772 }
1773 }
1774 pNode = pNode->next;
1775 }
1776
1777 for (i = 0; i < LIST_HASH_INDEX_MAX; i++)
1778 {
1779 pNode = g_hash_table_array[i];
1780 if (pNode && pNode->hash_next)
1781 {
1782 pNode2 = g_hash_table_array[i];
1783 while (pNode2)
1784 {
1785 list_sort3_del(pList, pNode2);
1786 hash_mov_cnt++;
1787 //get_inode_info(pNode2, &ino, &offset, &version);
1788 //printf("sort3 del ino:%d offset:%x version:%d\n", ino, offset, version);
1789 pNode2 = pNode2->hash_next;
1790 }
1791 pNode2 = g_hash_table_array[i];
1792 while (pNode2)
1793 {
1794 list_sort3_add(pList, pNode2);
1795 //get_inode_info(pNode2, &ino, &offset, &version);
1796 //printf("sort3 add ino:%d offset:%x version:%d\n", ino, offset, version);
1797 pNode2 = pNode2->hash_next;
1798 }
1799 }
1800 }
1801
1802 printf("list_sort3: hash conflict:%d mov_cnt:%d\n", hash_conflict_cnt, hash_mov_cnt);
1803
1804 free(g_hash_table_array);
1805 g_hash_table_array = NULL;
1806}
1807
1808
1809//存放pNode节点
1810typedef struct iv_node {
1811 unsigned long long inover; //高位ino,低位version
1812 struct b_node *pNode; //存放pNode地址
1813 struct iv_node *next; //后指针
1814 struct iv_node *previous; //前指针
1815}IV_NODE;
1816
1817typedef struct iv_list {
1818 IV_NODE *listTail;
1819 IV_NODE *listHead;
1820 unsigned int listCount;
1821}IV_LIST;
1822
1823/**************************************************************************
1824* 函数名称: list_sort5_init
1825* 功能描述: 初始化双向链表
1826* 参数说明:
1827 (IN)
1828 pList:链表指针
1829 (OUT)
1830* 返 回 值:
1831**************************************************************************/
1832void list_sort5_init(IV_LIST *pList)
1833{
1834 //zOss_AssertExN(pList != NULL);
1835
1836 pList->listHead = NULL;
1837 pList->listTail = NULL;
1838 pList->listCount = 0;
1839}
1840
1841
1842/**************************************************************************
1843* 函数名称: list_sort5_insert
1844* 功能描述: 向链表指定的节点后插入节点
1845* 参数说明:
1846 (IN)
1847 pList:链表指针
1848 pPrev:插入点节点指针,当pPrev为空时,表示插入到链表首位置
1849 pNode:待插入的节点指针
1850 (OUT)
1851* 返 回 值:
1852**************************************************************************/
1853void list_sort5_insert (IV_LIST *pList, IV_NODE *pPrev, IV_NODE *pNode)
1854{
1855 IV_NODE *pNext = NULL;
1856
1857 // zOss_AssertExN(pList != NULL);
1858 // zOss_AssertExN(pNode != NULL);
1859
1860 if (pPrev != NULL)
1861 {
1862 pNext = pPrev->next; /* make prev node point fwd to new */
1863 pPrev->next = pNode;
1864 }
1865 else
1866 {
1867 pNext = pList->listHead; /* new node is to be first in list */
1868 pList->listHead = pNode;
1869 }
1870
1871 if (pNext != NULL)
1872 {
1873 pNext->previous = pNode; /* make next node point back to new */
1874 }
1875 else
1876 {
1877 pList->listTail = pNode; /* new node is to be last in list */
1878 }
1879
1880 /* set pointers in new node, and update node count */
1881 pNode->next = pNext;
1882 pNode->previous = pPrev;
1883
1884 pList->listCount++;
1885}
1886
1887
1888static int sort_list5(struct b_list *pList)
1889{
1890 IV_NODE *p_buffer; //
1891 IV_NODE *p_toInNode; //待插入IV_NODE节点
1892 IV_NODE *p_indexNode; //插入位置IV_NODE节点
1893 int buffer_len;
1894 struct b_node *pNode; /* list loop */
1895 struct b_node *pNode2; /* list loop */
1896
1897 int i, j, count;
1898 unsigned int ino, offset, version;
1899 unsigned long long inover = 0;
1900 unsigned long long iontemp = 0;
1901 unsigned int totalcompare = 0;
1902 unsigned int totaldetal = 0;
1903
1904 IV_LIST vList = {0};
1905
1906 if (!pList->listHead)
1907 return -1;
1908
1909
1910 buffer_len = pList->listCount * sizeof(IV_NODE);
1911 p_buffer = malloc(buffer_len);
1912 if (p_buffer == NULL) {
1913 putstr("sort_list5: malloc failed\n");
1914 return -1;
1915 }
1916 memset(p_buffer, 0, buffer_len);
1917
1918 list_sort5_init(&vList);
1919 pNode = pList->listHead;
1920 p_toInNode = p_buffer;
1921 p_indexNode = NULL;
1922
1923 printf("jffs2_1pass sort_list5 start\n");
1924 while(pNode)
1925 {
1926 get_inode_info2(pNode, &ino, &offset, &version);
1927 iontemp = ino;
1928 inover = (iontemp << 32) + version;
1929 p_toInNode->pNode = pNode;
1930 p_toInNode->inover = inover;
1931
1932 //p_Nindex = vList.listTail;
1933
1934
1935 while (1)
1936 {
1937 totalcompare++;
1938
1939 if(p_indexNode == NULL)
1940 {
1941 list_sort5_insert(&vList, p_indexNode, p_toInNode);
1942 p_indexNode = p_toInNode;
1943 break;
1944 }
1945
1946 //printf("guowei------------------------%d p_toInNode->inover: %llu, p_indexNode->inover: %llu\n", __LINE__, p_toInNode->inover, p_indexNode->inover);
1947 if(p_toInNode->inover >= p_indexNode->inover)
1948 {
1949 if ((p_indexNode->next == NULL) || (p_toInNode->inover <= p_indexNode->next->inover))
1950 {
1951 list_sort5_insert(&vList, p_indexNode, p_toInNode);
1952 p_indexNode = p_toInNode;
1953 break;
1954 }
1955 else
1956 {
1957 p_indexNode = p_indexNode->next;
1958 }
1959 }
1960 else
1961 {
1962 p_indexNode = p_indexNode->previous;
1963 }
1964
1965 }
1966
1967 pNode = pNode->next;
1968 p_toInNode++;
1969 totaldetal++;
1970
1971 }
1972
1973 printf("jffs2_1pass pList->listCount: %d totalcompare: %d\n", pList->listCount, totalcompare);
1974
1975 pNode = vList.listHead->pNode;
1976 pList->listHead = pNode;
1977 p_toInNode = vList.listHead->next;
1978
1979 while (p_toInNode)
1980 {
1981 pNode->next = p_toInNode->pNode;
1982 //get_inode_info(pNode, &ino, &offset, &version);
1983 //printf("guowei2-----------------------%d ino: %x version: %x\n", __LINE__, ino, version);
1984 pNode = pNode->next;
1985 p_toInNode = p_toInNode->next;
1986 }
1987
1988 pList->listTail = pNode;
1989 pList->listTail->next = NULL;
1990
1991 free(p_buffer);
1992 //printf("jffs2_1pass sort_list5 finish\n");
1993 return 0;
1994}
1995
1996#endif /* CONFIG_SYS_JFFS2_SORT_FRAGMENTS */
1997
1998static u32
1999jffs2_1pass_build_lists(struct part_info * part)
2000{
2001 struct b_lists *pL;
2002 struct jffs2_unknown_node *node;
2003 u32 nr_sectors;
2004 u32 i;
2005 u32 counter4 = 0;
2006 u32 counterF = 0;
2007 u32 counterN = 0;
2008 u32 max_totlen = 0;
2009 u32 buf_size;
2010 char *buf = NULL;
2011
2012 nr_sectors = lldiv(part->size, part->sector_size);
2013 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
2014 /* jffs2 list building enterprise nope. in newer versions the overhead is */
2015 /* only about 5 %. not enough to inconvenience people for. */
2016 /* lcd_off(); */
2017
2018 /* if we are building a list we need to refresh the cache. */
2019 jffs_init_1pass_list(part);
2020 pL = (struct b_lists *)part->jffs2_priv;
2021 buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
2022 if(buf == NULL){
2023 return 0;
2024 }
2025 puts ("Scanning JFFS2 FS: ");
2026
2027 /* start at the beginning of the partition */
2028 for (i = 0; i < nr_sectors; i++) {
2029 uint32_t sector_ofs = i * part->sector_size;
2030 uint32_t buf_ofs = sector_ofs;
2031 uint32_t buf_len;
2032 uint32_t ofs, prevofs;
2033#ifdef CONFIG_JFFS2_SUMMARY
2034 struct jffs2_sum_marker *sm;
2035 void *sumptr = NULL;
2036 uint32_t sumlen;
2037 int ret;
2038#endif
2039 /* Indicates a sector with a CLEANMARKER was found */
2040 int clean_sector = 0;
2041
2042 /* Set buf_size to maximum length */
2043 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
2044 //WATCHDOG_RESET();
2045
2046#ifdef CONFIG_JFFS2_SUMMARY
2047 buf_len = sizeof(*sm);
2048
2049 /* Read as much as we want into the _end_ of the preallocated
2050 * buffer
2051 */
2052 get_fl_mem(part->offset + sector_ofs + part->sector_size -
2053 buf_len, buf_len, buf + buf_size - buf_len);
2054
2055 sm = (void *)buf + buf_size - sizeof(*sm);
2056 if (sm->magic == JFFS2_SUM_MAGIC) {
2057 sumlen = part->sector_size - sm->offset;
2058 sumptr = buf + buf_size - sumlen;
2059
2060 /* Now, make sure the summary itself is available */
2061 if (sumlen > buf_size) {
2062 /* Need to kmalloc for this. */
2063 sumptr = malloc(sumlen);
2064 if (!sumptr) {
2065 putstr("Can't get memory for summary "
2066 "node!\n");
2067 free(buf);
2068 jffs2_free_cache(part);
2069 return 0;
2070 }
2071 memcpy(sumptr + sumlen - buf_len, buf +
2072 buf_size - buf_len, buf_len);
2073 }
2074 if (buf_len < sumlen) {
2075 /* Need to read more so that the entire summary
2076 * node is present
2077 */
2078 get_fl_mem(part->offset + sector_ofs +
2079 part->sector_size - sumlen,
2080 sumlen - buf_len, sumptr);
2081 }
2082 }
2083
2084 if (sumptr) {
2085 ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
2086 sumlen, pL);
2087
2088 if (buf_size && sumlen > buf_size)
2089 free(sumptr);
2090 if (ret < 0) {
2091 free(buf);
2092 jffs2_free_cache(part);
2093 return 0;
2094 }
2095 if (ret)
2096 continue;
2097
2098 }
2099#endif /* CONFIG_JFFS2_SUMMARY */
2100
2101 buf_len = EMPTY_SCAN_SIZE(part->sector_size);
2102
2103 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
2104
2105 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
2106 ofs = 0;
2107
2108 /* Scan only 4KiB of 0xFF before declaring it's empty */
2109 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
2110 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
2111 ofs += 4;
2112
2113 if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
2114 continue;
2115
2116 ofs += sector_ofs;
2117 prevofs = ofs - 1;
2118 /*
2119 * Set buf_size down to the minimum size required.
2120 * This prevents reading in chunks of flash data unnecessarily.
2121 */
2122 buf_size = sizeof(union jffs2_node_union);
2123
2124 scan_more:
2125 while (ofs < sector_ofs + part->sector_size) {
2126 if (ofs == prevofs) {
2127 printf("offset %08x already seen, skip\n", ofs);
2128 ofs += 4;
2129 counter4++;
2130 continue;
2131 }
2132 prevofs = ofs;
2133 if (sector_ofs + part->sector_size <
2134 ofs + sizeof(*node))
2135 break;
2136 if (buf_ofs + buf_len < ofs + sizeof(*node)) {
2137 buf_len = min_t(uint32_t, buf_size, sector_ofs
2138 + part->sector_size - ofs);
2139 get_fl_mem((u32)part->offset + ofs, buf_len,
2140 buf);
2141 buf_ofs = ofs;
2142 }
2143
2144 node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
2145
2146 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
2147 uint32_t inbuf_ofs;
2148 uint32_t scan_end;
2149
2150 ofs += 4;
2151 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
2152 part->sector_size)/8,
2153 buf_len);
2154 more_empty:
2155 inbuf_ofs = ofs - buf_ofs;
2156 while (inbuf_ofs < scan_end) {
2157 if (*(uint32_t *)(&buf[inbuf_ofs]) !=
2158 0xffffffff)
2159 goto scan_more;
2160
2161 inbuf_ofs += 4;
2162 ofs += 4;
2163 }
2164 /* Ran off end. */
2165 /*
2166 * If this sector had a clean marker at the
2167 * beginning, and immediately following this
2168 * have been a bunch of FF bytes, treat the
2169 * entire sector as empty.
2170 */
2171 if (clean_sector)
2172 break;
2173
2174 /* See how much more there is to read in this
2175 * eraseblock...
2176 */
2177 buf_len = min_t(uint32_t, buf_size,
2178 sector_ofs +
2179 part->sector_size - ofs);
2180 if (!buf_len) {
2181 /* No more to read. Break out of main
2182 * loop without marking this range of
2183 * empty space as dirty (because it's
2184 * not)
2185 */
2186 break;
2187 }
2188 scan_end = buf_len;
2189 get_fl_mem((u32)part->offset + ofs, buf_len,
2190 buf);
2191 buf_ofs = ofs;
2192 goto more_empty;
2193 }
2194 /*
2195 * Found something not erased in the sector, so reset
2196 * the 'clean_sector' flag.
2197 */
2198 clean_sector = 0;
2199 if (node->magic != JFFS2_MAGIC_BITMASK ||
2200 !hdr_crc(node)) {
2201 ofs += 4;
2202 counter4++;
2203 continue;
2204 }
2205 if (ofs + node->totlen >
2206 sector_ofs + part->sector_size) {
2207 ofs += 4;
2208 counter4++;
2209 continue;
2210 }
2211 /* if its a fragment add it */
2212 switch (node->nodetype) {
2213 case JFFS2_NODETYPE_INODE:
2214 if (buf_ofs + buf_len < ofs + sizeof(struct
2215 jffs2_raw_inode)) {
2216 buf_len = min_t(uint32_t,
2217 sizeof(struct jffs2_raw_inode),
2218 sector_ofs +
2219 part->sector_size -
2220 ofs);
2221 get_fl_mem((u32)part->offset + ofs,
2222 buf_len, buf);
2223 buf_ofs = ofs;
2224 node = (void *)buf;
2225 }
2226 if (!inode_crc((struct jffs2_raw_inode *)node))
2227 break;
2228
2229 if (insert_node(&pL->frag, (u32) part->offset +
2230 ofs) == NULL) {
2231 free(buf);
2232 jffs2_free_cache(part);
2233 return 0;
2234 }
2235 if (max_totlen < node->totlen)
2236 max_totlen = node->totlen;
2237 break;
2238 case JFFS2_NODETYPE_DIRENT:
2239 if (buf_ofs + buf_len < ofs + sizeof(struct
2240 jffs2_raw_dirent) +
2241 ((struct
2242 jffs2_raw_dirent *)
2243 node)->nsize) {
2244 buf_len = min_t(uint32_t,
2245 node->totlen,
2246 sector_ofs +
2247 part->sector_size -
2248 ofs);
2249 get_fl_mem((u32)part->offset + ofs,
2250 buf_len, buf);
2251 buf_ofs = ofs;
2252 node = (void *)buf;
2253 }
2254
2255 if (!dirent_crc((struct jffs2_raw_dirent *)
2256 node) ||
2257 !dirent_name_crc(
2258 (struct
2259 jffs2_raw_dirent *)
2260 node))
2261 break;
2262 if (! (counterN%100))
2263 puts ("\b\b. ");
2264 if (insert_node(&pL->dir, (u32) part->offset +
2265 ofs) == NULL) {
2266 free(buf);
2267 jffs2_free_cache(part);
2268 return 0;
2269 }
2270 if (max_totlen < node->totlen)
2271 max_totlen = node->totlen;
2272 counterN++;
2273 break;
2274 case JFFS2_NODETYPE_CLEANMARKER:
2275 if (node->totlen != sizeof(struct jffs2_unknown_node))
2276 printf("OOPS Cleanmarker has bad size "
2277 "%d != %zu\n",
2278 node->totlen,
2279 sizeof(struct jffs2_unknown_node));
2280 if ((node->totlen ==
2281 sizeof(struct jffs2_unknown_node)) &&
2282 (ofs == sector_ofs)) {
2283 /*
2284 * Found a CLEANMARKER at the beginning
2285 * of the sector. It's in the correct
2286 * place with correct size and CRC.
2287 */
2288 clean_sector = 1;
2289 }
2290 break;
2291 case JFFS2_NODETYPE_PADDING:
2292 if (node->totlen < sizeof(struct jffs2_unknown_node))
2293 printf("OOPS Padding has bad size "
2294 "%d < %zu\n",
2295 node->totlen,
2296 sizeof(struct jffs2_unknown_node));
2297 break;
2298 case JFFS2_NODETYPE_SUMMARY:
2299 break;
2300 default:
2301 printf("Unknown node type: %x len %d offset 0x%x\n",
2302 node->nodetype,
2303 node->totlen, ofs);
2304 }
2305 ofs += ((node->totlen + 3) & ~3);
2306 counterF++;
2307 }
2308 }
2309
2310 free(buf);
2311#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
2312 /*
2313 * Sort the lists.
2314 */
2315#if CONFIG_MUTUAL_DEBUG
2316 printf("frag list count %d \n",(&pL->frag)->listCount);
2317 printf("dir list count %d \n",(&pL->dir)->listCount);
2318
2319 g_compare_inode_count = 0;
2320 (&pL->frag)->listCompare = compare_inodes;
2321 sort_list(&pL->frag);
2322 printf("sort_list frag inode_compare_count %d\n", g_compare_inode_count);
2323 list_inode_print(&pL->frag, 2000, 15);
2324
2325 g_compare_inode_count = 0;
2326 (&pL->frag)->listCompare = compare_inodes2;
2327 sort_list2(&pL->frag);
2328 printf("sort_list2 frag inode_compare_count %d\n", g_compare_inode_count);
2329 list_inode_print(&pL->frag, 2000, 15);
2330#else
2331 //sort_list2(&pL->frag);
2332 //list_sort3_init(&pL->frag);
2333 //list_sort3(&pL->frag);
2334 sort_list5(&pL->frag);
2335
2336#endif
2337 //sort_list(&pL->dir);
2338#endif
2339
2340#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS_DIR
2341#if CONFIG_MUTUAL_DEBUG
2342 printf("dir list count %d \n",(&pL->dir)->listCount);
2343#endif
2344 sort_list(&pL->dir);
2345#endif
2346
2347 putstr("\b\b done.\r\n"); /* close off the dots */
2348
2349 /* We don't care if malloc failed - then each read operation will
2350 * allocate its own buffer as necessary (NAND) or will read directly
2351 * from flash (NOR).
2352 */
2353 pL->readbuf = malloc(max_totlen);
2354
2355 /* turn the lcd back on. */
2356 /* splash(); */
2357#ifdef DEBUG_DIRENTS
2358 dump_dirents(pL);
2359#endif
2360
2361#ifdef DEBUG_FRAGMENTS
2362 dump_fragments(pL);
2363#endif
2364
2365 /* give visual feedback that we are done scanning the flash */
2366 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
2367 return 1;
2368}
2369
2370
2371static u32
2372jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
2373{
2374 struct b_node *b;
2375 struct jffs2_raw_inode ojNode;
2376 struct jffs2_raw_inode *jNode;
2377 int i;
2378
2379 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
2380 piL->compr_info[i].num_frags = 0;
2381 piL->compr_info[i].compr_sum = 0;
2382 piL->compr_info[i].decompr_sum = 0;
2383 }
2384
2385 b = pL->frag.listHead;
2386 while (b) {
2387 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
2388 sizeof(ojNode), &ojNode);
2389 if(jNode == NULL)
2390 {
2391 printf("jffs2_1pass_fill_info get_fl_mem fail.\n");
2392 return 1;
2393 }
2394
2395 if (jNode->compr < JFFS2_NUM_COMPR) {
2396 piL->compr_info[jNode->compr].num_frags++;
2397 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
2398 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
2399 }
2400 b = b->next;
2401 }
2402 return 0;
2403}
2404
2405
2406static struct b_lists *
2407jffs2_get_list(struct part_info * part, const char *who)
2408{
2409 /* copy requested part_info struct pointer to global location */
2410 current_part = part;
2411
2412 if (jffs2_1pass_rescan_needed(part)) {
2413 if (!jffs2_1pass_build_lists(part)) {
2414 printf("%s: Failed to scan JFFSv2 file structure\n", who);
2415 return NULL;
2416 }
2417 }
2418 return (struct b_lists *)part->jffs2_priv;
2419}
2420
2421
2422/* Print directory / file contents */
2423u32
2424jffs2_1pass_ls(struct part_info * part, const char *fname)
2425{
2426 struct b_lists *pl;
2427 long ret = 1;
2428 u32 inode;
2429
2430 if (! (pl = jffs2_get_list(part, "ls")))
2431 return 0;
2432
2433 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
2434 putstr("ls: Failed to scan jffs2 file structure\r\n");
2435 return 0;
2436 }
2437
2438 return ret;
2439}
2440
2441
2442/* Load a file from flash into memory. fname can be a full path */
2443u32
2444jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
2445{
2446
2447 struct b_lists *pl;
2448 long ret = 1;
2449 u32 inode;
2450
2451 if (! (pl = jffs2_get_list(part, "load")))
2452 return 0;
2453
2454 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
2455 putstr("load: Failed to find inode\r\n");
2456 return 0;
2457 }
2458
2459 /* Resolve symlinks */
2460 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
2461 putstr("load: Failed to resolve inode structure\r\n");
2462 return 0;
2463 }
2464
2465 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
2466 putstr("load: Failed to read inode\r\n");
2467 return 0;
2468 }
2469
2470 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
2471 (unsigned long) dest, ret);
2472 return ret;
2473}
2474
2475/* Return information about the fs on this partition */
2476u32
2477jffs2_1pass_info(struct part_info * part)
2478{
2479 struct b_jffs2_info info;
2480 struct b_lists *pl;
2481 int i;
2482
2483 if (! (pl = jffs2_get_list(part, "info")))
2484 return 0;
2485
2486 jffs2_1pass_fill_info(pl, &info);
2487 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
2488 printf ("Compression: %s\n"
2489 "\tfrag count: %d\n"
2490 "\tcompressed sum: %d\n"
2491 "\tuncompressed sum: %d\n",
2492 compr_names[i],
2493 info.compr_info[i].num_frags,
2494 info.compr_info[i].compr_sum,
2495 info.compr_info[i].decompr_sum);
2496 }
2497 return 1;
2498}