FAT32文件系统 C语言源代码.doc

上传人:PIYPING 文档编号:11094729 上传时间:2021-06-29 格式:DOC 页数:33 大小:159.50KB
返回 下载 相关 举报
FAT32文件系统 C语言源代码.doc_第1页
第1页 / 共33页
FAT32文件系统 C语言源代码.doc_第2页
第2页 / 共33页
FAT32文件系统 C语言源代码.doc_第3页
第3页 / 共33页
FAT32文件系统 C语言源代码.doc_第4页
第4页 / 共33页
FAT32文件系统 C语言源代码.doc_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《FAT32文件系统 C语言源代码.doc》由会员分享,可在线阅读,更多相关《FAT32文件系统 C语言源代码.doc(33页珍藏版)》请在三一文库上搜索。

1、/这是FAT32文件系统源代码#ifndef _FAT_H_#define _FAT_H_#define CHARchar#define BYTEunsigned char#define WORDunsigned int#define DWORDunsigned long#define FIX_DIRECTORY 0/* 1 means use fix directory, 0 for any directory */#if FIX_DIRECTORY=0#define RECORD_ADDR_START 0/* eeprom start address */#define RECORD_AD

2、DR_END 512/* eeprom end address */#include/#include/#include#endif/#include UART.H#include /#include gui.h/声明外部器件读写函数/external hardware operating functionextern unsigned char MMC_SD_ReadSingleBlock(unsigned long sector, unsigned char* buffer);extern unsigned char MMC_SD_WriteSingleBlock(unsigned lon

3、g sector, unsigned char* buffer);extern unsigned long MMC_SD_ReadCapacity(void);#define MSDOSFSROOT 0 / cluster 0 means the root dir#define CLUST_FREE 0 / cluster 0 also means a free cluster#define MSDOSFSFREE CLUST_FREE#define CLUST_FIRST 2 / first legal cluster number#define CLUST_RSRVD 0xfff6 / r

4、eserved cluster range#define CLUST_BAD 0xfff7 / a cluster with a defect#define CLUST_EOFS 0xfff8 / start of eof cluster range#define CLUST_EOFE 0xffff / end of eof cluster range#if 0struct partsector/*char*/unsigned charpsPartCode/*512-64-2*/446;/ pad so struct is 512bunsigned charpsPart64;/ four pa

5、rtition records (64 bytes)unsigned charpsBootSectSig0;/ two signature bytes (2 bytes)unsigned charpsBootSectSig1;#define BOOTSIG0 0x55#define BOOTSIG1 0xaa;struct extboot CHARexDriveNumber;/ drive number (0x80)/0x00 for floopy disk 0x80 for hard diskCHARexReserved1;/ reserved should always set 0CHAR

6、exBootSignature;/ ext. boot signature (0x29)#define EXBOOTSIG 0x29CHARexVolumeID4;/ volume ID numberCHARexVolumeLabel11;/ volume label NO NAMECHARexFileSysType8;/ fs type (FAT12 or FAT);struct bootsector50 BYTEbsJump3;/ jump inst E9xxxx or EBxx90CHARbsOemName8;/ OEM name and versionCHARbsBPB25;/ BIO

7、S parameter blockCHARbsExt26;/ Bootsector ExtensionCHARbsBootCode448;/ pad so structure is 512bBYTEbsBootSectSig0;/ boot sector signature byte 0x55 BYTEbsBootSectSig1;/ boot sector signature byte 0xAA#define BOOTSIG0 0x55#define BOOTSIG1 0xaa;struct bpb50 WORDbpbBytesPerSec; / bytes per sector/512 1

8、024 2048 or 4096 BYTEbpbSecPerClust; / sectors per cluster/ power of 2 WORDbpbResSectors; / number of reserved sectors/1 is recommend BYTEbpbFATs; / number of FATs/ 2 is recommend WORDbpbRootDirEnts; / number of root directory entries WORDbpbSectors; / total number of sectors BYTEbpbMedia; / media d

9、escriptor/0xf8 match the fat0 WORDbpbFATsecs; / number of sectors per FAT WORDbpbSecPerTrack; / sectors per track WORDbpbHeads; / number of heads DWORDbpbHiddenSecs; / # of hidden sectors DWORDbpbHugeSectors; / # of sectors if bpbSectors = 0;/MDR-DPT(Disk Partition Table)/ length 16 bytes/struct par

10、trecordstruct _DPTbool bActive_Partition;/*=1:激活*/unsigned charcStart_Head;/ starting head for partitionunsigned charcStart_Sector;/ starting cylinder and sectorunsigned int iStart_Cylinder;/unsigned char;/ partition type (see above)unsigned charcFat_Index;/ ending head for this partitionunsigned in

11、tiEnd_Sector;/ ending cylinder and sectorunsigned longiEnd_Cylinder;/ first LBA sector for this partitionunsigned longlSector_Len;/ size of this partition (bytes or sectors ?);#endif/#define BOOTSIG0 0x55/#define BOOTSIG1 0xaa/#define BOOTSIG2 0x00/#define BOOTSIG3 0x00/512 bytes for DBR infostruct

12、DBRunsigned char cJump3;/ jump inst E9xxxx or EBxx90unsigned char cOem_Name8;/ OEM name and versionunsigned char cBpb53;/ BIOS parameter blockunsigned char cExt_Bpb26;/ Bootsector Extensionunsigned char cBoot_Code420;/ pad so structure is 512bunsigned char cEnd_Flag2;/ boot sector signature byte 0x0

13、0 ;#define FATNUM 0xf/ mask for numbering active FAT#define FATMIRROR 0x80/ FAT is mirrored (like it always was)#define FSVERS 0/ currently only 0 is understood/BPB(BIOS Parameter Block)/53bytesstruct BPB /bpb infounsigned intiBytes_Per_Sector;/ bytes per sectorunsigned charcSectors_Per_Clust;/ sect

14、ors per clusterunsigned intiRsv_Sectors;/ number of reserved sectorsunsigned charcFats;/ number of FATsunsigned intiRoot_Dir_Entries;/ number of root directory entries(fat12/16 only;fat32=0)unsigned intiSmall_Sectors;/ total number of sectorsunsigned charcMedia;/ media descriptorunsigned intiSectors

15、_Per_Fat16;/ number of sectors per FATunsigned intiSectors_Per_Track;/ sectors per trackunsigned intiHeads;/ number of headsunsigned longlHidden_Sectors;/ # of hidden sectorsunsigned longlBig_Sectors;/ like bpbFATsecs for FAT32/FAT32特有(地址采用插入方式,FAT16后面的信息往后排)unsigned longlSectors_Per_Fat32;unsigned

16、intiExt_Flag;unsigned intiFile_System_Ver;/ filesystem versionunsigned long lRoot_Clust;/ start cluster for root directoryunsigned int iFile_System_Info;/ filesystem info structure sectorunsigned int iBackup_Boot_Sector;/ backup boot sector/unsigned charcRsv_Fat3212;/ There is a 12 byte filler here,

17、 but we ignore it/boot sector infounsigned charcDrv_Num;/(0=软;0x80=硬)/unsigned charcRsv_Fat16;/reservedunsigned charcBoot_Sig;/ext boot sig(0x29)unsigned longlVol_Id;/vol idunsigned charcVol_Lab11;/vol labunsigned charcFile_System_Type8;/char for file system;#define SLOT_EMPTY 0x00 / slot has never

18、been used#define SLOT_E5 0x05 / the real value is 0xE5#define SLOT_DELETED 0xE5 / file in this slot deleted#define SLOT_DIR0x2E/ a directorymmm#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 i

19、s a system file#define ATTR_VOLUME 0x08 / entry is a volume label#define ATTR_LONG_FILENAME0x0F/ this is a long filename entry #define ATTR_DIRECTORY 0x10 / entry is a directory name#define ATTR_ARCHIVE 0x20 / file is new or modified#define LCASE_BASE 0x08 / filename base in lower case#define LCASE_

20、EXT 0x10 / filename extension in lower case/ Structure of a dos directory entry./FDTstruct direntryBYTEdeName8; / filename, blank filledBYTEdeExtension3; / extension, blank filledBYTEdeAttributes; / file attributesBYTE deLowerCase; / NT VFAT lower case flags (set to zero)BYTE deCHundredth; / hundred

21、th of seconds in CTimeBYTE deCTime2; / create timeBYTE deCDate2; / create dateBYTE deADate2; / access dateunsigned int deHighClust; / high bytes of cluster numberBYTE deMTime2; / last update timeBYTE deMDate2; / last update dateunsigned int deStartCluster; / starting cluster of fileunsigned long deF

22、ileSize; / size of file in bytes;/ number of directory entries in one sector#define DIRENTRIES_PER_SECTOR0x10/when the bpbBytesPerSec=512 / Structure of a Win95 long name directory entrystruct winentry BYTEweCnt;/ #define WIN_LAST 0x40#define WIN_CNT 0x3fBYTEwePart110;BYTEweAttributes;#define ATTR_W

23、IN95 0x0fBYTEweReserved1;BYTEweChksum;BYTEwePart212;WORD weReserved2;BYTEwePart34;#define WIN_ENTRY_CHARS13 / Number of chars per winentry/ Maximum filename length in Win95/ Note: Must be sizeof(dirent.d_name)#define WIN_MAXLEN 255/ This is the format of the contents of the deTime field in the diren

24、try/ structure./ We dont use bitfields because we dont 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#def

25、ine DT_HOURS_SHIFT 11/ This is the format of the contents of the deDate field in the direntry/ structure.#define DD_DAY_MASK0x1F/ day of month#define DD_DAY_SHIFT0#define DD_MONTH_MASK0x1E0/ month#define DD_MONTH_SHIFT5#define DD_YEAR_MASK0xFE00/ year - 1980#define DD_YEAR_SHIFT9/ Stucturesstruct Fi

26、leInfoStructunsigned long StartCluster;/ file starting cluster for last file accessedunsigned long Size;/ file size for last file accessedunsigned char Attr;/ file attr for last file accessed/unsigned short CreateTime;/ file creation time for last file accessed/unsigned short CreateDate;/ file creat

27、ion date for last file accessedunsigned long Sector;/file record placeunsigned int Offset;/file record offset;extern unsigned char Disp_Bmp(void);extern unsigned char Disp_file_name(void);extern unsigned char FAT_Init();/初始化#if 0extern unsigned long FAT_NextCluster(unsigned long cluster);/查找下一簇号exte

28、rn unsigned int FAT_FindItem(unsigned long cluster, BYTE *name, struct FileInfoStruct *FileInfo);/查找文件extern unsigned long FAT_OpenDir(BYTE * dir);/打开目录#if FIX_DIRECTORYextern unsigned char Search(BYTE *dir,struct direntry *MusicInfo,WORD *Count,BYTE *type);/查找音乐文件#elseextern BYTE SearchInit(void);e

29、xtern unsigned char Search(struct direntry *MusicInfo,WORD *Count,BYTE *type);/查找音乐文件#endifextern unsigned char FAT_LoadPartCluster(unsigned long cluster,unsigned part,BYTE * buffer);/加载文件#endif#endif以下是FAT.C源代码/*Fat.c*/*磁盘结构:namesize(sector)noteMBR/DPT sector1(jmp to DBR by DTP)xxxxDBR sector(BPB)1

30、逻辑0扇区/FAT开始rsv sectorx(get size by PBP)FAT1x(get size by PBP)FATX.x(get FATS by PBP)DIR(FDT)32file root dir(FAT12/16 only)dataall*/*init fat file system1:get MBR or DPT & jmp to DBR by DPTMBR:master boot record(主引导记录)+DPT:Disk Partition Table(磁盘分区表;和MBR同一扇区,跟在它后面)+0x55 0xaa.2.get BPB in DBR(DOS BOOT

31、 RECORD):逻辑0扇区,可以由DPT得到物理位置.3.get rsv sectors by BPB4.get fats by BPB5.get fat sectors by BPB6.get root dir sector by BPB(fat12/16 only)*/*每族字节数=32k*/*根目录:1.根FDT中每32字节一个目录项,可以是文件和目录.2.固定占用32扇区;子目录:1.最前面为父/子目录项(.和.);2.之后为子目录和文件,32字节1单位;*/#include FAT.h#include tft.h#include MMC_SD.h#include char.huns

32、igned long lFirst_Fat_Sector;/ The first FAT sectorunsigned long lFat_Sectors;/ The amount sector a FAT occupiedunsigned long lFirst_Dir_Sector;/ The first Dir sectorunsigned long lFirst_Dir_Clust; /first directory clusterunsigned long lRoot_Dir_Sectors;/ The sector number a Root dir occupied unsign

33、ed long lFirst_Data_Sector;/ The first sector number of dataunsigned char cSectors_Per_Clust;BYTE FAT32_Enable;BYTE (* FAT_ReadSector)(DWORD,BYTE *);BYTE (* FAT_WriteSector)(DWORD,BYTE *);/函数指针指向sd卡的读写函数/function pointer to the sd card read & write single block/wirte sector are not use in this playe

34、runsigned char (* FAT_ReadSector)(unsigned long sector, unsigned char * buffer)=MMC_SD_ReadSingleBlock;/device readunsigned char (* FAT_WriteSector)(unsigned long sector, unsigned char * buffer)=MMC_SD_WriteSingleBlock;/device writestruct FileInfoStruct FileInfo;/temporarily buffer for file informat

35、ionunsigned long lDbr_Sector = 0;/*DBR所在扇取*/unsigned long Capacity;struct BPB bpb;/get DBR(BPB)infovoid Get_Dbr_Info(unsigned char *p)unsigned char i,j;bpb.iBytes_Per_Sector= p18 | p0;bpb.cSectors_Per_Clust= p2;bpb.iRsv_Sectors= p48 | p3;bpb.cFats= p5;bpb.iRoot_Dir_Entries= p78 | p6;bpb.iSmall_Secto

36、rs= p98 | p8;bpb.cMedia= p10;bpb.iSectors_Per_Fat16= p128 | p11;bpb.iSectors_Per_Track= p148 | p13;bpb.iHeads= p168 | p15;bpb.lHidden_Sectors= p2024 | p1916 | p188 | p17;bpb.lBig_Sectors= p2424 | p2316 | p228 | p21;i = 25;/FAT32时插如信息,FAT16直接运行后面程序if(bpb.iSectors_Per_Fat16 = 0)bpb.lSectors_Per_Fat32=

37、 p2824 | p2716 | p268 | p25;bpb.iExt_Flag= p308 | p29;bpb.iFile_System_Ver= p328 | p31;bpb.lRoot_Clust= p3624 | p3516 | p348 | p33;bpb.iFile_System_Info= p388 | p37;bpb.iBackup_Boot_Sector= p408 | p39;/12bytsi=53;/boot sector(FAT16 & FAT32)bpb.cDrv_Num= pi+;i+;/rsvbpb.cBoot_Sig= pi+;bpb.lVol_Id= pi+

38、324 | pi+216 | pi+18 | pi;i += 4;for(j=0; j11; j+)bpb.cVol_Labj= pi+;for(j=0; j8; j+)bpb.cFile_System_Typej= pi+;/Initialize of FAT need initialize SD firstunsigned char FAT_Init()struct DBR * bs;unsigned charbuffer512;/1.memory sizeCapacity = MMC_SD_ReadCapacity();if(Capacity 0xff)return 1;/2.read

39、MBR or DBRif(FAT_ReadSector(0, buffer)/*read MBR(有可能直接为DBR)*/return 1;if(buffer510 != 0x55) | (buffer511 != 0xaa)return 1;/首扇区=DBR?if(buffer0!=0xE9) & (buffer0!=0xEB) )/2.1读取MBR中的DPT(分区表)信息,用于跳到DBRlDbr_Sector = buffer45724 | buffer45616 | buffer455= Capacity/512)/*所有扇区?*/lDbr_Sector = 0;return 1;else/2.2read DBRif(FAT_ReadSector(lDbr_Sector, buffer)/read DBRreturn 1;/read the bpb sectorif(buffer510 != 0x55) | (buffer511 != 0xaa)return 1;if(buffer0!=0xE9) & (buffer0!=0xEB)return 1;/DBR infobs = (struct DBR *)buffer;/3.get bpb infoGet_Dbr_Info(&(bs-cBpb0);/*读取DBR(BPB)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1