2 #define _FAT_DRIVER_H 1
4 #define DIR_CHAIN_SIZE 32
6 #define FAT_MAX_NAME 128
8 //attributes (bits:5-Archive 4-Directory 3-Volume Label 2-System 1-Hidden 0-Read Only)
9 #define FAT_ATTR_READONLY 0x01
10 #define FAT_ATTR_HIDDEN 0x02
11 #define FAT_ATTR_SYSTEM 0x04
12 #define FAT_ATTR_VOLUME_LABEL 0x08
13 #define FAT_ATTR_DIRECTORY 0x10
14 #define FAT_ATTR_ARCHIVE 0x20
16 typedef struct _fat_bpb
{
17 unsigned int sectorSize
; //bytes per sector - should be 512
18 unsigned char clusterSize
; //sectors per cluster - power of two
19 unsigned int resSectors
; //reserved sectors - typically 1 (boot sector)
20 unsigned char fatCount
; //number of FATs - must be 2
21 unsigned int rootSize
; //number of rootdirectory entries - typically 512
22 unsigned int fatSize
; //sectors per FAT - varies
23 unsigned int trackSize
; //sectors per track
24 unsigned int headCount
; //number of heads
25 unsigned int sectorCount
; //number of sectors
26 unsigned int partStart
; //sector where partition starts (boot sector)
27 unsigned int rootDirStart
; //sector where root directory starts
28 unsigned int rootDirCluster
; //fat32 - cluster of the root directory
29 unsigned int activeFat
; //fat32 - current active fat number
30 unsigned char fatType
; //12-FAT16, 16-FAT16, 32-FAT32
31 unsigned char fatId
[9]; //File system ID. "FAT12", "FAT16" or "FAT " - for debug only
32 unsigned int dataStart
; //sector where data starts
35 typedef struct _fat_driver
{
37 fat_bpb partBpb
; //partition bios parameter block
40 #define MAX_DIR_CLUSTER 512
41 unsigned int cbuf
[MAX_DIR_CLUSTER
]; //cluster index buffer // 2048 by Hermes
43 unsigned int lastChainCluster
;
46 /* enough for long filename of length 260 characters (20*13) and one short filename */
47 #define MAX_DE_STACK 21
48 unsigned int deSec
[MAX_DE_STACK
]; //direntry sector
49 int deOfs
[MAX_DE_STACK
]; //direntry offset
50 int deIdx
; //direntry index
52 #define SEQ_MASK_SIZE 2048 //Allow 2K files per directory
53 u8 seq_mask
[SEQ_MASK_SIZE
/8]; //bitmask for consumed seq numbers
54 #define DIR_MASK_SIZE 2048*11 //Allow 2K maxed fullnames per directory
55 u8 dir_used_mask
[DIR_MASK_SIZE
/8]; //bitmask for used directory entries
57 #define MAX_CLUSTER_STACK 128
58 unsigned int clStack
[MAX_CLUSTER_STACK
]; //cluster allocation stack
60 unsigned int clStackLast
; // last free cluster of the fat table
63 typedef struct _fat_dir_list
{
64 unsigned int direntryCluster
; //the directory cluster requested by getFirstDirentry
65 int direntryIndex
; //index of the directory children
68 typedef struct _fat_dir_chain_record
{
71 } fat_dir_chain_record
;
73 typedef struct _fat_dir
{
74 unsigned char attr
; //attributes (bits:5-Archive 4-Directory 3-Volume Label 2-System 1-Hidden 0-Read Only)
75 unsigned char name
[FAT_MAX_NAME
];
76 unsigned char cdate
[4]; //D:M:Yl:Yh
77 unsigned char ctime
[3]; //H:M:S
78 unsigned char adate
[4]; //D:M:Yl:Yh
79 unsigned char atime
[3]; //H:M:S
80 unsigned char mdate
[4]; //D:M:Yl:Yh
81 unsigned char mtime
[3]; //H:M:S
82 unsigned int size
; //file size, 0 for directory
83 unsigned int lastCluster
;
84 fat_dir_chain_record chain
[DIR_CHAIN_SIZE
]; //cluser/offset cache - for seeking purpose
87 int strEqual(const unsigned char *s1
, const unsigned char* s2
);
89 int fat_mount(mass_dev
* dev
, unsigned int start
, unsigned int count
);
90 void fat_forceUnmount(mass_dev
* dev
);
91 void fat_setFatDirChain(fat_driver
* fatd
, fat_dir
* fatDir
);
92 int fat_readFile(fat_driver
* fatd
, fat_dir
* fatDir
, unsigned int filePos
, unsigned char* buffer
, unsigned int size
);
93 int fat_getFirstDirentry(fat_driver
* fatd
, const unsigned char* dirName
, fat_dir_list
* fatdlist
, fat_dir
* fatDir
);
94 int fat_getNextDirentry(fat_driver
* fatd
, fat_dir_list
* fatdlist
, fat_dir
* fatDir
);
96 fat_driver
* fat_getData(int device
);
97 int fat_getFileStartCluster(fat_driver
* fatd
, const unsigned char* fname
, unsigned int* startCluster
, fat_dir
* fatDir
);
98 int fat_getClusterChain(fat_driver
* fatd
, unsigned int cluster
, unsigned int* buf
, int bufSize
, int start
);