📄 fat.h
字号:
#ifndef _FAT_H
#define _FAT_H
#define MAKE_DWORD(p) (*(p)|(*(p+1)<<8) |(*(p+2)<<16)|(*(p+3)<<24) )
#define FAT_CLUSTER_BAD 0x0FFFFFF7 //You should not worry about use only one DWORD value in FAT16,FAT12,
//because it's high bits will be cut when writing it to the disk.
#define FAT_CLUSTER_EOC 0x0FFFFFFF
#define FAT_CLUSTER_EOC16 0xFFFF
#define MAX_SHORT_NAME_PAHTH 80 //including NULL
#define MAX_LONG_NAME_PATH 260 //including NULL
#define MAX_LONG_NAME 255 //not include NULL
typedef struct Partition_Def {
DWORD dwStartSector;
DWORD dwSectorNum;
DWORD dwType;
DWORD dwIndex;
}PARTITION_INFO, *PPARTITION_INFO;
#define FAT_MAKDWORD(hi, low) ((hi)<<16 | (low) )
#define FAT_VERSION12 1
#define FAT_VERSION16 2
#define FAT_VERSION32 3
#define LABEL_SIZE 11
//Note: the fist data cluster is cluster 2. so, we should plus 2 before caculate the location.
//#define FIRST_SECTOR_OF_CLUSTER(pVol,N) ( (((N)-2) * pVol->fat_info.dwSecPerCluster) + pVol->dwFirstDataSec )
#define FIRST_SECTOR_OF_CLUSTER(pVol,N) ( (((N)-pVol->dwRootDirClusterNum -2) * pVol->fat_info.dwSecPerCluster) + pVol->dwFirstDataSec )
#define START_SETOR_OF_CLUSTER(end,pVol,N) \
if( (N) >= (pVol->dwRootDirClusterNum +2)){ \
end=( (((N)- pVol->dwRootDirClusterNum -2) * pVol->fat_info.dwSecPerCluster) + pVol->dwFirstDataSec ); \
}else if( (N)< pVol->dwRootDirClusterNum ){ \
end= pVol->dwRootDirSec+(N)*pVol->fat_info.dwSecPerCluster; \
}else{ \
end=-1; \
}
#pragma pack(push,1)
typedef struct _BPB_STRUCT{
BYTE cBS_JumpRoot[3]; // where is the boot code located.
char cBS_OemName[8]; //the name of the FSD driver,
WORD wBPB_BytePerSec; //It takes on only the value: 512, 1024, 2048 or 4096.
BYTE nPBP_SecPerClus;//it must be the power of 2. so it can be 1,2,4,8,16,32,64,128.
//but note, the cluster size should not greater than 32K.
WORD wPBP_RsvdSecCnt;//reserved sector count.
BYTE nPBP_NumFATs;
WORD wPBP_RootEntryCount;
WORD wPBP_TotalSector16;
BYTE nPBP_MediaType;
WORD wPBP_FATSize16;// size of one FAT.
WORD wPBP_SectorsPerTrack;
WORD wPBP_NumHeads;
DWORD dwPBP_HiddSector;
DWORD dwPBP_TotalSector32;
}BPB_STRUCT, *PBPB_STRUCT;
typedef struct _FAT16STRUCT{ //this structure starts from offset 36.
BYTE nBS_DrvNum;
BYTE cBS_Reserved1;
BYTE nBS_BootSig;
DWORD dwBS_VolId;
char cBS_VolLab[11];
char cBS_FilSysType[8];
}FAT16STRUCT,*PFAT16STRUCT;
typedef struct _FAT32STRUCT{ //this structure starts from offset 36.
DWORD dwBPB_FATSize32;
WORD wBPB_ExtFlags;
WORD wBPB_FATVersion;
DWORD dwBPB_RootCluster; //the number of first cluster in root directory.
WORD wBPB_FSInfo;
WORD wBPB_BackupBootSector;
BYTE cBPB_Reserved[12];
BYTE nBS_DrvNum;
BYTE cBS_Reserved1;
BYTE nBS_BootSig;
DWORD dwBS_VolId;
char cBS_VolLab[11];
char cBS_FilSysType[8];
}FAT32STRUCT,*PFAT32STRUCT;
typedef struct FSINFO{
DWORD dwLeadSig;
char cReserved1[480];
DWORD dwStructSig;
DWORD dwFreeCount;
DWORD dwNextFree;
char cReserved2[12];
DWORD dwTrailSig;
}FSINFO,*PFSINFO;
typedef struct DSKSZTOSECPERCLUS {
DWORD dwDiskSize;
BYTE cSecPerClusVal;
}DSKSZTOSECPERCLUS;
//Now, define the directory entry.
#define FILE_ATTR_READ_ONLY 0x01
#define FILE_ATTR_HIDDEN 0x02
#define FILE_ATTR_SYSTEM 0x04
#define FILE_ATTR_VOLUME_ID 0x08
#define FILE_ATTR_DIRECTORY 0x10
#define FILE_ATTR_ARCHIVE 0x20
#define FILE_ATTR_LONG_NAME (FILE_ATTR_READ_ONLY |FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM | FILE_ATTR_VOLUME_ID)
#define DIR_ENTRY_FREE 0xE5
#define DIR_ENTRY_EOF 0x00
#define DIR_ENTRY_E5 0x05
// Invalid character for dir name.
//0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D,and 0x7C.
typedef struct FAT_DATE{
unsigned short nDay: 5 ;
unsigned short nMonth:4;
unsigned short nYearCount:7; //Year count from 1980.
}FAT_DATE, *PFAT_DATE;
typedef struct FAT_TIME{
unsigned short nTwiceSecond :5; //two seconds count.
unsigned short nMinute :6;
unsigned short nHour :5;
}FAT_TIME,*PFAT_TIME;
typedef struct _FAT_FOLDER_ENTRY{
UCHAR cShortName[11];
UCHAR cAttr;
UCHAR cRsved;
UCHAR cTimeTenth;// tenth of a second. valid value is 0-199.
FAT_TIME mTimeCreated;
FAT_DATE mDateCreated;
FAT_DATE mLastAccessDate;
WORD wFirstClusterHigh;
FAT_TIME mTimeLastWrite;
FAT_DATE mDateLastWrite;
WORD wFirstClusterLow;
DWORD dwFileSize; //for a dirctory, this value is always 0.
}DIR_ENTRY, *PDIR_ENTRY;
#define LAST_LONG_ENTRY 0x40
#define MAX_LONG_NAME_ENTRY 0x20
#define NAME_SIZE_ONE_ENTRY 26
#define NAME_SIZE_WORD 13
typedef struct _LONG_NAME_ENTRY{
UCHAR cNameOrder;
UCHAR cLongName1[10];
UCHAR cAttr;
UCHAR cType;
UCHAR cChecksum;
UCHAR cLongName2[12];
WORD wFirstClusterLow;
UCHAR cLongName3[4];
}LONG_NAME_ENTRY,*PLONG_NAME_ENTRY;
#pragma pack(pop)
typedef struct _FAT_INFO{
DWORD dwClusterCount; //The cluster number in the volume(only data area).
DWORD dwFreeCluster;
DWORD dwBytesPerSec;
DWORD dwSecPerCluster;
DWORD dwRsvedSector;
DWORD dwNumFAT;
DWORD dwRootEntryCount; //only in FAT16/12.
DWORD dwTotalSector;
DWORD dwFATSize; // size of FAT table, in sectors.
DWORD dwRootCluster;
//PVOLUME_FAT pVolume;
}FAT_INFO ,*PFAT_INFO;
typedef struct _VOULME_FAT{
struct _VOULME_FAT * pNext;
char csVolName[20];
DWORD dwVersion;
DWORD dwFirstDataSec;
DWORD dwClusterSize;
DWORD dwRootDirSec; //It should be identical with dwFirstDataSec in FAT32.
//in FAT16, it should be in the front of dwFirstDataSec, with a distance of RootEntry size.
DWORD dwRootDirClusterNum; //FAT16中此值为 根目录所占的 cluster数目(只有要目录所占的空间是 cluster的整数倍才被支持)。
//FAT32中这个值为0。在FAT16中,我们将从硬件上读到的 cluster数值加了 dwRootDirClusterNum,
FAT_INFO fat_info;
PPARTITION_INFO pPartInfo;
char cLabel[LABEL_SIZE];
}VOLUME_FAT,*PVOLUME_FAT;
typedef struct _FILE_DESC{
PVOLUME_FAT pVolume;
// DWORD dwStartCluster;
DWORD dwCurrCluster;
DWORD dwPosInCluster;
DWORD dwClusterSize;
DWORD nFilePointer;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwFirstCluster;
DWORD dwFileAttributes;
TCHAR csName[512];
BOOL bValid;
DWORD dwPosInBuf;
PUCHAR pFileData;
}FILE_DESC,*PFILE_DESC;
typedef struct _DIR_ENTRY_FIND{
TCHAR csName[512];
UCHAR cAttr;
UCHAR cChecksum;
BOOL bLongName;
DWORD dwCurEntryIndex;
DWORD bValid;
// DWORD dwLongNameOffset;
// DWORD dwLongNameCluster;
}DIR_ENTRY_FIND, *PDIR_ENTRY_FIND;
#define SECTOR_SIZE 512
BOOL InitializePartionTable( void );
BOOL InitializeFATSystem( void );
BOOL OpenFile( TCHAR * csName );
DWORD lReadFile(PUCHAR pBuf, DWORD dwLength);
//BOOL ReadSectors(UCHAR Drive, ULONG LBA, USHORT nSectors, PUCHAR pBuffer);
BOOLEAN ReadSectors(UCHAR Drive, ULONG LBA, USHORT nSectors, PUCHAR pBuffer);
int OutputMessage (LPCWSTR lpszFormat, ...);
#define DEBUG_TEST 0
#define DEBUG_ERROR 1
//#define RETAILMSG(cond,printf_exp) \
// ((cond)?(OutputMessage printf_exp),1:0)
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -