MdePkg/BaseUefiImageLib: Factor out ExecutionSupport

This commit is contained in:
Marvin Häuser 2023-04-23 22:50:40 +02:00 committed by Mikhail Krichanov
parent efb6eea39e
commit 07d0ca418e
3 changed files with 114 additions and 100 deletions

View File

@ -16,6 +16,7 @@
[Sources] [Sources]
CommonSupport.c CommonSupport.c
ExecutionSupport.c
PeCoffSupport.h PeCoffSupport.h
PeCoffSupport.c PeCoffSupport.c
UefiImageLibPeCoff.c UefiImageLibPeCoff.c

View File

@ -11,7 +11,6 @@
#include <Uefi/UefiSpec.h> #include <Uefi/UefiSpec.h>
#include <Library/BaseOverflowLib.h> #include <Library/BaseOverflowLib.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/DebugLib.h> #include <Library/DebugLib.h>
#include <Library/UefiImageLib.h> #include <Library/UefiImageLib.h>
@ -41,7 +40,6 @@ UefiImageLoaderGetImageEntryPoint (
UINT32 EntryPointAddress; UINT32 EntryPointAddress;
ASSERT (Context != NULL); ASSERT (Context != NULL);
ASSERT (Context->ImageBuffer != NULL);
ImageAddress = UefiImageLoaderGetImageAddress (Context); ImageAddress = UefiImageLoaderGetImageAddress (Context);
EntryPointAddress = UefiImageGetEntryPointAddress (Context); EntryPointAddress = UefiImageGetEntryPointAddress (Context);
@ -49,104 +47,6 @@ UefiImageLoaderGetImageEntryPoint (
return ImageAddress + EntryPointAddress; 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? // FIXME: Some image prints use this and some don't. Is this really needed?
RETURN_STATUS RETURN_STATUS
UefiImageGetModuleNameFromSymbolsPath ( UefiImageGetModuleNameFromSymbolsPath (

View File

@ -0,0 +1,113 @@
/** @file
Support for functions common to all Image formats.
Copyright (c) 2021, Marvin Häuser. All rights reserved.<BR>
SPDX-License-Identifier: BSD-3-Clause
**/
#include <Base.h>
#include <Uefi/UefiBaseType.h>
#include <Uefi/UefiSpec.h>
#include <Library/BaseOverflowLib.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiImageLib.h>
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;
}