/** @file
Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Common/UefiBaseTypes.h>
#include <CommonLib.h>
#include <IndustryStandard/PeImage.h>
#include "PeCoffLib.h"
typedef union {
);
);
VOID *
);
);
);
);
);
)
/*++
Routine Description:
Arguments:
ImageContext - The context of the image being loaded
PeHdr - The buffer in which to return the PE header
TeHdr - The buffer in which to return the TE header
Returns:
RETURN_SUCCESS if the PE or TE Header is read,
--*/
{
//
// Read the DOS image headers
//
Size = sizeof (EFI_IMAGE_DOS_HEADER);
0,
&Size,
);
if (RETURN_ERROR (Status)) {
return Status;
}
//
// DOS image header is present, so read the PE header after the DOS image header
//
}
//
//
*PeHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN)ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
//
//
return RETURN_UNSUPPORTED;
}
}
return RETURN_SUCCESS;
}
)
/*++
Routine Description:
Arguments:
ImageContext - The context of the image being loaded
PeHdr - The buffer in which to return the PE header
TeHdr - The buffer in which to return the TE header
Returns:
--*/
{
//
// See if the machine type is supported.
// We support a native machine type (IA-32/Itanium-based)
//
} else {
}
//
// a single machine type we need to check for ARM.
//
} else {
}
} else {
//
// unsupported PeImage machine type
//
return RETURN_UNSUPPORTED;
}
}
//
// See if the image type is supported. We support EFI Applications,
// EFI Boot Service Drivers, EFI Runtime Drivers and EFI SAL Drivers.
//
} else {
}
//
// upsupported PeImage subsystem type
//
return RETURN_UNSUPPORTED;
}
return RETURN_SUCCESS;
}
)
/*++
Routine Description:
Arguments:
This - Calling context
ImageContext - The context of the image being loaded
Returns:
RETURN_INVALID_PARAMETER - ImageContext is NULL.
ImageContext->ImageRead() function
--*/
{
if (NULL == ImageContext) {
return RETURN_INVALID_PARAMETER;
}
//
// Assume success
//
if (RETURN_ERROR (Status)) {
return Status;
}
//
// Verify machine type
//
if (RETURN_ERROR (Status)) {
return Status;
}
//
// Retrieve the base address of the image
//
if (!(ImageContext->IsTeImage)) {
} else {
}
} else {
ImageContext->ImageAddress = (PHYSICAL_ADDRESS) (TeHdr->ImageBase + TeHdr->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));
}
//
// Initialize the alternate destination address to 0 indicating that it
// should not be used.
//
//
// Initialize the codeview pointer.
//
//
// Three cases with regards to relocations:
// - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
// - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
// - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
// has no base relocs to apply
// Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
//
// Look at the file header to determine if relocations have been stripped, and
// save this info in the image context for later use.
//
if ((!(ImageContext->IsTeImage)) && ((PeHdr->Pe32.FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {
} else {
}
if (!(ImageContext->IsTeImage)) {
//
// Modify ImageSize to contain .PDB file name if required and initialize
// PdbRVA field...
//
DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *) &(OptionHeader.Optional32->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
}
} else {
//
// Modify ImageSize to contain .PDB file name if required and initialize
// PdbRVA field...
//
DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *) &(OptionHeader.Optional64->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
}
}
if (DebugDirectoryEntryRva != 0) {
//
// Determine the file offset of the debug directory... This means we walk
// the sections to find which section contains the RVA of the debug
// directory
//
SectionHeaderOffset = (UINTN)(
sizeof (UINT32) +
sizeof (EFI_IMAGE_FILE_HEADER) +
);
//
// Read section header from file
//
Size = sizeof (EFI_IMAGE_SECTION_HEADER);
&Size,
);
if (RETURN_ERROR (Status)) {
return Status;
}
break;
}
SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
}
if (DebugDirectoryEntryFileOffset != 0) {
for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {
//
// Read next debug directory entry
//
Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
&Size,
);
if (RETURN_ERROR (Status)) {
return Status;
}
}
return RETURN_SUCCESS;
}
}
}
}
} else {
ImageContext->ImageSize = 0;
ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN) TeHdr->BaseOfCode - (UINTN) TeHdr->StrippedSize;
//
// Read section header from file
//
Size = sizeof (EFI_IMAGE_SECTION_HEADER);
&Size,
);
if (RETURN_ERROR (Status)) {
return Status;
}
sizeof (EFI_TE_IMAGE_HEADER) -
//
// File offset of the debug directory was found, if this is not the last
// section, then skip to the last section for calculating the image size.
//
continue;
}
}
//
// In Te image header there is not a field to describe the ImageSize.
// Actually, the ImageSize equals the RVA plus the VirtualSize of
// the last section mapped into memory (Must be rounded up to
// section headers in the Section Table must appear in order of the RVA
// values for the corresponding sections. So the ImageSize can be determined
// by the RVA and the VirtualSize of the last section header in the
// Section Table.
//
}
SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
}
if (DebugDirectoryEntryFileOffset != 0) {
for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {
//
// Read next debug directory entry
//
Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
&Size,
);
if (RETURN_ERROR (Status)) {
return Status;
}
return RETURN_SUCCESS;
}
}
}
}
return RETURN_SUCCESS;
}
VOID *
)
/*++
Routine Description:
Converts an image address to the loaded address
Arguments:
ImageContext - The context of the image being loaded
Address - The address to be converted to the loaded address
Returns:
NULL if the address can not be converted, otherwise, the converted address
--*/
{
return NULL;
}
}
)
/*++
Routine Description:
Arguments:
This - Calling context
ImageContext - Contains information on the loaded image to relocate
Returns:
RETURN_UNSUPPORTED not support
--*/
{
//
// Assume success
//
//
// If there are no relocation entries, then we are done
//
if (ImageContext->RelocationsStripped) {
return RETURN_SUCCESS;
}
//
// Use DestinationAddress field of ImageContext as the relocation address even if it is 0.
//
if (!(ImageContext->IsTeImage)) {
//
// Find the relocation block
//
// is present in the image. You have to check the NumberOfRvaAndSizes in
// the optional header to verify a desired directory entry is there.
//
);
} else {
//
// Set base and end to bypass processing below.
//
RelocBase = RelocBaseEnd = 0;
}
} else {
//
// Find the relocation block
//
// is present in the image. You have to check the NumberOfRvaAndSizes in
// the optional header to verify a desired directory entry is there.
//
);
} else {
//
// Set base and end to bypass processing below.
//
RelocBase = RelocBaseEnd = 0;
}
}
} else {
//
// Find the relocation block
//
sizeof(EFI_TE_IMAGE_HEADER) -
);
}
//
// Run the relocation information and apply the fixups
//
while (RelocBase < RelocBaseEnd) {
if (!(ImageContext->IsTeImage)) {
} else {
sizeof(EFI_TE_IMAGE_HEADER) -
);
}
return RETURN_LOAD_ERROR;
}
//
// Run this relocation record
//
switch ((*Reloc) >> 12) {
break;
case EFI_IMAGE_REL_BASED_HIGH:
}
break;
case EFI_IMAGE_REL_BASED_LOW:
}
break;
}
break;
//
// Return the same EFI_UNSUPPORTED return code as
// PeCoffLoaderRelocateImageEx() returns if it does not recognize
// the relocation type.
//
return RETURN_UNSUPPORTED;
default:
switch (MachineType) {
case EFI_IMAGE_MACHINE_IA32:
break;
case EFI_IMAGE_MACHINE_ARMT:
break;
case EFI_IMAGE_MACHINE_X64:
break;
case EFI_IMAGE_MACHINE_IA64:
break;
default:
break;
}
if (RETURN_ERROR (Status)) {
return Status;
}
}
//
// Next relocation record
//
Reloc += 1;
}
//
// Next reloc block
//
}
return RETURN_SUCCESS;
}
)
/*++
Routine Description:
Arguments:
This - Calling context
ImageContext - Contains information on image to load into memory
Returns:
RETURN_BUFFER_TOO_SMALL if the caller did not provide a large enough buffer
RETURN_LOAD_ERROR if the image is a runtime driver with no relocations
RETURN_INVALID_PARAMETER if the image address is invalid
--*/
{
//
// Assume success
//
//
// Copy the provided context info into our local version, get what we
// can from the original image, and then use that to make sure everything
// is legit.
//
if (RETURN_ERROR (Status)) {
return Status;
}
//
// Make sure there is enough allocated space for the image being loaded
//
return RETURN_BUFFER_TOO_SMALL;
}
//
// If there's no relocations, then make sure it's not a runtime driver,
// and that it's being loaded at the linked address.
//
if (CheckContext.RelocationsStripped) {
//
// If the image does not contain relocations and it is a runtime driver
// then return an error.
//
return RETURN_LOAD_ERROR;
}
//
// If the image does not contain relocations, and the requested load address
// is not the linked address, then return an error.
//
return RETURN_INVALID_PARAMETER;
}
}
//
// Make sure the allocated space has the proper section alignment
//
if (!(ImageContext->IsTeImage)) {
return RETURN_INVALID_PARAMETER;
}
}
//
//
if (!(ImageContext->IsTeImage)) {
0,
);
FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
sizeof(UINT32) +
sizeof(EFI_IMAGE_FILE_HEADER) +
);
} else {
0,
);
FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
sizeof(EFI_TE_IMAGE_HEADER)
);
}
if (RETURN_ERROR (Status)) {
return RETURN_LOAD_ERROR;
}
//
// Load each section of the image
//
//
// Compute sections address
//
);
//
// If the base start or end address resolved to 0, then fail.
//
return RETURN_LOAD_ERROR;
}
if (ImageContext->IsTeImage) {
}
}
//
// Read the section
//
}
if (Section->SizeOfRawData) {
if (!(ImageContext->IsTeImage)) {
&Size,
);
} else {
&Size,
);
}
if (RETURN_ERROR (Status)) {
return Status;
}
}
//
// If raw size is less then virt size, zero fill the remaining
//
}
//
// Next Section
//
Section += 1;
}
//
// Get image's entry point
//
if (!(ImageContext->IsTeImage)) {
);
} else {
(UINTN)sizeof(EFI_TE_IMAGE_HEADER) -
);
}
//
// Determine the size of the fixup data
//
// is present in the image. You have to check the NumberOfRvaAndSizes in
// the optional header to verify a desired directory entry is there.
//
if (!(ImageContext->IsTeImage)) {
} else {
ImageContext->FixupDataSize = 0;
}
} else {
} else {
ImageContext->FixupDataSize = 0;
}
}
} else {
}
//
// Consumer must allocate a buffer for the relocation fixup log.
// Only used for runtime drivers.
//
//
// Load the Codeview info if present
//
if (ImageContext->DebugDirectoryEntryRva != 0) {
if (!(ImageContext->IsTeImage)) {
);
} else {
sizeof(EFI_TE_IMAGE_HEADER) -
);
}
if (DebugEntry != NULL) {
Section--;
} else {
}
}
if (TempDebugEntryRva != 0) {
if (!(ImageContext->IsTeImage)) {
} else {
(UINTN)sizeof(EFI_TE_IMAGE_HEADER) -
);
}
return RETURN_LOAD_ERROR;
}
if (DebugEntry->RVA == 0) {
if (!(ImageContext->IsTeImage)) {
&Size,
);
} else {
&Size,
);
//
// Should we apply fix up to this field according to the size difference between PE and TE?
// Because now we maintain TE header fields unfixed, this field will also remain as they are
// in original PE image.
//
}
if (RETURN_ERROR (Status)) {
return RETURN_LOAD_ERROR;
}
}
case CODEVIEW_SIGNATURE_NB10:
ImageContext->PdbPointer = (CHAR8 *) ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);
break;
case CODEVIEW_SIGNATURE_RSDS:
ImageContext->PdbPointer = (CHAR8 *) ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
break;
case CODEVIEW_SIGNATURE_MTOC:
ImageContext->PdbPointer = (CHAR8 *) ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY);
default:
break;
}
}
}
}
return Status;
}
/**
debug directory entry, then NULL is returned. If the debug directory entry
then NULL is returned.
If Pe32Data is NULL, then return NULL.
memory.
if it cannot be retrieved.
**/
VOID *
)
{
return NULL;
}
TEImageAdjust = 0;
DebugEntry = NULL;
NumberOfRvaAndSizes = 0;
Index = 0;
Index1 = 0;
//
// DOS image header is present, so read the PE header after the DOS image header.
//
} else {
//
// DOS image header is not present, so PE header is at the image base.
//
}
//
// Get the DebugEntry offset in the raw data image.
//
(DirectoryEntry->VirtualAddress < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {
break;
}
}
}
//
// It is due to backward-compatibility, for some system might
// generate PE32+ image with PE32 Magic.
//
case EFI_IMAGE_MACHINE_IA32:
case EFI_IMAGE_MACHINE_ARMT:
//
// Assume PE32 image with IA32 Machine field.
//
break;
case EFI_IMAGE_MACHINE_X64:
case EFI_IMAGE_MACHINE_IPF:
//
// Assume PE32+ image with X64 or IPF Machine field
//
break;
default:
//
// For unknow Machine field, use Magic in optional Header
//
}
sizeof (UINT32) +
sizeof (EFI_IMAGE_FILE_HEADER) +
);
if (EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC == Magic) {
//
// Use PE32 offset get Debug Directory Entry
//
DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
//
// Use PE32+ offset get Debug Directory Entry
//
DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
}
if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG || DirectoryEntry->VirtualAddress == 0) {
DebugEntry = NULL;
} else {
//
// Get the DebugEntry offset in the raw data image.
//
(DirectoryEntry->VirtualAddress < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {
break;
}
}
}
} else {
return NULL;
}
return NULL;
}
//
// Scan the directory to find the debug entry.
//
for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {
if (DebugEntry->SizeOfData > 0) {
//
// Get the DebugEntry offset in the raw data image.
//
(DebugEntry->RVA < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {
CodeViewEntryPointer = (VOID *) (
break;
}
}
//
//
continue;
}
switch (* (UINT32 *) CodeViewEntryPointer) {
case CODEVIEW_SIGNATURE_NB10:
case CODEVIEW_SIGNATURE_RSDS:
case CODEVIEW_SIGNATURE_MTOC:
default:
break;
}
}
}
}
return NULL;
}
)
{
//
// DOS image header is present, so read the PE header after the DOS image header.
//
} else {
//
// DOS image header is not present, so PE header is at the image base.
//
}
//
// Calculate the entry point relative to the start of the image.
// AddressOfEntryPoint is common for PE32 & PE32+
//
*BaseOfImage = (VOID *)(UINTN)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));
*EntryPoint = (VOID *)((UINTN)*BaseOfImage + (Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize);
return RETURN_SUCCESS;
} else {
}
return RETURN_SUCCESS;
}
return RETURN_UNSUPPORTED;
}