📄 fat.c
字号:
return(BadSector);
}
Disk->FATBuf = databuf;
}
FatPtr = (PUCHAR)Disk->FATBuf; // Pointer to the device FAT
// FatOffs = (Cluster * 2);
FatOffs = (Cluster * 2)%512;
FatEntry = FatPtr[FatOffs] + (FatPtr[FatOffs+1] << 8);
// free(databuf);
// databuf = NULL;
// EdbgOutputDebugString ("FATGetNextCluster: Exit.\r\n");
return(FatEntry);
}
ULONG FATFileRead( ULONG FileDesc,
ULONG Length,
PUCHAR DataBuf)
{
struct _FileTable *File;
ATA_DEVICE *Disk;
ULONG Sector;
ULONG ClusterSize;
ULONG DataArea;
ULONG BytesRead;
ULONG SecNum;
ULONG SecOffset;
ULONG CopyLen;
ULONG i,j;
BYTE* pImage;
ULONG count=0;
char selection;
BOOL flag = 0;
PUCHAR pDat;
//ImageBufStart = SectorBuf;
FileDesc -= 1; //fileopen的时候这个参数+1了
//EdbgOutputDebugString ("FATFileRead: Entry.\r\n");
if(FileDesc >= NUM_FILES || !FileTable[FileDesc].Opened) //检查文件是否打开
{
EdbgOutputDebugString ("FATFileRead: Exit(1).\r\n");
return(0);
}
File = &FileTable[FileDesc];
Disk = AtaDisks;
ClusterSize = Disk->BytesPerSec * Disk->SecPerCluster;//一簇的字节数
DataArea = 1; //这里要跳转到数据区,要跳转的包括1DBR,2个fat,以及根目录项目数(ResvSectors)
DataArea += (Disk->NumFATs*Disk->SecPerFAT);
DataArea += ((Disk->DirEntries * sizeof(DIRENTRY)) / Disk->BytesPerSec);
DataArea = DirSector+32;
BytesRead = 0;
SecNum = 0;
pImage = ImageBufStart;
//--------------------------------------------------------------------------------------------------------
#if 0
while(count<(Length/Disk->BytesPerSec+1))
{
Sector = DataArea + ((File->Cluster-2)*Disk->SecPerCluster);//跳转到文件的起始数据区
if(1)//如果偏移量为0,说明读取的数据小于1扇区,直接读取1扇区数据
{
if (CFATAReadSectors(Sector, 1, File->SecBuf,PAtoLA) != 0)
{
EdbgOutputDebugString ("FATFileRead: Exit(2).\r\n");
return(0);//返回的数据只能是需要读取的而不是实际读取的
}
}
//EdbgOutputDebugString ("Memory Copy(1).\r\n");
for(i=0;i<512;i++)
{
*pImage = File->SecBuf[i];
// EdbgOutputDebugString("[%x]",*DataBuf);
if(*pImage != File->SecBuf[i])
{
EdbgOutputDebugString("\r\n*pImage : 0x%x\r\n",*pImage);
EdbgOutputDebugString("\r\nFile->SecBuf[i] : 0x%x\r\n",File->SecBuf[i]);
EdbgOutputDebugString("\r\nMemory Copy Failed at : 0x%x\r\n",pImage);
return 0;
}
pImage++;
}
//memcpy(pImage,File->SecBuf,512);
//EdbgOutputDebugString ("Memory Copy(2).\r\n");
//pImage+=512;
Sector++;
SecNum++;
count++;
if(SecNum == Disk->SecPerCluster)
{
SecNum = 0;
File->Cluster = FATGetNextCluster(Disk, File->Cluster);//读取1簇数据,且调整簇的位置
Sector = (DataArea) + ((File->Cluster-2) * Disk->SecPerCluster);
EdbgOutputDebugString (".");
}
}
return(-1);
#endif
//---------------------------------------------------------------------------------------------------------
//EdbgOutputDebugString ("File->Position :0x%x\r\n",File->Position);
//------------------------------------------------------------just debug
#if 0
CFATAReadSectors(0x2000, 1, File->SecBuf,PAtoLA);
pDat = File->SecBuf;
for(i=0;i<512;i++)
{
if(!(i%16))
EdbgOutputDebugString ("\r\n%x: ",i);
if(*pDat<0x10)
EdbgOutputDebugString ("0%x ",*pDat++);
else
EdbgOutputDebugString ("%x ",*pDat++);
}
return 0;
#endif
//------------------------------------------------------------------------
while(BytesRead < Length)//如果读取的字节数小于要读取的,需要循环读
{
SecOffset = File->Position & (Disk->BytesPerSec-1);//读取的数据在扇区里的偏移量
SecNum = (File->Position & (ClusterSize-1)) / Disk->BytesPerSec;//需要读取的扇区
Sector = DataArea + ((File->Cluster-2)*Disk->SecPerCluster);//跳转到文件的起始数据区
Sector += SecNum;
if(flag==1)
{
EdbgOutputDebugString ("File->Position :0x%x\r\n",File->Position);
EdbgOutputDebugString ("File->Cluster :0x%x\r\n",File->Cluster);
}
while(SecOffset != 0 || ((Length-BytesRead) < Disk->BytesPerSec))//当扇区的偏移量不为0或者需要取的数据小于1扇区时循环读取
{
//EdbgOutputDebugString ("FATFileRead: BytesRead: 0x%X\r\n",BytesRead);
//EdbgOutputDebugString ("FATFileRead: SecNum: 0x%X\r\n",SecNum);
//EdbgOutputDebugString ("FATFileRead: Sector: 0x%X\r\n",Sector);
//EdbgOutputDebugString ("FATFileRead: SecOffset: 0x%X\r\n",SecOffset);
//EdbgOutputDebugString ("FATFileRead: DataArea: 0x%X\r\n",DataArea);
//EdbgOutputDebugString ("FATFileRead: DataBuf: 0x%X\r\n",DataBuf);
if(SecOffset == 0)//如果偏移量为0,说明读取的数据小于1扇区,直接读取1扇区数据
{
if (CFATAReadSectors(Sector, 1, File->SecBuf,PAtoLA) != 0)
{
EdbgOutputDebugString ("FATFileRead: Exit(2).\r\n");
return(BytesRead);//返回的数据只能是需要读取的而不是实际读取的
}
}
// for(i=0;i<512;i++)
// EdbgOutputDebugString ("[0x%x]",File->SecBuf[i]);
CopyLen = min(Length-BytesRead, (Disk->BytesPerSec - SecOffset));//取其中小的,防止多拷贝数据
//EdbgOutputDebugString ("FATFileRead(memcpy): SectorBuf : 0x%X\r\n",SectorBuf);
//EdbgOutputDebugString ("FATFileRead(memcpy): &(File->SecBuf[SecOffset]): 0x%X\r\n",&(File->SecBuf[SecOffset]));
//EdbgOutputDebugString ("FATFileRead(memcpy): CopyLen : 0x%X\r\n",CopyLen);
//EdbgOutputDebugString ("\r\nFATFileRead(memcpy): DataBuf: 0x%X\r\n",DataBuf);
//memset(DataBuf,0,CopyLen);
//for(i=SecOffset;i<CopyLen+SecOffset;i++)
// EdbgOutputDebugString("[%x]",File->SecBuf[i]);
//EdbgOutputDebugString("\r\n");
/*for(i=SecOffset;i<CopyLen+SecOffset;i++)
{
*DataBuf = File->SecBuf[i];
EdbgOutputDebugString("[%x]",*DataBuf);
if(*DataBuf != File->SecBuf[i])
{
EdbgOutputDebugString("\r\n*DataBuf : 0x%x\r\n",*DataBuf);
EdbgOutputDebugString("\r\nFile->SecBuf[i] : 0x%x\r\n",File->SecBuf[i]);
EdbgOutputDebugString("\r\nMemory Copy Failed at : 0x%x\r\n",DataBuf);
return 0;
}
DataBuf++;
}*/
memcpy(DataBuf, &(File->SecBuf[SecOffset]), CopyLen);//拷贝数据,之所以在这里拷贝,是因为数据早在上一次读取时就读出了
if(flag == 1)
{
pDat = DataBuf;
for(i=0;i<CopyLen;i++)
{
if(!(i%16))
EdbgOutputDebugString ("\r\n%X: ",pDat);
if(*pDat<0x10)
EdbgOutputDebugString ("0%x ",*pDat++);
else
EdbgOutputDebugString ("%x ",*pDat++);
}
}
// EdbgOutputDebugString ("\r\nFATFileRead(memcpy): CopyLen: 0x%X\r\n",CopyLen);
if(flag == 1)
{
while(1)
{
selection = OEMReadDebugByte();
if(selection == 'a')
break;
}
}
File->Position += CopyLen;//文件位置增加
//偏移量不为0,这里需要进一步判断是否大于1簇
if(!(File->Position & (ClusterSize-1)))//如果文件的位置跨簇
{
File->Cluster = FATGetNextCluster(Disk, File->Cluster);//读取1簇数据,且调整簇的位置
Sector = (DataArea) + ((File->Cluster-2) * Disk->SecPerCluster);
SecNum = 0;
}
else
{
Sector += 1;
SecNum += 1;
}
SecOffset = 0;
DataBuf += CopyLen;
BytesRead += CopyLen;
if(flag == 1)
{
EdbgOutputDebugString ("\r\nFATFileRead: CopyLen(1): 0x%X\r\n", CopyLen);
EdbgOutputDebugString ("\r\nFATFileRead: SecOffset(1): 0x%X\r\n", SecOffset);
EdbgOutputDebugString ("\r\nFATFileRead: DataBuf(1): 0x%X\r\n", DataBuf);
EdbgOutputDebugString ("\r\nFATFileRead: BytesRead(1): 0x%X\r\n", BytesRead);
}
if(BytesRead == Length)
{
//EdbgOutputDebugString ("FATFileRead: Exit(3).\r\n");
return(BytesRead);
}
}
if( SecNum > 0 || ((Length-BytesRead) < ClusterSize) ) //到这里应该是sector偏移量为0,数据大于1扇区,
{
CopyLen = (Disk->SecPerCluster-SecNum) * Disk->BytesPerSec;//这里为什么要-1??因为这段不是读完整的一簇,第一扇区的一部分在上面已经读出了。
if(CopyLen > (Length-BytesRead))
{
CopyLen = Length-BytesRead; //这里需要读取的数据可能没有满簇
}
if(flag == 1)
{
EdbgOutputDebugString ("\r\nFATFileRead: CopyLen(2): 0x%X\r\n", CopyLen);
EdbgOutputDebugString ("\r\nFATFileRead: SecOffset(2): 0x%X\r\n", SecOffset);
EdbgOutputDebugString ("\r\nFATFileRead: DataBuf(2): 0x%X\r\n", DataBuf);
EdbgOutputDebugString ("\r\nFATFileRead: BytesRead(2): 0x%X\r\n", BytesRead);
}
CopyLen &= ~(Disk->BytesPerSec-1);//如果需要读取的数据没有满簇,这里把它与扇区对齐
if(CFATAReadSectors(Sector, CopyLen/Disk->BytesPerSec, DataBuf,PAtoLA) != 0 )
{
EdbgOutputDebugString ("FATFileRead: Exit(4).\r\n");
return(BytesRead);
}
if(flag == 1)
{
pDat = DataBuf;
for(j=0;j<CopyLen/Disk->BytesPerSec;j++)
{
while(1)
{
selection = OEMReadDebugByte();
if(selection == 'a')
break;
}
for(i=0;i<512;i++)
{
if(!(i%16))
EdbgOutputDebugString ("\r\n%X: ",pDat);
if(*pDat<0x10)
EdbgOutputDebugString ("0%x ",*pDat++);
else
EdbgOutputDebugString ("%x ",*pDat++);
}
}
}
File->Position += CopyLen;
if(!(File->Position & (ClusterSize-1))) //到这里已经读完一簇的内容
{
File->Cluster = FATGetNextCluster(Disk, File->Cluster);
}
DataBuf += CopyLen;
BytesRead += CopyLen;
if(flag == 1)
{
EdbgOutputDebugString ("\r\nFATFileRead: CopyLen(3): 0x%X\r\n", CopyLen);
EdbgOutputDebugString ("\r\nFATFileRead: DataBuf(3): 0x%X\r\n", DataBuf);
EdbgOutputDebugString ("\r\nFATFileRead: BytesRead(3): 0x%X\r\n", BytesRead);
}
}
if(flag == 1)
{
while(1)
{
selection = OEMReadDebugByte();
if(selection == 'a')
break;
}
}
while((Length-BytesRead) >= ClusterSize)
{
Sector = DataArea + ((File->Cluster-2)*Disk->SecPerCluster);
if(flag == 1)
{
EdbgOutputDebugString ("\r\nFATFileRead: Position(4): 0x%X\r\n", File->Position);
EdbgOutputDebugString ("\r\nFATFileRead: Cluster(4): 0x%X\r\n", File->Cluster);
EdbgOutputDebugString ("\r\nFATFileRead: BytesRead(4): 0x%X\r\n", BytesRead);
EdbgOutputDebugString ("\r\nFATFileRead: Sector: 0x%X\r\n", Sector);
}
if (CFATAReadSectors(Sector,Disk->SecPerCluster, DataBuf,PAtoLA) == ATA_ERR_NONE)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -