www.pudn.com > flashfat16.rar > fat16.H


#ifndef FAT_H 
#define FAT_H 
 
#ifndef BYTE 
typedef unsigned char BYTE; 
typedef unsigned char U8; 
#endif 
 
#ifndef CHAR 
typedef char CHAR; 
#endif 
 
#ifndef WORD 
typedef unsigned short WORD; 
#endif 
 
#ifndef DWORD 
typedef unsigned long DWORD; 
#endif 
 
#define CLUST_FIRST     0       // first legal cluster number(for PART_TYPE_FAT16xxx) 
 
 // Format of a boot sector.  This is the first sector on a DOS floppy disk 
 // or the first sector of a partition on a hard disk.  But, it is not the 
 // first sector of a partitioned hard disk. 
#define NAND_FLASH_Drv   1 
#define DRIVE0           NAND_FLASH_Drv 
 
#define FAT12_MASK      0x00000fff      // mask for 12 bit cluster numbers 
#define FAT16_MASK      0x0000ffff      // mask for 16 bit cluster numbers 
#define FAT32_MASK      0x0fffffff      // mask for FAT32 cluster numbers 
 
// Partition Type used in the partition record 
#define PART_TYPE_UNKNOWN       0x00 
#define PART_TYPE_FAT12         0x01 
#define PART_TYPE_XENIX         0x02 
#define PART_TYPE_DOSFAT16      0x04 
#define PART_TYPE_EXTDOS        0x05 
#define PART_TYPE_FAT16         0x06 
#define PART_TYPE_NTFS          0x07 
#define PART_TYPE_FAT32         0x0B 
#define PART_TYPE_FAT32LBA      0x0C 
#define PART_TYPE_FAT16LBA      0x0E 
#define PART_TYPE_EXTDOSLBA     0x0F 
#define PART_TYPE_ONTRACK       0x33 
#define PART_TYPE_NOVELL        0x40 
#define PART_TYPE_PCIX          0x4B 
#define PART_TYPE_PHOENIXSAVE   0xA0 
#define PART_TYPE_CPM           0xDB 
#define PART_TYPE_DBFS          0xE0 
#define PART_TYPE_BBT           0xFF 
//MBR struct 
typedef __packed struct  
{ 
     CHAR    psPartCode[512-64-2];       // pad so struct is 512b 
     BYTE    psPart[64];                 // four partition records (64 bytes) 
     BYTE    psBootSectSig0;             // two signature bytes (2 bytes) 
     BYTE    psBootSectSig1; 
 #define BOOTSIG0        0x55 
 #define BOOTSIG1        0xaa 
}PARTSECTOR; 
 
 
//partition record information 
typedef __packed struct  // length 16 bytes 
 {            
     BYTE    prIsActive;                 // 0x80 indicates active partition 
     BYTE    prStartHead;                // starting head for partition 
     WORD    prStartCylSect;             // starting cylinder and sector 
     BYTE    prPartType;                 // partition type (see above PART_TYPE_XXX) 
     BYTE    prEndHead;                  // ending head for this partition 
     WORD    prEndCylSect;               // ending cylinder and sector 
     DWORD   prStartLBA;                 // first LBA sector for this partition 
     DWORD   prSize;                     // size of this partition (bytes or sectors ?sector) 
 }PARTRECORD; 
//boot sector for dos3.3  
typedef __packed struct { 
     BYTE    bsJump[3];                  // jump inst E9xxxx or EBxx90 
     CHAR    bsOemName[8];               // OEM name and version 
     CHAR    bsBPB[19];                  // BIOS parameter block 
     CHAR    bsDriveNumber;              // drive number (0x80) 
     CHAR    bsBootCode[479];            // pad so struct is 512b 
     BYTE    bsBootSectSig0;             // boot sector signature byte 0x55 
     BYTE    bsBootSectSig1;             // boot sector signature byte 0xAA 
 #define BOOTSIG0        0x55 
 #define BOOTSIG1        0xaa 
 }BOOTSECTOR33 ; 
  
typedef __packed struct { 
     CHAR    exDriveNumber;              // drive number (0x80) 
     CHAR    exReserved1;                // reserved 
     CHAR    exBootSignature;            // ext. boot signature (0x29) 
 #define EXBOOTSIG       0x29 
     DWORD   exVolumeID;              // volume ID number 
     CHAR    exVolumeLabel[11];          // volume label 
     CHAR    exFileSysType[8];           // fs type (FAT12 or FAT16) 
 }EXTBOOT; 
//boot sector for dos5.0  
typedef __packed struct { 
     BYTE    bsJump[3];                  // jump inst E9xxxx or EBxx90 
     CHAR    bsOemName[8];               // OEM name and version 
     CHAR    bsBPB[25];                  // BIOS parameter block 
     CHAR    bsExt[26];                  // Bootsector Extension 
     CHAR    bsBootCode[448];            // pad so structure is 512b 
     BYTE    bsBootSectSig0;             // boot sector signature byte 0x55  
     BYTE    bsBootSectSig1;             // boot sector signature byte 0xAA 
 #define BOOTSIG0        0x55 
 #define BOOTSIG1        0xaa 
 }BOOTSECTOR50; 
//boot sector for dos7.1  
typedef __packed struct { 
     BYTE    bsJump[3];                  // jump inst E9xxxx or EBxx90 
     CHAR    bsOEMName[8];               // OEM name and version 
     CHAR    bsBPB[53];                  // BIOS parameter block 
     CHAR    bsExt[26];                  // Bootsector Extension 
     CHAR    bsBootCode[418];            // pad so structure is 512b 
     BYTE    bsBootSectSig2;             // 2 & 3 are only defined for FAT32? 
     BYTE    bsBootSectSig3; 
     BYTE    bsBootSectSig0;             // boot sector signature byte 0x55 
     BYTE    bsBootSectSig1;             // boot sector signature byte 0xAA 
 #define BOOTSIG0        0x55 
 #define BOOTSIG1        0xaa 
 #define BOOTSIG2        0 
 #define BOOTSIG3        0 
 }BOOTSECTOR710; 
  
 /***************************************************************/ 
 /***************************************************************/ 
  
 // BIOS Parameter Block (BPB) for DOS 3.3 
typedef __packed struct { 
         WORD    bpbBytesPerSec; // bytes per sector 
         BYTE    bpbSecPerClust; // sectors per cluster 
         WORD    bpbResSectors;  // number of reserved sectors 
         BYTE    bpbFATs;        // number of FATs 
         WORD    bpbRootDirEnts; // number of root directory entries 
         WORD    bpbSectors;     // total number of sectors 
         BYTE    bpbMedia;       // media descriptor 
         WORD    bpbFATsecs;     // number of sectors per FAT 
         WORD    bpbSecPerTrack; // sectors per track 
         WORD    bpbHeads;       // number of heads 
         WORD    bpbHiddenSecs;  // number of hidden sectors 
 }BPB33; 
  
 // BPB for DOS 5.0 
 // The difference is bpbHiddenSecs is a short for DOS 3.3, 
 // and bpbHugeSectors is not present in the DOS 3.3 bpb. 
 typedef __packed struct { 
         WORD    bpbBytesPerSec; // bytes per sector 
         BYTE    bpbSecPerClust; // sectors per cluster 
         WORD    bpbResSectors;  // number of reserved sectors 
         BYTE    bpbFATs;        // number of FATs 
         WORD    bpbRootDirEnts; // number of root directory entries 
         WORD    bpbSectors;     // total number of sectors 
         BYTE    bpbMedia;       // media descriptor 
         WORD    bpbFATsecs;     // number of sectors per FAT 
         WORD    bpbSecPerTrack; // sectors per track 
         WORD    bpbHeads;       // number of heads 
         DWORD   bpbHiddenSecs;  // # of hidden sectors 
 // 3.3 compat ends here 
         DWORD   bpbHugeSectors; // # of sectors if bpbSectors == 0 
 }BPB50; 
  
 // BPB for DOS 7.10 (FAT32) 
 // This one has a few extensions to bpb50. 
 typedef __packed struct { 
         WORD    bpbBytesPerSec; // bytes per sector 
         BYTE    bpbSecPerClust; // sectors per cluster 
         WORD    bpbResSectors;  // number of reserved sectors 
         BYTE    bpbFATs;        // number of FATs 
         WORD    bpbRootDirEnts; // number of root directory entries 
         WORD    bpbSectors;     // total number of sectors 
         BYTE    bpbMedia;       // media descriptor 
         WORD    bpbFATsecs;     // number of sectors per FAT 
         WORD    bpbSecPerTrack; // sectors per track 
         WORD    bpbHeads;       // number of heads 
         DWORD   bpbHiddenSecs;  // # of hidden sectors 
 // 3.3 compat ends here 
         DWORD   bpbHugeSectors; // # of sectors if bpbSectors == 0 
 // 5.0 compat ends here 
         DWORD     bpbBigFATsecs;// like bpbFATsecs for FAT32 
         WORD      bpbExtFlags;  // extended flags: 
 #define FATNUM    0xf           // mask for numbering active FAT 
 #define FATMIRROR 0x80          // FAT is mirrored (like it always was) 
         WORD      bpbFSVers;    // filesystem version 
 #define FSVERS    0             // currently only 0 is understood 
         DWORD     bpbRootClust; // start cluster for root directory 
         WORD      bpbFSInfo;    // filesystem info structure sector 
         WORD      bpbBackup;    // backup boot sector 
         // There is a 12 byte filler here, but we ignore it 
 }BPB710; 
  
 
// Structure of a dos directory entry. 
typedef __packed struct 
{ 
         BYTE        deName[8];      // filename, blank filled 
 #define SLOT_EMPTY      0x00            // slot has never been used 
 #define SLOT_E5         0x05            // the real value is 0xe5 
 #define SLOT_DELETED    0xe5            // file in this slot deleted 
 #define IS_DIR          0x2e            // this is directory 
         char        deExtension[3]; // extension, blank filled 
         BYTE        deAttributes;   // file attributes 
 #define ATTR_NORMAL     0x00            // normal file 
 #define ATTR_READONLY   0x01            // file is readonly 
 #define ATTR_HIDDEN     0x02            // file is hidden 
 #define ATTR_SYSTEM     0x04            // file is a system file 
 #define ATTR_VOLUME     0x08            // entry is a volume label 
 #define ATTR_LONG_FILENAME  0x0f        // this is a long filename entry                 
 #define ATTR_DIRECTORY  0x10            // entry is a directory name 
 #define ATTR_ARCHIVE    0x20            // file is new or modified 
         BYTE        deLowerCase;    // NT VFAT lower case flags 
 #define LCASE_BASE      0x08            // filename base in lower case 
 #define LCASE_EXT       0x10            // filename extension in lower case 
         BYTE        CrtTimeTenth;   // hundredth of seconds in CTime 
         WORD        CrtTime;     // create time 
         WORD        CrtDate;     // create date 
         WORD        LstAccDate;     // access date 
         WORD        FstClusHI;    // high bytes of cluster number 
         WORD        WrtTime;     // last update time 
         WORD        WrtDate;     // last update date 
         WORD        deStartCluster; // starting cluster of file 
         DWORD       deFileSize;     // size of file in bytes 
}DIRENTRY; 
 
// Structure of a Win95 long name directory entry 
 typedef __packed struct   
 { 
         BYTE        weCnt; 
 #define WIN_LAST        0x40 
 #define WIN_CNT         0x3f 
         BYTE        wePart1[10]; 
         BYTE        weAttributes; 
 #define ATTR_WIN95      0x0f 
         BYTE        weReserved1; 
         BYTE        weChksum; 
         BYTE        wePart2[12]; 
         WORD        weReserved2; 
         BYTE        wePart3[4]; 
 }WINENTRY;	 
  
 // This is the format of the contents of the deTime field in the direntry 
 // structure. 
 // We don't use bitfields because we don't know how compilers for 
 // arbitrary machines will lay them out. 
 #define DT_2SECONDS_MASK        0x1F    // seconds divided by 2 
 #define DT_2SECONDS_SHIFT       0 
 #define DT_MINUTES_MASK         0x7E0   // minutes 
 #define DT_MINUTES_SHIFT        5 
 #define DT_HOURS_MASK           0xF800  // hours 
 #define DT_HOURS_SHIFT          11 
  
 // This is the format of the contents of the deDate field in the direntry 
 // structure. 
 #define DD_DAY_MASK             0x1F    // day of month 
 #define DD_DAY_SHIFT            0 
 #define DD_MONTH_MASK           0x1E0   // month 
 #define DD_MONTH_SHIFT          5 
 #define DD_YEAR_MASK            0xFE00  // year - 1980 
 #define DD_YEAR_SHIFT           9 
  
 typedef __packed struct  
{ 
	WORD Date; 
	WORD Time; 
	BYTE TimeTenth; 
}FatDateTime; 
// 
//FAT16 Apis 
// 
 
typedef __packed struct  
{ 
	BYTE Attr; 
	BYTE CrtTimeTenth; 
	WORD CrtTime; 
	WORD CrtDate; 
	WORD LstAccDate; 
	WORD WrtTime; 
	WORD WrtDate; 
	DWORD FileSize; 
}_STAT; 
 
typedef __packed struct  
{ 
	int valid; // 1 valid, 0 free. 
 
	DWORD DirSectorNum; 
	int DirIndex; 
 
	DWORD StartSectorNum; 
	DWORD CurrentSectorNum; 
	DWORD SectorOffset; 
 
	DIRENTRY dir; 
 
	unsigned long offset; 
}_FILE; 
 
typedef __packed struct  
{ 
	DWORD DirSectorNum; 
	int DirIndex; 
	int IsRootDir; 
	char filename[13]; 
}FatGet; 
  
 // Prototypes 
 int fat_format(unsigned char Media,unsigned char Sizeofdisk_M,unsigned char FilesysType); 
  
 unsigned char fatInit(void); 
  
   
void fat16_Nandflash_test(void); 
  
 
  
 
 
int fat_mkdir( const char *dirname ); 
int fat_rmdir( const char *dirname ); 
int fat_getfirst(const char *path, char* filename); 
int fat_getnext(char* filename); 
 
int fat_close(int handle); 
int fat_creat(const char* filename, BYTE attribute); 
long fat_lseek(int handle, long offset, int origin); 
int fat_open(const char* filename); 
unsigned int fat_read(int handle, void* buffer, unsigned int bytes); 
unsigned int fat_write(int handle, const char* buffer, unsigned int bytes); 
 
int fat_remove( const char *filename); 
int fat_get_stat( const char *filename,  _STAT *stat); 
int fat_set_stat( const char *filename,  _STAT *stat); 
int fat_rename( const char *oldname, const char *newname );                                  
  
 #endif