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

📄 filesystem.h

📁 支持nvram盘
💻 H
字号:
/*
 * The FLASH or RAM filesystem
 */
/*
 * The flash file system organizes the equal sized sectors on your flash into
 * the default of 4096 byte blocks. Every block has a block number.  Blocks differ
 * in their sequence numbers, version numbers and checksums.  When bytes are
 * added to a file, a new version of the last block of the file is written. The
 * previous version of that block is now considered to be a free block.
 *
 * All blocks in a file have contiguious sequence numbers.  The block numbers are
 * allowed to wrap around the maximum value of the sequence number.  This makes it
 * possible to have the sequence number of the first block greater than the sequence
 * number of the last block.
 *
 * On system initialization all of the blocks are checked for bad checksums and if
 * there is more than a 1-bit error and a previous version of the block is present,
 * this indicates that a write operation was interrupted by a system reset or crash.
 * In this case the bad block is marked as free and the previous version of the block
 * is used in the file.  Any corrupted blocks are repaired on system initialization.
 *
 * Each block has an eighteen byte header that is organized as follows:
 * typedef struct {
 *		byte flag;
 *		byte file;
 *		word first_byte;
 *		word last_byte;
 *		unsigned long seq;
 *		byte ver;
 *		unsigned long wear;
 *		byte reserved;
 *		word crc;
 * } FSHeader;
 * flag - In a block that has been erased, all bits are set to one.  But, erased blocks
 * have a transitory lifetime and usually are not present when a search for a free block
 * is being done.  A value of 0xff in flag indicates a system crash during or after a
 * block erase.  Calling fs_init will cause flag to be set to FS_FREE for that block,
 * making it free for allocation.  When a block has been allocated, the flag is set to
 * FS_USED.
 *
 * file - This number is an identifier for the file.  File number 0, and numbers 128-255
 * are reserved for Z-World抯 future use.
 *
 * first_byte and last_byte - This is the offset from the start of the block where data
 * begins and ends.  The first_byte will allow developers to add functions that allow
 * deletion of data from the beginning of a file.  This is currently not implemented.
 * The last_byte is a pointer to the last valid byte in a sector.  If last_byte is set
 * beyond the end of a block the block is completely full.
 *
 * seq - Each successive block has a sequence number one higher than the previous block.
 * However, the first block of the file can have any sequence number.  Wrapping the
 * sequence number is valid.
 *
 * ver - Blocks with obsolete version numbers and the sequence and file number are
 * considered free blocks.  Only two versions of a block are ever present at one time
 * in normal operation.  When a new version is written the old version is marked free.
 *
 * wear - For the purpose of wear leveling, wear holds the number of times a block has
 * been written to.  Every time the bock is erased,  wear plus one is written to the block.
 *
 * crc - This is a sixteen byte ones complement checksum.
/*** BeginHeader */

typedef byte FileNumber; /* filename (number, 0->255) */
typedef unsigned long FSSeq; /* sequence number */

typedef struct {
	FileNumber name;
	int mode;

	FSSeq first_sequence;
	int first_block;

	FSSeq current_sequence;	/* current sequence number */
	int current_block;	/* block current_sequence refrences */
	long position;		/* current position in file */
	int current_offset;	/* offset into current_block that position points to */

	int num_blocks;		/* number of blocks in file */
} File;		/* file handler */

/* used by the flash writer */
typedef struct {
	long	address;
	int	length;
} FSWriteBlock;

//for block table
typedef struct {
	FileNumber name;
	byte used;
	int next_block;
} FSBlockLink;
#define FS_BLOCKLINK sizeof(FSBlockLink)

/* Load the low-level drivers */
#ifdef FS_FLASH
	#use "fs_flash.lib"
#else
	#ifdef FS_RAM
		#use "fs_ram.lib"
	#else
		#ifdef FS_FLASH_SINGLE
			#use "fs_flash_single.lib"
		#else
			#error "fs: must define FS_FLASH or FS_RAM to load a driver!"
		#endif
	#endif
#endif

/* 'whence' values for fseek() */
#define SEEK_SET	0
#define SEEK_CUR	1
#define SEEK_END	2

/* block flags */
#define FS_ERASED	0xff
#define FS_FREE		1
#define FS_USED		2
#define FS_BAD			3

/* read/write modes */
#define FS_NOTOPEN	0
#define FS_READ		1
#define FS_WRITE	2

/* reserved block number */
#define FS_NOBLOCK	-1

/* current file system version */
#define FS_VERSION 0xfe

/* fsck options */
#define FSCK_HEADERS		0x0001
#define FSCK_CHECKSUMS	0x0002
#define FSCK_VERSION		0x0004

/* where the fs is */
long fs_offset;
int fs_num_blocks;
//tables in xmem
unsigned long fs_blocklist;
unsigned long fs_filelist;
//track blocks used
int fs_blocks_used;
int fs_nonpriv_blocks_used;
int fs_blocks_reserved;	//blocks kept free for priveleged files
//sector buffer in xmem
unsigned long fs_sectorbuf;

typedef struct {
	byte fs_version;
	byte flag;
	byte file;
	word first_byte;
	word last_byte;
	unsigned long seq;
	byte ver;
	unsigned long wear;
	word crc;
} FSHeader;
#define FS_HEADER sizeof(FSHeader)

⌨️ 快捷键说明

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