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