blob: 5d0192cb801da8e7a7924a9977db18935aeec2f3 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2-------------------------------------------------------------------------
3 * Filename: jffs2.c
4 * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
5 * Copyright: Copyright (C) 2001, Russ Dill
6 * Author: Russ Dill <Russ.Dill@asu.edu>
7 * Description: Module to load kernel from jffs2
8 *-----------------------------------------------------------------------*/
9/*
10 * some portions of this code are taken from jffs2, and as such, the
11 * following copyright notice is included.
12 *
13 * JFFS2 -- Journalling Flash File System, Version 2.
14 *
15 * Copyright (C) 2001 Red Hat, Inc.
16 *
17 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
18 *
19 * The original JFFS, from which the design for JFFS2 was derived,
20 * was designed and implemented by Axis Communications AB.
21 *
22 * The contents of this file are subject to the Red Hat eCos Public
23 * License Version 1.1 (the "Licence"); you may not use this file
24 * except in compliance with the Licence. You may obtain a copy of
25 * the Licence at http://www.redhat.com/
26 *
27 * Software distributed under the Licence is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
29 * See the Licence for the specific language governing rights and
30 * limitations under the Licence.
31 *
32 * The Original Code is JFFS2 - Journalling Flash File System, version 2
33 *
34 * Alternatively, the contents of this file may be used under the
35 * terms of the GNU General Public License version 2 (the "GPL"), in
36 * which case the provisions of the GPL are applicable instead of the
37 * above. If you wish to allow the use of your version of this file
38 * only under the terms of the GPL and not to allow others to use your
39 * version of this file under the RHEPL, indicate your decision by
40 * deleting the provisions above and replace them with the notice and
41 * other provisions required by the GPL. If you do not delete the
42 * provisions above, a recipient may use your version of this file
43 * under either the RHEPL or the GPL.
44 *
45 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
46 *
47 */
48
49/* Ok, so anyone who knows the jffs2 code will probably want to get a papar
50 * bag to throw up into before reading this code. I looked through the jffs2
51 * code, the caching scheme is very elegant. I tried to keep the version
52 * for a bootloader as small and simple as possible. Instead of worring about
53 * unneccesary data copies, node scans, etc, I just optimized for the known
54 * common case, a kernel, which looks like:
55 * (1) most pages are 4096 bytes
56 * (2) version numbers are somewhat sorted in acsending order
57 * (3) multiple compressed blocks making up one page is uncommon
58 *
59 * So I create a linked list of decending version numbers (insertions at the
60 * head), and then for each page, walk down the list, until a matching page
61 * with 4096 bytes is found, and then decompress the watching pages in
62 * reverse order.
63 *
64 */
65
66/*
67 * Adapted by Nye Liu <nyet@zumanetworks.com> and
68 * Rex Feany <rfeany@zumanetworks.com>
69 * on Jan/2002 for U-Boot.
70 *
71 * Clipped out all the non-1pass functions, cleaned up warnings,
72 * wrappers, etc. No major changes to the code.
73 * Please, he really means it when he said have a paper bag
74 * handy. We needed it ;).
75 *
76 */
77
wdenk06d01db2003-03-14 20:47:52 +000078/*
79 * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
80 *
81 * - overhaul of the memory management. Removed much of the "paper-bagging"
82 * in that part of the code, fixed several bugs, now frees memory when
83 * partition is changed.
84 * It's still ugly :-(
85 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
86 * was incorrect. Removed a bit of the paper-bagging as well.
87 * - removed double crc calculation for fragment headers in jffs2_private.h
88 * for speedup.
89 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
90 * - spinning wheel now spins depending on how much memory has been scanned
91 * - lots of small changes all over the place to "improve" readability.
92 * - implemented fragment sorting to ensure that the newest data is copied
93 * if there are multiple copies of fragments for a certain file offset.
94 *
95 * The fragment sorting feature must be enabled by 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
wdenkdd875c72004-01-03 21:24:46 +0000109 * might be incompatible with any new code from the jffs2 maintainers anyway,
110 * it should probably be dumped and replaced by something like jffs2reader!
wdenk06d01db2003-03-14 20:47:52 +0000111 */
112
113
wdenkfe8c2802002-11-03 00:38:21 +0000114#include <common.h>
115#include <config.h>
116#include <malloc.h>
wdenk0b8fa032004-04-25 14:37:29 +0000117#include <watchdog.h>
wdenkfe8c2802002-11-03 00:38:21 +0000118#include <linux/stat.h>
119#include <linux/time.h>
120
121#if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
122
123#include <jffs2/jffs2.h>
124#include <jffs2/jffs2_1pass.h>
125
126#include "jffs2_private.h"
127
wdenkfe8c2802002-11-03 00:38:21 +0000128
wdenk06d01db2003-03-14 20:47:52 +0000129#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
wdenk0b8fa032004-04-25 14:37:29 +0000130#define SPIN_BLKSIZE 20 /* spin after having scanned 1<<BLKSIZE bytes */
wdenkfe8c2802002-11-03 00:38:21 +0000131
wdenk06d01db2003-03-14 20:47:52 +0000132/* Debugging switches */
133#undef DEBUG_DIRENTS /* print directory entry list after scan */
134#undef DEBUG_FRAGMENTS /* print fragment list after scan */
135#undef DEBUG /* enable debugging messages */
136
137
wdenkfe8c2802002-11-03 00:38:21 +0000138#ifdef DEBUG
139# define DEBUGF(fmt,args...) printf(fmt ,##args)
140#else
141# define DEBUGF(fmt,args...)
142#endif
143
wdenk998eaae2004-04-18 19:43:36 +0000144#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
145
146/*
147 * Support for jffs2 on top of NAND-flash
148 *
149 * NAND memory isn't mapped in processor's address space,
150 * so data should be fetched from flash before
151 * being processed. This is exactly what functions declared
152 * here do.
153 *
154 */
155
156/* this one defined in cmd_nand.c */
157int read_jffs2_nand(size_t start, size_t len,
158 size_t * retlen, u_char * buf, int nanddev);
159
160#define NAND_PAGE_SIZE 512
161#define NAND_PAGE_SHIFT 9
162#define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
163
164#ifndef NAND_CACHE_PAGES
165#define NAND_CACHE_PAGES 16
166#endif
167#define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
168
169static u8* nand_cache = NULL;
170static u32 nand_cache_off = (u32)-1;
171static int nanddev = 0; /* nand device of current partition */
172
173static int read_nand_cached(u32 off, u32 size, u_char *buf)
174{
175 u32 bytes_read = 0;
176 size_t retlen;
177 int cpy_bytes;
178
179 while (bytes_read < size) {
180 if ((off + bytes_read < nand_cache_off) ||
181 (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
182 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
183 if (!nand_cache) {
184 /* This memory never gets freed but 'cause
185 it's a bootloader, nobody cares */
wdenk507bbe32004-04-18 21:13:41 +0000186 nand_cache = malloc(NAND_CACHE_SIZE);
187 if (!nand_cache) {
wdenk998eaae2004-04-18 19:43:36 +0000188 printf("read_nand_cached: can't alloc cache size %d bytes\n",
189 NAND_CACHE_SIZE);
190 return -1;
191 }
192 }
193 if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE,
194 &retlen, nand_cache, nanddev) < 0 ||
195 retlen != NAND_CACHE_SIZE) {
196 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
197 nand_cache_off, NAND_CACHE_SIZE);
198 return -1;
199 }
200 }
201 cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
202 if (cpy_bytes > size - bytes_read)
203 cpy_bytes = size - bytes_read;
204 memcpy(buf + bytes_read,
205 nand_cache + off + bytes_read - nand_cache_off,
206 cpy_bytes);
207 bytes_read += cpy_bytes;
208 }
209 return bytes_read;
210}
211
212static void *get_fl_mem(u32 off, u32 size, void *ext_buf)
213{
214 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
215
216 if (NULL == buf) {
217 printf("get_fl_mem: can't alloc %d bytes\n", size);
218 return NULL;
219 }
220 if (read_nand_cached(off, size, buf) < 0) {
221 free(buf);
222 return NULL;
223 }
224
225 return buf;
226}
227
228static 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
wdenk507bbe32004-04-18 21:13:41 +0000245static void put_fl_mem(void *buf)
wdenk998eaae2004-04-18 19:43:36 +0000246{
247 free(buf);
248}
249
250#else /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
251
252static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
253{
254 return (void*)off;
255}
256
257static inline void *get_node_mem(u32 off)
258{
259 return (void*)off;
260}
261
wdenk507bbe32004-04-18 21:13:41 +0000262static inline void put_fl_mem(void *buf)
wdenk998eaae2004-04-18 19:43:36 +0000263{
264}
265
266#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
267
wdenkfe8c2802002-11-03 00:38:21 +0000268
wdenk06d01db2003-03-14 20:47:52 +0000269/* Compression names */
270static char *compr_names[] = {
271 "NONE",
272 "ZERO",
273 "RTIME",
274 "RUBINMIPS",
275 "COPY",
276 "DYNRUBIN",
277 "ZLIB"
278};
279
wdenk0b8fa032004-04-25 14:37:29 +0000280#if 0 /* Use spinning wheel */
wdenk06d01db2003-03-14 20:47:52 +0000281/* Spinning wheel */
282static char spinner[] = { '|', '/', '-', '\\' };
wdenk0b8fa032004-04-25 14:37:29 +0000283#endif
wdenk06d01db2003-03-14 20:47:52 +0000284
285/* Memory management */
286struct mem_block {
287 u32 index;
288 struct mem_block *next;
289 struct b_node nodes[NODE_CHUNK];
290};
291
292
293static void
294free_nodes(struct b_list *list)
295{
296 while (list->listMemBase != NULL) {
297 struct mem_block *next = list->listMemBase->next;
298 free( list->listMemBase );
299 list->listMemBase = next;
300 }
301}
wdenkfe8c2802002-11-03 00:38:21 +0000302
303static struct b_node *
wdenk06d01db2003-03-14 20:47:52 +0000304add_node(struct b_list *list)
wdenkfe8c2802002-11-03 00:38:21 +0000305{
wdenk06d01db2003-03-14 20:47:52 +0000306 u32 index = 0;
307 struct mem_block *memBase;
wdenkfe8c2802002-11-03 00:38:21 +0000308 struct b_node *b;
309
wdenk06d01db2003-03-14 20:47:52 +0000310 memBase = list->listMemBase;
311 if (memBase != NULL)
312 index = memBase->index;
wdenkfe8c2802002-11-03 00:38:21 +0000313#if 0
314 putLabeledWord("add_node: index = ", index);
wdenk06d01db2003-03-14 20:47:52 +0000315 putLabeledWord("add_node: memBase = ", list->listMemBase);
wdenkfe8c2802002-11-03 00:38:21 +0000316#endif
317
wdenk06d01db2003-03-14 20:47:52 +0000318 if (memBase == NULL || index >= NODE_CHUNK) {
319 /* we need more space before we continue */
320 memBase = mmalloc(sizeof(struct mem_block));
321 if (memBase == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +0000322 putstr("add_node: malloc failed\n");
323 return NULL;
324 }
wdenk06d01db2003-03-14 20:47:52 +0000325 memBase->next = list->listMemBase;
326 index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000327#if 0
328 putLabeledWord("add_node: alloced a new membase at ", *memBase);
329#endif
330
331 }
332 /* now we have room to add it. */
wdenk06d01db2003-03-14 20:47:52 +0000333 b = &memBase->nodes[index];
334 index ++;
wdenkfe8c2802002-11-03 00:38:21 +0000335
wdenk06d01db2003-03-14 20:47:52 +0000336 memBase->index = index;
337 list->listMemBase = memBase;
338 list->listCount++;
wdenkfe8c2802002-11-03 00:38:21 +0000339 return b;
wdenkfe8c2802002-11-03 00:38:21 +0000340}
341
wdenk06d01db2003-03-14 20:47:52 +0000342static struct b_node *
343insert_node(struct b_list *list, u32 offset)
344{
345 struct b_node *new;
346#ifdef CFG_JFFS2_SORT_FRAGMENTS
347 struct b_node *b, *prev;
348#endif
349
350 if (!(new = add_node(list))) {
351 putstr("add_node failed!\r\n");
352 return NULL;
353 }
354 new->offset = offset;
355
356#ifdef CFG_JFFS2_SORT_FRAGMENTS
357 if (list->listTail != NULL && list->listCompare(new, list->listTail))
358 prev = list->listTail;
359 else if (list->listLast != NULL && list->listCompare(new, list->listLast))
360 prev = list->listLast;
361 else
362 prev = NULL;
363
364 for (b = (prev ? prev->next : list->listHead);
365 b != NULL && list->listCompare(new, b);
366 prev = b, b = b->next) {
367 list->listLoops++;
368 }
369 if (b != NULL)
370 list->listLast = prev;
371
372 if (b != NULL) {
373 new->next = b;
374 if (prev != NULL)
375 prev->next = new;
376 else
377 list->listHead = new;
378 } else
379#endif
380 {
381 new->next = (struct b_node *) NULL;
382 if (list->listTail != NULL) {
383 list->listTail->next = new;
384 list->listTail = new;
385 } else {
386 list->listTail = list->listHead = new;
387 }
388 }
389
390 return new;
391}
392
393#ifdef CFG_JFFS2_SORT_FRAGMENTS
wdenk7205e402003-09-10 22:30:53 +0000394/* Sort data entries with the latest version last, so that if there
395 * is overlapping data the latest version will be used.
396 */
wdenk06d01db2003-03-14 20:47:52 +0000397static int compare_inodes(struct b_node *new, struct b_node *old)
398{
wdenk998eaae2004-04-18 19:43:36 +0000399 struct jffs2_raw_inode ojNew;
400 struct jffs2_raw_inode ojOld;
401 struct jffs2_raw_inode *jNew =
402 (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
403 struct jffs2_raw_inode *jOld =
404 (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
wdenk06d01db2003-03-14 20:47:52 +0000405
wdenk7205e402003-09-10 22:30:53 +0000406 return jNew->version > jOld->version;
wdenk06d01db2003-03-14 20:47:52 +0000407}
408
wdenk7205e402003-09-10 22:30:53 +0000409/* Sort directory entries so all entries in the same directory
410 * with the same name are grouped together, with the latest version
411 * last. This makes it easy to eliminate all but the latest version
412 * by marking the previous version dead by setting the inode to 0.
413 */
wdenk06d01db2003-03-14 20:47:52 +0000414static int compare_dirents(struct b_node *new, struct b_node *old)
415{
wdenk998eaae2004-04-18 19:43:36 +0000416 struct jffs2_raw_dirent ojNew;
417 struct jffs2_raw_dirent ojOld;
418 struct jffs2_raw_dirent *jNew =
419 (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
wdenk507bbe32004-04-18 21:13:41 +0000420 struct jffs2_raw_dirent *jOld =
wdenk998eaae2004-04-18 19:43:36 +0000421 (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
wdenk7205e402003-09-10 22:30:53 +0000422 int cmp;
wdenk06d01db2003-03-14 20:47:52 +0000423
wdenk7205e402003-09-10 22:30:53 +0000424 /* ascending sort by pino */
425 if (jNew->pino != jOld->pino)
426 return jNew->pino > jOld->pino;
427
428 /* pino is the same, so use ascending sort by nsize, so
429 * we don't do strncmp unless we really must.
430 */
431 if (jNew->nsize != jOld->nsize)
432 return jNew->nsize > jOld->nsize;
433
434 /* length is also the same, so use ascending sort by name
435 */
436 cmp = strncmp(jNew->name, jOld->name, jNew->nsize);
437 if (cmp != 0)
438 return cmp > 0;
439
440 /* we have duplicate names in this directory, so use ascending
441 * sort by version
442 */
443 if (jNew->version > jOld->version) {
444 /* since jNew is newer, we know jOld is not valid, so
445 * mark it with inode 0 and it will not be used
446 */
447 jOld->ino = 0;
448 return 1;
449 }
wdenk42d1f032003-10-15 23:53:47 +0000450
wdenk7205e402003-09-10 22:30:53 +0000451 return 0;
wdenk06d01db2003-03-14 20:47:52 +0000452}
453#endif
wdenkfe8c2802002-11-03 00:38:21 +0000454
455static u32
456jffs2_scan_empty(u32 start_offset, struct part_info *part)
457{
wdenk06d01db2003-03-14 20:47:52 +0000458 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
459 char *offset = part->offset + start_offset;
wdenk0b8fa032004-04-25 14:37:29 +0000460 int cntr = 0;
wdenk998eaae2004-04-18 19:43:36 +0000461 u32 off;
wdenkfe8c2802002-11-03 00:38:21 +0000462
wdenk998eaae2004-04-18 19:43:36 +0000463 while (offset < max &&
464 *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) {
wdenk06d01db2003-03-14 20:47:52 +0000465 offset += sizeof(u32);
wdenk0b8fa032004-04-25 14:37:29 +0000466 cntr++;
467#if 0 /* Use spinning wheel */
wdenk06d01db2003-03-14 20:47:52 +0000468 /* return if spinning is due */
469 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
wdenk0b8fa032004-04-25 14:37:29 +0000470#endif
471 if (cntr > 1024 && part->erasesize > 0) { /* 4k */
472 /* round up to next erase block border */
473 (u32)offset |= part->erasesize-1;
474 offset++;
475 cntr = 0;
476 }
wdenkfe8c2802002-11-03 00:38:21 +0000477 }
wdenk06d01db2003-03-14 20:47:52 +0000478 return offset - part->offset;
wdenkfe8c2802002-11-03 00:38:21 +0000479}
480
481static u32
482jffs_init_1pass_list(struct part_info *part)
483{
wdenk06d01db2003-03-14 20:47:52 +0000484 struct b_lists *pL;
wdenkfe8c2802002-11-03 00:38:21 +0000485
wdenk06d01db2003-03-14 20:47:52 +0000486 if (part->jffs2_priv != NULL) {
487 pL = (struct b_lists *)part->jffs2_priv;
488 free_nodes(&pL->frag);
489 free_nodes(&pL->dir);
490 free(pL);
491 }
492 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
493 pL = (struct b_lists *)part->jffs2_priv;
494
495 memset(pL, 0, sizeof(*pL));
496#ifdef CFG_JFFS2_SORT_FRAGMENTS
497 pL->dir.listCompare = compare_dirents;
498 pL->frag.listCompare = compare_inodes;
499#endif
wdenkfe8c2802002-11-03 00:38:21 +0000500 }
501 return 0;
502}
503
504/* find the inode from the slashless name given a parent */
505static long
506jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
507{
508 struct b_node *b;
509 struct jffs2_raw_inode *jNode;
wdenk06d01db2003-03-14 20:47:52 +0000510 u32 totalSize = 0;
wdenk7205e402003-09-10 22:30:53 +0000511 u32 latestVersion = 0;
wdenk06d01db2003-03-14 20:47:52 +0000512 char *lDest;
wdenkfe8c2802002-11-03 00:38:21 +0000513 char *src;
514 long ret;
515 int i;
516 u32 counter = 0;
wdenk7205e402003-09-10 22:30:53 +0000517#ifdef CFG_JFFS2_SORT_FRAGMENTS
518 /* Find file size before loading any data, so fragments that
519 * start past the end of file can be ignored. A fragment
520 * that is partially in the file is loaded, so extra data may
521 * be loaded up to the next 4K boundary above the file size.
522 * This shouldn't cause trouble when loading kernel images, so
523 * we will live with it.
524 */
525 for (b = pL->frag.listHead; b != NULL; b = b->next) {
wdenk507bbe32004-04-18 21:13:41 +0000526 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
wdenk998eaae2004-04-18 19:43:36 +0000527 sizeof(struct jffs2_raw_inode), NULL);
wdenk7205e402003-09-10 22:30:53 +0000528 if ((inode == jNode->ino)) {
529 /* get actual file length from the newest node */
530 if (jNode->version >= latestVersion) {
531 totalSize = jNode->isize;
532 latestVersion = jNode->version;
533 }
534 }
wdenk998eaae2004-04-18 19:43:36 +0000535 put_fl_mem(jNode);
wdenk7205e402003-09-10 22:30:53 +0000536 }
537#endif
wdenkfe8c2802002-11-03 00:38:21 +0000538
wdenk06d01db2003-03-14 20:47:52 +0000539 for (b = pL->frag.listHead; b != NULL; b = b->next) {
wdenk998eaae2004-04-18 19:43:36 +0000540 jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000541 if ((inode == jNode->ino)) {
wdenk06d01db2003-03-14 20:47:52 +0000542#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000543 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
544 putLabeledWord("read_inode: inode = ", jNode->ino);
545 putLabeledWord("read_inode: version = ", jNode->version);
546 putLabeledWord("read_inode: isize = ", jNode->isize);
547 putLabeledWord("read_inode: offset = ", jNode->offset);
548 putLabeledWord("read_inode: csize = ", jNode->csize);
549 putLabeledWord("read_inode: dsize = ", jNode->dsize);
550 putLabeledWord("read_inode: compr = ", jNode->compr);
551 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
552 putLabeledWord("read_inode: flags = ", jNode->flags);
wdenkfe8c2802002-11-03 00:38:21 +0000553#endif
wdenk7205e402003-09-10 22:30:53 +0000554
555#ifndef CFG_JFFS2_SORT_FRAGMENTS
wdenk06d01db2003-03-14 20:47:52 +0000556 /* get actual file length from the newest node */
557 if (jNode->version >= latestVersion) {
wdenkfe8c2802002-11-03 00:38:21 +0000558 totalSize = jNode->isize;
wdenk06d01db2003-03-14 20:47:52 +0000559 latestVersion = jNode->version;
wdenkfe8c2802002-11-03 00:38:21 +0000560 }
wdenk7205e402003-09-10 22:30:53 +0000561#endif
wdenkfe8c2802002-11-03 00:38:21 +0000562
563 if(dest) {
564 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
wdenk06d01db2003-03-14 20:47:52 +0000565 /* ignore data behind latest known EOF */
wdenk998eaae2004-04-18 19:43:36 +0000566 if (jNode->offset > totalSize) {
567 put_fl_mem(jNode);
wdenk06d01db2003-03-14 20:47:52 +0000568 continue;
wdenk998eaae2004-04-18 19:43:36 +0000569 }
wdenk06d01db2003-03-14 20:47:52 +0000570
wdenkfe8c2802002-11-03 00:38:21 +0000571 lDest = (char *) (dest + jNode->offset);
572#if 0
wdenk06d01db2003-03-14 20:47:52 +0000573 putLabeledWord("read_inode: src = ", src);
wdenkfe8c2802002-11-03 00:38:21 +0000574 putLabeledWord("read_inode: dest = ", lDest);
wdenkfe8c2802002-11-03 00:38:21 +0000575#endif
576 switch (jNode->compr) {
577 case JFFS2_COMPR_NONE:
wdenkfe8c2802002-11-03 00:38:21 +0000578 ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
579 break;
580 case JFFS2_COMPR_ZERO:
581 ret = 0;
582 for (i = 0; i < jNode->dsize; i++)
583 *(lDest++) = 0;
584 break;
585 case JFFS2_COMPR_RTIME:
586 ret = 0;
587 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
588 break;
589 case JFFS2_COMPR_DYNRUBIN:
590 /* this is slow but it works */
591 ret = 0;
592 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
593 break;
594 case JFFS2_COMPR_ZLIB:
595 ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
596 break;
597 default:
598 /* unknown */
599 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
wdenk998eaae2004-04-18 19:43:36 +0000600 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000601 return -1;
602 break;
603 }
604 }
605
wdenkfe8c2802002-11-03 00:38:21 +0000606#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000607 putLabeledWord("read_inode: totalSize = ", totalSize);
608 putLabeledWord("read_inode: compr ret = ", ret);
609#endif
610 }
wdenkfe8c2802002-11-03 00:38:21 +0000611 counter++;
wdenk998eaae2004-04-18 19:43:36 +0000612 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000613 }
wdenkfe8c2802002-11-03 00:38:21 +0000614
615#if 0
wdenk06d01db2003-03-14 20:47:52 +0000616 putLabeledWord("read_inode: returning = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000617#endif
wdenk06d01db2003-03-14 20:47:52 +0000618 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000619}
620
621/* find the inode from the slashless name given a parent */
622static u32
623jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
624{
625 struct b_node *b;
626 struct jffs2_raw_dirent *jDir;
627 int len;
628 u32 counter;
629 u32 version = 0;
630 u32 inode = 0;
631
632 /* name is assumed slash free */
633 len = strlen(name);
634
635 counter = 0;
636 /* we need to search all and return the inode with the highest version */
wdenk06d01db2003-03-14 20:47:52 +0000637 for(b = pL->dir.listHead; b; b = b->next, counter++) {
wdenk998eaae2004-04-18 19:43:36 +0000638 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenk06d01db2003-03-14 20:47:52 +0000639 if ((pino == jDir->pino) && (len == jDir->nsize) &&
640 (jDir->ino) && /* 0 for unlink */
wdenkfe8c2802002-11-03 00:38:21 +0000641 (!strncmp(jDir->name, name, len))) { /* a match */
wdenk998eaae2004-04-18 19:43:36 +0000642 if (jDir->version < version) {
643 put_fl_mem(jDir);
wdenk7205e402003-09-10 22:30:53 +0000644 continue;
wdenk998eaae2004-04-18 19:43:36 +0000645 }
wdenkfe8c2802002-11-03 00:38:21 +0000646
wdenk7205e402003-09-10 22:30:53 +0000647 if (jDir->version == version && inode != 0) {
648 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000649 putstr(" ** ERROR ** ");
650 putnstr(jDir->name, jDir->nsize);
651 putLabeledWord(" has dup version =", version);
652 }
653 inode = jDir->ino;
654 version = jDir->version;
655 }
656#if 0
657 putstr("\r\nfind_inode:p&l ->");
658 putnstr(jDir->name, jDir->nsize);
659 putstr("\r\n");
660 putLabeledWord("pino = ", jDir->pino);
661 putLabeledWord("nsize = ", jDir->nsize);
662 putLabeledWord("b = ", (u32) b);
663 putLabeledWord("counter = ", counter);
664#endif
wdenk998eaae2004-04-18 19:43:36 +0000665 put_fl_mem(jDir);
wdenkfe8c2802002-11-03 00:38:21 +0000666 }
667 return inode;
668}
669
wdenk180d3f72004-01-04 16:28:35 +0000670char *mkmodestr(unsigned long mode, char *str)
wdenkfe8c2802002-11-03 00:38:21 +0000671{
wdenk06d01db2003-03-14 20:47:52 +0000672 static const char *l = "xwr";
673 int mask = 1, i;
674 char c;
wdenkfe8c2802002-11-03 00:38:21 +0000675
wdenk06d01db2003-03-14 20:47:52 +0000676 switch (mode & S_IFMT) {
677 case S_IFDIR: str[0] = 'd'; break;
678 case S_IFBLK: str[0] = 'b'; break;
679 case S_IFCHR: str[0] = 'c'; break;
680 case S_IFIFO: str[0] = 'f'; break;
681 case S_IFLNK: str[0] = 'l'; break;
682 case S_IFSOCK: str[0] = 's'; break;
683 case S_IFREG: str[0] = '-'; break;
684 default: str[0] = '?';
685 }
wdenkfe8c2802002-11-03 00:38:21 +0000686
wdenk06d01db2003-03-14 20:47:52 +0000687 for(i = 0; i < 9; i++) {
688 c = l[i%3];
689 str[9-i] = (mode & mask)?c:'-';
690 mask = mask<<1;
691 }
wdenkfe8c2802002-11-03 00:38:21 +0000692
wdenk06d01db2003-03-14 20:47:52 +0000693 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
694 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
695 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
696 str[10] = '\0';
697 return str;
wdenkfe8c2802002-11-03 00:38:21 +0000698}
699
700static inline void dump_stat(struct stat *st, const char *name)
701{
wdenk06d01db2003-03-14 20:47:52 +0000702 char str[20];
703 char s[64], *p;
wdenkfe8c2802002-11-03 00:38:21 +0000704
wdenk06d01db2003-03-14 20:47:52 +0000705 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
706 st->st_mtime = 1;
wdenkfe8c2802002-11-03 00:38:21 +0000707
wdenk06d01db2003-03-14 20:47:52 +0000708 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
wdenkfe8c2802002-11-03 00:38:21 +0000709
wdenk06d01db2003-03-14 20:47:52 +0000710 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
711 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000712
713/*
wdenk06d01db2003-03-14 20:47:52 +0000714 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
715 st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000716*/
717
wdenk06d01db2003-03-14 20:47:52 +0000718 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000719}
720
721static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
722{
723 char fname[256];
724 struct stat st;
725
726 if(!d || !i) return -1;
727
728 strncpy(fname, d->name, d->nsize);
wdenk06d01db2003-03-14 20:47:52 +0000729 fname[d->nsize] = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000730
731 memset(&st,0,sizeof(st));
732
wdenk06d01db2003-03-14 20:47:52 +0000733 st.st_mtime = i->mtime;
734 st.st_mode = i->mode;
735 st.st_ino = i->ino;
wdenkfe8c2802002-11-03 00:38:21 +0000736
737 /* neither dsize nor isize help us.. do it the long way */
wdenk06d01db2003-03-14 20:47:52 +0000738 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
wdenkfe8c2802002-11-03 00:38:21 +0000739
740 dump_stat(&st, fname);
741
742 if (d->type == DT_LNK) {
743 unsigned char *src = (unsigned char *) (&i[1]);
744 putstr(" -> ");
745 putnstr(src, (int)i->dsize);
746 }
747
748 putstr("\r\n");
749
750 return 0;
751}
752
753/* list inodes with the given pino */
754static u32
755jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
756{
757 struct b_node *b;
758 struct jffs2_raw_dirent *jDir;
759
wdenk06d01db2003-03-14 20:47:52 +0000760 for (b = pL->dir.listHead; b; b = b->next) {
wdenk998eaae2004-04-18 19:43:36 +0000761 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenk06d01db2003-03-14 20:47:52 +0000762 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
763 u32 i_version = 0;
wdenk998eaae2004-04-18 19:43:36 +0000764 struct jffs2_raw_inode ojNode;
wdenk06d01db2003-03-14 20:47:52 +0000765 struct jffs2_raw_inode *jNode, *i = NULL;
766 struct b_node *b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000767
768 while (b2) {
wdenk998eaae2004-04-18 19:43:36 +0000769 jNode = (struct jffs2_raw_inode *)
770 get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
wdenkfe8c2802002-11-03 00:38:21 +0000771 if (jNode->ino == jDir->ino
wdenk06d01db2003-03-14 20:47:52 +0000772 && jNode->version >= i_version)
wdenk998eaae2004-04-18 19:43:36 +0000773 i = get_fl_mem(b2->offset, sizeof(*i), NULL);
wdenkfe8c2802002-11-03 00:38:21 +0000774 b2 = b2->next;
775 }
776
777 dump_inode(pL, jDir, i);
wdenk998eaae2004-04-18 19:43:36 +0000778 put_fl_mem(i);
wdenkfe8c2802002-11-03 00:38:21 +0000779 }
wdenk998eaae2004-04-18 19:43:36 +0000780 put_fl_mem(jDir);
wdenkfe8c2802002-11-03 00:38:21 +0000781 }
782 return pino;
783}
784
785static u32
786jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
787{
788 int i;
789 char tmp[256];
790 char working_tmp[256];
791 char *c;
792
793 /* discard any leading slash */
794 i = 0;
795 while (fname[i] == '/')
796 i++;
797 strcpy(tmp, &fname[i]);
798
799 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
800 {
801 strncpy(working_tmp, tmp, c - tmp);
802 working_tmp[c - tmp] = '\0';
803#if 0
804 putstr("search_inode: tmp = ");
805 putstr(tmp);
806 putstr("\r\n");
807 putstr("search_inode: wtmp = ");
808 putstr(working_tmp);
809 putstr("\r\n");
810 putstr("search_inode: c = ");
811 putstr(c);
812 putstr("\r\n");
813#endif
814 for (i = 0; i < strlen(c) - 1; i++)
815 tmp[i] = c[i + 1];
816 tmp[i] = '\0';
817#if 0
818 putstr("search_inode: post tmp = ");
819 putstr(tmp);
820 putstr("\r\n");
821#endif
822
823 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
824 putstr("find_inode failed for name=");
825 putstr(working_tmp);
826 putstr("\r\n");
827 return 0;
828 }
829 }
830 /* this is for the bare filename, directories have already been mapped */
831 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
832 putstr("find_inode failed for name=");
833 putstr(tmp);
834 putstr("\r\n");
835 return 0;
836 }
837 return pino;
838
839}
840
841static u32
842jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
843{
844 struct b_node *b;
845 struct b_node *b2;
846 struct jffs2_raw_dirent *jDir;
847 struct jffs2_raw_inode *jNode;
wdenk998eaae2004-04-18 19:43:36 +0000848 u8 jDirFoundType = 0;
849 u32 jDirFoundIno = 0;
850 u32 jDirFoundPino = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000851 char tmp[256];
852 u32 version = 0;
853 u32 pino;
854 unsigned char *src;
855
856 /* we need to search all and return the inode with the highest version */
wdenk06d01db2003-03-14 20:47:52 +0000857 for(b = pL->dir.listHead; b; b = b->next) {
wdenk998eaae2004-04-18 19:43:36 +0000858 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000859 if (ino == jDir->ino) {
wdenk998eaae2004-04-18 19:43:36 +0000860 if (jDir->version < version) {
861 put_fl_mem(jDir);
wdenk7205e402003-09-10 22:30:53 +0000862 continue;
wdenk998eaae2004-04-18 19:43:36 +0000863 }
wdenkfe8c2802002-11-03 00:38:21 +0000864
wdenk998eaae2004-04-18 19:43:36 +0000865 if (jDir->version == version && jDirFoundType) {
wdenk7205e402003-09-10 22:30:53 +0000866 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000867 putstr(" ** ERROR ** ");
868 putnstr(jDir->name, jDir->nsize);
869 putLabeledWord(" has dup version (resolve) = ",
870 version);
871 }
872
wdenk998eaae2004-04-18 19:43:36 +0000873 jDirFoundType = jDir->type;
874 jDirFoundIno = jDir->ino;
875 jDirFoundPino = jDir->pino;
wdenkfe8c2802002-11-03 00:38:21 +0000876 version = jDir->version;
877 }
wdenk998eaae2004-04-18 19:43:36 +0000878 put_fl_mem(jDir);
wdenkfe8c2802002-11-03 00:38:21 +0000879 }
880 /* now we found the right entry again. (shoulda returned inode*) */
wdenk998eaae2004-04-18 19:43:36 +0000881 if (jDirFoundType != DT_LNK)
882 return jDirFoundIno;
wdenk06d01db2003-03-14 20:47:52 +0000883
884 /* it's a soft link so we follow it again. */
885 b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000886 while (b2) {
wdenk998eaae2004-04-18 19:43:36 +0000887 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset);
888 if (jNode->ino == jDirFoundIno) {
wdenkfe8c2802002-11-03 00:38:21 +0000889 src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode));
890
891#if 0
892 putLabeledWord("\t\t dsize = ", jNode->dsize);
893 putstr("\t\t target = ");
894 putnstr(src, jNode->dsize);
895 putstr("\r\n");
896#endif
897 strncpy(tmp, src, jNode->dsize);
898 tmp[jNode->dsize] = '\0';
wdenk998eaae2004-04-18 19:43:36 +0000899 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000900 break;
901 }
902 b2 = b2->next;
wdenk998eaae2004-04-18 19:43:36 +0000903 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000904 }
905 /* ok so the name of the new file to find is in tmp */
906 /* if it starts with a slash it is root based else shared dirs */
907 if (tmp[0] == '/')
908 pino = 1;
909 else
wdenk998eaae2004-04-18 19:43:36 +0000910 pino = jDirFoundPino;
wdenkfe8c2802002-11-03 00:38:21 +0000911
912 return jffs2_1pass_search_inode(pL, tmp, pino);
913}
914
915static u32
916jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
917{
918 int i;
919 char tmp[256];
920 char working_tmp[256];
921 char *c;
922
923 /* discard any leading slash */
924 i = 0;
925 while (fname[i] == '/')
926 i++;
927 strcpy(tmp, &fname[i]);
928 working_tmp[0] = '\0';
929 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
930 {
931 strncpy(working_tmp, tmp, c - tmp);
932 working_tmp[c - tmp] = '\0';
933 for (i = 0; i < strlen(c) - 1; i++)
934 tmp[i] = c[i + 1];
935 tmp[i] = '\0';
wdenk0b8fa032004-04-25 14:37:29 +0000936
937 WATCHDOG_RESET();
938
wdenkfe8c2802002-11-03 00:38:21 +0000939 /* only a failure if we arent looking at top level */
wdenk06d01db2003-03-14 20:47:52 +0000940 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
941 (working_tmp[0])) {
wdenkfe8c2802002-11-03 00:38:21 +0000942 putstr("find_inode failed for name=");
943 putstr(working_tmp);
944 putstr("\r\n");
945 return 0;
946 }
947 }
948
949 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
950 putstr("find_inode failed for name=");
951 putstr(tmp);
952 putstr("\r\n");
953 return 0;
954 }
955 /* this is for the bare filename, directories have already been mapped */
956 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
957 putstr("find_inode failed for name=");
958 putstr(tmp);
959 putstr("\r\n");
960 return 0;
961 }
962 return pino;
963
964}
965
966unsigned char
967jffs2_1pass_rescan_needed(struct part_info *part)
968{
969 struct b_node *b;
wdenk998eaae2004-04-18 19:43:36 +0000970 struct jffs2_unknown_node onode;
wdenkfe8c2802002-11-03 00:38:21 +0000971 struct jffs2_unknown_node *node;
wdenk06d01db2003-03-14 20:47:52 +0000972 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +0000973
974 if (part->jffs2_priv == 0){
975 DEBUGF ("rescan: First time in use\n");
976 return 1;
977 }
978 /* if we have no list, we need to rescan */
wdenk06d01db2003-03-14 20:47:52 +0000979 if (pL->frag.listCount == 0) {
wdenkfe8c2802002-11-03 00:38:21 +0000980 DEBUGF ("rescan: fraglist zero\n");
981 return 1;
982 }
983
wdenk06d01db2003-03-14 20:47:52 +0000984 /* or if we are scanning a new partition */
wdenkfe8c2802002-11-03 00:38:21 +0000985 if (pL->partOffset != part->offset) {
986 DEBUGF ("rescan: different partition\n");
987 return 1;
988 }
wdenk998eaae2004-04-18 19:43:36 +0000989
990#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
991 if (nanddev != (int)part->usr_priv - 1) {
992 DEBUGF ("rescan: nand device changed\n");
993 return -1;
994 }
995#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
996
wdenk06d01db2003-03-14 20:47:52 +0000997 /* but suppose someone reflashed a partition at the same offset... */
998 b = pL->dir.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000999 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001000 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1001 sizeof(onode), &onode);
wdenkfe8c2802002-11-03 00:38:21 +00001002 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
wdenk06d01db2003-03-14 20:47:52 +00001003 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1004 (unsigned long) b->offset);
wdenkfe8c2802002-11-03 00:38:21 +00001005 return 1;
1006 }
1007 b = b->next;
1008 }
1009 return 0;
1010}
1011
wdenk06d01db2003-03-14 20:47:52 +00001012#ifdef DEBUG_FRAGMENTS
1013static void
1014dump_fragments(struct b_lists *pL)
1015{
1016 struct b_node *b;
wdenk998eaae2004-04-18 19:43:36 +00001017 struct jffs2_raw_inode ojNode;
wdenk06d01db2003-03-14 20:47:52 +00001018 struct jffs2_raw_inode *jNode;
1019
1020 putstr("\r\n\r\n******The fragment Entries******\r\n");
1021 b = pL->frag.listHead;
1022 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001023 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1024 sizeof(ojNode), &ojNode);
wdenk06d01db2003-03-14 20:47:52 +00001025 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1026 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1027 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1028 putLabeledWord("\tbuild_list: version = ", jNode->version);
1029 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1030 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1031 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1032 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1033 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1034 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1035 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1036 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
wdenk8bde7f72003-06-27 21:31:46 +00001037 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk06d01db2003-03-14 20:47:52 +00001038 b = b->next;
1039 }
1040}
1041#endif
1042
1043#ifdef DEBUG_DIRENTS
1044static void
1045dump_dirents(struct b_lists *pL)
1046{
1047 struct b_node *b;
1048 struct jffs2_raw_dirent *jDir;
1049
1050 putstr("\r\n\r\n******The directory Entries******\r\n");
1051 b = pL->dir.listHead;
1052 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001053 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenk06d01db2003-03-14 20:47:52 +00001054 putstr("\r\n");
1055 putnstr(jDir->name, jDir->nsize);
1056 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1057 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1058 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1059 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1060 putLabeledWord("\tbuild_list: version = ", jDir->version);
1061 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1062 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1063 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1064 putLabeledWord("\tbuild_list: type = ", jDir->type);
1065 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1066 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
wdenk8bde7f72003-06-27 21:31:46 +00001067 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk06d01db2003-03-14 20:47:52 +00001068 b = b->next;
wdenk998eaae2004-04-18 19:43:36 +00001069 put_fl_mem(jDir);
wdenk06d01db2003-03-14 20:47:52 +00001070 }
1071}
1072#endif
1073
wdenkfe8c2802002-11-03 00:38:21 +00001074static u32
1075jffs2_1pass_build_lists(struct part_info * part)
1076{
1077 struct b_lists *pL;
1078 struct jffs2_unknown_node *node;
wdenk0b8fa032004-04-25 14:37:29 +00001079 struct jffs2_unknown_node crcnode;
wdenk06d01db2003-03-14 20:47:52 +00001080 u32 offset, oldoffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001081 u32 max = part->size - sizeof(struct jffs2_raw_inode);
1082 u32 counter = 0;
1083 u32 counter4 = 0;
1084 u32 counterF = 0;
1085 u32 counterN = 0;
wdenk0b8fa032004-04-25 14:37:29 +00001086 u32 counterCRC = 0;
1087 u32 counterUNK = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001088
wdenk998eaae2004-04-18 19:43:36 +00001089#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
1090 nanddev = (int)part->usr_priv - 1;
1091#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
1092
wdenkfe8c2802002-11-03 00:38:21 +00001093 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
1094 /* jffs2 list building enterprise nope. in newer versions the overhead is */
1095 /* only about 5 %. not enough to inconvenience people for. */
1096 /* lcd_off(); */
1097
1098 /* if we are building a list we need to refresh the cache. */
wdenkfe8c2802002-11-03 00:38:21 +00001099 jffs_init_1pass_list(part);
wdenk06d01db2003-03-14 20:47:52 +00001100 pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +00001101 pL->partOffset = part->offset;
1102 offset = 0;
wdenk4b9206e2004-03-23 22:14:11 +00001103 puts ("Scanning JFFS2 FS: ");
wdenkfe8c2802002-11-03 00:38:21 +00001104
1105 /* start at the beginning of the partition */
1106 while (offset < max) {
wdenk06d01db2003-03-14 20:47:52 +00001107 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
wdenk0b8fa032004-04-25 14:37:29 +00001108 WATCHDOG_RESET();
1109#if 0 /* Use spinning wheel */
wdenk06d01db2003-03-14 20:47:52 +00001110 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
wdenk0b8fa032004-04-25 14:37:29 +00001111#endif
wdenk06d01db2003-03-14 20:47:52 +00001112 oldoffset = offset;
1113 }
wdenk998eaae2004-04-18 19:43:36 +00001114 node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset);
wdenk0b8fa032004-04-25 14:37:29 +00001115 if (node->magic == JFFS2_MAGIC_BITMASK) {
wdenkfe8c2802002-11-03 00:38:21 +00001116 /* if its a fragment add it */
wdenk0b8fa032004-04-25 14:37:29 +00001117 /* check crc by readding a JFFS2_NODE_ACCURATE */
1118 crcnode.magic = node->magic;
1119 crcnode.nodetype = node->nodetype | JFFS2_NODE_ACCURATE;
1120 crcnode.totlen = node->totlen;
1121 crcnode.hdr_crc = node->hdr_crc;
1122
1123 if (!hdr_crc(&crcnode)) {
1124 offset += 4;
1125 counterCRC++;
1126 continue;
1127 } else if (node->nodetype == JFFS2_NODETYPE_INODE &&
wdenk06d01db2003-03-14 20:47:52 +00001128 inode_crc((struct jffs2_raw_inode *) node)) {
1129 if (insert_node(&pL->frag, (u32) part->offset +
wdenk998eaae2004-04-18 19:43:36 +00001130 offset) == NULL) {
1131 put_fl_mem(node);
wdenkfe8c2802002-11-03 00:38:21 +00001132 return 0;
wdenk998eaae2004-04-18 19:43:36 +00001133 }
wdenkfe8c2802002-11-03 00:38:21 +00001134 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
1135 dirent_crc((struct jffs2_raw_dirent *) node) &&
1136 dirent_name_crc((struct jffs2_raw_dirent *) node)) {
wdenk0b8fa032004-04-25 14:37:29 +00001137 if (! (counterN%128))
1138#if 0 /* Use spinning wheel */
wdenk4b9206e2004-03-23 22:14:11 +00001139 puts ("\b\b. ");
wdenk0b8fa032004-04-25 14:37:29 +00001140#else
1141 puts (".");
1142#endif
wdenk06d01db2003-03-14 20:47:52 +00001143 if (insert_node(&pL->dir, (u32) part->offset +
wdenk998eaae2004-04-18 19:43:36 +00001144 offset) == NULL) {
1145 put_fl_mem(node);
wdenkfe8c2802002-11-03 00:38:21 +00001146 return 0;
wdenk998eaae2004-04-18 19:43:36 +00001147 }
wdenkfe8c2802002-11-03 00:38:21 +00001148 counterN++;
1149 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
1150 if (node->totlen != sizeof(struct jffs2_unknown_node))
wdenk06d01db2003-03-14 20:47:52 +00001151 printf("OOPS Cleanmarker has bad size "
1152 "%d != %d\n", node->totlen,
1153 sizeof(struct jffs2_unknown_node));
wdenk7205e402003-09-10 22:30:53 +00001154 } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
1155 if (node->totlen < sizeof(struct jffs2_unknown_node))
1156 printf("OOPS Padding has bad size "
1157 "%d < %d\n", node->totlen,
1158 sizeof(struct jffs2_unknown_node));
wdenkfe8c2802002-11-03 00:38:21 +00001159 } else {
wdenk0b8fa032004-04-25 14:37:29 +00001160 counterUNK++;
wdenkfe8c2802002-11-03 00:38:21 +00001161 }
1162 offset += ((node->totlen + 3) & ~3);
1163 counterF++;
wdenk06d01db2003-03-14 20:47:52 +00001164 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
1165 node->nodetype == JFFS2_EMPTY_BITMASK) {
wdenkfe8c2802002-11-03 00:38:21 +00001166 offset = jffs2_scan_empty(offset, part);
wdenk0b8fa032004-04-25 14:37:29 +00001167 } else { /* if we know nothing, we just step and look. */
wdenkfe8c2802002-11-03 00:38:21 +00001168 offset += 4;
1169 counter4++;
1170 }
1171/* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
wdenk998eaae2004-04-18 19:43:36 +00001172 put_fl_mem(node);
wdenkfe8c2802002-11-03 00:38:21 +00001173 }
1174
1175 putstr("\b\b done.\r\n"); /* close off the dots */
1176 /* turn the lcd back on. */
1177 /* splash(); */
1178
1179#if 0
wdenk06d01db2003-03-14 20:47:52 +00001180 putLabeledWord("dir entries = ", pL->dir.listCount);
1181 putLabeledWord("frag entries = ", pL->frag.listCount);
wdenkfe8c2802002-11-03 00:38:21 +00001182 putLabeledWord("+4 increments = ", counter4);
1183 putLabeledWord("+file_offset increments = ", counterF);
wdenk0b8fa032004-04-25 14:37:29 +00001184 putLabeledWord("Unknown node types = ", counterUNK);
1185 putLabeledWord("Bad hdr_crc = ", counterCRC);
wdenkfe8c2802002-11-03 00:38:21 +00001186
1187#endif
1188
wdenk06d01db2003-03-14 20:47:52 +00001189#ifdef DEBUG_DIRENTS
1190 dump_dirents(pL);
1191#endif
wdenkfe8c2802002-11-03 00:38:21 +00001192
wdenk06d01db2003-03-14 20:47:52 +00001193#ifdef DEBUG_FRAGMENTS
1194 dump_fragments(pL);
1195#endif
wdenkfe8c2802002-11-03 00:38:21 +00001196
wdenkfe8c2802002-11-03 00:38:21 +00001197 /* give visual feedback that we are done scanning the flash */
1198 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
1199 return 1;
1200}
1201
1202
wdenkfe8c2802002-11-03 00:38:21 +00001203static u32
1204jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1205{
1206 struct b_node *b;
wdenk998eaae2004-04-18 19:43:36 +00001207 struct jffs2_raw_inode ojNode;
wdenkfe8c2802002-11-03 00:38:21 +00001208 struct jffs2_raw_inode *jNode;
1209 int i;
1210
wdenkfe8c2802002-11-03 00:38:21 +00001211 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1212 piL->compr_info[i].num_frags = 0;
1213 piL->compr_info[i].compr_sum = 0;
1214 piL->compr_info[i].decompr_sum = 0;
1215 }
1216
wdenk06d01db2003-03-14 20:47:52 +00001217 b = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001218 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001219 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1220 sizeof(ojNode), &ojNode);
wdenkfe8c2802002-11-03 00:38:21 +00001221 if (jNode->compr < JFFS2_NUM_COMPR) {
1222 piL->compr_info[jNode->compr].num_frags++;
1223 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1224 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1225 }
1226 b = b->next;
1227 }
1228 return 0;
1229}
1230
1231
wdenkfe8c2802002-11-03 00:38:21 +00001232static struct b_lists *
1233jffs2_get_list(struct part_info * part, const char *who)
1234{
1235 if (jffs2_1pass_rescan_needed(part)) {
1236 if (!jffs2_1pass_build_lists(part)) {
1237 printf("%s: Failed to scan JFFSv2 file structure\n", who);
1238 return NULL;
1239 }
1240 }
wdenk0b8fa032004-04-25 14:37:29 +00001241 WATCHDOG_RESET();
wdenkfe8c2802002-11-03 00:38:21 +00001242 return (struct b_lists *)part->jffs2_priv;
1243}
1244
1245
1246/* Print directory / file contents */
1247u32
1248jffs2_1pass_ls(struct part_info * part, const char *fname)
1249{
1250 struct b_lists *pl;
wdenkfe8c2802002-11-03 00:38:21 +00001251 u32 inode;
1252
wdenk06d01db2003-03-14 20:47:52 +00001253 if (! (pl = jffs2_get_list(part, "ls")))
wdenkfe8c2802002-11-03 00:38:21 +00001254 return 0;
1255
wdenk0b8fa032004-04-25 14:37:29 +00001256
wdenkfe8c2802002-11-03 00:38:21 +00001257 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1258 putstr("ls: Failed to scan jffs2 file structure\r\n");
1259 return 0;
1260 }
wdenkfe8c2802002-11-03 00:38:21 +00001261#if 0
1262 putLabeledWord("found file at inode = ", inode);
1263 putLabeledWord("read_inode returns = ", ret);
1264#endif
wdenk0b8fa032004-04-25 14:37:29 +00001265 return inode;
wdenkfe8c2802002-11-03 00:38:21 +00001266}
1267
1268
wdenkfe8c2802002-11-03 00:38:21 +00001269/* Load a file from flash into memory. fname can be a full path */
1270u32
1271jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1272{
1273
1274 struct b_lists *pl;
1275 long ret = 0;
1276 u32 inode;
1277
1278 if (! (pl = jffs2_get_list(part, "load")))
1279 return 0;
1280
1281 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1282 putstr("load: Failed to find inode\r\n");
1283 return 0;
1284 }
1285
1286 /* Resolve symlinks */
1287 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1288 putstr("load: Failed to resolve inode structure\r\n");
1289 return 0;
1290 }
1291
1292 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1293 putstr("load: Failed to read inode\r\n");
1294 return 0;
1295 }
1296
1297 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1298 (unsigned long) dest, ret);
1299 return ret;
1300}
1301
1302/* Return information about the fs on this partition */
1303u32
1304jffs2_1pass_info(struct part_info * part)
1305{
1306 struct b_jffs2_info info;
1307 struct b_lists *pl;
1308 int i;
1309
1310 if (! (pl = jffs2_get_list(part, "info")))
1311 return 0;
1312
1313 jffs2_1pass_fill_info(pl, &info);
wdenk06d01db2003-03-14 20:47:52 +00001314 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
wdenk4b9206e2004-03-23 22:14:11 +00001315 printf ("Compression: %s\n"
1316 "\tfrag count: %d\n"
1317 "\tcompressed sum: %d\n"
1318 "\tuncompressed sum: %d\n",
1319 compr_names[i],
1320 info.compr_info[i].num_frags,
1321 info.compr_info[i].compr_sum,
1322 info.compr_info[i].decompr_sum);
wdenkfe8c2802002-11-03 00:38:21 +00001323 }
1324 return 1;
1325}
1326
1327#endif /* CFG_CMD_JFFS2 */