diff --git a/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf b/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf index 33cd68bb1c..3d4a30603a 100644 --- a/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf +++ b/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf @@ -16,6 +16,7 @@ [Sources] CommonSupport.c + ExecutionSupport.c PeCoffSupport.h PeCoffSupport.c UefiImageLibPeCoff.c diff --git a/MdePkg/Library/BaseUefiImageLib/CommonSupport.c b/MdePkg/Library/BaseUefiImageLib/CommonSupport.c index ab814a3912..9b487b4e9d 100644 --- a/MdePkg/Library/BaseUefiImageLib/CommonSupport.c +++ b/MdePkg/Library/BaseUefiImageLib/CommonSupport.c @@ -11,7 +11,6 @@ #include #include -#include #include #include @@ -41,7 +40,6 @@ UefiImageLoaderGetImageEntryPoint ( UINT32 EntryPointAddress; ASSERT (Context != NULL); - ASSERT (Context->ImageBuffer != NULL); ImageAddress = UefiImageLoaderGetImageAddress (Context); EntryPointAddress = UefiImageGetEntryPointAddress (Context); @@ -49,104 +47,6 @@ UefiImageLoaderGetImageEntryPoint ( return ImageAddress + EntryPointAddress; } -RETURN_STATUS -UefiImageRelocateImageInplaceForExecution ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *Context - ) -{ - RETURN_STATUS Status; - UINTN ImageAddress; - UINTN ImageSize; - - Status = UefiImageRelocateImageInplace (Context); - if (RETURN_ERROR (Status)) { - DEBUG_RAISE (); - return Status; - } - - ImageAddress = UefiImageLoaderGetImageAddress (Context); - ImageSize = UefiImageGetImageSizeInplace (Context); - // - // Flush the instruction cache so the image data is written before - // execution. - // - InvalidateInstructionCacheRange ((VOID *) ImageAddress, ImageSize); - - return RETURN_SUCCESS; -} - -// FIXME: Check Subsystem here -RETURN_STATUS -UefiImageLoadImageForExecution ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *Context, - OUT VOID *Destination, - IN UINT32 DestinationSize, - OUT UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RuntimeContext OPTIONAL, - IN UINT32 RuntimeContextSize - ) -{ - RETURN_STATUS Status; - UINTN BaseAddress; - UINTN SizeOfImage; - // - // Load the Image into the memory space. - // - Status = UefiImageLoadImage (Context, Destination, DestinationSize); - if (RETURN_ERROR (Status)) { - return Status; - } - // - // Relocate the Image to the address it has been loaded to. - // - BaseAddress = UefiImageLoaderGetImageAddress (Context); - Status = UefiImageRelocateImage ( - Context, - BaseAddress, - RuntimeContext, - RuntimeContextSize - ); - if (RETURN_ERROR (Status)) { - return Status; - } - - SizeOfImage = UefiImageGetImageSize (Context); - // - // Flush the instruction cache so the image data is written before execution. - // - InvalidateInstructionCacheRange ((VOID *) BaseAddress, SizeOfImage); - - return RETURN_SUCCESS; -} - -RETURN_STATUS -UefiImageRuntimeRelocateImageForExecution ( - IN OUT VOID *Image, - IN UINT32 ImageSize, - IN UINT64 BaseAddress, - IN CONST UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RuntimeContext - ) -{ - RETURN_STATUS Status; - // - // Relocate the Image to the new address. - // - Status = UefiImageRuntimeRelocateImage ( - Image, - ImageSize, - BaseAddress, - RuntimeContext - ); - if (RETURN_ERROR (Status)) { - return Status; - } - // - // Flush the instruction cache so the image data is written before execution. - // - InvalidateInstructionCacheRange (Image, ImageSize); - - return RETURN_SUCCESS; -} - // FIXME: Some image prints use this and some don't. Is this really needed? RETURN_STATUS UefiImageGetModuleNameFromSymbolsPath ( diff --git a/MdePkg/Library/BaseUefiImageLib/ExecutionSupport.c b/MdePkg/Library/BaseUefiImageLib/ExecutionSupport.c new file mode 100644 index 0000000000..96b51cff09 --- /dev/null +++ b/MdePkg/Library/BaseUefiImageLib/ExecutionSupport.c @@ -0,0 +1,113 @@ +/** @file + Support for functions common to all Image formats. + + Copyright (c) 2021, Marvin Häuser. All rights reserved.
+ + SPDX-License-Identifier: BSD-3-Clause +**/ + +#include +#include +#include + +#include +#include +#include +#include + +RETURN_STATUS +UefiImageRelocateImageInplaceForExecution ( + IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *Context + ) +{ + RETURN_STATUS Status; + UINTN ImageAddress; + UINTN ImageSize; + + Status = UefiImageRelocateImageInplace (Context); + if (RETURN_ERROR (Status)) { + return Status; + } + + ImageAddress = UefiImageLoaderGetImageAddress (Context); + ImageSize = UefiImageGetImageSizeInplace (Context); + // + // Flush the instruction cache so the image data is written before + // execution. + // + InvalidateInstructionCacheRange ((VOID *) ImageAddress, ImageSize); + + return RETURN_SUCCESS; +} + +// FIXME: Check Subsystem here +RETURN_STATUS +UefiImageLoadImageForExecution ( + IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *Context, + OUT VOID *Destination, + IN UINT32 DestinationSize, + OUT UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RuntimeContext OPTIONAL, + IN UINT32 RuntimeContextSize + ) +{ + RETURN_STATUS Status; + UINTN BaseAddress; + UINTN SizeOfImage; + // + // Load the Image into the memory space. + // + Status = UefiImageLoadImage (Context, Destination, DestinationSize); + if (RETURN_ERROR (Status)) { + return Status; + } + // + // Relocate the Image to the address it has been loaded to. + // + BaseAddress = UefiImageLoaderGetImageAddress (Context); + Status = UefiImageRelocateImage ( + Context, + BaseAddress, + RuntimeContext, + RuntimeContextSize + ); + if (RETURN_ERROR (Status)) { + return Status; + } + + SizeOfImage = UefiImageGetImageSize (Context); + // + // Flush the instruction cache so the image data is written before execution. + // + InvalidateInstructionCacheRange ((VOID *) BaseAddress, SizeOfImage); + + return RETURN_SUCCESS; +} + +RETURN_STATUS +UefiImageRuntimeRelocateImageForExecution ( + IN OUT VOID *Image, + IN UINT32 ImageSize, + IN UINT64 BaseAddress, + IN CONST UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RuntimeContext + ) +{ + RETURN_STATUS Status; + // + // Relocate the Image to the new address. + // + Status = UefiImageRuntimeRelocateImage ( + Image, + ImageSize, + BaseAddress, + RuntimeContext + ); + if (RETURN_ERROR (Status)) { + return Status; + } + // + // Flush the instruction cache so the image data is written before execution. + // + InvalidateInstructionCacheRange (Image, ImageSize); + + return RETURN_SUCCESS; +}