blob: c3553cb4ae438e0533e5ccb4a6ac5b9748700a00 [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
wdenk6b58f332003-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
wdenkae65f502004-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!
wdenk6b58f332003-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>
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
wdenk6b58f332003-03-14 20:47:52 +0000127
128#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
wdenk7ad5e4c2004-04-25 15:41:35 +0000129#define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
wdenkfe8c2802002-11-03 00:38:21 +0000130
wdenk6b58f332003-03-14 20:47:52 +0000131/* 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 */
wdenkfe8c2802002-11-03 00:38:21 +0000135
wdenk6b58f332003-03-14 20:47:52 +0000136
wdenkfe8c2802002-11-03 00:38:21 +0000137#ifdef DEBUG
138# define DEBUGF(fmt,args...) printf(fmt ,##args)
139#else
140# define DEBUGF(fmt,args...)
141#endif
142
wdenk8886a662004-04-18 19:43:36 +0000143#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 */
156int 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
168static u8* nand_cache = NULL;
169static u32 nand_cache_off = (u32)-1;
170static int nanddev = 0; /* nand device of current partition */
171
172static 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 */
wdenk12490652004-04-18 21:13:41 +0000185 nand_cache = malloc(NAND_CACHE_SIZE);
186 if (!nand_cache) {
wdenk8886a662004-04-18 19:43:36 +0000187 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
211static 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) {
wdenkf248ebc2004-05-05 19:44:41 +0000220 if (!ext_buf)
221 free(buf);
wdenk8886a662004-04-18 19:43:36 +0000222 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
wdenk12490652004-04-18 21:13:41 +0000245static void put_fl_mem(void *buf)
wdenk8886a662004-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
wdenk12490652004-04-18 21:13:41 +0000262static inline void put_fl_mem(void *buf)
wdenk8886a662004-04-18 19:43:36 +0000263{
264}
265
266#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
267
wdenk6b58f332003-03-14 20:47:52 +0000268
269/* Compression names */
270static char *compr_names[] = {
271 "NONE",
272 "ZERO",
273 "RTIME",
274 "RUBINMIPS",
275 "COPY",
276 "DYNRUBIN",
wdenke84ec902005-05-05 00:04:14 +0000277 "ZLIB",
wdenkb85efc22005-05-05 09:51:44 +0000278#if defined(CONFIG_JFFS2_LZO_LZARI)
wdenke84ec902005-05-05 00:04:14 +0000279 "LZO",
wdenke84ec902005-05-05 00:04:14 +0000280 "LZARI",
281#endif
wdenk6b58f332003-03-14 20:47:52 +0000282};
283
284/* Spinning wheel */
285static char spinner[] = { '|', '/', '-', '\\' };
wdenkfe8c2802002-11-03 00:38:21 +0000286
wdenk6b58f332003-03-14 20:47:52 +0000287/* Memory management */
288struct mem_block {
289 u32 index;
290 struct mem_block *next;
291 struct b_node nodes[NODE_CHUNK];
292};
293
294
295static void
296free_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}
wdenkfe8c2802002-11-03 00:38:21 +0000304
305static struct b_node *
wdenk6b58f332003-03-14 20:47:52 +0000306add_node(struct b_list *list)
wdenkfe8c2802002-11-03 00:38:21 +0000307{
wdenk6b58f332003-03-14 20:47:52 +0000308 u32 index = 0;
309 struct mem_block *memBase;
wdenkfe8c2802002-11-03 00:38:21 +0000310 struct b_node *b;
311
wdenk6b58f332003-03-14 20:47:52 +0000312 memBase = list->listMemBase;
313 if (memBase != NULL)
314 index = memBase->index;
wdenkfe8c2802002-11-03 00:38:21 +0000315#if 0
316 putLabeledWord("add_node: index = ", index);
wdenk6b58f332003-03-14 20:47:52 +0000317 putLabeledWord("add_node: memBase = ", list->listMemBase);
wdenkfe8c2802002-11-03 00:38:21 +0000318#endif
319
wdenk6b58f332003-03-14 20:47:52 +0000320 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) {
wdenkfe8c2802002-11-03 00:38:21 +0000324 putstr("add_node: malloc failed\n");
325 return NULL;
326 }
wdenk6b58f332003-03-14 20:47:52 +0000327 memBase->next = list->listMemBase;
328 index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000329#if 0
330 putLabeledWord("add_node: alloced a new membase at ", *memBase);
331#endif
332
333 }
334 /* now we have room to add it. */
wdenk6b58f332003-03-14 20:47:52 +0000335 b = &memBase->nodes[index];
336 index ++;
wdenkfe8c2802002-11-03 00:38:21 +0000337
wdenk6b58f332003-03-14 20:47:52 +0000338 memBase->index = index;
339 list->listMemBase = memBase;
340 list->listCount++;
341 return b;
342}
wdenkfe8c2802002-11-03 00:38:21 +0000343
wdenk6b58f332003-03-14 20:47:52 +0000344static struct b_node *
345insert_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;
wdenkfe8c2802002-11-03 00:38:21 +0000350#endif
351
wdenk6b58f332003-03-14 20:47:52 +0000352 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
wdenkfe8c2802002-11-03 00:38:21 +0000381#endif
wdenk6b58f332003-03-14 20:47:52 +0000382 {
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 }
wdenkfe8c2802002-11-03 00:38:21 +0000391
wdenk6b58f332003-03-14 20:47:52 +0000392 return new;
wdenkfe8c2802002-11-03 00:38:21 +0000393}
394
wdenk6b58f332003-03-14 20:47:52 +0000395#ifdef CFG_JFFS2_SORT_FRAGMENTS
wdenk2c9b05d2003-09-10 22:30:53 +0000396/* Sort data entries with the latest version last, so that if there
397 * is overlapping data the latest version will be used.
398 */
wdenk6b58f332003-03-14 20:47:52 +0000399static int compare_inodes(struct b_node *new, struct b_node *old)
400{
wdenk8886a662004-04-18 19:43:36 +0000401 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);
wdenk6b58f332003-03-14 20:47:52 +0000407
wdenk2c9b05d2003-09-10 22:30:53 +0000408 return jNew->version > jOld->version;
wdenk6b58f332003-03-14 20:47:52 +0000409}
410
wdenk2c9b05d2003-09-10 22:30:53 +0000411/* 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 */
wdenk6b58f332003-03-14 20:47:52 +0000416static int compare_dirents(struct b_node *new, struct b_node *old)
417{
wdenk8886a662004-04-18 19:43:36 +0000418 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);
wdenk12490652004-04-18 21:13:41 +0000422 struct jffs2_raw_dirent *jOld =
wdenk8886a662004-04-18 19:43:36 +0000423 (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
wdenk2c9b05d2003-09-10 22:30:53 +0000424 int cmp;
wdenk6b58f332003-03-14 20:47:52 +0000425
wdenk2c9b05d2003-09-10 22:30:53 +0000426 /* 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 }
wdenk9c53f402003-10-15 23:53:47 +0000452
wdenk2c9b05d2003-09-10 22:30:53 +0000453 return 0;
wdenk6b58f332003-03-14 20:47:52 +0000454}
455#endif
wdenkfe8c2802002-11-03 00:38:21 +0000456
457static u32
458jffs2_scan_empty(u32 start_offset, struct part_info *part)
459{
wdenk6b58f332003-03-14 20:47:52 +0000460 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
461 char *offset = part->offset + start_offset;
wdenk8886a662004-04-18 19:43:36 +0000462 u32 off;
wdenkfe8c2802002-11-03 00:38:21 +0000463
wdenk8886a662004-04-18 19:43:36 +0000464 while (offset < max &&
465 *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) {
wdenk6b58f332003-03-14 20:47:52 +0000466 offset += sizeof(u32);
467 /* return if spinning is due */
468 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
wdenkfe8c2802002-11-03 00:38:21 +0000469 }
wdenk7ad5e4c2004-04-25 15:41:35 +0000470
wdenk6b58f332003-03-14 20:47:52 +0000471 return offset - part->offset;
wdenkfe8c2802002-11-03 00:38:21 +0000472}
473
474static u32
475jffs_init_1pass_list(struct part_info *part)
476{
wdenk6b58f332003-03-14 20:47:52 +0000477 struct b_lists *pL;
478
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;
wdenkfe8c2802002-11-03 00:38:21 +0000487
wdenk6b58f332003-03-14 20:47:52 +0000488 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
wdenkfe8c2802002-11-03 00:38:21 +0000493 }
494 return 0;
495}
496
497/* find the inode from the slashless name given a parent */
498static long
499jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
500{
501 struct b_node *b;
502 struct jffs2_raw_inode *jNode;
wdenk6b58f332003-03-14 20:47:52 +0000503 u32 totalSize = 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000504 u32 latestVersion = 0;
wdenk6b58f332003-03-14 20:47:52 +0000505 char *lDest;
wdenkfe8c2802002-11-03 00:38:21 +0000506 char *src;
507 long ret;
508 int i;
509 u32 counter = 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000510#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) {
wdenk12490652004-04-18 21:13:41 +0000519 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
wdenk8886a662004-04-18 19:43:36 +0000520 sizeof(struct jffs2_raw_inode), NULL);
wdenk2c9b05d2003-09-10 22:30:53 +0000521 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 }
wdenk8886a662004-04-18 19:43:36 +0000528 put_fl_mem(jNode);
wdenk2c9b05d2003-09-10 22:30:53 +0000529 }
530#endif
wdenkfe8c2802002-11-03 00:38:21 +0000531
wdenk6b58f332003-03-14 20:47:52 +0000532 for (b = pL->frag.listHead; b != NULL; b = b->next) {
wdenk8886a662004-04-18 19:43:36 +0000533 jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000534 if ((inode == jNode->ino)) {
wdenk6b58f332003-03-14 20:47:52 +0000535#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000536 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);
wdenkfe8c2802002-11-03 00:38:21 +0000546#endif
wdenk2c9b05d2003-09-10 22:30:53 +0000547
548#ifndef CFG_JFFS2_SORT_FRAGMENTS
wdenk6b58f332003-03-14 20:47:52 +0000549 /* get actual file length from the newest node */
550 if (jNode->version >= latestVersion) {
wdenkfe8c2802002-11-03 00:38:21 +0000551 totalSize = jNode->isize;
wdenk6b58f332003-03-14 20:47:52 +0000552 latestVersion = jNode->version;
wdenkfe8c2802002-11-03 00:38:21 +0000553 }
wdenk2c9b05d2003-09-10 22:30:53 +0000554#endif
wdenkfe8c2802002-11-03 00:38:21 +0000555
556 if(dest) {
557 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
wdenk6b58f332003-03-14 20:47:52 +0000558 /* ignore data behind latest known EOF */
wdenk8886a662004-04-18 19:43:36 +0000559 if (jNode->offset > totalSize) {
560 put_fl_mem(jNode);
wdenk6b58f332003-03-14 20:47:52 +0000561 continue;
wdenk8886a662004-04-18 19:43:36 +0000562 }
wdenk6b58f332003-03-14 20:47:52 +0000563
wdenkfe8c2802002-11-03 00:38:21 +0000564 lDest = (char *) (dest + jNode->offset);
565#if 0
wdenk6b58f332003-03-14 20:47:52 +0000566 putLabeledWord("read_inode: src = ", src);
wdenkfe8c2802002-11-03 00:38:21 +0000567 putLabeledWord("read_inode: dest = ", lDest);
wdenkfe8c2802002-11-03 00:38:21 +0000568#endif
569 switch (jNode->compr) {
570 case JFFS2_COMPR_NONE:
wdenkfe8c2802002-11-03 00:38:21 +0000571 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;
wdenkb85efc22005-05-05 09:51:44 +0000590#if defined(CONFIG_JFFS2_LZO_LZARI)
wdenke84ec902005-05-05 00:04:14 +0000591 case JFFS2_COMPR_LZO:
592 ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
593 break;
wdenkb85efc22005-05-05 09:51:44 +0000594 case JFFS2_COMPR_LZARI:
595 ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
596 break;
wdenke84ec902005-05-05 00:04:14 +0000597#endif
wdenkfe8c2802002-11-03 00:38:21 +0000598 default:
599 /* unknown */
600 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
wdenk8886a662004-04-18 19:43:36 +0000601 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000602 return -1;
603 break;
604 }
605 }
606
wdenkfe8c2802002-11-03 00:38:21 +0000607#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000608 putLabeledWord("read_inode: totalSize = ", totalSize);
609 putLabeledWord("read_inode: compr ret = ", ret);
610#endif
611 }
wdenkfe8c2802002-11-03 00:38:21 +0000612 counter++;
wdenk8886a662004-04-18 19:43:36 +0000613 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000614 }
wdenkfe8c2802002-11-03 00:38:21 +0000615
616#if 0
wdenk6b58f332003-03-14 20:47:52 +0000617 putLabeledWord("read_inode: returning = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000618#endif
wdenk6b58f332003-03-14 20:47:52 +0000619 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000620}
621
622/* find the inode from the slashless name given a parent */
623static u32
624jffs2_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 */
wdenk6b58f332003-03-14 20:47:52 +0000638 for(b = pL->dir.listHead; b; b = b->next, counter++) {
wdenk8886a662004-04-18 19:43:36 +0000639 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenk6b58f332003-03-14 20:47:52 +0000640 if ((pino == jDir->pino) && (len == jDir->nsize) &&
641 (jDir->ino) && /* 0 for unlink */
wdenkfe8c2802002-11-03 00:38:21 +0000642 (!strncmp(jDir->name, name, len))) { /* a match */
wdenk8886a662004-04-18 19:43:36 +0000643 if (jDir->version < version) {
644 put_fl_mem(jDir);
wdenk2c9b05d2003-09-10 22:30:53 +0000645 continue;
wdenk8886a662004-04-18 19:43:36 +0000646 }
wdenkfe8c2802002-11-03 00:38:21 +0000647
wdenk2c9b05d2003-09-10 22:30:53 +0000648 if (jDir->version == version && inode != 0) {
649 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000650 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
wdenk8886a662004-04-18 19:43:36 +0000666 put_fl_mem(jDir);
wdenkfe8c2802002-11-03 00:38:21 +0000667 }
668 return inode;
669}
670
wdenkad276f22004-01-04 16:28:35 +0000671char *mkmodestr(unsigned long mode, char *str)
wdenkfe8c2802002-11-03 00:38:21 +0000672{
wdenk6b58f332003-03-14 20:47:52 +0000673 static const char *l = "xwr";
674 int mask = 1, i;
675 char c;
wdenkfe8c2802002-11-03 00:38:21 +0000676
wdenk6b58f332003-03-14 20:47:52 +0000677 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 }
wdenkfe8c2802002-11-03 00:38:21 +0000687
wdenk6b58f332003-03-14 20:47:52 +0000688 for(i = 0; i < 9; i++) {
689 c = l[i%3];
690 str[9-i] = (mode & mask)?c:'-';
691 mask = mask<<1;
692 }
wdenkfe8c2802002-11-03 00:38:21 +0000693
wdenk6b58f332003-03-14 20:47:52 +0000694 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;
wdenkfe8c2802002-11-03 00:38:21 +0000699}
700
701static inline void dump_stat(struct stat *st, const char *name)
702{
wdenk6b58f332003-03-14 20:47:52 +0000703 char str[20];
704 char s[64], *p;
wdenkfe8c2802002-11-03 00:38:21 +0000705
wdenk6b58f332003-03-14 20:47:52 +0000706 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
707 st->st_mtime = 1;
wdenkfe8c2802002-11-03 00:38:21 +0000708
wdenk6b58f332003-03-14 20:47:52 +0000709 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
wdenkfe8c2802002-11-03 00:38:21 +0000710
wdenk6b58f332003-03-14 20:47:52 +0000711 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
712 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000713
714/*
wdenk6b58f332003-03-14 20:47:52 +0000715 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
716 st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000717*/
718
wdenk6b58f332003-03-14 20:47:52 +0000719 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000720}
721
722static 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);
wdenk6b58f332003-03-14 20:47:52 +0000730 fname[d->nsize] = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000731
732 memset(&st,0,sizeof(st));
733
wdenk6b58f332003-03-14 20:47:52 +0000734 st.st_mtime = i->mtime;
735 st.st_mode = i->mode;
736 st.st_ino = i->ino;
wdenkfe8c2802002-11-03 00:38:21 +0000737
738 /* neither dsize nor isize help us.. do it the long way */
wdenk6b58f332003-03-14 20:47:52 +0000739 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
wdenkfe8c2802002-11-03 00:38:21 +0000740
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 */
755static u32
756jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
757{
758 struct b_node *b;
759 struct jffs2_raw_dirent *jDir;
760
wdenk6b58f332003-03-14 20:47:52 +0000761 for (b = pL->dir.listHead; b; b = b->next) {
wdenk8886a662004-04-18 19:43:36 +0000762 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenk6b58f332003-03-14 20:47:52 +0000763 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
764 u32 i_version = 0;
wdenk8886a662004-04-18 19:43:36 +0000765 struct jffs2_raw_inode ojNode;
wdenk6b58f332003-03-14 20:47:52 +0000766 struct jffs2_raw_inode *jNode, *i = NULL;
767 struct b_node *b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000768
769 while (b2) {
wdenk8886a662004-04-18 19:43:36 +0000770 jNode = (struct jffs2_raw_inode *)
771 get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
wdenkf248ebc2004-05-05 19:44:41 +0000772 if (jNode->ino == jDir->ino && jNode->version >= i_version) {
773 if (i)
wdenke07ec1b2004-05-12 22:54:36 +0000774 put_fl_mem(i);
wdenke02d2ae2005-05-04 23:50:54 +0000775
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);
wdenkf248ebc2004-05-05 19:44:41 +0000780 }
wdenkfe8c2802002-11-03 00:38:21 +0000781 b2 = b2->next;
782 }
783
784 dump_inode(pL, jDir, i);
wdenk8886a662004-04-18 19:43:36 +0000785 put_fl_mem(i);
wdenkfe8c2802002-11-03 00:38:21 +0000786 }
wdenk8886a662004-04-18 19:43:36 +0000787 put_fl_mem(jDir);
wdenkfe8c2802002-11-03 00:38:21 +0000788 }
789 return pino;
790}
791
792static u32
793jffs2_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
848static u32
849jffs2_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;
wdenk8886a662004-04-18 19:43:36 +0000855 u8 jDirFoundType = 0;
856 u32 jDirFoundIno = 0;
857 u32 jDirFoundPino = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000858 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 */
wdenk6b58f332003-03-14 20:47:52 +0000864 for(b = pL->dir.listHead; b; b = b->next) {
wdenk8886a662004-04-18 19:43:36 +0000865 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000866 if (ino == jDir->ino) {
wdenk8886a662004-04-18 19:43:36 +0000867 if (jDir->version < version) {
868 put_fl_mem(jDir);
wdenk2c9b05d2003-09-10 22:30:53 +0000869 continue;
wdenk8886a662004-04-18 19:43:36 +0000870 }
wdenkfe8c2802002-11-03 00:38:21 +0000871
wdenk8886a662004-04-18 19:43:36 +0000872 if (jDir->version == version && jDirFoundType) {
wdenk2c9b05d2003-09-10 22:30:53 +0000873 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000874 putstr(" ** ERROR ** ");
875 putnstr(jDir->name, jDir->nsize);
876 putLabeledWord(" has dup version (resolve) = ",
877 version);
878 }
879
wdenk8886a662004-04-18 19:43:36 +0000880 jDirFoundType = jDir->type;
881 jDirFoundIno = jDir->ino;
882 jDirFoundPino = jDir->pino;
wdenkfe8c2802002-11-03 00:38:21 +0000883 version = jDir->version;
884 }
wdenk8886a662004-04-18 19:43:36 +0000885 put_fl_mem(jDir);
wdenkfe8c2802002-11-03 00:38:21 +0000886 }
887 /* now we found the right entry again. (shoulda returned inode*) */
wdenk8886a662004-04-18 19:43:36 +0000888 if (jDirFoundType != DT_LNK)
889 return jDirFoundIno;
wdenk6b58f332003-03-14 20:47:52 +0000890
891 /* it's a soft link so we follow it again. */
892 b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000893 while (b2) {
wdenk8886a662004-04-18 19:43:36 +0000894 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset);
895 if (jNode->ino == jDirFoundIno) {
wdenk6c59edc2004-05-03 20:45:30 +0000896 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
wdenkfe8c2802002-11-03 00:38:21 +0000897
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';
wdenk8886a662004-04-18 19:43:36 +0000906 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000907 break;
908 }
909 b2 = b2->next;
wdenk8886a662004-04-18 19:43:36 +0000910 put_fl_mem(jNode);
wdenkfe8c2802002-11-03 00:38:21 +0000911 }
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
wdenk8886a662004-04-18 19:43:36 +0000917 pino = jDirFoundPino;
wdenkfe8c2802002-11-03 00:38:21 +0000918
919 return jffs2_1pass_search_inode(pL, tmp, pino);
920}
921
922static u32
923jffs2_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 */
wdenk6b58f332003-03-14 20:47:52 +0000944 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
945 (working_tmp[0])) {
wdenkfe8c2802002-11-03 00:38:21 +0000946 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
970unsigned char
971jffs2_1pass_rescan_needed(struct part_info *part)
972{
973 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +0000974 struct jffs2_unknown_node onode;
wdenkfe8c2802002-11-03 00:38:21 +0000975 struct jffs2_unknown_node *node;
wdenk6b58f332003-03-14 20:47:52 +0000976 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +0000977
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 */
wdenk6b58f332003-03-14 20:47:52 +0000983 if (pL->frag.listCount == 0) {
wdenkfe8c2802002-11-03 00:38:21 +0000984 DEBUGF ("rescan: fraglist zero\n");
985 return 1;
986 }
987
wdenk6b58f332003-03-14 20:47:52 +0000988 /* or if we are scanning a new partition */
wdenkfe8c2802002-11-03 00:38:21 +0000989 if (pL->partOffset != part->offset) {
990 DEBUGF ("rescan: different partition\n");
991 return 1;
992 }
wdenk8886a662004-04-18 19:43:36 +0000993
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
wdenk6b58f332003-03-14 20:47:52 +00001001 /* but suppose someone reflashed a partition at the same offset... */
1002 b = pL->dir.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001003 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001004 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1005 sizeof(onode), &onode);
wdenkfe8c2802002-11-03 00:38:21 +00001006 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
wdenk6b58f332003-03-14 20:47:52 +00001007 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1008 (unsigned long) b->offset);
wdenkfe8c2802002-11-03 00:38:21 +00001009 return 1;
1010 }
1011 b = b->next;
1012 }
1013 return 0;
1014}
wdenk6b58f332003-03-14 20:47:52 +00001015
1016#ifdef DEBUG_FRAGMENTS
1017static void
1018dump_fragments(struct b_lists *pL)
1019{
1020 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001021 struct jffs2_raw_inode ojNode;
wdenk6b58f332003-03-14 20:47:52 +00001022 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) {
wdenk8886a662004-04-18 19:43:36 +00001027 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1028 sizeof(ojNode), &ojNode);
wdenk6b58f332003-03-14 20:47:52 +00001029 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);
wdenk57b2d802003-06-27 21:31:46 +00001041 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk6b58f332003-03-14 20:47:52 +00001042 b = b->next;
1043 }
1044}
1045#endif
1046
1047#ifdef DEBUG_DIRENTS
1048static void
1049dump_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) {
wdenk8886a662004-04-18 19:43:36 +00001057 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
wdenk6b58f332003-03-14 20:47:52 +00001058 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);
wdenk57b2d802003-06-27 21:31:46 +00001071 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk6b58f332003-03-14 20:47:52 +00001072 b = b->next;
wdenk8886a662004-04-18 19:43:36 +00001073 put_fl_mem(jDir);
wdenk6b58f332003-03-14 20:47:52 +00001074 }
1075}
1076#endif
wdenkfe8c2802002-11-03 00:38:21 +00001077
1078static u32
1079jffs2_1pass_build_lists(struct part_info * part)
1080{
1081 struct b_lists *pL;
1082 struct jffs2_unknown_node *node;
wdenk6b58f332003-03-14 20:47:52 +00001083 u32 offset, oldoffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001084 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
wdenk8886a662004-04-18 19:43:36 +00001090#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
wdenkfe8c2802002-11-03 00:38:21 +00001094 /* 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. */
wdenkfe8c2802002-11-03 00:38:21 +00001100 jffs_init_1pass_list(part);
wdenk6b58f332003-03-14 20:47:52 +00001101 pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +00001102 pL->partOffset = part->offset;
1103 offset = 0;
wdenk42c05472004-03-23 22:14:11 +00001104 puts ("Scanning JFFS2 FS: ");
wdenkfe8c2802002-11-03 00:38:21 +00001105
1106 /* start at the beginning of the partition */
1107 while (offset < max) {
wdenk6b58f332003-03-14 20:47:52 +00001108 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
1109 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
1110 oldoffset = offset;
1111 }
wdenk7ad5e4c2004-04-25 15:41:35 +00001112
wdenk8886a662004-04-18 19:43:36 +00001113 node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset);
wdenk7ad5e4c2004-04-25 15:41:35 +00001114 if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
wdenkfe8c2802002-11-03 00:38:21 +00001115 /* if its a fragment add it */
wdenk7ad5e4c2004-04-25 15:41:35 +00001116 if (node->nodetype == JFFS2_NODETYPE_INODE &&
wdenk6b58f332003-03-14 20:47:52 +00001117 inode_crc((struct jffs2_raw_inode *) node)) {
1118 if (insert_node(&pL->frag, (u32) part->offset +
wdenk8886a662004-04-18 19:43:36 +00001119 offset) == NULL) {
1120 put_fl_mem(node);
wdenkfe8c2802002-11-03 00:38:21 +00001121 return 0;
wdenk8886a662004-04-18 19:43:36 +00001122 }
wdenkfe8c2802002-11-03 00:38:21 +00001123 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
1124 dirent_crc((struct jffs2_raw_dirent *) node) &&
1125 dirent_name_crc((struct jffs2_raw_dirent *) node)) {
wdenk7ad5e4c2004-04-25 15:41:35 +00001126 if (! (counterN%100))
wdenk42c05472004-03-23 22:14:11 +00001127 puts ("\b\b. ");
wdenk6b58f332003-03-14 20:47:52 +00001128 if (insert_node(&pL->dir, (u32) part->offset +
wdenk8886a662004-04-18 19:43:36 +00001129 offset) == NULL) {
1130 put_fl_mem(node);
wdenkfe8c2802002-11-03 00:38:21 +00001131 return 0;
wdenk8886a662004-04-18 19:43:36 +00001132 }
wdenkfe8c2802002-11-03 00:38:21 +00001133 counterN++;
1134 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
1135 if (node->totlen != sizeof(struct jffs2_unknown_node))
wdenk6b58f332003-03-14 20:47:52 +00001136 printf("OOPS Cleanmarker has bad size "
1137 "%d != %d\n", node->totlen,
1138 sizeof(struct jffs2_unknown_node));
wdenk2c9b05d2003-09-10 22:30:53 +00001139 } 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));
wdenkfe8c2802002-11-03 00:38:21 +00001144 } else {
wdenk7ad5e4c2004-04-25 15:41:35 +00001145 printf("Unknown node type: %x len %d "
1146 "offset 0x%x\n", node->nodetype,
1147 node->totlen, offset);
wdenkfe8c2802002-11-03 00:38:21 +00001148 }
1149 offset += ((node->totlen + 3) & ~3);
1150 counterF++;
wdenk6b58f332003-03-14 20:47:52 +00001151 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
1152 node->nodetype == JFFS2_EMPTY_BITMASK) {
wdenkfe8c2802002-11-03 00:38:21 +00001153 offset = jffs2_scan_empty(offset, part);
wdenk7ad5e4c2004-04-25 15:41:35 +00001154 } else { /* if we know nothing, we just step and look. */
wdenkfe8c2802002-11-03 00:38:21 +00001155 offset += 4;
1156 counter4++;
1157 }
1158/* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
wdenk8886a662004-04-18 19:43:36 +00001159 put_fl_mem(node);
wdenkfe8c2802002-11-03 00:38:21 +00001160 }
1161
1162 putstr("\b\b done.\r\n"); /* close off the dots */
1163 /* turn the lcd back on. */
1164 /* splash(); */
1165
1166#if 0
wdenk6b58f332003-03-14 20:47:52 +00001167 putLabeledWord("dir entries = ", pL->dir.listCount);
1168 putLabeledWord("frag entries = ", pL->frag.listCount);
wdenkfe8c2802002-11-03 00:38:21 +00001169 putLabeledWord("+4 increments = ", counter4);
1170 putLabeledWord("+file_offset increments = ", counterF);
1171
1172#endif
1173
wdenk6b58f332003-03-14 20:47:52 +00001174#ifdef DEBUG_DIRENTS
1175 dump_dirents(pL);
1176#endif
wdenkfe8c2802002-11-03 00:38:21 +00001177
wdenk6b58f332003-03-14 20:47:52 +00001178#ifdef DEBUG_FRAGMENTS
1179 dump_fragments(pL);
1180#endif
wdenkfe8c2802002-11-03 00:38:21 +00001181
wdenkfe8c2802002-11-03 00:38:21 +00001182 /* 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
wdenkfe8c2802002-11-03 00:38:21 +00001188static u32
1189jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1190{
1191 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001192 struct jffs2_raw_inode ojNode;
wdenkfe8c2802002-11-03 00:38:21 +00001193 struct jffs2_raw_inode *jNode;
1194 int i;
1195
wdenkfe8c2802002-11-03 00:38:21 +00001196 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
wdenk6b58f332003-03-14 20:47:52 +00001202 b = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001203 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001204 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1205 sizeof(ojNode), &ojNode);
wdenkfe8c2802002-11-03 00:38:21 +00001206 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
wdenkfe8c2802002-11-03 00:38:21 +00001217static struct b_lists *
1218jffs2_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 */
1231u32
1232jffs2_1pass_ls(struct part_info * part, const char *fname)
1233{
1234 struct b_lists *pl;
wdenk7ad5e4c2004-04-25 15:41:35 +00001235 long ret = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001236 u32 inode;
1237
wdenk6b58f332003-03-14 20:47:52 +00001238 if (! (pl = jffs2_get_list(part, "ls")))
wdenkfe8c2802002-11-03 00:38:21 +00001239 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 }
wdenk7ad5e4c2004-04-25 15:41:35 +00001245
1246
wdenkfe8c2802002-11-03 00:38:21 +00001247#if 0
1248 putLabeledWord("found file at inode = ", inode);
1249 putLabeledWord("read_inode returns = ", ret);
1250#endif
wdenk7ad5e4c2004-04-25 15:41:35 +00001251
1252 return ret;
wdenkfe8c2802002-11-03 00:38:21 +00001253}
1254
1255
wdenkfe8c2802002-11-03 00:38:21 +00001256/* Load a file from flash into memory. fname can be a full path */
1257u32
1258jffs2_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 */
1290u32
1291jffs2_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);
wdenk6b58f332003-03-14 20:47:52 +00001301 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
wdenk42c05472004-03-23 22:14:11 +00001302 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);
wdenkfe8c2802002-11-03 00:38:21 +00001310 }
1311 return 1;
1312}
1313
1314#endif /* CFG_CMD_JFFS2 */