📄 tpbulk.i
字号:
typedef struct _INQUIRY_SPC { UINT8 OperationCode; UINT8 EnableVPD:1 ; UINT8 CmdSupportData:1 ; UINT8 Reserved0:6 ; UINT8 PageCode; UINT8 Reserved1; UINT8 AllocationLen; UINT8 Control; } INQUIRY_SPC, *pINQUIRY_SPC; 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 "tpbulk.c" /0 SYS_INFO_BLOCK DeviceInfo; TPB_STRUC data TPBulk_Block; extern XXGFLAGS bdata bXXGFlags; extern UINT8 xdata DBUF[512]; extern XXGPKG usbstack; extern UINT16 xdata FAT[512/2]; unsigned char EnumMassDev(void) { UINT32 FATSz; UINT32 TotSec; UINT32 RootDirSectors; UINT32 DataSec; UINT32 CountofClusters; pMBR_BLOCK pMBR; pBPB_BLOCK pBPB; pBPB_BLOCK32 pBPB32; if(!SPC_Inquiry()) return 0; DelayUs(2); if(!SPC_RequestSense()) return 0; DelayUs(2); if(!SPC_TestUnit()) return 0; DelayUs(2); if(!RBC_ReadCapacity()) return 0; pMBR=(pMBR_BLOCK)DBUF; DeviceInfo.BPB_BytesPerSec=512; if(!SPC_RequestSense()) return 0; DelayUs(5); if(!RBC_ReadCapacity()) return 0; DelayUs(5); if(!RBC_ReadOneSec(0x0,DBUF)) return 0; if(DBUF[0]==0xeb||DBUF[0]==0xe9) { DeviceInfo.StartSector=0; } else { DeviceInfo.StartSector=SwapINT32(pMBR->StartSector); } pBPB=(pBPB_BLOCK)DBUF; pBPB32 = (pBPB_BLOCK32)DBUF; DelayUs(5); if(!RBC_ReadOneSec(DeviceInfo.StartSector, DBUF)) return 0; DeviceInfo.BPB_BytesPerSec=WordSwap(pBPB->BPB_BytesPerSec); DeviceInfo.BPB_SecPerClus=pBPB->BPB_SecPerClus; DeviceInfo.BPB_NumFATs=pBPB->BPB_NumFATs; DeviceInfo.BPB_RootEntCnt=WordSwap(pBPB->BPB_RootEntCnt); DeviceInfo.BPB_TotSec16=WordSwap(pBPB->BPB_TotSec16); DeviceInfo.BPB_FATSz16=WordSwap(pBPB->BPB_FATSz16); DeviceInfo.BPB_FATSz32=WordSwap(pBPB32->BPB_FATSz32); DeviceInfo.BPB_TotSec32=SwapINT32(pBPB->BPB_TotSec32); DeviceInfo.FatStartSector=DeviceInfo.StartSector+WordSwap(pBPB->BPB_RsvdSecCnt); DeviceInfo.RootStartSector=DeviceInfo.FatStartSector+2*DeviceInfo.BPB_FATSz16; DeviceInfo.FirstDataSector=DeviceInfo.RootStartSector+0x20; if(DeviceInfo.BPB_FATSz16 != 0) FATSz = DeviceInfo.BPB_FATSz16; else FATSz = DeviceInfo.BPB_FATSz32; if(DeviceInfo.BPB_TotSec16 != 0) TotSec = DeviceInfo.BPB_TotSec16; else TotSec = DeviceInfo.BPB_TotSec32; RootDirSectors = ((DeviceInfo.BPB_RootEntCnt * 32) + (DeviceInfo.BPB_BytesPerSec - 1)) / DeviceInfo.BPB_BytesPerSec; DataSec = TotSec - (WordSwap(pBPB->BPB_RsvdSecCnt) + (DeviceInfo.BPB_NumFATs * FATSz) + RootDirSectors); CountofClusters = DataSec / DeviceInfo.BPB_SecPerClus; if(CountofClusters < 4085) { DeviceInfo.bFatType = 1; } else if(CountofClusters < 65525) { DeviceInfo.bFatType = 2; #line 115 "tpbulk.c" /1 #line 119 "tpbulk.c" /0 } else { DeviceInfo.bFatType = 3; DeviceInfo.TotCluster = (DeviceInfo.BPB_TotSec32-DeviceInfo.FirstDataSector+1)/DeviceInfo.BPB_SecPerClus+1; #line 126 "tpbulk.c" /1 #line 130 "tpbulk.c" /0 } return 1; } unsigned char SPC_Inquiry(void) { TPBulk_Block.TPBulk_CommandBlock.dCBW_Signature=0x55534243; TPBulk_Block.TPBulk_CommandBlock.dCBW_Tag=0x60a624de; TPBulk_Block.TPBulk_CommandBlock.dCBW_DataXferLen=0x24000000; TPBulk_Block.TPBulk_CommandBlock.bCBW_Flag=0x80; TPBulk_Block.TPBulk_CommandBlock.bCBW_LUN=0; TPBulk_Block.TPBulk_CommandBlock.bCBW_CDBLen=sizeof(INQUIRY_SPC); TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_Inquiry.OperationCode=0x12; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_Inquiry.EnableVPD=0; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_Inquiry.CmdSupportData=0; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_Inquiry.PageCode=0; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_Inquiry.AllocationLen=0x24; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_Inquiry.Control=0; if(!epBulkSend((unsigned char *)&TPBulk_Block.TPBulk_CommandBlock,31)) return 0; DelayMs(150); if(!epBulkRcv(DBUF,36)) return 0; if(!epBulkRcv((unsigned char *)&TPBulk_Block.TPBulk_CommandStatus,13)) return 0; return 1; } unsigned char SPC_RequestSense(void) { TPBulk_Block.TPBulk_CommandBlock.dCBW_Signature=0x55534243; TPBulk_Block.TPBulk_CommandBlock.dCBW_Tag=0x60a624de; TPBulk_Block.TPBulk_CommandBlock.dCBW_DataXferLen=0x0e000000; TPBulk_Block.TPBulk_CommandBlock.bCBW_Flag=0x80; TPBulk_Block.TPBulk_CommandBlock.bCBW_LUN=0; TPBulk_Block.TPBulk_CommandBlock.bCBW_CDBLen=sizeof(REQUEST_SENSE_SPC); TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_RequestSense.OperationCode=0x03; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_RequestSense.AllocationLen=0x00; if(!epBulkSend((unsigned char *)&TPBulk_Block.TPBulk_CommandBlock,31)) return 0; DelayMs(5); if(!epBulkRcv(DBUF,14)) return 0; if(!epBulkRcv((unsigned char *)&TPBulk_Block.TPBulk_CommandStatus,13)) return 0; return 1; } unsigned char SPC_TestUnit(void) { TPBulk_Block.TPBulk_CommandBlock.dCBW_Signature=0x55534243; TPBulk_Block.TPBulk_CommandBlock.dCBW_Tag=0x60a624de; TPBulk_Block.TPBulk_CommandBlock.dCBW_DataXferLen=0x00000000; TPBulk_Block.TPBulk_CommandBlock.bCBW_Flag=0x80; TPBulk_Block.TPBulk_CommandBlock.bCBW_LUN=0; TPBulk_Block.TPBulk_CommandBlock.bCBW_CDBLen=sizeof(TEST_UNIT_SPC); TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_TestUnit.OperationCode=0x00; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.SpcCdb_TestUnit.Reserved[3]=0x0; if(!epBulkSend((unsigned char *)&TPBulk_Block.TPBulk_CommandBlock,31)) return 0; DelayMs(5); if(!epBulkRcv((unsigned char *)&TPBulk_Block.TPBulk_CommandStatus,13)) return 0; return 1; } unsigned char RBC_ReadCapacity(void) { TPBulk_Block.TPBulk_CommandBlock.dCBW_Signature=0x55534243; TPBulk_Block.TPBulk_CommandBlock.dCBW_Tag=0x60a624de; TPBulk_Block.TPBulk_CommandBlock.bCBW_LUN=0; TPBulk_Block.TPBulk_CommandBlock.dCBW_DataXferLen=0x08000000; TPBulk_Block.TPBulk_CommandBlock.bCBW_Flag=0x80; TPBulk_Block.TPBulk_CommandBlock.bCBW_CDBLen=sizeof(READ_CAPACITY_RBC); TPBulk_Block.TPBulk_CommandBlock.cdbRBC.RbcCdb_ReadCapacity.OperationCode=0x25; if(!epBulkSend((unsigned char *)&TPBulk_Block.TPBulk_CommandBlock,31)) return 0; DelayMs(10); if(!epBulkRcv(DBUF,8)) return 0; if(!epBulkRcv((unsigned char *)&TPBulk_Block.TPBulk_CommandStatus,13)) return 0; return 1; } unsigned char RBC_ReadOneSec(unsigned long lba, unsigned char *pBuffer) { TPBulk_Block.TPBulk_CommandBlock.dCBW_Signature=0x55534243; TPBulk_Block.TPBulk_CommandBlock.dCBW_Tag=0x60a624de; TPBulk_Block.TPBulk_CommandBlock.dCBW_DataXferLen=SwapINT32(512); TPBulk_Block.TPBulk_CommandBlock.bCBW_Flag=0x80; TPBulk_Block.TPBulk_CommandBlock.bCBW_LUN=0; TPBulk_Block.TPBulk_CommandBlock.bCBW_CDBLen=sizeof(READ_RBC); TPBulk_Block.TPBulk_CommandBlock.cdbRBC.RbcCdb_Read.OperationCode=0x28; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.RbcCdb_Read.VendorSpecific=0; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.RbcCdb_Read.LBA.LBA_W32=lba; TPBulk_Block.TPBulk_CommandBlock.cdbRBC.RbcCdb_Read.XferLength=0x1; if(!epBulkSend((unsigned char *)&TPBulk_Block.TPBulk_CommandBlock,31)) return 0; if(!epBulkRcv(pBuffer,512)) return 0; bXXGFlags.bIN_ISR = 0x0; if(!epBulkRcv((unsigned char *)&TPBulk_Block.TPBulk_CommandStatus,13)) return 0; return 1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -