/*** BeginHeader */
#ifndef __FS2_LIB
#define __FS2_LIB
/*** EndHeader */
/*
* fs2.lib
*
* Copyright (C) Z-World, Inc. All rights reserved.
*
* Filesystem Mk II
*
* Testing note:
* The symbol __TESTENV__ should only be defined in the
* test harness environment.
*
*/
/* START LIBRARY DESCRIPTION *********************************************
FILESYSTEM.LIB
Copyright (c) 2001, ZWorld.
DESCRIPTION:
Filesystem Mk II.
This an a (mostly) API-compatible replacement for FILESYSTEM.LIB.
This library should be used for new code.
END DESCRIPTION **********************************************************/
/*** BeginHeader _fs */
#use "errno.lib"
#ifndef __TESTENV__
#define TRACE(x)
//#define TRACE(x) do { if (_fs.trap) printf x; } while(0)
#else
// We need to rename some of the API functions so they don't
// conflict with <stdio.h>. They should be referred to as
// the fs_* version when called from __TESTENV__ test harness.
// (These are undef'd at end)
#define fclose fs_close
#define fflush fs_flush
#define fread fs_read
#define fwrite fs_write
#define fseek fs_seek
#define ftell fs_tell
extern int t_trace; // Runtime trace
#define TRACE(x) do { if (t_trace) printf x; } while(0)
#endif
#ifndef fs_nodebug
#define fs_nodebug nodebug
#endif
/*
* Default values for compile-time options
*/
#ifndef FS2_USE_PROGRAM_FLASH
#define FS2_USE_PROGRAM_FLASH 0 // Default to not using the 1st (program) flash.
#endif
#ifndef FS_MAX_DEVICES
#if (FS2_USE_PROGRAM_FLASH > 0 && XMEM_RESERVE_SIZE > 0)
#if (FS2_RAM_RESERVE > 0)
#define FS_MAX_DEVICES 3 // Maximum physical media
#else
#define FS_MAX_DEVICES 2
#endif
#else
#if (FS2_RAM_RESERVE > 0)
#define FS_MAX_DEVICES 2
#else
#define FS_MAX_DEVICES 1
#endif
#endif
#endif
#ifndef FS_MAX_LX
#define FS_MAX_LX FS_MAX_DEVICES // Maximum logical extents
#endif
#ifndef FS_MAX_FILES
#define FS_MAX_FILES 6 // Maximum files in filesystem
#endif
#ifndef FS_MAX_LS_PER_PS
#define FS_MAX_LS_PER_PS 64 // Max LS per PS of any LX.
#endif
#ifndef FS_MIN_PBUF_SIZE
// This must be a power of 2 between 64 and 8192 inclusive.
#define FS_MIN_PBUF_SIZE 256 // Min physical sector buffer size
#endif
#define FS_DEFAULT_FLASH_SHIFT 10 // Default LS size of 1K for flash
#define FS_DEFAULT_RAM_SHIFT 7 // Default LS size of 128 bytes for RAM
/*
* Basic typedefs
*/
typedef word FileNumber; // External file numbers (for API) - contains file number
// in low byte, LX number in high byte.
// Block identifier; first byte of structures stored in filesystem (*-blocks).
typedef byte FSmagic;
#define FSMAGIC_F 0xFF // Free block. This must be 0xFF to correspond to erased h/w
#define FSMAGIC_B 0x7F // B-block (header for file data per LS)
#define FSMAGIC_H 0xFE // H-block (header for file metadata per LS)
#define FSMAGIC_DEL (FSMAGIC_B & FSMAGIC_H) // A B- or H-block marked as deleted (BW devices)
#define FSMAGIC_W 0x18 // Wear-leveling unit header
#define FSMAGIC_BAD 0x00 // Bad block (do not re-use)
// File attribute bits: Reserved (not yet implemented)
typedef byte FSattrs;
#define FSATTR_BIN 0x80 // Binary (else cannot contain 0xFF chars)
#define FSATTR_NOLOG 0x40 // No automatic logging (else all updates logged)
#define FSATTR_APONLY 0x20 // Append-only (else overwritable)
// Device classes
typedef byte FSdevclass;
#define FSDC_SSW 0x01 // Small-sector writable flash
#define FSDC_BW 0x02 // Byte-writable (AND over existing data) flash
#define FSDC_NVRAM 0x03 // Battery-backed SRAM
// Types used in *-block fields
typedef long FSwear; // Wear (erase) counter
typedef word FSrwear; // Relative wear counter
typedef byte FSwlocount; // Wear-leveling operation count
typedef long FSversion; // LS rewrite counter per file/metadata. Starts at 1.
typedef word FSseq; // LS sequence number within file
typedef byte FSfilenum; // File number within LX
typedef byte FSEFnum; // Existing file index (filesystem-wide)
typedef byte FSLXnum; // Logical extent (LX) number
typedef word FSoffset; // Offset of byte (or count) within LS
typedef word FSLSnum; // Logical sector (LS) number or counter
typedef word FSPSnum; // Physical sector (PS) number or counter
typedef word FSchecksum; // Checksum value. Checksums are computed using 1's complement
// addition of 16-bit words. Where necessary, odd byte counts
// are padded with assumed zeros. The checksum is stored as its
// 1's complement, with 0xFFFF stored as 0x0000 (0xFFFF in a checksum
// field indicates the checksum was not written). Thus, if the
// checksum field itself is included in the verification, then the
// result of a valid checksum will be 0xFFFF or 0x0000.
// 'whence' parameter values for fseek().
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_RAW 3
// 'flags' bits for fsck().
#define FSCK_HEADERS 0x0001
#define FSCK_CHECKSUMS 0x0002
#define FSCK_VERSION 0x0004
// 'command' values for fs_setup().
#define FS_MODIFY_EXTENT 1
#define FS_PARTITION_FRACTION 2
#define FS_INVALID_LS ((FSLSnum)(-1)) // End of chain marker etc.
#define FS_INVALID_CHK ((FSchecksum)(-1)) // Checksum not written.
// *-block definitions. All must be even size unless noted otherwise.
// Wear-leveling unit header. Note that dummy W-blocks (used when LS size < PS size)
// are set to all 0xFF.
typedef struct {
FSmagic magic; // FSMAGIC_W
FSwlocount wlo_num; // Wear-leveling operation counter
FSrwear rwear; // Wear since last WLO
FSwear wear; // Total wear (erase or rewrite count)
} FS_w;
typedef struct {
FS_w w;
FSmagic magic;
} FS_w_aug; // W-block augmented with following block's magic ID.
typedef struct {
FSmagic magic; // FSMAGIC_B
FSfilenum filenum; // File name
FSseq seq; // Sequence within file
FSversion version; // Version within file
#ifdef __TESTENV__
short pad; // Pad to force chksum to be last bytes in struct
#endif
FSchecksum chksum; // ~Checksum of this B-block and preceding W-block (if any)
} FS_b;
typedef struct {
FSmagic magic; //
FSfilenum filenum; // These 4 fields must match definition in FS_b.
FSseq seq; //
FSversion version; //
FSLXnum lxn; // LX number of data
FSattrs attrs; // File attributes (reserved)
FSoffset first_offs; // Offset of start of data in first data LS
FSseq first_seq; // Sequence number of first data LS
FSoffset last_offs; // Offset of next free byte in last data LS
FSchecksum last_chk; // Checksum of last data LS
FSchecksum chksum; // ~Checksum of this H-block and preceding W-block (if any)
} FS_h;
typedef struct {
FSoffset new_offs; // New offset in last (append) or first (shift) LS. Only the 13 LSBs
// contain the offset, the high 3 bits contain the log entry type.
#define FS_OFFS_MASK 0x1FFF // Mask for bits in the above corresponding to offset
#define FS_LOG_DEL 0x8000 // This bit marks entry as deletion (shift) entry, otherwise append:
// Allows proper interpretation of the log_data field.
FSchecksum log_data; // New last LS checksum (append) or first data sequence number (shift).
FSchecksum log_chk; // ~Checksum of this log entry.
} FS_l;
// Structures maintained at run-time only.
// Existing file descriptor
typedef struct {
short in_use; // Flag indicating entry in use. 0 if this EF not in use.
FSfilenum name; // Corresponding file number
FSLXnum metalx; // Metadata LX number
FSLXnum datalx; // Data LX number
FSattrs attrs; // File attributes (fixed once created).
FSLSnum first_mls; // First LS in metadata LX chain
FSLSnum first_dls; // First LS in data LX chain
FSLS
评论0