]> git.ipfire.org Git - u-boot.git/blame - fs/jffs2/jffs2_1pass.c
* Code cleanup:
[u-boot.git] / fs / jffs2 / jffs2_1pass.c
CommitLineData
06d01dbe 1/* vi: set sw=4 ts=4: */
fe8c2806
WD
2/*
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
06d01dbe
WD
79/*
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
fe8c2806
WD
115#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
fe8c2806 128
06d01dbe
WD
129#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
130#define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
131
132/* 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
fe8c2806 137
fe8c2806
WD
138#ifdef DEBUG
139# define DEBUGF(fmt,args...) printf(fmt ,##args)
140#else
141# define DEBUGF(fmt,args...)
142#endif
143
fe8c2806 144
06d01dbe
WD
145/* 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}
fe8c2806
WD
176
177static struct b_node *
06d01dbe 178add_node(struct b_list *list)
fe8c2806 179{
06d01dbe
WD
180 u32 index = 0;
181 struct mem_block *memBase;
fe8c2806
WD
182 struct b_node *b;
183
06d01dbe
WD
184 memBase = list->listMemBase;
185 if (memBase != NULL)
186 index = memBase->index;
fe8c2806
WD
187#if 0
188 putLabeledWord("add_node: index = ", index);
06d01dbe 189 putLabeledWord("add_node: memBase = ", list->listMemBase);
fe8c2806
WD
190#endif
191
06d01dbe
WD
192 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) {
fe8c2806
WD
196 putstr("add_node: malloc failed\n");
197 return NULL;
198 }
06d01dbe
WD
199 memBase->next = list->listMemBase;
200 index = 0;
fe8c2806
WD
201#if 0
202 putLabeledWord("add_node: alloced a new membase at ", *memBase);
203#endif
204
205 }
206 /* now we have room to add it. */
06d01dbe
WD
207 b = &memBase->nodes[index];
208 index ++;
fe8c2806 209
06d01dbe
WD
210 memBase->index = index;
211 list->listMemBase = memBase;
212 list->listCount++;
213 return b;
214}
fe8c2806 215
06d01dbe
WD
216static 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;
fe8c2806
WD
222#endif
223
06d01dbe
WD
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
fe8c2806 253#endif
06d01dbe
WD
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 }
fe8c2806 263
06d01dbe 264 return new;
fe8c2806
WD
265}
266
06d01dbe
WD
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
fe8c2806
WD
284
285static u32
286jffs2_scan_empty(u32 start_offset, struct part_info *part)
287{
06d01dbe
WD
288 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
289 char *offset = part->offset + start_offset;
fe8c2806 290
06d01dbe
WD
291 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;
fe8c2806 295 }
06d01dbe
WD
296
297 return offset - part->offset;
fe8c2806
WD
298}
299
300static u32
301jffs_init_1pass_list(struct part_info *part)
302{
06d01dbe 303 struct b_lists *pL;
fe8c2806 304
06d01dbe
WD
305 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
fe8c2806
WD
319 }
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;
06d01dbe
WD
329 u32 totalSize = 0;
330 u16 latestVersion = 0;
331 char *lDest;
fe8c2806
WD
332 char *src;
333 long ret;
334 int i;
335 u32 counter = 0;
fe8c2806 336
06d01dbe 337 for (b = pL->frag.listHead; b != NULL; b = b->next) {
fe8c2806
WD
338 jNode = (struct jffs2_raw_inode *) (b->offset);
339 if ((inode == jNode->ino)) {
06d01dbe 340#if 0
fe8c2806
WD
341 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);
fe8c2806 351#endif
06d01dbe
WD
352 /* get actual file length from the newest node */
353 if (jNode->version >= latestVersion) {
fe8c2806 354 totalSize = jNode->isize;
06d01dbe 355 latestVersion = jNode->version;
fe8c2806 356 }
fe8c2806
WD
357
358 if(dest) {
359 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
06d01dbe
WD
360 /* ignore data behind latest known EOF */
361 if (jNode->offset > totalSize)
362 continue;
363
fe8c2806
WD
364 lDest = (char *) (dest + jNode->offset);
365#if 0
06d01dbe 366 putLabeledWord("read_inode: src = ", src);
fe8c2806 367 putLabeledWord("read_inode: dest = ", lDest);
fe8c2806
WD
368#endif
369 switch (jNode->compr) {
370 case JFFS2_COMPR_NONE:
fe8c2806
WD
371 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
fe8c2806 398#if 0
fe8c2806
WD
399 putLabeledWord("read_inode: totalSize = ", totalSize);
400 putLabeledWord("read_inode: compr ret = ", ret);
401#endif
402 }
fe8c2806
WD
403 counter++;
404 }
fe8c2806
WD
405
406#if 0
06d01dbe 407 putLabeledWord("read_inode: returning = ", totalSize);
fe8c2806 408#endif
06d01dbe 409 return totalSize;
fe8c2806
WD
410}
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 */
06d01dbe 428 for(b = pL->dir.listHead; b; b = b->next, counter++) {
fe8c2806 429 jDir = (struct jffs2_raw_dirent *) (b->offset);
06d01dbe
WD
430 if ((pino == jDir->pino) && (len == jDir->nsize) &&
431 (jDir->ino) && /* 0 for unlink */
fe8c2806
WD
432 (!strncmp(jDir->name, name, len))) { /* a match */
433 if (jDir->version < version) continue;
434
06d01dbe 435 if(jDir->version == 0) {
fe8c2806
WD
436 /* Is this legal? */
437 putstr(" ** WARNING ** ");
438 putnstr(jDir->name, jDir->nsize);
439 putstr(" is version 0 (in find, ignoring)\r\n");
06d01dbe 440 } else if(jDir->version == version) {
fe8c2806
WD
441 /* 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{
06d01dbe
WD
464 static const char *l = "xwr";
465 int mask = 1, i;
466 char c;
467
468 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 }
478
479 for(i = 0; i < 9; i++) {
480 c = l[i%3];
481 str[9-i] = (mode & mask)?c:'-';
482 mask = mask<<1;
483 }
484
485 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;
fe8c2806
WD
490}
491
492static inline void dump_stat(struct stat *st, const char *name)
493{
06d01dbe
WD
494 char str[20];
495 char s[64], *p;
fe8c2806 496
06d01dbe
WD
497 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
498 st->st_mtime = 1;
fe8c2806 499
06d01dbe 500 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
fe8c2806 501
06d01dbe
WD
502 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
503 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
fe8c2806
WD
504
505/*
06d01dbe
WD
506 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
507 st->st_size, s, name);
fe8c2806
WD
508*/
509
06d01dbe 510 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
fe8c2806
WD
511}
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);
06d01dbe 521 fname[d->nsize] = '\0';
fe8c2806
WD
522
523 memset(&st,0,sizeof(st));
524
06d01dbe
WD
525 st.st_mtime = i->mtime;
526 st.st_mode = i->mode;
527 st.st_ino = i->ino;
fe8c2806
WD
528
529 /* neither dsize nor isize help us.. do it the long way */
06d01dbe 530 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
fe8c2806
WD
531
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
06d01dbe 552 for (b = pL->dir.listHead; b; b = b->next) {
fe8c2806 553 jDir = (struct jffs2_raw_dirent *) (b->offset);
06d01dbe
WD
554 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;
fe8c2806
WD
558
559 while (b2) {
560 jNode = (struct jffs2_raw_inode *) (b2->offset);
561 if (jNode->ino == jDir->ino
06d01dbe
WD
562 && jNode->version >= i_version)
563 i = jNode;
fe8c2806
WD
564 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 */
06d01dbe 643 for(b = pL->dir.listHead; b; b = b->next) {
fe8c2806
WD
644 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;
06d01dbe
WD
668
669 /* it's a soft link so we follow it again. */
670 b2 = pL->frag.listHead;
fe8c2806
WD
671 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 */
06d01dbe
WD
720 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
721 (working_tmp[0])) {
fe8c2806
WD
722 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;
06d01dbe 751 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
fe8c2806
WD
752
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 */
06d01dbe 758 if (pL->frag.listCount == 0) {
fe8c2806
WD
759 DEBUGF ("rescan: fraglist zero\n");
760 return 1;
761 }
762
06d01dbe 763 /* or if we are scanning a new partition */
fe8c2806
WD
764 if (pL->partOffset != part->offset) {
765 DEBUGF ("rescan: different partition\n");
766 return 1;
767 }
06d01dbe
WD
768 /* but suppose someone reflashed a partition at the same offset... */
769 b = pL->dir.listHead;
fe8c2806
WD
770 while (b) {
771 node = (struct jffs2_unknown_node *) (b->offset);
772 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
06d01dbe
WD
773 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
774 (unsigned long) b->offset);
fe8c2806
WD
775 return 1;
776 }
777 b = b->next;
778 }
779 return 0;
780}
781
06d01dbe
WD
782#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);
8bde7f77 805 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
06d01dbe
WD
806 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);
8bde7f77 835 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
06d01dbe
WD
836 b = b->next;
837 }
838}
839#endif
840
fe8c2806
WD
841static u32
842jffs2_1pass_build_lists(struct part_info * part)
843{
844 struct b_lists *pL;
845 struct jffs2_unknown_node *node;
06d01dbe 846 u32 offset, oldoffset = 0;
fe8c2806
WD
847 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. */
fe8c2806 859 jffs_init_1pass_list(part);
06d01dbe 860 pL = (struct b_lists *)part->jffs2_priv;
fe8c2806
WD
861 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) {
06d01dbe
WD
867 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
868 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
869 oldoffset = offset;
870 }
fe8c2806
WD
871
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 */
06d01dbe
WD
875 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)
fe8c2806 879 return 0;
fe8c2806
WD
880 } 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. ");
06d01dbe
WD
885 if (insert_node(&pL->dir, (u32) part->offset +
886 offset) == NULL)
fe8c2806 887 return 0;
fe8c2806
WD
888 counterN++;
889 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
890 if (node->totlen != sizeof(struct jffs2_unknown_node))
06d01dbe
WD
891 printf("OOPS Cleanmarker has bad size "
892 "%d != %d\n", node->totlen,
893 sizeof(struct jffs2_unknown_node));
fe8c2806 894 } else {
06d01dbe
WD
895 printf("Unknown node type: %x len %d "
896 "offset 0x%x\n", node->nodetype,
897 node->totlen, offset);
fe8c2806
WD
898 }
899 offset += ((node->totlen + 3) & ~3);
900 counterF++;
06d01dbe
WD
901 } else if (node->magic == JFFS2_EMPTY_BITMASK &&
902 node->nodetype == JFFS2_EMPTY_BITMASK) {
fe8c2806 903 offset = jffs2_scan_empty(offset, part);
06d01dbe 904 } else { /* if we know nothing, we just step and look. */
fe8c2806
WD
905 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
06d01dbe
WD
917 putLabeledWord("dir entries = ", pL->dir.listCount);
918 putLabeledWord("frag entries = ", pL->frag.listCount);
fe8c2806
WD
919 putLabeledWord("+4 increments = ", counter4);
920 putLabeledWord("+file_offset increments = ", counterF);
921
922#endif
923
06d01dbe
WD
924#ifdef DEBUG_DIRENTS
925 dump_dirents(pL);
926#endif
fe8c2806 927
06d01dbe
WD
928#ifdef DEBUG_FRAGMENTS
929 dump_fragments(pL);
930#endif
fe8c2806 931
fe8c2806
WD
932 /* 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
fe8c2806
WD
938static 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
fe8c2806
WD
945 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
06d01dbe 951 b = pL->frag.listHead;
fe8c2806
WD
952 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
fe8c2806
WD
965static 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
06d01dbe 986 if (! (pl = jffs2_get_list(part, "ls")))
fe8c2806
WD
987 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
fe8c2806
WD
1004/* 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);
06d01dbe 1049 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
fe8c2806
WD
1050 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 */