fat.i
来自「51环境下的FAT及FAT32文件系统源码」· I 代码 · 共 1,398 行 · 第 1/2 页
I
1,398 行
typedef struct _MODE_SELECT_SPC { UINT8 OperationCode; UINT8 SavePage : 1 ; UINT8 Reseved0 : 3 ; UINT8 PageFormat : 1 ; UINT8 Reserved1 : 3 ; UINT8 Reserved2[2]; UINT8 ParameterLen; UINT8 Control; } MODE_SELECT_SPC, *pMODE_SELECT_SPC; typedef struct _MBR_BLOCK { UINT8 Res[454]; unsigned long StartSector; unsigned long TotalSector; UINT8 Res1[50]; } MBR_BLOCK,*pMBR_BLOCK; typedef struct _BPB_BLOCK { UINT8 BS_jmpBoo[3]; UINT8 BS_OEMName[8]; UINT16 BPB_BytesPerSec; UINT8 BPB_SecPerClus; UINT16 BPB_RsvdSecCnt; UINT8 BPB_NumFATs; UINT16 BPB_RootEntCnt; UINT16 BPB_TotSec16; UINT8 BPB_Media; UINT16 BPB_FATSz16; UINT16 BPB_SecPerTrk; UINT16 BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; UINT8 BS_DrvNum; UINT8 BS_Reserved1; UINT8 BS_BootSig; UINT8 BS_VolID[4]; UINT8 BS_VolLab[11]; UINT8 BS_FilSysType[8]; UINT8 ExecutableCode[448]; UINT8 Marker[2]; } BPB_BLOCK,*pBPB_BLOCK; typedef struct _BPB_BLOCK32 { UINT8 BS_jmpBoo[3]; UINT8 BS_OEMName[8]; UINT16 BPB_BytesPerSec; UINT8 BPB_SecPerClus; UINT16 BPB_RsvdSecCnt; UINT8 BPB_NumFATs; UINT16 BPB_RootEntCnt; UINT16 BPB_TotSec16; UINT8 BPB_Media; UINT16 BPB_FATSz16; UINT16 BPB_SecPerTrk; UINT16 BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; UINT32 BPB_FATSz32; UINT16 BPB_ExtFlags; UINT16 BPB_FSVer; UINT32 BPB_RootClus; UINT16 BPB_FSInfo; UINT16 BPB_BkBootSec; UINT8 BPB_Reserved[12]; UINT8 BS_DrvNum; UINT8 BS_Reserved1; UINT8 BS_BootSig; UINT32 BS_VolID; UINT8 BS_VolLab[11]; UINT8 BS_FilSysType[8]; } BPB_BLOCK32,*pBPB_BLOCK32; typedef struct _SYS_INFO_BLOCK{ unsigned long StartSector; unsigned long TotalSector; UINT16 BPB_BytesPerSec; UINT8 BPB_SecPerClus; UINT8 BPB_NumFATs; UINT16 BPB_RootEntCnt; UINT16 BPB_TotSec16; UINT8 BPB_Media; UINT16 BPB_FATSz16; UINT32 BPB_FATSz32; UINT16 BPB_SecPerTrk; UINT16 BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; UINT8 BS_DrvNum; UINT8 BS_BootSig; UINT8 BS_VolID[4]; UINT8 BS_VolLab[11]; UINT8 BS_FilSysType[8]; unsigned long FatStartSector; unsigned long RootStartSector; unsigned long FirstDataSector; unsigned long TotCluster; unsigned char bFatType; } SYS_INFO_BLOCK,*pSYS_INFO_BLOCK; typedef struct _FILE_INFO{ unsigned char bFileOpen; unsigned int StartCluster; unsigned long LengthInByte; unsigned int ClusterPointer; unsigned long SectorPointer; unsigned int OffsetofSector; unsigned char SectorofCluster; unsigned long pointer; } FILE_INFO, *pFILE_INFO; typedef struct _DIR_INFO{ unsigned char name[8]; unsigned char extension[3]; unsigned char attribute; unsigned char Reserved[10]; unsigned int lastUpdateDate; unsigned int lastUpdateTime; unsigned int startCluster; unsigned long length; } DIR_INFO, *pDIR_INFO; typedef struct _MODE_SENSE_SPC { UINT8 OperationCode; UINT8 Reseved0 : 3 ; UINT8 DisableBlockDescriptor : 1 ; UINT8 Reserved0 : 4 ; UINT8 PageCode:6 ; UINT8 PageControl : 2 ; UINT8 Reserved1; UINT8 ParameterLen; UINT8 Control; } MODE_SENSE_SPC, *pMODE_SENSE_SPC; typedef struct _MEDIA_REMOVAL_SPC { UINT8 OperationCode; UINT8 Reserved0[3]; UINT8 Prevent; } MEDIA_REMOVAL_SPC, *pMEDIA_REMOVAL_SPC; typedef struct _REQUEST_SENSE_SPC { UINT8 OperationCode; UINT8 Reserved[3]; UINT8 AllocationLen; UINT8 Control; } REQUEST_SENSE_SPC, *pREQUEST_SENSE_SPC; typedef struct _TEST_UNIT_SPC { UINT8 OperationCode; UINT8 Reserved[4]; UINT8 Control; } TEST_UNIT_SPC, *pTEST_UNIT_SPC; typedef struct _WRITE_BUFFER_SPC { UINT8 OperationCode; UINT8 Mod:4 ; UINT8 Reserved0:4 ; UINT8 BufferID; UINT8 BufferOff_2; UINT8 BufferOff_1; UINT8 BufferOff_0; UINT8 ParameterLen_2; UINT8 ParameterLen_1; UINT8 ParameterLen_0; UINT8 Control; } WRITE_BUFFER_SPC, *pWRITE_BUFFER_SPC; typedef union _CDB_RBC { GENERIC_CDB Cdb_Generic; GENERIC_RBC RbcCdb_Generic; FORMAT_RBC RbcCdb_Format; READ_RBC RbcCdb_Read; READ_CAPACITY_RBC RbcCdb_ReadCapacity; START_STOP_RBC RbcCdb_OnOffUnit; SYNCHRONIZE_CACHE_RBC RbcCdb_SyncCache; VERIFY_RBC RbcCdb_Verify; WRITE_RBC RbcCdb_Write; INQUIRY_SPC SpcCdb_Inquiry; MODE_SELECT_SPC SpcCdb_ModeSelect; MODE_SENSE_SPC SpcCdb_ModeSense; MEDIA_REMOVAL_SPC SpcCdb_Remove; REQUEST_SENSE_SPC SpcCdb_RequestSense; TEST_UNIT_SPC SpcCdb_TestUnit; WRITE_BUFFER_SPC SpcCdb_WriteBuffer; READ_10 CmdRead10; WRITE_10 CmdWrite10; MODE_SELECT_10 CmdModeSel10; MODE_SENSE_10 CmdModeSen10; READ_LONG_CMD SpcCdb_ReadLong; } CDB_RBC, *pCDB_RBC; #line 1 "tpbulk.h" /0 typedef struct _COMMAND_BLOCK_WRAPPER{ UINT32 dCBW_Signature; UINT32 dCBW_Tag; UINT32 dCBW_DataXferLen; UINT8 bCBW_Flag; UINT8 bCBW_LUN; UINT8 bCBW_CDBLen; CDB_RBC cdbRBC; } CBW, *pCBW; typedef struct _COMMAND_STATUS_WRAPPER{ UINT32 dCSW_Signature; UINT32 dCSW_Tag; UINT32 dCSW_DataResidue; UINT8 bCSW_Status; } CSW, *pCSW; typedef union _TPBULK_STRUC { CBW TPBulk_CommandBlock; CSW TPBulk_CommandStatus; }TPB_STRUC, *pTPB_STRUC; unsigned char EnumMassDev(void); unsigned char TPBulk_GetMaxLUN(void); unsigned char SPC_Inquiry(void); unsigned char SPC_READLONG(void); unsigned char SPC_RequestSense(void); unsigned char SPC_TestUnit(void); unsigned char SPC_LockMedia(void); unsigned char RBC_ReadCapacity(void); unsigned char RBC_ReadOneSec(unsigned long lba, unsigned char *pBuffer); unsigned char RBC_Write(unsigned long lba,unsigned char len,unsigned char *pBuffer); #line 3 "fat.c" /0 #line 1 "usb.h" /0 #line 1 "C:\KEIL\C51\INC\ABSACC.H" /0 #line 10 "C:\KEIL\C51\INC\ABSACC.H" /1 #line 1 "usb.h" /0#line 1 "usb.h" /0 typedef struct { unsigned short wVID, wPID; unsigned char bClass; unsigned char bNumOfEPs; unsigned char iMfg; unsigned char iPdt; unsigned char bId1; unsigned char bId2; unsigned char bEPAddr[0x05]; unsigned char bAttr[0x05]; unsigned short wPayLoad[0x05]; unsigned short bInterval[0x05]; unsigned char bData1[0x05]; } USBDEV, *pUSBDEV; typedef struct { unsigned char bmRequest; unsigned char bRequest; unsigned short wValue; unsigned short wIndex; unsigned short wLength; } SetupPKG, *pSetupPKG; typedef struct { unsigned char usbaddr; unsigned char pid; unsigned char wPayload; unsigned int wLen; unsigned char *buffer; SetupPKG setup; unsigned char epbulkin; unsigned char epbulkout; } XXGPKG, *pXXGPKG; typedef struct { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } sDevDesc, *pDevDesc; typedef struct { unsigned char bLength; unsigned char bType; unsigned short wLength; unsigned char bNumIntf; unsigned char bCV; unsigned char bIndex; unsigned char bAttr; unsigned char bMaxPower; } sCfgDesc, *pCfgDesc; typedef struct { unsigned char bLength; unsigned char bType; unsigned char iNum; unsigned char iAltString; unsigned char bEndPoints; unsigned char iClass; unsigned char iSub; unsigned char iProto; unsigned char iIndex; } sIntfDesc, *pIntfDesc; typedef struct { unsigned char bLength; unsigned char bType; unsigned char bEPAdd; unsigned char bAttr; unsigned short wPayLoad; unsigned char bInterval; } sEPDesc, *pEPDesc; void usbBufRead(unsigned char *s, unsigned char c); void usbBufWrite(unsigned char *s, unsigned char c); void usbReset(void); unsigned char usbXfer(unsigned char endPoint); unsigned char ep0Xfer(void); unsigned char epBulkSend(unsigned char *pBuffer,unsigned int len); unsigned char epBulkRcv(unsigned char *pBuffer,unsigned int len); unsigned char setAddress(unsigned char addr); unsigned char setConfiguration(void); unsigned char setDesc(void); unsigned char enumUsbDev(unsigned char usbaddr); void usbInit(void); void checkKeyLED(void); #line 4 "fat.c" /0 extern SYS_INFO_BLOCK xdata DeviceInfo; extern FILE_INFO xdata ThisFile; unsigned int FatSectorPointer=-1; extern UINT8 xdata FATBUF[512]; unsigned int ThisFatSecNum(unsigned int clusterNum) { unsigned int temp; temp=clusterNum*2; temp=temp/DeviceInfo.BPB_BytesPerSec; temp=temp+DeviceInfo.FatStartSector; return temp; } unsigned long FirstSectorofCluster(unsigned int clusterNum) { unsigned long temp; temp=clusterNum-2; temp=temp*DeviceInfo.BPB_SecPerClus; temp=temp+DeviceInfo.FirstDataSector; return temp; } unsigned int ThisFatEntOffset(unsigned int clusterNum) { unsigned int temp1,temp2; temp1=2*clusterNum; temp2=temp1/DeviceInfo.BPB_BytesPerSec; temp1=temp1-temp2*DeviceInfo.BPB_BytesPerSec; return temp1; } unsigned int GetNextClusterNum(unsigned int clusterNum) { unsigned int xxgFatSecNum,xxgFatEntOffset; xxgFatSecNum=ThisFatSecNum(clusterNum); xxgFatEntOffset=ThisFatEntOffset(clusterNum); if(FatSectorPointer!=xxgFatSecNum) { if(!RBC_ReadOneSec(xxgFatSecNum, FATBUF)) return 0xFFFF; FatSectorPointer=xxgFatSecNum; } clusterNum=FATBUF[xxgFatEntOffset+1]; clusterNum=clusterNum<<8; clusterNum+=FATBUF[xxgFatEntOffset]; return clusterNum; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?