Skip to content

Commit

Permalink
MdeModulePkg: Added EFI_PEI_LOAD_FILE_WITH_HOB_PPI
Browse files Browse the repository at this point in the history
and removed temporary hook.
  • Loading branch information
Mikhail Krichanov committed Oct 12, 2023
1 parent 5044534 commit c21a7a4
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 60 deletions.
4 changes: 2 additions & 2 deletions MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
gEfiPeiDecompressPpiGuid ## PRODUCES
gEfiEndOfPeiSignalPpiGuid ## SOMETIMES_PRODUCES # Not produced on S3 boot path
gEfiPeiReadOnlyVariable2PpiGuid ## SOMETIMES_CONSUMES
gEfiPeiLoadFilePpiGuid ## SOMETIMES_CONSUMES
gEfiPeiLoadFileWithHobPpiGuid ## SOMETIMES_CONSUMES
gEfiPeiS3Resume2PpiGuid ## SOMETIMES_CONSUMES # Consumed on S3 boot path
gEfiPeiRecoveryModulePpiGuid ## SOMETIMES_CONSUMES # Consumed on recovery boot path
## SOMETIMES_CONSUMES
Expand Down Expand Up @@ -130,7 +130,7 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdImageProtectionPolicy ## SOMETIMES_CONSUMES

[Depex]
gEfiPeiLoadFilePpiGuid AND gEfiPeiMasterBootModePpiGuid
gEfiPeiLoadFileWithHobPpiGuid AND gEfiPeiMasterBootModePpiGuid

#
# [BootMode]
Expand Down
8 changes: 4 additions & 4 deletions MdeModulePkg/Core/DxeIplPeim/DxeLoad.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ DxeLoadCore (
EFI_BOOT_MODE BootMode;
EFI_PEI_FILE_HANDLE FileHandle;
EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable;
EFI_PEI_LOAD_FILE_PPI *LoadFile;
EFI_PEI_LOAD_FILE_WITH_HOB_PPI *LoadFile;
UINTN Instance;
UINT32 AuthenticationState;
UINTN DataSize;
Expand Down Expand Up @@ -410,19 +410,19 @@ DxeLoadCore (
//
Instance = 0;
do {
Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, Instance++, NULL, (VOID **)&LoadFile);
Status = PeiServicesLocatePpi (&gEfiPeiLoadFileWithHobPpiGuid, Instance++, NULL, (VOID **)&LoadFile);
//
// These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
//
ASSERT_EFI_ERROR (Status);

Status = LoadFile->LoadFile (
(CONST EFI_PEI_LOAD_FILE_PPI *)&ImageContext,
FileHandle,
&DxeCoreAddress,
&DxeCoreSize,
&DxeCoreEntryPoint,
&AuthenticationState
&AuthenticationState,
ImageContext
);
} while (EFI_ERROR (Status));

Expand Down
209 changes: 155 additions & 54 deletions MdeModulePkg/Core/Pei/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {
PeiLoadImageLoadImageWrapper
};

EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {
(EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
&gEfiPeiLoadFilePpiGuid,
&mPeiLoadImagePpi
EFI_PEI_LOAD_FILE_WITH_HOB_PPI mPeiLoadImageWithHobPpi = {
PeiLoadImageLoadImageWithHob
};

EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList[] = {
{
EFI_PEI_PPI_DESCRIPTOR_PPI,
&gEfiPeiLoadFileWithHobPpiGuid,
&mPeiLoadImageWithHobPpi
},
{
(EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
&gEfiPeiLoadFilePpiGuid,
&mPeiLoadImagePpi
}
};

/**
Expand Down Expand Up @@ -461,10 +472,6 @@ PeiLoadImageLoadImage (
EFI_PHYSICAL_ADDRESS ImageAddress;
UINTN DebugBase;
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
UINTN Instance;
EFI_PEI_LOAD_FILE_PPI *LoadFile;
BOOLEAN IsHook;
HOB_IMAGE_CONTEXT *Hob;

*EntryPoint = 0;
*AuthenticationState = 0;
Expand Down Expand Up @@ -506,52 +513,146 @@ PeiLoadImageLoadImage (
}

//
// Save ImageContext into DXE CORE HOB
// Got the entry point from the loaded Pe32Data
//
Instance = 0;
IsHook = TRUE;
do {
Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, Instance++, NULL, (VOID **)&LoadFile);
if ((UINTN)PeiServices == (UINTN)LoadFile) {
IsHook = FALSE;
}
} while (!EFI_ERROR (Status));

if (IsHook) {
Hob = (HOB_IMAGE_CONTEXT *)*PeiServices;

Hob->FormatIndex = ImageContext.FormatIndex;

if (Hob->FormatIndex == UefiImageFormatPe) {
Hob->Ctx.Pe.ImageBuffer = (UINT32)(UINTN)ImageContext.Ctx.Pe.ImageBuffer;
Hob->Ctx.Pe.AddressOfEntryPoint = ImageContext.Ctx.Pe.AddressOfEntryPoint;
Hob->Ctx.Pe.ImageType = ImageContext.Ctx.Pe.ImageType;
Hob->Ctx.Pe.FileBuffer = (UINT32)(UINTN)ImageContext.Ctx.Pe.FileBuffer;
Hob->Ctx.Pe.ExeHdrOffset = ImageContext.Ctx.Pe.ExeHdrOffset;
Hob->Ctx.Pe.SizeOfImage = ImageContext.Ctx.Pe.SizeOfImage;
Hob->Ctx.Pe.FileSize = ImageContext.Ctx.Pe.FileSize;
Hob->Ctx.Pe.Subsystem = ImageContext.Ctx.Pe.Subsystem;
Hob->Ctx.Pe.SectionAlignment = ImageContext.Ctx.Pe.SectionAlignment;
Hob->Ctx.Pe.SectionsOffset = ImageContext.Ctx.Pe.SectionsOffset;
Hob->Ctx.Pe.NumberOfSections = ImageContext.Ctx.Pe.NumberOfSections;
Hob->Ctx.Pe.SizeOfHeaders = ImageContext.Ctx.Pe.SizeOfHeaders;
} else if (Hob->FormatIndex == UefiImageFormatUe) {
Hob->Ctx.Ue.ImageBuffer = (UINT32)(UINTN)ImageContext.Ctx.Ue.ImageBuffer;
Hob->Ctx.Ue.FileBuffer = (UINT32)(UINTN)ImageContext.Ctx.Ue.FileBuffer;
Hob->Ctx.Ue.EntryPointAddress = ImageContext.Ctx.Ue.EntryPointAddress;
Hob->Ctx.Ue.LoadTablesFileOffset = ImageContext.Ctx.Ue.LoadTablesFileOffset;
Hob->Ctx.Ue.NumLoadTables = ImageContext.Ctx.Ue.NumLoadTables;
Hob->Ctx.Ue.LoadTables = (UINT32)(UINTN)ImageContext.Ctx.Ue.LoadTables;
Hob->Ctx.Ue.Segments = (UINT32)(UINTN)ImageContext.Ctx.Ue.Segments;
Hob->Ctx.Ue.LastSegmentIndex = ImageContext.Ctx.Ue.LastSegmentIndex;
Hob->Ctx.Ue.SegmentAlignment = ImageContext.Ctx.Ue.SegmentAlignment;
Hob->Ctx.Ue.ImageSize = ImageContext.Ctx.Ue.ImageSize;
Hob->Ctx.Ue.Subsystem = ImageContext.Ctx.Ue.Subsystem;
Hob->Ctx.Ue.SegmentImageInfoIterSize = ImageContext.Ctx.Ue.SegmentImageInfoIterSize;
Hob->Ctx.Ue.SegmentsFileOffset = ImageContext.Ctx.Ue.SegmentsFileOffset;
*EntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext);

if (ImageAddressArg != NULL) {
*ImageAddressArg = ImageAddress;
}

if (ImageSizeArg != NULL) {
*ImageSizeArg =UefiImageGetImageSize (&ImageContext);
}

DEBUG_CODE_BEGIN ();
CHAR8 EfiFileName[512];
UINT16 Machine;

Machine = UefiImageGetMachine (&ImageContext);

//
// Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
//
if (Machine != EFI_IMAGE_MACHINE_IA64) {
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading PEIM at 0x%11p DebugBase=0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)DebugBase, (VOID *)(UINTN)*EntryPoint));
} else {
ASSERT (FALSE);
//
// For IPF Image, the real entry point should be print.
//
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading PEIM at 0x%11p DebugBase=0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)DebugBase, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));
}

//
// Print Module Name by PeImage PDB file name.
//
Status = UefiImageGetModuleNameFromSymbolsPath (
&ImageContext,
EfiFileName,
sizeof (EfiFileName)
);
if (!RETURN_ERROR (Status)) {
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName));
}

DEBUG_CODE_END ();

DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));

return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
PeiLoadImageLoadImageWithHob (
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg OPTIONAL,
OUT UINT64 *ImageSizeArg OPTIONAL,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState,
OUT HOB_IMAGE_CONTEXT *Hob
)
{
EFI_STATUS Status;
VOID *Pe32Data;
UINT32 Pe32DataSize;
EFI_PHYSICAL_ADDRESS ImageAddress;
UINTN DebugBase;
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;

*EntryPoint = 0;
*AuthenticationState = 0;

//
// Try to find the exe section.
//
Status = PeiServicesFfsFindSectionData4 (
EFI_SECTION_PE32,
0,
FileHandle,
&Pe32Data,
&Pe32DataSize,
AuthenticationState
);
if (EFI_ERROR (Status)) {
//
// PEI core only carry the loader function for PE32 executables
// If this two section does not exist, just return.
//
return Status;
}

DEBUG ((DEBUG_INFO, "Loading PEIM %g\n", FileHandle));

//
// If memory is installed, perform the shadow operations
//
Status = LoadAndRelocateUefiImage (
FileHandle,
Pe32Data,
Pe32DataSize,
&ImageContext,
&ImageAddress,
&DebugBase
);
if (EFI_ERROR (Status)) {
return Status;
}

//
// Save ImageContext into DXE CORE HOB
//
Hob->FormatIndex = ImageContext.FormatIndex;

if (Hob->FormatIndex == UefiImageFormatPe) {
Hob->Ctx.Pe.ImageBuffer = (UINT32)(UINTN)ImageContext.Ctx.Pe.ImageBuffer;
Hob->Ctx.Pe.AddressOfEntryPoint = ImageContext.Ctx.Pe.AddressOfEntryPoint;
Hob->Ctx.Pe.ImageType = ImageContext.Ctx.Pe.ImageType;
Hob->Ctx.Pe.FileBuffer = (UINT32)(UINTN)ImageContext.Ctx.Pe.FileBuffer;
Hob->Ctx.Pe.ExeHdrOffset = ImageContext.Ctx.Pe.ExeHdrOffset;
Hob->Ctx.Pe.SizeOfImage = ImageContext.Ctx.Pe.SizeOfImage;
Hob->Ctx.Pe.FileSize = ImageContext.Ctx.Pe.FileSize;
Hob->Ctx.Pe.Subsystem = ImageContext.Ctx.Pe.Subsystem;
Hob->Ctx.Pe.SectionAlignment = ImageContext.Ctx.Pe.SectionAlignment;
Hob->Ctx.Pe.SectionsOffset = ImageContext.Ctx.Pe.SectionsOffset;
Hob->Ctx.Pe.NumberOfSections = ImageContext.Ctx.Pe.NumberOfSections;
Hob->Ctx.Pe.SizeOfHeaders = ImageContext.Ctx.Pe.SizeOfHeaders;
} else if (Hob->FormatIndex == UefiImageFormatUe) {
Hob->Ctx.Ue.ImageBuffer = (UINT32)(UINTN)ImageContext.Ctx.Ue.ImageBuffer;
Hob->Ctx.Ue.FileBuffer = (UINT32)(UINTN)ImageContext.Ctx.Ue.FileBuffer;
Hob->Ctx.Ue.EntryPointAddress = ImageContext.Ctx.Ue.EntryPointAddress;
Hob->Ctx.Ue.LoadTablesFileOffset = ImageContext.Ctx.Ue.LoadTablesFileOffset;
Hob->Ctx.Ue.NumLoadTables = ImageContext.Ctx.Ue.NumLoadTables;
Hob->Ctx.Ue.LoadTables = (UINT32)(UINTN)ImageContext.Ctx.Ue.LoadTables;
Hob->Ctx.Ue.Segments = (UINT32)(UINTN)ImageContext.Ctx.Ue.Segments;
Hob->Ctx.Ue.LastSegmentIndex = ImageContext.Ctx.Ue.LastSegmentIndex;
Hob->Ctx.Ue.SegmentAlignment = ImageContext.Ctx.Ue.SegmentAlignment;
Hob->Ctx.Ue.ImageSize = ImageContext.Ctx.Ue.ImageSize;
Hob->Ctx.Ue.Subsystem = ImageContext.Ctx.Ue.Subsystem;
Hob->Ctx.Ue.SegmentImageInfoIterSize = ImageContext.Ctx.Ue.SegmentImageInfoIterSize;
Hob->Ctx.Ue.SegmentsFileOffset = ImageContext.Ctx.Ue.SegmentsFileOffset;
} else {
ASSERT (FALSE);
}

//
Expand Down Expand Up @@ -629,7 +730,7 @@ PeiLoadImageLoadImageWrapper (
)
{
return PeiLoadImageLoadImage (
(CONST EFI_PEI_SERVICES **)This,
GetPeiServicesTablePointer (),
FileHandle,
ImageAddressArg,
ImageSizeArg,
Expand Down Expand Up @@ -799,13 +900,13 @@ InitializeImageServices (
// The first time we are XIP (running from FLASH). We need to remember the
// FLASH address so we can reinstall the memory version that runs faster
//
PrivateData->XipLoadFile = &gPpiLoadFilePpiList;
PrivateData->XipLoadFile = gPpiLoadFilePpiList;
PeiServicesInstallPpi (PrivateData->XipLoadFile);
} else {
//
// 2nd time we are running from memory so replace the XIP version with the
// new memory version.
//
PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);
PeiServicesReInstallPpi (PrivateData->XipLoadFile, gPpiLoadFilePpiList);
}
}
11 changes: 11 additions & 0 deletions MdeModulePkg/Core/Pei/PeiMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,17 @@ PeiLoadImageLoadImageWrapper (
OUT UINT32 *AuthenticationState
);

EFI_STATUS
EFIAPI
PeiLoadImageLoadImageWithHob (
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg OPTIONAL,
OUT UINT64 *ImageSizeArg OPTIONAL,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState,
OUT HOB_IMAGE_CONTEXT *Hob
);

/**
Provide a callback for when the security PPI is installed.
Expand Down
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Pei/PeiMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
## PRODUCES
## CONSUMES
gEfiPeiLoadFilePpiGuid
gEfiPeiLoadFileWithHobPpiGuid
gEfiPeiSecurity2PpiGuid ## NOTIFY
gEfiTemporaryRamSupportPpiGuid ## SOMETIMES_CONSUMES
gEfiTemporaryRamDonePpiGuid ## SOMETIMES_CONSUMES
Expand Down
19 changes: 19 additions & 0 deletions MdePkg/Include/Ppi/LoadFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
#ifndef __LOAD_FILE_PPI_H__
#define __LOAD_FILE_PPI_H__

#include <Library/UefiImageLib.h>

#define EFI_PEI_LOAD_FILE_PPI_GUID \
{ 0xb9e0abfe, 0x5979, 0x4914, { 0x97, 0x7f, 0x6d, 0xee, 0x78, 0xc2, 0x78, 0xa6 } }

typedef struct _EFI_PEI_LOAD_FILE_PPI EFI_PEI_LOAD_FILE_PPI;
typedef struct _EFI_PEI_LOAD_FILE_WITH_HOB_PPI EFI_PEI_LOAD_FILE_WITH_HOB_PPI;

/**
Loads a PEIM into memory for subsequent execution.
Expand Down Expand Up @@ -56,6 +59,17 @@ EFI_STATUS
OUT UINT32 *AuthenticationState
);

typedef
EFI_STATUS
(EFIAPI *EFI_PEI_LOAD_FILE_WITH_HOB)(
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
OUT UINT64 *ImageSize,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState,
OUT HOB_IMAGE_CONTEXT *Hob
);

///
/// This PPI is a pointer to the Load File service.
/// This service will be published by a PEIM. The PEI Foundation
Expand All @@ -65,6 +79,11 @@ struct _EFI_PEI_LOAD_FILE_PPI {
EFI_PEI_LOAD_FILE LoadFile;
};

struct _EFI_PEI_LOAD_FILE_WITH_HOB_PPI {
EFI_PEI_LOAD_FILE_WITH_HOB LoadFile;
};

extern EFI_GUID gEfiPeiLoadFilePpiGuid;
extern EFI_GUID gEfiPeiLoadFileWithHobPpiGuid;

#endif
1 change: 1 addition & 0 deletions MdePkg/MdePkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@

## Include/Ppi/LoadFile.h
gEfiPeiLoadFilePpiGuid = { 0xb9e0abfe, 0x5979, 0x4914, { 0x97, 0x7f, 0x6d, 0xee, 0x78, 0xc2, 0x78, 0xa6 } }
gEfiPeiLoadFileWithHobPpiGuid = { 0x14c2d0d0, 0xccfd, 0x40e5, { 0xae, 0xa7, 0x57, 0x23, 0x58, 0xdd, 0xbf, 0xe8 } }

## Include/Ppi/Decompress.h
gEfiPeiDecompressPpiGuid = { 0x1a36e4e7, 0xfab6, 0x476a, { 0x8e, 0x75, 0x69, 0x5a, 0x5, 0x76, 0xfd, 0xd7 } }
Expand Down

0 comments on commit c21a7a4

Please sign in to comment.