/*You can find the function in Shell LoadPciRom.c*/
EFI_STATUS
LoadEfiDriversFromRomImage (
VOID *RomBar,
UINTN RomSize,
CHAR16 *FileName
)
/*++
Routine Description:
Command entry point.
Arguments:
RomBar - Rom
RomSize - Rom size
FileName - The file name
Returns:
EFI_SUCCESS - The command completed successfully
EFI_INVALID_PARAMETER - Command usage error
EFI_UNSUPPORTED - Protocols unsupported
EFI_OUT_OF_RESOURCES - Out of memory
Other value - Unknown error
--*/
{
EFI_PCI_EXPANSION_ROM_HEADER *EfiRomHeader;
PCI_DATA_STRUCTURE *Pcir;
UINTN ImageIndex;
UINTN RomBarOffset;
UINT32 ImageSize;
UINT16 ImageOffset;
EFI_HANDLE ImageHandle;
EFI_STATUS Status;
EFI_STATUS retStatus;
CHAR16 RomFileName[280];
EFI_DEVICE_PATH_PROTOCOL *FilePath;
BOOLEAN SkipImage;
UINT32 DestinationSize;
UINT32 ScratchSize;
UINT8 *Scratch;
VOID *ImageBuffer;
VOID *DecompressedImageBuffer;
UINT32 ImageLength;
EFI_DECOMPRESS_PROTOCOL *Decompress;
ImageIndex = 0;
retStatus = EFI_NOT_FOUND;
RomBarOffset = (UINTN) RomBar;
do {
EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) (UINTN) RomBarOffset;
if (EfiRomHeader->Signature != 0xaa55) {
PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);
return retStatus;
}
Pcir = (PCI_DATA_STRUCTURE *) (UINTN) (RomBarOffset + EfiRomHeader->PcirOffset);
ImageSize = Pcir->ImageLength * 512;
if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&
(EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE)
) {
if ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||
(EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
) {
ImageOffset = EfiRomHeader->EfiImageHeaderOffset;
ImageSize = EfiRomHeader->InitializationSize * 512;
ImageBuffer = (VOID *) (UINTN) (RomBarOffset + ImageOffset);
ImageLength = ImageSize - ImageOffset;
DecompressedImageBuffer = NULL;
//
// decompress here if needed
//
SkipImage = FALSE;
if (EfiRomHeader->CompressionType > EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
SkipImage = TRUE;
}
if (EfiRomHeader->CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
Status = BS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, &Decompress);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_LOADPCIROM_DECOMP_NOT_FOUND), HiiHandle);
SkipImage = TRUE;
} else {
SkipImage = TRUE;
Status = Decompress->GetInfo (
Decompress,
ImageBuffer,
ImageLength,
&DestinationSize,
&ScratchSize
);
if (!EFI_ERROR (Status)) {
DecompressedImageBuffer = AllocatePool (DestinationSize);
if (ImageBuffer != NULL) {
Scratch = AllocatePool (ScratchSize);
if (Scratch != NULL) {
Status = Decompress->Decompress (
Decompress,
ImageBuffer,
ImageLength,
DecompressedImageBuffer,
DestinationSize,
Scratch,
ScratchSize
);
if (!EFI_ERROR (Status)) {
ImageBuffer = DecompressedImageBuffer;
ImageLength = DestinationSize;
SkipImage = FALSE;
}
FreePool (Scratch);
}
}
}
}
}
if (SkipImage == FALSE) {
//
// load image and start image
//
SPrint (RomFileName, sizeof (RomFileName), L"%s[%d]", FileName, ImageIndex);
FilePath = FileDevicePath (NULL, RomFileName);
Status = BS->LoadImage (
TRUE,
gMyImageHandle,
FilePath,
ImageBuffer,
ImageLength,
&ImageHandle
);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
} else {
Status = BS->StartImage (ImageHandle, NULL, NULL);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);
} else {
retStatus = Status;
}
}
}
if (DecompressedImageBuffer != NULL) {
FreePool (DecompressedImageBuffer);
}
}
}
RomBarOffset = RomBarOffset + ImageSize;
ImageIndex++;
} while (((Pcir->Indicator & 0x80) == 0x00) && ((RomBarOffset - (UINTN) RomBar) < RomSize));
return retStatus;
}
沒有留言:
張貼留言