Library: Replace OcPeCoffLib with UefiImageLib

This commit is contained in:
Savva Mitrofanov 2023-04-13 18:36:41 +06:00 committed by Vitaly Cheptsov
parent d185827c70
commit 70bb87c7b0
14 changed files with 150 additions and 136 deletions

View File

@ -19,7 +19,7 @@
* OcGuardLib — Basic sanity checking (static assertions, overflow maths)
* OcMachoLib — Mach-O image handling and transformation
* OcMiscLib — Miscellaneous stuff not fitting elsewhere
* OcPeCoffLib - EFI image management
* OcPeCoffLib - EFI image management (deprecated, replaced with UefiImageLib)
* OcPeCoffExtLib — EFI image management extras for Apple
* OcPngLib — PNG image decoding
* OcRtcLib — CMOS memory access

View File

@ -10,7 +10,7 @@
#define OC_PE_COFF_EXT_LIB_H
#include <IndustryStandard/Apfs.h>
#include <Library/OcPeCoffLib.h>
#include <Library/UefiImageLib.h>
/**
Verify Apple COFF legacy signature.

View File

@ -18,8 +18,8 @@
#include <Library/BaseOverflowLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiImageLib.h>
#include <Library/OcApfsLib.h>
#include <Library/OcPeCoffLib.h>
STATIC
UINT64

View File

@ -62,9 +62,9 @@
OcConsoleLib
OcDriverConnectionLib
OcMiscLib
OcPeCoffLib
OcPeCoffExtLib
MemoryAllocationLib
UefiBootServicesTableLib
UefiLib
UefiImageLib
UefiRuntimeServicesTableLib

View File

@ -28,6 +28,7 @@
#include <Library/OcDebugLogLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/MemoryAllocationLibEx.h>
#include <Library/OcAppleSecureBootLib.h>
#include <Library/OcBootManagementLib.h>
#include <Library/OcDebugLogLib.h>
@ -36,7 +37,7 @@
#include <Library/OcMachoLib.h>
#include <Library/OcMiscLib.h>
#include <Library/OcStringLib.h>
#include <Library/OcPeCoffLib.h>
#include <Library/UefiImageLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
@ -256,14 +257,17 @@ OcImageLoaderLoad (
OUT EFI_HANDLE *ImageHandle
)
{
EFI_STATUS Status;
EFI_STATUS ImageStatus;
PE_COFF_IMAGE_CONTEXT ImageContext;
EFI_PHYSICAL_ADDRESS DestinationArea;
UINT32 DestinationSize;
VOID *DestinationBuffer;
OC_LOADED_IMAGE_PROTOCOL *OcLoadedImage;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_STATUS Status;
EFI_STATUS ImageStatus;
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
UINT32 ImageSize;
UINT32 DestinationSize;
UINT32 DestinationPages;
UINT32 DestinationAlignment;
EFI_PHYSICAL_ADDRESS DestinationArea;
VOID *DestinationBuffer;
OC_LOADED_IMAGE_PROTOCOL *OcLoadedImage;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
ASSERT (SourceBuffer != NULL);
@ -277,7 +281,7 @@ OcImageLoaderLoad (
//
// Initialize the image context.
//
ImageStatus = PeCoffInitializeContext (
ImageStatus = UefiImageInitializeContext (
&ImageContext,
SourceBuffer,
(UINT32)SourceSize
@ -303,14 +307,10 @@ OcImageLoaderLoad (
return EFI_UNSUPPORTED;
}
//
// FIXME: This needs to be backported as a function:
// https://github.com/mhaeuser/edk2/blob/2021-gsoc-secure-loader/MdePkg/Library/BaseUefiImageLib/CommonSupport.c#L19-L53
//
DestinationSize = ImageContext.SizeOfImage + ImageContext.SizeOfImageDebugAdd;
if (BaseOverflowAddU32 (DestinationSize, ImageContext.SectionAlignment, &DestinationSize)) {
return RETURN_UNSUPPORTED;
}
ImageSize = UefiImageGetImageSize (&ImageContext);
DestinationPages = EFI_SIZE_TO_PAGES (ImageSize);
DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages);
DestinationAlignment = UefiImageGetSegmentAlignment (&ImageContext);
if (DestinationSize >= BASE_16MB) {
DEBUG ((DEBUG_INFO, "OCB: PeCoff prohibits files over 16M (%u)\n", DestinationSize));
@ -321,45 +321,36 @@ OcImageLoaderLoad (
// Allocate the image destination memory.
// FIXME: RT drivers require EfiRuntimeServicesCode.
//
Status = gBS->AllocatePages (
AllocateAnyPages,
ImageContext.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
Status = AllocateAlignedPagesEx (
AllocateAnyPages,
ImageContext.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
? EfiLoaderCode : EfiBootServicesCode,
EFI_SIZE_TO_PAGES (ImageContext.SizeOfImage),
&DestinationArea
);
DestinationPages,
DestinationAlignment,
&DestinationArea
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCB: PeCoff could allocate image buffer\n"));
return Status;
}
DestinationBuffer = (VOID *)(UINTN)DestinationArea;
//
// Load SourceBuffer into DestinationBuffer.
// Load and relocate image for execution.
//
ImageStatus = PeCoffLoadImage (
ImageStatus = UefiImageLoadImageForExecution (
&ImageContext,
DestinationBuffer,
ImageContext.SizeOfImage
);
if (EFI_ERROR (ImageStatus)) {
DEBUG ((DEBUG_INFO, "OCB: PeCoff load image error - %r\n", ImageStatus));
FreePages (DestinationBuffer, EFI_SIZE_TO_PAGES (ImageContext.SizeOfImage));
return EFI_UNSUPPORTED;
}
//
// Relocate the loaded image to the destination address.
//
ImageStatus = PeCoffRelocateImage (
&ImageContext,
(UINTN)DestinationBuffer,
DestinationSize,
NULL,
0
);
if (EFI_ERROR (ImageStatus)) {
DEBUG ((DEBUG_INFO, "OCB: PeCoff relocate image error - %r\n", ImageStatus));
FreePages (DestinationBuffer, EFI_SIZE_TO_PAGES (ImageContext.SizeOfImage));
DEBUG ((DEBUG_INFO, "OCB: PeCoff load image for execution error - %r\n", ImageStatus));
FreeAlignedPages (DestinationBuffer, DestinationPages);
return EFI_UNSUPPORTED;
}
@ -368,13 +359,13 @@ OcImageLoaderLoad (
//
OcLoadedImage = AllocateZeroPool (sizeof (*OcLoadedImage));
if (OcLoadedImage == NULL) {
FreePages (DestinationBuffer, EFI_SIZE_TO_PAGES (ImageContext.SizeOfImage));
FreeAlignedPages (DestinationBuffer, DestinationPages);
return EFI_OUT_OF_RESOURCES;
}
OcLoadedImage->EntryPoint = (EFI_IMAGE_ENTRY_POINT)((UINTN)DestinationBuffer + ImageContext.AddressOfEntryPoint);
OcLoadedImage->ImageArea = DestinationArea;
OcLoadedImage->PageCount = EFI_SIZE_TO_PAGES (ImageContext.SizeOfImage);
OcLoadedImage->PageCount = DestinationPages;
OcLoadedImage->Subsystem = ImageContext.Subsystem;
LoadedImage = &OcLoadedImage->LoadedImage;
@ -383,7 +374,7 @@ OcImageLoaderLoad (
LoadedImage->ParentHandle = ParentImageHandle;
LoadedImage->SystemTable = gST;
LoadedImage->ImageBase = DestinationBuffer;
LoadedImage->ImageSize = ImageContext.SizeOfImage;
LoadedImage->ImageSize = DestinationSize;
//
// FIXME: Support RT drivers.
//
@ -410,7 +401,7 @@ OcImageLoaderLoad (
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCB: PeCoff proto install error - %r\n", Status));
FreePool (OcLoadedImage);
FreePages (DestinationBuffer, EFI_SIZE_TO_PAGES (ImageContext.SizeOfImage));
FreeAlignedPages (DestinationBuffer, DestinationPages);
return Status;
}
@ -464,7 +455,7 @@ InternalDirectUnloadImage (
return Status;
}
gBS->FreePages (OcLoadedImage->ImageArea, OcLoadedImage->PageCount);
FreeAlignedPages ((VOID *)(UINTN)OcLoadedImage->ImageArea, OcLoadedImage->PageCount);
FreePool (OcLoadedImage);
//
// NOTE: Avoid EFI 1.10 extension of closing opened protocols.

View File

@ -94,6 +94,7 @@
MemoryAllocationLib
PrintLib
UefiBootServicesTableLib
UefiImageLib
OcApfsLib
OcAppleBootPolicyLib
OcAppleChunklistLib
@ -109,7 +110,6 @@
OcFlexArrayLib
OcMachoLib
OcMiscLib
OcPeCoffLib
OcRtcLib
OcTypingLib
OcVariableLib

View File

@ -26,7 +26,7 @@
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <IndustryStandard/Pci23.h>
#include <IndustryStandard/PeCoffImage.h>
#include <IndustryStandard/PeImage2.h>
#include <Protocol/Decompress.h>
#include <Protocol/PciIo.h>

View File

@ -25,6 +25,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/UefiImageLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
@ -32,7 +33,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/UefiLib.h>
#include <Library/OcCryptoLib.h>
#include <Library/OcAppleKeysLib.h>
#include <Library/OcPeCoffLib.h>
#include <Guid/AppleCertificate.h>
#include "OcPeCoffExtInternal.h"
@ -40,7 +40,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
STATIC
RETURN_STATUS
PeCoffGetSecurityDirectoryEntry (
IN PE_COFF_IMAGE_CONTEXT *Context,
IN PE_COFF_LOADER_IMAGE_CONTEXT *Context,
IN UINT32 FileSize,
OUT CONST EFI_IMAGE_DATA_DIRECTORY **DirectoryEntry
)
@ -54,7 +54,7 @@ PeCoffGetSecurityDirectoryEntry (
ASSERT (DirectoryEntry != NULL);
switch (Context->ImageType) {
case ImageTypePe32:
case PeCoffLoaderTypePe32:
Pe32Hdr = (CONST EFI_IMAGE_NT_HEADERS32 *)(CONST VOID *)(
(CONST CHAR8 *)Context->FileBuffer + Context->ExeHdrOffset
);
@ -66,7 +66,7 @@ PeCoffGetSecurityDirectoryEntry (
*DirectoryEntry = &Pe32Hdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
break;
case ImageTypePe32Plus:
case PeCoffLoaderTypePe32Plus:
Pe32PlusHdr = (CONST EFI_IMAGE_NT_HEADERS64 *)(CONST VOID *)(
(CONST CHAR8 *)Context->FileBuffer + Context->ExeHdrOffset
);
@ -107,11 +107,11 @@ PeCoffGetSecurityDirectoryEntry (
STATIC
EFI_STATUS
PeCoffGetAppleCertificateInfo (
IN PE_COFF_IMAGE_CONTEXT *Context,
IN UINT32 FileSize,
OUT APPLE_EFI_CERTIFICATE_INFO **CertInfo,
OUT UINT32 *SecDirOffset,
OUT UINT32 *SignedFileSize
IN PE_COFF_LOADER_IMAGE_CONTEXT *Context,
IN UINT32 FileSize,
OUT APPLE_EFI_CERTIFICATE_INFO **CertInfo,
OUT UINT32 *SecDirOffset,
OUT UINT32 *SignedFileSize
)
{
EFI_STATUS Status;
@ -164,9 +164,9 @@ PeCoffGetAppleCertificateInfo (
STATIC
EFI_STATUS
PeCoffGetAppleSignature (
IN PE_COFF_IMAGE_CONTEXT *Context,
IN APPLE_EFI_CERTIFICATE_INFO *CertInfo,
OUT APPLE_SIGNATURE_CONTEXT *SignatureContext
IN PE_COFF_LOADER_IMAGE_CONTEXT *Context,
IN APPLE_EFI_CERTIFICATE_INFO *CertInfo,
OUT APPLE_SIGNATURE_CONTEXT *SignatureContext
)
{
UINTN Index;
@ -263,10 +263,10 @@ PeCoffGetAppleSignature (
STATIC
EFI_STATUS
PeCoffSanitiseAppleImage (
IN PE_COFF_IMAGE_CONTEXT *Context,
IN UINT32 SecDirOffset,
IN UINT32 SignedFileSize,
IN UINT32 FileSize
IN PE_COFF_LOADER_IMAGE_CONTEXT *Context,
IN UINT32 SecDirOffset,
IN UINT32 SignedFileSize,
IN UINT32 FileSize
)
{
//
@ -274,8 +274,8 @@ PeCoffSanitiseAppleImage (
// one might add more PE types in the future technically.
// Restrict file type as early as possible.
//
if ( (Context->ImageType != ImageTypePe32)
&& (Context->ImageType != ImageTypePe32Plus))
if ( (Context->ImageType != PeCoffLoaderTypePe32)
&& (Context->ImageType != PeCoffLoaderTypePe32Plus))
{
DEBUG ((DEBUG_INFO, "OCPE: Unsupported image type %d for Apple Image\n", Context->ImageType));
return EFI_UNSUPPORTED;
@ -329,10 +329,10 @@ PeCoffSanitiseAppleImage (
STATIC
VOID
PeCoffHashAppleImage (
IN PE_COFF_IMAGE_CONTEXT *Context,
IN UINT32 SecDirOffset,
IN UINT32 SignedFileSize,
OUT UINT8 *Hash
IN PE_COFF_LOADER_IMAGE_CONTEXT *Context,
IN UINT32 SecDirOffset,
IN UINT32 SignedFileSize,
OUT UINT8 *Hash
)
{
UINTN HashSize;
@ -381,14 +381,14 @@ PeCoffVerifyAppleSignature (
IN OUT UINT32 *ImageSize
)
{
EFI_STATUS ImageStatus;
PE_COFF_IMAGE_CONTEXT ImageContext;
APPLE_SIGNATURE_CONTEXT SignatureContext;
UINT8 Hash[SHA256_DIGEST_SIZE];
BOOLEAN Success;
APPLE_EFI_CERTIFICATE_INFO *CertInfo;
UINT32 SecDirOffset;
UINT32 SignedFileSize;
EFI_STATUS ImageStatus;
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
APPLE_SIGNATURE_CONTEXT SignatureContext;
UINT8 Hash[SHA256_DIGEST_SIZE];
BOOLEAN Success;
APPLE_EFI_CERTIFICATE_INFO *CertInfo;
UINT32 SecDirOffset;
UINT32 SignedFileSize;
ImageStatus = PeCoffInitializeContext (
&ImageContext,
@ -472,12 +472,12 @@ PeCoffGetApfsDriverVersion (
// apfs.efi versioning is more restricted than generic PE parsing.
//
EFI_STATUS ImageStatus;
PE_COFF_IMAGE_CONTEXT ImageContext;
EFI_IMAGE_NT_HEADERS64 *OptionalHeader;
EFI_IMAGE_SECTION_HEADER *SectionHeader;
APFS_DRIVER_VERSION *DriverVersion;
UINT32 ImageVersion;
EFI_STATUS ImageStatus;
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
EFI_IMAGE_NT_HEADERS64 *OptionalHeader;
EFI_IMAGE_SECTION_HEADER *SectionHeader;
APFS_DRIVER_VERSION *DriverVersion;
UINT32 ImageVersion;
ImageStatus = PeCoffInitializeContext (
&ImageContext,
@ -490,7 +490,7 @@ PeCoffGetApfsDriverVersion (
}
if ( (ImageContext.Machine != IMAGE_FILE_MACHINE_X64)
|| (ImageContext.ImageType != ImageTypePe32Plus)
|| (ImageContext.ImageType != PeCoffLoaderTypePe32Plus)
|| (ImageContext.Subsystem != EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER))
{
DEBUG ((DEBUG_INFO, "OCPE: PeCoff unsupported image\n"));

View File

@ -37,6 +37,7 @@
OpenCorePkg/OpenCorePkg.dec
[LibraryClasses]
UefiImageLib
UefiRuntimeServicesTableLib
UefiBootServicesTableLib
MemoryAllocationLib
@ -46,7 +47,6 @@
UefiLib
DebugLib
OcAppleKeysLib
OcPeCoffLib
OcCryptoLib
[Guids]

View File

@ -770,14 +770,6 @@
## @Prompt Allow these signature hashing algorithms for cryptographic usage.
gOpenCorePkgTokenSpaceGuid.PcdOcCryptoAllowedSigHashTypes|0x07|UINT16|0x00000501
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderRtRelocAllowTargetMismatch|FALSE|BOOLEAN|0x00000600
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderHashProhibitOverlap|TRUE|BOOLEAN|0x00000601
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderLoadHeader|TRUE|BOOLEAN|0x00000602
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderSupportArmThumb|FALSE|BOOLEAN|0x00000603
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderForceLoadDebug|FALSE|BOOLEAN|0x00000604
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderTolerantLoad|TRUE|BOOLEAN|0x00000605
gOpenCorePkgTokenSpaceGuid.PcdImageLoaderSupportDebug|FALSE|BOOLEAN|0x00000606
## Indicates, whether usage of TSC instead of RNG is allowed.<BR><BR>
## TRUE - TSC is allowed.<BR>
## FALSE - TSC is NOT allowed.<BR>
@ -932,9 +924,6 @@
## @libraryclass
OcPeCoffExtLib|Include/Acidanthera/Library/OcPeCoffExtLib.h
## @libraryclass
OcPeCoffLib|Include/Acidanthera/Library/OcPeCoffLib.h
## @libraryclass
OcPngLib|Include/Acidanthera/Library/OcPngLib.h

View File

@ -139,7 +139,6 @@
OcWaveLib|OpenCorePkg/Library/OcWaveLib/OcWaveLib.inf
OcXmlLib|OpenCorePkg/Library/OcXmlLib/OcXmlLib.inf
OcPeCoffExtLib|OpenCorePkg/Library/OcPeCoffExtLib/OcPeCoffExtLib.inf
OcPeCoffLib|OpenCorePkg/Library/OcPeCoffLib/OcPeCoffLib.inf
OcVariableLib|OpenCorePkg/Library/OcVariableLib/OcVariableLib.inf
OcVariableRuntimeLib|OpenCorePkg/Library/OcVariableRuntimeLib/OcVariableRuntimeLib.inf
PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
@ -280,7 +279,6 @@
OpenCorePkg/Library/OcMp3Lib/OcMp3Lib.inf
OpenCorePkg/Library/OcOSInfoLib/OcOSInfoLib.inf
OpenCorePkg/Library/OcPeCoffExtLib/OcPeCoffExtLib.inf
OpenCorePkg/Library/OcPeCoffLib/OcPeCoffLib.inf
OpenCorePkg/Library/OcPngLib/OcPngLib.inf
OpenCorePkg/Library/OcRngLib/OcRngLib.inf
OpenCorePkg/Library/OcSerializeLib/OcSerializeLib.inf
@ -392,6 +390,17 @@
!endif
gOpenCorePkgTokenSpaceGuid.PcdCanaryAllowRdtscFallback|TRUE
# ImageLoader settings
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRtRelocAllowTargetMismatch|FALSE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderHashProhibitOverlap|TRUE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderLoadHeader|TRUE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderDebugSupport|FALSE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderAllowMisalignedOffset|FALSE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRemoveXForWX|FALSE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderWXorX|TRUE
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderAlignmentPolicy|0xFFFFFFFF
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRelocTypePolicy|0xFFFFFFFF
[PcdsPatchableInModule]
gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterAccessWidth|8
gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseMmio|FALSE

View File

@ -9,11 +9,12 @@ PRODUCT = $(PROJECT)$(INFIX)$(SUFFIX)
OBJS = $(PROJECT).o \
PeCoffDebug.o \
PeCoffHash.o \
PeCoffInfo.o \
PeCoffInit.o \
PeCoffLoad.o \
PeCoffRelocate.o \
OcPeCoffExtLib.o
VPATH = ../../Library/OcPeCoffLib:$\
VPATH = $(UDK_PATH)/MdePkg/Library/BasePeCoffLib2:$\
../../Library/OcPeCoffExtLib:$\
include ../../User/Makefile

View File

@ -8,8 +8,9 @@ PRODUCT = $(PROJECT)$(INFIX)$(SUFFIX)
OBJS = $(PROJECT).o \
PeCoffDebug.o \
PeCoffHash.o \
PeCoffInfo.o \
PeCoffInit.o \
PeCoffLoad.o \
PeCoffRelocate.o
VPATH = ../../Library/OcPeCoffLib
VPATH = $(UDK_PATH)/MdePkg/Library/BasePeCoffLib2:$
include ../../User/Makefile

View File

@ -5,10 +5,10 @@
#include "../Include/Uefi.h"
#include <Library/OcPeCoffLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiImageLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
@ -57,14 +57,14 @@ HashUpdate (
STATIC
EFI_STATUS
PeCoffTestRtReloc (
IN OUT PE_COFF_IMAGE_CONTEXT *Context
IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context
)
{
EFI_STATUS Status;
PE_COFF_RUNTIME_CONTEXT *RtCtx;
UINT32 RtCtxSize;
EFI_STATUS Status;
PE_COFF_LOADER_RUNTIME_CONTEXT *RtCtx;
UINT32 RtCtxSize;
Status = PeCoffRelocationDataSize (Context, &RtCtxSize);
Status = PeCoffLoaderGetRuntimeContextSize (Context, &RtCtxSize);
if (EFI_ERROR (Status)) {
return EFI_UNSUPPORTED;
}
@ -80,7 +80,7 @@ PeCoffTestRtReloc (
return Status;
}
Status = PeCoffRelocateImageForRuntime (Context->ImageBuffer, Context->SizeOfImage, 0x96969696, RtCtx);
Status = PeCoffRuntimeRelocateImage (Context->ImageBuffer, Context->SizeOfImage, 0x96969696, RtCtx);
FreePool (RtCtx);
@ -90,9 +90,9 @@ PeCoffTestRtReloc (
STATIC
EFI_STATUS
PeCoffTestLoad (
IN OUT PE_COFF_IMAGE_CONTEXT *Context,
OUT VOID *Destination,
IN UINT32 DestinationSize
IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context,
OUT VOID *Destination,
IN UINT32 DestinationSize
)
{
EFI_STATUS Status;
@ -101,7 +101,7 @@ PeCoffTestLoad (
(VOID)PeCoffLoadImage (Context, Destination, DestinationSize);
Status = PeCoffGetPdbPath (Context, &PdbPath, &PdbPathSize);
Status = PeCoffGetPdbPath (Context, (CONST CHAR8 **)&PdbPath, &PdbPathSize);
if (!EFI_ERROR (Status)) {
ZeroMem (PdbPath, PdbPathSize);
}
@ -141,10 +141,27 @@ LoadConfig (
PcdGetBool (PcdImageLoaderRtRelocAllowTargetMismatch) = (LastByte & 1U) != 0;
PcdGetBool (PcdImageLoaderHashProhibitOverlap) = (LastByte & 2U) != 0;
PcdGetBool (PcdImageLoaderLoadHeader) = (LastByte & 4U) != 0;
PcdGetBool (PcdImageLoaderSupportArmThumb) = (LastByte & 8U) != 0;
PcdGetBool (PcdImageLoaderForceLoadDebug) = (LastByte & 16U) != 0;
PcdGetBool (PcdImageLoaderTolerantLoad) = (LastByte & 32U) != 0;
PcdGetBool (PcdImageLoaderSupportDebug) = (LastByte & 64U) != 0;
PcdGetBool (PcdImageLoaderDebugSupport) = (LastByte & 8U) != 0;
PcdGetBool (PcdImageLoaderAllowMisalignedOffset) = (LastByte & 16U) != 0;
PcdGetBool (PcdImageLoaderRemoveXForWX) = (LastByte & 32U) != 0;
Off += sizeof (UINT32);
if (Size >= Off) {
CopyMem (&LastByte, &Data[Size - Off], sizeof (UINT32));
} else {
LastByte = MAX_UINT32;
}
PcdGet32 (PcdImageLoaderAlignmentPolicy) = LastByte;
Off += sizeof (UINT32);
if (Size >= Off) {
CopyMem (&LastByte, &Data[Size - Off], sizeof (UINT32));
} else {
LastByte = MAX_UINT32;
}
PcdGet32 (PcdImageLoaderRelocTypePolicy) = LastByte;
Off += sizeof (UINT64);
if (Size >= Off) {
@ -175,44 +192,50 @@ PeCoffTestLoadFull (
IN UINT32 FileSize
)
{
EFI_STATUS Status;
BOOLEAN Result;
PE_COFF_IMAGE_CONTEXT Context;
VOID *Destination;
UINT32 DestinationSize;
UINT8 HashContext;
EFI_STATUS Status;
BOOLEAN Result;
PE_COFF_LOADER_IMAGE_CONTEXT Context;
VOID *Destination;
UINT32 ImageSize;
UINT32 DestinationSize;
UINT32 DestinationPages;
UINT32 DestinationAlignment;
UINT8 HashContext;
Status = PeCoffInitializeContext (&Context, FileBuffer, FileSize);
if (EFI_ERROR (Status)) {
return EFI_UNSUPPORTED;
}
Result = PeCoffHashImage (
Result = PeCoffHashImageAuthenticode (
&Context,
HashUpdate,
&HashContext
&HashContext,
HashUpdate
);
if (!Result) {
return EFI_UNSUPPORTED;
}
DestinationSize = Context.SizeOfImage + Context.SizeOfImageDebugAdd;
if (BaseOverflowAddU32 (DestinationSize, Context.SectionAlignment, &DestinationSize)) {
return EFI_UNSUPPORTED;
}
ImageSize = PeCoffGetSizeOfImage (&Context);
DestinationPages = EFI_SIZE_TO_PAGES (ImageSize);
DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages);
DestinationAlignment = PeCoffGetSectionAlignment (&Context);
if (DestinationSize >= BASE_16MB) {
return EFI_UNSUPPORTED;
}
Destination = AllocatePages (EFI_SIZE_TO_PAGES (DestinationSize));
Destination = AllocateAlignedCodePages (
DestinationPages,
DestinationAlignment
);
if (Destination == NULL) {
return EFI_UNSUPPORTED;
}
Status = PeCoffTestLoad (&Context, Destination, DestinationSize);
FreePages (Destination, EFI_SIZE_TO_PAGES (DestinationSize));
FreeAlignedPages (Destination, DestinationPages);
return Status;
}