⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jffs2_nand_1pass.c

📁 OMAP2530 uboot source code
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <common.h>#if !defined(CFG_NAND_LEGACY) && (CONFIG_COMMANDS & CFG_CMD_JFFS2)#include <malloc.h>#include <linux/stat.h>#include <linux/time.h>#include <jffs2/jffs2.h>#include <jffs2/jffs2_1pass.h>#include <nand.h>#include "jffs2_nand_private.h"#define	NODE_CHUNK	1024	/* size of memory allocation chunk in b_nodes *//* Debugging switches */#undef	DEBUG_DIRENTS		/* print directory entry list after scan */#undef	DEBUG_FRAGMENTS		/* print fragment list after scan */#undef	DEBUG			/* enable debugging messages */#ifdef  DEBUG# define DEBUGF(fmt,args...)	printf(fmt ,##args)#else# define DEBUGF(fmt,args...)#endifstatic nand_info_t *nand;/* Compression names */static char *compr_names[] = {	"NONE",	"ZERO",	"RTIME",	"RUBINMIPS",	"COPY",	"DYNRUBIN",	"ZLIB",#if defined(CONFIG_JFFS2_LZO_LZARI)	"LZO",	"LZARI",#endif};/* Spinning wheel */static char spinner[] = { '|', '/', '-', '\\' };/* Memory management */struct mem_block {	unsigned index;	struct mem_block *next;	char nodes[0];};static voidfree_nodes(struct b_list *list){	while (list->listMemBase != NULL) {		struct mem_block *next = list->listMemBase->next;		free(list->listMemBase);		list->listMemBase = next;	}}static struct b_node *add_node(struct b_list *list, int size){	u32 index = 0;	struct mem_block *memBase;	struct b_node *b;	memBase = list->listMemBase;	if (memBase != NULL)		index = memBase->index;	if (memBase == NULL || index >= NODE_CHUNK) {		/* we need more space before we continue */		memBase = mmalloc(sizeof(struct mem_block) + NODE_CHUNK * size);		if (memBase == NULL) {			putstr("add_node: malloc failed\n");			return NULL;		}		memBase->next = list->listMemBase;		index = 0;	}	/* now we have room to add it. */	b = (struct b_node *)&memBase->nodes[size * index];	index ++;	memBase->index = index;	list->listMemBase = memBase;	list->listCount++;	return b;}static struct b_node *insert_node(struct b_list *list, struct b_node *new){#ifdef CFG_JFFS2_SORT_FRAGMENTS	struct b_node *b, *prev;	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#endif	{		new->next = (struct b_node *) NULL;		if (list->listTail != NULL) {			list->listTail->next = new;			list->listTail = new;		} else {			list->listTail = list->listHead = new;		}	}	return new;}static struct b_node *insert_inode(struct b_list *list, struct jffs2_raw_inode *node, u32 offset){	struct b_inode *new;	if (!(new = (struct b_inode *)add_node(list, sizeof(struct b_inode)))) {		putstr("add_node failed!\r\n");		return NULL;	}	new->offset = offset;	new->version = node->version;	new->ino = node->ino;	new->isize = node->isize;	new->csize = node->csize;	return insert_node(list, (struct b_node *)new);}static struct b_node *insert_dirent(struct b_list *list, struct jffs2_raw_dirent *node, u32 offset){	struct b_dirent *new;	if (!(new = (struct b_dirent *)add_node(list, sizeof(struct b_dirent)))) {		putstr("add_node failed!\r\n");		return NULL;	}	new->offset = offset;	new->version = node->version;	new->pino = node->pino;	new->ino = node->ino;	new->nhash = full_name_hash(node->name, node->nsize);	new->nsize = node->nsize;	new->type = node->type;	return insert_node(list, (struct b_node *)new);}#ifdef CFG_JFFS2_SORT_FRAGMENTS/* Sort data entries with the latest version last, so that if there * is overlapping data the latest version will be used. */static int compare_inodes(struct b_node *new, struct b_node *old){	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);	return jNew->version > jOld->version;}/* 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. */static int compare_dirents(struct b_node *new, struct b_node *old){	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);	struct jffs2_raw_dirent *jOld =		(struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);	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	 */	cmp = strncmp(jNew->name, jOld->name, jNew->nsize);	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;	}	return 0;}#endifstatic u32jffs_init_1pass_list(struct part_info *part){	struct b_lists *pL;	if (part->jffs2_priv != NULL) {		pL = (struct b_lists *)part->jffs2_priv;		free_nodes(&pL->frag);		free_nodes(&pL->dir);		free(pL);	}	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	}	return 0;}/* find the inode from the slashless name given a parent */static longjffs2_1pass_read_inode(struct b_lists *pL, u32 ino, char *dest,		       struct stat *stat){	struct b_inode *jNode;	u32 totalSize = 0;	u32 latestVersion = 0;	long ret;#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 (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {		if ((ino == jNode->ino)) {			/* get actual file length from the newest node */			if (jNode->version >= latestVersion) {				totalSize = jNode->isize;				latestVersion = jNode->version;			}		}	}#endif	for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) {		if ((ino != jNode->ino))			continue;#ifndef CFG_JFFS2_SORT_FRAGMENTS		/* get actual file length from the newest node */		if (jNode->version >= latestVersion) {			totalSize = jNode->isize;			latestVersion = jNode->version;		}#endif		if (dest || stat) {			char *src, *dst;			char data[4096 + sizeof(struct jffs2_raw_inode)];			struct jffs2_raw_inode *inode;			size_t len;			inode = (struct jffs2_raw_inode *)&data;			len = sizeof(struct jffs2_raw_inode);			if (dest)				len += jNode->csize;			nand_read(nand, jNode->offset, &len, inode);			/* ignore data behind latest known EOF */			if (inode->offset > totalSize)				continue;			if (stat) {				stat->st_mtime = inode->mtime;				stat->st_mode = inode->mode;				stat->st_ino = inode->ino;				stat->st_size = totalSize;			}			if (!dest)				continue;			src = ((char *) inode) + sizeof(struct jffs2_raw_inode);			dst = (char *) (dest + inode->offset);			switch (inode->compr) {			case JFFS2_COMPR_NONE:				ret = 0;				memcpy(dst, src, inode->dsize);				break;			case JFFS2_COMPR_ZERO:				ret = 0;				memset(dst, 0, inode->dsize);				break;			case JFFS2_COMPR_RTIME:				ret = 0;				rtime_decompress(src, dst, inode->csize, inode->dsize);				break;			case JFFS2_COMPR_DYNRUBIN:				/* this is slow but it works */				ret = 0;				dynrubin_decompress(src, dst, inode->csize, inode->dsize);				break;			case JFFS2_COMPR_ZLIB:				ret = zlib_decompress(src, dst, inode->csize, inode->dsize);				break;#if defined(CONFIG_JFFS2_LZO_LZARI)			case JFFS2_COMPR_LZO:				ret = lzo_decompress(src, dst, inode->csize, inode->dsize);				break;			case JFFS2_COMPR_LZARI:				ret = lzari_decompress(src, dst, inode->csize, inode->dsize);				break;#endif			default:				/* unknown */				putLabeledWord("UNKOWN COMPRESSION METHOD = ", inode->compr);				return -1;			}		}	}	return totalSize;}/* find the inode from the slashless name given a parent */static u32jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino){	struct b_dirent *jDir;	int len = strlen(name);	/* name is assumed slash free */	unsigned int nhash = full_name_hash(name, len);	u32 version = 0;	u32 inode = 0;	/* we need to search all and return the inode with the highest version */	for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {		if ((pino == jDir->pino) && (jDir->ino) &&	/* 0 for unlink */		    (len == jDir->nsize) && (nhash == jDir->nhash)) {			/* TODO: compare name */			if (jDir->version < version)				continue;			if (jDir->version == version && inode != 0) {				/* I'm pretty sure this isn't legal */				putstr(" ** ERROR ** ");/*				putnstr(jDir->name, jDir->nsize); *//*				putLabeledWord(" has dup version =", version); */			}			inode = jDir->ino;			version = jDir->version;		}	}	return inode;}char *mkmodestr(unsigned long mode, char *str){	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;}static inline void dump_stat(struct stat *st, const char *name){	char str[20];	char s[64], *p;	if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */		st->st_mtime = 1;	ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */	if ((p = strchr(s,'\n')) != NULL) *p = '\0';	if ((p = strchr(s,'\r')) != NULL) *p = '\0';/*	printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),		st->st_size, s, name);*/	printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);}static inline intdump_inode(struct b_lists *pL, struct b_dirent *d, struct b_inode *i){	char fname[JFFS2_MAX_NAME_LEN + 1];	struct stat st;	size_t len;	if(!d || !i) return -1;	len = d->nsize;	nand_read(nand, d->offset + sizeof(struct jffs2_raw_dirent),		  &len, &fname);	fname[d->nsize] = '\0';	memset(&st, 0, sizeof(st));	jffs2_1pass_read_inode(pL, i->ino, NULL, &st);	dump_stat(&st, fname);/* FIXME	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 u32jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino){	struct b_dirent *jDir;	u32 i_version = 0;	for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) {		if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */			struct b_inode *jNode = (struct b_inode *)pL->frag.listHead;			struct b_inode *i = NULL;			while (jNode) {				if (jNode->ino == jDir->ino && jNode->version >= i_version) {					i_version = jNode->version;					i = jNode;				}				jNode = jNode->next;			}			dump_inode(pL, jDir, i);		}	}	return pino;}static u32jffs2_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 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -