xref: /rk3399_rockchip-uboot/fs/jffs2/jffs2_1pass.c (revision 225cf4cdf9ffd73978f4a266d3ecc6c88b048a74)
1fe8c2806Swdenk /*
2fe8c2806Swdenk -------------------------------------------------------------------------
3fe8c2806Swdenk  * Filename:      jffs2.c
4fe8c2806Swdenk  * Version:       $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
5fe8c2806Swdenk  * Copyright:     Copyright (C) 2001, Russ Dill
6fe8c2806Swdenk  * Author:        Russ Dill <Russ.Dill@asu.edu>
7fe8c2806Swdenk  * Description:   Module to load kernel from jffs2
8fe8c2806Swdenk  *-----------------------------------------------------------------------*/
9fe8c2806Swdenk /*
10fe8c2806Swdenk  * some portions of this code are taken from jffs2, and as such, the
11fe8c2806Swdenk  * following copyright notice is included.
12fe8c2806Swdenk  *
13fe8c2806Swdenk  * JFFS2 -- Journalling Flash File System, Version 2.
14fe8c2806Swdenk  *
15fe8c2806Swdenk  * Copyright (C) 2001 Red Hat, Inc.
16fe8c2806Swdenk  *
17fe8c2806Swdenk  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
18fe8c2806Swdenk  *
19fe8c2806Swdenk  * The original JFFS, from which the design for JFFS2 was derived,
20fe8c2806Swdenk  * was designed and implemented by Axis Communications AB.
21fe8c2806Swdenk  *
22fe8c2806Swdenk  * The contents of this file are subject to the Red Hat eCos Public
23fe8c2806Swdenk  * License Version 1.1 (the "Licence"); you may not use this file
24fe8c2806Swdenk  * except in compliance with the Licence.  You may obtain a copy of
25fe8c2806Swdenk  * the Licence at http://www.redhat.com/
26fe8c2806Swdenk  *
27fe8c2806Swdenk  * Software distributed under the Licence is distributed on an "AS IS"
28fe8c2806Swdenk  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
29fe8c2806Swdenk  * See the Licence for the specific language governing rights and
30fe8c2806Swdenk  * limitations under the Licence.
31fe8c2806Swdenk  *
32fe8c2806Swdenk  * The Original Code is JFFS2 - Journalling Flash File System, version 2
33fe8c2806Swdenk  *
34fe8c2806Swdenk  * Alternatively, the contents of this file may be used under the
35fe8c2806Swdenk  * terms of the GNU General Public License version 2 (the "GPL"), in
36fe8c2806Swdenk  * which case the provisions of the GPL are applicable instead of the
37fe8c2806Swdenk  * above.  If you wish to allow the use of your version of this file
38fe8c2806Swdenk  * only under the terms of the GPL and not to allow others to use your
39fe8c2806Swdenk  * version of this file under the RHEPL, indicate your decision by
40fe8c2806Swdenk  * deleting the provisions above and replace them with the notice and
41fe8c2806Swdenk  * other provisions required by the GPL.  If you do not delete the
42fe8c2806Swdenk  * provisions above, a recipient may use your version of this file
43fe8c2806Swdenk  * under either the RHEPL or the GPL.
44fe8c2806Swdenk  *
45fe8c2806Swdenk  * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
46fe8c2806Swdenk  *
47fe8c2806Swdenk  */
48fe8c2806Swdenk 
49fe8c2806Swdenk /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
50fe8c2806Swdenk  * bag to throw up into before reading this code. I looked through the jffs2
51fe8c2806Swdenk  * code, the caching scheme is very elegant. I tried to keep the version
52fe8c2806Swdenk  * for a bootloader as small and simple as possible. Instead of worring about
53fe8c2806Swdenk  * unneccesary data copies, node scans, etc, I just optimized for the known
54fe8c2806Swdenk  * common case, a kernel, which looks like:
55fe8c2806Swdenk  *	(1) most pages are 4096 bytes
56fe8c2806Swdenk  *	(2) version numbers are somewhat sorted in acsending order
57fe8c2806Swdenk  *	(3) multiple compressed blocks making up one page is uncommon
58fe8c2806Swdenk  *
59fe8c2806Swdenk  * So I create a linked list of decending version numbers (insertions at the
60fe8c2806Swdenk  * head), and then for each page, walk down the list, until a matching page
61fe8c2806Swdenk  * with 4096 bytes is found, and then decompress the watching pages in
62fe8c2806Swdenk  * reverse order.
63fe8c2806Swdenk  *
64fe8c2806Swdenk  */
65fe8c2806Swdenk 
66fe8c2806Swdenk /*
67fe8c2806Swdenk  * Adapted by Nye Liu <nyet@zumanetworks.com> and
68fe8c2806Swdenk  * Rex Feany <rfeany@zumanetworks.com>
69fe8c2806Swdenk  * on Jan/2002 for U-Boot.
70fe8c2806Swdenk  *
71fe8c2806Swdenk  * Clipped out all the non-1pass functions, cleaned up warnings,
72fe8c2806Swdenk  * wrappers, etc. No major changes to the code.
73fe8c2806Swdenk  * Please, he really means it when he said have a paper bag
74fe8c2806Swdenk  * handy. We needed it ;).
75fe8c2806Swdenk  *
76fe8c2806Swdenk  */
77fe8c2806Swdenk 
7806d01dbeSwdenk /*
7906d01dbeSwdenk  * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
8006d01dbeSwdenk  *
8106d01dbeSwdenk  * - overhaul of the memory management. Removed much of the "paper-bagging"
8206d01dbeSwdenk  *   in that part of the code, fixed several bugs, now frees memory when
8306d01dbeSwdenk  *   partition is changed.
8406d01dbeSwdenk  *   It's still ugly :-(
8506d01dbeSwdenk  * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
8606d01dbeSwdenk  *   was incorrect. Removed a bit of the paper-bagging as well.
8706d01dbeSwdenk  * - removed double crc calculation for fragment headers in jffs2_private.h
8806d01dbeSwdenk  *   for speedup.
8906d01dbeSwdenk  * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
9006d01dbeSwdenk  * - spinning wheel now spins depending on how much memory has been scanned
9106d01dbeSwdenk  * - lots of small changes all over the place to "improve" readability.
9206d01dbeSwdenk  * - implemented fragment sorting to ensure that the newest data is copied
9306d01dbeSwdenk  *   if there are multiple copies of fragments for a certain file offset.
9406d01dbeSwdenk  *
956d0f6bcfSJean-Christophe PLAGNIOL-VILLARD  * The fragment sorting feature must be enabled by CONFIG_SYS_JFFS2_SORT_FRAGMENTS.
9606d01dbeSwdenk  * Sorting is done while adding fragments to the lists, which is more or less a
9706d01dbeSwdenk  * bubble sort. This takes a lot of time, and is most probably not an issue if
9806d01dbeSwdenk  * the boot filesystem is always mounted readonly.
9906d01dbeSwdenk  *
10006d01dbeSwdenk  * You should define it if the boot filesystem is mounted writable, and updates
10106d01dbeSwdenk  * to the boot files are done by copying files to that filesystem.
10206d01dbeSwdenk  *
10306d01dbeSwdenk  *
10406d01dbeSwdenk  * There's a big issue left: endianess is completely ignored in this code. Duh!
10506d01dbeSwdenk  *
10606d01dbeSwdenk  *
10706d01dbeSwdenk  * You still should have paper bags at hand :-(. The code lacks more or less
10806d01dbeSwdenk  * any comment, and is still arcane and difficult to read in places. As this
109dd875c76Swdenk  * might be incompatible with any new code from the jffs2 maintainers anyway,
110dd875c76Swdenk  * it should probably be dumped and replaced by something like jffs2reader!
11106d01dbeSwdenk  */
11206d01dbeSwdenk 
11306d01dbeSwdenk 
114fe8c2806Swdenk #include <common.h>
115fe8c2806Swdenk #include <config.h>
116fe8c2806Swdenk #include <malloc.h>
11718e86724STom Rini #include <div64.h>
118fe8c2806Swdenk #include <linux/stat.h>
119fe8c2806Swdenk #include <linux/time.h>
12086d3273eSStuart Wood #include <watchdog.h>
121fe8c2806Swdenk #include <jffs2/jffs2.h>
122fe8c2806Swdenk #include <jffs2/jffs2_1pass.h>
1237b15e2bbSMike Frysinger #include <linux/compat.h>
1248cf19b9fSIlya Yanok #include <asm/errno.h>
125fe8c2806Swdenk 
126fe8c2806Swdenk #include "jffs2_private.h"
127fe8c2806Swdenk 
12806d01dbeSwdenk 
12906d01dbeSwdenk #define	NODE_CHUNK	1024	/* size of memory allocation chunk in b_nodes */
130fc1cfcdbSwdenk #define	SPIN_BLKSIZE	18	/* spin after having scanned 1<<BLKSIZE bytes */
13106d01dbeSwdenk 
13206d01dbeSwdenk /* Debugging switches */
13306d01dbeSwdenk #undef	DEBUG_DIRENTS		/* print directory entry list after scan */
13406d01dbeSwdenk #undef	DEBUG_FRAGMENTS		/* print fragment list after scan */
13506d01dbeSwdenk #undef	DEBUG			/* enable debugging messages */
13606d01dbeSwdenk 
13706d01dbeSwdenk 
13806d01dbeSwdenk #ifdef  DEBUG
13906d01dbeSwdenk # define DEBUGF(fmt,args...)	printf(fmt ,##args)
14006d01dbeSwdenk #else
14106d01dbeSwdenk # define DEBUGF(fmt,args...)
14206d01dbeSwdenk #endif
14306d01dbeSwdenk 
1449b707622SIlya Yanok #include "summary.h"
1459b707622SIlya Yanok 
146700a0c64SWolfgang Denk /* keeps pointer to currentlu processed partition */
147700a0c64SWolfgang Denk static struct part_info *current_part;
148998eaaecSwdenk 
149e4dbe1b2SWolfgang Denk #if (defined(CONFIG_JFFS2_NAND) && \
150dd60d122SJon Loeliger      defined(CONFIG_CMD_NAND) )
151addb2e16SBartlomiej Sieka #include <nand.h>
152998eaaecSwdenk /*
153998eaaecSwdenk  * Support for jffs2 on top of NAND-flash
154998eaaecSwdenk  *
155998eaaecSwdenk  * NAND memory isn't mapped in processor's address space,
156998eaaecSwdenk  * so data should be fetched from flash before
157998eaaecSwdenk  * being processed. This is exactly what functions declared
158998eaaecSwdenk  * here do.
159998eaaecSwdenk  *
160998eaaecSwdenk  */
161998eaaecSwdenk 
162998eaaecSwdenk #define NAND_PAGE_SIZE 512
163998eaaecSwdenk #define NAND_PAGE_SHIFT 9
164998eaaecSwdenk #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
165998eaaecSwdenk 
166998eaaecSwdenk #ifndef NAND_CACHE_PAGES
167998eaaecSwdenk #define NAND_CACHE_PAGES 16
168998eaaecSwdenk #endif
169998eaaecSwdenk #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
170998eaaecSwdenk 
171998eaaecSwdenk static u8* nand_cache = NULL;
172998eaaecSwdenk static u32 nand_cache_off = (u32)-1;
173998eaaecSwdenk 
174998eaaecSwdenk static int read_nand_cached(u32 off, u32 size, u_char *buf)
175998eaaecSwdenk {
176700a0c64SWolfgang Denk 	struct mtdids *id = current_part->dev->id;
177998eaaecSwdenk 	u32 bytes_read = 0;
1786db39708SMarian Balakowicz 	size_t retlen;
179998eaaecSwdenk 	int cpy_bytes;
180998eaaecSwdenk 
181998eaaecSwdenk 	while (bytes_read < size) {
182998eaaecSwdenk 		if ((off + bytes_read < nand_cache_off) ||
183998eaaecSwdenk 		    (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
184998eaaecSwdenk 			nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
185998eaaecSwdenk 			if (!nand_cache) {
186998eaaecSwdenk 				/* This memory never gets freed but 'cause
187998eaaecSwdenk 				   it's a bootloader, nobody cares */
188998eaaecSwdenk 				nand_cache = malloc(NAND_CACHE_SIZE);
189998eaaecSwdenk 				if (!nand_cache) {
190998eaaecSwdenk 					printf("read_nand_cached: can't alloc cache size %d bytes\n",
191998eaaecSwdenk 					       NAND_CACHE_SIZE);
192998eaaecSwdenk 					return -1;
193998eaaecSwdenk 				}
194998eaaecSwdenk 			}
195addb2e16SBartlomiej Sieka 
196addb2e16SBartlomiej Sieka 			retlen = NAND_CACHE_SIZE;
197addb2e16SBartlomiej Sieka 			if (nand_read(&nand_info[id->num], nand_cache_off,
198addb2e16SBartlomiej Sieka 						&retlen, nand_cache) != 0 ||
199998eaaecSwdenk 					retlen != NAND_CACHE_SIZE) {
200998eaaecSwdenk 				printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
201998eaaecSwdenk 						nand_cache_off, NAND_CACHE_SIZE);
202998eaaecSwdenk 				return -1;
203998eaaecSwdenk 			}
204998eaaecSwdenk 		}
205998eaaecSwdenk 		cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
206998eaaecSwdenk 		if (cpy_bytes > size - bytes_read)
207998eaaecSwdenk 			cpy_bytes = size - bytes_read;
208998eaaecSwdenk 		memcpy(buf + bytes_read,
209998eaaecSwdenk 		       nand_cache + off + bytes_read - nand_cache_off,
210998eaaecSwdenk 		       cpy_bytes);
211998eaaecSwdenk 		bytes_read += cpy_bytes;
212998eaaecSwdenk 	}
213998eaaecSwdenk 	return bytes_read;
214998eaaecSwdenk }
215998eaaecSwdenk 
216700a0c64SWolfgang Denk static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
217998eaaecSwdenk {
218998eaaecSwdenk 	u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
219998eaaecSwdenk 
220998eaaecSwdenk 	if (NULL == buf) {
221700a0c64SWolfgang Denk 		printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
222998eaaecSwdenk 		return NULL;
223998eaaecSwdenk 	}
224998eaaecSwdenk 	if (read_nand_cached(off, size, buf) < 0) {
22532877d66Swdenk 		if (!ext_buf)
226998eaaecSwdenk 			free(buf);
227998eaaecSwdenk 		return NULL;
228998eaaecSwdenk 	}
229998eaaecSwdenk 
230998eaaecSwdenk 	return buf;
231998eaaecSwdenk }
232998eaaecSwdenk 
23370741004SIlya Yanok static void *get_node_mem_nand(u32 off, void *ext_buf)
234998eaaecSwdenk {
235998eaaecSwdenk 	struct jffs2_unknown_node node;
236998eaaecSwdenk 	void *ret = NULL;
237998eaaecSwdenk 
238700a0c64SWolfgang Denk 	if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
239998eaaecSwdenk 		return NULL;
240998eaaecSwdenk 
241700a0c64SWolfgang Denk 	if (!(ret = get_fl_mem_nand(off, node.magic ==
242998eaaecSwdenk 			       JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
24370741004SIlya Yanok 			       ext_buf))) {
244998eaaecSwdenk 		printf("off = %#x magic %#x type %#x node.totlen = %d\n",
245998eaaecSwdenk 		       off, node.magic, node.nodetype, node.totlen);
246998eaaecSwdenk 	}
247998eaaecSwdenk 	return ret;
248998eaaecSwdenk }
249998eaaecSwdenk 
250700a0c64SWolfgang Denk static void put_fl_mem_nand(void *buf)
251998eaaecSwdenk {
252998eaaecSwdenk 	free(buf);
253998eaaecSwdenk }
254dd60d122SJon Loeliger #endif
255998eaaecSwdenk 
2561a7f8cceSKyungmin Park #if defined(CONFIG_CMD_ONENAND)
2571a7f8cceSKyungmin Park 
2581a7f8cceSKyungmin Park #include <linux/mtd/mtd.h>
2591a7f8cceSKyungmin Park #include <linux/mtd/onenand.h>
2601a7f8cceSKyungmin Park #include <onenand_uboot.h>
2611a7f8cceSKyungmin Park 
2621a7f8cceSKyungmin Park #define ONENAND_PAGE_SIZE 2048
2631a7f8cceSKyungmin Park #define ONENAND_PAGE_SHIFT 11
2641a7f8cceSKyungmin Park #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
2651a7f8cceSKyungmin Park 
2661a7f8cceSKyungmin Park #ifndef ONENAND_CACHE_PAGES
2671a7f8cceSKyungmin Park #define ONENAND_CACHE_PAGES 4
2681a7f8cceSKyungmin Park #endif
2691a7f8cceSKyungmin Park #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
2701a7f8cceSKyungmin Park 
2711a7f8cceSKyungmin Park static u8* onenand_cache;
2721a7f8cceSKyungmin Park static u32 onenand_cache_off = (u32)-1;
2731a7f8cceSKyungmin Park 
2741a7f8cceSKyungmin Park static int read_onenand_cached(u32 off, u32 size, u_char *buf)
2751a7f8cceSKyungmin Park {
2761a7f8cceSKyungmin Park 	u32 bytes_read = 0;
2771a7f8cceSKyungmin Park 	size_t retlen;
2781a7f8cceSKyungmin Park 	int cpy_bytes;
2791a7f8cceSKyungmin Park 
2801a7f8cceSKyungmin Park 	while (bytes_read < size) {
2811a7f8cceSKyungmin Park 		if ((off + bytes_read < onenand_cache_off) ||
2821a7f8cceSKyungmin Park 		    (off + bytes_read >= onenand_cache_off + ONENAND_CACHE_SIZE)) {
2831a7f8cceSKyungmin Park 			onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
2841a7f8cceSKyungmin Park 			if (!onenand_cache) {
2851a7f8cceSKyungmin Park 				/* This memory never gets freed but 'cause
2861a7f8cceSKyungmin Park 				   it's a bootloader, nobody cares */
2871a7f8cceSKyungmin Park 				onenand_cache = malloc(ONENAND_CACHE_SIZE);
2881a7f8cceSKyungmin Park 				if (!onenand_cache) {
2891a7f8cceSKyungmin Park 					printf("read_onenand_cached: can't alloc cache size %d bytes\n",
2901a7f8cceSKyungmin Park 					       ONENAND_CACHE_SIZE);
2911a7f8cceSKyungmin Park 					return -1;
2921a7f8cceSKyungmin Park 				}
2931a7f8cceSKyungmin Park 			}
2941a7f8cceSKyungmin Park 
2951a7f8cceSKyungmin Park 			retlen = ONENAND_CACHE_SIZE;
2961a7f8cceSKyungmin Park 			if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
2971a7f8cceSKyungmin Park 						&retlen, onenand_cache) != 0 ||
2981a7f8cceSKyungmin Park 					retlen != ONENAND_CACHE_SIZE) {
2991a7f8cceSKyungmin Park 				printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
3001a7f8cceSKyungmin Park 					onenand_cache_off, ONENAND_CACHE_SIZE);
3011a7f8cceSKyungmin Park 				return -1;
3021a7f8cceSKyungmin Park 			}
3031a7f8cceSKyungmin Park 		}
3041a7f8cceSKyungmin Park 		cpy_bytes = onenand_cache_off + ONENAND_CACHE_SIZE - (off + bytes_read);
3051a7f8cceSKyungmin Park 		if (cpy_bytes > size - bytes_read)
3061a7f8cceSKyungmin Park 			cpy_bytes = size - bytes_read;
3071a7f8cceSKyungmin Park 		memcpy(buf + bytes_read,
3081a7f8cceSKyungmin Park 		       onenand_cache + off + bytes_read - onenand_cache_off,
3091a7f8cceSKyungmin Park 		       cpy_bytes);
3101a7f8cceSKyungmin Park 		bytes_read += cpy_bytes;
3111a7f8cceSKyungmin Park 	}
3121a7f8cceSKyungmin Park 	return bytes_read;
3131a7f8cceSKyungmin Park }
3141a7f8cceSKyungmin Park 
3151a7f8cceSKyungmin Park static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
3161a7f8cceSKyungmin Park {
3171a7f8cceSKyungmin Park 	u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
3181a7f8cceSKyungmin Park 
3191a7f8cceSKyungmin Park 	if (NULL == buf) {
3201a7f8cceSKyungmin Park 		printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
3211a7f8cceSKyungmin Park 		return NULL;
3221a7f8cceSKyungmin Park 	}
3231a7f8cceSKyungmin Park 	if (read_onenand_cached(off, size, buf) < 0) {
3241a7f8cceSKyungmin Park 		if (!ext_buf)
3251a7f8cceSKyungmin Park 			free(buf);
3261a7f8cceSKyungmin Park 		return NULL;
3271a7f8cceSKyungmin Park 	}
3281a7f8cceSKyungmin Park 
3291a7f8cceSKyungmin Park 	return buf;
3301a7f8cceSKyungmin Park }
3311a7f8cceSKyungmin Park 
33270741004SIlya Yanok static void *get_node_mem_onenand(u32 off, void *ext_buf)
3331a7f8cceSKyungmin Park {
3341a7f8cceSKyungmin Park 	struct jffs2_unknown_node node;
3351a7f8cceSKyungmin Park 	void *ret = NULL;
3361a7f8cceSKyungmin Park 
3371a7f8cceSKyungmin Park 	if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
3381a7f8cceSKyungmin Park 		return NULL;
3391a7f8cceSKyungmin Park 
3401a7f8cceSKyungmin Park 	ret = get_fl_mem_onenand(off, node.magic ==
3411a7f8cceSKyungmin Park 			JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
34270741004SIlya Yanok 			ext_buf);
3431a7f8cceSKyungmin Park 	if (!ret) {
3441a7f8cceSKyungmin Park 		printf("off = %#x magic %#x type %#x node.totlen = %d\n",
3451a7f8cceSKyungmin Park 		       off, node.magic, node.nodetype, node.totlen);
3461a7f8cceSKyungmin Park 	}
3471a7f8cceSKyungmin Park 	return ret;
3481a7f8cceSKyungmin Park }
3491a7f8cceSKyungmin Park 
3501a7f8cceSKyungmin Park 
3511a7f8cceSKyungmin Park static void put_fl_mem_onenand(void *buf)
3521a7f8cceSKyungmin Park {
3531a7f8cceSKyungmin Park 	free(buf);
3541a7f8cceSKyungmin Park }
3551a7f8cceSKyungmin Park #endif
3561a7f8cceSKyungmin Park 
357998eaaecSwdenk 
358dd60d122SJon Loeliger #if defined(CONFIG_CMD_FLASH)
359700a0c64SWolfgang Denk /*
360700a0c64SWolfgang Denk  * Support for jffs2 on top of NOR-flash
361700a0c64SWolfgang Denk  *
362700a0c64SWolfgang Denk  * NOR flash memory is mapped in processor's address space,
363700a0c64SWolfgang Denk  * just return address.
364700a0c64SWolfgang Denk  */
36570741004SIlya Yanok static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
366700a0c64SWolfgang Denk {
367700a0c64SWolfgang Denk 	u32 addr = off;
368700a0c64SWolfgang Denk 	struct mtdids *id = current_part->dev->id;
369700a0c64SWolfgang Denk 
370e6f2e902SMarian Balakowicz 	extern flash_info_t flash_info[];
371700a0c64SWolfgang Denk 	flash_info_t *flash = &flash_info[id->num];
372700a0c64SWolfgang Denk 
373700a0c64SWolfgang Denk 	addr += flash->start[0];
37470741004SIlya Yanok 	if (ext_buf) {
37570741004SIlya Yanok 		memcpy(ext_buf, (void *)addr, size);
37670741004SIlya Yanok 		return ext_buf;
37770741004SIlya Yanok 	}
378700a0c64SWolfgang Denk 	return (void*)addr;
379700a0c64SWolfgang Denk }
380700a0c64SWolfgang Denk 
38170741004SIlya Yanok static inline void *get_node_mem_nor(u32 off, void *ext_buf)
3828a36d31fSIlya Yanok {
38370741004SIlya Yanok 	struct jffs2_unknown_node *pNode;
3848a36d31fSIlya Yanok 
38570741004SIlya Yanok 	/* pNode will point directly to flash - don't provide external buffer
38670741004SIlya Yanok 	   and don't care about size */
38770741004SIlya Yanok 	pNode = get_fl_mem_nor(off, 0, NULL);
38870741004SIlya Yanok 	return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
38970741004SIlya Yanok 			pNode->totlen : sizeof(*pNode), ext_buf);
390700a0c64SWolfgang Denk }
391dd60d122SJon Loeliger #endif
392700a0c64SWolfgang Denk 
393700a0c64SWolfgang Denk 
394700a0c64SWolfgang Denk /*
395700a0c64SWolfgang Denk  * Generic jffs2 raw memory and node read routines.
396700a0c64SWolfgang Denk  *
397700a0c64SWolfgang Denk  */
398998eaaecSwdenk static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
399998eaaecSwdenk {
400700a0c64SWolfgang Denk 	struct mtdids *id = current_part->dev->id;
401700a0c64SWolfgang Denk 
4022d2018f3SHeiko Schocher 	switch(id->type) {
403dd60d122SJon Loeliger #if defined(CONFIG_CMD_FLASH)
4042d2018f3SHeiko Schocher 	case MTD_DEV_TYPE_NOR:
40570741004SIlya Yanok 		return get_fl_mem_nor(off, size, ext_buf);
4062d2018f3SHeiko Schocher 		break;
407700a0c64SWolfgang Denk #endif
408dd60d122SJon Loeliger #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
4092d2018f3SHeiko Schocher 	case MTD_DEV_TYPE_NAND:
410700a0c64SWolfgang Denk 		return get_fl_mem_nand(off, size, ext_buf);
4112d2018f3SHeiko Schocher 		break;
412700a0c64SWolfgang Denk #endif
4131a7f8cceSKyungmin Park #if defined(CONFIG_CMD_ONENAND)
4142d2018f3SHeiko Schocher 	case MTD_DEV_TYPE_ONENAND:
4151a7f8cceSKyungmin Park 		return get_fl_mem_onenand(off, size, ext_buf);
4162d2018f3SHeiko Schocher 		break;
4171a7f8cceSKyungmin Park #endif
4182d2018f3SHeiko Schocher 	default:
4192d2018f3SHeiko Schocher 		printf("get_fl_mem: unknown device type, " \
4202d2018f3SHeiko Schocher 			"using raw offset!\n");
4212d2018f3SHeiko Schocher 	}
422998eaaecSwdenk 	return (void*)off;
423998eaaecSwdenk }
424998eaaecSwdenk 
42570741004SIlya Yanok static inline void *get_node_mem(u32 off, void *ext_buf)
426998eaaecSwdenk {
427700a0c64SWolfgang Denk 	struct mtdids *id = current_part->dev->id;
428700a0c64SWolfgang Denk 
4292d2018f3SHeiko Schocher 	switch(id->type) {
430dd60d122SJon Loeliger #if defined(CONFIG_CMD_FLASH)
4312d2018f3SHeiko Schocher 	case MTD_DEV_TYPE_NOR:
43270741004SIlya Yanok 		return get_node_mem_nor(off, ext_buf);
4332d2018f3SHeiko Schocher 		break;
434700a0c64SWolfgang Denk #endif
435e4dbe1b2SWolfgang Denk #if defined(CONFIG_JFFS2_NAND) && \
436dd60d122SJon Loeliger     defined(CONFIG_CMD_NAND)
4372d2018f3SHeiko Schocher 	case MTD_DEV_TYPE_NAND:
43870741004SIlya Yanok 		return get_node_mem_nand(off, ext_buf);
4392d2018f3SHeiko Schocher 		break;
440700a0c64SWolfgang Denk #endif
4411a7f8cceSKyungmin Park #if defined(CONFIG_CMD_ONENAND)
4422d2018f3SHeiko Schocher 	case MTD_DEV_TYPE_ONENAND:
44370741004SIlya Yanok 		return get_node_mem_onenand(off, ext_buf);
4442d2018f3SHeiko Schocher 		break;
4451a7f8cceSKyungmin Park #endif
4462d2018f3SHeiko Schocher 	default:
4472d2018f3SHeiko Schocher 		printf("get_fl_mem: unknown device type, " \
4482d2018f3SHeiko Schocher 			"using raw offset!\n");
4492d2018f3SHeiko Schocher 	}
450998eaaecSwdenk 	return (void*)off;
451998eaaecSwdenk }
452998eaaecSwdenk 
45370741004SIlya Yanok static inline void put_fl_mem(void *buf, void *ext_buf)
454998eaaecSwdenk {
455700a0c64SWolfgang Denk 	struct mtdids *id = current_part->dev->id;
456700a0c64SWolfgang Denk 
45770741004SIlya Yanok 	/* If buf is the same as ext_buf, it was provided by the caller -
45870741004SIlya Yanok 	   we shouldn't free it then. */
45970741004SIlya Yanok 	if (buf == ext_buf)
46070741004SIlya Yanok 		return;
4612f77c7f4SScott Wood 	switch (id->type) {
4622f77c7f4SScott Wood #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
4632f77c7f4SScott Wood 	case MTD_DEV_TYPE_NAND:
464700a0c64SWolfgang Denk 		return put_fl_mem_nand(buf);
465700a0c64SWolfgang Denk #endif
4661a7f8cceSKyungmin Park #if defined(CONFIG_CMD_ONENAND)
4672f77c7f4SScott Wood 	case MTD_DEV_TYPE_ONENAND:
4681a7f8cceSKyungmin Park 		return put_fl_mem_onenand(buf);
4691a7f8cceSKyungmin Park #endif
470998eaaecSwdenk 	}
4712f77c7f4SScott Wood }
472998eaaecSwdenk 
473fe8c2806Swdenk /* Compression names */
474fe8c2806Swdenk static char *compr_names[] = {
475fe8c2806Swdenk 	"NONE",
476fe8c2806Swdenk 	"ZERO",
477fe8c2806Swdenk 	"RTIME",
478fe8c2806Swdenk 	"RUBINMIPS",
479fe8c2806Swdenk 	"COPY",
480fe8c2806Swdenk 	"DYNRUBIN",
48107cc0999Swdenk 	"ZLIB",
482f0983371SWolfgang Denk #if defined(CONFIG_JFFS2_LZO)
48307cc0999Swdenk 	"LZO",
48407cc0999Swdenk #endif
48506d01dbeSwdenk };
486fe8c2806Swdenk 
48706d01dbeSwdenk /* Memory management */
48806d01dbeSwdenk struct mem_block {
48906d01dbeSwdenk 	u32	index;
49006d01dbeSwdenk 	struct mem_block *next;
49106d01dbeSwdenk 	struct b_node nodes[NODE_CHUNK];
49206d01dbeSwdenk };
493fe8c2806Swdenk 
494fe8c2806Swdenk 
49506d01dbeSwdenk static void
49606d01dbeSwdenk free_nodes(struct b_list *list)
49706d01dbeSwdenk {
49806d01dbeSwdenk 	while (list->listMemBase != NULL) {
49906d01dbeSwdenk 		struct mem_block *next = list->listMemBase->next;
50006d01dbeSwdenk 		free( list->listMemBase );
50106d01dbeSwdenk 		list->listMemBase = next;
50206d01dbeSwdenk 	}
50306d01dbeSwdenk }
504fe8c2806Swdenk 
505fe8c2806Swdenk static struct b_node *
50606d01dbeSwdenk add_node(struct b_list *list)
507fe8c2806Swdenk {
50806d01dbeSwdenk 	u32 index = 0;
50906d01dbeSwdenk 	struct mem_block *memBase;
510fe8c2806Swdenk 	struct b_node *b;
511fe8c2806Swdenk 
51206d01dbeSwdenk 	memBase = list->listMemBase;
51306d01dbeSwdenk 	if (memBase != NULL)
51406d01dbeSwdenk 		index = memBase->index;
515fe8c2806Swdenk #if 0
516fe8c2806Swdenk 	putLabeledWord("add_node: index = ", index);
51706d01dbeSwdenk 	putLabeledWord("add_node: memBase = ", list->listMemBase);
518fe8c2806Swdenk #endif
519fe8c2806Swdenk 
52006d01dbeSwdenk 	if (memBase == NULL || index >= NODE_CHUNK) {
52106d01dbeSwdenk 		/* we need more space before we continue */
52206d01dbeSwdenk 		memBase = mmalloc(sizeof(struct mem_block));
52306d01dbeSwdenk 		if (memBase == NULL) {
524fe8c2806Swdenk 			putstr("add_node: malloc failed\n");
525fe8c2806Swdenk 			return NULL;
526fe8c2806Swdenk 		}
52706d01dbeSwdenk 		memBase->next = list->listMemBase;
52806d01dbeSwdenk 		index = 0;
529fe8c2806Swdenk #if 0
530fe8c2806Swdenk 		putLabeledWord("add_node: alloced a new membase at ", *memBase);
531fe8c2806Swdenk #endif
532fe8c2806Swdenk 
533fe8c2806Swdenk 	}
534fe8c2806Swdenk 	/* now we have room to add it. */
53506d01dbeSwdenk 	b = &memBase->nodes[index];
53606d01dbeSwdenk 	index ++;
537fe8c2806Swdenk 
53806d01dbeSwdenk 	memBase->index = index;
53906d01dbeSwdenk 	list->listMemBase = memBase;
54006d01dbeSwdenk 	list->listCount++;
541fe8c2806Swdenk 	return b;
542fe8c2806Swdenk }
543fe8c2806Swdenk 
54406d01dbeSwdenk static struct b_node *
54506d01dbeSwdenk insert_node(struct b_list *list, u32 offset)
54606d01dbeSwdenk {
54706d01dbeSwdenk 	struct b_node *new;
5486d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
54906d01dbeSwdenk 	struct b_node *b, *prev;
55006d01dbeSwdenk #endif
55106d01dbeSwdenk 
55206d01dbeSwdenk 	if (!(new = add_node(list))) {
55306d01dbeSwdenk 		putstr("add_node failed!\r\n");
55406d01dbeSwdenk 		return NULL;
55506d01dbeSwdenk 	}
55606d01dbeSwdenk 	new->offset = offset;
55706d01dbeSwdenk 
5586d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
55906d01dbeSwdenk 	if (list->listTail != NULL && list->listCompare(new, list->listTail))
56006d01dbeSwdenk 		prev = list->listTail;
56106d01dbeSwdenk 	else if (list->listLast != NULL && list->listCompare(new, list->listLast))
56206d01dbeSwdenk 		prev = list->listLast;
56306d01dbeSwdenk 	else
56406d01dbeSwdenk 		prev = NULL;
56506d01dbeSwdenk 
56606d01dbeSwdenk 	for (b = (prev ? prev->next : list->listHead);
56706d01dbeSwdenk 	     b != NULL && list->listCompare(new, b);
56806d01dbeSwdenk 	     prev = b, b = b->next) {
56906d01dbeSwdenk 		list->listLoops++;
57006d01dbeSwdenk 	}
57106d01dbeSwdenk 	if (b != NULL)
57206d01dbeSwdenk 		list->listLast = prev;
57306d01dbeSwdenk 
57406d01dbeSwdenk 	if (b != NULL) {
57506d01dbeSwdenk 		new->next = b;
57606d01dbeSwdenk 		if (prev != NULL)
57706d01dbeSwdenk 			prev->next = new;
57806d01dbeSwdenk 		else
57906d01dbeSwdenk 			list->listHead = new;
58006d01dbeSwdenk 	} else
58106d01dbeSwdenk #endif
58206d01dbeSwdenk 	{
58306d01dbeSwdenk 		new->next = (struct b_node *) NULL;
58406d01dbeSwdenk 		if (list->listTail != NULL) {
58506d01dbeSwdenk 			list->listTail->next = new;
58606d01dbeSwdenk 			list->listTail = new;
58706d01dbeSwdenk 		} else {
58806d01dbeSwdenk 			list->listTail = list->listHead = new;
58906d01dbeSwdenk 		}
59006d01dbeSwdenk 	}
59106d01dbeSwdenk 
59206d01dbeSwdenk 	return new;
59306d01dbeSwdenk }
59406d01dbeSwdenk 
5956d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
5967205e407Swdenk /* Sort data entries with the latest version last, so that if there
5977205e407Swdenk  * is overlapping data the latest version will be used.
5987205e407Swdenk  */
59906d01dbeSwdenk static int compare_inodes(struct b_node *new, struct b_node *old)
60006d01dbeSwdenk {
601*225cf4cdSMark Tomlinson 	/*
602*225cf4cdSMark Tomlinson 	 * Only read in the version info from flash, not the entire inode.
603*225cf4cdSMark Tomlinson 	 * This can make a big difference to speed if flash is slow.
604*225cf4cdSMark Tomlinson 	 */
605*225cf4cdSMark Tomlinson 	u32 new_version;
606*225cf4cdSMark Tomlinson 	u32 old_version;
607*225cf4cdSMark Tomlinson 	get_fl_mem(new->offset + offsetof(struct jffs2_raw_inode, version),
608*225cf4cdSMark Tomlinson 		   sizeof(new_version), &new_version);
609*225cf4cdSMark Tomlinson 	get_fl_mem(old->offset + offsetof(struct jffs2_raw_inode, version),
610*225cf4cdSMark Tomlinson 		   sizeof(old_version), &old_version);
61106d01dbeSwdenk 
612*225cf4cdSMark Tomlinson 	return new_version > old_version;
61306d01dbeSwdenk }
61406d01dbeSwdenk 
6157205e407Swdenk /* Sort directory entries so all entries in the same directory
6167205e407Swdenk  * with the same name are grouped together, with the latest version
6177205e407Swdenk  * last. This makes it easy to eliminate all but the latest version
6187205e407Swdenk  * by marking the previous version dead by setting the inode to 0.
6197205e407Swdenk  */
62006d01dbeSwdenk static int compare_dirents(struct b_node *new, struct b_node *old)
62106d01dbeSwdenk {
622*225cf4cdSMark Tomlinson 	/*
623*225cf4cdSMark Tomlinson 	 * Using NULL as the buffer for NOR flash prevents the entire node
624*225cf4cdSMark Tomlinson 	 * being read. This makes most comparisons much quicker as only one
625*225cf4cdSMark Tomlinson 	 * or two entries from the node will be used most of the time.
626*225cf4cdSMark Tomlinson 	 */
627*225cf4cdSMark Tomlinson 	struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
628*225cf4cdSMark Tomlinson 	struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
6297205e407Swdenk 	int cmp;
630*225cf4cdSMark Tomlinson 	int ret;
63106d01dbeSwdenk 
632*225cf4cdSMark Tomlinson 	if (jNew->pino != jOld->pino) {
6337205e407Swdenk 		/* ascending sort by pino */
634*225cf4cdSMark Tomlinson 		ret = jNew->pino > jOld->pino;
635*225cf4cdSMark Tomlinson 	} else if (jNew->nsize != jOld->nsize) {
636*225cf4cdSMark Tomlinson 		/*
637*225cf4cdSMark Tomlinson 		 * pino is the same, so use ascending sort by nsize,
638*225cf4cdSMark Tomlinson 		 * so we don't do strncmp unless we really must.
6397205e407Swdenk 		 */
640*225cf4cdSMark Tomlinson 		ret = jNew->nsize > jOld->nsize;
641*225cf4cdSMark Tomlinson 	} else {
642*225cf4cdSMark Tomlinson 		/*
643*225cf4cdSMark Tomlinson 		 * length is also the same, so use ascending sort by name
6447205e407Swdenk 		 */
645*225cf4cdSMark Tomlinson 		cmp = strncmp((char *)jNew->name, (char *)jOld->name,
646*225cf4cdSMark Tomlinson 			jNew->nsize);
647*225cf4cdSMark Tomlinson 		if (cmp != 0) {
648*225cf4cdSMark Tomlinson 			ret = cmp > 0;
649*225cf4cdSMark Tomlinson 		} else {
650*225cf4cdSMark Tomlinson 			/*
651*225cf4cdSMark Tomlinson 			 * we have duplicate names in this directory,
652*225cf4cdSMark Tomlinson 			 * so use ascending sort by version
6537205e407Swdenk 			 */
654*225cf4cdSMark Tomlinson 			ret = jNew->version > jOld->version;
6557205e407Swdenk 		}
656*225cf4cdSMark Tomlinson 	}
657*225cf4cdSMark Tomlinson 	put_fl_mem(jNew, NULL);
658*225cf4cdSMark Tomlinson 	put_fl_mem(jOld, NULL);
6597205e407Swdenk 
660*225cf4cdSMark Tomlinson 	return ret;
66106d01dbeSwdenk }
66206d01dbeSwdenk #endif
663fe8c2806Swdenk 
664700a0c64SWolfgang Denk void
665700a0c64SWolfgang Denk jffs2_free_cache(struct part_info *part)
666fe8c2806Swdenk {
66706d01dbeSwdenk 	struct b_lists *pL;
668fe8c2806Swdenk 
66906d01dbeSwdenk 	if (part->jffs2_priv != NULL) {
67006d01dbeSwdenk 		pL = (struct b_lists *)part->jffs2_priv;
67106d01dbeSwdenk 		free_nodes(&pL->frag);
67206d01dbeSwdenk 		free_nodes(&pL->dir);
67370741004SIlya Yanok 		free(pL->readbuf);
67406d01dbeSwdenk 		free(pL);
67506d01dbeSwdenk 	}
676700a0c64SWolfgang Denk }
677700a0c64SWolfgang Denk 
678700a0c64SWolfgang Denk static u32
679700a0c64SWolfgang Denk jffs_init_1pass_list(struct part_info *part)
680700a0c64SWolfgang Denk {
681700a0c64SWolfgang Denk 	struct b_lists *pL;
682700a0c64SWolfgang Denk 
683700a0c64SWolfgang Denk 	jffs2_free_cache(part);
684700a0c64SWolfgang Denk 
68506d01dbeSwdenk 	if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
68606d01dbeSwdenk 		pL = (struct b_lists *)part->jffs2_priv;
68706d01dbeSwdenk 
68806d01dbeSwdenk 		memset(pL, 0, sizeof(*pL));
6896d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
69006d01dbeSwdenk 		pL->dir.listCompare = compare_dirents;
69106d01dbeSwdenk 		pL->frag.listCompare = compare_inodes;
69206d01dbeSwdenk #endif
693fe8c2806Swdenk 	}
694fe8c2806Swdenk 	return 0;
695fe8c2806Swdenk }
696fe8c2806Swdenk 
697fe8c2806Swdenk /* find the inode from the slashless name given a parent */
698fe8c2806Swdenk static long
699fe8c2806Swdenk jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
700fe8c2806Swdenk {
701fe8c2806Swdenk 	struct b_node *b;
702fe8c2806Swdenk 	struct jffs2_raw_inode *jNode;
70306d01dbeSwdenk 	u32 totalSize = 0;
7047205e407Swdenk 	u32 latestVersion = 0;
70577ddac94SWolfgang Denk 	uchar *lDest;
70677ddac94SWolfgang Denk 	uchar *src;
707fe8c2806Swdenk 	int i;
708fe8c2806Swdenk 	u32 counter = 0;
7096d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
7107205e407Swdenk 	/* Find file size before loading any data, so fragments that
7117205e407Swdenk 	 * start past the end of file can be ignored. A fragment
7127205e407Swdenk 	 * that is partially in the file is loaded, so extra data may
7137205e407Swdenk 	 * be loaded up to the next 4K boundary above the file size.
7147205e407Swdenk 	 * This shouldn't cause trouble when loading kernel images, so
7157205e407Swdenk 	 * we will live with it.
7167205e407Swdenk 	 */
7177205e407Swdenk 	for (b = pL->frag.listHead; b != NULL; b = b->next) {
718998eaaecSwdenk 		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
71970741004SIlya Yanok 			sizeof(struct jffs2_raw_inode), pL->readbuf);
7207205e407Swdenk 		if ((inode == jNode->ino)) {
7217205e407Swdenk 			/* get actual file length from the newest node */
7227205e407Swdenk 			if (jNode->version >= latestVersion) {
7237205e407Swdenk 				totalSize = jNode->isize;
7247205e407Swdenk 				latestVersion = jNode->version;
7257205e407Swdenk 			}
7267205e407Swdenk 		}
72770741004SIlya Yanok 		put_fl_mem(jNode, pL->readbuf);
7287205e407Swdenk 	}
7293799b3f4SMark Tomlinson 	/*
7303799b3f4SMark Tomlinson 	 * If no destination is provided, we are done.
7313799b3f4SMark Tomlinson 	 * Just return the total size.
7323799b3f4SMark Tomlinson 	 */
7333799b3f4SMark Tomlinson 	if (!dest)
7343799b3f4SMark Tomlinson 		return totalSize;
7357205e407Swdenk #endif
736fe8c2806Swdenk 
73706d01dbeSwdenk 	for (b = pL->frag.listHead; b != NULL; b = b->next) {
73870741004SIlya Yanok 		jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset,
73970741004SIlya Yanok 								pL->readbuf);
74046f46fd4SJeroen Hofstee 		if (inode == jNode->ino) {
74106d01dbeSwdenk #if 0
742fe8c2806Swdenk 			putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
743fe8c2806Swdenk 			putLabeledWord("read_inode: inode = ", jNode->ino);
744fe8c2806Swdenk 			putLabeledWord("read_inode: version = ", jNode->version);
745fe8c2806Swdenk 			putLabeledWord("read_inode: isize = ", jNode->isize);
746fe8c2806Swdenk 			putLabeledWord("read_inode: offset = ", jNode->offset);
747fe8c2806Swdenk 			putLabeledWord("read_inode: csize = ", jNode->csize);
748fe8c2806Swdenk 			putLabeledWord("read_inode: dsize = ", jNode->dsize);
749fe8c2806Swdenk 			putLabeledWord("read_inode: compr = ", jNode->compr);
750fe8c2806Swdenk 			putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
751fe8c2806Swdenk 			putLabeledWord("read_inode: flags = ", jNode->flags);
752fe8c2806Swdenk #endif
7537205e407Swdenk 
7546d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
75506d01dbeSwdenk 			/* get actual file length from the newest node */
75606d01dbeSwdenk 			if (jNode->version >= latestVersion) {
757fe8c2806Swdenk 				totalSize = jNode->isize;
75806d01dbeSwdenk 				latestVersion = jNode->version;
759fe8c2806Swdenk 			}
7607205e407Swdenk #endif
761fe8c2806Swdenk 
762fe8c2806Swdenk 			if(dest) {
76377ddac94SWolfgang Denk 				src = ((uchar *) jNode) + sizeof(struct jffs2_raw_inode);
76406d01dbeSwdenk 				/* ignore data behind latest known EOF */
765998eaaecSwdenk 				if (jNode->offset > totalSize) {
76670741004SIlya Yanok 					put_fl_mem(jNode, pL->readbuf);
76706d01dbeSwdenk 					continue;
768998eaaecSwdenk 				}
769142a80ffSIlya Yanok 				if (b->datacrc == CRC_UNKNOWN)
770142a80ffSIlya Yanok 					b->datacrc = data_crc(jNode) ?
771142a80ffSIlya Yanok 						CRC_OK : CRC_BAD;
772142a80ffSIlya Yanok 				if (b->datacrc == CRC_BAD) {
77370741004SIlya Yanok 					put_fl_mem(jNode, pL->readbuf);
7748a36d31fSIlya Yanok 					continue;
7758a36d31fSIlya Yanok 				}
77606d01dbeSwdenk 
77777ddac94SWolfgang Denk 				lDest = (uchar *) (dest + jNode->offset);
778fe8c2806Swdenk #if 0
77906d01dbeSwdenk 				putLabeledWord("read_inode: src = ", src);
780fe8c2806Swdenk 				putLabeledWord("read_inode: dest = ", lDest);
781fe8c2806Swdenk #endif
782fe8c2806Swdenk 				switch (jNode->compr) {
783fe8c2806Swdenk 				case JFFS2_COMPR_NONE:
78416b9afd2SWolfgang Denk 					ldr_memcpy(lDest, src, jNode->dsize);
785fe8c2806Swdenk 					break;
786fe8c2806Swdenk 				case JFFS2_COMPR_ZERO:
787fe8c2806Swdenk 					for (i = 0; i < jNode->dsize; i++)
788fe8c2806Swdenk 						*(lDest++) = 0;
789fe8c2806Swdenk 					break;
790fe8c2806Swdenk 				case JFFS2_COMPR_RTIME:
791fe8c2806Swdenk 					rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
792fe8c2806Swdenk 					break;
793fe8c2806Swdenk 				case JFFS2_COMPR_DYNRUBIN:
794fe8c2806Swdenk 					/* this is slow but it works */
795fe8c2806Swdenk 					dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
796fe8c2806Swdenk 					break;
797fe8c2806Swdenk 				case JFFS2_COMPR_ZLIB:
79816b9afd2SWolfgang Denk 					zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
799fe8c2806Swdenk 					break;
800f0983371SWolfgang Denk #if defined(CONFIG_JFFS2_LZO)
80107cc0999Swdenk 				case JFFS2_COMPR_LZO:
80216b9afd2SWolfgang Denk 					lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
80307cc0999Swdenk 					break;
80407cc0999Swdenk #endif
805fe8c2806Swdenk 				default:
806fe8c2806Swdenk 					/* unknown */
8076052cbabSLoïc Minier 					putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
80870741004SIlya Yanok 					put_fl_mem(jNode, pL->readbuf);
809fe8c2806Swdenk 					return -1;
810fe8c2806Swdenk 					break;
811fe8c2806Swdenk 				}
812fe8c2806Swdenk 			}
813fe8c2806Swdenk 
814fe8c2806Swdenk #if 0
815fe8c2806Swdenk 			putLabeledWord("read_inode: totalSize = ", totalSize);
816fe8c2806Swdenk #endif
817fe8c2806Swdenk 		}
818fe8c2806Swdenk 		counter++;
81970741004SIlya Yanok 		put_fl_mem(jNode, pL->readbuf);
820fe8c2806Swdenk 	}
821fe8c2806Swdenk 
822fe8c2806Swdenk #if 0
82306d01dbeSwdenk 	putLabeledWord("read_inode: returning = ", totalSize);
824fe8c2806Swdenk #endif
82506d01dbeSwdenk 	return totalSize;
826fe8c2806Swdenk }
827fe8c2806Swdenk 
828fe8c2806Swdenk /* find the inode from the slashless name given a parent */
829fe8c2806Swdenk static u32
830fe8c2806Swdenk jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
831fe8c2806Swdenk {
832fe8c2806Swdenk 	struct b_node *b;
833fe8c2806Swdenk 	struct jffs2_raw_dirent *jDir;
834fe8c2806Swdenk 	int len;
835fe8c2806Swdenk 	u32 counter;
836fe8c2806Swdenk 	u32 version = 0;
837fe8c2806Swdenk 	u32 inode = 0;
838fe8c2806Swdenk 
839fe8c2806Swdenk 	/* name is assumed slash free */
840fe8c2806Swdenk 	len = strlen(name);
841fe8c2806Swdenk 
842fe8c2806Swdenk 	counter = 0;
843fe8c2806Swdenk 	/* we need to search all and return the inode with the highest version */
84406d01dbeSwdenk 	for(b = pL->dir.listHead; b; b = b->next, counter++) {
84570741004SIlya Yanok 		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
84670741004SIlya Yanok 								pL->readbuf);
84706d01dbeSwdenk 		if ((pino == jDir->pino) && (len == jDir->nsize) &&
84806d01dbeSwdenk 		    (jDir->ino) &&	/* 0 for unlink */
84977ddac94SWolfgang Denk 		    (!strncmp((char *)jDir->name, name, len))) {	/* a match */
850998eaaecSwdenk 			if (jDir->version < version) {
85170741004SIlya Yanok 				put_fl_mem(jDir, pL->readbuf);
8527205e407Swdenk 				continue;
853998eaaecSwdenk 			}
854fe8c2806Swdenk 
8557205e407Swdenk 			if (jDir->version == version && inode != 0) {
8567205e407Swdenk 				/* I'm pretty sure this isn't legal */
857fe8c2806Swdenk 				putstr(" ** ERROR ** ");
858fe8c2806Swdenk 				putnstr(jDir->name, jDir->nsize);
859fe8c2806Swdenk 				putLabeledWord(" has dup version =", version);
860fe8c2806Swdenk 			}
861fe8c2806Swdenk 			inode = jDir->ino;
862fe8c2806Swdenk 			version = jDir->version;
863fe8c2806Swdenk 		}
864fe8c2806Swdenk #if 0
865fe8c2806Swdenk 		putstr("\r\nfind_inode:p&l ->");
866fe8c2806Swdenk 		putnstr(jDir->name, jDir->nsize);
867fe8c2806Swdenk 		putstr("\r\n");
868fe8c2806Swdenk 		putLabeledWord("pino = ", jDir->pino);
869fe8c2806Swdenk 		putLabeledWord("nsize = ", jDir->nsize);
870fe8c2806Swdenk 		putLabeledWord("b = ", (u32) b);
871fe8c2806Swdenk 		putLabeledWord("counter = ", counter);
872fe8c2806Swdenk #endif
87370741004SIlya Yanok 		put_fl_mem(jDir, pL->readbuf);
874fe8c2806Swdenk 	}
875fe8c2806Swdenk 	return inode;
876fe8c2806Swdenk }
877fe8c2806Swdenk 
878180d3f74Swdenk char *mkmodestr(unsigned long mode, char *str)
879fe8c2806Swdenk {
880fe8c2806Swdenk 	static const char *l = "xwr";
881fe8c2806Swdenk 	int mask = 1, i;
882fe8c2806Swdenk 	char c;
883fe8c2806Swdenk 
884fe8c2806Swdenk 	switch (mode & S_IFMT) {
885fe8c2806Swdenk 		case S_IFDIR:    str[0] = 'd'; break;
886fe8c2806Swdenk 		case S_IFBLK:    str[0] = 'b'; break;
887fe8c2806Swdenk 		case S_IFCHR:    str[0] = 'c'; break;
888fe8c2806Swdenk 		case S_IFIFO:    str[0] = 'f'; break;
889fe8c2806Swdenk 		case S_IFLNK:    str[0] = 'l'; break;
890fe8c2806Swdenk 		case S_IFSOCK:   str[0] = 's'; break;
891fe8c2806Swdenk 		case S_IFREG:    str[0] = '-'; break;
892fe8c2806Swdenk 		default:         str[0] = '?';
893fe8c2806Swdenk 	}
894fe8c2806Swdenk 
895fe8c2806Swdenk 	for(i = 0; i < 9; i++) {
896fe8c2806Swdenk 		c = l[i%3];
897fe8c2806Swdenk 		str[9-i] = (mode & mask)?c:'-';
898fe8c2806Swdenk 		mask = mask<<1;
899fe8c2806Swdenk 	}
900fe8c2806Swdenk 
901fe8c2806Swdenk 	if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
902fe8c2806Swdenk 	if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
903fe8c2806Swdenk 	if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
904fe8c2806Swdenk 	str[10] = '\0';
905fe8c2806Swdenk 	return str;
906fe8c2806Swdenk }
907fe8c2806Swdenk 
908fe8c2806Swdenk static inline void dump_stat(struct stat *st, const char *name)
909fe8c2806Swdenk {
910fe8c2806Swdenk 	char str[20];
911fe8c2806Swdenk 	char s[64], *p;
912fe8c2806Swdenk 
913fe8c2806Swdenk 	if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
914fe8c2806Swdenk 		st->st_mtime = 1;
915fe8c2806Swdenk 
91677ddac94SWolfgang Denk 	ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
917fe8c2806Swdenk 
918fe8c2806Swdenk 	if ((p = strchr(s,'\n')) != NULL) *p = '\0';
919fe8c2806Swdenk 	if ((p = strchr(s,'\r')) != NULL) *p = '\0';
920fe8c2806Swdenk 
921fe8c2806Swdenk /*
922fe8c2806Swdenk 	printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
923fe8c2806Swdenk 		st->st_size, s, name);
924fe8c2806Swdenk */
925fe8c2806Swdenk 
926fe8c2806Swdenk 	printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
927fe8c2806Swdenk }
928fe8c2806Swdenk 
929fe8c2806Swdenk static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
930fe8c2806Swdenk {
931fe8c2806Swdenk 	char fname[256];
932fe8c2806Swdenk 	struct stat st;
933fe8c2806Swdenk 
934fe8c2806Swdenk 	if(!d || !i) return -1;
935fe8c2806Swdenk 
93677ddac94SWolfgang Denk 	strncpy(fname, (char *)d->name, d->nsize);
937fe8c2806Swdenk 	fname[d->nsize] = '\0';
938fe8c2806Swdenk 
939fe8c2806Swdenk 	memset(&st,0,sizeof(st));
940fe8c2806Swdenk 
941fe8c2806Swdenk 	st.st_mtime = i->mtime;
942fe8c2806Swdenk 	st.st_mode = i->mode;
943fe8c2806Swdenk 	st.st_ino = i->ino;
944f7384695SIlya Yanok 	st.st_size = i->isize;
945fe8c2806Swdenk 
946fe8c2806Swdenk 	dump_stat(&st, fname);
947fe8c2806Swdenk 
948fe8c2806Swdenk 	if (d->type == DT_LNK) {
949fe8c2806Swdenk 		unsigned char *src = (unsigned char *) (&i[1]);
950fe8c2806Swdenk 	        putstr(" -> ");
951fe8c2806Swdenk 		putnstr(src, (int)i->dsize);
952fe8c2806Swdenk 	}
953fe8c2806Swdenk 
954fe8c2806Swdenk 	putstr("\r\n");
955fe8c2806Swdenk 
956fe8c2806Swdenk 	return 0;
957fe8c2806Swdenk }
958fe8c2806Swdenk 
959fe8c2806Swdenk /* list inodes with the given pino */
960fe8c2806Swdenk static u32
961fe8c2806Swdenk jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
962fe8c2806Swdenk {
963fe8c2806Swdenk 	struct b_node *b;
964fe8c2806Swdenk 	struct jffs2_raw_dirent *jDir;
965fe8c2806Swdenk 
96606d01dbeSwdenk 	for (b = pL->dir.listHead; b; b = b->next) {
96770741004SIlya Yanok 		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
96870741004SIlya Yanok 								pL->readbuf);
96906d01dbeSwdenk 		if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
970fe8c2806Swdenk 			u32 i_version = 0;
971998eaaecSwdenk 			struct jffs2_raw_inode ojNode;
972fe8c2806Swdenk 			struct jffs2_raw_inode *jNode, *i = NULL;
97306d01dbeSwdenk 			struct b_node *b2 = pL->frag.listHead;
974fe8c2806Swdenk 
975fe8c2806Swdenk 			while (b2) {
976998eaaecSwdenk 				jNode = (struct jffs2_raw_inode *)
977998eaaecSwdenk 					get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
97832877d66Swdenk 				if (jNode->ino == jDir->ino && jNode->version >= i_version) {
979f7384695SIlya Yanok 					i_version = jNode->version;
98032877d66Swdenk 					if (i)
98170741004SIlya Yanok 						put_fl_mem(i, NULL);
982cf8bc577Swdenk 
983cf8bc577Swdenk 					if (jDir->type == DT_LNK)
98470741004SIlya Yanok 						i = get_node_mem(b2->offset,
98570741004SIlya Yanok 								 NULL);
986cf8bc577Swdenk 					else
98770741004SIlya Yanok 						i = get_fl_mem(b2->offset,
98870741004SIlya Yanok 							       sizeof(*i),
98970741004SIlya Yanok 							       NULL);
99032877d66Swdenk 				}
991fe8c2806Swdenk 				b2 = b2->next;
992fe8c2806Swdenk 			}
993fe8c2806Swdenk 
994fe8c2806Swdenk 			dump_inode(pL, jDir, i);
99570741004SIlya Yanok 			put_fl_mem(i, NULL);
996fe8c2806Swdenk 		}
99770741004SIlya Yanok 		put_fl_mem(jDir, pL->readbuf);
998fe8c2806Swdenk 	}
999fe8c2806Swdenk 	return pino;
1000fe8c2806Swdenk }
1001fe8c2806Swdenk 
1002fe8c2806Swdenk static u32
1003fe8c2806Swdenk jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1004fe8c2806Swdenk {
1005fe8c2806Swdenk 	int i;
1006fe8c2806Swdenk 	char tmp[256];
1007fe8c2806Swdenk 	char working_tmp[256];
1008fe8c2806Swdenk 	char *c;
1009fe8c2806Swdenk 
1010fe8c2806Swdenk 	/* discard any leading slash */
1011fe8c2806Swdenk 	i = 0;
1012fe8c2806Swdenk 	while (fname[i] == '/')
1013fe8c2806Swdenk 		i++;
1014fe8c2806Swdenk 	strcpy(tmp, &fname[i]);
1015fe8c2806Swdenk 
1016fe8c2806Swdenk 	while ((c = (char *) strchr(tmp, '/')))	/* we are still dired searching */
1017fe8c2806Swdenk 	{
1018fe8c2806Swdenk 		strncpy(working_tmp, tmp, c - tmp);
1019fe8c2806Swdenk 		working_tmp[c - tmp] = '\0';
1020fe8c2806Swdenk #if 0
1021fe8c2806Swdenk 		putstr("search_inode: tmp = ");
1022fe8c2806Swdenk 		putstr(tmp);
1023fe8c2806Swdenk 		putstr("\r\n");
1024fe8c2806Swdenk 		putstr("search_inode: wtmp = ");
1025fe8c2806Swdenk 		putstr(working_tmp);
1026fe8c2806Swdenk 		putstr("\r\n");
1027fe8c2806Swdenk 		putstr("search_inode: c = ");
1028fe8c2806Swdenk 		putstr(c);
1029fe8c2806Swdenk 		putstr("\r\n");
1030fe8c2806Swdenk #endif
1031fe8c2806Swdenk 		for (i = 0; i < strlen(c) - 1; i++)
1032fe8c2806Swdenk 			tmp[i] = c[i + 1];
1033fe8c2806Swdenk 		tmp[i] = '\0';
1034fe8c2806Swdenk #if 0
1035fe8c2806Swdenk 		putstr("search_inode: post tmp = ");
1036fe8c2806Swdenk 		putstr(tmp);
1037fe8c2806Swdenk 		putstr("\r\n");
1038fe8c2806Swdenk #endif
1039fe8c2806Swdenk 
1040fe8c2806Swdenk 		if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1041fe8c2806Swdenk 			putstr("find_inode failed for name=");
1042fe8c2806Swdenk 			putstr(working_tmp);
1043fe8c2806Swdenk 			putstr("\r\n");
1044fe8c2806Swdenk 			return 0;
1045fe8c2806Swdenk 		}
1046fe8c2806Swdenk 	}
1047fe8c2806Swdenk 	/* this is for the bare filename, directories have already been mapped */
1048fe8c2806Swdenk 	if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1049fe8c2806Swdenk 		putstr("find_inode failed for name=");
1050fe8c2806Swdenk 		putstr(tmp);
1051fe8c2806Swdenk 		putstr("\r\n");
1052fe8c2806Swdenk 		return 0;
1053fe8c2806Swdenk 	}
1054fe8c2806Swdenk 	return pino;
1055fe8c2806Swdenk 
1056fe8c2806Swdenk }
1057fe8c2806Swdenk 
1058fe8c2806Swdenk static u32
1059fe8c2806Swdenk jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1060fe8c2806Swdenk {
1061fe8c2806Swdenk 	struct b_node *b;
1062fe8c2806Swdenk 	struct b_node *b2;
1063fe8c2806Swdenk 	struct jffs2_raw_dirent *jDir;
1064fe8c2806Swdenk 	struct jffs2_raw_inode *jNode;
1065998eaaecSwdenk 	u8 jDirFoundType = 0;
1066998eaaecSwdenk 	u32 jDirFoundIno = 0;
1067998eaaecSwdenk 	u32 jDirFoundPino = 0;
1068fe8c2806Swdenk 	char tmp[256];
1069fe8c2806Swdenk 	u32 version = 0;
1070fe8c2806Swdenk 	u32 pino;
1071fe8c2806Swdenk 	unsigned char *src;
1072fe8c2806Swdenk 
1073fe8c2806Swdenk 	/* we need to search all and return the inode with the highest version */
107406d01dbeSwdenk 	for(b = pL->dir.listHead; b; b = b->next) {
107570741004SIlya Yanok 		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
107670741004SIlya Yanok 								pL->readbuf);
1077fe8c2806Swdenk 		if (ino == jDir->ino) {
1078998eaaecSwdenk 			if (jDir->version < version) {
107970741004SIlya Yanok 				put_fl_mem(jDir, pL->readbuf);
10807205e407Swdenk 				continue;
1081998eaaecSwdenk 			}
1082fe8c2806Swdenk 
1083998eaaecSwdenk 			if (jDir->version == version && jDirFoundType) {
10847205e407Swdenk 				/* I'm pretty sure this isn't legal */
1085fe8c2806Swdenk 				putstr(" ** ERROR ** ");
1086fe8c2806Swdenk 				putnstr(jDir->name, jDir->nsize);
1087fe8c2806Swdenk 				putLabeledWord(" has dup version (resolve) = ",
1088fe8c2806Swdenk 					version);
1089fe8c2806Swdenk 			}
1090fe8c2806Swdenk 
1091998eaaecSwdenk 			jDirFoundType = jDir->type;
1092998eaaecSwdenk 			jDirFoundIno = jDir->ino;
1093998eaaecSwdenk 			jDirFoundPino = jDir->pino;
1094fe8c2806Swdenk 			version = jDir->version;
1095fe8c2806Swdenk 		}
109670741004SIlya Yanok 		put_fl_mem(jDir, pL->readbuf);
1097fe8c2806Swdenk 	}
1098fe8c2806Swdenk 	/* now we found the right entry again. (shoulda returned inode*) */
1099998eaaecSwdenk 	if (jDirFoundType != DT_LNK)
1100998eaaecSwdenk 		return jDirFoundIno;
110106d01dbeSwdenk 
110206d01dbeSwdenk 	/* it's a soft link so we follow it again. */
110306d01dbeSwdenk 	b2 = pL->frag.listHead;
1104fe8c2806Swdenk 	while (b2) {
110570741004SIlya Yanok 		jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
110670741004SIlya Yanok 								pL->readbuf);
1107998eaaecSwdenk 		if (jNode->ino == jDirFoundIno) {
11082729af9dSwdenk 			src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
1109fe8c2806Swdenk 
1110fe8c2806Swdenk #if 0
1111fe8c2806Swdenk 			putLabeledWord("\t\t dsize = ", jNode->dsize);
1112fe8c2806Swdenk 			putstr("\t\t target = ");
1113fe8c2806Swdenk 			putnstr(src, jNode->dsize);
1114fe8c2806Swdenk 			putstr("\r\n");
1115fe8c2806Swdenk #endif
111677ddac94SWolfgang Denk 			strncpy(tmp, (char *)src, jNode->dsize);
1117fe8c2806Swdenk 			tmp[jNode->dsize] = '\0';
111870741004SIlya Yanok 			put_fl_mem(jNode, pL->readbuf);
1119fe8c2806Swdenk 			break;
1120fe8c2806Swdenk 		}
1121fe8c2806Swdenk 		b2 = b2->next;
112270741004SIlya Yanok 		put_fl_mem(jNode, pL->readbuf);
1123fe8c2806Swdenk 	}
1124fe8c2806Swdenk 	/* ok so the name of the new file to find is in tmp */
1125fe8c2806Swdenk 	/* if it starts with a slash it is root based else shared dirs */
1126fe8c2806Swdenk 	if (tmp[0] == '/')
1127fe8c2806Swdenk 		pino = 1;
1128fe8c2806Swdenk 	else
1129998eaaecSwdenk 		pino = jDirFoundPino;
1130fe8c2806Swdenk 
1131fe8c2806Swdenk 	return jffs2_1pass_search_inode(pL, tmp, pino);
1132fe8c2806Swdenk }
1133fe8c2806Swdenk 
1134fe8c2806Swdenk static u32
1135fe8c2806Swdenk jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1136fe8c2806Swdenk {
1137fe8c2806Swdenk 	int i;
1138fe8c2806Swdenk 	char tmp[256];
1139fe8c2806Swdenk 	char working_tmp[256];
1140fe8c2806Swdenk 	char *c;
1141fe8c2806Swdenk 
1142fe8c2806Swdenk 	/* discard any leading slash */
1143fe8c2806Swdenk 	i = 0;
1144fe8c2806Swdenk 	while (fname[i] == '/')
1145fe8c2806Swdenk 		i++;
1146fe8c2806Swdenk 	strcpy(tmp, &fname[i]);
1147fe8c2806Swdenk 	working_tmp[0] = '\0';
1148fe8c2806Swdenk 	while ((c = (char *) strchr(tmp, '/')))	/* we are still dired searching */
1149fe8c2806Swdenk 	{
1150fe8c2806Swdenk 		strncpy(working_tmp, tmp, c - tmp);
1151fe8c2806Swdenk 		working_tmp[c - tmp] = '\0';
1152fe8c2806Swdenk 		for (i = 0; i < strlen(c) - 1; i++)
1153fe8c2806Swdenk 			tmp[i] = c[i + 1];
1154fe8c2806Swdenk 		tmp[i] = '\0';
1155fe8c2806Swdenk 		/* only a failure if we arent looking at top level */
115606d01dbeSwdenk 		if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
115706d01dbeSwdenk 		    (working_tmp[0])) {
1158fe8c2806Swdenk 			putstr("find_inode failed for name=");
1159fe8c2806Swdenk 			putstr(working_tmp);
1160fe8c2806Swdenk 			putstr("\r\n");
1161fe8c2806Swdenk 			return 0;
1162fe8c2806Swdenk 		}
1163fe8c2806Swdenk 	}
1164fe8c2806Swdenk 
1165fe8c2806Swdenk 	if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1166fe8c2806Swdenk 		putstr("find_inode failed for name=");
1167fe8c2806Swdenk 		putstr(tmp);
1168fe8c2806Swdenk 		putstr("\r\n");
1169fe8c2806Swdenk 		return 0;
1170fe8c2806Swdenk 	}
1171fe8c2806Swdenk 	/* this is for the bare filename, directories have already been mapped */
1172fe8c2806Swdenk 	if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1173fe8c2806Swdenk 		putstr("find_inode failed for name=");
1174fe8c2806Swdenk 		putstr(tmp);
1175fe8c2806Swdenk 		putstr("\r\n");
1176fe8c2806Swdenk 		return 0;
1177fe8c2806Swdenk 	}
1178fe8c2806Swdenk 	return pino;
1179fe8c2806Swdenk 
1180fe8c2806Swdenk }
1181fe8c2806Swdenk 
1182fe8c2806Swdenk unsigned char
1183fe8c2806Swdenk jffs2_1pass_rescan_needed(struct part_info *part)
1184fe8c2806Swdenk {
1185fe8c2806Swdenk 	struct b_node *b;
1186998eaaecSwdenk 	struct jffs2_unknown_node onode;
1187fe8c2806Swdenk 	struct jffs2_unknown_node *node;
1188fe8c2806Swdenk 	struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
1189fe8c2806Swdenk 
1190fe8c2806Swdenk 	if (part->jffs2_priv == 0){
1191fe8c2806Swdenk 		DEBUGF ("rescan: First time in use\n");
1192fe8c2806Swdenk 		return 1;
1193fe8c2806Swdenk 	}
1194700a0c64SWolfgang Denk 
1195fe8c2806Swdenk 	/* if we have no list, we need to rescan */
119606d01dbeSwdenk 	if (pL->frag.listCount == 0) {
1197fe8c2806Swdenk 		DEBUGF ("rescan: fraglist zero\n");
1198fe8c2806Swdenk 		return 1;
1199fe8c2806Swdenk 	}
1200fe8c2806Swdenk 
120106d01dbeSwdenk 	/* but suppose someone reflashed a partition at the same offset... */
120206d01dbeSwdenk 	b = pL->dir.listHead;
1203fe8c2806Swdenk 	while (b) {
1204998eaaecSwdenk 		node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1205998eaaecSwdenk 			sizeof(onode), &onode);
1206fe8c2806Swdenk 		if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
120706d01dbeSwdenk 			DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
120806d01dbeSwdenk 					(unsigned long) b->offset);
1209fe8c2806Swdenk 			return 1;
1210fe8c2806Swdenk 		}
1211fe8c2806Swdenk 		b = b->next;
1212fe8c2806Swdenk 	}
1213fe8c2806Swdenk 	return 0;
1214fe8c2806Swdenk }
1215fe8c2806Swdenk 
12168cf19b9fSIlya Yanok #ifdef CONFIG_JFFS2_SUMMARY
12178cf19b9fSIlya Yanok static u32 sum_get_unaligned32(u32 *ptr)
12188cf19b9fSIlya Yanok {
12198cf19b9fSIlya Yanok 	u32 val;
12208cf19b9fSIlya Yanok 	u8 *p = (u8 *)ptr;
12218cf19b9fSIlya Yanok 
12228cf19b9fSIlya Yanok 	val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
12238cf19b9fSIlya Yanok 
12248cf19b9fSIlya Yanok 	return __le32_to_cpu(val);
12258cf19b9fSIlya Yanok }
12268cf19b9fSIlya Yanok 
12278cf19b9fSIlya Yanok static u16 sum_get_unaligned16(u16 *ptr)
12288cf19b9fSIlya Yanok {
12298cf19b9fSIlya Yanok 	u16 val;
12308cf19b9fSIlya Yanok 	u8 *p = (u8 *)ptr;
12318cf19b9fSIlya Yanok 
12328cf19b9fSIlya Yanok 	val = *p | (*(p + 1) << 8);
12338cf19b9fSIlya Yanok 
12348cf19b9fSIlya Yanok 	return __le16_to_cpu(val);
12358cf19b9fSIlya Yanok }
12368cf19b9fSIlya Yanok 
12379b707622SIlya Yanok #define dbg_summary(...) do {} while (0);
12388cf19b9fSIlya Yanok /*
12398cf19b9fSIlya Yanok  * Process the stored summary information - helper function for
12409b707622SIlya Yanok  * jffs2_sum_scan_sumnode()
12419b707622SIlya Yanok  */
12429b707622SIlya Yanok 
12439b707622SIlya Yanok static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
12449b707622SIlya Yanok 				struct jffs2_raw_summary *summary,
12459b707622SIlya Yanok 				struct b_lists *pL)
12469b707622SIlya Yanok {
12479b707622SIlya Yanok 	void *sp;
12488cf19b9fSIlya Yanok 	int i, pass;
12498cf19b9fSIlya Yanok 	void *ret;
12509b707622SIlya Yanok 
12518cf19b9fSIlya Yanok 	for (pass = 0; pass < 2; pass++) {
12529b707622SIlya Yanok 		sp = summary->sum;
12539b707622SIlya Yanok 
12549b707622SIlya Yanok 		for (i = 0; i < summary->sum_num; i++) {
12558cf19b9fSIlya Yanok 			struct jffs2_sum_unknown_flash *spu = sp;
12569b707622SIlya Yanok 			dbg_summary("processing summary index %d\n", i);
12579b707622SIlya Yanok 
12588cf19b9fSIlya Yanok 			switch (sum_get_unaligned16(&spu->nodetype)) {
12599b707622SIlya Yanok 				case JFFS2_NODETYPE_INODE: {
12609b707622SIlya Yanok 				struct jffs2_sum_inode_flash *spi;
12618cf19b9fSIlya Yanok 					if (pass) {
12629b707622SIlya Yanok 						spi = sp;
12639b707622SIlya Yanok 
12648cf19b9fSIlya Yanok 						ret = insert_node(&pL->frag,
12658cf19b9fSIlya Yanok 							(u32)part->offset +
12668cf19b9fSIlya Yanok 							offset +
12678cf19b9fSIlya Yanok 							sum_get_unaligned32(
12688cf19b9fSIlya Yanok 								&spi->offset));
12698cf19b9fSIlya Yanok 						if (ret == NULL)
12709b707622SIlya Yanok 							return -1;
12718cf19b9fSIlya Yanok 					}
12729b707622SIlya Yanok 
12739b707622SIlya Yanok 					sp += JFFS2_SUMMARY_INODE_SIZE;
12749b707622SIlya Yanok 
12759b707622SIlya Yanok 					break;
12769b707622SIlya Yanok 				}
12779b707622SIlya Yanok 				case JFFS2_NODETYPE_DIRENT: {
12789b707622SIlya Yanok 					struct jffs2_sum_dirent_flash *spd;
12799b707622SIlya Yanok 					spd = sp;
12808cf19b9fSIlya Yanok 					if (pass) {
12818cf19b9fSIlya Yanok 						ret = insert_node(&pL->dir,
12828cf19b9fSIlya Yanok 							(u32) part->offset +
12838cf19b9fSIlya Yanok 							offset +
12848cf19b9fSIlya Yanok 							sum_get_unaligned32(
12858cf19b9fSIlya Yanok 								&spd->offset));
12868cf19b9fSIlya Yanok 						if (ret == NULL)
12879b707622SIlya Yanok 							return -1;
12888cf19b9fSIlya Yanok 					}
12899b707622SIlya Yanok 
12908cf19b9fSIlya Yanok 					sp += JFFS2_SUMMARY_DIRENT_SIZE(
12918cf19b9fSIlya Yanok 							spd->nsize);
12929b707622SIlya Yanok 
12939b707622SIlya Yanok 					break;
12949b707622SIlya Yanok 				}
12959b707622SIlya Yanok 				default : {
12968cf19b9fSIlya Yanok 					uint16_t nodetype = sum_get_unaligned16(
12978cf19b9fSIlya Yanok 								&spu->nodetype);
12988cf19b9fSIlya Yanok 					printf("Unsupported node type %x found"
12998cf19b9fSIlya Yanok 							" in summary!\n",
13008cf19b9fSIlya Yanok 							nodetype);
13018cf19b9fSIlya Yanok 					if ((nodetype & JFFS2_COMPAT_MASK) ==
13028cf19b9fSIlya Yanok 							JFFS2_FEATURE_INCOMPAT)
13038cf19b9fSIlya Yanok 						return -EIO;
13048cf19b9fSIlya Yanok 					return -EBADMSG;
13058cf19b9fSIlya Yanok 				}
13069b707622SIlya Yanok 			}
13079b707622SIlya Yanok 		}
13089b707622SIlya Yanok 	}
13099b707622SIlya Yanok 	return 0;
13109b707622SIlya Yanok }
13119b707622SIlya Yanok 
13129b707622SIlya Yanok /* Process the summary node - called from jffs2_scan_eraseblock() */
13139b707622SIlya Yanok int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
13149b707622SIlya Yanok 			   struct jffs2_raw_summary *summary, uint32_t sumsize,
13159b707622SIlya Yanok 			   struct b_lists *pL)
13169b707622SIlya Yanok {
13179b707622SIlya Yanok 	struct jffs2_unknown_node crcnode;
13189b707622SIlya Yanok 	int ret, ofs;
13199b707622SIlya Yanok 	uint32_t crc;
13209b707622SIlya Yanok 
13219b707622SIlya Yanok 	ofs = part->sector_size - sumsize;
13229b707622SIlya Yanok 
13239b707622SIlya Yanok 	dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
13249b707622SIlya Yanok 		    offset, offset + ofs, sumsize);
13259b707622SIlya Yanok 
13269b707622SIlya Yanok 	/* OK, now check for node validity and CRC */
13279b707622SIlya Yanok 	crcnode.magic = JFFS2_MAGIC_BITMASK;
13289b707622SIlya Yanok 	crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
13299b707622SIlya Yanok 	crcnode.totlen = summary->totlen;
13309b707622SIlya Yanok 	crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
13319b707622SIlya Yanok 
13329b707622SIlya Yanok 	if (summary->hdr_crc != crc) {
13339b707622SIlya Yanok 		dbg_summary("Summary node header is corrupt (bad CRC or "
13349b707622SIlya Yanok 				"no summary at all)\n");
13359b707622SIlya Yanok 		goto crc_err;
13369b707622SIlya Yanok 	}
13379b707622SIlya Yanok 
13389b707622SIlya Yanok 	if (summary->totlen != sumsize) {
13399b707622SIlya Yanok 		dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
13409b707622SIlya Yanok 		goto crc_err;
13419b707622SIlya Yanok 	}
13429b707622SIlya Yanok 
13439b707622SIlya Yanok 	crc = crc32_no_comp(0, (uchar *)summary,
13449b707622SIlya Yanok 			sizeof(struct jffs2_raw_summary)-8);
13459b707622SIlya Yanok 
13469b707622SIlya Yanok 	if (summary->node_crc != crc) {
13479b707622SIlya Yanok 		dbg_summary("Summary node is corrupt (bad CRC)\n");
13489b707622SIlya Yanok 		goto crc_err;
13499b707622SIlya Yanok 	}
13509b707622SIlya Yanok 
13519b707622SIlya Yanok 	crc = crc32_no_comp(0, (uchar *)summary->sum,
13529b707622SIlya Yanok 			sumsize - sizeof(struct jffs2_raw_summary));
13539b707622SIlya Yanok 
13549b707622SIlya Yanok 	if (summary->sum_crc != crc) {
13559b707622SIlya Yanok 		dbg_summary("Summary node data is corrupt (bad CRC)\n");
13569b707622SIlya Yanok 		goto crc_err;
13579b707622SIlya Yanok 	}
13589b707622SIlya Yanok 
13599b707622SIlya Yanok 	if (summary->cln_mkr)
13609b707622SIlya Yanok 		dbg_summary("Summary : CLEANMARKER node \n");
13619b707622SIlya Yanok 
13629b707622SIlya Yanok 	ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
13638cf19b9fSIlya Yanok 	if (ret == -EBADMSG)
13648cf19b9fSIlya Yanok 		return 0;
13659b707622SIlya Yanok 	if (ret)
13669b707622SIlya Yanok 		return ret;		/* real error */
13679b707622SIlya Yanok 
13689b707622SIlya Yanok 	return 1;
13699b707622SIlya Yanok 
13709b707622SIlya Yanok crc_err:
13719b707622SIlya Yanok 	putstr("Summary node crc error, skipping summary information.\n");
13729b707622SIlya Yanok 
13739b707622SIlya Yanok 	return 0;
13749b707622SIlya Yanok }
13758cf19b9fSIlya Yanok #endif /* CONFIG_JFFS2_SUMMARY */
13769b707622SIlya Yanok 
137706d01dbeSwdenk #ifdef DEBUG_FRAGMENTS
137806d01dbeSwdenk static void
137906d01dbeSwdenk dump_fragments(struct b_lists *pL)
1380fe8c2806Swdenk {
1381fe8c2806Swdenk 	struct b_node *b;
1382998eaaecSwdenk 	struct jffs2_raw_inode ojNode;
1383fe8c2806Swdenk 	struct jffs2_raw_inode *jNode;
1384fe8c2806Swdenk 
138506d01dbeSwdenk 	putstr("\r\n\r\n******The fragment Entries******\r\n");
138606d01dbeSwdenk 	b = pL->frag.listHead;
138706d01dbeSwdenk 	while (b) {
1388998eaaecSwdenk 		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1389998eaaecSwdenk 			sizeof(ojNode), &ojNode);
139006d01dbeSwdenk 		putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
139106d01dbeSwdenk 		putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
139206d01dbeSwdenk 		putLabeledWord("\tbuild_list: inode = ", jNode->ino);
139306d01dbeSwdenk 		putLabeledWord("\tbuild_list: version = ", jNode->version);
139406d01dbeSwdenk 		putLabeledWord("\tbuild_list: isize = ", jNode->isize);
139506d01dbeSwdenk 		putLabeledWord("\tbuild_list: atime = ", jNode->atime);
139606d01dbeSwdenk 		putLabeledWord("\tbuild_list: offset = ", jNode->offset);
139706d01dbeSwdenk 		putLabeledWord("\tbuild_list: csize = ", jNode->csize);
139806d01dbeSwdenk 		putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
139906d01dbeSwdenk 		putLabeledWord("\tbuild_list: compr = ", jNode->compr);
140006d01dbeSwdenk 		putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
140106d01dbeSwdenk 		putLabeledWord("\tbuild_list: flags = ", jNode->flags);
14028bde7f77Swdenk 		putLabeledWord("\tbuild_list: offset = ", b->offset);	/* FIXME: ? [RS] */
140306d01dbeSwdenk 		b = b->next;
140406d01dbeSwdenk 	}
140506d01dbeSwdenk }
140606d01dbeSwdenk #endif
140706d01dbeSwdenk 
140806d01dbeSwdenk #ifdef DEBUG_DIRENTS
140906d01dbeSwdenk static void
141006d01dbeSwdenk dump_dirents(struct b_lists *pL)
141106d01dbeSwdenk {
141206d01dbeSwdenk 	struct b_node *b;
141306d01dbeSwdenk 	struct jffs2_raw_dirent *jDir;
141406d01dbeSwdenk 
1415fe8c2806Swdenk 	putstr("\r\n\r\n******The directory Entries******\r\n");
141606d01dbeSwdenk 	b = pL->dir.listHead;
1417fe8c2806Swdenk 	while (b) {
141870741004SIlya Yanok 		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
141970741004SIlya Yanok 								pL->readbuf);
1420fe8c2806Swdenk 		putstr("\r\n");
1421fe8c2806Swdenk 		putnstr(jDir->name, jDir->nsize);
1422fe8c2806Swdenk 		putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1423fe8c2806Swdenk 		putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1424fe8c2806Swdenk 		putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1425fe8c2806Swdenk 		putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1426fe8c2806Swdenk 		putLabeledWord("\tbuild_list: version = ", jDir->version);
1427fe8c2806Swdenk 		putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1428fe8c2806Swdenk 		putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1429fe8c2806Swdenk 		putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1430fe8c2806Swdenk 		putLabeledWord("\tbuild_list: type = ", jDir->type);
1431fe8c2806Swdenk 		putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1432fe8c2806Swdenk 		putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
14338bde7f77Swdenk 		putLabeledWord("\tbuild_list: offset = ", b->offset);	/* FIXME: ? [RS] */
1434fe8c2806Swdenk 		b = b->next;
143570741004SIlya Yanok 		put_fl_mem(jDir, pL->readbuf);
1436fe8c2806Swdenk 	}
1437fe8c2806Swdenk }
143806d01dbeSwdenk #endif
143906d01dbeSwdenk 
14408a36d31fSIlya Yanok #define DEFAULT_EMPTY_SCAN_SIZE	4096
14418a36d31fSIlya Yanok 
14428a36d31fSIlya Yanok static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
14438a36d31fSIlya Yanok {
14448a36d31fSIlya Yanok 	if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
14458a36d31fSIlya Yanok 		return sector_size;
14468a36d31fSIlya Yanok 	else
14478a36d31fSIlya Yanok 		return DEFAULT_EMPTY_SCAN_SIZE;
14488a36d31fSIlya Yanok }
14498a36d31fSIlya Yanok 
145006d01dbeSwdenk static u32
145106d01dbeSwdenk jffs2_1pass_build_lists(struct part_info * part)
145206d01dbeSwdenk {
145306d01dbeSwdenk 	struct b_lists *pL;
145406d01dbeSwdenk 	struct jffs2_unknown_node *node;
145518e86724STom Rini 	u32 nr_sectors;
14568a36d31fSIlya Yanok 	u32 i;
145706d01dbeSwdenk 	u32 counter4 = 0;
145806d01dbeSwdenk 	u32 counterF = 0;
145906d01dbeSwdenk 	u32 counterN = 0;
146070741004SIlya Yanok 	u32 max_totlen = 0;
14618a36d31fSIlya Yanok 	u32 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
14628a36d31fSIlya Yanok 	char *buf;
146306d01dbeSwdenk 
146418e86724STom Rini 	nr_sectors = lldiv(part->size, part->sector_size);
146506d01dbeSwdenk 	/* turn off the lcd.  Refreshing the lcd adds 50% overhead to the */
146606d01dbeSwdenk 	/* jffs2 list building enterprise nope.  in newer versions the overhead is */
146706d01dbeSwdenk 	/* only about 5 %.  not enough to inconvenience people for. */
146806d01dbeSwdenk 	/* lcd_off(); */
146906d01dbeSwdenk 
147006d01dbeSwdenk 	/* if we are building a list we need to refresh the cache. */
147106d01dbeSwdenk 	jffs_init_1pass_list(part);
147206d01dbeSwdenk 	pL = (struct b_lists *)part->jffs2_priv;
14738a36d31fSIlya Yanok 	buf = malloc(buf_size);
14744b9206edSwdenk 	puts ("Scanning JFFS2 FS:   ");
147506d01dbeSwdenk 
147606d01dbeSwdenk 	/* start at the beginning of the partition */
14778a36d31fSIlya Yanok 	for (i = 0; i < nr_sectors; i++) {
14788a36d31fSIlya Yanok 		uint32_t sector_ofs = i * part->sector_size;
14798a36d31fSIlya Yanok 		uint32_t buf_ofs = sector_ofs;
14809b707622SIlya Yanok 		uint32_t buf_len;
14818a36d31fSIlya Yanok 		uint32_t ofs, prevofs;
14828cf19b9fSIlya Yanok #ifdef CONFIG_JFFS2_SUMMARY
14839b707622SIlya Yanok 		struct jffs2_sum_marker *sm;
14849b707622SIlya Yanok 		void *sumptr = NULL;
14859b707622SIlya Yanok 		uint32_t sumlen;
14869b707622SIlya Yanok 		int ret;
14878cf19b9fSIlya Yanok #endif
14880b8fa03bSwdenk 
148986d3273eSStuart Wood 		WATCHDOG_RESET();
14909b707622SIlya Yanok 
14918cf19b9fSIlya Yanok #ifdef CONFIG_JFFS2_SUMMARY
14929b707622SIlya Yanok 		buf_len = sizeof(*sm);
14939b707622SIlya Yanok 
14949b707622SIlya Yanok 		/* Read as much as we want into the _end_ of the preallocated
14959b707622SIlya Yanok 		 * buffer
14969b707622SIlya Yanok 		 */
14979b707622SIlya Yanok 		get_fl_mem(part->offset + sector_ofs + part->sector_size -
14989b707622SIlya Yanok 				buf_len, buf_len, buf + buf_size - buf_len);
14999b707622SIlya Yanok 
15009b707622SIlya Yanok 		sm = (void *)buf + buf_size - sizeof(*sm);
15019b707622SIlya Yanok 		if (sm->magic == JFFS2_SUM_MAGIC) {
15029b707622SIlya Yanok 			sumlen = part->sector_size - sm->offset;
15039b707622SIlya Yanok 			sumptr = buf + buf_size - sumlen;
15049b707622SIlya Yanok 
15059b707622SIlya Yanok 			/* Now, make sure the summary itself is available */
15069b707622SIlya Yanok 			if (sumlen > buf_size) {
15079b707622SIlya Yanok 				/* Need to kmalloc for this. */
15089b707622SIlya Yanok 				sumptr = malloc(sumlen);
15099b707622SIlya Yanok 				if (!sumptr) {
15109b707622SIlya Yanok 					putstr("Can't get memory for summary "
15119b707622SIlya Yanok 							"node!\n");
1512b644006eSIlya Yanok 					free(buf);
1513b644006eSIlya Yanok 					jffs2_free_cache(part);
15149b707622SIlya Yanok 					return 0;
15159b707622SIlya Yanok 				}
15169b707622SIlya Yanok 				memcpy(sumptr + sumlen - buf_len, buf +
15179b707622SIlya Yanok 						buf_size - buf_len, buf_len);
15189b707622SIlya Yanok 			}
15199b707622SIlya Yanok 			if (buf_len < sumlen) {
15209b707622SIlya Yanok 				/* Need to read more so that the entire summary
15219b707622SIlya Yanok 				 * node is present
15229b707622SIlya Yanok 				 */
15239b707622SIlya Yanok 				get_fl_mem(part->offset + sector_ofs +
15249b707622SIlya Yanok 						part->sector_size - sumlen,
15259b707622SIlya Yanok 						sumlen - buf_len, sumptr);
15269b707622SIlya Yanok 			}
15279b707622SIlya Yanok 		}
15289b707622SIlya Yanok 
15299b707622SIlya Yanok 		if (sumptr) {
15309b707622SIlya Yanok 			ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
15319b707622SIlya Yanok 					sumlen, pL);
15329b707622SIlya Yanok 
15339b707622SIlya Yanok 			if (buf_size && sumlen > buf_size)
15349b707622SIlya Yanok 				free(sumptr);
1535b644006eSIlya Yanok 			if (ret < 0) {
1536b644006eSIlya Yanok 				free(buf);
1537b644006eSIlya Yanok 				jffs2_free_cache(part);
15389b707622SIlya Yanok 				return 0;
1539b644006eSIlya Yanok 			}
15409b707622SIlya Yanok 			if (ret)
15419b707622SIlya Yanok 				continue;
15429b707622SIlya Yanok 
15439b707622SIlya Yanok 		}
15448cf19b9fSIlya Yanok #endif /* CONFIG_JFFS2_SUMMARY */
15459b707622SIlya Yanok 
15469b707622SIlya Yanok 		buf_len = EMPTY_SCAN_SIZE(part->sector_size);
15479b707622SIlya Yanok 
15488a36d31fSIlya Yanok 		get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
154986d3273eSStuart Wood 
15508a36d31fSIlya Yanok 		/* We temporarily use 'ofs' as a pointer into the buffer/jeb */
15518a36d31fSIlya Yanok 		ofs = 0;
15528a36d31fSIlya Yanok 
15538a36d31fSIlya Yanok 		/* Scan only 4KiB of 0xFF before declaring it's empty */
15548a36d31fSIlya Yanok 		while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
15558a36d31fSIlya Yanok 				*(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
15568a36d31fSIlya Yanok 			ofs += 4;
15578a36d31fSIlya Yanok 
15588a36d31fSIlya Yanok 		if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
15598a36d31fSIlya Yanok 			continue;
15608a36d31fSIlya Yanok 
15618a36d31fSIlya Yanok 		ofs += sector_ofs;
15628a36d31fSIlya Yanok 		prevofs = ofs - 1;
15638a36d31fSIlya Yanok 
15648a36d31fSIlya Yanok 	scan_more:
15658a36d31fSIlya Yanok 		while (ofs < sector_ofs + part->sector_size) {
15668a36d31fSIlya Yanok 			if (ofs == prevofs) {
15678a36d31fSIlya Yanok 				printf("offset %08x already seen, skip\n", ofs);
15688a36d31fSIlya Yanok 				ofs += 4;
15698a36d31fSIlya Yanok 				counter4++;
15708a36d31fSIlya Yanok 				continue;
1571998eaaecSwdenk 			}
15728a36d31fSIlya Yanok 			prevofs = ofs;
15738a36d31fSIlya Yanok 			if (sector_ofs + part->sector_size <
15748a36d31fSIlya Yanok 					ofs + sizeof(*node))
15758a36d31fSIlya Yanok 				break;
15768a36d31fSIlya Yanok 			if (buf_ofs + buf_len < ofs + sizeof(*node)) {
15778a36d31fSIlya Yanok 				buf_len = min_t(uint32_t, buf_size, sector_ofs
15788a36d31fSIlya Yanok 						+ part->sector_size - ofs);
15798a36d31fSIlya Yanok 				get_fl_mem((u32)part->offset + ofs, buf_len,
15808a36d31fSIlya Yanok 					   buf);
15818a36d31fSIlya Yanok 				buf_ofs = ofs;
15828a36d31fSIlya Yanok 			}
15838a36d31fSIlya Yanok 
15848a36d31fSIlya Yanok 			node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
15858a36d31fSIlya Yanok 
15868a36d31fSIlya Yanok 			if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
15878a36d31fSIlya Yanok 				uint32_t inbuf_ofs;
158816b9afd2SWolfgang Denk 				uint32_t scan_end;
15898a36d31fSIlya Yanok 
15908a36d31fSIlya Yanok 				ofs += 4;
15918a36d31fSIlya Yanok 				scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
15928a36d31fSIlya Yanok 							part->sector_size)/8,
15938a36d31fSIlya Yanok 							buf_len);
15948a36d31fSIlya Yanok 			more_empty:
15958a36d31fSIlya Yanok 				inbuf_ofs = ofs - buf_ofs;
15968a36d31fSIlya Yanok 				while (inbuf_ofs < scan_end) {
15978a36d31fSIlya Yanok 					if (*(uint32_t *)(&buf[inbuf_ofs]) !=
15988a36d31fSIlya Yanok 							0xffffffff)
15998a36d31fSIlya Yanok 						goto scan_more;
16008a36d31fSIlya Yanok 
16018a36d31fSIlya Yanok 					inbuf_ofs += 4;
16028a36d31fSIlya Yanok 					ofs += 4;
16038a36d31fSIlya Yanok 				}
16048a36d31fSIlya Yanok 				/* Ran off end. */
16058a36d31fSIlya Yanok 
16068a36d31fSIlya Yanok 				/* See how much more there is to read in this
16078a36d31fSIlya Yanok 				 * eraseblock...
16088a36d31fSIlya Yanok 				 */
16098a36d31fSIlya Yanok 				buf_len = min_t(uint32_t, buf_size,
16108a36d31fSIlya Yanok 						sector_ofs +
16118a36d31fSIlya Yanok 						part->sector_size - ofs);
16128a36d31fSIlya Yanok 				if (!buf_len) {
16138a36d31fSIlya Yanok 					/* No more to read. Break out of main
16148a36d31fSIlya Yanok 					 * loop without marking this range of
16158a36d31fSIlya Yanok 					 * empty space as dirty (because it's
16168a36d31fSIlya Yanok 					 * not)
16178a36d31fSIlya Yanok 					 */
16188a36d31fSIlya Yanok 					break;
16198a36d31fSIlya Yanok 				}
16208a36d31fSIlya Yanok 				scan_end = buf_len;
16218a36d31fSIlya Yanok 				get_fl_mem((u32)part->offset + ofs, buf_len,
16228a36d31fSIlya Yanok 					   buf);
16238a36d31fSIlya Yanok 				buf_ofs = ofs;
16248a36d31fSIlya Yanok 				goto more_empty;
16258a36d31fSIlya Yanok 			}
16268a36d31fSIlya Yanok 			if (node->magic != JFFS2_MAGIC_BITMASK ||
16278a36d31fSIlya Yanok 					!hdr_crc(node)) {
16288a36d31fSIlya Yanok 				ofs += 4;
16298a36d31fSIlya Yanok 				counter4++;
16308a36d31fSIlya Yanok 				continue;
16318a36d31fSIlya Yanok 			}
16328a36d31fSIlya Yanok 			if (ofs + node->totlen >
16338a36d31fSIlya Yanok 					sector_ofs + part->sector_size) {
16348a36d31fSIlya Yanok 				ofs += 4;
16358a36d31fSIlya Yanok 				counter4++;
16368a36d31fSIlya Yanok 				continue;
16378a36d31fSIlya Yanok 			}
16388a36d31fSIlya Yanok 			/* if its a fragment add it */
16398a36d31fSIlya Yanok 			switch (node->nodetype) {
16408a36d31fSIlya Yanok 			case JFFS2_NODETYPE_INODE:
16418a36d31fSIlya Yanok 				if (buf_ofs + buf_len < ofs + sizeof(struct
16428a36d31fSIlya Yanok 							jffs2_raw_inode)) {
16438a36d31fSIlya Yanok 					get_fl_mem((u32)part->offset + ofs,
16448a36d31fSIlya Yanok 						   buf_len, buf);
16458a36d31fSIlya Yanok 					buf_ofs = ofs;
16468a36d31fSIlya Yanok 					node = (void *)buf;
16478a36d31fSIlya Yanok 				}
16488a36d31fSIlya Yanok 				if (!inode_crc((struct jffs2_raw_inode *) node))
16498a36d31fSIlya Yanok 				       break;
16508a36d31fSIlya Yanok 
16518a36d31fSIlya Yanok 				if (insert_node(&pL->frag, (u32) part->offset +
1652b644006eSIlya Yanok 						ofs) == NULL) {
1653b644006eSIlya Yanok 					free(buf);
1654b644006eSIlya Yanok 					jffs2_free_cache(part);
16558a36d31fSIlya Yanok 					return 0;
1656b644006eSIlya Yanok 				}
165770741004SIlya Yanok 				if (max_totlen < node->totlen)
165870741004SIlya Yanok 					max_totlen = node->totlen;
16598a36d31fSIlya Yanok 				break;
16608a36d31fSIlya Yanok 			case JFFS2_NODETYPE_DIRENT:
16618a36d31fSIlya Yanok 				if (buf_ofs + buf_len < ofs + sizeof(struct
16628a36d31fSIlya Yanok 							jffs2_raw_dirent) +
16638a36d31fSIlya Yanok 							((struct
16648a36d31fSIlya Yanok 							 jffs2_raw_dirent *)
16658a36d31fSIlya Yanok 							node)->nsize) {
16668a36d31fSIlya Yanok 					get_fl_mem((u32)part->offset + ofs,
16678a36d31fSIlya Yanok 						   buf_len, buf);
16688a36d31fSIlya Yanok 					buf_ofs = ofs;
16698a36d31fSIlya Yanok 					node = (void *)buf;
16708a36d31fSIlya Yanok 				}
16718a36d31fSIlya Yanok 
16728a36d31fSIlya Yanok 				if (!dirent_crc((struct jffs2_raw_dirent *)
16738a36d31fSIlya Yanok 							node) ||
16748a36d31fSIlya Yanok 						!dirent_name_crc(
16758a36d31fSIlya Yanok 							(struct
16768a36d31fSIlya Yanok 							 jffs2_raw_dirent *)
16778a36d31fSIlya Yanok 							node))
16788a36d31fSIlya Yanok 					break;
1679fc1cfcdbSwdenk 				if (! (counterN%100))
16804b9206edSwdenk 					puts ("\b\b.  ");
168106d01dbeSwdenk 				if (insert_node(&pL->dir, (u32) part->offset +
1682b644006eSIlya Yanok 						ofs) == NULL) {
1683b644006eSIlya Yanok 					free(buf);
1684b644006eSIlya Yanok 					jffs2_free_cache(part);
168506d01dbeSwdenk 					return 0;
1686b644006eSIlya Yanok 				}
168770741004SIlya Yanok 				if (max_totlen < node->totlen)
168870741004SIlya Yanok 					max_totlen = node->totlen;
168906d01dbeSwdenk 				counterN++;
16908a36d31fSIlya Yanok 				break;
16918a36d31fSIlya Yanok 			case JFFS2_NODETYPE_CLEANMARKER:
169206d01dbeSwdenk 				if (node->totlen != sizeof(struct jffs2_unknown_node))
169306d01dbeSwdenk 					printf("OOPS Cleanmarker has bad size "
1694b64f190bSWolfgang Denk 						"%d != %zu\n",
1695b64f190bSWolfgang Denk 						node->totlen,
169606d01dbeSwdenk 						sizeof(struct jffs2_unknown_node));
16978a36d31fSIlya Yanok 				break;
16988a36d31fSIlya Yanok 			case JFFS2_NODETYPE_PADDING:
16997205e407Swdenk 				if (node->totlen < sizeof(struct jffs2_unknown_node))
17007205e407Swdenk 					printf("OOPS Padding has bad size "
1701b64f190bSWolfgang Denk 						"%d < %zu\n",
1702b64f190bSWolfgang Denk 						node->totlen,
17037205e407Swdenk 						sizeof(struct jffs2_unknown_node));
17048a36d31fSIlya Yanok 				break;
17059b707622SIlya Yanok 			case JFFS2_NODETYPE_SUMMARY:
17069b707622SIlya Yanok 				break;
17078a36d31fSIlya Yanok 			default:
1708b64f190bSWolfgang Denk 				printf("Unknown node type: %x len %d offset 0x%x\n",
1709b64f190bSWolfgang Denk 					node->nodetype,
17108a36d31fSIlya Yanok 					node->totlen, ofs);
171106d01dbeSwdenk 			}
17128a36d31fSIlya Yanok 			ofs += ((node->totlen + 3) & ~3);
171306d01dbeSwdenk 			counterF++;
171406d01dbeSwdenk 		}
171506d01dbeSwdenk 	}
171606d01dbeSwdenk 
17178a36d31fSIlya Yanok 	free(buf);
171806d01dbeSwdenk 	putstr("\b\b done.\r\n");		/* close off the dots */
171970741004SIlya Yanok 
172070741004SIlya Yanok 	/* We don't care if malloc failed - then each read operation will
172170741004SIlya Yanok 	 * allocate its own buffer as necessary (NAND) or will read directly
172270741004SIlya Yanok 	 * from flash (NOR).
172370741004SIlya Yanok 	 */
172470741004SIlya Yanok 	pL->readbuf = malloc(max_totlen);
172570741004SIlya Yanok 
172606d01dbeSwdenk 	/* turn the lcd back on. */
172706d01dbeSwdenk 	/* splash(); */
172806d01dbeSwdenk 
172906d01dbeSwdenk #if 0
173006d01dbeSwdenk 	putLabeledWord("dir entries = ", pL->dir.listCount);
173106d01dbeSwdenk 	putLabeledWord("frag entries = ", pL->frag.listCount);
173206d01dbeSwdenk 	putLabeledWord("+4 increments = ", counter4);
173306d01dbeSwdenk 	putLabeledWord("+file_offset increments = ", counterF);
173406d01dbeSwdenk 
173506d01dbeSwdenk #endif
173606d01dbeSwdenk 
173706d01dbeSwdenk #ifdef DEBUG_DIRENTS
173806d01dbeSwdenk 	dump_dirents(pL);
173906d01dbeSwdenk #endif
174006d01dbeSwdenk 
174106d01dbeSwdenk #ifdef DEBUG_FRAGMENTS
174206d01dbeSwdenk 	dump_fragments(pL);
174306d01dbeSwdenk #endif
174406d01dbeSwdenk 
1745fe8c2806Swdenk 	/* give visual feedback that we are done scanning the flash */
1746fe8c2806Swdenk 	led_blink(0x0, 0x0, 0x1, 0x1);	/* off, forever, on 100ms, off 100ms */
1747fe8c2806Swdenk 	return 1;
1748fe8c2806Swdenk }
1749fe8c2806Swdenk 
1750fe8c2806Swdenk 
1751fe8c2806Swdenk static u32
1752fe8c2806Swdenk jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1753fe8c2806Swdenk {
1754fe8c2806Swdenk 	struct b_node *b;
1755998eaaecSwdenk 	struct jffs2_raw_inode ojNode;
1756fe8c2806Swdenk 	struct jffs2_raw_inode *jNode;
1757fe8c2806Swdenk 	int i;
1758fe8c2806Swdenk 
1759fe8c2806Swdenk 	for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1760fe8c2806Swdenk 		piL->compr_info[i].num_frags = 0;
1761fe8c2806Swdenk 		piL->compr_info[i].compr_sum = 0;
1762fe8c2806Swdenk 		piL->compr_info[i].decompr_sum = 0;
1763fe8c2806Swdenk 	}
1764fe8c2806Swdenk 
176506d01dbeSwdenk 	b = pL->frag.listHead;
1766fe8c2806Swdenk 	while (b) {
1767998eaaecSwdenk 		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1768998eaaecSwdenk 			sizeof(ojNode), &ojNode);
1769fe8c2806Swdenk 		if (jNode->compr < JFFS2_NUM_COMPR) {
1770fe8c2806Swdenk 			piL->compr_info[jNode->compr].num_frags++;
1771fe8c2806Swdenk 			piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1772fe8c2806Swdenk 			piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1773fe8c2806Swdenk 		}
1774fe8c2806Swdenk 		b = b->next;
1775fe8c2806Swdenk 	}
1776fe8c2806Swdenk 	return 0;
1777fe8c2806Swdenk }
1778fe8c2806Swdenk 
1779fe8c2806Swdenk 
1780fe8c2806Swdenk static struct b_lists *
1781fe8c2806Swdenk jffs2_get_list(struct part_info * part, const char *who)
1782fe8c2806Swdenk {
1783700a0c64SWolfgang Denk 	/* copy requested part_info struct pointer to global location */
1784700a0c64SWolfgang Denk 	current_part = part;
1785700a0c64SWolfgang Denk 
1786fe8c2806Swdenk 	if (jffs2_1pass_rescan_needed(part)) {
1787fe8c2806Swdenk 		if (!jffs2_1pass_build_lists(part)) {
1788fe8c2806Swdenk 			printf("%s: Failed to scan JFFSv2 file structure\n", who);
1789fe8c2806Swdenk 			return NULL;
1790fe8c2806Swdenk 		}
1791fe8c2806Swdenk 	}
1792fe8c2806Swdenk 	return (struct b_lists *)part->jffs2_priv;
1793fe8c2806Swdenk }
1794fe8c2806Swdenk 
1795fe8c2806Swdenk 
1796fe8c2806Swdenk /* Print directory / file contents */
1797fe8c2806Swdenk u32
1798fe8c2806Swdenk jffs2_1pass_ls(struct part_info * part, const char *fname)
1799fe8c2806Swdenk {
1800fe8c2806Swdenk 	struct b_lists *pl;
180187b8bd5aSWolfgang Denk 	long ret = 1;
1802fe8c2806Swdenk 	u32 inode;
1803fe8c2806Swdenk 
1804fe8c2806Swdenk 	if (! (pl = jffs2_get_list(part, "ls")))
1805fe8c2806Swdenk 		return 0;
1806fe8c2806Swdenk 
1807fe8c2806Swdenk 	if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1808fe8c2806Swdenk 		putstr("ls: Failed to scan jffs2 file structure\r\n");
1809fe8c2806Swdenk 		return 0;
1810fe8c2806Swdenk 	}
1811fc1cfcdbSwdenk 
1812fc1cfcdbSwdenk 
1813fe8c2806Swdenk #if 0
1814fe8c2806Swdenk 	putLabeledWord("found file at inode = ", inode);
1815fe8c2806Swdenk 	putLabeledWord("read_inode returns = ", ret);
1816fe8c2806Swdenk #endif
1817fc1cfcdbSwdenk 
1818fc1cfcdbSwdenk 	return ret;
1819fe8c2806Swdenk }
1820fe8c2806Swdenk 
1821fe8c2806Swdenk 
1822fe8c2806Swdenk /* Load a file from flash into memory. fname can be a full path */
1823fe8c2806Swdenk u32
1824fe8c2806Swdenk jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1825fe8c2806Swdenk {
1826fe8c2806Swdenk 
1827fe8c2806Swdenk 	struct b_lists *pl;
182887b8bd5aSWolfgang Denk 	long ret = 1;
1829fe8c2806Swdenk 	u32 inode;
1830fe8c2806Swdenk 
1831fe8c2806Swdenk 	if (! (pl  = jffs2_get_list(part, "load")))
1832fe8c2806Swdenk 		return 0;
1833fe8c2806Swdenk 
1834fe8c2806Swdenk 	if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1835fe8c2806Swdenk 		putstr("load: Failed to find inode\r\n");
1836fe8c2806Swdenk 		return 0;
1837fe8c2806Swdenk 	}
1838fe8c2806Swdenk 
1839fe8c2806Swdenk 	/* Resolve symlinks */
1840fe8c2806Swdenk 	if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1841fe8c2806Swdenk 		putstr("load: Failed to resolve inode structure\r\n");
1842fe8c2806Swdenk 		return 0;
1843fe8c2806Swdenk 	}
1844fe8c2806Swdenk 
1845fe8c2806Swdenk 	if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1846fe8c2806Swdenk 		putstr("load: Failed to read inode\r\n");
1847fe8c2806Swdenk 		return 0;
1848fe8c2806Swdenk 	}
1849fe8c2806Swdenk 
1850fe8c2806Swdenk 	DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1851fe8c2806Swdenk 				(unsigned long) dest, ret);
1852fe8c2806Swdenk 	return ret;
1853fe8c2806Swdenk }
1854fe8c2806Swdenk 
1855fe8c2806Swdenk /* Return information about the fs on this partition */
1856fe8c2806Swdenk u32
1857fe8c2806Swdenk jffs2_1pass_info(struct part_info * part)
1858fe8c2806Swdenk {
1859fe8c2806Swdenk 	struct b_jffs2_info info;
1860fe8c2806Swdenk 	struct b_lists *pl;
1861fe8c2806Swdenk 	int i;
1862fe8c2806Swdenk 
1863fe8c2806Swdenk 	if (! (pl  = jffs2_get_list(part, "info")))
1864fe8c2806Swdenk 		return 0;
1865fe8c2806Swdenk 
1866fe8c2806Swdenk 	jffs2_1pass_fill_info(pl, &info);
1867fe8c2806Swdenk 	for (i = 0; i < JFFS2_NUM_COMPR; i++) {
18684b9206edSwdenk 		printf ("Compression: %s\n"
18694b9206edSwdenk 			"\tfrag count: %d\n"
18704b9206edSwdenk 			"\tcompressed sum: %d\n"
18714b9206edSwdenk 			"\tuncompressed sum: %d\n",
18724b9206edSwdenk 			compr_names[i],
18734b9206edSwdenk 			info.compr_info[i].num_frags,
18744b9206edSwdenk 			info.compr_info[i].compr_sum,
18754b9206edSwdenk 			info.compr_info[i].decompr_sum);
1876fe8c2806Swdenk 	}
1877fe8c2806Swdenk 	return 1;
1878fe8c2806Swdenk }
1879