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

📄 jffs2_1pass.c

📁 OMAP2530 uboot source code
💻 C
📖 第 1 页 / 共 3 页
字号:
/*------------------------------------------------------------------------- * Filename:      jffs2.c * Version:       $Id: jffs2_1pass.c,v 1.1.1.1 2007/04/03 06:37:44 kanagesh 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.1.1.1 2007/04/03 06:37:44 kanagesh 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 ;). * *//* * 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 * might be incompatible with any new code from the jffs2 maintainers anyway, * it should probably be dumped and replaced by something like jffs2reader! */#include <common.h>#include <config.h>#include <malloc.h>#include <linux/stat.h>#include <linux/time.h>#if (CONFIG_COMMANDS & CFG_CMD_JFFS2)#include <jffs2/jffs2.h>#include <jffs2/jffs2_1pass.h>#include "jffs2_private.h"#define	NODE_CHUNK  	1024	/* size of memory allocation chunk in b_nodes */#define	SPIN_BLKSIZE	18  	/* spin after having scanned 1<<BLKSIZE bytes *//* 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...)#endif/* keeps pointer to currentlu processed partition */static struct part_info *current_part;#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)#if defined(CFG_NAND_LEGACY)#include <linux/mtd/nand_legacy.h>#else#include <nand.h>#endif/* * 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. * */#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/* info for NAND chips, defined in drivers/nand/nand.c */extern nand_info_t nand_info[];#endif#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){	struct mtdids *id = current_part->dev->id;	u32 bytes_read = 0;#if defined(CFG_NAND_LEGACY)	size_t retlen;#else	ulong retlen;#endif	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 */				nand_cache = malloc(NAND_CACHE_SIZE);				if (!nand_cache) {					printf("read_nand_cached: can't alloc cache size %d bytes\n",					       NAND_CACHE_SIZE);					return -1;				}			}#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			retlen = NAND_CACHE_SIZE;			if (nand_read(&nand_info[id->num], nand_cache_off,						&retlen, nand_cache) != 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;			}#endif		}		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;}static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf){	u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);	if (NULL == buf) {		printf("get_fl_mem_nand: can't alloc %d bytes\n", size);		return NULL;	}	if (read_nand_cached(off, size, buf) < 0) {		if (!ext_buf)			free(buf);		return NULL;	}	return buf;}static void *get_node_mem_nand(u32 off){	struct jffs2_unknown_node node;	void *ret = NULL;	if (NULL == get_fl_mem_nand(off, sizeof(node), &node))		return NULL;	if (!(ret = get_fl_mem_nand(off, node.magic ==			       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;}static void put_fl_mem_nand(void *buf){	free(buf);}#endif /* #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */#if (CONFIG_COMMANDS & CFG_CMD_FLASH)/* * 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;	extern flash_info_t flash_info[];	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);}#endif /* #if (CONFIG_COMMANDS & CFG_CMD_FLASH) *//* * Generic jffs2 raw memory and node read routines. * */static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf){	struct mtdids *id = current_part->dev->id;#if (CONFIG_COMMANDS & CFG_CMD_FLASH)	if (id->type == MTD_DEV_TYPE_NOR)		return get_fl_mem_nor(off);#endif#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)	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");	return (void*)off;}static inline void *get_node_mem(u32 off){	struct mtdids *id = current_part->dev->id;#if (CONFIG_COMMANDS & CFG_CMD_FLASH)	if (id->type == MTD_DEV_TYPE_NOR)		return get_node_mem_nor(off);#endif#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)	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");	return (void*)off;}static inline void put_fl_mem(void *buf){#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)	struct mtdids *id = current_part->dev->id;	if (id->type == MTD_DEV_TYPE_NAND)		return put_fl_mem_nand(buf);#endif}/* 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 {	u32	index;	struct mem_block *next;	struct b_node nodes[NODE_CHUNK];};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){	u32 index = 0;	struct mem_block *memBase;	struct b_node *b;	memBase = list->listMemBase;	if (memBase != NULL)		index = memBase->index;#if 0	putLabeledWord("add_node: index = ", index);	putLabeledWord("add_node: memBase = ", list->listMemBase);#endif	if (memBase == NULL || index >= NODE_CHUNK) {		/* we need more space before we continue */		memBase = mmalloc(sizeof(struct mem_block));		if (memBase == NULL) {			putstr("add_node: malloc failed\n");			return NULL;		}		memBase->next = list->listMemBase;		index = 0;#if 0		putLabeledWord("add_node: alloced a new membase at ", *memBase);#endif	}	/* now we have room to add it. */	b = &memBase->nodes[index];	index ++;	memBase->index = index;	list->listMemBase = memBase;	list->listCount++;	return b;}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;#endif	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;

⌨️ 快捷键说明

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