PrmPkg: Refactor some PrmLoaderDxe functionality into libraries

This change breaks out two sets of responsibilities in
PrmLoaderDxe into libraries:
  * PE/COFF functions -> PrmPeCoffLib
  * PRM module discovery functions -> PrmModuleDiscoveryLib

This is core infrastructure code for PRM functionality that needs
to be directly reused and tested in other places. At this time,
the primary motivating factor is to use this code in two other
locations:
  1.) Link the functionality into unit testing modules
  2.) Link the functionality into a PRM UEFI application

Cc: Andrew Fish <afish@apple.com>
Cc: Kang Gao <kang.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Michael Kubacki <michael.kubacki@microsoft.com>
Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Liu Yun <yun.y.liu@intel.com>
Cc: Ankit Sinha <ankit.sinha@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Acked-by: Michael D Kinney <michael.d.kinney@intel.com>
Acked-by: Liming Gao <gaoliming@byosoft.com.cn>
Acked-by: Leif Lindholm <quic_llindhol@quicinc.com>
Reviewed-by: Ankit Sinha <ankit.sinha@intel.com>
This commit is contained in:
Michael Kubacki
2020-06-12 14:24:46 -07:00
committed by mergify[bot]
parent d10b8dc5d8
commit 6b7dde7cdd
13 changed files with 1133 additions and 636 deletions

View File

@@ -0,0 +1,60 @@
/** @file
The PRM Module Discovery library provides functionality to discover PRM modules installed by platform firmware.
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef PRM_MODULE_DISCOVERY_LIB_H_
#define PRM_MODULE_DISCOVERY_LIB_H_
#include <Base.h>
#include <PrmContextBuffer.h>
#include <PrmModuleImageContext.h>
#include <Uefi.h>
/**
Gets the next PRM module discovered after the given PRM module.
@param[in,out] ModuleImageContext A pointer to a pointer to a PRM module image context structure.
@retval EFI_SUCCESS The next PRM module was found successfully.
@retval EFI_INVALID_PARAMETER The given ModuleImageContext structure is invalid or the pointer is NULL.
@retval EFI_NOT_FOUND The next PRM module was not found.
**/
EFI_STATUS
EFIAPI
GetNextPrmModuleEntry (
IN OUT PRM_MODULE_IMAGE_CONTEXT **ModuleImageContext
);
/**
Discovers all PRM Modules loaded during boot.
Each PRM Module discovered is placed into a linked list so the list can br processsed in the future.
@param[out] ModuleCount An optional pointer parameter that, if provided, is set to the number
of PRM modules discovered.
@param[out] HandlerCount An optional pointer parameter that, if provided, is set to the number
of PRM handlers discovered.
@retval EFI_SUCCESS All PRM Modules were discovered successfully.
@retval EFI_INVALID_PARAMETER An actual pointer parameter was passed as NULL.
@retval EFI_NOT_FOUND The gEfiLoadedImageProtocolGuid protocol could not be found.
@retval EFI_OUT_OF_RESOURCES Insufficient memory resources to allocate the new PRM Context
linked list nodes.
@retval EFI_ALREADY_STARTED The function was called previously and already discovered the PRM modules
loaded on this boot.
**/
EFI_STATUS
EFIAPI
DiscoverPrmModules (
OUT UINTN *ModuleCount OPTIONAL,
OUT UINTN *HandlerCount OPTIONAL
);
#endif

View File

@@ -0,0 +1,111 @@
/** @file
The PRM PE/COFF library provides functionality to support additional PE/COFF functionality needed to use
Platform Runtime Mechanism (PRM) modules.
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef PRM_PECOFF_LIB_H_
#define PRM_PECOFF_LIB_H_
#include <Base.h>
#include <PrmExportDescriptor.h>
#include <IndustryStandard/PeImage.h>
#include <Library/PeCoffLib.h>
/**
Gets a pointer to the export directory in a given PE/COFF image.
@param[in] ImageExportDirectory A pointer to an export directory table in a PE/COFF image.
@param[in] PeCoffLoaderImageContext A pointer to a PE_COFF_LOADER_IMAGE_CONTEXT structure that contains the
PE/COFF image context for the Image containing the PRM Module Export
Descriptor table.
@param[out] ExportDescriptor A pointer to a pointer to the PRM Module Export Descriptor table found
in the ImageExportDirectory given.
@retval EFI_SUCCESS The PRM Module Export Descriptor table was found successfully.
@retval EFI_INVALID_PARAMETER A required parameter is NULL.
@retval EFI_NOT_FOUND The PRM Module Export Descriptor table was not found in the given
ImageExportDirectory.
**/
EFI_STATUS
GetPrmModuleExportDescriptorTable (
IN EFI_IMAGE_EXPORT_DIRECTORY *ImageExportDirectory,
IN PE_COFF_LOADER_IMAGE_CONTEXT *PeCoffLoaderImageContext,
OUT PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT **ExportDescriptor
);
/**
Gets a pointer to the export directory in a given PE/COFF image.
@param[in] Image A pointer to a PE32/COFF image base address that is loaded into memory
and already relocated to the memory base address. RVAs in the image given
should be valid.
@param[in] PeCoffLoaderImageContext A pointer to a PE_COFF_LOADER_IMAGE_CONTEXT structure that contains the
PE/COFF image context for the Image given.
@param[out] ImageExportDirectory A pointer to a pointer to the export directory found in the Image given.
@retval EFI_SUCCESS The export directory was found successfully.
@retval EFI_INVALID_PARAMETER A required parameter is NULL.
@retval EFI_UNSUPPORTED The PE/COFF image given is not supported as a PRM Module.
@retval EFI_NOT_FOUND The image export directory could not be found for this image.
**/
EFI_STATUS
GetExportDirectoryInPeCoffImage (
IN VOID *Image,
IN PE_COFF_LOADER_IMAGE_CONTEXT *PeCoffLoaderImageContext,
OUT EFI_IMAGE_EXPORT_DIRECTORY **ImageExportDirectory
);
/**
Returns the image major and image minor version in a given PE/COFF image.
@param[in] Image A pointer to a PE32/COFF image base address that is loaded into memory
and already relocated to the memory base address. RVAs in the image given
should be valid.
@param[in] PeCoffLoaderImageContext A pointer to a PE_COFF_LOADER_IMAGE_CONTEXT structure that contains the
PE/COFF image context for the Image given.
@param[out] ImageMajorVersion A pointer to a UINT16 buffer to hold the image major version.
@param[out] ImageMinorVersion A pointer to a UINT16 buffer to hold the image minor version.
@retval EFI_SUCCESS The image version was read successfully.
@retval EFI_INVALID_PARAMETER A required parameter is NULL.
@retval EFI_UNSUPPORTED The PE/COFF image given is not supported.
**/
EFI_STATUS
GetImageVersionInPeCoffImage (
IN VOID *Image,
IN PE_COFF_LOADER_IMAGE_CONTEXT *PeCoffLoaderImageContext,
OUT UINT16 *ImageMajorVersion,
OUT UINT16 *ImageMinorVersion
);
/**
Gets the address of an entry in an image export table by ASCII name.
@param[in] ExportName A pointer to an ASCII name string of the entry name.
@param[in] ImageBaseAddress The base address of the PE/COFF image.
@param[in] ImageExportDirectory A pointer to the export directory in the image.
@param[out] ExportPhysicalAddress A pointer that will be updated with the address of the address of the
export entry if found.
@retval EFI_SUCCESS The export entry was found successfully.
@retval EFI_INVALID_PARAMETER A required pointer argument is NULL.
@retval EFI_NOT_FOUND An entry with the given ExportName was not found.
**/
EFI_STATUS
GetExportEntryAddress (
IN CONST CHAR8 *ExportName,
IN EFI_PHYSICAL_ADDRESS ImageBaseAddress,
IN EFI_IMAGE_EXPORT_DIRECTORY *ImageExportDirectory,
OUT EFI_PHYSICAL_ADDRESS *ExportPhysicalAddress
);
#endif

View File

@@ -0,0 +1,28 @@
/** @file
Definitions used internal to the PrmPkg implementation for PRM module image context.
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef PRM_MODULE_IMAGE_CONTEXT_H_
#define PRM_MODULE_IMAGE_CONTEXT_H_
#include <IndustryStandard/PeImage.h>
#include <Library/PeCoffLib.h>
#include <PrmExportDescriptor.h>
#pragma pack(push, 1)
typedef struct {
PE_COFF_LOADER_IMAGE_CONTEXT PeCoffImageContext;
EFI_IMAGE_EXPORT_DIRECTORY *ExportDirectory;
PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *ExportDescriptor;
} PRM_MODULE_IMAGE_CONTEXT;
#pragma pack(pop)
#endif