xref: /openbmc/u-boot/fs/jffs2/jffs2_nand_1pass.c (revision 344c8376)
1 #include <common.h>
2 
3 #include <malloc.h>
4 #include <linux/stat.h>
5 #include <linux/time.h>
6 
7 #include <jffs2/jffs2.h>
8 #include <jffs2/jffs2_1pass.h>
9 #include <nand.h>
10 
11 #include "jffs2_nand_private.h"
12 
13 #define	NODE_CHUNK	1024	/* size of memory allocation chunk in b_nodes */
14 
15 /* Debugging switches */
16 #undef	DEBUG_DIRENTS		/* print directory entry list after scan */
17 #undef	DEBUG_FRAGMENTS		/* print fragment list after scan */
18 #undef	DEBUG			/* enable debugging messages */
19 
20 #ifdef  DEBUG
21 # define DEBUGF(fmt,args...)	printf(fmt ,##args)
22 #else
23 # define DEBUGF(fmt,args...)
24 #endif
25 
26 static nand_info_t *nand;
27 
28 /* Compression names */
29 static char *compr_names[] = {
30 	"NONE",
31 	"ZERO",
32 	"RTIME",
33 	"RUBINMIPS",
34 	"COPY",
35 	"DYNRUBIN",
36 	"ZLIB",
37 #if defined(CONFIG_JFFS2_LZO)
38 	"LZO",
39 #endif
40 };
41 
42 /* Spinning wheel */
43 static char spinner[] = { '|', '/', '-', '\\' };
44 
45 /* Memory management */
46 struct mem_block {
47 	unsigned index;
48 	struct mem_block *next;
49 	char nodes[0];
50 };
51 
52 static void
53 free_nodes(struct b_list *list)
54 {
55 	while (list->listMemBase != NULL) {
56 		struct mem_block *next = list->listMemBase->next;
57 		free(list->listMemBase);
58 		list->listMemBase = next;
59 	}
60 }
61 
62 static struct b_node *
63 add_node(struct b_list *list, int size)
64 {
65 	u32 index = 0;
66 	struct mem_block *memBase;
67 	struct b_node *b;
68 
69 	memBase = list->listMemBase;
70 	if (memBase != NULL)
71 		index = memBase->index;
72 
73 	if (memBase == NULL || index >= NODE_CHUNK) {
74 		/* we need more space before we continue */
75 		memBase = mmalloc(sizeof(struct mem_block) + NODE_CHUNK * size);
76 		if (memBase == NULL) {
77 			putstr("add_node: malloc failed\n");
78 			return NULL;
79 		}
80 		memBase->next = list->listMemBase;
81 		index = 0;
82 	}
83 	/* now we have room to add it. */
84 	b = (struct b_node *)&memBase->nodes[size * index];
85 	index ++;
86 
87 	memBase->index = index;
88 	list->listMemBase = memBase;
89 	list->listCount++;
90 	return b;
91 }
92 
93 static struct b_node *
94 insert_node(struct b_list *list, struct b_node *new)
95 {
96 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
97 	struct b_node *b, *prev;
98 
99 	if (list->listTail != NULL && list->listCompare(new, list->listTail))
100 		prev = list->listTail;
101 	else if (list->listLast != NULL && list->listCompare(new, list->listLast))
102 		prev = list->listLast;
103 	else
104 		prev = NULL;
105 
106 	for (b = (prev ? prev->next : list->listHead);
107 	     b != NULL && list->listCompare(new, b);
108 	     prev = b, b = b->next) {
109 		list->listLoops++;
110 	}
111 	if (b != NULL)
112 		list->listLast = prev;
113 
114 	if (b != NULL) {
115 		new->next = b;
116 		if (prev != NULL)
117 			prev->next = new;
118 		else
119 			list->listHead = new;
120 	} else
121 #endif
122 	{
123 		new->next = (struct b_node *) NULL;
124 		if (list->listTail != NULL) {
125 			list->listTail->next = new;
126 			list->listTail = new;
127 		} else {
128 			list->listTail = list->listHead = new;
129 		}
130 	}
131 
132 	return new;
133 }
134 
135 static struct b_node *
136 insert_inode(struct b_list *list, struct jffs2_raw_inode *node, u32 offset)
137 {
138 	struct b_inode *new;
139 
140 	if (!(new = (struct b_inode *)add_node(list, sizeof(struct b_inode)))) {
141 		putstr("add_node failed!\r\n");
142 		return NULL;
143 	}
144 	new->offset = offset;
145 	new->version = node->version;
146 	new->ino = node->ino;
147 	new->isize = node->isize;
148 	new->csize = node->csize;
149 
150 	return insert_node(list, (struct b_node *)new);
151 }
152 
153 static struct b_node *
154 insert_dirent(struct b_list *list, struct jffs2_raw_dirent *node, u32 offset)
155 {
156 	struct b_dirent *new;
157 
158 	if (!(new = (struct b_dirent *)add_node(list, sizeof(struct b_dirent)))) {
159 		putstr("add_node failed!\r\n");
160 		return NULL;
161 	}
162 	new->offset = offset;
163 	new->version = node->version;
164 	new->pino = node->pino;
165 	new->ino = node->ino;
166 	new->nhash = full_name_hash(node->name, node->nsize);
167 	new->nsize = node->nsize;
168 	new->type = node->type;
169 
170 	return insert_node(list, (struct b_node *)new);
171 }
172 
173 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
174 /* Sort data entries with the latest version last, so that if there
175  * is overlapping data the latest version will be used.
176  */
177 static int compare_inodes(struct b_node *new, struct b_node *old)
178 {
179 	struct jffs2_raw_inode ojNew;
180 	struct jffs2_raw_inode ojOld;
181 	struct jffs2_raw_inode *jNew =
182 		(struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
183 	struct jffs2_raw_inode *jOld =
184 		(struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
185 
186 	return jNew->version > jOld->version;
187 }
188 
189 /* Sort directory entries so all entries in the same directory
190  * with the same name are grouped together, with the latest version
191  * last. This makes it easy to eliminate all but the latest version
192  * by marking the previous version dead by setting the inode to 0.
193  */
194 static int compare_dirents(struct b_node *new, struct b_node *old)
195 {
196 	struct jffs2_raw_dirent ojNew;
197 	struct jffs2_raw_dirent ojOld;
198 	struct jffs2_raw_dirent *jNew =
199 		(struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
200 	struct jffs2_raw_dirent *jOld =
201 		(struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
202 	int cmp;
203 
204 	/* ascending sort by pino */
205 	if (jNew->pino != jOld->pino)
206 		return jNew->pino > jOld->pino;
207 
208 	/* pino is the same, so use ascending sort by nsize, so
209 	 * we don't do strncmp unless we really must.
210 	 */
211 	if (jNew->nsize != jOld->nsize)
212 		return jNew->nsize > jOld->nsize;
213 
214 	/* length is also the same, so use ascending sort by name
215 	 */
216 	cmp = strncmp(jNew->name, jOld->name, jNew->nsize);
217 	if (cmp != 0)
218 		return cmp > 0;
219 
220 	/* we have duplicate names in this directory, so use ascending
221 	 * sort by version
222 	 */
223 	if (jNew->version > jOld->version) {
224 		/* since jNew is newer, we know jOld is not valid, so
225 		 * mark it with inode 0 and it will not be used
226 		 */
227 		jOld->ino = 0;
228 		return 1;
229 	}
230 
231 	return 0;
232 }
233 #endif
234 
235 static u32
236 jffs_init_1pass_list(struct part_info *part)
237 {
238 	struct b_lists *pL;
239 
240 	if (part->jffs2_priv != NULL) {
241 		pL = (struct b_lists *)part->jffs2_priv;
242 		free_nodes(&pL->frag);
243 		free_nodes(&pL->dir);
244 		free(pL);
245 	}
246 	if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
247 		pL = (struct b_lists *)part->jffs2_priv;
248 
249 		memset(pL, 0, sizeof(*pL));
250 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
251 		pL->dir.listCompare = compare_dirents;
252 		pL->frag.listCompare = compare_inodes;
253 #endif
254 	}
255 	return 0;
256 }
257 
258 /* find the inode from the slashless name given a parent */
259 static long
260 jffs2_1pass_read_inode(struct b_lists *pL, u32 ino, char *dest,
261 		       struct stat *stat)
262 {
263 	struct b_inode *jNode;
264 	u32 totalSize = 0;
265 	u32 latestVersion = 0;
266 	long ret;
267 
268 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
269 	/* Find file size before loading any data, so fragments that
270 	 * start past the end of file can be ignored. A fragment
271 	 * that is partially in the file is loaded, so extra data may
272 	 * be loaded up to the next 4K boundary above the file size.
273 	 * This shouldn't cause trouble when loading kernel images, so
274 	 * we will live with it.
275 	 */
276 	for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
277 		if ((ino == jNode->ino)) {
278 			/* get actual file length from the newest node */
279 			if (jNode->version >= latestVersion) {
280 				totalSize = jNode->isize;
281 				latestVersion = jNode->version;
282 			}
283 		}
284 	}
285 #endif
286 
287 	for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
288 		if ((ino != jNode->ino))
289 			continue;
290 #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
291 		/* get actual file length from the newest node */
292 		if (jNode->version >= latestVersion) {
293 			totalSize = jNode->isize;
294 			latestVersion = jNode->version;
295 		}
296 #endif
297 		if (dest || stat) {
298 			char *src, *dst;
299 			char data[4096 + sizeof(struct jffs2_raw_inode)];
300 			struct jffs2_raw_inode *inode;
301 			size_t len;
302 
303 			inode = (struct jffs2_raw_inode *)&data;
304 			len = sizeof(struct jffs2_raw_inode);
305 			if (dest)
306 				len += jNode->csize;
307 			nand_read(nand, jNode->offset, &len, inode);
308 			/* ignore data behind latest known EOF */
309 			if (inode->offset > totalSize)
310 				continue;
311 
312 			if (stat) {
313 				stat->st_mtime = inode->mtime;
314 				stat->st_mode = inode->mode;
315 				stat->st_ino = inode->ino;
316 				stat->st_size = totalSize;
317 			}
318 
319 			if (!dest)
320 				continue;
321 
322 			src = ((char *) inode) + sizeof(struct jffs2_raw_inode);
323 			dst = (char *) (dest + inode->offset);
324 
325 			switch (inode->compr) {
326 			case JFFS2_COMPR_NONE:
327 				ret = 0;
328 				memcpy(dst, src, inode->dsize);
329 				break;
330 			case JFFS2_COMPR_ZERO:
331 				ret = 0;
332 				memset(dst, 0, inode->dsize);
333 				break;
334 			case JFFS2_COMPR_RTIME:
335 				ret = 0;
336 				rtime_decompress(src, dst, inode->csize, inode->dsize);
337 				break;
338 			case JFFS2_COMPR_DYNRUBIN:
339 				/* this is slow but it works */
340 				ret = 0;
341 				dynrubin_decompress(src, dst, inode->csize, inode->dsize);
342 				break;
343 			case JFFS2_COMPR_ZLIB:
344 				ret = zlib_decompress(src, dst, inode->csize, inode->dsize);
345 				break;
346 #if defined(CONFIG_JFFS2_LZO)
347 			case JFFS2_COMPR_LZO:
348 				ret = lzo_decompress(src, dst, inode->csize, inode->dsize);
349 				break;
350 #endif
351 			default:
352 				/* unknown */
353 				putLabeledWord("UNKNOWN COMPRESSION METHOD = ", inode->compr);
354 				return -1;
355 			}
356 		}
357 	}
358 
359 	return totalSize;
360 }
361 
362 /* find the inode from the slashless name given a parent */
363 static u32
364 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
365 {
366 	struct b_dirent *jDir;
367 	int len = strlen(name);	/* name is assumed slash free */
368 	unsigned int nhash = full_name_hash(name, len);
369 	u32 version = 0;
370 	u32 inode = 0;
371 
372 	/* we need to search all and return the inode with the highest version */
373 	for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
374 		if ((pino == jDir->pino) && (jDir->ino) &&	/* 0 for unlink */
375 		    (len == jDir->nsize) && (nhash == jDir->nhash)) {
376 			/* TODO: compare name */
377 			if (jDir->version < version)
378 				continue;
379 
380 			if (jDir->version == version && inode != 0) {
381 				/* I'm pretty sure this isn't legal */
382 				putstr(" ** ERROR ** ");
383 /*				putnstr(jDir->name, jDir->nsize); */
384 /*				putLabeledWord(" has dup version =", version); */
385 			}
386 			inode = jDir->ino;
387 			version = jDir->version;
388 		}
389 	}
390 	return inode;
391 }
392 
393 char *mkmodestr(unsigned long mode, char *str)
394 {
395 	static const char *l = "xwr";
396 	int mask = 1, i;
397 	char c;
398 
399 	switch (mode & S_IFMT) {
400 		case S_IFDIR:    str[0] = 'd'; break;
401 		case S_IFBLK:    str[0] = 'b'; break;
402 		case S_IFCHR:    str[0] = 'c'; break;
403 		case S_IFIFO:    str[0] = 'f'; break;
404 		case S_IFLNK:    str[0] = 'l'; break;
405 		case S_IFSOCK:   str[0] = 's'; break;
406 		case S_IFREG:    str[0] = '-'; break;
407 		default:         str[0] = '?';
408 	}
409 
410 	for(i = 0; i < 9; i++) {
411 		c = l[i%3];
412 		str[9-i] = (mode & mask)?c:'-';
413 		mask = mask<<1;
414 	}
415 
416 	if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
417 	if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
418 	if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
419 	str[10] = '\0';
420 	return str;
421 }
422 
423 static inline void dump_stat(struct stat *st, const char *name)
424 {
425 	char str[20];
426 	char s[64], *p;
427 
428 	if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
429 		st->st_mtime = 1;
430 
431 	ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
432 
433 	if ((p = strchr(s,'\n')) != NULL) *p = '\0';
434 	if ((p = strchr(s,'\r')) != NULL) *p = '\0';
435 
436 /*
437 	printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
438 		st->st_size, s, name);
439 */
440 
441 	printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
442 }
443 
444 static inline int
445 dump_inode(struct b_lists *pL, struct b_dirent *d, struct b_inode *i)
446 {
447 	char fname[JFFS2_MAX_NAME_LEN + 1];
448 	struct stat st;
449 	size_t len;
450 
451 	if(!d || !i) return -1;
452 	len = d->nsize;
453 	nand_read(nand, d->offset + sizeof(struct jffs2_raw_dirent),
454 		  &len, &fname);
455 	fname[d->nsize] = '\0';
456 
457 	memset(&st, 0, sizeof(st));
458 
459 	jffs2_1pass_read_inode(pL, i->ino, NULL, &st);
460 
461 	dump_stat(&st, fname);
462 /* FIXME
463 	if (d->type == DT_LNK) {
464 		unsigned char *src = (unsigned char *) (&i[1]);
465 	        putstr(" -> ");
466 		putnstr(src, (int)i->dsize);
467 	}
468 */
469 	putstr("\r\n");
470 
471 	return 0;
472 }
473 
474 /* list inodes with the given pino */
475 static u32
476 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
477 {
478 	struct b_dirent *jDir;
479 	u32 i_version = 0;
480 
481 	for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
482 		if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
483 			struct b_inode *jNode = (struct b_inode *)pL->frag.listHead;
484 			struct b_inode *i = NULL;
485 
486 			while (jNode) {
487 				if (jNode->ino == jDir->ino && jNode->version >= i_version) {
488 					i_version = jNode->version;
489 					i = jNode;
490 				}
491 				jNode = jNode->next;
492 			}
493 			dump_inode(pL, jDir, i);
494 		}
495 	}
496 	return pino;
497 }
498 
499 static u32
500 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
501 {
502 	int i;
503 	char tmp[256];
504 	char working_tmp[256];
505 	char *c;
506 
507 	/* discard any leading slash */
508 	i = 0;
509 	while (fname[i] == '/')
510 		i++;
511 	strcpy(tmp, &fname[i]);
512 
513 	while ((c = (char *) strchr(tmp, '/')))	/* we are still dired searching */
514 	{
515 		strncpy(working_tmp, tmp, c - tmp);
516 		working_tmp[c - tmp] = '\0';
517 #if 0
518 		putstr("search_inode: tmp = ");
519 		putstr(tmp);
520 		putstr("\r\n");
521 		putstr("search_inode: wtmp = ");
522 		putstr(working_tmp);
523 		putstr("\r\n");
524 		putstr("search_inode: c = ");
525 		putstr(c);
526 		putstr("\r\n");
527 #endif
528 		for (i = 0; i < strlen(c) - 1; i++)
529 			tmp[i] = c[i + 1];
530 		tmp[i] = '\0';
531 #if 0
532 		putstr("search_inode: post tmp = ");
533 		putstr(tmp);
534 		putstr("\r\n");
535 #endif
536 
537 		if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
538 			putstr("find_inode failed for name=");
539 			putstr(working_tmp);
540 			putstr("\r\n");
541 			return 0;
542 		}
543 	}
544 	/* this is for the bare filename, directories have already been mapped */
545 	if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
546 		putstr("find_inode failed for name=");
547 		putstr(tmp);
548 		putstr("\r\n");
549 		return 0;
550 	}
551 	return pino;
552 
553 }
554 
555 static u32
556 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
557 {
558 	struct b_dirent *jDir;
559 	struct b_inode *jNode;
560 	u8 jDirFoundType = 0;
561 	u32 jDirFoundIno = 0;
562 	u32 jDirFoundPino = 0;
563 	char tmp[JFFS2_MAX_NAME_LEN + 1];
564 	u32 version = 0;
565 	u32 pino;
566 
567 	/* we need to search all and return the inode with the highest version */
568 	for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {
569 		if (ino == jDir->ino) {
570 			if (jDir->version < version)
571 				continue;
572 
573 			if (jDir->version == version && jDirFoundType) {
574 				/* I'm pretty sure this isn't legal */
575 				putstr(" ** ERROR ** ");
576 /*				putnstr(jDir->name, jDir->nsize); */
577 /*				putLabeledWord(" has dup version (resolve) = ", */
578 /*					version); */
579 			}
580 
581 			jDirFoundType = jDir->type;
582 			jDirFoundIno = jDir->ino;
583 			jDirFoundPino = jDir->pino;
584 			version = jDir->version;
585 		}
586 	}
587 	/* now we found the right entry again. (shoulda returned inode*) */
588 	if (jDirFoundType != DT_LNK)
589 		return jDirFoundIno;
590 
591 	/* it's a soft link so we follow it again. */
592 	for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {
593 		if (jNode->ino == jDirFoundIno) {
594 			size_t len = jNode->csize;
595 			nand_read(nand, jNode->offset + sizeof(struct jffs2_raw_inode), &len, &tmp);
596 			tmp[jNode->csize] = '\0';
597 			break;
598 		}
599 	}
600 	/* ok so the name of the new file to find is in tmp */
601 	/* if it starts with a slash it is root based else shared dirs */
602 	if (tmp[0] == '/')
603 		pino = 1;
604 	else
605 		pino = jDirFoundPino;
606 
607 	return jffs2_1pass_search_inode(pL, tmp, pino);
608 }
609 
610 static u32
611 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
612 {
613 	int i;
614 	char tmp[256];
615 	char working_tmp[256];
616 	char *c;
617 
618 	/* discard any leading slash */
619 	i = 0;
620 	while (fname[i] == '/')
621 		i++;
622 	strcpy(tmp, &fname[i]);
623 	working_tmp[0] = '\0';
624 	while ((c = (char *) strchr(tmp, '/')))	/* we are still dired searching */
625 	{
626 		strncpy(working_tmp, tmp, c - tmp);
627 		working_tmp[c - tmp] = '\0';
628 		for (i = 0; i < strlen(c) - 1; i++)
629 			tmp[i] = c[i + 1];
630 		tmp[i] = '\0';
631 		/* only a failure if we arent looking at top level */
632 		if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
633 		    (working_tmp[0])) {
634 			putstr("find_inode failed for name=");
635 			putstr(working_tmp);
636 			putstr("\r\n");
637 			return 0;
638 		}
639 	}
640 
641 	if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
642 		putstr("find_inode failed for name=");
643 		putstr(tmp);
644 		putstr("\r\n");
645 		return 0;
646 	}
647 	/* this is for the bare filename, directories have already been mapped */
648 	if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
649 		putstr("find_inode failed for name=");
650 		putstr(tmp);
651 		putstr("\r\n");
652 		return 0;
653 	}
654 	return pino;
655 
656 }
657 
658 unsigned char
659 jffs2_1pass_rescan_needed(struct part_info *part)
660 {
661 	struct b_node *b;
662 	struct jffs2_unknown_node onode;
663 	struct jffs2_unknown_node *node;
664 	struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
665 
666 	if (part->jffs2_priv == 0){
667 		DEBUGF ("rescan: First time in use\n");
668 		return 1;
669 	}
670 	/* if we have no list, we need to rescan */
671 	if (pL->frag.listCount == 0) {
672 		DEBUGF ("rescan: fraglist zero\n");
673 		return 1;
674 	}
675 
676 	/* or if we are scanning a new partition */
677 	if (pL->partOffset != part->offset) {
678 		DEBUGF ("rescan: different partition\n");
679 		return 1;
680 	}
681 
682 	/* FIXME */
683 #if 0
684 	/* but suppose someone reflashed a partition at the same offset... */
685 	b = pL->dir.listHead;
686 	while (b) {
687 		node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
688 			sizeof(onode), &onode);
689 		if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
690 			DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
691 					(unsigned long) b->offset);
692 			return 1;
693 		}
694 		b = b->next;
695 	}
696 #endif
697 	return 0;
698 }
699 
700 #ifdef DEBUG_FRAGMENTS
701 static void
702 dump_fragments(struct b_lists *pL)
703 {
704 	struct b_node *b;
705 	struct jffs2_raw_inode ojNode;
706 	struct jffs2_raw_inode *jNode;
707 
708 	putstr("\r\n\r\n******The fragment Entries******\r\n");
709 	b = pL->frag.listHead;
710 	while (b) {
711 		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
712 			sizeof(ojNode), &ojNode);
713 		putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
714 		putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
715 		putLabeledWord("\tbuild_list: inode = ", jNode->ino);
716 		putLabeledWord("\tbuild_list: version = ", jNode->version);
717 		putLabeledWord("\tbuild_list: isize = ", jNode->isize);
718 		putLabeledWord("\tbuild_list: atime = ", jNode->atime);
719 		putLabeledWord("\tbuild_list: offset = ", jNode->offset);
720 		putLabeledWord("\tbuild_list: csize = ", jNode->csize);
721 		putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
722 		putLabeledWord("\tbuild_list: compr = ", jNode->compr);
723 		putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
724 		putLabeledWord("\tbuild_list: flags = ", jNode->flags);
725 		putLabeledWord("\tbuild_list: offset = ", b->offset);	/* FIXME: ? [RS] */
726 		b = b->next;
727 	}
728 }
729 #endif
730 
731 #ifdef DEBUG_DIRENTS
732 static void
733 dump_dirents(struct b_lists *pL)
734 {
735 	struct b_node *b;
736 	struct jffs2_raw_dirent *jDir;
737 
738 	putstr("\r\n\r\n******The directory Entries******\r\n");
739 	b = pL->dir.listHead;
740 	while (b) {
741 		jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
742 		putstr("\r\n");
743 		putnstr(jDir->name, jDir->nsize);
744 		putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
745 		putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
746 		putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
747 		putLabeledWord("\tbuild_list: pino = ", jDir->pino);
748 		putLabeledWord("\tbuild_list: version = ", jDir->version);
749 		putLabeledWord("\tbuild_list: ino = ", jDir->ino);
750 		putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
751 		putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
752 		putLabeledWord("\tbuild_list: type = ", jDir->type);
753 		putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
754 		putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
755 		putLabeledWord("\tbuild_list: offset = ", b->offset);	/* FIXME: ? [RS] */
756 		b = b->next;
757 		put_fl_mem(jDir);
758 	}
759 }
760 #endif
761 
762 static int
763 jffs2_fill_scan_buf(nand_info_t *nand, unsigned char *buf,
764 		    unsigned ofs, unsigned len)
765 {
766 	int ret;
767 	unsigned olen;
768 
769 	olen = len;
770 	ret = nand_read(nand, ofs, &olen, buf);
771 	if (ret) {
772 		printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret);
773 		return ret;
774 	}
775 	if (olen < len) {
776 		printf("Read at 0x%x gave only 0x%x bytes\n", ofs, olen);
777 		return -1;
778 	}
779 	return 0;
780 }
781 
782 #define	EMPTY_SCAN_SIZE	1024
783 static u32
784 jffs2_1pass_build_lists(struct part_info * part)
785 {
786 	struct b_lists *pL;
787 	struct jffs2_unknown_node *node;
788 	unsigned nr_blocks, sectorsize, ofs, offset;
789 	char *buf;
790 	int i;
791 	u32 counter = 0;
792 	u32 counter4 = 0;
793 	u32 counterF = 0;
794 	u32 counterN = 0;
795 
796 	struct mtdids *id = part->dev->id;
797 	nand = nand_info + id->num;
798 
799 	/* if we are building a list we need to refresh the cache. */
800 	jffs_init_1pass_list(part);
801 	pL = (struct b_lists *)part->jffs2_priv;
802 	pL->partOffset = part->offset;
803 	puts ("Scanning JFFS2 FS:   ");
804 
805 	sectorsize = nand->erasesize;
806 	nr_blocks = part->size / sectorsize;
807 	buf = malloc(sectorsize);
808 	if (!buf)
809 		return 0;
810 
811 	for (i = 0; i < nr_blocks; i++) {
812 		printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
813 
814 		offset = part->offset + i * sectorsize;
815 
816 		if (nand_block_isbad(nand, offset))
817 			continue;
818 
819 		if (jffs2_fill_scan_buf(nand, buf, offset, EMPTY_SCAN_SIZE))
820 			return 0;
821 
822 		ofs = 0;
823 		/* Scan only 4KiB of 0xFF before declaring it's empty */
824 		while (ofs < EMPTY_SCAN_SIZE && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
825 			ofs += 4;
826 		if (ofs == EMPTY_SCAN_SIZE)
827 			continue;
828 
829 		if (jffs2_fill_scan_buf(nand, buf + EMPTY_SCAN_SIZE, offset + EMPTY_SCAN_SIZE, sectorsize - EMPTY_SCAN_SIZE))
830 			return 0;
831 		offset += ofs;
832 
833 		while (ofs < sectorsize - sizeof(struct jffs2_unknown_node)) {
834 			node = (struct jffs2_unknown_node *)&buf[ofs];
835 			if (node->magic != JFFS2_MAGIC_BITMASK || !hdr_crc(node)) {
836 				offset += 4;
837 				ofs += 4;
838 				counter4++;
839 				continue;
840 			}
841 			/* if its a fragment add it */
842 			if (node->nodetype == JFFS2_NODETYPE_INODE &&
843 				    inode_crc((struct jffs2_raw_inode *) node)) {
844 				if (insert_inode(&pL->frag, (struct jffs2_raw_inode *) node,
845 						 offset) == NULL) {
846 					return 0;
847 				}
848 			} else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
849 				   dirent_crc((struct jffs2_raw_dirent *) node)  &&
850 				   dirent_name_crc((struct jffs2_raw_dirent *) node)) {
851 				if (! (counterN%100))
852 					puts ("\b\b.  ");
853 				if (insert_dirent(&pL->dir, (struct jffs2_raw_dirent *) node,
854 						  offset) == NULL) {
855 					return 0;
856 				}
857 				counterN++;
858 			} else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
859 				if (node->totlen != sizeof(struct jffs2_unknown_node))
860 					printf("OOPS Cleanmarker has bad size "
861 						"%d != %zu\n",
862 						node->totlen,
863 						sizeof(struct jffs2_unknown_node));
864 			} else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
865 				if (node->totlen < sizeof(struct jffs2_unknown_node))
866 					printf("OOPS Padding has bad size "
867 						"%d < %zu\n",
868 						node->totlen,
869 						sizeof(struct jffs2_unknown_node));
870 			} else {
871 				printf("Unknown node type: %x len %d offset 0x%x\n",
872 					node->nodetype,
873 					node->totlen, offset);
874 			}
875 			offset += ((node->totlen + 3) & ~3);
876 			ofs += ((node->totlen + 3) & ~3);
877 			counterF++;
878 		}
879 	}
880 
881 	putstr("\b\b done.\r\n");		/* close off the dots */
882 
883 #if 0
884 	putLabeledWord("dir entries = ", pL->dir.listCount);
885 	putLabeledWord("frag entries = ", pL->frag.listCount);
886 	putLabeledWord("+4 increments = ", counter4);
887 	putLabeledWord("+file_offset increments = ", counterF);
888 #endif
889 
890 #ifdef DEBUG_DIRENTS
891 	dump_dirents(pL);
892 #endif
893 
894 #ifdef DEBUG_FRAGMENTS
895 	dump_fragments(pL);
896 #endif
897 
898 	/* give visual feedback that we are done scanning the flash */
899 	led_blink(0x0, 0x0, 0x1, 0x1);	/* off, forever, on 100ms, off 100ms */
900 	free(buf);
901 
902 	return 1;
903 }
904 
905 
906 static u32
907 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
908 {
909 	struct b_node *b;
910 	struct jffs2_raw_inode ojNode;
911 	struct jffs2_raw_inode *jNode;
912 	int i;
913 
914 	for (i = 0; i < JFFS2_NUM_COMPR; i++) {
915 		piL->compr_info[i].num_frags = 0;
916 		piL->compr_info[i].compr_sum = 0;
917 		piL->compr_info[i].decompr_sum = 0;
918 	}
919 /*	FIXME
920 	b = pL->frag.listHead;
921 	while (b) {
922 		jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
923 			sizeof(ojNode), &ojNode);
924 		if (jNode->compr < JFFS2_NUM_COMPR) {
925 			piL->compr_info[jNode->compr].num_frags++;
926 			piL->compr_info[jNode->compr].compr_sum += jNode->csize;
927 			piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
928 		}
929 		b = b->next;
930 	}
931 */
932 	return 0;
933 }
934 
935 
936 static struct b_lists *
937 jffs2_get_list(struct part_info * part, const char *who)
938 {
939 	if (jffs2_1pass_rescan_needed(part)) {
940 		if (!jffs2_1pass_build_lists(part)) {
941 			printf("%s: Failed to scan JFFSv2 file structure\n", who);
942 			return NULL;
943 		}
944 	}
945 	return (struct b_lists *)part->jffs2_priv;
946 }
947 
948 
949 /* Print directory / file contents */
950 u32
951 jffs2_1pass_ls(struct part_info * part, const char *fname)
952 {
953 	struct b_lists *pl;
954 	long ret = 0;
955 	u32 inode;
956 
957 	if (! (pl = jffs2_get_list(part, "ls")))
958 		return 0;
959 
960 	if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
961 		putstr("ls: Failed to scan jffs2 file structure\r\n");
962 		return 0;
963 	}
964 
965 #if 0
966 	putLabeledWord("found file at inode = ", inode);
967 	putLabeledWord("read_inode returns = ", ret);
968 #endif
969 
970 	return ret;
971 }
972 
973 
974 /* Load a file from flash into memory. fname can be a full path */
975 u32
976 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
977 {
978 
979 	struct b_lists *pl;
980 	long ret = 0;
981 	u32 inode;
982 
983 	if (! (pl = jffs2_get_list(part, "load")))
984 		return 0;
985 
986 	if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
987 		putstr("load: Failed to find inode\r\n");
988 		return 0;
989 	}
990 
991 	/* Resolve symlinks */
992 	if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
993 		putstr("load: Failed to resolve inode structure\r\n");
994 		return 0;
995 	}
996 
997 	if ((ret = jffs2_1pass_read_inode(pl, inode, dest, NULL)) < 0) {
998 		putstr("load: Failed to read inode\r\n");
999 		return 0;
1000 	}
1001 
1002 	DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1003 				(unsigned long) dest, ret);
1004 	return ret;
1005 }
1006 
1007 /* Return information about the fs on this partition */
1008 u32
1009 jffs2_1pass_info(struct part_info * part)
1010 {
1011 	struct b_jffs2_info info;
1012 	struct b_lists *pl;
1013 	int i;
1014 
1015 	if (! (pl = jffs2_get_list(part, "info")))
1016 		return 0;
1017 
1018 	jffs2_1pass_fill_info(pl, &info);
1019 	for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1020 		printf ("Compression: %s\n"
1021 			"\tfrag count: %d\n"
1022 			"\tcompressed sum: %d\n"
1023 			"\tuncompressed sum: %d\n",
1024 			compr_names[i],
1025 			info.compr_info[i].num_frags,
1026 			info.compr_info[i].compr_sum,
1027 			info.compr_info[i].decompr_sum);
1028 	}
1029 	return 1;
1030 }
1031