?? hpi.c
字號:
#include "common.h"
extern XXGFLAGS bdata bXXGFlags;
extern unsigned char xdata DBUF[BUFFER_LENGTH];
//////////////////////////////////////////
//CH_CMD_BLOCK xdata inblock;
unsigned char xdata CHBUF[CHBUF_LENGTH];
SYS_INFO_BLOCK xdata DeviceInfo;
CH_CMD_BLOCK xdata CHCmdBlock;
CH_RSP_BLOCK xdata CHRspBlock;
FILE_INFO xdata ThisFile;
extern FREE_FAT_INFO xdata FreeFat;
unsigned char EnumMassDev(void)
{
PMBR_BLOCK pMBR;
PBPB_BLOCK pBPB;
if(mInitDisk( )!=USB_INT_SUCCESS)return 0 ; /* 初始化USB存儲器 */
pMBR=(PMBR_BLOCK)DBUF;
DeviceInfo.BPB_BytesPerSec=512; //暫假設為512
//if(!SPC_RequestSense())
// return FALSE;
if(!SPC_TestUnit())
return FALSE;
if(!SPC_LockMedia())
return FALSE;
if(!ReadCapacity())
return FALSE;
if(!RBC_Read(0x0,1,DBUF))
return FALSE;
//////////////////////////////////
if(DBUF[0]==0xeb||DBUF[0]==0xe9){ //判斷是否是BS_jmpBoot
DeviceInfo.StartSector=0;
//DeviceInfo.TotalSector=SwapINT32(pMBR->TotalSector);
}
else {
//////////////////////////////////
DeviceInfo.StartSector=SwapINT32(pMBR->StartSector);
//DeviceInfo.TotalSector=SwapINT32(pMBR->TotalSector);
}
///////////////////////////////////////////////////////
pBPB=(PBPB_BLOCK)DBUF;
if(!RBC_Read(DeviceInfo.StartSector,1,DBUF))
return FALSE;
DeviceInfo.BPB_BytesPerSec=SwapINT16(pBPB->BPB_BytesPerSec);
DeviceInfo.BPB_SecPerClus=pBPB->BPB_SecPerClus;
DeviceInfo.BPB_NumFATs=pBPB->BPB_NumFATs;
DeviceInfo.BPB_RootEntCnt=SwapINT16(pBPB->BPB_RootEntCnt);
DeviceInfo.BPB_TotSec16=SwapINT16(pBPB->BPB_TotSec16);
DeviceInfo.BPB_FATSz16=SwapINT16(pBPB->BPB_FATSz16);
DeviceInfo.BPB_TotSec32=SwapINT32(pBPB->BPB_TotSec32);
DeviceInfo.FatStartSector=DeviceInfo.StartSector+pBPB->BPB_RsvdSecCn[0];
DeviceInfo.RootStartSector=DeviceInfo.StartSector+2*DeviceInfo.BPB_FATSz16+pBPB->BPB_RsvdSecCn[0];
DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+2*DeviceInfo.BPB_FATSz16+32;
///////////////////////////////////////////////////////
ThisFile.bFileOpen=0;
bXXGFlags.bits.SLAVE_IS_ATTACHED=1;
///////////////////////////////////////////////////////
return TRUE;
}
//***********************************************
unsigned char DetectDevice(void) //檢測設備
{
#define RspBlockDetectDevice CHRspBlock.RspBlock.Rsp_DetectDevice
return bXXGFlags.bits.SLAVE_IS_ATTACHED;
#undef RspBlockDetectDevice
}
unsigned char List(void) //列表文件
{
#define RspBlockList CHRspBlock.RspBlock.Rsp_List
unsigned int item,i;
unsigned char j,k,bstop,sector;
//RspBlockList.errcode=ERC_NODEVICE;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
CHRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
///////////////////////////////////////////////////////////
item=0;
bstop=0;
//RspBlockList.result=0x1;
for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
{
//////////////////////////////////////////////////
if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
{
//item=0;
CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
///////////////////////////////////////////////////
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if(DBUF[i]==0x00)
{bstop=1;
break;}
else if(DBUF[i]==0xE5)
continue;
else if((DBUF[i]&0x40==0x40)&&(DBUF[i+11]==0xff))
{
j=DBUF[i]&0x0F;
j=j+1;
for(k=0;k<j*32;k++)
CHBUF[item*32+k]=DBUF[i+k];
i=i+(j-1)*32;
item=item+j;
}
else
{
for(k=0;k<32;k++)
CHBUF[item*32+k]=DBUF[i+k];
item=item+1;
}
}
///////////////////////////////////////////////////////
if(bstop==1)break;
}
//pBuf=(PREAD_CAPACITY_RSP)DBUF;
///////////////////////////////////////////
RspBlockList.len=SwapINT16(item*32);
CHRspBlock.len=item*32;
return TRUE;
#undef RspBlockList
}
unsigned char OpenFile(unsigned char *pBuffer)
{
#define RspBlockOpenFile CHRspBlock.RspBlock.Rsp_OpenFile
unsigned int i;
unsigned char j,bstop,sector;
PDIR_INFO pDirInfo;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
CHRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
///////////////////////////////////////////////////////////
ThisFile.bFileOpen=0;
//RspBlockOpenFile.result=0x0;
//RspBlockOpenFile.errcode=ERC_OK;
for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
{
//////////////////////////////////////////////////
if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
{
CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
///////////////////////////////////////////////////
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if(DBUF[i]==0x00)
{
CHRspBlock.errcode=ERC_FILENOTFOUND;
//CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
///////////////////////////////////////////
j=0;
while(DBUF[i+j]==*(pBuffer+j))
{
j=j+1;
if(j>10)
break;
}
if(j>10)
{
for(j=0;j<32;j++)
RspBlockOpenFile.item[j]=DBUF[i+j];
//RspBlockOpenFile.result=0x1;
ThisFile.bFileOpen=1;
bstop=1;
break;}
}
///////////////////////////////////////////////////////
if(bstop==1)break;
///////////////////////////////////////////////////////
//if(DeviceInfo.BPB_RootEntCnt)
}
if(sector>=DeviceInfo.BPB_RootEntCnt)
{
CHRspBlock.errcode=ERC_FILENOTFOUND;
//CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
////////////////////////////////////////////
pDirInfo=(PDIR_INFO)RspBlockOpenFile.item;
ThisFile.StartCluster=SwapINT16(pDirInfo->startCluster);
ThisFile.LengthInByte=SwapINT32(pDirInfo->length);
ThisFile.ClusterPointer=ThisFile.StartCluster;
ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
ThisFile.OffsetofSector=0;
ThisFile.SectorofCluster=0;
//=ThisFatSecNum(clusterNum);
//xxgFatEntOffset=ThisFatEntOffset(clusterNum);
ThisFile.FatSectorPointer=0;
//ThisFile.bFileOpen=1;
ThisFile.pointer=0;
///////////////////////////////////////////
FreeFat.SectorNum=DeviceInfo.FatStartSector;
FreeFat.OldSectorNum=DeviceInfo.FatStartSector;
FreeFat.OffsetofSector=2;
return TRUE;
#undef RspBlockOpenFile
}
unsigned char ReadFile(unsigned int readLength,unsigned char *pBuffer)
{
#define RspBlockReadFile CHRspBlock.RspBlock.Rsp_ReadFile
unsigned int idata len,i;
unsigned char bSuccess;
//unsigned char sector;
//unsigned long lba;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
CHRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
if(!ThisFile.bFileOpen)
{
CHRspBlock.errcode=ERC_FILENOTOPENED;
return FALSE;
}
///////////////////////////////////////////////////////////
ThisFile.bFileOpen=0;
bSuccess=1;
CHRspBlock.len=0;
//lba=GetSecNumFromPointer();
//cluster=GetClusterNumFromSectorNum(lba);
//cluster=ThisFile.StartCluster;
//lba=FirstSectorofCluster(ThisFile.StartCluster);
//readLength=SwapINT16(CHCmdBlock.CmdBlock.Cmd_ReadFile.readLength);
if(readLength>MAX_READ_LENGTH)
{
CHRspBlock.errcode=ERC_LENGTHEXCEED;
return FALSE;
}
if(readLength+ThisFile.pointer>ThisFile.LengthInByte)
{
CHRspBlock.errcode=ERC_LENGTHEXCEED;
return FALSE;
}
////////////////////////////////////////////
///////////////////////////////////////////////////
while(readLength>0)
{
if(readLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
len=DeviceInfo.BPB_BytesPerSec;
else
len=readLength+ThisFile.OffsetofSector;
//////////////////////////////////////////////////////
if(ThisFile.OffsetofSector>0)
{
if(RBC_Read(ThisFile.SectorPointer,1,DBUF))
{
//ThisFile.OffsetofSector=len;
len=len-ThisFile.OffsetofSector;
for(i=0;i<len;i++)
//CHBUF[i]=DBUF[ThisFile.OffsetofSector+i];
*(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i];
ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
}
else
{
CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
}
else
{
if(!RBC_Read(ThisFile.SectorPointer,1,pBuffer+CHRspBlock.len))
{
CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
ThisFile.OffsetofSector=len;
}
////////////////////////////////////////////////////////////
// if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
// ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
readLength-=len;
CHRspBlock.len+=len;
// ThisFile.OffsetofSector=;
/////////////////////////////////////////////////////////
if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
{
ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
ThisFile.SectorofCluster+=1;
if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
{
ThisFile.SectorofCluster=0;
ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
if(ThisFile.ClusterPointer>0xffef)
{
//RspBlockReadFile.errcode=ERC_REACHEND;
//RspBlockReadFile.result=0x0;
CHRspBlock.errcode=ERC_REACHEND;
return FALSE;
}
ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);
}
else
ThisFile.SectorPointer=ThisFile.SectorPointer+1;
}
//////////////////////////////////////////////////////////////////
}//end while
ThisFile.bFileOpen=1;
ThisFile.pointer+=CHRspBlock.len;
//////////////////////////////////////////////
RspBlockReadFile.readLength=SwapINT16(CHRspBlock.len);
return TRUE;
#undef RspBlockReadFile
}
unsigned char CreateFile(unsigned char *pBuffer) //創建文件
{
#define RspBlockCreateFile CHRspBlock.RspBlock.Rsp_CreateFile
//unsigned long sectorNum;
unsigned int sector,i,j;
unsigned char bstop;
PDIR_INFO pDirInfo;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED) //檢測設備是否連入
{
CHRspBlock.errcode=ERC_NODEVICE; //否則返回錯誤
return FALSE;
}
///////////////////////////////////////////////////////////
if(OpenFile( pBuffer))RemoveFile(pBuffer);
//RspBlockCreateFile.result=0x1;
//RspBlockCreateFile.errcode=ERC_OK;
pDirInfo=(PDIR_INFO)pBuffer;
///////// Search the file of the same name ///////////
//CHCmdBlock.CmdBlock.Cmd_CreateFile.filename[j]
//if(!SPC_TestUnit())
// return FALSE;
//////// Search the fat for a free cluster ////////////
pDirInfo->startCluster=SwapINT16(GetFreeCusterNum());
if(pDirInfo->startCluster<0x2)
{
CHRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
pDirInfo->length=0;
/////// Search a free space in the root dir space and build the item ///
ThisFile.bFileOpen=0;
bstop=0;
for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
{
//////////////////////////////////////////////////
if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
{
CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
///////////////////////////////////////////////////
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
{
for(j=0;j<32;j++)
//DBUF[i+j]=CHCmdBlock.CmdBlock.Cmd_CreateFile.item[j];
DBUF[i+j]=*(pBuffer+j);
if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
{
CHRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
bstop=1;
break;
}
}
///////////////////////////////////////////////////////
if(bstop==1)break;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -