jffs2_1pass.c 36.8 KB
Newer Older
W
wdenk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*
-------------------------------------------------------------------------
 * Filename:      jffs2.c
 * Version:       $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
 * Copyright:     Copyright (C) 2001, Russ Dill
 * Author:        Russ Dill <Russ.Dill@asu.edu>
 * Description:   Module to load kernel from jffs2
 *-----------------------------------------------------------------------*/
/*
 * some portions of this code are taken from jffs2, and as such, the
 * following copyright notice is included.
 *
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
 * Copyright (C) 2001 Red Hat, Inc.
 *
 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
 *
 * The original JFFS, from which the design for JFFS2 was derived,
 * was designed and implemented by Axis Communications AB.
 *
 * The contents of this file are subject to the Red Hat eCos Public
 * License Version 1.1 (the "Licence"); you may not use this file
 * except in compliance with the Licence.  You may obtain a copy of
 * the Licence at http://www.redhat.com/
 *
 * Software distributed under the Licence is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing rights and
 * limitations under the Licence.
 *
 * The Original Code is JFFS2 - Journalling Flash File System, version 2
 *
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License version 2 (the "GPL"), in
 * which case the provisions of the GPL are applicable instead of the
 * above.  If you wish to allow the use of your version of this file
 * only under the terms of the GPL and not to allow others to use your
 * version of this file under the RHEPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and
 * other provisions required by the GPL.  If you do not delete the
 * provisions above, a recipient may use your version of this file
 * under either the RHEPL or the GPL.
 *
 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
 *
 */

/* Ok, so anyone who knows the jffs2 code will probably want to get a papar
 * bag to throw up into before reading this code. I looked through the jffs2
 * code, the caching scheme is very elegant. I tried to keep the version
 * for a bootloader as small and simple as possible. Instead of worring about
 * unneccesary data copies, node scans, etc, I just optimized for the known
 * common case, a kernel, which looks like:
 * 	(1) most pages are 4096 bytes
 *	(2) version numbers are somewhat sorted in acsending order
 *	(3) multiple compressed blocks making up one page is uncommon
 *
 * So I create a linked list of decending version numbers (insertions at the
 * head), and then for each page, walk down the list, until a matching page
 * with 4096 bytes is found, and then decompress the watching pages in
 * reverse order.
 *
 */

/*
 * Adapted by Nye Liu <nyet@zumanetworks.com> and
 * Rex Feany <rfeany@zumanetworks.com>
 * on Jan/2002 for U-Boot.
 *
 * Clipped out all the non-1pass functions, cleaned up warnings,
 * wrappers, etc. No major changes to the code.
 * Please, he really means it when he said have a paper bag
 * handy. We needed it ;).
 *
 */

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*
 * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
 *
 * - overhaul of the memory management. Removed much of the "paper-bagging"
 *   in that part of the code, fixed several bugs, now frees memory when
 *   partition is changed.
 *   It's still ugly :-(
 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
 *   was incorrect. Removed a bit of the paper-bagging as well.
 * - removed double crc calculation for fragment headers in jffs2_private.h
 *   for speedup.
 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
 * - spinning wheel now spins depending on how much memory has been scanned
 * - lots of small changes all over the place to "improve" readability.
 * - implemented fragment sorting to ensure that the newest data is copied
 *   if there are multiple copies of fragments for a certain file offset.
 *
 * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS.
 * Sorting is done while adding fragments to the lists, which is more or less a
 * bubble sort. This takes a lot of time, and is most probably not an issue if
 * the boot filesystem is always mounted readonly.
 *
 * You should define it if the boot filesystem is mounted writable, and updates
 * to the boot files are done by copying files to that filesystem.
 *
 *
 * There's a big issue left: endianess is completely ignored in this code. Duh!
 *
 *
 * You still should have paper bags at hand :-(. The code lacks more or less
 * any comment, and is still arcane and difficult to read in places. As this
W
wdenk 已提交
109 110
 * might be incompatible with any new code from the jffs2 maintainers anyway,
 * it should probably be dumped and replaced by something like jffs2reader!
111 112 113
 */


W
wdenk 已提交
114 115 116 117 118 119
#include <common.h>
#include <config.h>
#include <malloc.h>
#include <linux/stat.h>
#include <linux/time.h>

120
#if defined(CONFIG_CMD_JFFS2)
W
wdenk 已提交
121 122 123 124 125 126 127

#include <jffs2/jffs2.h>
#include <jffs2/jffs2_1pass.h>

#include "jffs2_private.h"


128
#define	NODE_CHUNK  	1024	/* size of memory allocation chunk in b_nodes */
129
#define	SPIN_BLKSIZE	18  	/* spin after having scanned 1<<BLKSIZE bytes */
130 131 132 133 134 135

/* Debugging switches */
#undef	DEBUG_DIRENTS		/* print directory entry list after scan */
#undef	DEBUG_FRAGMENTS		/* print fragment list after scan */
#undef	DEBUG   			/* enable debugging messages */

W
wdenk 已提交
136 137 138 139 140 141 142

#ifdef  DEBUG
# define DEBUGF(fmt,args...)	printf(fmt ,##args)
#else
# define DEBUGF(fmt,args...)
#endif

143 144
/* keeps pointer to currentlu processed partition */
static struct part_info *current_part;
145

146
#if (defined(CONFIG_JFFS2_NAND) && \
147
     defined(CONFIG_CMD_NAND) )
148 149 150
#if defined(CFG_NAND_LEGACY)
#include <linux/mtd/nand_legacy.h>
#else
151
#include <nand.h>
152
#endif
153 154 155 156 157 158 159 160 161 162
/*
 * Support for jffs2 on top of NAND-flash
 *
 * NAND memory isn't mapped in processor's address space,
 * so data should be fetched from flash before
 * being processed. This is exactly what functions declared
 * here do.
 *
 */

163 164 165 166 167
#if defined(CFG_NAND_LEGACY)
/* this one defined in nand_legacy.c */
int read_jffs2_nand(size_t start, size_t len,
		size_t * retlen, u_char * buf, int nanddev);
#else
168 169
/* info for NAND chips, defined in drivers/nand/nand.c */
extern nand_info_t nand_info[];
170
#endif
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

#define NAND_PAGE_SIZE 512
#define NAND_PAGE_SHIFT 9
#define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))

#ifndef NAND_CACHE_PAGES
#define NAND_CACHE_PAGES 16
#endif
#define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)

static u8* nand_cache = NULL;
static u32 nand_cache_off = (u32)-1;

static int read_nand_cached(u32 off, u32 size, u_char *buf)
{
186
	struct mtdids *id = current_part->dev->id;
187
	u32 bytes_read = 0;
188 189 190
#if defined(CFG_NAND_LEGACY)
	size_t retlen;
#else
191
	ulong retlen;
192
#endif
193 194 195 196 197 198 199 200 201
	int cpy_bytes;

	while (bytes_read < size) {
		if ((off + bytes_read < nand_cache_off) ||
		    (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
			nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
			if (!nand_cache) {
				/* This memory never gets freed but 'cause
				   it's a bootloader, nobody cares */
W
wdenk 已提交
202 203
				nand_cache = malloc(NAND_CACHE_SIZE);
				if (!nand_cache) {
204 205 206 207 208
					printf("read_nand_cached: can't alloc cache size %d bytes\n",
					       NAND_CACHE_SIZE);
					return -1;
				}
			}
209

210 211 212 213 214 215 216 217 218
#if defined(CFG_NAND_LEGACY)
			if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE,
						&retlen, nand_cache, id->num) < 0 ||
					retlen != NAND_CACHE_SIZE) {
				printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
						nand_cache_off, NAND_CACHE_SIZE);
				return -1;
			}
#else
219 220
			retlen = NAND_CACHE_SIZE;
			if (nand_read(&nand_info[id->num], nand_cache_off,
221
						&retlen, nand_cache) != 0 ||
222
					retlen != NAND_CACHE_SIZE) {
223
				printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
224
						nand_cache_off, NAND_CACHE_SIZE);
225 226
				return -1;
			}
227
#endif
228 229 230 231 232 233 234 235 236 237 238 239
		}
		cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
		if (cpy_bytes > size - bytes_read)
			cpy_bytes = size - bytes_read;
		memcpy(buf + bytes_read,
		       nand_cache + off + bytes_read - nand_cache_off,
		       cpy_bytes);
		bytes_read += cpy_bytes;
	}
	return bytes_read;
}

240
static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
241 242 243 244
{
	u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);

	if (NULL == buf) {
245
		printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
246 247 248
		return NULL;
	}
	if (read_nand_cached(off, size, buf) < 0) {
249 250
		if (!ext_buf)
			free(buf);
251 252 253 254 255 256
		return NULL;
	}

	return buf;
}

257
static void *get_node_mem_nand(u32 off)
258 259 260 261
{
	struct jffs2_unknown_node node;
	void *ret = NULL;

262
	if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
263 264
		return NULL;

265
	if (!(ret = get_fl_mem_nand(off, node.magic ==
266 267 268 269 270 271 272 273
			       JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
			       NULL))) {
		printf("off = %#x magic %#x type %#x node.totlen = %d\n",
		       off, node.magic, node.nodetype, node.totlen);
	}
	return ret;
}

274
static void put_fl_mem_nand(void *buf)
275 276 277
{
	free(buf);
}
278
#endif
279 280


281
#if defined(CONFIG_CMD_FLASH)
282 283 284 285 286 287 288 289 290 291 292
/*
 * Support for jffs2 on top of NOR-flash
 *
 * NOR flash memory is mapped in processor's address space,
 * just return address.
 */
static inline void *get_fl_mem_nor(u32 off)
{
	u32 addr = off;
	struct mtdids *id = current_part->dev->id;

293
	extern flash_info_t flash_info[];
294 295 296 297 298 299 300 301 302 303
	flash_info_t *flash = &flash_info[id->num];

	addr += flash->start[0];
	return (void*)addr;
}

static inline void *get_node_mem_nor(u32 off)
{
	return (void*)get_fl_mem_nor(off);
}
304
#endif
305 306


307
/*
308
 * Generic jffs2 raw memory and node read routines.
309 310
 *
 */
311 312
static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
{
313
	struct mtdids *id = current_part->dev->id;
314

315
#if defined(CONFIG_CMD_FLASH)
316 317 318 319
	if (id->type == MTD_DEV_TYPE_NOR)
		return get_fl_mem_nor(off);
#endif

320
#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
321 322 323 324 325
	if (id->type == MTD_DEV_TYPE_NAND)
		return get_fl_mem_nand(off, size, ext_buf);
#endif

	printf("get_fl_mem: unknown device type, using raw offset!\n");
326 327 328 329 330
	return (void*)off;
}

static inline void *get_node_mem(u32 off)
{
331
	struct mtdids *id = current_part->dev->id;
332

333
#if defined(CONFIG_CMD_FLASH)
334 335 336 337
	if (id->type == MTD_DEV_TYPE_NOR)
		return get_node_mem_nor(off);
#endif

338
#if defined(CONFIG_JFFS2_NAND) && \
339
    defined(CONFIG_CMD_NAND)
340 341 342 343 344
	if (id->type == MTD_DEV_TYPE_NAND)
		return get_node_mem_nand(off);
#endif

	printf("get_node_mem: unknown device type, using raw offset!\n");
345 346 347
	return (void*)off;
}

W
wdenk 已提交
348
static inline void put_fl_mem(void *buf)
349
{
350
#if defined(CONFIG_JFFS2_NAND) && \
351
    defined(CONFIG_CMD_NAND)
352
	struct mtdids *id = current_part->dev->id;
353

354 355 356 357
	if (id->type == MTD_DEV_TYPE_NAND)
		return put_fl_mem_nand(buf);
#endif
}
W
wdenk 已提交
358

359 360 361 362 363 364 365 366
/* Compression names */
static char *compr_names[] = {
	"NONE",
	"ZERO",
	"RTIME",
	"RUBINMIPS",
	"COPY",
	"DYNRUBIN",
367
	"ZLIB",
368
#if defined(CONFIG_JFFS2_LZO_LZARI)
369 370 371
	"LZO",
	"LZARI",
#endif
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
};

/* Spinning wheel */
static char spinner[] = { '|', '/', '-', '\\' };

/* Memory management */
struct mem_block {
	u32	index;
	struct mem_block *next;
	struct b_node nodes[NODE_CHUNK];
};


static void
free_nodes(struct b_list *list)
{
	while (list->listMemBase != NULL) {
		struct mem_block *next = list->listMemBase->next;
		free( list->listMemBase );
		list->listMemBase = next;
	}
}
W
wdenk 已提交
394 395

static struct b_node *
396
add_node(struct b_list *list)
W
wdenk 已提交
397
{
398 399
	u32 index = 0;
	struct mem_block *memBase;
W
wdenk 已提交
400 401
	struct b_node *b;

402 403 404
	memBase = list->listMemBase;
	if (memBase != NULL)
		index = memBase->index;
W
wdenk 已提交
405 406
#if 0
	putLabeledWord("add_node: index = ", index);
407
	putLabeledWord("add_node: memBase = ", list->listMemBase);
W
wdenk 已提交
408 409
#endif

410 411 412 413
	if (memBase == NULL || index >= NODE_CHUNK) {
		/* we need more space before we continue */
		memBase = mmalloc(sizeof(struct mem_block));
		if (memBase == NULL) {
W
wdenk 已提交
414 415 416
			putstr("add_node: malloc failed\n");
			return NULL;
		}
417 418
		memBase->next = list->listMemBase;
		index = 0;
W
wdenk 已提交
419 420 421 422 423 424
#if 0
		putLabeledWord("add_node: alloced a new membase at ", *memBase);
#endif

	}
	/* now we have room to add it. */
425 426
	b = &memBase->nodes[index];
	index ++;
W
wdenk 已提交
427

428 429 430 431 432
	memBase->index = index;
	list->listMemBase = memBase;
	list->listCount++;
	return b;
}
W
wdenk 已提交
433

434 435 436 437 438 439
static struct b_node *
insert_node(struct b_list *list, u32 offset)
{
	struct b_node *new;
#ifdef CFG_JFFS2_SORT_FRAGMENTS
	struct b_node *b, *prev;
W
wdenk 已提交
440 441
#endif

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	if (!(new = add_node(list))) {
		putstr("add_node failed!\r\n");
		return NULL;
	}
	new->offset = offset;

#ifdef CFG_JFFS2_SORT_FRAGMENTS
	if (list->listTail != NULL && list->listCompare(new, list->listTail))
		prev = list->listTail;
	else if (list->listLast != NULL && list->listCompare(new, list->listLast))
		prev = list->listLast;
	else
		prev = NULL;

	for (b = (prev ? prev->next : list->listHead);
	     b != NULL && list->listCompare(new, b);
	     prev = b, b = b->next) {
		list->listLoops++;
	}
	if (b != NULL)
		list->listLast = prev;

	if (b != NULL) {
		new->next = b;
		if (prev != NULL)
			prev->next = new;
		else
			list->listHead = new;
	} else
W
wdenk 已提交
471
#endif
472 473 474 475 476 477 478 479 480
	{
		new->next = (struct b_node *) NULL;
		if (list->listTail != NULL) {
			list->listTail->next = new;
			list->listTail = new;
		} else {
			list->listTail = list->listHead = new;
		}
	}
W
wdenk 已提交
481

482
	return new;
W
wdenk 已提交
483 484
}

485
#ifdef CFG_JFFS2_SORT_FRAGMENTS
W
wdenk 已提交
486 487 488
/* Sort data entries with the latest version last, so that if there
 * is overlapping data the latest version will be used.
 */
489 490
static int compare_inodes(struct b_node *new, struct b_node *old)
{
491 492 493 494 495 496
	struct jffs2_raw_inode ojNew;
	struct jffs2_raw_inode ojOld;
	struct jffs2_raw_inode *jNew =
		(struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
	struct jffs2_raw_inode *jOld =
		(struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
497

W
wdenk 已提交
498
	return jNew->version > jOld->version;
499 500
}

W
wdenk 已提交
501 502 503 504 505
/* Sort directory entries so all entries in the same directory
 * with the same name are grouped together, with the latest version
 * last. This makes it easy to eliminate all but the latest version
 * by marking the previous version dead by setting the inode to 0.
 */
506 507
static int compare_dirents(struct b_node *new, struct b_node *old)
{
508 509 510 511
	struct jffs2_raw_dirent ojNew;
	struct jffs2_raw_dirent ojOld;
	struct jffs2_raw_dirent *jNew =
		(struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
W
wdenk 已提交
512
	struct jffs2_raw_dirent *jOld =
513
		(struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
W
wdenk 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527
	int cmp;

	/* ascending sort by pino */
	if (jNew->pino != jOld->pino)
		return jNew->pino > jOld->pino;

	/* pino is the same, so use ascending sort by nsize, so
	 * we don't do strncmp unless we really must.
	 */
	if (jNew->nsize != jOld->nsize)
		return jNew->nsize > jOld->nsize;

	/* length is also the same, so use ascending sort by name
	 */
W
Wolfgang Denk 已提交
528
	cmp = strncmp((char *)jNew->name, (char *)jOld->name, jNew->nsize);
W
wdenk 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541
	if (cmp != 0)
		return cmp > 0;

	/* we have duplicate names in this directory, so use ascending
	 * sort by version
	 */
	if (jNew->version > jOld->version) {
		/* since jNew is newer, we know jOld is not valid, so
		 * mark it with inode 0 and it will not be used
		 */
		jOld->ino = 0;
		return 1;
	}
W
wdenk 已提交
542

W
wdenk 已提交
543
	return 0;
544 545
}
#endif
W
wdenk 已提交
546 547 548 549

static u32
jffs2_scan_empty(u32 start_offset, struct part_info *part)
{
550 551
	char *max = (char *)(part->offset + part->size - sizeof(struct jffs2_raw_inode));
	char *offset = (char *)(part->offset + start_offset);
552
	u32 off;
W
wdenk 已提交
553

554 555
	while (offset < max &&
	       *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) {
556 557 558
		offset += sizeof(u32);
		/* return if spinning is due */
		if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
W
wdenk 已提交
559
	}
560

561
	return (u32)offset - part->offset;
W
wdenk 已提交
562 563
}

564 565
void
jffs2_free_cache(struct part_info *part)
W
wdenk 已提交
566
{
567
	struct b_lists *pL;
W
wdenk 已提交
568

569 570 571 572 573 574
	if (part->jffs2_priv != NULL) {
		pL = (struct b_lists *)part->jffs2_priv;
		free_nodes(&pL->frag);
		free_nodes(&pL->dir);
		free(pL);
	}
575 576 577 578 579 580 581 582 583
}

static u32
jffs_init_1pass_list(struct part_info *part)
{
	struct b_lists *pL;

	jffs2_free_cache(part);

584 585 586 587 588 589 590 591
	if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
		pL = (struct b_lists *)part->jffs2_priv;

		memset(pL, 0, sizeof(*pL));
#ifdef CFG_JFFS2_SORT_FRAGMENTS
		pL->dir.listCompare = compare_dirents;
		pL->frag.listCompare = compare_inodes;
#endif
W
wdenk 已提交
592 593 594 595 596 597 598 599 600 601
	}
	return 0;
}

/* find the inode from the slashless name given a parent */
static long
jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
{
	struct b_node *b;
	struct jffs2_raw_inode *jNode;
602
	u32 totalSize = 0;
W
wdenk 已提交
603
	u32 latestVersion = 0;
W
Wolfgang Denk 已提交
604 605
	uchar *lDest;
	uchar *src;
W
wdenk 已提交
606 607 608
	long ret;
	int i;
	u32 counter = 0;
W
wdenk 已提交
609 610 611 612 613 614 615 616 617
#ifdef CFG_JFFS2_SORT_FRAGMENTS
	/* Find file size before loading any data, so fragments that
	 * start past the end of file can be ignored. A fragment
	 * that is partially in the file is loaded, so extra data may
	 * be loaded up to the next 4K boundary above the file size.
	 * This shouldn't cause trouble when loading kernel images, so
	 * we will live with it.
	 */
	for (b = pL->frag.listHead; b != NULL; b = b->next) {
W
wdenk 已提交
618
		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
619
		        sizeof(struct jffs2_raw_inode), NULL);
W
wdenk 已提交
620 621 622 623 624 625 626
		if ((inode == jNode->ino)) {
			/* get actual file length from the newest node */
			if (jNode->version >= latestVersion) {
				totalSize = jNode->isize;
				latestVersion = jNode->version;
			}
		}
627
		put_fl_mem(jNode);
W
wdenk 已提交
628 629
	}
#endif
W
wdenk 已提交
630

631
	for (b = pL->frag.listHead; b != NULL; b = b->next) {
632
		jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset);
W
wdenk 已提交
633
		if ((inode == jNode->ino)) {
634
#if 0
W
wdenk 已提交
635 636 637 638 639 640 641 642 643 644 645
			putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
			putLabeledWord("read_inode: inode = ", jNode->ino);
			putLabeledWord("read_inode: version = ", jNode->version);
			putLabeledWord("read_inode: isize = ", jNode->isize);
			putLabeledWord("read_inode: offset = ", jNode->offset);
			putLabeledWord("read_inode: csize = ", jNode->csize);
			putLabeledWord("read_inode: dsize = ", jNode->dsize);
			putLabeledWord("read_inode: compr = ", jNode->compr);
			putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
			putLabeledWord("read_inode: flags = ", jNode->flags);
#endif
W
wdenk 已提交
646 647

#ifndef CFG_JFFS2_SORT_FRAGMENTS
648 649
			/* get actual file length from the newest node */
			if (jNode->version >= latestVersion) {
W
wdenk 已提交
650
				totalSize = jNode->isize;
651
				latestVersion = jNode->version;
W
wdenk 已提交
652
			}
W
wdenk 已提交
653
#endif
W
wdenk 已提交
654 655

			if(dest) {
W
Wolfgang Denk 已提交
656
				src = ((uchar *) jNode) + sizeof(struct jffs2_raw_inode);
657
				/* ignore data behind latest known EOF */
658 659
				if (jNode->offset > totalSize) {
					put_fl_mem(jNode);
660
					continue;
661
				}
662

W
Wolfgang Denk 已提交
663
				lDest = (uchar *) (dest + jNode->offset);
W
wdenk 已提交
664
#if 0
665
				putLabeledWord("read_inode: src = ", src);
W
wdenk 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
				putLabeledWord("read_inode: dest = ", lDest);
#endif
				switch (jNode->compr) {
				case JFFS2_COMPR_NONE:
					ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
					break;
				case JFFS2_COMPR_ZERO:
					ret = 0;
					for (i = 0; i < jNode->dsize; i++)
						*(lDest++) = 0;
					break;
				case JFFS2_COMPR_RTIME:
					ret = 0;
					rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
					break;
				case JFFS2_COMPR_DYNRUBIN:
					/* this is slow but it works */
					ret = 0;
					dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
					break;
				case JFFS2_COMPR_ZLIB:
					ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
					break;
689
#if defined(CONFIG_JFFS2_LZO_LZARI)
690 691 692
				case JFFS2_COMPR_LZO:
					ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
					break;
693 694 695
				case JFFS2_COMPR_LZARI:
					ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
					break;
696
#endif
W
wdenk 已提交
697 698 699
				default:
					/* unknown */
					putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
700
					put_fl_mem(jNode);
W
wdenk 已提交
701 702 703 704 705 706 707 708 709 710 711
					return -1;
					break;
				}
			}

#if 0
			putLabeledWord("read_inode: totalSize = ", totalSize);
			putLabeledWord("read_inode: compr ret = ", ret);
#endif
		}
		counter++;
712
		put_fl_mem(jNode);
W
wdenk 已提交
713 714 715
	}

#if 0
716
	putLabeledWord("read_inode: returning = ", totalSize);
W
wdenk 已提交
717
#endif
718
	return totalSize;
W
wdenk 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
}

/* find the inode from the slashless name given a parent */
static u32
jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
{
	struct b_node *b;
	struct jffs2_raw_dirent *jDir;
	int len;
	u32 counter;
	u32 version = 0;
	u32 inode = 0;

	/* name is assumed slash free */
	len = strlen(name);

	counter = 0;
	/* we need to search all and return the inode with the highest version */
737
	for(b = pL->dir.listHead; b; b = b->next, counter++) {
738
		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
739 740
		if ((pino == jDir->pino) && (len == jDir->nsize) &&
		    (jDir->ino) &&	/* 0 for unlink */
W
Wolfgang Denk 已提交
741
		    (!strncmp((char *)jDir->name, name, len))) {	/* a match */
742 743
			if (jDir->version < version) {
				put_fl_mem(jDir);
W
wdenk 已提交
744
				continue;
745
			}
W
wdenk 已提交
746

W
wdenk 已提交
747 748
			if (jDir->version == version && inode != 0) {
			    	/* I'm pretty sure this isn't legal */
W
wdenk 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
				putstr(" ** ERROR ** ");
				putnstr(jDir->name, jDir->nsize);
				putLabeledWord(" has dup version =", version);
			}
			inode = jDir->ino;
			version = jDir->version;
		}
#if 0
		putstr("\r\nfind_inode:p&l ->");
		putnstr(jDir->name, jDir->nsize);
		putstr("\r\n");
		putLabeledWord("pino = ", jDir->pino);
		putLabeledWord("nsize = ", jDir->nsize);
		putLabeledWord("b = ", (u32) b);
		putLabeledWord("counter = ", counter);
#endif
765
		put_fl_mem(jDir);
W
wdenk 已提交
766 767 768 769
	}
	return inode;
}

770
char *mkmodestr(unsigned long mode, char *str)
W
wdenk 已提交
771
{
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	static const char *l = "xwr";
	int mask = 1, i;
	char c;

	switch (mode & S_IFMT) {
		case S_IFDIR:    str[0] = 'd'; break;
		case S_IFBLK:    str[0] = 'b'; break;
		case S_IFCHR:    str[0] = 'c'; break;
		case S_IFIFO:    str[0] = 'f'; break;
		case S_IFLNK:    str[0] = 'l'; break;
		case S_IFSOCK:   str[0] = 's'; break;
		case S_IFREG:    str[0] = '-'; break;
		default:         str[0] = '?';
	}

	for(i = 0; i < 9; i++) {
		c = l[i%3];
		str[9-i] = (mode & mask)?c:'-';
		mask = mask<<1;
	}

	if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
	if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
	if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
	str[10] = '\0';
	return str;
W
wdenk 已提交
798 799 800 801
}

static inline void dump_stat(struct stat *st, const char *name)
{
802 803
	char str[20];
	char s[64], *p;
W
wdenk 已提交
804

805 806
	if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
		st->st_mtime = 1;
W
wdenk 已提交
807

W
Wolfgang Denk 已提交
808
	ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
W
wdenk 已提交
809

810 811
	if ((p = strchr(s,'\n')) != NULL) *p = '\0';
	if ((p = strchr(s,'\r')) != NULL) *p = '\0';
W
wdenk 已提交
812 813

/*
814 815
	printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
		st->st_size, s, name);
W
wdenk 已提交
816 817
*/

818
	printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
W
wdenk 已提交
819 820 821 822 823 824 825 826 827
}

static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
{
	char fname[256];
	struct stat st;

	if(!d || !i) return -1;

W
Wolfgang Denk 已提交
828
	strncpy(fname, (char *)d->name, d->nsize);
829
	fname[d->nsize] = '\0';
W
wdenk 已提交
830 831 832

	memset(&st,0,sizeof(st));

833 834 835
	st.st_mtime = i->mtime;
	st.st_mode = i->mode;
	st.st_ino = i->ino;
W
wdenk 已提交
836 837

	/* neither dsize nor isize help us.. do it the long way */
838
	st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
W
wdenk 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

	dump_stat(&st, fname);

	if (d->type == DT_LNK) {
		unsigned char *src = (unsigned char *) (&i[1]);
	        putstr(" -> ");
		putnstr(src, (int)i->dsize);
	}

	putstr("\r\n");

	return 0;
}

/* list inodes with the given pino */
static u32
jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
{
	struct b_node *b;
	struct jffs2_raw_dirent *jDir;

860
	for (b = pL->dir.listHead; b; b = b->next) {
861
		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
862 863
		if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
			u32 i_version = 0;
864
			struct jffs2_raw_inode ojNode;
865 866
			struct jffs2_raw_inode *jNode, *i = NULL;
			struct b_node *b2 = pL->frag.listHead;
W
wdenk 已提交
867 868

			while (b2) {
869 870
				jNode = (struct jffs2_raw_inode *)
					get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
871 872
				if (jNode->ino == jDir->ino && jNode->version >= i_version) {
					if (i)
W
wdenk 已提交
873
						put_fl_mem(i);
874 875 876 877 878

					if (jDir->type == DT_LNK)
						i = get_node_mem(b2->offset);
					else
						i = get_fl_mem(b2->offset, sizeof(*i), NULL);
879
				}
W
wdenk 已提交
880 881 882 883
				b2 = b2->next;
			}

			dump_inode(pL, jDir, i);
884
			put_fl_mem(i);
W
wdenk 已提交
885
		}
886
		put_fl_mem(jDir);
W
wdenk 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	}
	return pino;
}

static u32
jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
{
	int i;
	char tmp[256];
	char working_tmp[256];
	char *c;

	/* discard any leading slash */
	i = 0;
	while (fname[i] == '/')
		i++;
	strcpy(tmp, &fname[i]);

	while ((c = (char *) strchr(tmp, '/')))	/* we are still dired searching */
	{
		strncpy(working_tmp, tmp, c - tmp);
		working_tmp[c - tmp] = '\0';
#if 0
		putstr("search_inode: tmp = ");
		putstr(tmp);
		putstr("\r\n");
		putstr("search_inode: wtmp = ");
		putstr(working_tmp);
		putstr("\r\n");
		putstr("search_inode: c = ");
		putstr(c);
		putstr("\r\n");
#endif
		for (i = 0; i < strlen(c) - 1; i++)
			tmp[i] = c[i + 1];
		tmp[i] = '\0';
#if 0
		putstr("search_inode: post tmp = ");
		putstr(tmp);
		putstr("\r\n");
#endif

		if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
			putstr("find_inode failed for name=");
			putstr(working_tmp);
			putstr("\r\n");
			return 0;
		}
	}
	/* this is for the bare filename, directories have already been mapped */
	if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
		putstr("find_inode failed for name=");
		putstr(tmp);
		putstr("\r\n");
		return 0;
	}
	return pino;

}

static u32
jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
{
	struct b_node *b;
	struct b_node *b2;
	struct jffs2_raw_dirent *jDir;
	struct jffs2_raw_inode *jNode;
954 955 956
	u8 jDirFoundType = 0;
	u32 jDirFoundIno = 0;
	u32 jDirFoundPino = 0;
W
wdenk 已提交
957 958 959 960 961 962
	char tmp[256];
	u32 version = 0;
	u32 pino;
	unsigned char *src;

	/* we need to search all and return the inode with the highest version */
963
	for(b = pL->dir.listHead; b; b = b->next) {
964
		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
W
wdenk 已提交
965
		if (ino == jDir->ino) {
966 967
		    	if (jDir->version < version) {
				put_fl_mem(jDir);
W
wdenk 已提交
968
				continue;
969
			}
W
wdenk 已提交
970

971
			if (jDir->version == version && jDirFoundType) {
W
wdenk 已提交
972
			    	/* I'm pretty sure this isn't legal */
W
wdenk 已提交
973 974 975 976 977 978
				putstr(" ** ERROR ** ");
				putnstr(jDir->name, jDir->nsize);
				putLabeledWord(" has dup version (resolve) = ",
					version);
			}

979 980 981
			jDirFoundType = jDir->type;
			jDirFoundIno = jDir->ino;
			jDirFoundPino = jDir->pino;
W
wdenk 已提交
982 983
			version = jDir->version;
		}
984
		put_fl_mem(jDir);
W
wdenk 已提交
985 986
	}
	/* now we found the right entry again. (shoulda returned inode*) */
987 988
	if (jDirFoundType != DT_LNK)
		return jDirFoundIno;
989 990 991

	/* it's a soft link so we follow it again. */
	b2 = pL->frag.listHead;
W
wdenk 已提交
992
	while (b2) {
993 994
		jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset);
		if (jNode->ino == jDirFoundIno) {
W
wdenk 已提交
995
			src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
W
wdenk 已提交
996 997 998 999 1000 1001 1002

#if 0
			putLabeledWord("\t\t dsize = ", jNode->dsize);
			putstr("\t\t target = ");
			putnstr(src, jNode->dsize);
			putstr("\r\n");
#endif
W
Wolfgang Denk 已提交
1003
			strncpy(tmp, (char *)src, jNode->dsize);
W
wdenk 已提交
1004
			tmp[jNode->dsize] = '\0';
1005
			put_fl_mem(jNode);
W
wdenk 已提交
1006 1007 1008
			break;
		}
		b2 = b2->next;
1009
		put_fl_mem(jNode);
W
wdenk 已提交
1010 1011 1012 1013 1014 1015
	}
	/* ok so the name of the new file to find is in tmp */
	/* if it starts with a slash it is root based else shared dirs */
	if (tmp[0] == '/')
		pino = 1;
	else
1016
		pino = jDirFoundPino;
W
wdenk 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

	return jffs2_1pass_search_inode(pL, tmp, pino);
}

static u32
jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
{
	int i;
	char tmp[256];
	char working_tmp[256];
	char *c;

	/* discard any leading slash */
	i = 0;
	while (fname[i] == '/')
		i++;
	strcpy(tmp, &fname[i]);
	working_tmp[0] = '\0';
	while ((c = (char *) strchr(tmp, '/')))	/* we are still dired searching */
	{
		strncpy(working_tmp, tmp, c - tmp);
		working_tmp[c - tmp] = '\0';
		for (i = 0; i < strlen(c) - 1; i++)
			tmp[i] = c[i + 1];
		tmp[i] = '\0';
		/* only a failure if we arent looking at top level */
1043 1044
		if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
		    (working_tmp[0])) {
W
wdenk 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
			putstr("find_inode failed for name=");
			putstr(working_tmp);
			putstr("\r\n");
			return 0;
		}
	}

	if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
		putstr("find_inode failed for name=");
		putstr(tmp);
		putstr("\r\n");
		return 0;
	}
	/* this is for the bare filename, directories have already been mapped */
	if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
		putstr("find_inode failed for name=");
		putstr(tmp);
		putstr("\r\n");
		return 0;
	}
	return pino;

}

unsigned char
jffs2_1pass_rescan_needed(struct part_info *part)
{
	struct b_node *b;
1073
	struct jffs2_unknown_node onode;
W
wdenk 已提交
1074
	struct jffs2_unknown_node *node;
1075
	struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
W
wdenk 已提交
1076 1077 1078 1079 1080

	if (part->jffs2_priv == 0){
		DEBUGF ("rescan: First time in use\n");
		return 1;
	}
1081

W
wdenk 已提交
1082
	/* if we have no list, we need to rescan */
1083
	if (pL->frag.listCount == 0) {
W
wdenk 已提交
1084 1085 1086 1087
		DEBUGF ("rescan: fraglist zero\n");
		return 1;
	}

1088 1089
	/* but suppose someone reflashed a partition at the same offset... */
	b = pL->dir.listHead;
W
wdenk 已提交
1090
	while (b) {
1091 1092
		node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
			sizeof(onode), &onode);
W
wdenk 已提交
1093
		if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
1094 1095
			DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
					(unsigned long) b->offset);
W
wdenk 已提交
1096 1097 1098 1099 1100 1101 1102
			return 1;
		}
		b = b->next;
	}
	return 0;
}

1103 1104 1105 1106 1107
#ifdef DEBUG_FRAGMENTS
static void
dump_fragments(struct b_lists *pL)
{
	struct b_node *b;
1108
	struct jffs2_raw_inode ojNode;
1109 1110 1111 1112 1113
	struct jffs2_raw_inode *jNode;

	putstr("\r\n\r\n******The fragment Entries******\r\n");
	b = pL->frag.listHead;
	while (b) {
1114 1115
		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
			sizeof(ojNode), &ojNode);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
		putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
		putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
		putLabeledWord("\tbuild_list: inode = ", jNode->ino);
		putLabeledWord("\tbuild_list: version = ", jNode->version);
		putLabeledWord("\tbuild_list: isize = ", jNode->isize);
		putLabeledWord("\tbuild_list: atime = ", jNode->atime);
		putLabeledWord("\tbuild_list: offset = ", jNode->offset);
		putLabeledWord("\tbuild_list: csize = ", jNode->csize);
		putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
		putLabeledWord("\tbuild_list: compr = ", jNode->compr);
		putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
		putLabeledWord("\tbuild_list: flags = ", jNode->flags);
W
wdenk 已提交
1128
		putLabeledWord("\tbuild_list: offset = ", b->offset);	/* FIXME: ? [RS] */
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
		b = b->next;
	}
}
#endif

#ifdef DEBUG_DIRENTS
static void
dump_dirents(struct b_lists *pL)
{
	struct b_node *b;
	struct jffs2_raw_dirent *jDir;

	putstr("\r\n\r\n******The directory Entries******\r\n");
	b = pL->dir.listHead;
	while (b) {
1144
		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
		putstr("\r\n");
		putnstr(jDir->name, jDir->nsize);
		putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
		putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
		putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
		putLabeledWord("\tbuild_list: pino = ", jDir->pino);
		putLabeledWord("\tbuild_list: version = ", jDir->version);
		putLabeledWord("\tbuild_list: ino = ", jDir->ino);
		putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
		putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
		putLabeledWord("\tbuild_list: type = ", jDir->type);
		putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
		putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
W
wdenk 已提交
1158
		putLabeledWord("\tbuild_list: offset = ", b->offset); 	/* FIXME: ? [RS] */
1159
		b = b->next;
1160
		put_fl_mem(jDir);
1161 1162 1163 1164
	}
}
#endif

W
wdenk 已提交
1165 1166 1167 1168 1169
static u32
jffs2_1pass_build_lists(struct part_info * part)
{
	struct b_lists *pL;
	struct jffs2_unknown_node *node;
1170
	u32 offset, oldoffset = 0;
W
wdenk 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	u32 max = part->size - sizeof(struct jffs2_raw_inode);
	u32 counter = 0;
	u32 counter4 = 0;
	u32 counterF = 0;
	u32 counterN = 0;

	/* turn off the lcd.  Refreshing the lcd adds 50% overhead to the */
	/* jffs2 list building enterprise nope.  in newer versions the overhead is */
	/* only about 5 %.  not enough to inconvenience people for. */
	/* lcd_off(); */

	/* if we are building a list we need to refresh the cache. */
	jffs_init_1pass_list(part);
1184
	pL = (struct b_lists *)part->jffs2_priv;
W
wdenk 已提交
1185
	offset = 0;
1186
	puts ("Scanning JFFS2 FS:   ");
W
wdenk 已提交
1187 1188 1189

	/* start at the beginning of the partition */
	while (offset < max) {
1190 1191 1192 1193
	    	if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
			printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
			oldoffset = offset;
		}
1194

1195
		node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset);
1196
		if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
W
wdenk 已提交
1197
			/* if its a fragment add it */
1198
			if (node->nodetype == JFFS2_NODETYPE_INODE &&
1199 1200
				    inode_crc((struct jffs2_raw_inode *) node) &&
				    data_crc((struct jffs2_raw_inode *) node)) {
1201
				if (insert_node(&pL->frag, (u32) part->offset +
1202 1203
						offset) == NULL) {
					put_fl_mem(node);
W
wdenk 已提交
1204
					return 0;
1205
				}
W
wdenk 已提交
1206 1207 1208
			} else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
				   dirent_crc((struct jffs2_raw_dirent *) node)  &&
				   dirent_name_crc((struct jffs2_raw_dirent *) node)) {
1209
				if (! (counterN%100))
1210
					puts ("\b\b.  ");
1211
				if (insert_node(&pL->dir, (u32) part->offset +
1212 1213
						offset) == NULL) {
					put_fl_mem(node);
W
wdenk 已提交
1214
					return 0;
1215
				}
W
wdenk 已提交
1216 1217 1218
				counterN++;
			} else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
				if (node->totlen != sizeof(struct jffs2_unknown_node))
1219 1220 1221
					printf("OOPS Cleanmarker has bad size "
						"%d != %d\n", node->totlen,
						sizeof(struct jffs2_unknown_node));
W
wdenk 已提交
1222 1223 1224 1225 1226
			} else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
				if (node->totlen < sizeof(struct jffs2_unknown_node))
					printf("OOPS Padding has bad size "
						"%d < %d\n", node->totlen,
						sizeof(struct jffs2_unknown_node));
W
wdenk 已提交
1227
			} else {
1228 1229 1230
				printf("Unknown node type: %x len %d "
					"offset 0x%x\n", node->nodetype,
					node->totlen, offset);
W
wdenk 已提交
1231 1232 1233
			}
			offset += ((node->totlen + 3) & ~3);
			counterF++;
1234 1235
		} else if (node->magic == JFFS2_EMPTY_BITMASK &&
			   node->nodetype == JFFS2_EMPTY_BITMASK) {
W
wdenk 已提交
1236
			offset = jffs2_scan_empty(offset, part);
1237
		} else {	/* if we know nothing, we just step and look. */
W
wdenk 已提交
1238 1239 1240 1241
			offset += 4;
			counter4++;
		}
/*             printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
1242
		put_fl_mem(node);
W
wdenk 已提交
1243 1244 1245 1246 1247 1248 1249
	}

	putstr("\b\b done.\r\n");		/* close off the dots */
	/* turn the lcd back on. */
	/* splash(); */

#if 0
1250 1251
	putLabeledWord("dir entries = ", pL->dir.listCount);
	putLabeledWord("frag entries = ", pL->frag.listCount);
W
wdenk 已提交
1252 1253 1254 1255 1256
	putLabeledWord("+4 increments = ", counter4);
	putLabeledWord("+file_offset increments = ", counterF);

#endif

1257 1258 1259
#ifdef DEBUG_DIRENTS
	dump_dirents(pL);
#endif
W
wdenk 已提交
1260

1261 1262 1263
#ifdef DEBUG_FRAGMENTS
	dump_fragments(pL);
#endif
W
wdenk 已提交
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	/* give visual feedback that we are done scanning the flash */
	led_blink(0x0, 0x0, 0x1, 0x1);	/* off, forever, on 100ms, off 100ms */
	return 1;
}


static u32
jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
{
	struct b_node *b;
1275
	struct jffs2_raw_inode ojNode;
W
wdenk 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284
	struct jffs2_raw_inode *jNode;
	int i;

	for (i = 0; i < JFFS2_NUM_COMPR; i++) {
		piL->compr_info[i].num_frags = 0;
		piL->compr_info[i].compr_sum = 0;
		piL->compr_info[i].decompr_sum = 0;
	}

1285
	b = pL->frag.listHead;
W
wdenk 已提交
1286
	while (b) {
1287 1288
		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
			sizeof(ojNode), &ojNode);
W
wdenk 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
		if (jNode->compr < JFFS2_NUM_COMPR) {
			piL->compr_info[jNode->compr].num_frags++;
			piL->compr_info[jNode->compr].compr_sum += jNode->csize;
			piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
		}
		b = b->next;
	}
	return 0;
}


static struct b_lists *
jffs2_get_list(struct part_info * part, const char *who)
{
1303 1304 1305
	/* copy requested part_info struct pointer to global location */
	current_part = part;

W
wdenk 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	if (jffs2_1pass_rescan_needed(part)) {
		if (!jffs2_1pass_build_lists(part)) {
			printf("%s: Failed to scan JFFSv2 file structure\n", who);
			return NULL;
		}
	}
	return (struct b_lists *)part->jffs2_priv;
}


/* Print directory / file contents */
u32
jffs2_1pass_ls(struct part_info * part, const char *fname)
{
	struct b_lists *pl;
1321
	long ret = 1;
W
wdenk 已提交
1322 1323
	u32 inode;

1324
	if (! (pl = jffs2_get_list(part, "ls")))
W
wdenk 已提交
1325 1326 1327 1328 1329 1330
		return 0;

	if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
		putstr("ls: Failed to scan jffs2 file structure\r\n");
		return 0;
	}
1331 1332


W
wdenk 已提交
1333 1334 1335 1336
#if 0
	putLabeledWord("found file at inode = ", inode);
	putLabeledWord("read_inode returns = ", ret);
#endif
1337 1338

	return ret;
W
wdenk 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347
}


/* Load a file from flash into memory. fname can be a full path */
u32
jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
{

	struct b_lists *pl;
1348
	long ret = 1;
W
wdenk 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	u32 inode;

	if (! (pl  = jffs2_get_list(part, "load")))
		return 0;

	if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
		putstr("load: Failed to find inode\r\n");
		return 0;
	}

	/* Resolve symlinks */
	if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
		putstr("load: Failed to resolve inode structure\r\n");
		return 0;
	}

	if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
		putstr("load: Failed to read inode\r\n");
		return 0;
	}

	DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
				(unsigned long) dest, ret);
	return ret;
}

/* Return information about the fs on this partition */
u32
jffs2_1pass_info(struct part_info * part)
{
	struct b_jffs2_info info;
	struct b_lists *pl;
	int i;

	if (! (pl  = jffs2_get_list(part, "info")))
		return 0;

	jffs2_1pass_fill_info(pl, &info);
1387
	for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1388 1389 1390 1391 1392 1393 1394 1395
		printf ("Compression: %s\n"
			"\tfrag count: %d\n"
			"\tcompressed sum: %d\n"
			"\tuncompressed sum: %d\n",
			compr_names[i],
			info.compr_info[i].num_frags,
			info.compr_info[i].compr_sum,
			info.compr_info[i].decompr_sum);
W
wdenk 已提交
1396 1397 1398 1399 1400
	}
	return 1;
}

#endif /* CFG_CMD_JFFS2 */