📄 imgboot.c
字号:
if(pulImageAddress)
{
*pulImageAddress = BinFileHeader.ImageAddress;
}
StartAddress = BinFileHeader.ImageAddress;
ImageLength = BinFileHeader.ImageLength;
// ****************************************************************************
// * Sweep through the BIN file records and load the image.
// * Print a series of '.' to show the user that progress is being made.
// *
// * Read the record header block.
// * Update our 'bytes read' counter.
// *
// * Set the destination address from the record header.
// * Note that we assume that this is a RAM address and that we're not
// * going to step on the location that we're running from right now (in
// * cases where the loader is copied to RAM and executed from there).
// * If the address is not RAM or if we stomp on ourself, the program will
// * hang, but not destroy anything.
// *
// ****************************************************************************
BytesProcessed = sizeof(BINFILE_HEADER);
while(BytesProcessed < (FileSize - sizeof(BINFILE_RECORD_HEADER)))
{
OALMSG(1, ( L"."));
if ((count%50)==0) RETAILMSG(1, ( L"\r\n"));
count++;
BytesToRead = sizeof(BINFILE_RECORD_HEADER);
BytesRead =FATReadFile(&fnFileInfo, (PUCHAR)&BinRecordHeader, BytesToRead);
if (BytesRead != BytesToRead)
{
OALMSG(1, (L"FATReadBin: Failed to read a BIN record header.\r\n"));
return FALSE;
}
BytesToRead = BinRecordHeader.Length;
Destination = BinRecordHeader.LoadAddress;
// ****************************************************************************
// * Retrieve the data from the file.
// * Perform the checksum verification.
// * Increment our byte counter.
// * Loop back to the top to get another record header and its data.
// *
// ****************************************************************************
DEBUGMSG(1, (L"FATReadBin: Record Address = : %8.8X\r\n", Destination));
DEBUGMSG(1, (L"FATReadBin: Record Length = : %8.8X\r\n", BytesToRead));
retry = 0;
do
{
BytesRead =FATReadFile(&fnFileInfo, (PUCHAR)Destination, BytesToRead);
if (BytesRead != BytesToRead)
{
OALMSG(1, (L"FATReadBin: Failed to read a record. %x\r\n",BytesRead));
return FALSE;
}
CheckSum = 0;
for (pData = (PUCHAR)Destination;
pData < (PUCHAR)Destination + BytesRead;
pData++)
{
CheckSum += *pData;
}
if ((ULONG)CheckSum != BinRecordHeader.CheckSum)
{
if (retry == 3)
{
OALMSG(1, (L"FATReadBin: ERROR: Record checksum failure. Aborting.\r\n"));
return FALSE;
}
else
{
retry++;
OALMSG(1, (L"FATReadBin: ERROR: Record checksum failure. Retrying. %d\r\n",retry));
OALMSG(1, (L"FATReadBin: calc %8.8X rec %8.8X\r\n",(ULONG)CheckSum,BinRecordHeader.CheckSum));
}
}
else
break;
} while (retry);
BytesProcessed += BytesRead + sizeof(BINFILE_RECORD_HEADER);
} //while bytesprocessed < filesize - binfile_header_size
// ****************************************************************************
// * Retrieve the termination record which contains the jump address.
// * Update the jump address.
// * Close the file.
// *
// *
// ****************************************************************************
DEBUGMSG(1, (L"\r\nProcess the termination record which contains the jump address\r\n"));
BytesToRead = sizeof(BINFILE_RECORD_HEADER);
BytesRead =FATReadFile(&fnFileInfo, (PUCHAR)&BinRecordHeader, BytesToRead);
if (BytesRead != BytesToRead)
{
OALMSG(1, (L"FATReadBin: Failed to read termination record.\r\n"));
return FALSE;
}
if ((BinRecordHeader.LoadAddress != 0) || (BinRecordHeader.CheckSum != 0))
{
OALMSG(1, (L"FATReadBin: WARNING: Termination record invalid format.\r\n"));
}
*JumpAddress = BinRecordHeader.Length;
*pulSize = BinFileHeader.ImageLength;
OALMSG(1, (L"\r\nINFO: Copied BIN file from card to RAM \r\n"));
FATCloseFile(&fnFileInfo);
DEBUGMSG(1, (L"FATReadBin: Exit\r\n"));
return TRUE;
}
ULONG ReadSectors(
IN ULONG Sector,
IN ULONG Count,
OUT PUCHAR SectorBuf
)
{
ULONG status = ATA_ERROR_GENERAL;
//DEBUGMSG(1, (L"ReadSectors: Sec %x Count %x Buf %x\r\n",Sector,Count,SectorBuf));
switch (BootDisk.DeviceType) {
case DEVICE_TYPE_CF:
status = CFReadSectors(Sector, Count, SectorBuf);
break;
case DEVICE_TYPE_MMC:
if (mmc_read(Sector, (USHORT)Count, SectorBuf))
status = 0;
break;
}
return status;
}
void CardBoot(void)
{
ULONG ulJumpAddr; //The image boot address.
ULONG ulImageSize; //The image size.
ULONG ulImageAddr; //The image address.
if (FATReadBin("nk.bin", &ulJumpAddr, &ulImageSize, &ulImageAddr))
{
OALMSG(1, (L"Start CE at 0x%X\r\n", ulJumpAddr));
msWait(100);
OEMLaunch(ulImageAddr, ulImageSize, ulJumpAddr, NULL);
}
}
static BOOL VerifyMemoryCFCard(BOOL slot0)
{
UINT32 status;
UINT8 Attribute;
UINT8 *pAttribute;
UINT8 *pLastAttribute;
if (slot0)
pAttribute = (UINT8 *) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S0_ATTR, FALSE);
else
pAttribute = (UINT8 *) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S1_ATTR, FALSE);
// End of the slot attribute list.
pLastAttribute = pAttribute + 0x200;
Attribute = *pAttribute;
if (Attribute != (UINT8) 0x01)
{
EdbgOutputDebugString("ERROR: Invalid slot configuration data 0x%x.\r\n", Attribute);
return FALSE;
}
// Look for tuple.
//
while ((Attribute != (UINT8) 0x21) && (pAttribute < pLastAttribute))
{
pAttribute += sizeof(UINT16); // Step to tuple length.
Attribute = *pAttribute;
pAttribute += sizeof(UINT16) + (sizeof(UINT16) * Attribute); // Step over tuple.
Attribute = *pAttribute;
}
// Find a valid tuple?
//
if (pAttribute < pLastAttribute)
{
pAttribute += sizeof(UINT16); // Defined tuple length.
if ((Attribute = *pAttribute) == 2)
{
pAttribute += sizeof(UINT16);
Attribute = *pAttribute;
// Found a Memory card...
//
if (Attribute == 0x04)
{
*pLastAttribute = 0x42; // Configuration Option Register
BootDisk.DeviceType = DEVICE_TYPE_CF;
if (slot0)
{
BootDisk.CmdBase = (UINT32) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S0_IO + 0x1f0, FALSE);
}
else
{
BootDisk.CmdBase = (UINT32) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S1_IO + 0x1f0, FALSE);
}
BootDisk.BytesPerSec = 512; // Default to minimum
status = CFIdend();
if (status)
{
OALMSG(1, (L"ERROR: CFIdent fail. 0x%8.8X\r\n", status));
return FALSE;
}
return TRUE;
}
}
}
return (FALSE);
}
static BOOL LocateMMCCard( void )
{
volatile GPIO_REG_T *pGPIORegs = (volatile GPIO_REG_T *) OALPAtoVA(PXA255_BASE_REG_PA_GPIO, FALSE);
// Initialize the MMC interface if a card is detected.
//
// if (!((pGPIORegs->GPLR0 & GPIO_22_nCDMMC) == GPIO_22_nCDMMC) || !((pGPIORegs->GPLR1 & GPIO_54_nCDMMC) == GPIO_54_nCDMMC))
// Note: Card detection is not consistant across all permutations of hardware ... just try to read it
if ( TRUE )
{
EdbgOutputDebugString("INFO: Performing MMC slot initialization.\r\n");
if (mmc_init() && mmc_drive_open())
{
return(TRUE);
}
}
EdbgOutputDebugString("INFO: No card detected.\r\n");
return(FALSE);
}
static BOOL VerifyMemoryMMCCard( void )
{
DRV_GEOMETRY_DESC DriveGeometry;
if (!mmc_read_serial(&DriveGeometry))
{
EdbgOutputDebugString("ERROR: Invalid drive configuration data.\r\n");
return FALSE;
}
BootDisk.DeviceType = DEVICE_TYPE_MMC;
BootDisk.BytesPerSec = 512; // Default to minimum
return TRUE;
}
BOOL InitSpecifiedImgBootDevice(UINT32 ImgBootDevice, UINT32 dwPlatformHardware)
{
BOOL ret = FALSE;
UINT32 EbootDeviceAddress = 0;
UINT8 string[10];
// Determine the image boot device
//
switch (ImgBootDevice)
{
case IMG_LOAD_CF:
if (!(dwPlatformHardware & GUMCFG_CF))
{
EdbgOutputDebugString("INFO: CF hardware not defined!\r\n");
break;
}
EdbgOutputDebugString("INFO: Trying to locate/initialize CF Memory card in slot 0...\r\n");
if (VerifyMemoryCFCard(TRUE) && FATInitDisk())
{
GetFileSystemType(string);
EdbgOutputDebugString("INFO: Found valid %s CF Memory card in slot 0!\r\n",string);
CardBoot(); // should never return ... if everything works
}
EdbgOutputDebugString("INFO: Trying to locate/initialize CF Memory card in slot 1...\r\n");
if (VerifyMemoryCFCard(FALSE) && FATInitDisk())
{
GetFileSystemType(string);
EdbgOutputDebugString("INFO: Found valid %s CF Memory card in slot 1!\r\n",string);
CardBoot(); // should never return ... if everything works
}
ret = FALSE; // didn't make it
break;
case IMG_LOAD_MMC:
if (!(dwPlatformHardware & GUMCFG_MMC))
{
EdbgOutputDebugString("INFO: MMC hardware not defined!\r\n");
break;
}
EdbgOutputDebugString("INFO: Trying to locate/initialize MMC/SD Memory card...\r\n");
if (LocateMMCCard() && VerifyMemoryMMCCard() && FATInitDisk())
{
GetFileSystemType(string);
EdbgOutputDebugString("INFO: Found valid %s MMC/SD Memory card!\r\n",string);
CardBoot(); // should never return ... if everything works
}
ret = FALSE; // didn't make it
break;
case IMG_LOAD_FLASH:
ret = TRUE;
break;
default: // flash
EdbgOutputDebugString("INFO: Invalid image load device\r\n");
break;
}
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -