From 70bb87c7b0cc35d0c20d0e041e0641eb873acde1 Mon Sep 17 00:00:00 2001 From: Savva Mitrofanov Date: Thu, 13 Apr 2023 18:36:41 +0600 Subject: [PATCH] Library: Replace OcPeCoffLib with UefiImageLib --- Docs/Libraries.md | 2 +- Include/Acidanthera/Library/OcPeCoffExtLib.h | 2 +- Library/OcApfsLib/OcApfsIo.c | 2 +- Library/OcApfsLib/OcApfsLib.inf | 2 +- Library/OcBootManagementLib/ImageLoader.c | 85 +++++++++---------- .../OcBootManagementLib.inf | 2 +- Library/OcDeviceMiscLib/ReloadOptionRoms.c | 2 +- Library/OcPeCoffExtLib/OcPeCoffExtLib.c | 74 ++++++++-------- Library/OcPeCoffExtLib/OcPeCoffExtLib.inf | 2 +- OpenCorePkg.dec | 11 --- OpenCorePkg.dsc | 13 ++- Utilities/AppleEfiSignTool/Makefile | 3 +- Utilities/TestPeCoff/Makefile | 3 +- Utilities/TestPeCoff/PeCoff.c | 83 +++++++++++------- 14 files changed, 150 insertions(+), 136 deletions(-) diff --git a/Docs/Libraries.md b/Docs/Libraries.md index 9a47aec7..7b48d437 100644 --- a/Docs/Libraries.md +++ b/Docs/Libraries.md @@ -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 diff --git a/Include/Acidanthera/Library/OcPeCoffExtLib.h b/Include/Acidanthera/Library/OcPeCoffExtLib.h index eb3189e4..2d61bf75 100644 --- a/Include/Acidanthera/Library/OcPeCoffExtLib.h +++ b/Include/Acidanthera/Library/OcPeCoffExtLib.h @@ -10,7 +10,7 @@ #define OC_PE_COFF_EXT_LIB_H #include -#include +#include /** Verify Apple COFF legacy signature. diff --git a/Library/OcApfsLib/OcApfsIo.c b/Library/OcApfsLib/OcApfsIo.c index 5c231f7e..95bfb135 100644 --- a/Library/OcApfsLib/OcApfsIo.c +++ b/Library/OcApfsLib/OcApfsIo.c @@ -18,8 +18,8 @@ #include #include #include +#include #include -#include STATIC UINT64 diff --git a/Library/OcApfsLib/OcApfsLib.inf b/Library/OcApfsLib/OcApfsLib.inf index c38f9544..7317d489 100644 --- a/Library/OcApfsLib/OcApfsLib.inf +++ b/Library/OcApfsLib/OcApfsLib.inf @@ -62,9 +62,9 @@ OcConsoleLib OcDriverConnectionLib OcMiscLib - OcPeCoffLib OcPeCoffExtLib MemoryAllocationLib UefiBootServicesTableLib UefiLib + UefiImageLib UefiRuntimeServicesTableLib diff --git a/Library/OcBootManagementLib/ImageLoader.c b/Library/OcBootManagementLib/ImageLoader.c index fff5bb73..60bff7ec 100644 --- a/Library/OcBootManagementLib/ImageLoader.c +++ b/Library/OcBootManagementLib/ImageLoader.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -36,7 +37,7 @@ #include #include #include -#include +#include #include #include #include @@ -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. diff --git a/Library/OcBootManagementLib/OcBootManagementLib.inf b/Library/OcBootManagementLib/OcBootManagementLib.inf index 4502d133..efb5743b 100644 --- a/Library/OcBootManagementLib/OcBootManagementLib.inf +++ b/Library/OcBootManagementLib/OcBootManagementLib.inf @@ -94,6 +94,7 @@ MemoryAllocationLib PrintLib UefiBootServicesTableLib + UefiImageLib OcApfsLib OcAppleBootPolicyLib OcAppleChunklistLib @@ -109,7 +110,6 @@ OcFlexArrayLib OcMachoLib OcMiscLib - OcPeCoffLib OcRtcLib OcTypingLib OcVariableLib diff --git a/Library/OcDeviceMiscLib/ReloadOptionRoms.c b/Library/OcDeviceMiscLib/ReloadOptionRoms.c index 50d3ac1a..9926c32c 100644 --- a/Library/OcDeviceMiscLib/ReloadOptionRoms.c +++ b/Library/OcDeviceMiscLib/ReloadOptionRoms.c @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include diff --git a/Library/OcPeCoffExtLib/OcPeCoffExtLib.c b/Library/OcPeCoffExtLib/OcPeCoffExtLib.c index f3c2d290..97fffebb 100644 --- a/Library/OcPeCoffExtLib/OcPeCoffExtLib.c +++ b/Library/OcPeCoffExtLib/OcPeCoffExtLib.c @@ -25,6 +25,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #include #include #include @@ -32,7 +33,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include -#include #include #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")); diff --git a/Library/OcPeCoffExtLib/OcPeCoffExtLib.inf b/Library/OcPeCoffExtLib/OcPeCoffExtLib.inf index bb742a7d..e5e14fad 100644 --- a/Library/OcPeCoffExtLib/OcPeCoffExtLib.inf +++ b/Library/OcPeCoffExtLib/OcPeCoffExtLib.inf @@ -37,6 +37,7 @@ OpenCorePkg/OpenCorePkg.dec [LibraryClasses] + UefiImageLib UefiRuntimeServicesTableLib UefiBootServicesTableLib MemoryAllocationLib @@ -46,7 +47,6 @@ UefiLib DebugLib OcAppleKeysLib - OcPeCoffLib OcCryptoLib [Guids] diff --git a/OpenCorePkg.dec b/OpenCorePkg.dec index 6832118e..49ac9397 100755 --- a/OpenCorePkg.dec +++ b/OpenCorePkg.dec @@ -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.

## TRUE - TSC is allowed.
## FALSE - TSC is NOT allowed.
@@ -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 diff --git a/OpenCorePkg.dsc b/OpenCorePkg.dsc index 00dd22ed..9191496e 100755 --- a/OpenCorePkg.dsc +++ b/OpenCorePkg.dsc @@ -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 diff --git a/Utilities/AppleEfiSignTool/Makefile b/Utilities/AppleEfiSignTool/Makefile index 3a9761aa..6c8b89d6 100644 --- a/Utilities/AppleEfiSignTool/Makefile +++ b/Utilities/AppleEfiSignTool/Makefile @@ -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 diff --git a/Utilities/TestPeCoff/Makefile b/Utilities/TestPeCoff/Makefile index c6524467..b86966b4 100644 --- a/Utilities/TestPeCoff/Makefile +++ b/Utilities/TestPeCoff/Makefile @@ -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 diff --git a/Utilities/TestPeCoff/PeCoff.c b/Utilities/TestPeCoff/PeCoff.c index 5b9128ec..a5c3e046 100644 --- a/Utilities/TestPeCoff/PeCoff.c +++ b/Utilities/TestPeCoff/PeCoff.c @@ -5,10 +5,10 @@ #include "../Include/Uefi.h" -#include #include #include #include +#include #include #include @@ -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; }