📄 libfat.h
字号:
/****************************************************************
* Viaon Technology (Suzhou) Co.,Ltd
*
* Copyright 2007, Viaon Technology (Suzhou) Co.,Ltd,Suzhou,China
* All rights reserved.
*
*
* Filename: libfat.h
*
* Programmer: Greg
*
* Created: 1/2/2008
*
* Description: public functions
*
*
* Change History (most recent first): 2008.1.2
****************************************************************/
#ifndef __LIBFAT_H__
#define __LIBFAT_H__
/*
*******************************************************************************
* #include
*******************************************************************************
*/
#include "utiltypedef.h"
/*!
@defgroup CONST Constants
Define constants and the return CODE of file operation.
@{
*/
/*
*******************************************************************************
* #define CONSTANTS
*******************************************************************************
*/
///Maximum number of drive
//#define FS_MAX_DRIVE 0x10
#define FS_MAX_DRIVE 0x01
///Maximum number of file opened concurrently
//#define FS_MAX_STREAM 0x10
#define FS_MAX_STREAM 0x01
#define FS_MAX_STREAMEXP 0x00
// The maximum level of subdirectory
#define DIR_STACK_DEEP 0x08
//the number of sectors for fat cache, should be 2^Exp, Exp= 0, 1, ...
#define FAT_CACHE_SIZEEXP 0x00
#define FAT_CACHE_SIZE 0x01 //Must be 1
#define SIGNATURE_OFFSET 0x01fe // will contain 0x55AA
#define PARTITION_OFFSET 0x01be // will contain partition table
#define FSINFO_OFFSET 0x01e4
#define COMMON_SIGNATURE 0x55AA
#define FSINFO_SIGNATURE 0x72724161L
#define FAT_CACHE_CHANGED 0x01
///The number of bytes per sector
#define SECTOR_SIZE 512
//在FAT16时,每个FAT ENTRY是16bits,一个512B的Virtual SECTOR可容纳256个FAT ENTRY
//从cluster num.到对应FAT表的virtual sector(512B)偏移的计算方法:cluster >> 8
#define FAT16ENTRY_SIZEEXP 1
#define FAT16ENTRY_512B_EXP 8
//在FAT32时,每个FAT ENTRY是32bits,一个512B的Virtual SECTOR可容纳128个FAT ENTRY
//从cluster num.到对应FAT表的Vitural sector偏移的计算方法:cluster >> 7
#define FAT32ENTRY_SIZEEXP 2
#define FAT32ENTRY_512B_EXP 7
#define LFN_NODE_MAX 9
#define LFN_WORD_MAX (LFN_NODE_MAX*13)
/*!
@ingroup CONST Constants
@defgroup ERROR_CODE Error Code
Define the return CODE of file operation, the non-zero value indicate
some conditions except normal case.
@{
*/
///The API function execution without any problem
#define FS_SUCCEED 0
///Request drive not exist
#define INVALID_DRIVE 1
///No more memroy for stream
#define OUT_OF_STREAM_HANDLE 2
#define OUT_OF_SECTOR_CACHE 3
///FAT12/FAT16 root section full
#define OVER_ROOT_BOUNDARY 4
///No more space in current drive
#define DISK_FULL 5
///The moving destination is not exist
#define OUT_OF_RANGE 6
#define INVALID_DIR_LOCATION 7
#define TOO_MANY_FILES_OPENED 8
#define NOT_SUPPORT 9
///FDB node is in current directory were reached end
#define END_OF_DIR 10
///FDB node is in the 0th position of current dirctory
#define BEGIN_OF_DIR 11
///FDB node is in current directory were reached end and in end of cluster
#define END_OF_CLUS 12
///File assigned is not found
#define FILE_NOT_FOUND 13
#define FILENAME_SCAN_CONTINUE 14
///Unknow error encounter
#define ABNORMAL_STATUS 15
///Node operated is not a directory
#define NOT_DIRECTORY 16
///Current directory is root directory
#define NO_PARENT_DIR 17
// Read / write drive fail
#define DRIVE_ACCESS_FAIL 18
// driver file system scen fail
#define FS_SCAN_FAIL 19
// driver file system scen fail
#define DISK_NO_MP3 20
///@}
///@}
/*
*******************************************************************************
* GLOBAL MACROS
*******************************************************************************
*/
/* none */
/*
*******************************************************************************
* GLOBAL DATA TYPES
*******************************************************************************
*/
/*
this record will begin at middle of a long word, so some members will be separated in tow parts
*/
typedef struct // Partition Entries definition
{
BYTE Active;
BYTE res1;
BYTE res2;
BYTE res3;
BYTE Type;
BYTE res4;
BYTE res5;
BYTE res6;
UDWORD Start;
UDWORD Size;
} PARTITION;
/*
this record begin at the 11th byte of a sector, for the data alignment
some members will be separated in tow parts
*/
typedef struct // Partition Boot Record definition
{
BYTE JumpCode[3]; // 0-2
BYTE OEM[8]; // 3-10
//BYTE BytesPerSector; // 11-12typedef struct
//BYTE padding1;
WORD BytesPerSector;
BYTE SectorsPerCluster; // 13
WORD ReservedSectors; // 14-15,保留扇区数,从DBR 到FAT 的扇区数
BYTE NumberOfFATs; // 16
//For FAT12 and FAT16 volumes, this field contains the count of 32-byte directory entries in the root directory.
//For FAT32 volumes,this field must be set to 0.
BYTE RootEntries; // 17-18
BYTE padding2;
BYTE TotalSectors; // 19-20
BYTE padding3;
BYTE MediaDescritor; // 21
WORD SectorsPerFAT; // 22-23
WORD SectorsPerTrack; // 24-25
WORD Heads; // 26-27
UDWORD HiddenSectors; // 28-31
UDWORD BigTotalSectors; // 32-35
UDWORD BigSectorPerFAT; // 36-39
WORD ExtFlags; // 40-41
WORD FS_Version; // 42-43
UDWORD RootDirStartCluster; // 44-47
WORD FSInfoSector; // 48-49,FSInfoSector
WORD BackUpBootSector; // 50-51
} BOOT_SECTOR;
typedef struct
{
UDWORD Signature;
UDWORD FreeClusters;
UDWORD NextFree;
} FSINFO;
typedef struct
{
BYTE Name[8];
BYTE Extension[3];
BYTE Attribute;
WORD Attribute2;
WORD CreateTime;
WORD CreateDate;
WORD AccessDate;
WORD StartHigh;//this entry's first cluster number. High
WORD ModifyTime;
WORD ModifyDate;
WORD StartLow;//this entry's first cluster number. Low
UDWORD Size;//32-bit DWORD holding this file’s size in bytes.
} FDB;
typedef struct
{
BYTE Number;
BYTE Name0[10];
BYTE Attribute1;
BYTE Attribute2;
BYTE CheckSum;
BYTE Name1[12];
BYTE res[2];
BYTE Name2[4];
} LONG_NAME;
typedef struct
{
WORD CreateTime;
WORD CreateDate;
WORD AccessDate;
WORD Time;
WORD Date;
} FILE_TIME;
typedef struct
{
volatile UDWORD Start; // the start cluster of this chain
volatile UDWORD Current; // the current cluster the chain point located
volatile UDWORD Point; // the logical byte location counter of chain point (FILE)(0, 32, 64, ....)
volatile UDWORD Size; // the chain actual size in byte
} CHAIN;
#define CHAIN_SIZEEXP 4 //This macro is the exponent of the size of structure CHAIN
typedef struct
{
// function members definition
BYTE DevID;
// BYTE Partition;
////读取cluster号对应的FAT Entry的content
// UDWORD (*FatRead)(void, UDWORD);
// int (*FatWrite)(void * xdata drv, DWORD xdata cluster, DWORD xdata content);
// status or error report
// BYTE StatusCode;
// data members definition
struct
{
BYTE Present :1;
BYTE ReadOnly :1;
BYTE NotFormated :1;
BYTE Fat32 :1;
BYTE FatCacheChanged :1;
BYTE DirCacheChanged :1;
BYTE LongNameHit :1;
BYTE DriveLayer :1;
} Flag;
BYTE ClusterSize;//SectorsPerCluster
BYTE ClusterExp;// the exponent of sectors per cluster
BYTE NumberOfFat;
WORD DirStackPoint;
// BYTE DirAuxPoint; // used for parsing the long name
BYTE LFNCnt; // used for parsing the long name
// UDWORD FsInfoAddr;
UDWORD FatStart;
UDWORD FatSize;//SectorsPerFAT
UDWORD RootStart;//FAT32: This is set to the cluster number of the first cluster of the root director.,
UDWORD DataStart;//文件目录表FDT的LBA???
UDWORD FatCachePoint;//the cached sector's offset in the FAT
BYTE FatCacheOffset;//the cached sector's offset in the FAT
// UDWORD FreeClusters; //the number of free cluster
// UDWORD LastCluster; //the index of cluster which is just before the first free cluster
UDWORD TotalClusters;
BYTE *FatCacheBuffer;
CHAIN *DirStackBuffer;
FDB *DirCacheBuffer;
FDB *Node;//指向DirCacheBuffer中的目录项
UDWORD DirCachePoint;//DirCache保存的sector的sector number.
BYTE DirCacheOffset;
// WORD Path[128]; // the string of path of current node
/*Long File Name: Each LFN node contain 13WORD */
BYTE Name[LFN_NODE_MAX*13 + 1];//WORD Name[LFN_NODE_MAX*13 + 1]; // the string of name of current node
//WORD Path[64]; // the string of path of current node
//WORD Name[32]; // the string of name of current node
//UDWORD dwAudioTotalFiles;
//UDWORD dwImageTotalFiles;
//UDWORD dwMovieTotalFiles;
//WORD wPhyBlkSize;
//BYTE bPhyBlkSizeExp;
WORD wBytesPerSector;
BYTE bBytesPerSectorExp;
BYTE bMaxOffset;
} DRIVE;
#if 0
typedef struct
{
BYTE FatCache[SECTOR_SIZE * 4];
FDB DirCache[16];
CHAIN Stack[DIR_STACK_DEEP];
} CACHE;
#else
typedef struct
{
BYTE FatCache[SECTOR_SIZE * FAT_CACHE_SIZE];
FDB DirCache[16]; //32*16 == 512bytes == 1 sector
CHAIN Stack[DIR_STACK_DEEP];
} CACHE;
#endif
typedef struct
{
DRIVE *Drv; // drive pointer, if 0 mean this handle is not used
UDWORD DirSector; // the sector lba of DIR that contain this file
WORD FdbOffset; // the byte offset of this file entry in DIR that contain this file
void *ParDir; //TPL STREAM imcomplete type, for compiler pass
#if 0
struct
{
BYTE SizeChanged :1;
BYTE ContentChanged :1;
} Flag;
#endif
CHAIN Chain;
DWORD dwMp3Start; //unit 512b block
DWORD dw512BCnt;
} STREAM;
/*
*******************************************************************************
* GLOBAL VARIABLES
*******************************************************************************
*/
typedef struct
{
BYTE Byte[512];
} SECTOR;
typedef struct
{
BYTE Byte0;
BYTE Byte1;
BYTE Byte2;
BYTE Byte3;
} BYTE_STRC;
/*
*******************************************************************************
* GLOBAL FUNCTION PROTOTYPES
*******************************************************************************
*/
WORD LoadUnalign16(void *);
UDWORD LoadUnalign32(void *);
WORD LoadAlien16(void *);
//UDWORD LoadAlien32(void *);
void LoadAlien32(void *in, void *out);
void SaveAlien16(void *, WORD);
void SaveAlien32(void *, UDWORD);
//WORD *LoadAlienArray16(WORD *, void *, BYTE);
BYTE *LoadAlienArray16(BYTE *, void *, BYTE);
void UpperCase08(BYTE *);
void UpperCase16(WORD *);
BYTE *StringCopy08(BYTE *, BYTE *);
WORD *StringCopy16(WORD *, WORD *);
BYTE *StringCopy1608(BYTE *, WORD *);
WORD *StringCopy0816(WORD *, BYTE *);
BYTE *HexString(BYTE *, UDWORD, BYTE);
BYTE *DecString(BYTE *, UDWORD, BYTE, BYTE);
BYTE *SkipLeadingZero(BYTE *);
BYTE *DigitStringSeparate(BYTE *);
UDWORD Bin2Bcd(UDWORD);
WORD StringLength08(BYTE *);
WORD StringLength16(WORD *);
/* none */
/*
*******************************************************************************
* #ERROR SECTION
*******************************************************************************
*/
/* none */
#endif //__LIBFAT_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -