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