From: ajfish Date: Wed, 26 Jul 2006 15:23:35 +0000 (+0000) Subject: Removed cross references from PciCf8Lib and PciExpressLib class to PciLib class. X-Git-Tag: edk2-stable201903~24769 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=2ce311322c72857f73138c45358e722607a1e80c Removed cross references from PciCf8Lib and PciExpressLib class to PciLib class. Added PeCoffLoaderGetMachineType to the PeCoffGetEntryPointLibrary Class. Document to be updated. Added the PeCoffLoaderImageReadFromMemory() and PeCoffLoaderRelocateImageForRuntime () to the PcCoffLib. Updated EfiImage.h and removed EFI_IMAGE_OPTIONAL_HEADER and EFI_IMAGE_NT_HEADERS as they were replaced with checking the MachineType. PeCoffLib – Added checks for MachineType so the PeCoff lib can load any PE32 or PE32+ image. The relocations are still limited to IA32, X64, IPF, and EBC. I also added a re-relocator function to remove PeLoader Code from Runtime Lib. Even though there is only one instance of the re-relocator I wanted to get all the PeCoff loader code together. Replaced DEBUG_CODE() macro with DEBUG_CODE_START() and DEBUG_CODE_END() so you can debug through the DEBUG_CODE() macros. Also removed PE/COFF code and replaced with library usage. I also updated the IO Instrinsic lib to use _ReadWriteBarrior() to help with sync problems git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1103 6f19259b-4bc3-4df7-8a09-765794883524 --- diff --git a/EdkModulePkg/Bus/Pci/AtapiPassThru/Dxe/AtapiPassThru.c b/EdkModulePkg/Bus/Pci/AtapiPassThru/Dxe/AtapiPassThru.c index 1bdce40f67..e48cf73235 100644 --- a/EdkModulePkg/Bus/Pci/AtapiPassThru/Dxe/AtapiPassThru.c +++ b/EdkModulePkg/Bus/Pci/AtapiPassThru/Dxe/AtapiPassThru.c @@ -2085,94 +2085,90 @@ AtapiPassThruCheckErrorStatus ( ) { UINT8 StatusRegister; - -//#ifdef EFI_DEBUG - UINT8 ErrorRegister; -//#endif - StatusRegister = ReadPortB ( AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg.Status ); - DEBUG_CODE ( + + DEBUG_CODE_BEGIN (); if (StatusRegister & DWF) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Write Fault\n", - StatusRegister) - ); - } + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Write Fault\n", + StatusRegister) + ); + } - if (StatusRegister & CORR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Corrected Data\n", - StatusRegister) - ); - } + if (StatusRegister & CORR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Corrected Data\n", + StatusRegister) + ); + } - if (StatusRegister & ERR) { - ErrorRegister = ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg1.Error); + if (StatusRegister & ERR) { + ErrorRegister = ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg1.Error); + - if (ErrorRegister & BBK_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Bad Block Detected\n", - ErrorRegister) - ); - } + if (ErrorRegister & BBK_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Bad Block Detected\n", + ErrorRegister) + ); + } - if (ErrorRegister & UNC_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Uncorrectable Data\n", - ErrorRegister) - ); - } + if (ErrorRegister & UNC_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Uncorrectable Data\n", + ErrorRegister) + ); + } - if (ErrorRegister & MC_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Media Change\n", - ErrorRegister) - ); - } + if (ErrorRegister & MC_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Media Change\n", + ErrorRegister) + ); + } - if (ErrorRegister & ABRT_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Abort\n", - ErrorRegister) - ); - } + if (ErrorRegister & ABRT_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Abort\n", + ErrorRegister) + ); + } - if (ErrorRegister & TK0NF_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Track 0 Not Found\n", - ErrorRegister) - ); - } + if (ErrorRegister & TK0NF_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Track 0 Not Found\n", + ErrorRegister) + ); + } - if (ErrorRegister & AMNF_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "AtapiPassThruCheckErrorStatus()-- %02x : Error : Address Mark Not Found\n", - ErrorRegister) - ); - } + if (ErrorRegister & AMNF_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "AtapiPassThruCheckErrorStatus()-- %02x : Error : Address Mark Not Found\n", + ErrorRegister) + ); + } + } - } - ); + DEBUG_CODE_END (); if ((StatusRegister & (ERR | DWF | CORR)) == 0) { - return EFI_SUCCESS; } + return EFI_DEVICE_ERROR; - -} +} \ No newline at end of file diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c index 9a1542d0ea..cb184a497d 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c @@ -640,77 +640,77 @@ CheckErrorStatus ( StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status); - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (StatusRegister & DWF) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Write Fault\n", - StatusRegister) - ); - } + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Write Fault\n", + StatusRegister) + ); + } - if (StatusRegister & CORR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Corrected Data\n", - StatusRegister) - ); - } + if (StatusRegister & CORR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Corrected Data\n", + StatusRegister) + ); + } - if (StatusRegister & ERR) { - ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error); + if (StatusRegister & ERR) { + ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error); - if (ErrorRegister & BBK_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n", - ErrorRegister) - ); - } + if (ErrorRegister & BBK_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n", + ErrorRegister) + ); + } - if (ErrorRegister & UNC_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n", - ErrorRegister) - ); - } + if (ErrorRegister & UNC_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n", + ErrorRegister) + ); + } - if (ErrorRegister & MC_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Media Change\n", - ErrorRegister) - ); - } + if (ErrorRegister & MC_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Media Change\n", + ErrorRegister) + ); + } - if (ErrorRegister & ABRT_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Abort\n", - ErrorRegister) - ); - } + if (ErrorRegister & ABRT_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Abort\n", + ErrorRegister) + ); + } - if (ErrorRegister & TK0NF_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n", - ErrorRegister) - ); - } + if (ErrorRegister & TK0NF_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n", + ErrorRegister) + ); + } - if (ErrorRegister & AMNF_ERR) { - DEBUG ( - (EFI_D_BLKIO, - "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n", - ErrorRegister) - ); - } + if (ErrorRegister & AMNF_ERR) { + DEBUG ( + (EFI_D_BLKIO, + "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n", + ErrorRegister) + ); + } + } - } - ); + DEBUG_CODE_END (); if ((StatusRegister & (ERR | DWF | CORR)) == 0) { return EFI_SUCCESS; diff --git a/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciBus.msa b/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciBus.msa index 182baa7c6b..c6194d6aa8 100644 --- a/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciBus.msa +++ b/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciBus.msa @@ -1,23 +1,13 @@ - - - + + PciBus DXE_DRIVER 93B80004-9FB3-11d4-9A3A-0090273FC14D 1.0 Component description file for PciBus module. - - PCI bus driver. This module will probe all PCI devices and allocate MMIO and IO - space for these devices. - + PCI bus driver. This module will probe all PCI devices and allocate MMIO and IO + space for these devices. Copyright (c) 2006, Intel Corporation All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License @@ -63,6 +53,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.--> DevicePathLib + + PeCoffGetEntryPointLib + PciBus.h diff --git a/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciDriverOverride.c b/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciDriverOverride.c index 76c5a20784..cd2d585303 100644 --- a/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciDriverOverride.c +++ b/EdkModulePkg/Bus/Pci/PciBus/Dxe/PciDriverOverride.c @@ -139,8 +139,6 @@ Returns: // TODO: EFI_SUCCESS - add return value to function comment { EFI_STATUS Status; - EFI_IMAGE_DOS_HEADER *DosHdr; - EFI_IMAGE_NT_HEADERS *PeHdr; EFI_LOADED_IMAGE_PROTOCOL *LoadedImage; PCI_DRIVER_OVERRIDE_LIST *Node; EFI_DRIVER_OS_HANDOFF_HEADER *DriverOsHandoffHeader; @@ -169,14 +167,7 @@ Returns: PciIoDevice->BusOverride = TRUE; - DosHdr = (EFI_IMAGE_DOS_HEADER *) LoadedImage->ImageBase; - if (DosHdr->e_magic != EFI_IMAGE_DOS_SIGNATURE) { - return EFI_SUCCESS; - } - - PeHdr = (EFI_IMAGE_NT_HEADERS *) ((UINTN) LoadedImage->ImageBase + DosHdr->e_lfanew); - - if (PeHdr->FileHeader.Machine != EFI_IMAGE_MACHINE_EBC) { + if (PeCoffLoaderGetMachineType ((VOID *)(UINTN)LoadedImage->ImageBase) != EFI_IMAGE_MACHINE_EBC) { return EFI_SUCCESS; } diff --git a/EdkModulePkg/Core/Dxe/DxeMain/DxeMain.c b/EdkModulePkg/Core/Dxe/DxeMain/DxeMain.c index 24568ea334..9f5f2f1510 100644 --- a/EdkModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/EdkModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -557,9 +557,9 @@ Returns: // // Display Architectural protocols that were not loaded if this is DEBUG build // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); CoreDisplayMissingArchProtocols (); - ); + DEBUG_CODE_END (); // // Assert if the Architectural Protocols are not present. @@ -574,9 +574,9 @@ Returns: // Display any drivers that were not dispatched because dependency expression // evaluated to false if this is a debug build // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); CoreDisplayDiscoveredNotDispatched (); - ); + DEBUG_CODE_END (); // // Transfer control to the BDS Architectural Protocol diff --git a/EdkModulePkg/Core/Dxe/Image/Image.c b/EdkModulePkg/Core/Dxe/Image/Image.c index c818cc2131..d77f4c4fca 100644 --- a/EdkModulePkg/Core/Dxe/Image/Image.c +++ b/EdkModulePkg/Core/Dxe/Image/Image.c @@ -268,6 +268,15 @@ Returns: return Status; } + if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) { + // + // The PE/COFF loader can support loading image types that can be executed. + // If we loaded an image type that we can not execute return EFI_UNSUPORTED. + // + return EFI_UNSUPPORTED; + } + + // // Allocate memory of the correct memory type aligned on the required image boundry // @@ -440,8 +449,8 @@ Returns: // Print the load address and the PDB file name if it is available // - DEBUG_CODE ( - { + DEBUG_CODE_BEGIN (); + UINTN Index; UINTN StartIndex; CHAR8 EfiFileName[256]; @@ -473,8 +482,8 @@ Returns: DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex])); } DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n")); - } - ); + + DEBUG_CODE_END (); return EFI_SUCCESS; @@ -924,22 +933,13 @@ Returns: // PERF_START (ImageHandle, START_IMAGE_TOK, NULL, 0); - if (sizeof (UINTN) == 4 && Image->Machine == EFI_IMAGE_MACHINE_X64) { - return EFI_UNSUPPORTED; - } else if (sizeof (UINTN) == 8 && Image->Machine == EFI_IMAGE_MACHINE_IA32) { - return EFI_UNSUPPORTED; - } else { - // - // For orther possible cases - // - } // // Push the current start image context, and // link the current image to the head. This is the // only image that can call Exit() // - HandleDatabaseKey = CoreGetHandleDatabaseKey(); + HandleDatabaseKey = CoreGetHandleDatabaseKey (); LastImage = mCurrentImage; mCurrentImage = Image; Image->Tpl = gEfiCurrentTpl; @@ -970,11 +970,11 @@ Returns: // This make the user aware and check if the driver image have already released // all the resource in this situation. // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (EFI_ERROR (Image->Status)) { DEBUG ((EFI_D_ERROR, "Error: Image at %08X start failed: %x\n", Image->Info.ImageBase, Image->Status)); } - ); + DEBUG_CODE_END (); // // If the image returns, exit it through Exit() @@ -1003,7 +1003,7 @@ Returns: // // Handle the image's returned ExitData // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (Image->ExitDataSize != 0 || Image->ExitData != NULL) { DEBUG ( @@ -1017,7 +1017,7 @@ Returns: } DEBUG ((EFI_D_LOAD, "\n")); } - ); + DEBUG_CODE_END (); // // Return the exit data to the caller diff --git a/EdkModulePkg/Core/Pei/Dispatcher/Dispatcher.c b/EdkModulePkg/Core/Pei/Dispatcher/Dispatcher.c index 1eb5a4a020..6d32368fb7 100644 --- a/EdkModulePkg/Core/Pei/Dispatcher/Dispatcher.c +++ b/EdkModulePkg/Core/Pei/Dispatcher/Dispatcher.c @@ -105,7 +105,7 @@ Returns: // if (Status == EFI_SUCCESS) { - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); // // Fill list of found Peims for later list of those not installed @@ -116,7 +116,7 @@ Returns: sizeof (EFI_GUID) ); - ); + DEBUG_CODE_END (); if (!Dispatched ( DispatchData->CurrentPeim, @@ -320,7 +320,7 @@ Returns: DispatchData->CurrentFvAddress = DefaultFvAddress; } - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); // // Debug data for uninstalled Peim list // @@ -345,10 +345,10 @@ Returns: DebugFoundPeimPoint++; DebugNotDispatchedBitmap >>= 1; } + + DEBUG_CODE_END (); - ); - - return EFI_NOT_FOUND; + return EFI_NOT_FOUND; } VOID diff --git a/EdkModulePkg/Core/Pei/Hob/Hob.c b/EdkModulePkg/Core/Pei/Hob/Hob.c index 1eadf9458c..259d44b457 100644 --- a/EdkModulePkg/Core/Pei/Hob/Hob.c +++ b/EdkModulePkg/Core/Pei/Hob/Hob.c @@ -53,11 +53,11 @@ Returns: // Only check this parameter in debug mode // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (HobList == NULL) { return EFI_INVALID_PARAMETER; } - ); + DEBUG_CODE_END (); PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS(PeiServices); diff --git a/EdkModulePkg/Core/Pei/Image/Image.c b/EdkModulePkg/Core/Pei/Image/Image.c index 5edc8479eb..9df12f8ab3 100644 --- a/EdkModulePkg/Core/Pei/Image/Image.c +++ b/EdkModulePkg/Core/Pei/Image/Image.c @@ -21,6 +21,8 @@ Abstract: #include + + EFI_STATUS PeiLoadImage ( IN EFI_PEI_SERVICES **PeiServices, @@ -130,106 +132,35 @@ Returns: // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi // DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", Pe32Data, *EntryPoint)); - DEBUG_CODE ( - EFI_IMAGE_DATA_DIRECTORY * DirectoryEntry; - EFI_IMAGE_DEBUG_DIRECTORY_ENTRY * DebugEntry; - UINTN DirCount; - UINTN Index; - UINTN Index1; - BOOLEAN FileNameFound; - CHAR8 *AsciiString; - CHAR8 AsciiBuffer[512]; - VOID *CodeViewEntryPointer; - INTN TEImageAdjust; - EFI_IMAGE_DOS_HEADER *DosHeader; - EFI_IMAGE_NT_HEADERS *PeHeader; + DEBUG_CODE_BEGIN (); + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + UINTN Index; + CHAR8 *PdbStr; + CHAR8 AsciiBuffer[512]; - // - // Pe32Data is NULL when load TE image - // - PeHeader = NULL; - if (TEImageHeader == NULL) { - DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data; - if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) { - // - // DOS image header is present, so read the PE header after the DOS image header - // - PeHeader = (EFI_IMAGE_NT_HEADERS *) ((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff)); - } else { - // - // DOS image header is not present, so PE header is at the image base - // - PeHeader = (EFI_IMAGE_NT_HEADERS *) Pe32Data; - } - } - // - // Find the codeview info in the image and display the file name - // being loaded. - // - // Per the PE/COFF spec, you can't assume that a given data directory - // is present in the image. You have to check the NumberOfRvaAndSizes in - // the optional header to verify a desired directory entry is there. - // - DebugEntry = NULL; - DirectoryEntry = NULL; - TEImageAdjust = 0; - if (TEImageHeader == NULL) { - if (PeHeader->OptionalHeader.NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) { - DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *) &(PeHeader->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); - DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) ImageAddress + DirectoryEntry->VirtualAddress); - } - } else { - if (TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) { - DirectoryEntry = &TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG]; - TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize; - DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) TEImageHeader + - TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress + - TEImageAdjust); - } - } + ZeroMem (&ImageContext, sizeof (ImageContext)); + ImageContext.Handle = Pe32Data; + ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory; - if (DebugEntry != NULL && DirectoryEntry != NULL) { - for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount++, DebugEntry++) { - if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) { - if (DebugEntry->SizeOfData > 0) { - CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + (UINTN) ImageAddress + (UINTN)TEImageAdjust); - switch (* (UINT32 *) CodeViewEntryPointer) { - case CODEVIEW_SIGNATURE_NB10: - AsciiString = (CHAR8 *) CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY); - break; - - case CODEVIEW_SIGNATURE_RSDS: - AsciiString = (CHAR8 *) CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY); - break; - - default: - AsciiString = NULL; - break; - } - if (AsciiString != NULL) { - FileNameFound = FALSE; - for (Index = 0, Index1 = 0; AsciiString[Index] != 0; Index++) { - if (AsciiString[Index] == '\\') { - Index1 = Index; - FileNameFound = TRUE; - } - } - - if (FileNameFound) { - for (Index = Index1 + 1; AsciiString[Index] != '.'; Index++) { - AsciiBuffer[Index - (Index1 + 1)] = AsciiString[Index]; - } - AsciiBuffer[Index - (Index1 + 1)] = 0; - DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer)); - break; - } - } - } + PeCoffLoaderGetImageInfo (&ImageContext); + + if (ImageContext.PdbPointer != NULL) { + // + // Copy PDB pointer to AsciiBuffer and replace .PDB with .EFI + // + PdbStr = ImageContext.PdbPointer; + for (Index = 0; PdbStr != 0; Index++, PdbStr++) { + AsciiBuffer[Index] = *PdbStr; + if (*PdbStr == '.') { + AsciiBuffer[Index] = '\0'; } } + + DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer)); } - ); + + DEBUG_CODE_END (); DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n")); diff --git a/EdkModulePkg/Core/Pei/Memory/MemoryServices.c b/EdkModulePkg/Core/Pei/Memory/MemoryServices.c index 3da3e53185..d49b7427b4 100644 --- a/EdkModulePkg/Core/Pei/Memory/MemoryServices.c +++ b/EdkModulePkg/Core/Pei/Memory/MemoryServices.c @@ -65,10 +65,10 @@ Returns: SizeOfCarHeap = (UINT64) PeiStartupDescriptor->SizeOfCacheAsRam; SizeOfCarHeap = RShiftU64 (SizeOfCarHeap, 1); - DEBUG_CODE ( - PrivateData->SizeOfCacheAsRam = PeiStartupDescriptor->SizeOfCacheAsRam; - PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) SizeOfCarHeap); - ); + DEBUG_CODE_BEGIN (); + PrivateData->SizeOfCacheAsRam = PeiStartupDescriptor->SizeOfCacheAsRam; + PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) SizeOfCarHeap); + DEBUG_CODE_END (); PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap; diff --git a/EdkModulePkg/Core/Pei/PeiMain.msa b/EdkModulePkg/Core/Pei/PeiMain.msa index 18ec53d665..549820ef87 100644 --- a/EdkModulePkg/Core/Pei/PeiMain.msa +++ b/EdkModulePkg/Core/Pei/PeiMain.msa @@ -1,13 +1,5 @@ - - - + + PeiMain PEI_CORE @@ -60,6 +52,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.--> TimerLib + + PeCoffLib + PeiMain.h diff --git a/EdkModulePkg/Core/Pei/PeiMain/PeiMain.c b/EdkModulePkg/Core/Pei/PeiMain/PeiMain.c index b6d57f96d5..c94331c010 100644 --- a/EdkModulePkg/Core/Pei/PeiMain/PeiMain.c +++ b/EdkModulePkg/Core/Pei/PeiMain/PeiMain.c @@ -150,10 +150,10 @@ Returns: // // The following code dumps out interesting cache as RAM usage information // so we can keep tabs on how the cache as RAM is being utilized. The - // DEBUG_CODE macro is used to prevent this code from being compiled + // DEBUG_CODE_BEGIN macro is used to prevent this code from being compiled // on a debug build. // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); UINTN *StackPointer; UINTN StackValue; @@ -173,7 +173,7 @@ Returns: ((UINTN) OldCoreData->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN) OldCoreData->HobList.Raw) )); - ); + DEBUG_CODE_END (); // // Alert any listeners that there is permanent memory available diff --git a/EdkModulePkg/EdkModulePkg.fpd b/EdkModulePkg/EdkModulePkg.fpd index 3e826c8775..b1738f4fd8 100644 --- a/EdkModulePkg/EdkModulePkg.fpd +++ b/EdkModulePkg/EdkModulePkg.fpd @@ -1032,6 +1032,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -1293,6 +1294,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -3139,7 +3141,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - + + PcdMaximumUnicodeStringLength @@ -5422,6 +5425,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -5684,6 +5688,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -7448,6 +7453,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -9357,6 +9363,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -9620,6 +9627,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -11387,6 +11395,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -13296,6 +13305,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -14905,6 +14915,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + diff --git a/EdkModulePkg/Universal/Console/GraphicsConsole/Dxe/GraphicsConsole.c b/EdkModulePkg/Universal/Console/GraphicsConsole/Dxe/GraphicsConsole.c index a475723dfd..d70c979b72 100644 --- a/EdkModulePkg/Universal/Console/GraphicsConsole/Dxe/GraphicsConsole.c +++ b/EdkModulePkg/Universal/Console/GraphicsConsole/Dxe/GraphicsConsole.c @@ -423,9 +423,9 @@ GraphicsConsoleControllerDriverStart ( goto Error; } - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); GraphicsConsoleConOutOutputString (&Private->SimpleTextOutput, (CHAR16 *)L"Graphics Console Started\n\r"); - ); + DEBUG_CODE_END (); // // Install protocol interfaces for the Graphics Console device. diff --git a/EdkModulePkg/Universal/DebugSupport/Dxe/ipf/plDebugSupport.c b/EdkModulePkg/Universal/DebugSupport/Dxe/ipf/plDebugSupport.c index 6f2ded213e..b79390a95f 100644 --- a/EdkModulePkg/Universal/DebugSupport/Dxe/ipf/plDebugSupport.c +++ b/EdkModulePkg/Universal/DebugSupport/Dxe/ipf/plDebugSupport.c @@ -191,7 +191,7 @@ Returns: { static BOOLEAN InHandler = FALSE; - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (InHandler) { EfiDebugPrint (EFI_D_GENERIC, "ERROR: Re-entered debugger!\n" " ExceptionType == %X\n" @@ -205,7 +205,8 @@ Returns: Context.SystemContextIpf->CrIpsr, InHandler); } - ) + DEBUG_CODE_END (); + ASSERT (!InHandler); InHandler = TRUE; if (IvtEntryTable[ExceptionType].RegisteredCallback != NULL) { diff --git a/EdkModulePkg/Universal/Debugger/Debugport/Dxe/DebugPort.c b/EdkModulePkg/Universal/Debugger/Debugport/Dxe/DebugPort.c index a24bfd06d5..2152f75cf4 100644 --- a/EdkModulePkg/Universal/Debugger/Debugport/Dxe/DebugPort.c +++ b/EdkModulePkg/Universal/Debugger/Debugport/Dxe/DebugPort.c @@ -368,7 +368,7 @@ Returns: ); if (EFI_ERROR (Status)) { - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); UINTN BufferSize; BufferSize = 48; @@ -378,7 +378,7 @@ Returns: &BufferSize, "DebugPort driver failed to open child controller\n\n" ); - ); + DEBUG_CODE_END (); gBS->CloseProtocol ( ControllerHandle, @@ -389,7 +389,7 @@ Returns: return Status; } - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); UINTN BufferSize; BufferSize = 38; @@ -399,7 +399,7 @@ Returns: &BufferSize, "Hello World from the DebugPort driver\n\n" ); - ); + DEBUG_CODE_END (); return EFI_SUCCESS; } diff --git a/EdkModulePkg/Universal/Disk/Partition/Dxe/Gpt.c b/EdkModulePkg/Universal/Disk/Partition/Dxe/Gpt.c index 35ff1a8606..9077ac63c2 100644 --- a/EdkModulePkg/Universal/Disk/Partition/Dxe/Gpt.c +++ b/EdkModulePkg/Universal/Disk/Partition/Dxe/Gpt.c @@ -758,11 +758,11 @@ Returns: // // return status // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (OrgCrc != Crc) { DEBUG ((EFI_D_ERROR, "CheckCrc32: Crc check failed\n")); } - ); + DEBUG_CODE_END (); return (BOOLEAN) (OrgCrc == Crc); } diff --git a/EdkModulePkg/Universal/Ebc/Dxe/EbcExecute.c b/EdkModulePkg/Universal/Ebc/Dxe/EbcExecute.c index 9d375a5461..a672a794e0 100644 --- a/EdkModulePkg/Universal/Ebc/Dxe/EbcExecute.c +++ b/EdkModulePkg/Universal/Ebc/Dxe/EbcExecute.c @@ -685,9 +685,6 @@ Returns: EFI_STATUS Status; EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL *EbcSimpleDebugger; - // - // end DEBUG_CODE - // EbcSimpleDebugger = NULL; Status = EFI_SUCCESS; StackCorrupted = 0; @@ -704,7 +701,7 @@ Returns: // // Try to get the debug support for EBC // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); Status = gBS->LocateProtocol ( &mEbcSimpleDebuggerProtocolGuid, NULL, @@ -713,7 +710,7 @@ Returns: if (EFI_ERROR (Status)) { EbcSimpleDebugger = NULL; } - ); + DEBUG_CODE_END (); // // Save the start IP for debug. For example, if we take an exception we @@ -731,11 +728,11 @@ Returns: // // If we've found a simple debugger protocol, call it // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (EbcSimpleDebugger != NULL) { EbcSimpleDebugger->Debugger (EbcSimpleDebugger, VmPtr); } - ); + DEBUG_CODE_END (); // // Verify the opcode is in range. Otherwise generate an exception. diff --git a/EdkModulePkg/Universal/Ebc/Dxe/EbcInt.c b/EdkModulePkg/Universal/Ebc/Dxe/EbcInt.c index 220c8fefac..7b15a1a84b 100644 --- a/EdkModulePkg/Universal/Ebc/Dxe/EbcInt.c +++ b/EdkModulePkg/Universal/Ebc/Dxe/EbcInt.c @@ -298,9 +298,9 @@ Returns: // // Produce a VM test interface protocol. Not required for execution. // - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); InitEbcVmTestProtocol (&ImageHandle); - ); + DEBUG_CODE_END (); return Status; } @@ -908,10 +908,10 @@ Returns: } EbcVmTestProtocol->Execute = (EBC_VM_TEST_EXECUTE) EbcExecuteInstructions; - DEBUG_CODE( + DEBUG_CODE_BEGIN (); EbcVmTestProtocol->Assemble = (EBC_VM_TEST_ASM) EbcVmTestUnsupported; EbcVmTestProtocol->Disassemble = (EBC_VM_TEST_DASM) EbcVmTestUnsupported; - ); + DEBUG_CODE_END (); // // Publish the protocol diff --git a/EdkModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c b/EdkModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c index 80258f4cc3..85fa60e348 100644 --- a/EdkModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c +++ b/EdkModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c @@ -173,21 +173,23 @@ Note: Status = PciRootBridgeIo->Pci.Write ( PciRootBridgeIo, EfiPciWidthUint32, - EFI_PCI_ADDRESS (LPC_BUS_NUMBER, - LPC_DEVICE_NUMBER, - LPC_IF, - GEN_STATUS), + EFI_PCI_ADDRESS ( + LPC_BUS_NUMBER, + LPC_DEVICE_NUMBER, + LPC_IF, + GEN_STATUS + ), 1, &GenStatus ); - DEBUG_CODE ( + DEBUG_CODE_BEGIN (); if (TopSwap) { DEBUG ((EFI_D_ERROR, "SAR: Set top swap\n")); } else { DEBUG ((EFI_D_ERROR, "SAR: Clear top swap\n")); } - ); + DEBUG_CODE_END (); return EFI_SUCCESS; } diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ia32/PeHotRelocateEx.c b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ia32/PeHotRelocateEx.c index 82d464aebe..7678c4d365 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ia32/PeHotRelocateEx.c +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ia32/PeHotRelocateEx.c @@ -24,38 +24,6 @@ Revision History #include "Runtime.h" -EFI_STATUS -PeHotRelocateImageEx ( - IN UINT16 *Reloc, - IN OUT CHAR8 *Fixup, - IN OUT CHAR8 **FixupData, - IN UINT64 Adjust - ) -/*++ - -Routine Description: - - Performs an Itanium-based platform specific relocation fixup - -Arguments: - - Reloc - Pointer to the relocation record - - Fixup - Pointer to the address to fix up - - FixupData - Pointer to a buffer to log the fixups - - Adjust - The offset to adjust the fixup - -Returns: - - EFI_SUCCESS - ---*/ -{ - return EFI_SUCCESS; - -} // // Cache Flush Routine. diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ipf/PeHotRelocateEx.c b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ipf/PeHotRelocateEx.c index ffc0aaf4a4..97bdae0aa0 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ipf/PeHotRelocateEx.c +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Ipf/PeHotRelocateEx.c @@ -23,192 +23,6 @@ Revision History --*/ #include "Runtime.h" -#include "PeHotRelocateEx.h" - -EFI_STATUS -PeHotRelocateImageEx ( - IN UINT16 *Reloc, - IN OUT CHAR8 *Fixup, - IN OUT CHAR8 **FixupData, - IN UINT64 Adjust - ) -/*++ - -Routine Description: - - Performs an IPF specific relocation fixup - -Arguments: - - Reloc - Pointer to the relocation record - - Fixup - Pointer to the address to fix up - - FixupData - Pointer to a buffer to log the fixups - - Adjust - The offset to adjust the fixup - -Returns: - - None - ---*/ -{ - UINT64 *F64; - UINT64 FixupVal; - - switch ((*Reloc) >> 12) { - case EFI_IMAGE_REL_BASED_DIR64: - F64 = (UINT64 *) Fixup; - *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64)); - if (*(UINT64 *) (*FixupData) == *F64) { - *F64 = *F64 + (UINT64) Adjust; - } - - *FixupData = *FixupData + sizeof (UINT64); - break; - - case EFI_IMAGE_REL_BASED_IA64_IMM64: - F64 = (UINT64 *) Fixup; - *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64)); - if (*(UINT64 *) (*FixupData) == *F64) { - // - // Align it to bundle address before fixing up the - // 64-bit immediate value of the movl instruction. - // - // - Fixup = (CHAR8 *) ((UINT64) Fixup & (UINT64)~(15)); - FixupVal = (UINT64) 0; - - // - // Extract the lower 32 bits of IMM64 from bundle - // - EXT_IMM64 ( - FixupVal, - (UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X, - IMM64_IMM7B_SIZE_X, - IMM64_IMM7B_INST_WORD_POS_X, - IMM64_IMM7B_VAL_POS_X - ); - - EXT_IMM64 ( - FixupVal, - (UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X, - IMM64_IMM9D_SIZE_X, - IMM64_IMM9D_INST_WORD_POS_X, - IMM64_IMM9D_VAL_POS_X - ); - - EXT_IMM64 ( - FixupVal, - (UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X, - IMM64_IMM5C_SIZE_X, - IMM64_IMM5C_INST_WORD_POS_X, - IMM64_IMM5C_VAL_POS_X - ); - - EXT_IMM64 ( - FixupVal, - (UINT32 *) Fixup + IMM64_IC_INST_WORD_X, - IMM64_IC_SIZE_X, - IMM64_IC_INST_WORD_POS_X, - IMM64_IC_VAL_POS_X - ); - - EXT_IMM64 ( - FixupVal, - (UINT32 *) Fixup + IMM64_IMM41a_INST_WORD_X, - IMM64_IMM41a_SIZE_X, - IMM64_IMM41a_INST_WORD_POS_X, - IMM64_IMM41a_VAL_POS_X - ); - - // - // Update 64-bit address - // - FixupVal += Adjust; - - // - // Insert IMM64 into bundle - // - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X), - IMM64_IMM7B_SIZE_X, - IMM64_IMM7B_INST_WORD_POS_X, - IMM64_IMM7B_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X), - IMM64_IMM9D_SIZE_X, - IMM64_IMM9D_INST_WORD_POS_X, - IMM64_IMM9D_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X), - IMM64_IMM5C_SIZE_X, - IMM64_IMM5C_INST_WORD_POS_X, - IMM64_IMM5C_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IC_INST_WORD_X), - IMM64_IC_SIZE_X, - IMM64_IC_INST_WORD_POS_X, - IMM64_IC_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IMM41a_INST_WORD_X), - IMM64_IMM41a_SIZE_X, - IMM64_IMM41a_INST_WORD_POS_X, - IMM64_IMM41a_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IMM41b_INST_WORD_X), - IMM64_IMM41b_SIZE_X, - IMM64_IMM41b_INST_WORD_POS_X, - IMM64_IMM41b_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_IMM41c_INST_WORD_X), - IMM64_IMM41c_SIZE_X, - IMM64_IMM41c_INST_WORD_POS_X, - IMM64_IMM41c_VAL_POS_X - ); - - INS_IMM64 ( - FixupVal, - ((UINT32 *) Fixup + IMM64_SIGN_INST_WORD_X), - IMM64_SIGN_SIZE_X, - IMM64_SIGN_INST_WORD_POS_X, - IMM64_SIGN_VAL_POS_X - ); - - *(UINT64 *) (*FixupData) = *F64; - } - - *FixupData = *FixupData + sizeof (UINT64); - break; - - default: - DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n")); - return EFI_UNSUPPORTED; - } - - return EFI_SUCCESS; -} - // // Cache Flush Routine. diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/PeHotRelocate.c b/EdkModulePkg/Universal/Runtime/RuntimeDxe/PeHotRelocate.c index 4c2aeff78e..fd980a7b6a 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/PeHotRelocate.c +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/PeHotRelocate.c @@ -51,166 +51,3 @@ Returns: return (CHAR8 *) ((UINTN) Image->ImageBase + Address); } -VOID -RelocatePeImageForRuntime ( - RUNTIME_IMAGE_RELOCATION_DATA *Image - ) -{ - CHAR8 *OldBase; - CHAR8 *NewBase; - EFI_IMAGE_DOS_HEADER *DosHdr; - EFI_IMAGE_NT_HEADERS *PeHdr; - UINT32 NumberOfRvaAndSizes; - EFI_IMAGE_DATA_DIRECTORY *DataDirectory; - EFI_IMAGE_DATA_DIRECTORY *RelocDir; - EFI_IMAGE_BASE_RELOCATION *RelocBase; - EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd; - UINT16 *Reloc; - UINT16 *RelocEnd; - CHAR8 *Fixup; - CHAR8 *FixupBase; - UINT16 *F16; - UINT32 *F32; - CHAR8 *FixupData; - UINTN Adjust; - EFI_STATUS Status; - - OldBase = (CHAR8 *) ((UINTN) Image->ImageBase); - NewBase = (CHAR8 *) ((UINTN) Image->ImageBase); - - Status = RuntimeDriverConvertPointer (0, (VOID **) &NewBase); - ASSERT_EFI_ERROR (Status); - - Adjust = (UINTN) NewBase - (UINTN) OldBase; - - // - // Find the image's relocate dir info - // - DosHdr = (EFI_IMAGE_DOS_HEADER *) OldBase; - if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) { - // - // Valid DOS header so get address of PE header - // - PeHdr = (EFI_IMAGE_NT_HEADERS *) (((CHAR8 *) DosHdr) + DosHdr->e_lfanew); - } else { - // - // No Dos header so assume image starts with PE header. - // - PeHdr = (EFI_IMAGE_NT_HEADERS *) OldBase; - } - - if (PeHdr->Signature != EFI_IMAGE_NT_SIGNATURE) { - // - // Not a valid PE image so Exit - // - return ; - } - // - // Get some data from the PE type dependent data - // - NumberOfRvaAndSizes = PeHdr->OptionalHeader.NumberOfRvaAndSizes; - DataDirectory = &PeHdr->OptionalHeader.DataDirectory[0]; - - // - // Find the relocation block - // - // Per the PE/COFF spec, you can't assume that a given data directory - // is present in the image. You have to check the NumberOfRvaAndSizes in - // the optional header to verify a desired directory entry is there. - // - if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) { - RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC; - RelocBase = RuntimePeImageAddress (Image, RelocDir->VirtualAddress); - RelocBaseEnd = RuntimePeImageAddress (Image, RelocDir->VirtualAddress + RelocDir->Size); - } else { - // - // Cannot find relocations, cannot continue - // - ASSERT (FALSE); - return ; - } - - ASSERT (RelocBase != NULL && RelocBaseEnd != NULL); - - // - // Run the whole relocation block. And re-fixup data that has not been - // modified. The FixupData is used to see if the image has been modified - // since it was relocated. This is so data sections that have been updated - // by code will not be fixed up, since that would set them back to - // defaults. - // - FixupData = Image->RelocationData; - while (RelocBase < RelocBaseEnd) { - - Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION)); - RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock); - FixupBase = (CHAR8 *) ((UINTN) Image->ImageBase) + RelocBase->VirtualAddress; - - // - // Run this relocation record - // - while (Reloc < RelocEnd) { - - Fixup = FixupBase + (*Reloc & 0xFFF); - switch ((*Reloc) >> 12) { - - case EFI_IMAGE_REL_BASED_ABSOLUTE: - break; - - case EFI_IMAGE_REL_BASED_HIGH: - F16 = (UINT16 *) Fixup; - if (*(UINT16 *) FixupData == *F16) { - *F16 = (UINT16) ((*F16 << 16) + ((UINT16) Adjust & 0xffff)); - } - - FixupData = FixupData + sizeof (UINT16); - break; - - case EFI_IMAGE_REL_BASED_LOW: - F16 = (UINT16 *) Fixup; - if (*(UINT16 *) FixupData == *F16) { - *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff)); - } - - FixupData = FixupData + sizeof (UINT16); - break; - - case EFI_IMAGE_REL_BASED_HIGHLOW: - F32 = (UINT32 *) Fixup; - FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32)); - if (*(UINT32 *) FixupData == *F32) { - *F32 = *F32 + (UINT32) Adjust; - } - - FixupData = FixupData + sizeof (UINT32); - break; - - case EFI_IMAGE_REL_BASED_HIGHADJ: - // - // Not implemented, but not used in EFI 1.0 - // - ASSERT (FALSE); - break; - - default: - // - // Only Itanium requires ConvertPeImage_Ex - // - Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust); - if (EFI_ERROR (Status)) { - return ; - } - } - // - // Next relocation record - // - Reloc += 1; - } - // - // next reloc block - // - RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd; - } - - FlushCpuCache (Image->ImageBase, (UINT64) Image->ImageSize); -} diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.c b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.c index d2428ea1b6..f3899bc8e1 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.c +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.c @@ -352,6 +352,7 @@ RuntimeDriverSetVirtualAddressMap ( IN EFI_MEMORY_DESCRIPTOR *VirtualMap ) { + EFI_STATUS Status; RUNTIME_NOTIFY_EVENT_DATA *RuntimeEvent; RUNTIME_IMAGE_RELOCATION_DATA *RuntimeImage; LIST_ENTRY *Link; @@ -359,6 +360,7 @@ RuntimeDriverSetVirtualAddressMap ( UINTN Index1; EFI_DRIVER_OS_HANDOFF_HEADER *DriverOsHandoffHeader; EFI_DRIVER_OS_HANDOFF *DriverOsHandoff; + EFI_PHYSICAL_ADDRESS VirtImageBase; #if (EFI_SPECIFICATION_VERSION >= 0x00020000) EFI_CAPSULE_TABLE *CapsuleTable; #endif @@ -454,7 +456,19 @@ RuntimeDriverSetVirtualAddressMap ( for (Link = mRelocationList.ForwardLink; Link != &mRelocationList; Link = Link->ForwardLink) { RuntimeImage = _CR (Link, RUNTIME_IMAGE_RELOCATION_DATA, Link); if (RuntimeImage->Valid) { - RelocatePeImageForRuntime (RuntimeImage); + + VirtImageBase = RuntimeImage->ImageBase; + Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase); + ASSERT_EFI_ERROR (Status); + + PeCoffLoaderRelocateImageForRuntime ( + RuntimeImage->ImageBase, + VirtImageBase, + RuntimeImage->ImageSize, + RuntimeImage->RelocationData + ); + + FlushCpuCache (RuntimeImage->ImageBase, (UINT64)RuntimeImage->ImageSize); } } // diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.h b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.h index 3803a9dadd..5b582c76ba 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.h +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.h @@ -53,15 +53,6 @@ RelocatePeImageForRuntime ( ) ; -EFI_STATUS -PeHotRelocateImageEx ( - IN UINT16 *Reloc, - IN OUT CHAR8 *Fixup, - IN OUT CHAR8 **FixupData, - IN UINT64 Adjust - ) -; - EFI_STATUS EFIAPI RuntimeDriverCalculateCrc32 ( diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.msa b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.msa index c1373e1ea4..5773156f58 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.msa +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.msa @@ -60,7 +60,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.--> Runtime.dxs - PeHotRelocate.c Runtime.c Runtime.h Crc32.c diff --git a/EdkModulePkg/Universal/Runtime/RuntimeDxe/x64/PeHotRelocateEx.c b/EdkModulePkg/Universal/Runtime/RuntimeDxe/x64/PeHotRelocateEx.c index 82d464aebe..7678c4d365 100644 --- a/EdkModulePkg/Universal/Runtime/RuntimeDxe/x64/PeHotRelocateEx.c +++ b/EdkModulePkg/Universal/Runtime/RuntimeDxe/x64/PeHotRelocateEx.c @@ -24,38 +24,6 @@ Revision History #include "Runtime.h" -EFI_STATUS -PeHotRelocateImageEx ( - IN UINT16 *Reloc, - IN OUT CHAR8 *Fixup, - IN OUT CHAR8 **FixupData, - IN UINT64 Adjust - ) -/*++ - -Routine Description: - - Performs an Itanium-based platform specific relocation fixup - -Arguments: - - Reloc - Pointer to the relocation record - - Fixup - Pointer to the address to fix up - - FixupData - Pointer to a buffer to log the fixups - - Adjust - The offset to adjust the fixup - -Returns: - - EFI_SUCCESS - ---*/ -{ - return EFI_SUCCESS; - -} // // Cache Flush Routine. diff --git a/EdkNt32Pkg/Dxe/PlatformBds/Generic/BootMaint/BootOption.c b/EdkNt32Pkg/Dxe/PlatformBds/Generic/BootMaint/BootOption.c index 3c21b12a1c..13e82759e1 100644 --- a/EdkNt32Pkg/Dxe/PlatformBds/Generic/BootMaint/BootOption.c +++ b/EdkNt32Pkg/Dxe/PlatformBds/Generic/BootMaint/BootOption.c @@ -1165,6 +1165,30 @@ Returns: return FALSE; } + +RETURN_STATUS +EFIAPI +IsEfiAppReadFromFile ( + IN VOID *FileHandle, + IN UINTN FileOffset, + IN OUT UINTN *ReadSize, + OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + EFI_FILE_HANDLE File; + + File = (EFI_FILE_HANDLE)FileHandle; + Status = File->SetPosition (File, FileOffset); + if (EFI_ERROR (Status)) { + return Status; + } + + return File->Read (File, ReadSize, Buffer); +} + + + BOOLEAN BOpt_IsEfiApp ( IN EFI_FILE_HANDLE Dir, @@ -1185,60 +1209,32 @@ Returns: --*/ { - UINTN BufferSize; - EFI_IMAGE_DOS_HEADER DosHdr; - EFI_IMAGE_NT_HEADERS PeHdr; - EFI_IMAGE_OPTIONAL_HEADER32 *PeOpt32; - EFI_IMAGE_OPTIONAL_HEADER64 *PeOpt64; - UINT16 Subsystem; - EFI_FILE_HANDLE File; - EFI_STATUS Status; + EFI_STATUS Status; + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + EFI_FILE_HANDLE File; Status = Dir->Open (Dir, &File, FileName, EFI_FILE_MODE_READ, 0); - if (EFI_ERROR (Status)) { return FALSE; } - BufferSize = sizeof (EFI_IMAGE_DOS_HEADER); - File->Read (File, &BufferSize, &DosHdr); - if (DosHdr.e_magic != EFI_IMAGE_DOS_SIGNATURE) { - File->Close (File); - return FALSE; - } + ZeroMem (&ImageContext, sizeof (ImageContext)); + ImageContext.Handle = (VOID *)File; + ImageContext.ImageRead = IsEfiAppReadFromFile; - File->SetPosition (File, DosHdr.e_lfanew); - BufferSize = sizeof (EFI_IMAGE_NT_HEADERS); - File->Read (File, &BufferSize, &PeHdr); - if (PeHdr.Signature != EFI_IMAGE_NT_SIGNATURE) { - File->Close (File); - return FALSE; - } - // - // Determine PE type and read subsytem - // BugBug : We should be using EFI_IMAGE_MACHINE_TYPE_SUPPORTED (machine) - // macro to detect the machine type. - // We should not be using EFI_IMAGE_OPTIONAL_HEADER32 and - // EFI_IMAGE_OPTIONAL_HEADER64 - // - if (PeHdr.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { - PeOpt32 = (EFI_IMAGE_OPTIONAL_HEADER32 *) &(PeHdr.OptionalHeader); - Subsystem = PeOpt32->Subsystem; - } else if (PeHdr.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { - PeOpt64 = (EFI_IMAGE_OPTIONAL_HEADER64 *) &(PeHdr.OptionalHeader); - Subsystem = PeOpt64->Subsystem; - } else { + Status = PeCoffLoaderGetImageInfo (&ImageContext); + File->Close (File); + if (EFI_ERROR (Status)) { return FALSE; } - if (Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) { - File->Close (File); + if (ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) { return TRUE; } else { - File->Close (File); return FALSE; } -} + } + EFI_STATUS BOpt_FindDrivers ( diff --git a/EdkNt32Pkg/Library/EdkGenericBdsLib/Performance.c b/EdkNt32Pkg/Library/EdkGenericBdsLib/Performance.c index 320d6b7c44..c7b6bc9d5d 100644 --- a/EdkNt32Pkg/Library/EdkGenericBdsLib/Performance.c +++ b/EdkNt32Pkg/Library/EdkGenericBdsLib/Performance.c @@ -28,6 +28,9 @@ ClearDebugRegisters ( VOID ) { + // + // BugBug: We should not need to do this. We need to root cause this bug!!!! + // AsmWriteDr0 (0); AsmWriteDr1 (0); } @@ -85,6 +88,8 @@ Returns: return ; } + + STATIC CHAR8 * GetPdbPath ( @@ -107,51 +112,18 @@ Returns: --*/ { - CHAR8 *PdbPath; - UINT32 DirCount; - EFI_IMAGE_DOS_HEADER *DosHdr; - EFI_IMAGE_NT_HEADERS *NtHdr; - EFI_IMAGE_OPTIONAL_HEADER *OptionalHdr; - EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry; - EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry; - VOID *CodeViewEntryPointer; - - CodeViewEntryPointer = NULL; - PdbPath = NULL; - DosHdr = ImageBase; - - if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) { - NtHdr = (EFI_IMAGE_NT_HEADERS *) ((UINT8 *) DosHdr + DosHdr->e_lfanew); - OptionalHdr = (VOID *) &NtHdr->OptionalHeader; - DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *) &(OptionalHdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); - if (DirectoryEntry->VirtualAddress != 0) { - for (DirCount = 0; - (DirCount < DirectoryEntry->Size / sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) && CodeViewEntryPointer == NULL; - DirCount++ - ) { - DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) (DirectoryEntry->VirtualAddress + (UINTN) ImageBase + DirCount * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)); - if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) { - CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + (UINTN) ImageBase); - switch (*(UINT32 *) CodeViewEntryPointer) { - case CODEVIEW_SIGNATURE_NB10: - PdbPath = (CHAR8 *) CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY); - break; - - case CODEVIEW_SIGNATURE_RSDS: - PdbPath = (CHAR8 *) CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY); - break; - - default: - break; - } - } - } - } - } + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + + ZeroMem (&ImageContext, sizeof (ImageContext)); + ImageContext.Handle = ImageBase; + ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory; - return PdbPath; + PeCoffLoaderGetImageInfo (&ImageContext); + + return ImageContext.PdbPointer; } + STATIC VOID GetNameFromHandle ( diff --git a/EdkNt32Pkg/Nt32.fpd b/EdkNt32Pkg/Nt32.fpd index f0bd9bfbd4..24b1a53869 100644 --- a/EdkNt32Pkg/Nt32.fpd +++ b/EdkNt32Pkg/Nt32.fpd @@ -272,6 +272,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -2027,6 +2028,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + + @@ -2200,6 +2203,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + @@ -4169,6 +4173,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + diff --git a/MdePkg/Include/Common/EfiImage.h b/MdePkg/Include/Common/EfiImage.h index ebe251e479..c20a7a6f64 100644 --- a/MdePkg/Include/Common/EfiImage.h +++ b/MdePkg/Include/Common/EfiImage.h @@ -1,8 +1,10 @@ /** @file - EFI image format for PE32+. Please note some data structures are different - for IA-32 and Itanium-based images, look for UINTN and the #ifdef EFI_IA64 + EFI image format for PE32 and PE32+. Please note some data structures are + different for PE32 and PE32+. EFI_IMAGE_NT_HEADERS32 is for PE32 and + EFI_IMAGE_NT_HEADERS64 is for PE32+. - @bug Fix text - doc as defined in MSFT EFI specification. + This file is coded to the Visual Studio, Microsoft Portable Executable and + Common Object File Format Specification, Revision 8.0 - May 16, 2006. Copyright (c) 2006, Intel Corporation All rights reserved. This program and the accompanying materials @@ -26,17 +28,10 @@ #define EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION 10 #define EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11 #define EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER 12 +#define EFI_IMAGE_SUBSYSTEM_EFI_EFI_ROM 13 + #define EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER 13 -// -// BugBug: Need to get a real answer for this problem. This is not in the -// PE specification. -// -// A SAL runtime driver does not get fixed up when a transition to -// virtual mode is made. In all other cases it should be treated -// like a EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER image -// -#define EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER 13 // // PE32+ Machine type for EFI images @@ -58,7 +53,6 @@ #define EFI_IMAGE_OS2_SIGNATURE 0x454E // NE #define EFI_IMAGE_OS2_SIGNATURE_LE 0x454C // LE #define EFI_IMAGE_NT_SIGNATURE 0x00004550 // PE00 -#define EFI_IMAGE_EDOS_SIGNATURE 0x44454550 // PEED /// /// PE images can start with an optional DOS header, so if an image is run @@ -158,9 +152,9 @@ typedef struct { /// /// @attention -/// EFI_IMAGE_OPTIONAL_HEADER32 and EFI_IMAGE_OPTIONAL_HEADER64 -/// are for use ONLY by tools. All proper EFI code MUST use -/// EFI_IMAGE_OPTIONAL_HEADER ONLY!!! +/// EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC means PE32 and +/// EFI_IMAGE_OPTIONAL_HEADER32 must be used. The data structures only vary +/// after NT additional fields. /// #define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b @@ -206,9 +200,9 @@ typedef struct { /// /// @attention -/// EFI_IMAGE_OPTIONAL_HEADER32 and EFI_IMAGE_OPTIONAL_HEADER64 -/// are for use ONLY by tools. All proper EFI code MUST use -/// EFI_IMAGE_OPTIONAL_HEADER ONLY!!! +/// EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC means PE32+ and +/// EFI_IMAGE_OPTIONAL_HEADER64 must be used. The data structures only vary +/// after NT additional fields. /// #define EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b @@ -251,6 +245,7 @@ typedef struct { EFI_IMAGE_DATA_DIRECTORY DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES]; } EFI_IMAGE_OPTIONAL_HEADER64; + /// /// @attention /// EFI_IMAGE_NT_HEADERS32 and EFI_IMAGE_HEADERS64 are for use ONLY @@ -272,6 +267,7 @@ typedef struct { #define EFI_IMAGE_SIZEOF_NT_OPTIONAL64_HEADER sizeof (EFI_IMAGE_NT_HEADERS64) + // // Processor specific definition of EFI_IMAGE_OPTIONAL_HEADER so the // type name EFI_IMAGE_OPTIONAL_HEADER is appropriate to the build. Same for @@ -279,31 +275,35 @@ typedef struct { // #if defined (MDE_CPU_IA32) -typedef EFI_IMAGE_OPTIONAL_HEADER32 EFI_IMAGE_OPTIONAL_HEADER; -typedef EFI_IMAGE_NT_HEADERS32 EFI_IMAGE_NT_HEADERS; - -#define EFI_IMAGE_NT_OPTIONAL_HDR_MAGIC EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ (((Machine) == EFI_IMAGE_MACHINE_IA32) || ((Machine) == EFI_IMAGE_MACHINE_EBC)) -#elif defined (MDE_CPU_IPF) +#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_X64) + +// +// @bug - Remove me when other package updated. +// +typedef EFI_IMAGE_NT_HEADERS32 EFI_IMAGE_NT_HEADERS; -typedef EFI_IMAGE_OPTIONAL_HEADER64 EFI_IMAGE_OPTIONAL_HEADER; -typedef EFI_IMAGE_NT_HEADERS64 EFI_IMAGE_NT_HEADERS; +#elif defined (MDE_CPU_IPF) -#define EFI_IMAGE_NT_OPTIONAL_HDR_MAGIC EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ (((Machine) == EFI_IMAGE_MACHINE_IPF) || ((Machine) == EFI_IMAGE_MACHINE_EBC)) -#elif defined (MDE_CPU_X64) +#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE) -typedef EFI_IMAGE_OPTIONAL_HEADER64 EFI_IMAGE_OPTIONAL_HEADER; -typedef EFI_IMAGE_NT_HEADERS64 EFI_IMAGE_NT_HEADERS; +#elif defined (MDE_CPU_X64) -#define EFI_IMAGE_NT_OPTIONAL_HDR_MAGIC EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ (((Machine) == EFI_IMAGE_MACHINE_X64) || ((Machine) == EFI_IMAGE_MACHINE_EBC)) +#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_IA32) + +// +// @bug - Remove me when other package updated. +// +typedef EFI_IMAGE_NT_HEADERS32 EFI_IMAGE_NT_HEADERS; + #elif defined (MDE_CPU_EBC) // @@ -311,12 +311,10 @@ typedef EFI_IMAGE_NT_HEADERS64 EFI_IMAGE_NT_HEADERS; // It does not make sense to have a PE loader coded in EBC. You need to // understand the basic // -typedef EFI_IMAGE_OPTIONAL_HEADER64 EFI_IMAGE_OPTIONAL_HEADER; -typedef EFI_IMAGE_NT_HEADERS64 EFI_IMAGE_NT_HEADERS; - -#define EFI_IMAGE_NT_OPTIONAL_HDR_MAGIC EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_EBC) +#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE) + #else #error Unknown Processor Type #endif @@ -515,15 +513,36 @@ typedef struct { // // I386 relocation types. // -#define EFI_IMAGE_REL_I386_ABSOLUTE 0 // Reference is absolute, no relocation is necessary -#define EFI_IMAGE_REL_I386_DIR16 01 // Direct 16-bit reference to the symbols virtual address -#define EFI_IMAGE_REL_I386_REL16 02 // PC-relative 16-bit reference to the symbols virtual address -#define EFI_IMAGE_REL_I386_DIR32 06 // Direct 32-bit reference to the symbols virtual address -#define EFI_IMAGE_REL_I386_DIR32NB 07 // Direct 32-bit reference to the symbols virtual address, base not included -#define EFI_IMAGE_REL_I386_SEG12 011 // Direct 16-bit reference to the segment-selector bits of a 32-bit virtual address -#define EFI_IMAGE_REL_I386_SECTION 012 -#define EFI_IMAGE_REL_I386_SECREL 013 -#define EFI_IMAGE_REL_I386_REL32 024 // PC-relative 32-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_ABSOLUTE 0x0000 // Reference is absolute, no relocation is necessary +#define EFI_IMAGE_REL_I386_DIR16 0x0001 // Direct 16-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_REL16 0x0002 // PC-relative 16-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_DIR32 0x0006 // Direct 32-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_DIR32NB 0x0007 // Direct 32-bit reference to the symbols virtual address, base not included +#define EFI_IMAGE_REL_I386_SEG12 0x0009 // Direct 16-bit reference to the segment-selector bits of a 32-bit virtual address +#define EFI_IMAGE_REL_I386_SECTION 0x001a +#define EFI_IMAGE_REL_I386_SECREL 0x000b +#define EFI_IMAGE_REL_I386_REL32 0x0014 // PC-relative 32-bit reference to the symbols virtual address + +// +// x64 processor relocation types. +// +#define IMAGE_REL_AMD64_ABSOLUTE 0x0000 +#define IMAGE_REL_AMD64_ADDR64 0x0001 +#define IMAGE_REL_AMD64_ADDR32 0x0002 +#define IMAGE_REL_AMD64_ADDR32NB 0x0003 +#define IMAGE_REL_AMD64_REL32 0x0004 +#define IMAGE_REL_AMD64_REL32_1 0x0005 +#define IMAGE_REL_AMD64_REL32_2 0x0006 +#define IMAGE_REL_AMD64_REL32_3 0x0007 +#define IMAGE_REL_AMD64_REL32_4 0x0008 +#define IMAGE_REL_AMD64_REL32_5 0x0009 +#define IMAGE_REL_AMD64_SECTION 0x000A +#define IMAGE_REL_AMD64_SECREL 0x000B +#define IMAGE_REL_AMD64_SECREL7 0x000C +#define IMAGE_REL_AMD64_TOKEN 0x000D +#define IMAGE_REL_AMD64_SREL32 0x000E +#define IMAGE_REL_AMD64_PAIR 0x000F +#define IMAGE_REL_AMD64_SSPAN32 0x0010 /// /// Based relocation format. @@ -695,4 +714,21 @@ typedef struct { #define EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC 0 #define EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG 1 + +// +// Union of PE32, PE32+, and TE headers +// +typedef union { + EFI_IMAGE_NT_HEADERS32 Pe32; + EFI_IMAGE_NT_HEADERS64 Pe32Plus; + EFI_TE_IMAGE_HEADER Te; +} EFI_IMAGE_OPTIONAL_HEADER_UNION; + +typedef union { + EFI_IMAGE_NT_HEADERS32 *Pe32; + EFI_IMAGE_NT_HEADERS64 *Pe32Plus; + EFI_TE_IMAGE_HEADER *Te; + EFI_IMAGE_OPTIONAL_HEADER_UNION *Union; +} EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION; + #endif diff --git a/MdePkg/Include/Library/PciCf8Lib.h b/MdePkg/Include/Library/PciCf8Lib.h index e2808418fa..13af84ed34 100644 --- a/MdePkg/Include/Library/PciCf8Lib.h +++ b/MdePkg/Include/Library/PciCf8Lib.h @@ -17,7 +17,6 @@ #ifndef __PCI_CF8_LIB_H__ #define __PCI_CF8_LIB_H__ -#include /** Macro that converts PCI Bus, PCI Device, PCI Function and PCI Register to an @@ -36,7 +35,7 @@ **/ #define PCI_CF8_LIB_ADDRESS(Bus,Device,Function,Offset) \ - PCI_LIB_ADDRESS (Bus, Device, Function, Offset) + (((Offset) & 0xfff) | (((Function) & 0x07) << 12) | (((Device) & 0x1f) << 15) | (((Bus) & 0xff) << 20)) /** Reads an 8-bit PCI configuration register. diff --git a/MdePkg/Include/Library/PciExpressLib.h b/MdePkg/Include/Library/PciExpressLib.h index 9bd1166a87..29ff9dde82 100644 --- a/MdePkg/Include/Library/PciExpressLib.h +++ b/MdePkg/Include/Library/PciExpressLib.h @@ -36,7 +36,7 @@ **/ #define PCI_EXPRESS_LIB_ADDRESS(Bus,Device,Function,Offset) \ - PCI_LIB_ADDRESS (Bus, Device, Function, Offset) + (((Offset) & 0xfff) | (((Function) & 0x07) << 12) | (((Device) & 0x1f) << 15) | (((Bus) & 0xff) << 20)) /** Reads an 8-bit PCI configuration register. diff --git a/MdePkg/Include/Library/PeCoffGetEntryPointLib.h b/MdePkg/Include/Library/PeCoffGetEntryPointLib.h index 85e3e25c1e..973a368655 100644 --- a/MdePkg/Include/Library/PeCoffGetEntryPointLib.h +++ b/MdePkg/Include/Library/PeCoffGetEntryPointLib.h @@ -41,4 +41,19 @@ PeCoffLoaderGetEntryPoint ( OUT VOID **EntryPoint ); +/** + Returns the machine type of PE/COFF image. + + @param Image Pointer to a PE/COFF header + + @return Machine type or zero if not a valid iamge + +**/ +UINT16 +EFIAPI +PeCoffLoaderGetMachineType ( + IN VOID *Image + ); + + #endif diff --git a/MdePkg/Include/Library/PeCoffLib.h b/MdePkg/Include/Library/PeCoffLib.h index df588b704d..920004aebc 100644 --- a/MdePkg/Include/Library/PeCoffLib.h +++ b/MdePkg/Include/Library/PeCoffLib.h @@ -1,5 +1,5 @@ /** @file - Memory Only PE COFF loader + Memory Only PE COFF loader. Copyright (c) 2006, Intel Corporation All rights reserved. This program and the accompanying materials @@ -157,4 +157,53 @@ PeCoffLoaderLoadImage ( ) ; + +/** + ImageRead function that operates on a memory buffer whos base is passed into + FileHandle. + + @param FileHandle Ponter to baes of the input stream + @param FileOffset Offset to the start of the buffer + @param ReadSize Number of bytes to copy into the buffer + @param Buffer Location to place results of read + + @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into + the buffer. +**/ +RETURN_STATUS +EFIAPI +PeCoffLoaderImageReadFromMemory ( + IN VOID *FileHandle, + IN UINTN FileOffset, + IN OUT UINTN *ReadSize, + OUT VOID *Buffer + ) +; + + +/** + Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI + runtime. + + PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply + the fixups with a virtual mapping. + + + @param ImageBase Base address of relocated image + @param VirtImageBase Virtual mapping for ImageBase + @param ImageSize Size of the image to relocate + @param RelocationData Location to place results of read + +**/ +VOID +EFIAPI +PeCoffLoaderRelocateImageForRuntime ( + IN PHYSICAL_ADDRESS ImageBase, + IN PHYSICAL_ADDRESS VirtImageBase, + IN UINTN ImageSize, + IN VOID *RelocationData + ) +; + + #endif diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c index 126457a7e5..02c03b7aab 100644 --- a/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c +++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c @@ -64,192 +64,3 @@ IoWrite64 ( return 0; } -/** - Reads an 8-bit MMIO register. - - Reads the 8-bit MMIO register specified by Address. The 8-bit read value is - returned. This function must guarantee that all MMIO read and write - operations are serialized. - - If 8-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to read. - - @return The value read. - -**/ -UINT8 -EFIAPI -MmioRead8 ( - IN UINTN Address - ) -{ - return *(volatile UINT8*)Address; -} - -/** - Writes an 8-bit MMIO register. - - Writes the 8-bit MMIO register specified by Address with the value specified - by Value and returns Value. This function must guarantee that all MMIO read - and write operations are serialized. - - If 8-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to write. - @param Value The value to write to the MMIO register. - -**/ -UINT8 -EFIAPI -MmioWrite8 ( - IN UINTN Address, - IN UINT8 Value - ) -{ - return *(volatile UINT8*)Address = Value; -} - -/** - Reads a 16-bit MMIO register. - - Reads the 16-bit MMIO register specified by Address. The 16-bit read value is - returned. This function must guarantee that all MMIO read and write - operations are serialized. - - If 16-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to read. - - @return The value read. - -**/ -UINT16 -EFIAPI -MmioRead16 ( - IN UINTN Address - ) -{ - ASSERT ((Address & 1) == 0); - return *(volatile UINT16*)Address; -} - -/** - Writes a 16-bit MMIO register. - - Writes the 16-bit MMIO register specified by Address with the value specified - by Value and returns Value. This function must guarantee that all MMIO read - and write operations are serialized. - - If 16-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to write. - @param Value The value to write to the MMIO register. - -**/ -UINT16 -EFIAPI -MmioWrite16 ( - IN UINTN Address, - IN UINT16 Value - ) -{ - ASSERT ((Address & 1) == 0); - return *(volatile UINT16*)Address = Value; -} - -/** - Reads a 32-bit MMIO register. - - Reads the 32-bit MMIO register specified by Address. The 32-bit read value is - returned. This function must guarantee that all MMIO read and write - operations are serialized. - - If 32-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to read. - - @return The value read. - -**/ -UINT32 -EFIAPI -MmioRead32 ( - IN UINTN Address - ) -{ - ASSERT ((Address & 3) == 0); - return *(volatile UINT32*)Address; -} - -/** - Writes a 32-bit MMIO register. - - Writes the 32-bit MMIO register specified by Address with the value specified - by Value and returns Value. This function must guarantee that all MMIO read - and write operations are serialized. - - If 32-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to write. - @param Value The value to write to the MMIO register. - -**/ -UINT32 -EFIAPI -MmioWrite32 ( - IN UINTN Address, - IN UINT32 Value - ) -{ - ASSERT ((Address & 3) == 0); - return *(volatile UINT32*)Address = Value; -} - -/** - Reads a 64-bit MMIO register. - - Reads the 64-bit MMIO register specified by Address. The 64-bit read value is - returned. This function must guarantee that all MMIO read and write - operations are serialized. - - If 64-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to read. - - @return The value read. - -**/ -UINT64 -EFIAPI -MmioRead64 ( - IN UINTN Address - ) -{ - ASSERT ((Address & 7) == 0); - return *(volatile UINT64*)Address; -} - -/** - Writes a 64-bit MMIO register. - - Writes the 64-bit MMIO register specified by Address with the value specified - by Value and returns Value. This function must guarantee that all MMIO read - and write operations are serialized. - - If 64-bit MMIO register operations are not supported, then ASSERT(). - - @param Address The MMIO register to write. - @param Value The value to write to the MMIO register. - -**/ -UINT64 -EFIAPI -MmioWrite64 ( - IN UINTN Address, - IN UINT64 Value - ) -{ - ASSERT ((Address & 7) == 0); - return *(volatile UINT64*)Address = Value; -} diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c index a065c14299..69fb878888 100644 --- a/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c +++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c @@ -25,6 +25,198 @@ #ifdef __GNUC__ +/** + Reads an 8-bit MMIO register. + + Reads the 8-bit MMIO register specified by Address. The 8-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 8-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT8 +EFIAPI +MmioRead8 ( + IN UINTN Address + ) +{ + return *(volatile UINT8*)Address; +} + +/** + Writes an 8-bit MMIO register. + + Writes the 8-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 8-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT8 +EFIAPI +MmioWrite8 ( + IN UINTN Address, + IN UINT8 Value + ) +{ + return *(volatile UINT8*)Address = Value; +} + +/** + Reads a 16-bit MMIO register. + + Reads the 16-bit MMIO register specified by Address. The 16-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 16-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT16 +EFIAPI +MmioRead16 ( + IN UINTN Address + ) +{ + ASSERT ((Address & 1) == 0); + return *(volatile UINT16*)Address; +} + +/** + Writes a 16-bit MMIO register. + + Writes the 16-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 16-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT16 +EFIAPI +MmioWrite16 ( + IN UINTN Address, + IN UINT16 Value + ) +{ + ASSERT ((Address & 1) == 0); + return *(volatile UINT16*)Address = Value; +} + +/** + Reads a 32-bit MMIO register. + + Reads the 32-bit MMIO register specified by Address. The 32-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 32-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT32 +EFIAPI +MmioRead32 ( + IN UINTN Address + ) +{ + ASSERT ((Address & 3) == 0); + return *(volatile UINT32*)Address; +} + +/** + Writes a 32-bit MMIO register. + + Writes the 32-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 32-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT32 +EFIAPI +MmioWrite32 ( + IN UINTN Address, + IN UINT32 Value + ) +{ + ASSERT ((Address & 3) == 0); + return *(volatile UINT32*)Address = Value; +} + +/** + Reads a 64-bit MMIO register. + + Reads the 64-bit MMIO register specified by Address. The 64-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 64-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT64 +EFIAPI +MmioRead64 ( + IN UINTN Address + ) +{ + ASSERT ((Address & 7) == 0); + return *(volatile UINT64*)Address; +} + +/** + Writes a 64-bit MMIO register. + + Writes the 64-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 64-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT64 +EFIAPI +MmioWrite64 ( + IN UINTN Address, + IN UINT64 Value + ) +{ + ASSERT ((Address & 7) == 0); + return *(volatile UINT64*)Address = Value; +} + + + /** Reads an 8-bit I/O port. diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c index 8bce7a5eb9..cfe5ddff43 100644 --- a/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c +++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c @@ -27,12 +27,13 @@ // // Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics // -int _inp (unsigned short port); +int _inp (unsigned short port); unsigned short _inpw (unsigned short port); unsigned long _inpd (unsigned short port); int _outp (unsigned short port, int databyte ); -unsigned short _outpw(unsigned short port, unsigned short dataword ); -unsigned long _outpd(unsigned short port, unsigned long dataword ); +unsigned short _outpw (unsigned short port, unsigned short dataword ); +unsigned long _outpd (unsigned short port, unsigned long dataword ); +void _ReadWriteBarrier (void); #pragma intrinsic(_inp) #pragma intrinsic(_inpw) @@ -40,7 +41,15 @@ unsigned long _outpd(unsigned short port, unsigned long dataword ); #pragma intrinsic(_outp) #pragma intrinsic(_outpw) #pragma intrinsic(_outpd) +#pragma intrinsic(_ReadWriteBarrier) +// +// _ReadWriteBarrier() forces memory reads and writes to complete at the point +// in the call. This is only a hint to the compiler and does emit code. +// In past versions of the compiler, _ReadWriteBarrier was enforced only +// locally and did not affect functions up the call tree. In Visual C++ +// 2005, _ReadWriteBarrier is enforced all the way up the call tree. +// /** Reads an 8-bit I/O port. @@ -62,6 +71,7 @@ IoRead8 ( IN UINTN Port ) { + _ReadWriteBarrier (); return (UINT8)_inp ((UINT16)Port); } @@ -87,6 +97,7 @@ IoWrite8 ( IN UINT8 Value ) { + _ReadWriteBarrier (); return (UINT8)_outp ((UINT16)Port, Value); } @@ -111,6 +122,7 @@ IoRead16 ( ) { ASSERT ((Port & 1) == 0); + _ReadWriteBarrier (); return _inpw((UINT16)Port); } @@ -137,6 +149,7 @@ IoWrite16 ( ) { ASSERT ((Port & 1) == 0); + _ReadWriteBarrier (); return _outpw ((UINT16)Port, Value); } @@ -161,6 +174,7 @@ IoRead32 ( ) { ASSERT ((Port & 3) == 0); + _ReadWriteBarrier (); return _inpd((UINT16)Port); } @@ -187,7 +201,207 @@ IoWrite32 ( ) { ASSERT ((Port & 3) == 0); + _ReadWriteBarrier (); return _outpd ((UINT16)Port, Value); } + +/** + Reads an 8-bit MMIO register. + + Reads the 8-bit MMIO register specified by Address. The 8-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 8-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT8 +EFIAPI +MmioRead8 ( + IN UINTN Address + ) +{ + _ReadWriteBarrier (); + return *(volatile UINT8 *)Address; +} + +/** + Writes an 8-bit MMIO register. + + Writes the 8-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 8-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT8 +EFIAPI +MmioWrite8 ( + IN UINTN Address, + IN UINT8 Value + ) +{ + _ReadWriteBarrier (); + return *(volatile UINT8 *)Address = Value; +} + +/** + Reads a 16-bit MMIO register. + + Reads the 16-bit MMIO register specified by Address. The 16-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 16-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT16 +EFIAPI +MmioRead16 ( + IN UINTN Address + ) +{ + ASSERT ((Address & 1) == 0); + _ReadWriteBarrier (); + return *(volatile UINT16 *)Address; +} + +/** + Writes a 16-bit MMIO register. + + Writes the 16-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 16-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT16 +EFIAPI +MmioWrite16 ( + IN UINTN Address, + IN UINT16 Value + ) +{ + ASSERT ((Address & 1) == 0); + _ReadWriteBarrier (); + return *(volatile UINT16 *)Address = Value; +} + +/** + Reads a 32-bit MMIO register. + + Reads the 32-bit MMIO register specified by Address. The 32-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 32-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT32 +EFIAPI +MmioRead32 ( + IN UINTN Address + ) +{ + ASSERT ((Address & 3) == 0); + _ReadWriteBarrier (); + return *(volatile UINT32 *)Address; +} + +/** + Writes a 32-bit MMIO register. + + Writes the 32-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 32-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT32 +EFIAPI +MmioWrite32 ( + IN UINTN Address, + IN UINT32 Value + ) +{ + ASSERT ((Address & 3) == 0); + _ReadWriteBarrier (); + return *(volatile UINT32 *)Address = Value; +} + +/** + Reads a 64-bit MMIO register. + + Reads the 64-bit MMIO register specified by Address. The 64-bit read value is + returned. This function must guarantee that all MMIO read and write + operations are serialized. + + If 64-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT64 +EFIAPI +MmioRead64 ( + IN UINTN Address + ) +{ + ASSERT ((Address & 7) == 0); + _ReadWriteBarrier (); + return *(volatile UINT64 *)Address; +} + +/** + Writes a 64-bit MMIO register. + + Writes the 64-bit MMIO register specified by Address with the value specified + by Value and returns Value. This function must guarantee that all MMIO read + and write operations are serialized. + + If 64-bit MMIO register operations are not supported, then ASSERT(). + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT64 +EFIAPI +MmioWrite64 ( + IN UINTN Address, + IN UINT64 Value + ) +{ + ASSERT ((Address & 7) == 0); + _ReadWriteBarrier (); + return *(volatile UINT64 *)Address = Value; +} + #endif diff --git a/MdePkg/Library/BasePciCf8Lib/PciLib.c b/MdePkg/Library/BasePciCf8Lib/PciLib.c index cbbd407ead..272d641ff3 100644 --- a/MdePkg/Library/BasePciCf8Lib/PciLib.c +++ b/MdePkg/Library/BasePciCf8Lib/PciLib.c @@ -1315,7 +1315,7 @@ PciCf8ReadBuffer ( // // Read a byte if StartAddress is byte aligned // - *(UINT8*)Buffer = PciCf8Read8 (StartAddress); + *(volatile UINT8 *)Buffer = PciCf8Read8 (StartAddress); StartAddress += sizeof (UINT8); Size -= sizeof (UINT8); Buffer = (UINT8*)Buffer + 1; @@ -1325,7 +1325,7 @@ PciCf8ReadBuffer ( // // Read a word if StartAddress is word aligned // - *(UINT16*)Buffer = PciCf8Read16 (StartAddress); + *(volatile UINT16 *)Buffer = PciCf8Read16 (StartAddress); StartAddress += sizeof (UINT16); Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; @@ -1335,7 +1335,7 @@ PciCf8ReadBuffer ( // // Read as many double words as possible // - *(UINT32*)Buffer = PciCf8Read32 (StartAddress); + *(volatile UINT32 *)Buffer = PciCf8Read32 (StartAddress); StartAddress += sizeof (UINT32); Size -= sizeof (UINT32); Buffer = (UINT32*)Buffer + 1; @@ -1345,7 +1345,7 @@ PciCf8ReadBuffer ( // // Read the last remaining word if exist // - *(UINT16*)Buffer = PciCf8Read16 (StartAddress); + *(volatile UINT16 *)Buffer = PciCf8Read16 (StartAddress); StartAddress += sizeof (UINT16); Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; @@ -1355,7 +1355,7 @@ PciCf8ReadBuffer ( // // Read the last remaining byte if exist // - *(UINT8*)Buffer = PciCf8Read8 (StartAddress); + *(volatile UINT8 *)Buffer = PciCf8Read8 (StartAddress); } return ReturnValue; diff --git a/MdePkg/Library/BasePciExpressLib/PciLib.c b/MdePkg/Library/BasePciExpressLib/PciLib.c index 509a5e423f..6bb0893887 100644 --- a/MdePkg/Library/BasePciExpressLib/PciLib.c +++ b/MdePkg/Library/BasePciExpressLib/PciLib.c @@ -40,7 +40,7 @@ @return The base address of PCI Express. **/ -UINTN +volatile UINTN GetPciExpressBaseAddress ( VOID ) @@ -1220,7 +1220,7 @@ PciExpressReadBuffer ( // // Read a byte if StartAddress is byte aligned // - *(UINT8*)Buffer = PciExpressRead8 (StartAddress); + *(volatile UINT8 *)Buffer = PciExpressRead8 (StartAddress); StartAddress += sizeof (UINT8); Size -= sizeof (UINT8); Buffer = (UINT8*)Buffer + 1; @@ -1230,7 +1230,7 @@ PciExpressReadBuffer ( // // Read a word if StartAddress is word aligned // - *(UINT16*)Buffer = PciExpressRead16 (StartAddress); + *(volatile UINT16 *)Buffer = PciExpressRead16 (StartAddress); StartAddress += sizeof (UINT16); Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; @@ -1240,7 +1240,7 @@ PciExpressReadBuffer ( // // Read as many double words as possible // - *(UINT32*)Buffer = PciExpressRead32 (StartAddress); + *(volatile UINT32 *)Buffer = PciExpressRead32 (StartAddress); StartAddress += sizeof (UINT32); Size -= sizeof (UINT32); Buffer = (UINT32*)Buffer + 1; @@ -1250,7 +1250,7 @@ PciExpressReadBuffer ( // // Read the last remaining word if exist // - *(UINT16*)Buffer = PciExpressRead16 (StartAddress); + *(volatile UINT16 *)Buffer = PciExpressRead16 (StartAddress); StartAddress += sizeof (UINT16); Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; @@ -1260,7 +1260,7 @@ PciExpressReadBuffer ( // // Read the last remaining byte if exist // - *(UINT8*)Buffer = PciExpressRead8 (StartAddress); + *(volatile UINT8 *)Buffer = PciExpressRead8 (StartAddress); } return ReturnValue; diff --git a/MdePkg/Library/BasePeCoffGetEntryPointLib/PeCoffGetEntryPoint.c b/MdePkg/Library/BasePeCoffGetEntryPointLib/PeCoffGetEntryPoint.c index b740bd6e7c..001e043d3f 100644 --- a/MdePkg/Library/BasePeCoffGetEntryPointLib/PeCoffGetEntryPoint.c +++ b/MdePkg/Library/BasePeCoffGetEntryPointLib/PeCoffGetEntryPoint.c @@ -40,26 +40,63 @@ PeCoffLoaderGetEntryPoint ( OUT VOID **EntryPoint ) { - EFI_IMAGE_DOS_HEADER *DosHeader; - EFI_IMAGE_NT_HEADERS *PeHeader; + EFI_IMAGE_DOS_HEADER *DosHeader; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header; ASSERT (Pe32Data != NULL); ASSERT (EntryPoint != NULL); DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data; - if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) { // // DOS image header is present, so read the PE header after the DOS image header. // - PeHeader = (EFI_IMAGE_NT_HEADERS *) ((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff)); + Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff)); } else { // // DOS image header is not present, so PE header is at the image base. // - PeHeader = (EFI_IMAGE_NT_HEADERS *) Pe32Data; + Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data; } - *EntryPoint = (VOID *) ((UINTN) Pe32Data + (UINTN) (PeHeader->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff)); + // + // Calculate the entry point relative to the start of the image. + // AddressOfEntryPoint is common for PE32 & PE32+ + // + *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Header.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff)); return RETURN_SUCCESS; } + + +/** + Returns the machine type of PE/COFF image. + + @param Image Pointer to a PE/COFF header + + @return Machine type or zero if not a valid iamge + +**/ +UINT16 +EFIAPI +PeCoffLoaderGetMachineType ( + IN VOID *Pe32Data + ) +{ + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_DOS_HEADER *DosHdr; + + DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data; + if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) { + Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + DosHdr->e_lfanew); + } else { + Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data); + } + + if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) { + return Hdr.Pe32->FileHeader.Machine; + } + + return 0x0000; +} + + diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c index 6b8adc1fd6..1e8f573009 100644 --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c @@ -1,6 +1,9 @@ /** @file Tiano PE/COFF loader. + This PE/COFF loader supports loading any PE32 or PE32+ image type, but + only supports relocating IA32, X64, IPF, and EBC images. + Copyright (c) 2006, Intel Corporation All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License @@ -14,8 +17,10 @@ **/ + /** - Performs an Itanium-based specific relocation fixup. + Performs an Itanium-based specific relocation fixup and is a no-op on other + instruction sets. @param Reloc Pointer to the relocation record. @param Fixup Pointer to the address to fix up. @@ -34,33 +39,68 @@ PeCoffLoaderRelocateImageEx ( ); +/** + Performs an Itanium-based specific re-relocation fixup and is a no-op on other + instruction sets. This is used to re-relocated the image into the EFI virtual + space for runtime calls. + + @param Reloc Pointer to the relocation record. + @param Fixup Pointer to the address to fix up. + @param FixupData Pointer to a buffer to log the fixups. + @param Adjust The offset to adjust the fixup. + + @return Status code. + +**/ +RETURN_STATUS +PeHotRelocateImageEx ( + IN UINT16 *Reloc, + IN OUT CHAR8 *Fixup, + IN OUT CHAR8 **FixupData, + IN UINT64 Adjust + ); + + +/** + Returns TRUE if the machine type of PE/COFF image is supported. Supported + does not mean the image can be executed it means the PE/COFF loader supports + loading and relocating of the image type. It's up to the caller to support + the entry point. + + @param Machine Machine type from the PE Header. + + @return TRUE if this PE/COFF loader can load the image + +**/ +BOOLEAN +PeCoffLoaderImageFormatSupported ( + IN UINT16 Machine + ); + + /** Retrieves the PE or TE Header from a PE/COFF or TE image. @param ImageContext The context of the image being loaded. - @param PeHdr The buffer in which to return the PE header. - @param TeHdr The buffer in which to return the TE header. + @param Hdr The buffer in which to return the PE32, PE32+, or TE header. @retval RETURN_SUCCESS The PE or TE Header is read. @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function. **/ -STATIC RETURN_STATUS PeCoffLoaderGetPeHeader ( - IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, - OUT EFI_IMAGE_NT_HEADERS *PeHdr, - OUT EFI_TE_IMAGE_HEADER *TeHdr + IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, + OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr ) { RETURN_STATUS Status; EFI_IMAGE_DOS_HEADER DosHdr; UINTN Size; - ImageContext->IsTeImage = FALSE; // - // Read the DOS image headers + // Read the DOS image header to check for it's existance // Size = sizeof (EFI_IMAGE_DOS_HEADER); Status = ImageContext->ImageRead ( @@ -77,93 +117,85 @@ PeCoffLoaderGetPeHeader ( ImageContext->PeCoffHeaderOffset = 0; if (DosHdr.e_magic == EFI_IMAGE_DOS_SIGNATURE) { // - // DOS image header is present, so read the PE header after the DOS image header + // DOS image header is present, so read the PE header after the DOS image + // header // ImageContext->PeCoffHeaderOffset = DosHdr.e_lfanew; } + // - // Read the PE/COFF Header + // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much + // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic + // determins if this is a PE32 or PE32+ image. The magic is in the same + // location in both images. // - Size = sizeof (EFI_IMAGE_NT_HEADERS); + Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION); Status = ImageContext->ImageRead ( ImageContext->Handle, ImageContext->PeCoffHeaderOffset, &Size, - PeHdr + Hdr.Pe32 ); if (RETURN_ERROR (Status)) { ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; return Status; } + // - // Check the PE/COFF Header Signature. If not, then try to read a TE header + // Use Signature to figure out if we understand the image format // - if (PeHdr->Signature != EFI_IMAGE_NT_SIGNATURE) { - Size = sizeof (EFI_TE_IMAGE_HEADER); - Status = ImageContext->ImageRead ( - ImageContext->Handle, - 0, - &Size, - TeHdr - ); - if (TeHdr->Signature != EFI_TE_IMAGE_HEADER_SIGNATURE) { - return RETURN_UNSUPPORTED; - } - - ImageContext->IsTeImage = TRUE; - } - - return RETURN_SUCCESS; -} - -/** - Checks the PE or TE header of a PE/COFF or TE image to determine if it supported. - - @param ImageContext The context of the image being loaded. - @param PeHdr The buffer in which to return the PE header. - @param TeHdr The buffer in which to return the TE header. + if (Hdr.Pe32->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) { + ImageContext->IsTeImage = TRUE; + ImageContext->Machine = Hdr.Te->Machine; + ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem); + ImageContext->ImageSize = 0; + ImageContext->SectionAlignment = 4096; + ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize; - @retval RETURN_SUCCESS The PE/COFF or TE image is supported. - @retval RETURN_UNSUPPORTED The PE/COFF or TE image is not supported. + } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) { + ImageContext->IsTeImage = FALSE; + ImageContext->Machine = Hdr.Pe32->FileHeader.Machine; + + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem; + ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage; + ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment; + ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders; -**/ -STATIC -RETURN_STATUS -PeCoffLoaderCheckImageType ( - IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, - IN EFI_IMAGE_NT_HEADERS *PeHdr, - IN EFI_TE_IMAGE_HEADER *TeHdr - ) -{ - // - // See if the machine type is supported. We support a native machine type (IA-32/Itanium-based) - // and the machine type for the Virtual Machine. - // - if (ImageContext->IsTeImage == FALSE) { - ImageContext->Machine = PeHdr->FileHeader.Machine; + } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { + // + // Use PE32+ offset + // + ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem; + ImageContext->ImageSize = (UINT64) Hdr.Pe32Plus->OptionalHeader.SizeOfImage; + ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment; + ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders; + } else { + ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE; + return RETURN_UNSUPPORTED; + } } else { - ImageContext->Machine = TeHdr->Machine; - } - - if (!(EFI_IMAGE_MACHINE_TYPE_SUPPORTED (ImageContext->Machine))) { ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE; return RETURN_UNSUPPORTED; } - // - // See if the image type is supported. We support EFI Applications, - // EFI Boot Service Drivers, and EFI Runtime Drivers. - // - if (ImageContext->IsTeImage == FALSE) { - ImageContext->ImageType = PeHdr->OptionalHeader.Subsystem; - } else { - ImageContext->ImageType = (UINT16) (TeHdr->Subsystem); + if (!PeCoffLoaderImageFormatSupported (ImageContext->Machine)) { + // + // If the PE/COFF loader does not support the image type return + // unsupported. This library can suport lots of types of images + // this does not mean the user of this library can call the entry + // point of the image. + // + return RETURN_UNSUPPORTED; } - return RETURN_SUCCESS; } + /** Retrieves information about a PE/COFF image. @@ -189,17 +221,18 @@ PeCoffLoaderGetImageInfo ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ) { - RETURN_STATUS Status; - EFI_IMAGE_NT_HEADERS PeHdr; - EFI_TE_IMAGE_HEADER TeHdr; - EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry; - UINTN Size; - UINTN Index; - UINTN DebugDirectoryEntryRva; - UINTN DebugDirectoryEntryFileOffset; - UINTN SectionHeaderOffset; - EFI_IMAGE_SECTION_HEADER SectionHeader; - EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry; + RETURN_STATUS Status; + EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry; + UINTN Size; + UINTN Index; + UINTN DebugDirectoryEntryRva; + UINTN DebugDirectoryEntryFileOffset; + UINTN SectionHeaderOffset; + EFI_IMAGE_SECTION_HEADER SectionHeader; + EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry; + UINT32 NumberOfRvaAndSizes; if (NULL == ImageContext) { return RETURN_INVALID_PARAMETER; @@ -209,25 +242,31 @@ PeCoffLoaderGetImageInfo ( // ImageContext->ImageError = IMAGE_ERROR_SUCCESS; - Status = PeCoffLoaderGetPeHeader (ImageContext, &PeHdr, &TeHdr); - if (RETURN_ERROR (Status)) { - return Status; - } - // - // Verify machine type - // - Status = PeCoffLoaderCheckImageType (ImageContext, &PeHdr, &TeHdr); + Hdr.Union = &HdrData; + Status = PeCoffLoaderGetPeHeader (ImageContext, Hdr); if (RETURN_ERROR (Status)) { return Status; } + // // Retrieve the base address of the image // if (!(ImageContext->IsTeImage)) { - ImageContext->ImageAddress = PeHdr.OptionalHeader.ImageBase; + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + ImageContext->ImageAddress = Hdr.Pe32->OptionalHeader.ImageBase; + } else { + // + // Use PE32+ offset + // + ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase; + } } else { - ImageContext->ImageAddress = (PHYSICAL_ADDRESS) (TeHdr.ImageBase); + ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase); } + // // Initialize the alternate destination address to 0 indicating that it // should not be used. @@ -251,23 +290,28 @@ PeCoffLoaderGetImageInfo ( // 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.FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) { + if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) { ImageContext->RelocationsStripped = TRUE; } else { ImageContext->RelocationsStripped = FALSE; } if (!(ImageContext->IsTeImage)) { - ImageContext->ImageSize = (UINT64) PeHdr.OptionalHeader.SizeOfImage; - ImageContext->SectionAlignment = PeHdr.OptionalHeader.SectionAlignment; - ImageContext->SizeOfHeaders = PeHdr.OptionalHeader.SizeOfHeaders; - - // - // Modify ImageSize to contain .PDB file name if required and initialize - // PdbRVA field... - // - if (PeHdr.OptionalHeader.NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) { - DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *) &(PeHdr.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes; + DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); + } else { + // + // Use PE32+ offset + // + NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes; + DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); + } + + if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) { DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress; @@ -282,10 +326,10 @@ PeCoffLoaderGetImageInfo ( ImageContext->PeCoffHeaderOffset + sizeof (UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + - PeHdr.FileHeader.SizeOfOptionalHeader + Hdr.Pe32->FileHeader.SizeOfOptionalHeader ); - for (Index = 0; Index < PeHdr.FileHeader.NumberOfSections; Index++) { + for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) { // // Read section header from file // @@ -303,8 +347,8 @@ PeCoffLoaderGetImageInfo ( if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress && DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) { - DebugDirectoryEntryFileOffset = - DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData; + + DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData; break; } @@ -340,17 +384,14 @@ PeCoffLoaderGetImageInfo ( } } } else { - ImageContext->ImageSize = 0; - ImageContext->SectionAlignment = 4096; - ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN) TeHdr.BaseOfCode - (UINTN) TeHdr.StrippedSize; - DebugDirectoryEntry = &TeHdr.DataDirectory[1]; + DebugDirectoryEntry = &Hdr.Te->DataDirectory[1]; DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress; - SectionHeaderOffset = (UINTN) (sizeof (EFI_TE_IMAGE_HEADER)); + SectionHeaderOffset = (UINTN)(sizeof (EFI_TE_IMAGE_HEADER)); DebugDirectoryEntryFileOffset = 0; - for (Index = 0; Index < TeHdr.NumberOfSections;) { + for (Index = 0; Index < Hdr.Te->NumberOfSections;) { // // Read section header from file // @@ -372,15 +413,15 @@ PeCoffLoaderGetImageInfo ( SectionHeader.VirtualAddress + SectionHeader.PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - - TeHdr.StrippedSize; + Hdr.Te->StrippedSize; // // 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. // - if (Index < (UINTN) TeHdr.NumberOfSections - 1) { - SectionHeaderOffset += (TeHdr.NumberOfSections - 1 - Index) * sizeof (EFI_IMAGE_SECTION_HEADER); - Index = TeHdr.NumberOfSections - 1; + if (Index < (UINTN) Hdr.Te->NumberOfSections - 1) { + SectionHeaderOffset += (Hdr.Te->NumberOfSections - 1 - Index) * sizeof (EFI_IMAGE_SECTION_HEADER); + Index = Hdr.Te->NumberOfSections - 1; continue; } } @@ -395,7 +436,7 @@ PeCoffLoaderGetImageInfo ( // by the RVA and the VirtualSize of the last section header in the // Section Table. // - if ((++Index) == (UINTN) TeHdr.NumberOfSections) { + if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) { ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize + ImageContext->SectionAlignment - 1) & ~(ImageContext->SectionAlignment - 1); } @@ -431,6 +472,7 @@ PeCoffLoaderGetImageInfo ( return RETURN_SUCCESS; } + /** Converts an image address to the loaded address. @@ -440,19 +482,13 @@ PeCoffLoaderGetImageInfo ( @return The converted address or NULL if the address can not be converted. **/ -STATIC VOID * PeCoffLoaderImageAddress ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, IN UINTN Address ) { - if (Address >= ImageContext->ImageSize) { - ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS; - return NULL; - } - - return (CHAR8 *) ((UINTN) ImageContext->ImageAddress + Address); + return (CHAR8 *)((UINTN) ImageContext->ImageAddress + Address); } /** @@ -481,26 +517,25 @@ PeCoffLoaderRelocateImage ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ) { - RETURN_STATUS Status; - EFI_IMAGE_NT_HEADERS *PeHdr; - EFI_TE_IMAGE_HEADER *TeHdr; - EFI_IMAGE_DATA_DIRECTORY *RelocDir; - UINT64 Adjust; - EFI_IMAGE_BASE_RELOCATION *RelocBase; - EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd; - UINT16 *Reloc; - UINT16 *RelocEnd; - CHAR8 *Fixup; - CHAR8 *FixupBase; - UINT16 *F16; - UINT32 *F32; - CHAR8 *FixupData; - PHYSICAL_ADDRESS BaseAddress; + RETURN_STATUS Status; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_DATA_DIRECTORY *RelocDir; + UINT64 Adjust; + EFI_IMAGE_BASE_RELOCATION *RelocBase; + EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd; + UINT16 *Reloc; + UINT16 *RelocEnd; + CHAR8 *Fixup; + CHAR8 *FixupBase; + UINT16 *F16; + UINT32 *F32; + UINT64 *F64; + CHAR8 *FixupData; + PHYSICAL_ADDRESS BaseAddress; + UINT32 NumberOfRvaAndSizes; ASSERT (ImageContext != NULL); - PeHdr = NULL; - TeHdr = NULL; // // Assume success // @@ -524,21 +559,35 @@ PeCoffLoaderRelocateImage ( } if (!(ImageContext->IsTeImage)) { - PeHdr = (EFI_IMAGE_NT_HEADERS *)((UINTN)ImageContext->ImageAddress + - ImageContext->PeCoffHeaderOffset); - - Adjust = (UINT64) BaseAddress - PeHdr->OptionalHeader.ImageBase; - PeHdr->OptionalHeader.ImageBase = (UINTN)BaseAddress; + Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset); + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase; + Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress; + + NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes; + RelocDir = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]; + } else { + // + // Use PE32+ offset + // + Adjust = (UINT64) BaseAddress - Hdr.Pe32Plus->OptionalHeader.ImageBase; + Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress; + + NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes; + RelocDir = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]; + } // // Find the relocation block - // // Per the PE/COFF spec, you can't assume that a given data directory // is present in the image. You have to check the NumberOfRvaAndSizes in // the optional header to verify a desired directory entry is there. // - if (PeHdr->OptionalHeader.NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) { - RelocDir = &PeHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]; + + if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) { RelocBase = PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress); RelocBaseEnd = PeCoffLoaderImageAddress ( ImageContext, @@ -551,19 +600,19 @@ PeCoffLoaderRelocateImage ( RelocBase = RelocBaseEnd = 0; } } else { - TeHdr = (EFI_TE_IMAGE_HEADER *) (UINTN) (ImageContext->ImageAddress); - Adjust = (UINT64) (BaseAddress - TeHdr->ImageBase); - TeHdr->ImageBase = (UINT64) (BaseAddress); + Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress); + Adjust = (UINT64) (BaseAddress - Hdr.Te->ImageBase); + Hdr.Te->ImageBase = (UINT64) (BaseAddress); // // Find the relocation block // - RelocDir = &TeHdr->DataDirectory[0]; + RelocDir = &Hdr.Te->DataDirectory[0]; RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)( ImageContext->ImageAddress + RelocDir->VirtualAddress + sizeof(EFI_TE_IMAGE_HEADER) - - TeHdr->StrippedSize + Hdr.Te->StrippedSize ); RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) ((UINTN) RelocBase + (UINTN) RelocDir->Size - 1); } @@ -582,7 +631,7 @@ PeCoffLoaderRelocateImage ( FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress + RelocBase->VirtualAddress + sizeof(EFI_TE_IMAGE_HEADER) - - TeHdr->StrippedSize + Hdr.Te->StrippedSize ); } @@ -626,21 +675,27 @@ PeCoffLoaderRelocateImage ( *F32 = *F32 + (UINT32) Adjust; if (FixupData != NULL) { FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32)); - *(UINT32 *) FixupData = *F32; + *(UINT32 *)FixupData = *F32; FixupData = FixupData + sizeof (UINT32); } break; - case EFI_IMAGE_REL_BASED_HIGHADJ: - // - // Return the same EFI_UNSUPPORTED return code as - // PeCoffLoaderRelocateImageEx() returns if it does not recognize - // the relocation type. - // - ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION; - return RETURN_UNSUPPORTED; + case EFI_IMAGE_REL_BASED_DIR64: + F64 = (UINT64 *) Fixup; + *F64 = *F64 + (UINT64) Adjust; + if (FixupData != NULL) { + FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64)); + *(UINT64 *)(FixupData) = *F64; + FixupData = FixupData + sizeof(UINT64); + } + break; default: + // + // The common code does not handle some of the stranger IPF relocations + // PeCoffLoaderRelocateImageEx () addes support for these complex fixups + // on IPF and is a No-Op on other archtiectures. + // Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust); if (RETURN_ERROR (Status)) { ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION; @@ -693,8 +748,7 @@ PeCoffLoaderLoadImage ( ) { RETURN_STATUS Status; - EFI_IMAGE_NT_HEADERS *PeHdr; - EFI_TE_IMAGE_HEADER *TeHdr; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; PE_COFF_LOADER_IMAGE_CONTEXT CheckContext; EFI_IMAGE_SECTION_HEADER *FirstSection; EFI_IMAGE_SECTION_HEADER *Section; @@ -707,12 +761,10 @@ PeCoffLoaderLoadImage ( EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry; UINTN Size; UINT32 TempDebugEntryRva; + UINT32 NumberOfRvaAndSizes; ASSERT (ImageContext != NULL); - PeHdr = NULL; - TeHdr = NULL; - // // Assume success // @@ -786,32 +838,31 @@ PeCoffLoaderLoadImage ( (VOID *) (UINTN) ImageContext->ImageAddress ); - PeHdr = (EFI_IMAGE_NT_HEADERS *) - ((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset); + Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset); FirstSection = (EFI_IMAGE_SECTION_HEADER *) ( (UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER) + - PeHdr->FileHeader.SizeOfOptionalHeader + Hdr.Pe32->FileHeader.SizeOfOptionalHeader ); - NumberOfSections = (UINTN) (PeHdr->FileHeader.NumberOfSections); + NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections); } else { Status = ImageContext->ImageRead ( ImageContext->Handle, 0, &ImageContext->SizeOfHeaders, - (void *) (UINTN) ImageContext->ImageAddress + (void *)(UINTN)ImageContext->ImageAddress ); - TeHdr = (EFI_TE_IMAGE_HEADER *) (UINTN) (ImageContext->ImageAddress); + Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress); FirstSection = (EFI_IMAGE_SECTION_HEADER *) ( - (UINTN)ImageContext->ImageAddress + - sizeof(EFI_TE_IMAGE_HEADER) - ); - NumberOfSections = (UINTN) (TeHdr->NumberOfSections); + (UINTN)ImageContext->ImageAddress + + sizeof(EFI_TE_IMAGE_HEADER) + ); + NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections); } @@ -835,8 +886,8 @@ PeCoffLoaderLoadImage ( Section->VirtualAddress + Section->Misc.VirtualSize - 1 ); if (ImageContext->IsTeImage) { - Base = (CHAR8 *) ((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN) TeHdr->StrippedSize); - End = (CHAR8 *) ((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN) TeHdr->StrippedSize); + Base = (CHAR8 *)((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize); + End = (CHAR8 *)((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize); } if (End > MaxEnd) { @@ -869,7 +920,7 @@ PeCoffLoaderLoadImage ( } else { Status = ImageContext->ImageRead ( ImageContext->Handle, - Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN) TeHdr->StrippedSize, + Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize, &Size, Base ); @@ -899,17 +950,33 @@ PeCoffLoaderLoadImage ( // Get image's entry point // if (!(ImageContext->IsTeImage)) { - ImageContext->EntryPoint = (PHYSICAL_ADDRESS) (UINTN) PeCoffLoaderImageAddress ( - ImageContext, - PeHdr->OptionalHeader.AddressOfEntryPoint - ); + // + // Sizes of AddressOfEntryPoint are different so we need to do this safely + // + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress ( + ImageContext, + (UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint + ); + } else { + // + // Use PE32+ offset + // + ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress ( + ImageContext, + (UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint + ); + } } else { ImageContext->EntryPoint = (PHYSICAL_ADDRESS) ( - (UINTN)ImageContext->ImageAddress + - (UINTN)TeHdr->AddressOfEntryPoint + - (UINTN)sizeof(EFI_TE_IMAGE_HEADER) - - (UINTN) TeHdr->StrippedSize - ); + (UINTN)ImageContext->ImageAddress + + (UINTN)Hdr.Te->AddressOfEntryPoint + + (UINTN)sizeof(EFI_TE_IMAGE_HEADER) - + (UINTN)Hdr.Te->StrippedSize + ); } // @@ -920,15 +987,27 @@ PeCoffLoaderLoadImage ( // the optional header to verify a desired directory entry is there. // if (!(ImageContext->IsTeImage)) { - if (PeHdr->OptionalHeader.NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) { - DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *) - &PeHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]; + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes; + DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]; + } else { + // + // Use PE32+ offset + // + NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes; + DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]; + } + + if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) { ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN); } else { ImageContext->FixupDataSize = 0; } } else { - DirectoryEntry = &TeHdr->DataDirectory[0]; + DirectoryEntry = &Hdr.Te->DataDirectory[0]; ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN); } // @@ -948,18 +1027,18 @@ PeCoffLoaderLoadImage ( ); } else { DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)(UINTN)( - ImageContext->ImageAddress + - ImageContext->DebugDirectoryEntryRva + - sizeof(EFI_TE_IMAGE_HEADER) - - TeHdr->StrippedSize - ); + ImageContext->ImageAddress + + ImageContext->DebugDirectoryEntryRva + + sizeof(EFI_TE_IMAGE_HEADER) - + Hdr.Te->StrippedSize + ); } if (DebugEntry != NULL) { TempDebugEntryRva = DebugEntry->RVA; if (DebugEntry->RVA == 0 && DebugEntry->FileOffset != 0) { Section--; - if ((UINTN) Section->SizeOfRawData < Section->Misc.VirtualSize) { + if ((UINTN)Section->SizeOfRawData < Section->Misc.VirtualSize) { TempDebugEntryRva = Section->VirtualAddress + Section->Misc.VirtualSize; } else { TempDebugEntryRva = Section->VirtualAddress + Section->SizeOfRawData; @@ -971,11 +1050,11 @@ PeCoffLoaderLoadImage ( ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva); } else { ImageContext->CodeView = (VOID *)( - (UINTN)ImageContext->ImageAddress + - (UINTN)TempDebugEntryRva + - (UINTN)sizeof(EFI_TE_IMAGE_HEADER) - - (UINTN) TeHdr->StrippedSize - ); + (UINTN)ImageContext->ImageAddress + + (UINTN)TempDebugEntryRva + + (UINTN)sizeof (EFI_TE_IMAGE_HEADER) - + (UINTN) Hdr.Te->StrippedSize + ); } if (ImageContext->CodeView == NULL) { @@ -995,7 +1074,7 @@ PeCoffLoaderLoadImage ( } else { Status = ImageContext->ImageRead ( ImageContext->Handle, - DebugEntry->FileOffset + sizeof (EFI_TE_IMAGE_HEADER) - TeHdr->StrippedSize, + DebugEntry->FileOffset + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize, &Size, ImageContext->CodeView ); @@ -1016,11 +1095,11 @@ PeCoffLoaderLoadImage ( switch (*(UINT32 *) ImageContext->CodeView) { case CODEVIEW_SIGNATURE_NB10: - ImageContext->PdbPointer = (CHAR8 *) ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY); + 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); + ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY); break; default: @@ -1032,3 +1111,228 @@ PeCoffLoaderLoadImage ( return Status; } + + +/** + Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI + runtime. + + PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply + the fixups with a virtual mapping. + + + @param ImageBase Base address of relocated image + @param VirtImageBase Virtual mapping for ImageBase + @param ImageSize Size of the image to relocate + @param RelocationData Location to place results of read + +**/ +VOID +EFIAPI +PeCoffLoaderRelocateImageForRuntime ( + IN PHYSICAL_ADDRESS ImageBase, + IN PHYSICAL_ADDRESS VirtImageBase, + IN UINTN ImageSize, + IN VOID *RelocationData + ) +{ + CHAR8 *OldBase; + CHAR8 *NewBase; + EFI_IMAGE_DOS_HEADER *DosHdr; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + UINT32 NumberOfRvaAndSizes; + EFI_IMAGE_DATA_DIRECTORY *DataDirectory; + EFI_IMAGE_DATA_DIRECTORY *RelocDir; + EFI_IMAGE_BASE_RELOCATION *RelocBase; + EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd; + UINT16 *Reloc; + UINT16 *RelocEnd; + CHAR8 *Fixup; + CHAR8 *FixupBase; + UINT16 *F16; + UINT32 *F32; + UINT64 *F64; + CHAR8 *FixupData; + UINTN Adjust; + RETURN_STATUS Status; + + OldBase = (CHAR8 *)((UINTN)ImageBase); + NewBase = (CHAR8 *)((UINTN)VirtImageBase); + Adjust = (UINTN) NewBase - (UINTN) OldBase; + + // + // Find the image's relocate dir info + // + DosHdr = (EFI_IMAGE_DOS_HEADER *)OldBase; + if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) { + // + // Valid DOS header so get address of PE header + // + Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(((CHAR8 *)DosHdr) + DosHdr->e_lfanew); + } else { + // + // No Dos header so assume image starts with PE header. + // + Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)OldBase; + } + + if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) { + // + // Not a valid PE image so Exit + // + return ; + } + + // + // Get some data from the PE type dependent data + // + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use PE32 offset + // + NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes; + DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); + } else { + // + // Use PE32+ offset + // + NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes; + DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]); + } + + // + // Find the relocation block + // + // Per the PE/COFF spec, you can't assume that a given data directory + // is present in the image. You have to check the NumberOfRvaAndSizes in + // the optional header to verify a desired directory entry is there. + // + if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) { + RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC; + RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress); + RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size); + } else { + // + // Cannot find relocations, cannot continue + // + ASSERT (FALSE); + return ; + } + + ASSERT (RelocBase != NULL && RelocBaseEnd != NULL); + + // + // Run the whole relocation block. And re-fixup data that has not been + // modified. The FixupData is used to see if the image has been modified + // since it was relocated. This is so data sections that have been updated + // by code will not be fixed up, since that would set them back to + // defaults. + // + FixupData = RelocationData; + while (RelocBase < RelocBaseEnd) { + + Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION)); + RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock); + FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress; + + // + // Run this relocation record + // + while (Reloc < RelocEnd) { + + Fixup = FixupBase + (*Reloc & 0xFFF); + switch ((*Reloc) >> 12) { + + case EFI_IMAGE_REL_BASED_ABSOLUTE: + break; + + case EFI_IMAGE_REL_BASED_HIGH: + F16 = (UINT16 *) Fixup; + if (*(UINT16 *) FixupData == *F16) { + *F16 = (UINT16) ((*F16 << 16) + ((UINT16) Adjust & 0xffff)); + } + + FixupData = FixupData + sizeof (UINT16); + break; + + case EFI_IMAGE_REL_BASED_LOW: + F16 = (UINT16 *) Fixup; + if (*(UINT16 *) FixupData == *F16) { + *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff)); + } + + FixupData = FixupData + sizeof (UINT16); + break; + + case EFI_IMAGE_REL_BASED_HIGHLOW: + F32 = (UINT32 *) Fixup; + FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32)); + if (*(UINT32 *) FixupData == *F32) { + *F32 = *F32 + (UINT32) Adjust; + } + + FixupData = FixupData + sizeof (UINT32); + break; + + case EFI_IMAGE_REL_BASED_DIR64: + F64 = (UINT64 *)Fixup; + FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64)); + if (*(UINT32 *) FixupData == *F64) { + *F64 = *F64 + (UINT64)Adjust; + } + break; + + case EFI_IMAGE_REL_BASED_HIGHADJ: + // + // Not implemented, but not used in EFI 1.0 + // + ASSERT (FALSE); + break; + + default: + // + // Only Itanium requires ConvertPeImage_Ex + // + Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust); + if (RETURN_ERROR (Status)) { + return ; + } + } + // + // Next relocation record + // + Reloc += 1; + } + // + // next reloc block + // + RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd; + } +} + + +/** + ImageRead function that operates on a memory buffer whos base is passed into + FileHandle. + + @param FileHandle Ponter to baes of the input stream + @param FileOffset Offset to the start of the buffer + @param ReadSize Number of bytes to copy into the buffer + @param Buffer Location to place results of read + + @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into + the buffer. +**/ +RETURN_STATUS +EFIAPI +PeCoffLoaderImageReadFromMemory ( + IN VOID *FileHandle, + IN UINTN FileOffset, + IN OUT UINTN *ReadSize, + OUT VOID *Buffer + ) +{ + CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize); + return RETURN_SUCCESS; +} + diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.msa b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.msa index 69094ad1b8..6e4f9e0e36 100644 --- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.msa +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.msa @@ -1,15 +1,5 @@ - - + BasePeCoffLib BASE @@ -18,11 +8,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. Component description file for Base PE/COFF Library PE/COFF Loader Library implementation. Copyright (c) 2006, Intel Corporation. - All rights reserved. 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 - http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + All rights reserved. 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 + http://opensource.org/licenses/bsd-license.php + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052 @@ -41,6 +31,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. DebugLib + + PcdLib + BasePeCoff.c diff --git a/MdePkg/Library/BasePeCoffLib/Ebc/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/Ebc/PeCoffLoaderEx.c index fe41ac03bd..4289d0bb95 100644 --- a/MdePkg/Library/BasePeCoffLib/Ebc/PeCoffLoaderEx.c +++ b/MdePkg/Library/BasePeCoffLib/Ebc/PeCoffLoaderEx.c @@ -38,3 +38,56 @@ PeCoffLoaderRelocateImageEx ( { return RETURN_UNSUPPORTED; } + +/** + Returns TRUE if the machine type of PE/COFF image is supported. Supported + does not mean the image can be executed it means the PE/COFF loader supports + loading and relocating of the image type. It's up to the caller to support + the entry point. + + This function implies the basic PE/COFF loader/relocator supports IA32, EBC, + & X64 images. Calling the entry point in a correct mannor is up to the + consumer of this library. + + @param Machine Machine type from the PE Header. + + @return TRUE if this PE/COFF loader can load the image + +**/ +BOOLEAN +PeCoffLoaderImageFormatSupported ( + IN UINT16 Machine + ) +{ + if ((Machine == EFI_IMAGE_MACHINE_IA32) || (Machine == EFI_IMAGE_MACHINE_X64) || + (Machine == EFI_IMAGE_MACHINE_EBC)) { + return TRUE; + } + + return FALSE; +} + + +/** + Performs an Itanium-based specific re-relocation fixup and is a no-op on other + instruction sets. This is used to re-relocated the image into the EFI virtual + space for runtime calls. + + @param Reloc Pointer to the relocation record. + @param Fixup Pointer to the address to fix up. + @param FixupData Pointer to a buffer to log the fixups. + @param Adjust The offset to adjust the fixup. + + @return Status code. + +**/ +RETURN_STATUS +PeHotRelocateImageEx ( + IN UINT16 *Reloc, + IN OUT CHAR8 *Fixup, + IN OUT CHAR8 **FixupData, + IN UINT64 Adjust + ) +{ + return RETURN_UNSUPPORTED; +} diff --git a/MdePkg/Library/BasePeCoffLib/Ia32/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/Ia32/PeCoffLoaderEx.c index 9a2b2a04dd..9515a09f4d 100644 --- a/MdePkg/Library/BasePeCoffLib/Ia32/PeCoffLoaderEx.c +++ b/MdePkg/Library/BasePeCoffLib/Ia32/PeCoffLoaderEx.c @@ -39,3 +39,56 @@ PeCoffLoaderRelocateImageEx ( { return RETURN_UNSUPPORTED; } + +/** + Returns TRUE if the machine type of PE/COFF image is supported. Supported + does not mean the image can be executed it means the PE/COFF loader supports + loading and relocating of the image type. It's up to the caller to support + the entry point. + + This function implies the basic PE/COFF loader/relocator supports IA32, EBC, + & X64 images. Calling the entry point in a correct mannor is up to the + consumer of this library. + + @param Machine Machine type from the PE Header. + + @return TRUE if this PE/COFF loader can load the image + +**/ +BOOLEAN +PeCoffLoaderImageFormatSupported ( + IN UINT16 Machine + ) +{ + if ((Machine == EFI_IMAGE_MACHINE_IA32) || (Machine == EFI_IMAGE_MACHINE_X64) || + (Machine == EFI_IMAGE_MACHINE_EBC)) { + return TRUE; + } + + return FALSE; +} + +/** + Performs an Itanium-based specific re-relocation fixup and is a no-op on other + instruction sets. This is used to re-relocated the image into the EFI virtual + space for runtime calls. + + @param Reloc Pointer to the relocation record. + @param Fixup Pointer to the address to fix up. + @param FixupData Pointer to a buffer to log the fixups. + @param Adjust The offset to adjust the fixup. + + @return Status code. + +**/ +RETURN_STATUS +PeHotRelocateImageEx ( + IN UINT16 *Reloc, + IN OUT CHAR8 *Fixup, + IN OUT CHAR8 **FixupData, + IN UINT64 Adjust + ) +{ + return RETURN_UNSUPPORTED; +} + diff --git a/MdePkg/Library/BasePeCoffLib/Ipf/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/Ipf/PeCoffLoaderEx.c index 57c6555408..adbd3fca81 100644 --- a/MdePkg/Library/BasePeCoffLib/Ipf/PeCoffLoaderEx.c +++ b/MdePkg/Library/BasePeCoffLib/Ipf/PeCoffLoaderEx.c @@ -88,17 +88,6 @@ PeCoffLoaderRelocateImageEx ( UINT64 FixupVal; switch ((*Reloc) >> 12) { - - case EFI_IMAGE_REL_BASED_DIR64: - F64 = (UINT64 *) Fixup; - *F64 = *F64 + (UINT64) Adjust; - if (*FixupData != NULL) { - *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64)); - *(UINT64 *)(*FixupData) = *F64; - *FixupData = *FixupData + sizeof(UINT64); - } - break; - case EFI_IMAGE_REL_BASED_IA64_IMM64: // @@ -225,3 +214,232 @@ PeCoffLoaderRelocateImageEx ( return RETURN_SUCCESS; } + +/** + Returns TRUE if the machine type of PE/COFF image is supported. Supported + does not mean the image can be executed it means the PE/COFF loader supports + loading and relocating of the image type. It's up to the caller to support + the entry point. + + This function implies the basic PE/COFF loader/relocator supports IA32, EBC, + & X64 images. Calling the entry point in a correct mannor is up to the + consumer of this library. This version also supports the special relocations + for Itanium. + + @param Machine Machine type from the PE Header. + + @return TRUE if this PE/COFF loader can load the image + +**/ +BOOLEAN +PeCoffLoaderImageFormatSupported ( + IN UINT16 Machine + ) +{ + if ((Machine == EFI_IMAGE_MACHINE_IPF) || (Machine == EFI_IMAGE_MACHINE_IA32) || + (Machine == EFI_IMAGE_MACHINE_EBC) || (Machine == EFI_IMAGE_MACHINE_X64)) { + return TRUE; + } + + return FALSE; +} + + +/** + ImageRead function that operates on a memory buffer whos base is passed into + FileHandle. + + @param Reloc Ponter to baes of the input stream + @param Fixup Offset to the start of the buffer + @param FixupData Number of bytes to copy into the buffer + @param Adjust Location to place results of read + + @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into + the buffer. +**/ +RETURN_STATUS +PeHotRelocateImageEx ( + IN UINT16 *Reloc, + IN OUT CHAR8 *Fixup, + IN OUT CHAR8 **FixupData, + IN UINT64 Adjust + ) +/*++ + +Routine Description: + + Performs an IPF specific relocation fixup + +Arguments: + + Reloc - Pointer to the relocation record + + Fixup - Pointer to the address to fix up + + FixupData - Pointer to a buffer to log the fixups + + Adjust - The offset to adjust the fixup + +Returns: + + None + +--*/ +{ + UINT64 *F64; + UINT64 FixupVal; + + switch ((*Reloc) >> 12) { + case EFI_IMAGE_REL_BASED_DIR64: + F64 = (UINT64 *) Fixup; + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64)); + if (*(UINT64 *) (*FixupData) == *F64) { + *F64 = *F64 + (UINT64) Adjust; + } + + *FixupData = *FixupData + sizeof (UINT64); + break; + + case EFI_IMAGE_REL_BASED_IA64_IMM64: + F64 = (UINT64 *) Fixup; + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64)); + if (*(UINT64 *) (*FixupData) == *F64) { + // + // Align it to bundle address before fixing up the + // 64-bit immediate value of the movl instruction. + // + // + Fixup = (CHAR8 *) ((UINT64) Fixup & (UINT64)~(15)); + FixupVal = (UINT64) 0; + + // + // Extract the lower 32 bits of IMM64 from bundle + // + EXT_IMM64 ( + FixupVal, + (UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X, + IMM64_IMM7B_SIZE_X, + IMM64_IMM7B_INST_WORD_POS_X, + IMM64_IMM7B_VAL_POS_X + ); + + EXT_IMM64 ( + FixupVal, + (UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X, + IMM64_IMM9D_SIZE_X, + IMM64_IMM9D_INST_WORD_POS_X, + IMM64_IMM9D_VAL_POS_X + ); + + EXT_IMM64 ( + FixupVal, + (UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X, + IMM64_IMM5C_SIZE_X, + IMM64_IMM5C_INST_WORD_POS_X, + IMM64_IMM5C_VAL_POS_X + ); + + EXT_IMM64 ( + FixupVal, + (UINT32 *) Fixup + IMM64_IC_INST_WORD_X, + IMM64_IC_SIZE_X, + IMM64_IC_INST_WORD_POS_X, + IMM64_IC_VAL_POS_X + ); + + EXT_IMM64 ( + FixupVal, + (UINT32 *) Fixup + IMM64_IMM41a_INST_WORD_X, + IMM64_IMM41a_SIZE_X, + IMM64_IMM41a_INST_WORD_POS_X, + IMM64_IMM41a_VAL_POS_X + ); + + // + // Update 64-bit address + // + FixupVal += Adjust; + + // + // Insert IMM64 into bundle + // + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X), + IMM64_IMM7B_SIZE_X, + IMM64_IMM7B_INST_WORD_POS_X, + IMM64_IMM7B_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X), + IMM64_IMM9D_SIZE_X, + IMM64_IMM9D_INST_WORD_POS_X, + IMM64_IMM9D_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X), + IMM64_IMM5C_SIZE_X, + IMM64_IMM5C_INST_WORD_POS_X, + IMM64_IMM5C_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IC_INST_WORD_X), + IMM64_IC_SIZE_X, + IMM64_IC_INST_WORD_POS_X, + IMM64_IC_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IMM41a_INST_WORD_X), + IMM64_IMM41a_SIZE_X, + IMM64_IMM41a_INST_WORD_POS_X, + IMM64_IMM41a_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IMM41b_INST_WORD_X), + IMM64_IMM41b_SIZE_X, + IMM64_IMM41b_INST_WORD_POS_X, + IMM64_IMM41b_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_IMM41c_INST_WORD_X), + IMM64_IMM41c_SIZE_X, + IMM64_IMM41c_INST_WORD_POS_X, + IMM64_IMM41c_VAL_POS_X + ); + + INS_IMM64 ( + FixupVal, + ((UINT32 *) Fixup + IMM64_SIGN_INST_WORD_X), + IMM64_SIGN_SIZE_X, + IMM64_SIGN_INST_WORD_POS_X, + IMM64_SIGN_VAL_POS_X + ); + + *(UINT64 *) (*FixupData) = *F64; + } + + *FixupData = *FixupData + sizeof (UINT64); + break; + + default: + DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n")); + return RETURN_UNSUPPORTED; + } + + return RETURN_SUCCESS; +} + + + diff --git a/MdePkg/Library/BasePeCoffLib/x64/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/x64/PeCoffLoaderEx.c index db7eead97d..7141adc2ad 100644 --- a/MdePkg/Library/BasePeCoffLib/x64/PeCoffLoaderEx.c +++ b/MdePkg/Library/BasePeCoffLib/x64/PeCoffLoaderEx.c @@ -31,23 +31,58 @@ PeCoffLoaderRelocateImageEx ( IN UINT64 Adjust ) { - UINT64 *F64; - - switch ((*Reloc) >> 12) { - - case EFI_IMAGE_REL_BASED_DIR64: - F64 = (UINT64 *) Fixup; - *F64 = *F64 + (UINT64) Adjust; - if (*FixupData != NULL) { - *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64)); - *(UINT64 *)(*FixupData) = *F64; - *FixupData = *FixupData + sizeof(UINT64); - } - break; - - default: - return RETURN_UNSUPPORTED; + return RETURN_UNSUPPORTED; +} + +/** + Returns TRUE if the machine type of PE/COFF image is supported. Supported + does not mean the image can be executed it means the PE/COFF loader supports + loading and relocating of the image type. It's up to the caller to support + the entry point. + + This function implies the basic PE/COFF loader/relocator supports IA32, EBC, + & X64 images. Calling the entry point in a correct mannor is up to the + consumer of this library. + + @param Machine Machine type from the PE Header. + + @return TRUE if this PE/COFF loader can load the image + +**/ +BOOLEAN +PeCoffLoaderImageFormatSupported ( + IN UINT16 Machine + ) +{ + if ((Machine == EFI_IMAGE_MACHINE_IA32) || (Machine == EFI_IMAGE_MACHINE_X64) || + (Machine == EFI_IMAGE_MACHINE_EBC)) { + return TRUE; } - return RETURN_SUCCESS; + return FALSE; +} + + +/** + Performs an Itanium-based specific re-relocation fixup and is a no-op on other + instruction sets. This is used to re-relocated the image into the EFI virtual + space for runtime calls. + + @param Reloc Pointer to the relocation record. + @param Fixup Pointer to the address to fix up. + @param FixupData Pointer to a buffer to log the fixups. + @param Adjust The offset to adjust the fixup. + + @return Status code. + +**/ +RETURN_STATUS +PeHotRelocateImageEx ( + IN UINT16 *Reloc, + IN OUT CHAR8 *Fixup, + IN OUT CHAR8 **FixupData, + IN UINT64 Adjust + ) +{ + return RETURN_UNSUPPORTED; }