blob: 3fd4e52b33f95d427a97f1c6998962548c25d80e [file] [log] [blame]
wdenk06d01db2003-03-14 20:47:52 +00001/* vi: set sw=4 ts=4: */
wdenkfe8c2802002-11-03 00:38:21 +00002/*
3-------------------------------------------------------------------------
4 * Filename: jffs2.c
5 * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
6 * Copyright: Copyright (C) 2001, Russ Dill
7 * Author: Russ Dill <Russ.Dill@asu.edu>
8 * Description: Module to load kernel from jffs2
9 *-----------------------------------------------------------------------*/
10/*
11 * some portions of this code are taken from jffs2, and as such, the
12 * following copyright notice is included.
13 *
14 * JFFS2 -- Journalling Flash File System, Version 2.
15 *
16 * Copyright (C) 2001 Red Hat, Inc.
17 *
18 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
19 *
20 * The original JFFS, from which the design for JFFS2 was derived,
21 * was designed and implemented by Axis Communications AB.
22 *
23 * The contents of this file are subject to the Red Hat eCos Public
24 * License Version 1.1 (the "Licence"); you may not use this file
25 * except in compliance with the Licence. You may obtain a copy of
26 * the Licence at http://www.redhat.com/
27 *
28 * Software distributed under the Licence is distributed on an "AS IS"
29 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
30 * See the Licence for the specific language governing rights and
31 * limitations under the Licence.
32 *
33 * The Original Code is JFFS2 - Journalling Flash File System, version 2
34 *
35 * Alternatively, the contents of this file may be used under the
36 * terms of the GNU General Public License version 2 (the "GPL"), in
37 * which case the provisions of the GPL are applicable instead of the
38 * above. If you wish to allow the use of your version of this file
39 * only under the terms of the GPL and not to allow others to use your
40 * version of this file under the RHEPL, indicate your decision by
41 * deleting the provisions above and replace them with the notice and
42 * other provisions required by the GPL. If you do not delete the
43 * provisions above, a recipient may use your version of this file
44 * under either the RHEPL or the GPL.
45 *
46 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
47 *
48 */
49
50/* Ok, so anyone who knows the jffs2 code will probably want to get a papar
51 * bag to throw up into before reading this code. I looked through the jffs2
52 * code, the caching scheme is very elegant. I tried to keep the version
53 * for a bootloader as small and simple as possible. Instead of worring about
54 * unneccesary data copies, node scans, etc, I just optimized for the known
55 * common case, a kernel, which looks like:
56 * (1) most pages are 4096 bytes
57 * (2) version numbers are somewhat sorted in acsending order
58 * (3) multiple compressed blocks making up one page is uncommon
59 *
60 * So I create a linked list of decending version numbers (insertions at the
61 * head), and then for each page, walk down the list, until a matching page
62 * with 4096 bytes is found, and then decompress the watching pages in
63 * reverse order.
64 *
65 */
66
67/*
68 * Adapted by Nye Liu <nyet@zumanetworks.com> and
69 * Rex Feany <rfeany@zumanetworks.com>
70 * on Jan/2002 for U-Boot.
71 *
72 * Clipped out all the non-1pass functions, cleaned up warnings,
73 * wrappers, etc. No major changes to the code.
74 * Please, he really means it when he said have a paper bag
75 * handy. We needed it ;).
76 *
77 */
78
wdenk06d01db2003-03-14 20:47:52 +000079/*
80 * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
81 *
82 * - overhaul of the memory management. Removed much of the "paper-bagging"
83 * in that part of the code, fixed several bugs, now frees memory when
84 * partition is changed.
85 * It's still ugly :-(
86 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
87 * was incorrect. Removed a bit of the paper-bagging as well.
88 * - removed double crc calculation for fragment headers in jffs2_private.h
89 * for speedup.
90 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
91 * - spinning wheel now spins depending on how much memory has been scanned
92 * - lots of small changes all over the place to "improve" readability.
93 * - implemented fragment sorting to ensure that the newest data is copied
94 * if there are multiple copies of fragments for a certain file offset.
95 *
96 * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS.
97 * Sorting is done while adding fragments to the lists, which is more or less a
98 * bubble sort. This takes a lot of time, and is most probably not an issue if
99 * the boot filesystem is always mounted readonly.
100 *
101 * You should define it if the boot filesystem is mounted writable, and updates
102 * to the boot files are done by copying files to that filesystem.
103 *
104 *
105 * There's a big issue left: endianess is completely ignored in this code. Duh!
106 *
107 *
108 * You still should have paper bags at hand :-(. The code lacks more or less
109 * any comment, and is still arcane and difficult to read in places. As this
110 * is incompatible with any new code from the jffs2 maintainers anyway, it
111 * should probably be dumped and replaced by something like jffs2reader!
112 */
113
114
wdenkfe8c2802002-11-03 00:38:21 +0000115#include <common.h>
116#include <config.h>
117#include <malloc.h>
118#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 */
130#define SPIN_BLKSIZE 18 /* 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
wdenkfe8c2802002-11-03 00:38:21 +0000144
wdenk06d01db2003-03-14 20:47:52 +0000145/* Compression names */
146static char *compr_names[] = {
147 "NONE",
148 "ZERO",
149 "RTIME",
150 "RUBINMIPS",
151 "COPY",
152 "DYNRUBIN",
153 "ZLIB"
154};
155
156/* Spinning wheel */
157static char spinner[] = { '|', '/', '-', '\\' };
158
159/* Memory management */
160struct mem_block {
161 u32 index;
162 struct mem_block *next;
163 struct b_node nodes[NODE_CHUNK];
164};
165
166
167static void
168free_nodes(struct b_list *list)
169{
170 while (list->listMemBase != NULL) {
171 struct mem_block *next = list->listMemBase->next;
172 free( list->listMemBase );
173 list->listMemBase = next;
174 }
175}
wdenkfe8c2802002-11-03 00:38:21 +0000176
177static struct b_node *
wdenk06d01db2003-03-14 20:47:52 +0000178add_node(struct b_list *list)
wdenkfe8c2802002-11-03 00:38:21 +0000179{
wdenk06d01db2003-03-14 20:47:52 +0000180 u32 index = 0;
181 struct mem_block *memBase;
wdenkfe8c2802002-11-03 00:38:21 +0000182 struct b_node *b;
183
wdenk06d01db2003-03-14 20:47:52 +0000184 memBase = list->listMemBase;
185 if (memBase != NULL)
186 index = memBase->index;
wdenkfe8c2802002-11-03 00:38:21 +0000187#if 0
188 putLabeledWord("add_node: index = ", index);
wdenk06d01db2003-03-14 20:47:52 +0000189 putLabeledWord("add_node: memBase = ", list->listMemBase);
wdenkfe8c2802002-11-03 00:38:21 +0000190#endif
191
wdenk06d01db2003-03-14 20:47:52 +0000192 if (memBase == NULL || index >= NODE_CHUNK) {
193 /* we need more space before we continue */
194 memBase = mmalloc(sizeof(struct mem_block));
195 if (memBase == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +0000196 putstr("add_node: malloc failed\n");
197 return NULL;
198 }
wdenk06d01db2003-03-14 20:47:52 +0000199 memBase->next = list->listMemBase;
200 index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000201#if 0
202 putLabeledWord("add_node: alloced a new membase at ", *memBase);
203#endif
204
205 }
206 /* now we have room to add it. */
wdenk06d01db2003-03-14 20:47:52 +0000207 b = &memBase->nodes[index];
208 index ++;
wdenkfe8c2802002-11-03 00:38:21 +0000209
wdenk06d01db2003-03-14 20:47:52 +0000210 memBase->index = index;
211 list->listMemBase = memBase;
212 list->listCount++;
wdenkfe8c2802002-11-03 00:38:21 +0000213 return b;
wdenkfe8c2802002-11-03 00:38:21 +0000214}
215
wdenk06d01db2003-03-14 20:47:52 +0000216static struct b_node *
217insert_node(struct b_list *list, u32 offset)
218{
219 struct b_node *new;
220#ifdef CFG_JFFS2_SORT_FRAGMENTS
221 struct b_node *b, *prev;
222#endif
223
224 if (!(new = add_node(list))) {
225 putstr("add_node failed!\r\n");
226 return NULL;
227 }
228 new->offset = offset;
229
230#ifdef CFG_JFFS2_SORT_FRAGMENTS
231 if (list->listTail != NULL && list->listCompare(new, list->listTail))
232 prev = list->listTail;
233 else if (list->listLast != NULL && list->listCompare(new, list->listLast))
234 prev = list->listLast;
235 else
236 prev = NULL;
237
238 for (b = (prev ? prev->next : list->listHead);
239 b != NULL && list->listCompare(new, b);
240 prev = b, b = b->next) {
241 list->listLoops++;
242 }
243 if (b != NULL)
244 list->listLast = prev;
245
246 if (b != NULL) {
247 new->next = b;
248 if (prev != NULL)
249 prev->next = new;
250 else
251 list->listHead = new;
252 } else
253#endif
254 {
255 new->next = (struct b_node *) NULL;
256 if (list->listTail != NULL) {
257 list->listTail->next = new;
258 list->listTail = new;
259 } else {
260 list->listTail = list->listHead = new;
261 }
262 }
263
264 return new;
265}
266
267#ifdef CFG_JFFS2_SORT_FRAGMENTS
268static int compare_inodes(struct b_node *new, struct b_node *old)
269{
270 struct jffs2_raw_inode *jNew = (struct jffs2_raw_inode *)new->offset;
271 struct jffs2_raw_inode *jOld = (struct jffs2_raw_inode *)old->offset;
272
273 return jNew->version < jOld->version;
274}
275
276static int compare_dirents(struct b_node *new, struct b_node *old)
277{
278 struct jffs2_raw_dirent *jNew = (struct jffs2_raw_dirent *)new->offset;
279 struct jffs2_raw_dirent *jOld = (struct jffs2_raw_dirent *)old->offset;
280
281 return jNew->version > jOld->version;
282}
283#endif
wdenkfe8c2802002-11-03 00:38:21 +0000284
285static u32
286jffs2_scan_empty(u32 start_offset, struct part_info *part)
287{
wdenk06d01db2003-03-14 20:47:52 +0000288 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
289 char *offset = part->offset + start_offset;
wdenkfe8c2802002-11-03 00:38:21 +0000290
wdenk06d01db2003-03-14 20:47:52 +0000291 while (offset < max && *(u32 *)offset == 0xFFFFFFFF) {
292 offset += sizeof(u32);
293 /* return if spinning is due */
294 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
wdenkfe8c2802002-11-03 00:38:21 +0000295 }
wdenk06d01db2003-03-14 20:47:52 +0000296
297 return offset - part->offset;
wdenkfe8c2802002-11-03 00:38:21 +0000298}
299
300static u32
301jffs_init_1pass_list(struct part_info *part)
302{
wdenk06d01db2003-03-14 20:47:52 +0000303 struct b_lists *pL;
wdenkfe8c2802002-11-03 00:38:21 +0000304
wdenk06d01db2003-03-14 20:47:52 +0000305 if (part->jffs2_priv != NULL) {
306 pL = (struct b_lists *)part->jffs2_priv;
307 free_nodes(&pL->frag);
308 free_nodes(&pL->dir);
309 free(pL);
310 }
311 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
312 pL = (struct b_lists *)part->jffs2_priv;
313
314 memset(pL, 0, sizeof(*pL));
315#ifdef CFG_JFFS2_SORT_FRAGMENTS
316 pL->dir.listCompare = compare_dirents;
317 pL->frag.listCompare = compare_inodes;
318#endif
wdenkfe8c2802002-11-03 00:38:21 +0000319 }
320 return 0;
321}
322
323/* find the inode from the slashless name given a parent */
324static long
325jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
326{
327 struct b_node *b;
328 struct jffs2_raw_inode *jNode;
wdenk06d01db2003-03-14 20:47:52 +0000329 u32 totalSize = 0;
330 u16 latestVersion = 0;
331 char *lDest;
wdenkfe8c2802002-11-03 00:38:21 +0000332 char *src;
333 long ret;
334 int i;
335 u32 counter = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000336
wdenk06d01db2003-03-14 20:47:52 +0000337 for (b = pL->frag.listHead; b != NULL; b = b->next) {
wdenkfe8c2802002-11-03 00:38:21 +0000338 jNode = (struct jffs2_raw_inode *) (b->offset);
339 if ((inode == jNode->ino)) {
wdenk06d01db2003-03-14 20:47:52 +0000340#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000341 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
342 putLabeledWord("read_inode: inode = ", jNode->ino);
343 putLabeledWord("read_inode: version = ", jNode->version);
344 putLabeledWord("read_inode: isize = ", jNode->isize);
345 putLabeledWord("read_inode: offset = ", jNode->offset);
346 putLabeledWord("read_inode: csize = ", jNode->csize);
347 putLabeledWord("read_inode: dsize = ", jNode->dsize);
348 putLabeledWord("read_inode: compr = ", jNode->compr);
349 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
350 putLabeledWord("read_inode: flags = ", jNode->flags);
wdenkfe8c2802002-11-03 00:38:21 +0000351#endif
wdenk06d01db2003-03-14 20:47:52 +0000352 /* get actual file length from the newest node */
353 if (jNode->version >= latestVersion) {
wdenkfe8c2802002-11-03 00:38:21 +0000354 totalSize = jNode->isize;
wdenk06d01db2003-03-14 20:47:52 +0000355 latestVersion = jNode->version;
wdenkfe8c2802002-11-03 00:38:21 +0000356 }
wdenkfe8c2802002-11-03 00:38:21 +0000357
358 if(dest) {
359 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
wdenk06d01db2003-03-14 20:47:52 +0000360 /* ignore data behind latest known EOF */
361 if (jNode->offset > totalSize)
362 continue;
363
wdenkfe8c2802002-11-03 00:38:21 +0000364 lDest = (char *) (dest + jNode->offset);
365#if 0
wdenk06d01db2003-03-14 20:47:52 +0000366 putLabeledWord("read_inode: src = ", src);
wdenkfe8c2802002-11-03 00:38:21 +0000367 putLabeledWord("read_inode: dest = ", lDest);
wdenkfe8c2802002-11-03 00:38:21 +0000368#endif
369 switch (jNode->compr) {
370 case JFFS2_COMPR_NONE:
wdenkfe8c2802002-11-03 00:38:21 +0000371 ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
372 break;
373 case JFFS2_COMPR_ZERO:
374 ret = 0;
375 for (i = 0; i < jNode->dsize; i++)
376 *(lDest++) = 0;
377 break;
378 case JFFS2_COMPR_RTIME:
379 ret = 0;
380 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
381 break;
382 case JFFS2_COMPR_DYNRUBIN:
383 /* this is slow but it works */
384 ret = 0;
385 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
386 break;
387 case JFFS2_COMPR_ZLIB:
388 ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
389 break;
390 default:
391 /* unknown */
392 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
393 return -1;
394 break;
395 }
396 }
397
wdenkfe8c2802002-11-03 00:38:21 +0000398#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000399 putLabeledWord("read_inode: totalSize = ", totalSize);
400 putLabeledWord("read_inode: compr ret = ", ret);
401#endif
402 }
wdenkfe8c2802002-11-03 00:38:21 +0000403 counter++;
404 }
wdenkfe8c2802002-11-03 00:38:21 +0000405
406#if 0
wdenk06d01db2003-03-14 20:47:52 +0000407 putLabeledWord("read_inode: returning = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000408#endif
wdenk06d01db2003-03-14 20:47:52 +0000409 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000410}
411
412/* find the inode from the slashless name given a parent */
413static u32
414jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
415{
416 struct b_node *b;
417 struct jffs2_raw_dirent *jDir;
418 int len;
419 u32 counter;
420 u32 version = 0;
421 u32 inode = 0;
422
423 /* name is assumed slash free */
424 len = strlen(name);
425
426 counter = 0;
427 /* we need to search all and return the inode with the highest version */
wdenk06d01db2003-03-14 20:47:52 +0000428 for(b = pL->dir.listHead; b; b = b->next, counter++) {
wdenkfe8c2802002-11-03 00:38:21 +0000429 jDir = (struct jffs2_raw_dirent *) (b->offset);
wdenk06d01db2003-03-14 20:47:52 +0000430 if ((pino == jDir->pino) && (len == jDir->nsize) &&
431 (jDir->ino) && /* 0 for unlink */
wdenkfe8c2802002-11-03 00:38:21 +0000432 (!strncmp(jDir->name, name, len))) { /* a match */
433 if (jDir->version < version) continue;
434
wdenk06d01db2003-03-14 20:47:52 +0000435 if(jDir->version == 0) {
wdenkfe8c2802002-11-03 00:38:21 +0000436 /* Is this legal? */
437 putstr(" ** WARNING ** ");
438 putnstr(jDir->name, jDir->nsize);
439 putstr(" is version 0 (in find, ignoring)\r\n");
wdenk06d01db2003-03-14 20:47:52 +0000440 } else if(jDir->version == version) {
wdenkfe8c2802002-11-03 00:38:21 +0000441 /* Im pretty sure this isn't ... */
442 putstr(" ** ERROR ** ");
443 putnstr(jDir->name, jDir->nsize);
444 putLabeledWord(" has dup version =", version);
445 }
446 inode = jDir->ino;
447 version = jDir->version;
448 }
449#if 0
450 putstr("\r\nfind_inode:p&l ->");
451 putnstr(jDir->name, jDir->nsize);
452 putstr("\r\n");
453 putLabeledWord("pino = ", jDir->pino);
454 putLabeledWord("nsize = ", jDir->nsize);
455 putLabeledWord("b = ", (u32) b);
456 putLabeledWord("counter = ", counter);
457#endif
458 }
459 return inode;
460}
461
462static char *mkmodestr(unsigned long mode, char *str)
463{
wdenk06d01db2003-03-14 20:47:52 +0000464 static const char *l = "xwr";
465 int mask = 1, i;
466 char c;
wdenkfe8c2802002-11-03 00:38:21 +0000467
wdenk06d01db2003-03-14 20:47:52 +0000468 switch (mode & S_IFMT) {
469 case S_IFDIR: str[0] = 'd'; break;
470 case S_IFBLK: str[0] = 'b'; break;
471 case S_IFCHR: str[0] = 'c'; break;
472 case S_IFIFO: str[0] = 'f'; break;
473 case S_IFLNK: str[0] = 'l'; break;
474 case S_IFSOCK: str[0] = 's'; break;
475 case S_IFREG: str[0] = '-'; break;
476 default: str[0] = '?';
477 }
wdenkfe8c2802002-11-03 00:38:21 +0000478
wdenk06d01db2003-03-14 20:47:52 +0000479 for(i = 0; i < 9; i++) {
480 c = l[i%3];
481 str[9-i] = (mode & mask)?c:'-';
482 mask = mask<<1;
483 }
wdenkfe8c2802002-11-03 00:38:21 +0000484
wdenk06d01db2003-03-14 20:47:52 +0000485 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
486 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
487 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
488 str[10] = '\0';
489 return str;
wdenkfe8c2802002-11-03 00:38:21 +0000490}
491
492static inline void dump_stat(struct stat *st, const char *name)
493{
wdenk06d01db2003-03-14 20:47:52 +0000494 char str[20];
495 char s[64], *p;
wdenkfe8c2802002-11-03 00:38:21 +0000496
wdenk06d01db2003-03-14 20:47:52 +0000497 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
498 st->st_mtime = 1;
wdenkfe8c2802002-11-03 00:38:21 +0000499
wdenk06d01db2003-03-14 20:47:52 +0000500 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
wdenkfe8c2802002-11-03 00:38:21 +0000501
wdenk06d01db2003-03-14 20:47:52 +0000502 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
503 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000504
505/*
wdenk06d01db2003-03-14 20:47:52 +0000506 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
507 st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000508*/
509
wdenk06d01db2003-03-14 20:47:52 +0000510 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000511}
512
513static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
514{
515 char fname[256];
516 struct stat st;
517
518 if(!d || !i) return -1;
519
520 strncpy(fname, d->name, d->nsize);
wdenk06d01db2003-03-14 20:47:52 +0000521 fname[d->nsize] = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000522
523 memset(&st,0,sizeof(st));
524
wdenk06d01db2003-03-14 20:47:52 +0000525 st.st_mtime = i->mtime;
526 st.st_mode = i->mode;
527 st.st_ino = i->ino;
wdenkfe8c2802002-11-03 00:38:21 +0000528
529 /* neither dsize nor isize help us.. do it the long way */
wdenk06d01db2003-03-14 20:47:52 +0000530 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
wdenkfe8c2802002-11-03 00:38:21 +0000531
532 dump_stat(&st, fname);
533
534 if (d->type == DT_LNK) {
535 unsigned char *src = (unsigned char *) (&i[1]);
536 putstr(" -> ");
537 putnstr(src, (int)i->dsize);
538 }
539
540 putstr("\r\n");
541
542 return 0;
543}
544
545/* list inodes with the given pino */
546static u32
547jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
548{
549 struct b_node *b;
550 struct jffs2_raw_dirent *jDir;
551
wdenk06d01db2003-03-14 20:47:52 +0000552 for (b = pL->dir.listHead; b; b = b->next) {
wdenkfe8c2802002-11-03 00:38:21 +0000553 jDir = (struct jffs2_raw_dirent *) (b->offset);
wdenk06d01db2003-03-14 20:47:52 +0000554 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
555 u32 i_version = 0;
556 struct jffs2_raw_inode *jNode, *i = NULL;
557 struct b_node *b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000558
559 while (b2) {
560 jNode = (struct jffs2_raw_inode *) (b2->offset);
561 if (jNode->ino == jDir->ino
wdenk06d01db2003-03-14 20:47:52 +0000562 && jNode->version >= i_version)
563 i = jNode;
wdenkfe8c2802002-11-03 00:38:21 +0000564 b2 = b2->next;
565 }
566
567 dump_inode(pL, jDir, i);
568 }
569 }
570 return pino;
571}
572
573static u32
574jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
575{
576 int i;
577 char tmp[256];
578 char working_tmp[256];
579 char *c;
580
581 /* discard any leading slash */
582 i = 0;
583 while (fname[i] == '/')
584 i++;
585 strcpy(tmp, &fname[i]);
586
587 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
588 {
589 strncpy(working_tmp, tmp, c - tmp);
590 working_tmp[c - tmp] = '\0';
591#if 0
592 putstr("search_inode: tmp = ");
593 putstr(tmp);
594 putstr("\r\n");
595 putstr("search_inode: wtmp = ");
596 putstr(working_tmp);
597 putstr("\r\n");
598 putstr("search_inode: c = ");
599 putstr(c);
600 putstr("\r\n");
601#endif
602 for (i = 0; i < strlen(c) - 1; i++)
603 tmp[i] = c[i + 1];
604 tmp[i] = '\0';
605#if 0
606 putstr("search_inode: post tmp = ");
607 putstr(tmp);
608 putstr("\r\n");
609#endif
610
611 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
612 putstr("find_inode failed for name=");
613 putstr(working_tmp);
614 putstr("\r\n");
615 return 0;
616 }
617 }
618 /* this is for the bare filename, directories have already been mapped */
619 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
620 putstr("find_inode failed for name=");
621 putstr(tmp);
622 putstr("\r\n");
623 return 0;
624 }
625 return pino;
626
627}
628
629static u32
630jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
631{
632 struct b_node *b;
633 struct b_node *b2;
634 struct jffs2_raw_dirent *jDir;
635 struct jffs2_raw_inode *jNode;
636 struct jffs2_raw_dirent *jDirFound = NULL;
637 char tmp[256];
638 u32 version = 0;
639 u32 pino;
640 unsigned char *src;
641
642 /* we need to search all and return the inode with the highest version */
wdenk06d01db2003-03-14 20:47:52 +0000643 for(b = pL->dir.listHead; b; b = b->next) {
wdenkfe8c2802002-11-03 00:38:21 +0000644 jDir = (struct jffs2_raw_dirent *) (b->offset);
645 if (ino == jDir->ino) {
646 if(jDir->version < version) continue;
647
648 if(jDir->version == 0) {
649 /* Is this legal? */
650 putstr(" ** WARNING ** ");
651 putnstr(jDir->name, jDir->nsize);
652 putstr(" is version 0 (in resolve, ignoring)\r\n");
653 } else if(jDir->version == version) {
654 /* Im pretty sure this isn't ... */
655 putstr(" ** ERROR ** ");
656 putnstr(jDir->name, jDir->nsize);
657 putLabeledWord(" has dup version (resolve) = ",
658 version);
659 }
660
661 jDirFound = jDir;
662 version = jDir->version;
663 }
664 }
665 /* now we found the right entry again. (shoulda returned inode*) */
666 if (jDirFound->type != DT_LNK)
667 return jDirFound->ino;
wdenk06d01db2003-03-14 20:47:52 +0000668
669 /* it's a soft link so we follow it again. */
670 b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000671 while (b2) {
672 jNode = (struct jffs2_raw_inode *) (b2->offset);
673 if (jNode->ino == jDirFound->ino) {
674 src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode));
675
676#if 0
677 putLabeledWord("\t\t dsize = ", jNode->dsize);
678 putstr("\t\t target = ");
679 putnstr(src, jNode->dsize);
680 putstr("\r\n");
681#endif
682 strncpy(tmp, src, jNode->dsize);
683 tmp[jNode->dsize] = '\0';
684 break;
685 }
686 b2 = b2->next;
687 }
688 /* ok so the name of the new file to find is in tmp */
689 /* if it starts with a slash it is root based else shared dirs */
690 if (tmp[0] == '/')
691 pino = 1;
692 else
693 pino = jDirFound->pino;
694
695 return jffs2_1pass_search_inode(pL, tmp, pino);
696}
697
698static u32
699jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
700{
701 int i;
702 char tmp[256];
703 char working_tmp[256];
704 char *c;
705
706 /* discard any leading slash */
707 i = 0;
708 while (fname[i] == '/')
709 i++;
710 strcpy(tmp, &fname[i]);
711 working_tmp[0] = '\0';
712 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
713 {
714 strncpy(working_tmp, tmp, c - tmp);
715 working_tmp[c - tmp] = '\0';
716 for (i = 0; i < strlen(c) - 1; i++)
717 tmp[i] = c[i + 1];
718 tmp[i] = '\0';
719 /* only a failure if we arent looking at top level */
wdenk06d01db2003-03-14 20:47:52 +0000720 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
721 (working_tmp[0])) {
wdenkfe8c2802002-11-03 00:38:21 +0000722 putstr("find_inode failed for name=");
723 putstr(working_tmp);
724 putstr("\r\n");
725 return 0;
726 }
727 }
728
729 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
730 putstr("find_inode failed for name=");
731 putstr(tmp);
732 putstr("\r\n");
733 return 0;
734 }
735 /* this is for the bare filename, directories have already been mapped */
736 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
737 putstr("find_inode failed for name=");
738 putstr(tmp);
739 putstr("\r\n");
740 return 0;
741 }
742 return pino;
743
744}
745
746unsigned char
747jffs2_1pass_rescan_needed(struct part_info *part)
748{
749 struct b_node *b;
750 struct jffs2_unknown_node *node;
wdenk06d01db2003-03-14 20:47:52 +0000751 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +0000752
753 if (part->jffs2_priv == 0){
754 DEBUGF ("rescan: First time in use\n");
755 return 1;
756 }
757 /* if we have no list, we need to rescan */
wdenk06d01db2003-03-14 20:47:52 +0000758 if (pL->frag.listCount == 0) {
wdenkfe8c2802002-11-03 00:38:21 +0000759 DEBUGF ("rescan: fraglist zero\n");
760 return 1;
761 }
762
wdenk06d01db2003-03-14 20:47:52 +0000763 /* or if we are scanning a new partition */
wdenkfe8c2802002-11-03 00:38:21 +0000764 if (pL->partOffset != part->offset) {
765 DEBUGF ("rescan: different partition\n");
766 return 1;
767 }
wdenk06d01db2003-03-14 20:47:52 +0000768 /* but suppose someone reflashed a partition at the same offset... */
769 b = pL->dir.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000770 while (b) {
771 node = (struct jffs2_unknown_node *) (b->offset);
772 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
wdenk06d01db2003-03-14 20:47:52 +0000773 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
774 (unsigned long) b->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000775 return 1;
776 }
777 b = b->next;
778 }
779 return 0;
780}
781
wdenk06d01db2003-03-14 20:47:52 +0000782#ifdef DEBUG_FRAGMENTS
783static void
784dump_fragments(struct b_lists *pL)
785{
786 struct b_node *b;
787 struct jffs2_raw_inode *jNode;
788
789 putstr("\r\n\r\n******The fragment Entries******\r\n");
790 b = pL->frag.listHead;
791 while (b) {
792 jNode = (struct jffs2_raw_inode *) (b->offset);
793 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
794 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
795 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
796 putLabeledWord("\tbuild_list: version = ", jNode->version);
797 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
798 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
799 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
800 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
801 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
802 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
803 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
804 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
wdenk8bde7f72003-06-27 21:31:46 +0000805 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk06d01db2003-03-14 20:47:52 +0000806 b = b->next;
807 }
808}
809#endif
810
811#ifdef DEBUG_DIRENTS
812static void
813dump_dirents(struct b_lists *pL)
814{
815 struct b_node *b;
816 struct jffs2_raw_dirent *jDir;
817
818 putstr("\r\n\r\n******The directory Entries******\r\n");
819 b = pL->dir.listHead;
820 while (b) {
821 jDir = (struct jffs2_raw_dirent *) (b->offset);
822 putstr("\r\n");
823 putnstr(jDir->name, jDir->nsize);
824 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
825 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
826 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
827 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
828 putLabeledWord("\tbuild_list: version = ", jDir->version);
829 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
830 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
831 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
832 putLabeledWord("\tbuild_list: type = ", jDir->type);
833 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
834 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
wdenk8bde7f72003-06-27 21:31:46 +0000835 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk06d01db2003-03-14 20:47:52 +0000836 b = b->next;
837 }
838}
839#endif
840
wdenkfe8c2802002-11-03 00:38:21 +0000841static u32
842jffs2_1pass_build_lists(struct part_info * part)
843{
844 struct b_lists *pL;
845 struct jffs2_unknown_node *node;
wdenk06d01db2003-03-14 20:47:52 +0000846 u32 offset, oldoffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000847 u32 max = part->size - sizeof(struct jffs2_raw_inode);
848 u32 counter = 0;
849 u32 counter4 = 0;
850 u32 counterF = 0;
851 u32 counterN = 0;
852
853 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
854 /* jffs2 list building enterprise nope. in newer versions the overhead is */
855 /* only about 5 %. not enough to inconvenience people for. */
856 /* lcd_off(); */
857
858 /* if we are building a list we need to refresh the cache. */
wdenkfe8c2802002-11-03 00:38:21 +0000859 jffs_init_1pass_list(part);
wdenk06d01db2003-03-14 20:47:52 +0000860 pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +0000861 pL->partOffset = part->offset;
862 offset = 0;
863 printf("Scanning JFFS2 FS: ");
864
865 /* start at the beginning of the partition */
866 while (offset < max) {
wdenk06d01db2003-03-14 20:47:52 +0000867 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
868 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
869 oldoffset = offset;
870 }
wdenkfe8c2802002-11-03 00:38:21 +0000871
872 node = (struct jffs2_unknown_node *) (part->offset + offset);
873 if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
874 /* if its a fragment add it */
wdenk06d01db2003-03-14 20:47:52 +0000875 if (node->nodetype == JFFS2_NODETYPE_INODE &&
876 inode_crc((struct jffs2_raw_inode *) node)) {
877 if (insert_node(&pL->frag, (u32) part->offset +
878 offset) == NULL)
wdenkfe8c2802002-11-03 00:38:21 +0000879 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000880 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
881 dirent_crc((struct jffs2_raw_dirent *) node) &&
882 dirent_name_crc((struct jffs2_raw_dirent *) node)) {
883 if (! (counterN%100))
884 printf("\b\b. ");
wdenk06d01db2003-03-14 20:47:52 +0000885 if (insert_node(&pL->dir, (u32) part->offset +
886 offset) == NULL)
wdenkfe8c2802002-11-03 00:38:21 +0000887 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000888 counterN++;
889 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
890 if (node->totlen != sizeof(struct jffs2_unknown_node))
wdenk06d01db2003-03-14 20:47:52 +0000891 printf("OOPS Cleanmarker has bad size "
892 "%d != %d\n", node->totlen,
893 sizeof(struct jffs2_unknown_node));
wdenkfe8c2802002-11-03 00:38:21 +0000894 } else {
wdenk06d01db2003-03-14 20:47:52 +0000895 printf("Unknown node type: %x len %d "
896 "offset 0x%x\n", node->nodetype,
897 node->totlen, offset);
wdenkfe8c2802002-11-03 00:38:21 +0000898 }
899 offset += ((node->totlen + 3) & ~3);
900 counterF++;
wdenk06d01db2003-03-14 20:47:52 +0000901 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
902 node->nodetype == JFFS2_EMPTY_BITMASK) {
wdenkfe8c2802002-11-03 00:38:21 +0000903 offset = jffs2_scan_empty(offset, part);
wdenk06d01db2003-03-14 20:47:52 +0000904 } else { /* if we know nothing, we just step and look. */
wdenkfe8c2802002-11-03 00:38:21 +0000905 offset += 4;
906 counter4++;
907 }
908/* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
909
910 }
911
912 putstr("\b\b done.\r\n"); /* close off the dots */
913 /* turn the lcd back on. */
914 /* splash(); */
915
916#if 0
wdenk06d01db2003-03-14 20:47:52 +0000917 putLabeledWord("dir entries = ", pL->dir.listCount);
918 putLabeledWord("frag entries = ", pL->frag.listCount);
wdenkfe8c2802002-11-03 00:38:21 +0000919 putLabeledWord("+4 increments = ", counter4);
920 putLabeledWord("+file_offset increments = ", counterF);
921
922#endif
923
wdenk06d01db2003-03-14 20:47:52 +0000924#ifdef DEBUG_DIRENTS
925 dump_dirents(pL);
926#endif
wdenkfe8c2802002-11-03 00:38:21 +0000927
wdenk06d01db2003-03-14 20:47:52 +0000928#ifdef DEBUG_FRAGMENTS
929 dump_fragments(pL);
930#endif
wdenkfe8c2802002-11-03 00:38:21 +0000931
wdenkfe8c2802002-11-03 00:38:21 +0000932 /* give visual feedback that we are done scanning the flash */
933 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
934 return 1;
935}
936
937
wdenkfe8c2802002-11-03 00:38:21 +0000938static u32
939jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
940{
941 struct b_node *b;
942 struct jffs2_raw_inode *jNode;
943 int i;
944
wdenkfe8c2802002-11-03 00:38:21 +0000945 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
946 piL->compr_info[i].num_frags = 0;
947 piL->compr_info[i].compr_sum = 0;
948 piL->compr_info[i].decompr_sum = 0;
949 }
950
wdenk06d01db2003-03-14 20:47:52 +0000951 b = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +0000952 while (b) {
953 jNode = (struct jffs2_raw_inode *) (b->offset);
954 if (jNode->compr < JFFS2_NUM_COMPR) {
955 piL->compr_info[jNode->compr].num_frags++;
956 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
957 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
958 }
959 b = b->next;
960 }
961 return 0;
962}
963
964
wdenkfe8c2802002-11-03 00:38:21 +0000965static struct b_lists *
966jffs2_get_list(struct part_info * part, const char *who)
967{
968 if (jffs2_1pass_rescan_needed(part)) {
969 if (!jffs2_1pass_build_lists(part)) {
970 printf("%s: Failed to scan JFFSv2 file structure\n", who);
971 return NULL;
972 }
973 }
974 return (struct b_lists *)part->jffs2_priv;
975}
976
977
978/* Print directory / file contents */
979u32
980jffs2_1pass_ls(struct part_info * part, const char *fname)
981{
982 struct b_lists *pl;
983 long ret = 0;
984 u32 inode;
985
wdenk06d01db2003-03-14 20:47:52 +0000986 if (! (pl = jffs2_get_list(part, "ls")))
wdenkfe8c2802002-11-03 00:38:21 +0000987 return 0;
988
989 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
990 putstr("ls: Failed to scan jffs2 file structure\r\n");
991 return 0;
992 }
993
994
995#if 0
996 putLabeledWord("found file at inode = ", inode);
997 putLabeledWord("read_inode returns = ", ret);
998#endif
999
1000 return ret;
1001}
1002
1003
wdenkfe8c2802002-11-03 00:38:21 +00001004/* Load a file from flash into memory. fname can be a full path */
1005u32
1006jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1007{
1008
1009 struct b_lists *pl;
1010 long ret = 0;
1011 u32 inode;
1012
1013 if (! (pl = jffs2_get_list(part, "load")))
1014 return 0;
1015
1016 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1017 putstr("load: Failed to find inode\r\n");
1018 return 0;
1019 }
1020
1021 /* Resolve symlinks */
1022 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1023 putstr("load: Failed to resolve inode structure\r\n");
1024 return 0;
1025 }
1026
1027 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1028 putstr("load: Failed to read inode\r\n");
1029 return 0;
1030 }
1031
1032 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1033 (unsigned long) dest, ret);
1034 return ret;
1035}
1036
1037/* Return information about the fs on this partition */
1038u32
1039jffs2_1pass_info(struct part_info * part)
1040{
1041 struct b_jffs2_info info;
1042 struct b_lists *pl;
1043 int i;
1044
1045 if (! (pl = jffs2_get_list(part, "info")))
1046 return 0;
1047
1048 jffs2_1pass_fill_info(pl, &info);
wdenk06d01db2003-03-14 20:47:52 +00001049 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
wdenkfe8c2802002-11-03 00:38:21 +00001050 printf("Compression: %s\n", compr_names[i]);
1051 printf("\tfrag count: %d\n", info.compr_info[i].num_frags);
1052 printf("\tcompressed sum: %d\n", info.compr_info[i].compr_sum);
1053 printf("\tuncompressed sum: %d\n", info.compr_info[i].decompr_sum);
1054 }
1055 return 1;
1056}
1057
1058#endif /* CFG_CMD_JFFS2 */