]>
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: | |
55 | * (1) most pages are 4096 bytes | |
56 | * (2) version numbers are somewhat sorted in acsending order | |
57 | * (3) multiple compressed blocks making up one page is uncommon | |
58 | * | |
59 | * So I create a linked list of decending version numbers (insertions at the | |
60 | * head), and then for each page, walk down the list, until a matching page | |
61 | * with 4096 bytes is found, and then decompress the watching pages in | |
62 | * reverse order. | |
63 | * | |
64 | */ | |
65 | ||
66 | /* | |
67 | * Adapted by Nye Liu <[email protected]> and | |
68 | * Rex Feany <[email protected]> | |
69 | * on Jan/2002 for U-Boot. | |
70 | * | |
71 | * Clipped out all the non-1pass functions, cleaned up warnings, | |
72 | * wrappers, etc. No major changes to the code. | |
73 | * Please, he really means it when he said have a paper bag | |
74 | * handy. We needed it ;). | |
75 | * | |
76 | */ | |
77 | ||
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 | * | |
95 | * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS. | |
96 | * Sorting is done while adding fragments to the lists, which is more or less a | |
97 | * bubble sort. This takes a lot of time, and is most probably not an issue if | |
98 | * the boot filesystem is always mounted readonly. | |
99 | * | |
100 | * You should define it if the boot filesystem is mounted writable, and updates | |
101 | * to the boot files are done by copying files to that filesystem. | |
102 | * | |
103 | * | |
104 | * There's a big issue left: endianess is completely ignored in this code. Duh! | |
105 | * | |
106 | * | |
107 | * You still should have paper bags at hand :-(. The code lacks more or less | |
108 | * any comment, and is still arcane and difficult to read in places. As this | |
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> | |
117 | #include <linux/stat.h> | |
118 | #include <linux/time.h> | |
119 | ||
120 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) | |
121 | ||
122 | #include <jffs2/jffs2.h> | |
123 | #include <jffs2/jffs2_1pass.h> | |
124 | ||
125 | #include "jffs2_private.h" | |
126 | ||
fe8c2806 | 127 | |
06d01dbe | 128 | #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ |
fc1cfcdb | 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 */ | |
134 | #undef DEBUG /* enable debugging messages */ | |
135 | ||
fe8c2806 | 136 | |
fe8c2806 WD |
137 | #ifdef DEBUG |
138 | # define DEBUGF(fmt,args...) printf(fmt ,##args) | |
139 | #else | |
140 | # define DEBUGF(fmt,args...) | |
141 | #endif | |
142 | ||
998eaaec WD |
143 | #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) |
144 | ||
145 | /* | |
146 | * Support for jffs2 on top of NAND-flash | |
147 | * | |
148 | * NAND memory isn't mapped in processor's address space, | |
149 | * so data should be fetched from flash before | |
150 | * being processed. This is exactly what functions declared | |
151 | * here do. | |
152 | * | |
153 | */ | |
154 | ||
155 | /* this one defined in cmd_nand.c */ | |
156 | int read_jffs2_nand(size_t start, size_t len, | |
157 | size_t * retlen, u_char * buf, int nanddev); | |
158 | ||
159 | #define NAND_PAGE_SIZE 512 | |
160 | #define NAND_PAGE_SHIFT 9 | |
161 | #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1)) | |
162 | ||
163 | #ifndef NAND_CACHE_PAGES | |
164 | #define NAND_CACHE_PAGES 16 | |
165 | #endif | |
166 | #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE) | |
167 | ||
168 | static u8* nand_cache = NULL; | |
169 | static u32 nand_cache_off = (u32)-1; | |
170 | static int nanddev = 0; /* nand device of current partition */ | |
171 | ||
172 | static int read_nand_cached(u32 off, u32 size, u_char *buf) | |
173 | { | |
174 | u32 bytes_read = 0; | |
175 | size_t retlen; | |
176 | int cpy_bytes; | |
177 | ||
178 | while (bytes_read < size) { | |
179 | if ((off + bytes_read < nand_cache_off) || | |
180 | (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) { | |
181 | nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK; | |
182 | if (!nand_cache) { | |
183 | /* This memory never gets freed but 'cause | |
184 | it's a bootloader, nobody cares */ | |
507bbe3e WD |
185 | nand_cache = malloc(NAND_CACHE_SIZE); |
186 | if (!nand_cache) { | |
998eaaec WD |
187 | printf("read_nand_cached: can't alloc cache size %d bytes\n", |
188 | NAND_CACHE_SIZE); | |
189 | return -1; | |
190 | } | |
191 | } | |
192 | if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE, | |
193 | &retlen, nand_cache, nanddev) < 0 || | |
194 | retlen != NAND_CACHE_SIZE) { | |
195 | printf("read_nand_cached: error reading nand off %#x size %d bytes\n", | |
196 | nand_cache_off, NAND_CACHE_SIZE); | |
197 | return -1; | |
198 | } | |
199 | } | |
200 | cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read); | |
201 | if (cpy_bytes > size - bytes_read) | |
202 | cpy_bytes = size - bytes_read; | |
203 | memcpy(buf + bytes_read, | |
204 | nand_cache + off + bytes_read - nand_cache_off, | |
205 | cpy_bytes); | |
206 | bytes_read += cpy_bytes; | |
207 | } | |
208 | return bytes_read; | |
209 | } | |
210 | ||
211 | static void *get_fl_mem(u32 off, u32 size, void *ext_buf) | |
212 | { | |
213 | u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size); | |
214 | ||
215 | if (NULL == buf) { | |
216 | printf("get_fl_mem: can't alloc %d bytes\n", size); | |
217 | return NULL; | |
218 | } | |
219 | if (read_nand_cached(off, size, buf) < 0) { | |
32877d66 WD |
220 | if (!ext_buf) |
221 | free(buf); | |
998eaaec WD |
222 | return NULL; |
223 | } | |
224 | ||
225 | return buf; | |
226 | } | |
227 | ||
228 | static void *get_node_mem(u32 off) | |
229 | { | |
230 | struct jffs2_unknown_node node; | |
231 | void *ret = NULL; | |
232 | ||
233 | if (NULL == get_fl_mem(off, sizeof(node), &node)) | |
234 | return NULL; | |
235 | ||
236 | if (!(ret = get_fl_mem(off, node.magic == | |
237 | JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), | |
238 | NULL))) { | |
239 | printf("off = %#x magic %#x type %#x node.totlen = %d\n", | |
240 | off, node.magic, node.nodetype, node.totlen); | |
241 | } | |
242 | return ret; | |
243 | } | |
244 | ||
507bbe3e | 245 | static void put_fl_mem(void *buf) |
998eaaec WD |
246 | { |
247 | free(buf); | |
248 | } | |
249 | ||
250 | #else /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ | |
251 | ||
252 | static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf) | |
253 | { | |
254 | return (void*)off; | |
255 | } | |
256 | ||
257 | static inline void *get_node_mem(u32 off) | |
258 | { | |
259 | return (void*)off; | |
260 | } | |
261 | ||
507bbe3e | 262 | static inline void put_fl_mem(void *buf) |
998eaaec WD |
263 | { |
264 | } | |
265 | ||
266 | #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ | |
267 | ||
fe8c2806 | 268 | |
06d01dbe WD |
269 | /* Compression names */ |
270 | static char *compr_names[] = { | |
271 | "NONE", | |
272 | "ZERO", | |
273 | "RTIME", | |
274 | "RUBINMIPS", | |
275 | "COPY", | |
276 | "DYNRUBIN", | |
07cc0999 | 277 | "ZLIB", |
412babe3 | 278 | #if defined(CONFIG_JFFS2_LZO_LZARI) |
07cc0999 | 279 | "LZO", |
07cc0999 WD |
280 | "LZARI", |
281 | #endif | |
06d01dbe WD |
282 | }; |
283 | ||
284 | /* Spinning wheel */ | |
285 | static char spinner[] = { '|', '/', '-', '\\' }; | |
286 | ||
287 | /* Memory management */ | |
288 | struct mem_block { | |
289 | u32 index; | |
290 | struct mem_block *next; | |
291 | struct b_node nodes[NODE_CHUNK]; | |
292 | }; | |
293 | ||
294 | ||
295 | static void | |
296 | free_nodes(struct b_list *list) | |
297 | { | |
298 | while (list->listMemBase != NULL) { | |
299 | struct mem_block *next = list->listMemBase->next; | |
300 | free( list->listMemBase ); | |
301 | list->listMemBase = next; | |
302 | } | |
303 | } | |
fe8c2806 WD |
304 | |
305 | static struct b_node * | |
06d01dbe | 306 | add_node(struct b_list *list) |
fe8c2806 | 307 | { |
06d01dbe WD |
308 | u32 index = 0; |
309 | struct mem_block *memBase; | |
fe8c2806 WD |
310 | struct b_node *b; |
311 | ||
06d01dbe WD |
312 | memBase = list->listMemBase; |
313 | if (memBase != NULL) | |
314 | index = memBase->index; | |
fe8c2806 WD |
315 | #if 0 |
316 | putLabeledWord("add_node: index = ", index); | |
06d01dbe | 317 | putLabeledWord("add_node: memBase = ", list->listMemBase); |
fe8c2806 WD |
318 | #endif |
319 | ||
06d01dbe WD |
320 | if (memBase == NULL || index >= NODE_CHUNK) { |
321 | /* we need more space before we continue */ | |
322 | memBase = mmalloc(sizeof(struct mem_block)); | |
323 | if (memBase == NULL) { | |
fe8c2806 WD |
324 | putstr("add_node: malloc failed\n"); |
325 | return NULL; | |
326 | } | |
06d01dbe WD |
327 | memBase->next = list->listMemBase; |
328 | index = 0; | |
fe8c2806 WD |
329 | #if 0 |
330 | putLabeledWord("add_node: alloced a new membase at ", *memBase); | |
331 | #endif | |
332 | ||
333 | } | |
334 | /* now we have room to add it. */ | |
06d01dbe WD |
335 | b = &memBase->nodes[index]; |
336 | index ++; | |
fe8c2806 | 337 | |
06d01dbe WD |
338 | memBase->index = index; |
339 | list->listMemBase = memBase; | |
340 | list->listCount++; | |
341 | return b; | |
342 | } | |
fe8c2806 | 343 | |
06d01dbe WD |
344 | static struct b_node * |
345 | insert_node(struct b_list *list, u32 offset) | |
346 | { | |
347 | struct b_node *new; | |
348 | #ifdef CFG_JFFS2_SORT_FRAGMENTS | |
349 | struct b_node *b, *prev; | |
fe8c2806 WD |
350 | #endif |
351 | ||
06d01dbe WD |
352 | if (!(new = add_node(list))) { |
353 | putstr("add_node failed!\r\n"); | |
354 | return NULL; | |
355 | } | |
356 | new->offset = offset; | |
357 | ||
358 | #ifdef CFG_JFFS2_SORT_FRAGMENTS | |
359 | if (list->listTail != NULL && list->listCompare(new, list->listTail)) | |
360 | prev = list->listTail; | |
361 | else if (list->listLast != NULL && list->listCompare(new, list->listLast)) | |
362 | prev = list->listLast; | |
363 | else | |
364 | prev = NULL; | |
365 | ||
366 | for (b = (prev ? prev->next : list->listHead); | |
367 | b != NULL && list->listCompare(new, b); | |
368 | prev = b, b = b->next) { | |
369 | list->listLoops++; | |
370 | } | |
371 | if (b != NULL) | |
372 | list->listLast = prev; | |
373 | ||
374 | if (b != NULL) { | |
375 | new->next = b; | |
376 | if (prev != NULL) | |
377 | prev->next = new; | |
378 | else | |
379 | list->listHead = new; | |
380 | } else | |
fe8c2806 | 381 | #endif |
06d01dbe WD |
382 | { |
383 | new->next = (struct b_node *) NULL; | |
384 | if (list->listTail != NULL) { | |
385 | list->listTail->next = new; | |
386 | list->listTail = new; | |
387 | } else { | |
388 | list->listTail = list->listHead = new; | |
389 | } | |
390 | } | |
fe8c2806 | 391 | |
06d01dbe | 392 | return new; |
fe8c2806 WD |
393 | } |
394 | ||
06d01dbe | 395 | #ifdef CFG_JFFS2_SORT_FRAGMENTS |
7205e407 WD |
396 | /* Sort data entries with the latest version last, so that if there |
397 | * is overlapping data the latest version will be used. | |
398 | */ | |
06d01dbe WD |
399 | static int compare_inodes(struct b_node *new, struct b_node *old) |
400 | { | |
998eaaec WD |
401 | struct jffs2_raw_inode ojNew; |
402 | struct jffs2_raw_inode ojOld; | |
403 | struct jffs2_raw_inode *jNew = | |
404 | (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew); | |
405 | struct jffs2_raw_inode *jOld = | |
406 | (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld); | |
06d01dbe | 407 | |
7205e407 | 408 | return jNew->version > jOld->version; |
06d01dbe WD |
409 | } |
410 | ||
7205e407 WD |
411 | /* Sort directory entries so all entries in the same directory |
412 | * with the same name are grouped together, with the latest version | |
413 | * last. This makes it easy to eliminate all but the latest version | |
414 | * by marking the previous version dead by setting the inode to 0. | |
415 | */ | |
06d01dbe WD |
416 | static int compare_dirents(struct b_node *new, struct b_node *old) |
417 | { | |
998eaaec WD |
418 | struct jffs2_raw_dirent ojNew; |
419 | struct jffs2_raw_dirent ojOld; | |
420 | struct jffs2_raw_dirent *jNew = | |
421 | (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew); | |
507bbe3e | 422 | struct jffs2_raw_dirent *jOld = |
998eaaec | 423 | (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld); |
7205e407 WD |
424 | int cmp; |
425 | ||
426 | /* ascending sort by pino */ | |
427 | if (jNew->pino != jOld->pino) | |
428 | return jNew->pino > jOld->pino; | |
429 | ||
430 | /* pino is the same, so use ascending sort by nsize, so | |
431 | * we don't do strncmp unless we really must. | |
432 | */ | |
433 | if (jNew->nsize != jOld->nsize) | |
434 | return jNew->nsize > jOld->nsize; | |
435 | ||
436 | /* length is also the same, so use ascending sort by name | |
437 | */ | |
438 | cmp = strncmp(jNew->name, jOld->name, jNew->nsize); | |
439 | if (cmp != 0) | |
440 | return cmp > 0; | |
441 | ||
442 | /* we have duplicate names in this directory, so use ascending | |
443 | * sort by version | |
444 | */ | |
445 | if (jNew->version > jOld->version) { | |
446 | /* since jNew is newer, we know jOld is not valid, so | |
447 | * mark it with inode 0 and it will not be used | |
448 | */ | |
449 | jOld->ino = 0; | |
450 | return 1; | |
451 | } | |
42d1f039 | 452 | |
7205e407 | 453 | return 0; |
06d01dbe WD |
454 | } |
455 | #endif | |
fe8c2806 WD |
456 | |
457 | static u32 | |
458 | jffs2_scan_empty(u32 start_offset, struct part_info *part) | |
459 | { | |
06d01dbe WD |
460 | char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode); |
461 | char *offset = part->offset + start_offset; | |
998eaaec | 462 | u32 off; |
fe8c2806 | 463 | |
998eaaec WD |
464 | while (offset < max && |
465 | *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) { | |
06d01dbe WD |
466 | offset += sizeof(u32); |
467 | /* return if spinning is due */ | |
468 | if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break; | |
fe8c2806 | 469 | } |
fc1cfcdb | 470 | |
06d01dbe | 471 | return offset - part->offset; |
fe8c2806 WD |
472 | } |
473 | ||
474 | static u32 | |
475 | jffs_init_1pass_list(struct part_info *part) | |
476 | { | |
06d01dbe | 477 | struct b_lists *pL; |
fe8c2806 | 478 | |
06d01dbe WD |
479 | if (part->jffs2_priv != NULL) { |
480 | pL = (struct b_lists *)part->jffs2_priv; | |
481 | free_nodes(&pL->frag); | |
482 | free_nodes(&pL->dir); | |
483 | free(pL); | |
484 | } | |
485 | if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { | |
486 | pL = (struct b_lists *)part->jffs2_priv; | |
487 | ||
488 | memset(pL, 0, sizeof(*pL)); | |
489 | #ifdef CFG_JFFS2_SORT_FRAGMENTS | |
490 | pL->dir.listCompare = compare_dirents; | |
491 | pL->frag.listCompare = compare_inodes; | |
492 | #endif | |
fe8c2806 WD |
493 | } |
494 | return 0; | |
495 | } | |
496 | ||
497 | /* find the inode from the slashless name given a parent */ | |
498 | static long | |
499 | jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest) | |
500 | { | |
501 | struct b_node *b; | |
502 | struct jffs2_raw_inode *jNode; | |
06d01dbe | 503 | u32 totalSize = 0; |
7205e407 | 504 | u32 latestVersion = 0; |
06d01dbe | 505 | char *lDest; |
fe8c2806 WD |
506 | char *src; |
507 | long ret; | |
508 | int i; | |
509 | u32 counter = 0; | |
7205e407 WD |
510 | #ifdef CFG_JFFS2_SORT_FRAGMENTS |
511 | /* Find file size before loading any data, so fragments that | |
512 | * start past the end of file can be ignored. A fragment | |
513 | * that is partially in the file is loaded, so extra data may | |
514 | * be loaded up to the next 4K boundary above the file size. | |
515 | * This shouldn't cause trouble when loading kernel images, so | |
516 | * we will live with it. | |
517 | */ | |
518 | for (b = pL->frag.listHead; b != NULL; b = b->next) { | |
507bbe3e | 519 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
998eaaec | 520 | sizeof(struct jffs2_raw_inode), NULL); |
7205e407 WD |
521 | if ((inode == jNode->ino)) { |
522 | /* get actual file length from the newest node */ | |
523 | if (jNode->version >= latestVersion) { | |
524 | totalSize = jNode->isize; | |
525 | latestVersion = jNode->version; | |
526 | } | |
527 | } | |
998eaaec | 528 | put_fl_mem(jNode); |
7205e407 WD |
529 | } |
530 | #endif | |
fe8c2806 | 531 | |
06d01dbe | 532 | for (b = pL->frag.listHead; b != NULL; b = b->next) { |
998eaaec | 533 | jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset); |
fe8c2806 | 534 | if ((inode == jNode->ino)) { |
06d01dbe | 535 | #if 0 |
fe8c2806 WD |
536 | putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen); |
537 | putLabeledWord("read_inode: inode = ", jNode->ino); | |
538 | putLabeledWord("read_inode: version = ", jNode->version); | |
539 | putLabeledWord("read_inode: isize = ", jNode->isize); | |
540 | putLabeledWord("read_inode: offset = ", jNode->offset); | |
541 | putLabeledWord("read_inode: csize = ", jNode->csize); | |
542 | putLabeledWord("read_inode: dsize = ", jNode->dsize); | |
543 | putLabeledWord("read_inode: compr = ", jNode->compr); | |
544 | putLabeledWord("read_inode: usercompr = ", jNode->usercompr); | |
545 | putLabeledWord("read_inode: flags = ", jNode->flags); | |
fe8c2806 | 546 | #endif |
7205e407 WD |
547 | |
548 | #ifndef CFG_JFFS2_SORT_FRAGMENTS | |
06d01dbe WD |
549 | /* get actual file length from the newest node */ |
550 | if (jNode->version >= latestVersion) { | |
fe8c2806 | 551 | totalSize = jNode->isize; |
06d01dbe | 552 | latestVersion = jNode->version; |
fe8c2806 | 553 | } |
7205e407 | 554 | #endif |
fe8c2806 WD |
555 | |
556 | if(dest) { | |
557 | src = ((char *) jNode) + sizeof(struct jffs2_raw_inode); | |
06d01dbe | 558 | /* ignore data behind latest known EOF */ |
998eaaec WD |
559 | if (jNode->offset > totalSize) { |
560 | put_fl_mem(jNode); | |
06d01dbe | 561 | continue; |
998eaaec | 562 | } |
06d01dbe | 563 | |
fe8c2806 WD |
564 | lDest = (char *) (dest + jNode->offset); |
565 | #if 0 | |
06d01dbe | 566 | putLabeledWord("read_inode: src = ", src); |
fe8c2806 | 567 | putLabeledWord("read_inode: dest = ", lDest); |
fe8c2806 WD |
568 | #endif |
569 | switch (jNode->compr) { | |
570 | case JFFS2_COMPR_NONE: | |
fe8c2806 WD |
571 | ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize); |
572 | break; | |
573 | case JFFS2_COMPR_ZERO: | |
574 | ret = 0; | |
575 | for (i = 0; i < jNode->dsize; i++) | |
576 | *(lDest++) = 0; | |
577 | break; | |
578 | case JFFS2_COMPR_RTIME: | |
579 | ret = 0; | |
580 | rtime_decompress(src, lDest, jNode->csize, jNode->dsize); | |
581 | break; | |
582 | case JFFS2_COMPR_DYNRUBIN: | |
583 | /* this is slow but it works */ | |
584 | ret = 0; | |
585 | dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize); | |
586 | break; | |
587 | case JFFS2_COMPR_ZLIB: | |
588 | ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize); | |
589 | break; | |
412babe3 | 590 | #if defined(CONFIG_JFFS2_LZO_LZARI) |
07cc0999 WD |
591 | case JFFS2_COMPR_LZO: |
592 | ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize); | |
593 | break; | |
412babe3 WD |
594 | case JFFS2_COMPR_LZARI: |
595 | ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize); | |
596 | break; | |
07cc0999 | 597 | #endif |
fe8c2806 WD |
598 | default: |
599 | /* unknown */ | |
600 | putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr); | |
998eaaec | 601 | put_fl_mem(jNode); |
fe8c2806 WD |
602 | return -1; |
603 | break; | |
604 | } | |
605 | } | |
606 | ||
fe8c2806 | 607 | #if 0 |
fe8c2806 WD |
608 | putLabeledWord("read_inode: totalSize = ", totalSize); |
609 | putLabeledWord("read_inode: compr ret = ", ret); | |
610 | #endif | |
611 | } | |
fe8c2806 | 612 | counter++; |
998eaaec | 613 | put_fl_mem(jNode); |
fe8c2806 | 614 | } |
fe8c2806 WD |
615 | |
616 | #if 0 | |
06d01dbe | 617 | putLabeledWord("read_inode: returning = ", totalSize); |
fe8c2806 | 618 | #endif |
06d01dbe | 619 | return totalSize; |
fe8c2806 WD |
620 | } |
621 | ||
622 | /* find the inode from the slashless name given a parent */ | |
623 | static u32 | |
624 | jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino) | |
625 | { | |
626 | struct b_node *b; | |
627 | struct jffs2_raw_dirent *jDir; | |
628 | int len; | |
629 | u32 counter; | |
630 | u32 version = 0; | |
631 | u32 inode = 0; | |
632 | ||
633 | /* name is assumed slash free */ | |
634 | len = strlen(name); | |
635 | ||
636 | counter = 0; | |
637 | /* we need to search all and return the inode with the highest version */ | |
06d01dbe | 638 | for(b = pL->dir.listHead; b; b = b->next, counter++) { |
998eaaec | 639 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); |
06d01dbe WD |
640 | if ((pino == jDir->pino) && (len == jDir->nsize) && |
641 | (jDir->ino) && /* 0 for unlink */ | |
fe8c2806 | 642 | (!strncmp(jDir->name, name, len))) { /* a match */ |
998eaaec WD |
643 | if (jDir->version < version) { |
644 | put_fl_mem(jDir); | |
7205e407 | 645 | continue; |
998eaaec | 646 | } |
fe8c2806 | 647 | |
7205e407 WD |
648 | if (jDir->version == version && inode != 0) { |
649 | /* I'm pretty sure this isn't legal */ | |
fe8c2806 WD |
650 | putstr(" ** ERROR ** "); |
651 | putnstr(jDir->name, jDir->nsize); | |
652 | putLabeledWord(" has dup version =", version); | |
653 | } | |
654 | inode = jDir->ino; | |
655 | version = jDir->version; | |
656 | } | |
657 | #if 0 | |
658 | putstr("\r\nfind_inode:p&l ->"); | |
659 | putnstr(jDir->name, jDir->nsize); | |
660 | putstr("\r\n"); | |
661 | putLabeledWord("pino = ", jDir->pino); | |
662 | putLabeledWord("nsize = ", jDir->nsize); | |
663 | putLabeledWord("b = ", (u32) b); | |
664 | putLabeledWord("counter = ", counter); | |
665 | #endif | |
998eaaec | 666 | put_fl_mem(jDir); |
fe8c2806 WD |
667 | } |
668 | return inode; | |
669 | } | |
670 | ||
180d3f74 | 671 | char *mkmodestr(unsigned long mode, char *str) |
fe8c2806 | 672 | { |
06d01dbe WD |
673 | static const char *l = "xwr"; |
674 | int mask = 1, i; | |
675 | char c; | |
676 | ||
677 | switch (mode & S_IFMT) { | |
678 | case S_IFDIR: str[0] = 'd'; break; | |
679 | case S_IFBLK: str[0] = 'b'; break; | |
680 | case S_IFCHR: str[0] = 'c'; break; | |
681 | case S_IFIFO: str[0] = 'f'; break; | |
682 | case S_IFLNK: str[0] = 'l'; break; | |
683 | case S_IFSOCK: str[0] = 's'; break; | |
684 | case S_IFREG: str[0] = '-'; break; | |
685 | default: str[0] = '?'; | |
686 | } | |
687 | ||
688 | for(i = 0; i < 9; i++) { | |
689 | c = l[i%3]; | |
690 | str[9-i] = (mode & mask)?c:'-'; | |
691 | mask = mask<<1; | |
692 | } | |
693 | ||
694 | if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; | |
695 | if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; | |
696 | if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; | |
697 | str[10] = '\0'; | |
698 | return str; | |
fe8c2806 WD |
699 | } |
700 | ||
701 | static inline void dump_stat(struct stat *st, const char *name) | |
702 | { | |
06d01dbe WD |
703 | char str[20]; |
704 | char s[64], *p; | |
fe8c2806 | 705 | |
06d01dbe WD |
706 | if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ |
707 | st->st_mtime = 1; | |
fe8c2806 | 708 | |
06d01dbe | 709 | ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */ |
fe8c2806 | 710 | |
06d01dbe WD |
711 | if ((p = strchr(s,'\n')) != NULL) *p = '\0'; |
712 | if ((p = strchr(s,'\r')) != NULL) *p = '\0'; | |
fe8c2806 WD |
713 | |
714 | /* | |
06d01dbe WD |
715 | printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), |
716 | st->st_size, s, name); | |
fe8c2806 WD |
717 | */ |
718 | ||
06d01dbe | 719 | printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name); |
fe8c2806 WD |
720 | } |
721 | ||
722 | static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i) | |
723 | { | |
724 | char fname[256]; | |
725 | struct stat st; | |
726 | ||
727 | if(!d || !i) return -1; | |
728 | ||
729 | strncpy(fname, d->name, d->nsize); | |
06d01dbe | 730 | fname[d->nsize] = '\0'; |
fe8c2806 WD |
731 | |
732 | memset(&st,0,sizeof(st)); | |
733 | ||
06d01dbe WD |
734 | st.st_mtime = i->mtime; |
735 | st.st_mode = i->mode; | |
736 | st.st_ino = i->ino; | |
fe8c2806 WD |
737 | |
738 | /* neither dsize nor isize help us.. do it the long way */ | |
06d01dbe | 739 | st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL); |
fe8c2806 WD |
740 | |
741 | dump_stat(&st, fname); | |
742 | ||
743 | if (d->type == DT_LNK) { | |
744 | unsigned char *src = (unsigned char *) (&i[1]); | |
745 | putstr(" -> "); | |
746 | putnstr(src, (int)i->dsize); | |
747 | } | |
748 | ||
749 | putstr("\r\n"); | |
750 | ||
751 | return 0; | |
752 | } | |
753 | ||
754 | /* list inodes with the given pino */ | |
755 | static u32 | |
756 | jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) | |
757 | { | |
758 | struct b_node *b; | |
759 | struct jffs2_raw_dirent *jDir; | |
760 | ||
06d01dbe | 761 | for (b = pL->dir.listHead; b; b = b->next) { |
998eaaec | 762 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); |
06d01dbe WD |
763 | if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */ |
764 | u32 i_version = 0; | |
998eaaec | 765 | struct jffs2_raw_inode ojNode; |
06d01dbe WD |
766 | struct jffs2_raw_inode *jNode, *i = NULL; |
767 | struct b_node *b2 = pL->frag.listHead; | |
fe8c2806 WD |
768 | |
769 | while (b2) { | |
998eaaec WD |
770 | jNode = (struct jffs2_raw_inode *) |
771 | get_fl_mem(b2->offset, sizeof(ojNode), &ojNode); | |
32877d66 WD |
772 | if (jNode->ino == jDir->ino && jNode->version >= i_version) { |
773 | if (i) | |
02b11f8e | 774 | put_fl_mem(i); |
cf8bc577 WD |
775 | |
776 | if (jDir->type == DT_LNK) | |
777 | i = get_node_mem(b2->offset); | |
778 | else | |
779 | i = get_fl_mem(b2->offset, sizeof(*i), NULL); | |
32877d66 | 780 | } |
fe8c2806 WD |
781 | b2 = b2->next; |
782 | } | |
783 | ||
784 | dump_inode(pL, jDir, i); | |
998eaaec | 785 | put_fl_mem(i); |
fe8c2806 | 786 | } |
998eaaec | 787 | put_fl_mem(jDir); |
fe8c2806 WD |
788 | } |
789 | return pino; | |
790 | } | |
791 | ||
792 | static u32 | |
793 | jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino) | |
794 | { | |
795 | int i; | |
796 | char tmp[256]; | |
797 | char working_tmp[256]; | |
798 | char *c; | |
799 | ||
800 | /* discard any leading slash */ | |
801 | i = 0; | |
802 | while (fname[i] == '/') | |
803 | i++; | |
804 | strcpy(tmp, &fname[i]); | |
805 | ||
806 | while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ | |
807 | { | |
808 | strncpy(working_tmp, tmp, c - tmp); | |
809 | working_tmp[c - tmp] = '\0'; | |
810 | #if 0 | |
811 | putstr("search_inode: tmp = "); | |
812 | putstr(tmp); | |
813 | putstr("\r\n"); | |
814 | putstr("search_inode: wtmp = "); | |
815 | putstr(working_tmp); | |
816 | putstr("\r\n"); | |
817 | putstr("search_inode: c = "); | |
818 | putstr(c); | |
819 | putstr("\r\n"); | |
820 | #endif | |
821 | for (i = 0; i < strlen(c) - 1; i++) | |
822 | tmp[i] = c[i + 1]; | |
823 | tmp[i] = '\0'; | |
824 | #if 0 | |
825 | putstr("search_inode: post tmp = "); | |
826 | putstr(tmp); | |
827 | putstr("\r\n"); | |
828 | #endif | |
829 | ||
830 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) { | |
831 | putstr("find_inode failed for name="); | |
832 | putstr(working_tmp); | |
833 | putstr("\r\n"); | |
834 | return 0; | |
835 | } | |
836 | } | |
837 | /* this is for the bare filename, directories have already been mapped */ | |
838 | if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { | |
839 | putstr("find_inode failed for name="); | |
840 | putstr(tmp); | |
841 | putstr("\r\n"); | |
842 | return 0; | |
843 | } | |
844 | return pino; | |
845 | ||
846 | } | |
847 | ||
848 | static u32 | |
849 | jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino) | |
850 | { | |
851 | struct b_node *b; | |
852 | struct b_node *b2; | |
853 | struct jffs2_raw_dirent *jDir; | |
854 | struct jffs2_raw_inode *jNode; | |
998eaaec WD |
855 | u8 jDirFoundType = 0; |
856 | u32 jDirFoundIno = 0; | |
857 | u32 jDirFoundPino = 0; | |
fe8c2806 WD |
858 | char tmp[256]; |
859 | u32 version = 0; | |
860 | u32 pino; | |
861 | unsigned char *src; | |
862 | ||
863 | /* we need to search all and return the inode with the highest version */ | |
06d01dbe | 864 | for(b = pL->dir.listHead; b; b = b->next) { |
998eaaec | 865 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); |
fe8c2806 | 866 | if (ino == jDir->ino) { |
998eaaec WD |
867 | if (jDir->version < version) { |
868 | put_fl_mem(jDir); | |
7205e407 | 869 | continue; |
998eaaec | 870 | } |
fe8c2806 | 871 | |
998eaaec | 872 | if (jDir->version == version && jDirFoundType) { |
7205e407 | 873 | /* I'm pretty sure this isn't legal */ |
fe8c2806 WD |
874 | putstr(" ** ERROR ** "); |
875 | putnstr(jDir->name, jDir->nsize); | |
876 | putLabeledWord(" has dup version (resolve) = ", | |
877 | version); | |
878 | } | |
879 | ||
998eaaec WD |
880 | jDirFoundType = jDir->type; |
881 | jDirFoundIno = jDir->ino; | |
882 | jDirFoundPino = jDir->pino; | |
fe8c2806 WD |
883 | version = jDir->version; |
884 | } | |
998eaaec | 885 | put_fl_mem(jDir); |
fe8c2806 WD |
886 | } |
887 | /* now we found the right entry again. (shoulda returned inode*) */ | |
998eaaec WD |
888 | if (jDirFoundType != DT_LNK) |
889 | return jDirFoundIno; | |
06d01dbe WD |
890 | |
891 | /* it's a soft link so we follow it again. */ | |
892 | b2 = pL->frag.listHead; | |
fe8c2806 | 893 | while (b2) { |
998eaaec WD |
894 | jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset); |
895 | if (jNode->ino == jDirFoundIno) { | |
2729af9d | 896 | src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode); |
fe8c2806 WD |
897 | |
898 | #if 0 | |
899 | putLabeledWord("\t\t dsize = ", jNode->dsize); | |
900 | putstr("\t\t target = "); | |
901 | putnstr(src, jNode->dsize); | |
902 | putstr("\r\n"); | |
903 | #endif | |
904 | strncpy(tmp, src, jNode->dsize); | |
905 | tmp[jNode->dsize] = '\0'; | |
998eaaec | 906 | put_fl_mem(jNode); |
fe8c2806 WD |
907 | break; |
908 | } | |
909 | b2 = b2->next; | |
998eaaec | 910 | put_fl_mem(jNode); |
fe8c2806 WD |
911 | } |
912 | /* ok so the name of the new file to find is in tmp */ | |
913 | /* if it starts with a slash it is root based else shared dirs */ | |
914 | if (tmp[0] == '/') | |
915 | pino = 1; | |
916 | else | |
998eaaec | 917 | pino = jDirFoundPino; |
fe8c2806 WD |
918 | |
919 | return jffs2_1pass_search_inode(pL, tmp, pino); | |
920 | } | |
921 | ||
922 | static u32 | |
923 | jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino) | |
924 | { | |
925 | int i; | |
926 | char tmp[256]; | |
927 | char working_tmp[256]; | |
928 | char *c; | |
929 | ||
930 | /* discard any leading slash */ | |
931 | i = 0; | |
932 | while (fname[i] == '/') | |
933 | i++; | |
934 | strcpy(tmp, &fname[i]); | |
935 | working_tmp[0] = '\0'; | |
936 | while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ | |
937 | { | |
938 | strncpy(working_tmp, tmp, c - tmp); | |
939 | working_tmp[c - tmp] = '\0'; | |
940 | for (i = 0; i < strlen(c) - 1; i++) | |
941 | tmp[i] = c[i + 1]; | |
942 | tmp[i] = '\0'; | |
943 | /* only a failure if we arent looking at top level */ | |
06d01dbe WD |
944 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && |
945 | (working_tmp[0])) { | |
fe8c2806 WD |
946 | putstr("find_inode failed for name="); |
947 | putstr(working_tmp); | |
948 | putstr("\r\n"); | |
949 | return 0; | |
950 | } | |
951 | } | |
952 | ||
953 | if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { | |
954 | putstr("find_inode failed for name="); | |
955 | putstr(tmp); | |
956 | putstr("\r\n"); | |
957 | return 0; | |
958 | } | |
959 | /* this is for the bare filename, directories have already been mapped */ | |
960 | if (!(pino = jffs2_1pass_list_inodes(pL, pino))) { | |
961 | putstr("find_inode failed for name="); | |
962 | putstr(tmp); | |
963 | putstr("\r\n"); | |
964 | return 0; | |
965 | } | |
966 | return pino; | |
967 | ||
968 | } | |
969 | ||
970 | unsigned char | |
971 | jffs2_1pass_rescan_needed(struct part_info *part) | |
972 | { | |
973 | struct b_node *b; | |
998eaaec | 974 | struct jffs2_unknown_node onode; |
fe8c2806 | 975 | struct jffs2_unknown_node *node; |
06d01dbe | 976 | struct b_lists *pL = (struct b_lists *)part->jffs2_priv; |
fe8c2806 WD |
977 | |
978 | if (part->jffs2_priv == 0){ | |
979 | DEBUGF ("rescan: First time in use\n"); | |
980 | return 1; | |
981 | } | |
982 | /* if we have no list, we need to rescan */ | |
06d01dbe | 983 | if (pL->frag.listCount == 0) { |
fe8c2806 WD |
984 | DEBUGF ("rescan: fraglist zero\n"); |
985 | return 1; | |
986 | } | |
987 | ||
06d01dbe | 988 | /* or if we are scanning a new partition */ |
fe8c2806 WD |
989 | if (pL->partOffset != part->offset) { |
990 | DEBUGF ("rescan: different partition\n"); | |
991 | return 1; | |
992 | } | |
998eaaec WD |
993 | |
994 | #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) | |
995 | if (nanddev != (int)part->usr_priv - 1) { | |
996 | DEBUGF ("rescan: nand device changed\n"); | |
997 | return -1; | |
998 | } | |
999 | #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ | |
1000 | ||
06d01dbe WD |
1001 | /* but suppose someone reflashed a partition at the same offset... */ |
1002 | b = pL->dir.listHead; | |
fe8c2806 | 1003 | while (b) { |
998eaaec WD |
1004 | node = (struct jffs2_unknown_node *) get_fl_mem(b->offset, |
1005 | sizeof(onode), &onode); | |
fe8c2806 | 1006 | if (node->nodetype != JFFS2_NODETYPE_DIRENT) { |
06d01dbe WD |
1007 | DEBUGF ("rescan: fs changed beneath me? (%lx)\n", |
1008 | (unsigned long) b->offset); | |
fe8c2806 WD |
1009 | return 1; |
1010 | } | |
1011 | b = b->next; | |
1012 | } | |
1013 | return 0; | |
1014 | } | |
1015 | ||
06d01dbe WD |
1016 | #ifdef DEBUG_FRAGMENTS |
1017 | static void | |
1018 | dump_fragments(struct b_lists *pL) | |
1019 | { | |
1020 | struct b_node *b; | |
998eaaec | 1021 | struct jffs2_raw_inode ojNode; |
06d01dbe WD |
1022 | struct jffs2_raw_inode *jNode; |
1023 | ||
1024 | putstr("\r\n\r\n******The fragment Entries******\r\n"); | |
1025 | b = pL->frag.listHead; | |
1026 | while (b) { | |
998eaaec WD |
1027 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
1028 | sizeof(ojNode), &ojNode); | |
06d01dbe WD |
1029 | putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset); |
1030 | putLabeledWord("\tbuild_list: totlen = ", jNode->totlen); | |
1031 | putLabeledWord("\tbuild_list: inode = ", jNode->ino); | |
1032 | putLabeledWord("\tbuild_list: version = ", jNode->version); | |
1033 | putLabeledWord("\tbuild_list: isize = ", jNode->isize); | |
1034 | putLabeledWord("\tbuild_list: atime = ", jNode->atime); | |
1035 | putLabeledWord("\tbuild_list: offset = ", jNode->offset); | |
1036 | putLabeledWord("\tbuild_list: csize = ", jNode->csize); | |
1037 | putLabeledWord("\tbuild_list: dsize = ", jNode->dsize); | |
1038 | putLabeledWord("\tbuild_list: compr = ", jNode->compr); | |
1039 | putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr); | |
1040 | putLabeledWord("\tbuild_list: flags = ", jNode->flags); | |
8bde7f77 | 1041 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
06d01dbe WD |
1042 | b = b->next; |
1043 | } | |
1044 | } | |
1045 | #endif | |
1046 | ||
1047 | #ifdef DEBUG_DIRENTS | |
1048 | static void | |
1049 | dump_dirents(struct b_lists *pL) | |
1050 | { | |
1051 | struct b_node *b; | |
1052 | struct jffs2_raw_dirent *jDir; | |
1053 | ||
1054 | putstr("\r\n\r\n******The directory Entries******\r\n"); | |
1055 | b = pL->dir.listHead; | |
1056 | while (b) { | |
998eaaec | 1057 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); |
06d01dbe WD |
1058 | putstr("\r\n"); |
1059 | putnstr(jDir->name, jDir->nsize); | |
1060 | putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic); | |
1061 | putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype); | |
1062 | putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc); | |
1063 | putLabeledWord("\tbuild_list: pino = ", jDir->pino); | |
1064 | putLabeledWord("\tbuild_list: version = ", jDir->version); | |
1065 | putLabeledWord("\tbuild_list: ino = ", jDir->ino); | |
1066 | putLabeledWord("\tbuild_list: mctime = ", jDir->mctime); | |
1067 | putLabeledWord("\tbuild_list: nsize = ", jDir->nsize); | |
1068 | putLabeledWord("\tbuild_list: type = ", jDir->type); | |
1069 | putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc); | |
1070 | putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc); | |
8bde7f77 | 1071 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
06d01dbe | 1072 | b = b->next; |
998eaaec | 1073 | put_fl_mem(jDir); |
06d01dbe WD |
1074 | } |
1075 | } | |
1076 | #endif | |
1077 | ||
fe8c2806 WD |
1078 | static u32 |
1079 | jffs2_1pass_build_lists(struct part_info * part) | |
1080 | { | |
1081 | struct b_lists *pL; | |
1082 | struct jffs2_unknown_node *node; | |
06d01dbe | 1083 | u32 offset, oldoffset = 0; |
fe8c2806 WD |
1084 | u32 max = part->size - sizeof(struct jffs2_raw_inode); |
1085 | u32 counter = 0; | |
1086 | u32 counter4 = 0; | |
1087 | u32 counterF = 0; | |
1088 | u32 counterN = 0; | |
1089 | ||
998eaaec WD |
1090 | #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) |
1091 | nanddev = (int)part->usr_priv - 1; | |
1092 | #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ | |
1093 | ||
fe8c2806 WD |
1094 | /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */ |
1095 | /* jffs2 list building enterprise nope. in newer versions the overhead is */ | |
1096 | /* only about 5 %. not enough to inconvenience people for. */ | |
1097 | /* lcd_off(); */ | |
1098 | ||
1099 | /* if we are building a list we need to refresh the cache. */ | |
fe8c2806 | 1100 | jffs_init_1pass_list(part); |
06d01dbe | 1101 | pL = (struct b_lists *)part->jffs2_priv; |
fe8c2806 WD |
1102 | pL->partOffset = part->offset; |
1103 | offset = 0; | |
4b9206ed | 1104 | puts ("Scanning JFFS2 FS: "); |
fe8c2806 WD |
1105 | |
1106 | /* start at the beginning of the partition */ | |
1107 | while (offset < max) { | |
06d01dbe WD |
1108 | if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) { |
1109 | printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]); | |
1110 | oldoffset = offset; | |
1111 | } | |
fc1cfcdb | 1112 | |
998eaaec | 1113 | node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset); |
fc1cfcdb | 1114 | if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) { |
fe8c2806 | 1115 | /* if its a fragment add it */ |
fc1cfcdb | 1116 | if (node->nodetype == JFFS2_NODETYPE_INODE && |
06d01dbe WD |
1117 | inode_crc((struct jffs2_raw_inode *) node)) { |
1118 | if (insert_node(&pL->frag, (u32) part->offset + | |
998eaaec WD |
1119 | offset) == NULL) { |
1120 | put_fl_mem(node); | |
fe8c2806 | 1121 | return 0; |
998eaaec | 1122 | } |
fe8c2806 WD |
1123 | } else if (node->nodetype == JFFS2_NODETYPE_DIRENT && |
1124 | dirent_crc((struct jffs2_raw_dirent *) node) && | |
1125 | dirent_name_crc((struct jffs2_raw_dirent *) node)) { | |
fc1cfcdb | 1126 | if (! (counterN%100)) |
4b9206ed | 1127 | puts ("\b\b. "); |
06d01dbe | 1128 | if (insert_node(&pL->dir, (u32) part->offset + |
998eaaec WD |
1129 | offset) == NULL) { |
1130 | put_fl_mem(node); | |
fe8c2806 | 1131 | return 0; |
998eaaec | 1132 | } |
fe8c2806 WD |
1133 | counterN++; |
1134 | } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) { | |
1135 | if (node->totlen != sizeof(struct jffs2_unknown_node)) | |
06d01dbe WD |
1136 | printf("OOPS Cleanmarker has bad size " |
1137 | "%d != %d\n", node->totlen, | |
1138 | sizeof(struct jffs2_unknown_node)); | |
7205e407 WD |
1139 | } else if (node->nodetype == JFFS2_NODETYPE_PADDING) { |
1140 | if (node->totlen < sizeof(struct jffs2_unknown_node)) | |
1141 | printf("OOPS Padding has bad size " | |
1142 | "%d < %d\n", node->totlen, | |
1143 | sizeof(struct jffs2_unknown_node)); | |
fe8c2806 | 1144 | } else { |
fc1cfcdb WD |
1145 | printf("Unknown node type: %x len %d " |
1146 | "offset 0x%x\n", node->nodetype, | |
1147 | node->totlen, offset); | |
fe8c2806 WD |
1148 | } |
1149 | offset += ((node->totlen + 3) & ~3); | |
1150 | counterF++; | |
06d01dbe WD |
1151 | } else if (node->magic == JFFS2_EMPTY_BITMASK && |
1152 | node->nodetype == JFFS2_EMPTY_BITMASK) { | |
fe8c2806 | 1153 | offset = jffs2_scan_empty(offset, part); |
fc1cfcdb | 1154 | } else { /* if we know nothing, we just step and look. */ |
fe8c2806 WD |
1155 | offset += 4; |
1156 | counter4++; | |
1157 | } | |
1158 | /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */ | |
998eaaec | 1159 | put_fl_mem(node); |
fe8c2806 WD |
1160 | } |
1161 | ||
1162 | putstr("\b\b done.\r\n"); /* close off the dots */ | |
1163 | /* turn the lcd back on. */ | |
1164 | /* splash(); */ | |
1165 | ||
1166 | #if 0 | |
06d01dbe WD |
1167 | putLabeledWord("dir entries = ", pL->dir.listCount); |
1168 | putLabeledWord("frag entries = ", pL->frag.listCount); | |
fe8c2806 WD |
1169 | putLabeledWord("+4 increments = ", counter4); |
1170 | putLabeledWord("+file_offset increments = ", counterF); | |
1171 | ||
1172 | #endif | |
1173 | ||
06d01dbe WD |
1174 | #ifdef DEBUG_DIRENTS |
1175 | dump_dirents(pL); | |
1176 | #endif | |
fe8c2806 | 1177 | |
06d01dbe WD |
1178 | #ifdef DEBUG_FRAGMENTS |
1179 | dump_fragments(pL); | |
1180 | #endif | |
fe8c2806 | 1181 | |
fe8c2806 WD |
1182 | /* give visual feedback that we are done scanning the flash */ |
1183 | led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */ | |
1184 | return 1; | |
1185 | } | |
1186 | ||
1187 | ||
fe8c2806 WD |
1188 | static u32 |
1189 | jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) | |
1190 | { | |
1191 | struct b_node *b; | |
998eaaec | 1192 | struct jffs2_raw_inode ojNode; |
fe8c2806 WD |
1193 | struct jffs2_raw_inode *jNode; |
1194 | int i; | |
1195 | ||
fe8c2806 WD |
1196 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
1197 | piL->compr_info[i].num_frags = 0; | |
1198 | piL->compr_info[i].compr_sum = 0; | |
1199 | piL->compr_info[i].decompr_sum = 0; | |
1200 | } | |
1201 | ||
06d01dbe | 1202 | b = pL->frag.listHead; |
fe8c2806 | 1203 | while (b) { |
998eaaec WD |
1204 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
1205 | sizeof(ojNode), &ojNode); | |
fe8c2806 WD |
1206 | if (jNode->compr < JFFS2_NUM_COMPR) { |
1207 | piL->compr_info[jNode->compr].num_frags++; | |
1208 | piL->compr_info[jNode->compr].compr_sum += jNode->csize; | |
1209 | piL->compr_info[jNode->compr].decompr_sum += jNode->dsize; | |
1210 | } | |
1211 | b = b->next; | |
1212 | } | |
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | ||
fe8c2806 WD |
1217 | static struct b_lists * |
1218 | jffs2_get_list(struct part_info * part, const char *who) | |
1219 | { | |
1220 | if (jffs2_1pass_rescan_needed(part)) { | |
1221 | if (!jffs2_1pass_build_lists(part)) { | |
1222 | printf("%s: Failed to scan JFFSv2 file structure\n", who); | |
1223 | return NULL; | |
1224 | } | |
1225 | } | |
1226 | return (struct b_lists *)part->jffs2_priv; | |
1227 | } | |
1228 | ||
1229 | ||
1230 | /* Print directory / file contents */ | |
1231 | u32 | |
1232 | jffs2_1pass_ls(struct part_info * part, const char *fname) | |
1233 | { | |
1234 | struct b_lists *pl; | |
fc1cfcdb | 1235 | long ret = 0; |
fe8c2806 WD |
1236 | u32 inode; |
1237 | ||
06d01dbe | 1238 | if (! (pl = jffs2_get_list(part, "ls"))) |
fe8c2806 WD |
1239 | return 0; |
1240 | ||
1241 | if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) { | |
1242 | putstr("ls: Failed to scan jffs2 file structure\r\n"); | |
1243 | return 0; | |
1244 | } | |
fc1cfcdb WD |
1245 | |
1246 | ||
fe8c2806 WD |
1247 | #if 0 |
1248 | putLabeledWord("found file at inode = ", inode); | |
1249 | putLabeledWord("read_inode returns = ", ret); | |
1250 | #endif | |
fc1cfcdb WD |
1251 | |
1252 | return ret; | |
fe8c2806 WD |
1253 | } |
1254 | ||
1255 | ||
fe8c2806 WD |
1256 | /* Load a file from flash into memory. fname can be a full path */ |
1257 | u32 | |
1258 | jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) | |
1259 | { | |
1260 | ||
1261 | struct b_lists *pl; | |
1262 | long ret = 0; | |
1263 | u32 inode; | |
1264 | ||
1265 | if (! (pl = jffs2_get_list(part, "load"))) | |
1266 | return 0; | |
1267 | ||
1268 | if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) { | |
1269 | putstr("load: Failed to find inode\r\n"); | |
1270 | return 0; | |
1271 | } | |
1272 | ||
1273 | /* Resolve symlinks */ | |
1274 | if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) { | |
1275 | putstr("load: Failed to resolve inode structure\r\n"); | |
1276 | return 0; | |
1277 | } | |
1278 | ||
1279 | if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) { | |
1280 | putstr("load: Failed to read inode\r\n"); | |
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname, | |
1285 | (unsigned long) dest, ret); | |
1286 | return ret; | |
1287 | } | |
1288 | ||
1289 | /* Return information about the fs on this partition */ | |
1290 | u32 | |
1291 | jffs2_1pass_info(struct part_info * part) | |
1292 | { | |
1293 | struct b_jffs2_info info; | |
1294 | struct b_lists *pl; | |
1295 | int i; | |
1296 | ||
1297 | if (! (pl = jffs2_get_list(part, "info"))) | |
1298 | return 0; | |
1299 | ||
1300 | jffs2_1pass_fill_info(pl, &info); | |
06d01dbe | 1301 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
4b9206ed WD |
1302 | printf ("Compression: %s\n" |
1303 | "\tfrag count: %d\n" | |
1304 | "\tcompressed sum: %d\n" | |
1305 | "\tuncompressed sum: %d\n", | |
1306 | compr_names[i], | |
1307 | info.compr_info[i].num_frags, | |
1308 | info.compr_info[i].compr_sum, | |
1309 | info.compr_info[i].decompr_sum); | |
fe8c2806 WD |
1310 | } |
1311 | return 1; | |
1312 | } | |
1313 | ||
1314 | #endif /* CFG_CMD_JFFS2 */ |