📄 fat.c
字号:
if(flag == 1)
{
pDat = DataBuf;
for(j=0;j<Disk->SecPerCluster;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++);
}
}
}
BytesRead += ClusterSize;
DataBuf += ClusterSize;
File->Cluster = FATGetNextCluster(Disk, File->Cluster);
File->Position += ClusterSize;
}
else
{
EdbgOutputDebugString ("FATFileRead: Exit(5).\r\n");
return(BytesRead);
}
}
}
//EdbgOutputDebugString ("FATFileRead: Exit(6).\r\n");
return(BytesRead);
}
BOOL FATReadBin(IN PUCHAR FileName,
OUT PULONG JumpAddress,
OUT PULONG pulSize,
OUT PULONG pulImageAddress)
{
BINFILE_HEADER BinFileHeader; //It's just what it say's it is
BINFILE_RECORD_HEADER BinRecordHeader;
ULONG FatFileHandle; //Handle to the input filename.
ULONG Destination;
ULONG BytesToRead;
ULONG BytesRead;
ULONG BytesProcessed;
ULONG FileSize;
LONG CheckSum;
PUCHAR pData;
int i=0;
int loops=0;
LONG CheckSumTemp;
ULONG StartAddress;
ULONG ImageLength;
// PUCHAR pucTmp;
// ULONG j;
//flag=0;
PrintMsg(1, (TEXT("CFFATReadBin: Entry\r\n")));
FatFileHandle = FATFileOpen(FileName); //首先打开文件
if (FatFileHandle == 0)
{
EdbgOutputDebugString ("CFFATReadBin: FatFileHandle == 0\r\n");
return FALSE;
}
EdbgOutputDebugString ("Find %s in this card, will boot from it!\r\n\r\n",FileName);
EdbgOutputDebugString ("Copy BIN file from CF ATA to RAM. Please wait...\r\n");
FileSize = FATFileSize(FatFileHandle); //读取文件大小
EdbgOutputDebugString ("FATReadBin: BIN file size: %u bytes\r\n",FileSize);
// flag=1;
if(FileSize < sizeof(BINFILE_HEADER) + 2*sizeof(BINFILE_RECORD_HEADER))//文件大小不能小于1个header,两个record
{
EdbgOutputDebugString ("FATReadBin: BIN file size: %u bytes is too small.\r\n",FileSize);
FATFileClose(FatFileHandle);
return FALSE;
}
//------------------------------------------------------------------------------------------------------
#if 0
if(FATFileRead(FatFileHandle, FileSize, (PUCHAR)&BinFileHeader)==-1)
{
*pulImageAddress = (ULONG)ImageBufStart;
*pulSize = FileSize;
if(*pulSize%4!=0)
*pulSize = *pulSize+4-*pulSize%4;
return 1;
}
else return 0;
#endif
//--------------------------------------------------------------------------------------------------------
EdbgOutputDebugString ("\r\nCFFATReadBin: Read the BIN file header\r\n");
BytesToRead = sizeof(BINFILE_HEADER);//首先要读bin文件的header
//BytesToRead = 15;
BytesRead = FATFileRead(FatFileHandle, BytesToRead, (PUCHAR)&BinFileHeader);
if (BytesRead != BytesToRead)
{
EdbgOutputDebugString ("FATReadBin: Failed to read the BIN file header.\r\n");
FATFileClose(FatFileHandle);
return FALSE;
}
#if 0
pucTmp = (PUCHAR)(&BinFileHeader);
PrintMsg(1, (TEXT("\r\nCFFATReadBin: BINFILE_HEADER data start:\r\n")));
EdbgOutputDebugString ("\r\nFATReadBin: BINFILE_HEADER data start:\r\n");
for(j=0; j < BytesRead; j++)
{
PrintMsg(1, (TEXT("[%X]"), *(pucTmp+j)));
EdbgOutputDebugString ("[%X]",*(pucTmp+j));
}
PrintMsg(1, (TEXT("\r\nCFFATReadBin: BINFILE_HEADER data end:\r\n\r\n")));
EdbgOutputDebugString ("\r\nFATReadBin: BINFILE_HEADER data end:\r\n\r\n");
#endif
EdbgOutputDebugString ("Image Address = 0x%X",BinFileHeader.ImageAddress);
EdbgOutputDebugString ("Image Size = 0x%X\r\n",BinFileHeader.ImageLength);
if(pulImageAddress)
{
*pulImageAddress = BinFileHeader.ImageAddress+(DWORD)ImageBufStart-BinFileHeader.ImageAddress;
}
StartAddress = BinFileHeader.ImageAddress;
ImageLength = BinFileHeader.ImageLength;
Destination = (DWORD)ImageBufStart;
memset((PUCHAR)Destination,ImageLength,0);
BytesProcessed = sizeof(BINFILE_HEADER);
//BytesProcessed = 15;
while(BytesProcessed < (FileSize - sizeof(BINFILE_RECORD_HEADER)))
{
RETAILMSG(1, ( L"."));
EdbgOutputDebugString (".");
BytesToRead = sizeof(BINFILE_RECORD_HEADER);
BytesRead = FATFileRead(FatFileHandle, BytesToRead, (PUCHAR)&BinRecordHeader);
if(BytesRead != BytesToRead)
{
EdbgOutputDebugString ("FATReadBin: Failed to read a BIN record header.\r\n");
PrintMsg(1, (TEXT("FATReadBin: Failed to read a BIN record header.\r\n")));
FATFileClose(FatFileHandle);
return FALSE;
}
#if 0
pucTmp = (PUCHAR)(&BinRecordHeader);
PrintMsg(1, (TEXT("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data start:\r\n")));
EdbgOutputDebugString ("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data start:\r\n");
for(j=0; j < BytesRead; j++)
{
PrintMsg(1, (TEXT("[%X]"), *(pucTmp+j)));
EdbgOutputDebugString ("[%X]",*(pucTmp+j));
}
PrintMsg(1, (TEXT("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data end:\r\n\r\n")));
EdbgOutputDebugString ("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data end:\r\n");
#endif
BytesToRead = BinRecordHeader.Length;
Destination = BinRecordHeader.LoadAddress+(DWORD)ImageBufStart-StartAddress;
//debug------------------------------------------------------------------------------------------
#if 0
if(Destination == 0x988D64C8 || Destination == 0x988D651C)
{
EdbgOutputDebugString ("FATReadBin: Record Address : %X\r\n",Destination);
EdbgOutputDebugString ("FATReadBin: Record Length : %X\r\n",BytesToRead);
EdbgOutputDebugString ("FATReadBin: BinRecordHeader.CheckSum : %X\r\n",BinRecordHeader.CheckSum);
pucTmp = (PUCHAR)(&BinRecordHeader);
PrintMsg(1, (TEXT("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data start:\r\n")));
EdbgOutputDebugString ("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data start:\r\n");
for(j=0; j < BytesRead; j++)
{
PrintMsg(1, (TEXT("[%X]"), *(pucTmp+j)));
EdbgOutputDebugString ("[%X]",*(pucTmp+j));
}
PrintMsg(1, (TEXT("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data end:\r\n\r\n")));
EdbgOutputDebugString ("\r\nCFFATReadBin: BINFILE_RECORD_HEADER data end:\r\n");
}
#endif
//-----------------------------------------------------------------------------------------------
//EdbgOutputDebugString ("FATReadBin: LoadRecord Address: : %X\r\n",BinRecordHeader.LoadAddress);
//EdbgOutputDebugString ("FATReadBin: ImageBufStart: : %X\r\n",ImageBufStart);
//EdbgOutputDebugString ("FATReadBin: StartAddress: : %X\r\n",StartAddress);
//EdbgOutputDebugString ("FATReadBin: Record Address: : %X\r\n",Destination);
//EdbgOutputDebugString ("FATReadBin: Record Length: : %X\r\n",BytesToRead);
BytesRead = FATFileRead(FatFileHandle, BytesToRead, (PUCHAR)Destination);
if(BytesRead != BytesToRead)
{
EdbgOutputDebugString ("FATReadBin: Failed to read a record.\r\n");
FATFileClose(FatFileHandle);
return FALSE;
}
CheckSum = 0;
CheckSumTemp = BytesRead;
pData = (PUCHAR)Destination;
//PrintMsg(1, (TEXT("CFFATReadBin: BytesRead: 0x%x\r\n"),BytesRead));
//PrintMsg(1, (TEXT("CFFATReadBin: pData: 0x%x\r\n"),pData));
while(CheckSumTemp)
{
//PrintMsg(1, (TEXT("CFFATReadBin: 0x%x\r\n"),*pData));
CheckSum += *pData++;
CheckSumTemp--;
}
if((ULONG)CheckSum != BinRecordHeader.CheckSum)
{
CheckSumTemp = 0x100;
pData = (PUCHAR)Destination;
while(CheckSumTemp)
{
EdbgOutputDebugString ("0x%x ",*pData++);
CheckSumTemp--;
}
EdbgOutputDebugString ("\r\nRecord Address=0x%x, Record Length=0x%x",Destination, BytesToRead);
EdbgOutputDebugString ("\r\nFATReadBin: ERROR: Record checksum failure. Aborting.CheckSum=0x%x,BinRecordHeader.CheckSum=0x%x\r\n",CheckSum,BinRecordHeader.CheckSum);
FATFileClose(FatFileHandle);
return FALSE;
}
BytesProcessed += BytesRead + sizeof(BINFILE_RECORD_HEADER);
}
EdbgOutputDebugString ("\r\nProcess the termination record which contains the jump address\r\n");
BytesToRead = sizeof(BINFILE_RECORD_HEADER);
BytesRead = FATFileRead(FatFileHandle, BytesToRead, (PUCHAR)&BinRecordHeader);
if(BytesRead != BytesToRead)
{
EdbgOutputDebugString ("FFATReadBin: Failed to read termination record.\r\n");
FATFileClose(FatFileHandle);
return FALSE;
}
if((BinRecordHeader.LoadAddress != 0) || (BinRecordHeader.CheckSum != 0))
{
EdbgOutputDebugString ("FATReadBin: WARNING: Termination record invalid format.\r\n");
}
*JumpAddress = BinRecordHeader.Length;
*pulSize = BinFileHeader.ImageLength;
FATFileClose(FatFileHandle );
EdbgOutputDebugString ("\r\nCopy BIN file from ATA to RAM -- DONE!\r\n");
return TRUE;
}
void Boot(BOOL bWriteFlash,PULONG dwImageAddr,PULONG dwImageSize)
{
ULONG ulJumpAddr;
ULONG ulImageSize;
ULONG ulImageAddr;
UINT ulStatus;
//PDRIVER_GLOBALS pDriverGlobals = NULL;
EdbgOutputDebugString ("CFBoot: Search for 'NK.BIN' on SD card.\r\n");
//pDriverGlobals = (PDRIVER_GLOBALS)DRIVER_GLOBALS_PHYSICAL_MEMORY_START;
if(SetSDHW())
{
ulStatus = ATADeviceInit();
if(FATReadBin("nk.nb0",&ulJumpAddr, &ulImageSize, &ulImageAddr))
if(!FlashWrite((EBOOT_FLASH_CFG_START+0x40000),(PUCHAR)ulImageAddr,ulImageSize))
{
EdbgOutputDebugString("Flash programming Error. System halted!\r\n");
while (1);
}
if(FATReadBin("nk.bin", &ulJumpAddr, &ulImageSize, &ulImageAddr))
{
EdbgOutputDebugString ("CFBoot: Sucessed.\r\n");
if (*(DWORD*)(ulImageAddr + 64) == 0x43454345)
{
EdbgOutputDebugString("Found pTOC signature in downloaded image.\n");
}
SDPowerOff();
EdbgOutputDebugString ("ulImageAddr :0x%x\r\n",ulImageAddr);
EdbgOutputDebugString ("ulImageSize :0x%x\r\n",ulImageSize);
*dwImageAddr = ulImageAddr;
*dwImageSize = ulImageSize;
if(bWriteFlash)
{
if(!FlashWrite((EBOOT_FLASH_CFG_START+0x40000),(PUCHAR)ulImageAddr,ulImageSize))
{
EdbgOutputDebugString("Flash programming Error. System halted!\r\n");
while (1);
}
else
EdbgOutputDebugString("Download Complete,Please reset!\r\n");
}
else
{
ulImageAddr = (UINT32) OALVAtoPA((VOID *)ulImageAddr);
EdbgOutputDebugString ("*ulImageAddr :0x%x\r\n",*(DWORD*)ulImageAddr);
Launch(ulImageAddr);
}
//ulImageSize = 0x2000000-0x80000;
}
else
EdbgOutputDebugString ("CFBoot: Failed.\r\n");
}
else
EdbgOutputDebugString ("SD card missed.\r\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -