From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web09.12131.1647966134028652337 for ; Tue, 22 Mar 2022 09:22:14 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@linux.microsoft.com header.s=default header.b=McDu9ylV; spf=pass (domain: linux.microsoft.com, ip: 13.77.154.182, mailfrom: mikuback@linux.microsoft.com) Received: from localhost.localdomain (unknown [47.202.59.224]) by linux.microsoft.com (Postfix) with ESMTPSA id 93CB520B4783; Tue, 22 Mar 2022 09:22:12 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 93CB520B4783 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1647966133; bh=HiO35w5s5BZEwTFHK9COko0evRD4ca172G13pL7vtUs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=McDu9ylV5riAQHxDPGr7zKAuA8mns/j1iDy8IhccoZxqI5B1gGnFGaALDozhnHZM6 /k080OmEPWRKJWKUFwqGGHoy3l7rsCgbUpE1IOEDC/4qfiBBzoGyuNG8tSmbT8EKd/ ukEbnkByb4MRG/TXsXzIb43/cAXVg7K0Mq1W/9x4= From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Andrew Fish , Kang Gao , Michael D Kinney , Michael Kubacki , Leif Lindholm , Benjamin You , Liu Yun , Ankit Sinha , Nate DeSimone Subject: [PATCH v1 25/41] PrmPkg: Refactor some PrmLoaderDxe functionality into libraries Date: Tue, 22 Mar 2022 12:19:31 -0400 Message-Id: <20220322161947.9319-26-mikuback@linux.microsoft.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20220322161947.9319-1-mikuback@linux.microsoft.com> References: <20220322161947.9319-1-mikuback@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Kubacki 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 Cc: Kang Gao Cc: Michael D Kinney Cc: Michael Kubacki Cc: Leif Lindholm Cc: Benjamin You Cc: Liu Yun Cc: Ankit Sinha Cc: Nate DeSimone Signed-off-by: Michael Kubacki --- PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscoveryLib.c | 3= 82 +++++++++++++ PrmPkg/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.c | 4= 11 ++++++++++++++ PrmPkg/PrmLoaderDxe/PrmLoaderDxe.c | 6= 00 +------------------- PrmPkg/Include/Library/PrmModuleDiscoveryLib.h | = 60 ++ PrmPkg/Include/Library/PrmPeCoffLib.h | 1= 11 ++++ PrmPkg/Include/PrmModuleImageContext.h | = 28 + PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscoveryLib.inf | = 41 ++ PrmPkg/Library/DxePrmModuleDiscoveryLib/PrmModuleDiscovery.h | = 27 + PrmPkg/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.inf | = 32 ++ PrmPkg/PrmLoaderDxe/PrmLoader.h | = 51 -- PrmPkg/PrmLoaderDxe/PrmLoaderDxe.inf | = 5 +- PrmPkg/PrmPkg.dec | = 8 + PrmPkg/PrmPkg.dsc | = 13 + 13 files changed, 1133 insertions(+), 636 deletions(-) diff --git a/PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscover= yLib.c b/PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscoveryLib= .c new file mode 100644 index 000000000000..6977799aa8f3 --- /dev/null +++ b/PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscoveryLib.c @@ -0,0 +1,382 @@ +/** @file + + The PRM Module Discovery library provides functionality to discover PR= M modules installed by platform firmware. + + Copyright (c) Microsoft Corporation + Copyright (c) 2020 - 2022, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "PrmModuleDiscovery.h" + +#define _DBGMSGID_ "[PRMMODULEDISCOVERYLIB]" + +LIST_ENTRY mPrmModuleList; + +/** + Gets the next PRM module discovered after the given PRM module. + + @param[in,out] ModuleImageContext A pointer to a pointer to a PR= M module image context structure. + ModuleImageContext should poin= t to a pointer that points to NULL to + get the first PRM module disco= vered. + + @retval EFI_SUCCESS The next PRM module was found = successfully. + @retval EFI_INVALID_PARAMETER The given ModuleImageContext s= tructure is invalid or the pointer is NULL. + @retval EFI_NOT_FOUND The next PRM module was not fo= und. + +**/ +EFI_STATUS +EFIAPI +GetNextPrmModuleEntry ( + IN OUT PRM_MODULE_IMAGE_CONTEXT **ModuleImageContext + ) +{ + LIST_ENTRY *CurrentLink; + LIST_ENTRY *ForwardLink; + PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *CurrentListEntry; + PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *ForwardListEntry; + + DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); + + if (ModuleImageContext =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if (*ModuleImageContext =3D=3D NULL) { + ForwardLink =3D GetFirstNode (&mPrmModuleList); + } else { + CurrentListEntry =3D NULL; + CurrentListEntry =3D CR (*ModuleImageContext, PRM_MODULE_IMAGE_CONTE= XT_LIST_ENTRY, Context, PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY_SIGNATURE); + if (CurrentListEntry =3D=3D NULL || CurrentListEntry->Signature !=3D= PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY_SIGNATURE) { + return EFI_INVALID_PARAMETER; + } + + CurrentLink =3D &CurrentListEntry->Link; + ForwardLink =3D GetNextNode (&mPrmModuleList, CurrentLink); + + if (ForwardLink =3D=3D &mPrmModuleList) { + return EFI_NOT_FOUND; + } + } + + ForwardListEntry =3D BASE_CR (ForwardLink, PRM_MODULE_IMAGE_CONTEXT_LI= ST_ENTRY, Link); + if (ForwardListEntry->Signature =3D=3D PRM_MODULE_IMAGE_CONTEXT_LIST_E= NTRY_SIGNATURE) { + *ModuleImageContext =3D &ForwardListEntry->Context; + return EFI_SUCCESS; + } + + return EFI_NOT_FOUND; +} + +/** + Creates a new PRM Module Image Context linked list entry. + + @retval PrmModuleImageContextListEntry If successful, a pointer a PRM= Module Image Context linked list entry + otherwise, NULL is returned. + +**/ +STATIC +PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY * +CreateNewPrmModuleImageContextListEntry ( + VOID + ) +{ + PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *PrmModuleImageContextListEntr= y; + + DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); + + PrmModuleImageContextListEntry =3D AllocateZeroPool (sizeof (*PrmModul= eImageContextListEntry)); + if (PrmModuleImageContextListEntry =3D=3D NULL) { + return NULL; + } + DEBUG (( + DEBUG_INFO, + " %a %a: Allocated PrmModuleImageContextListEntry at 0x%x of size 0= x%x bytes.\n", + _DBGMSGID_, + __FUNCTION__, + (UINTN) PrmModuleImageContextListEntry, + sizeof (*PrmModuleImageContextListEntry) + )); + + PrmModuleImageContextListEntry->Signature =3D PRM_MODULE_IMAGE_CONTEXT= _LIST_ENTRY_SIGNATURE; + + return PrmModuleImageContextListEntry; +} + +/** + Check whether the address is within any of the MMRAM ranges. + + @param[in] Address The address to be checked. + @param[in] MmramRanges Pointer to MMRAM descriptor. + @param[in] MmramRangeCount MMRAM range count. + + @retval TRUE The address is in MMRAM ranges. + @retval FALSE The address is out of MMRAM ranges. +**/ +BOOLEAN +EFIAPI +IsAddressInMmram ( + IN EFI_PHYSICAL_ADDRESS Address, + IN EFI_MMRAM_DESCRIPTOR *MmramRanges, + IN UINTN MmramRangeCount + ) +{ + UINTN Index; + + for (Index =3D 0; Index < MmramRangeCount; Index++) { + if ((Address >=3D MmramRanges[Index].CpuStart) && + (Address < (MmramRanges[Index].CpuStart + MmramRanges[Index].Phy= sicalSize))) + { + return TRUE; + } + } + + return FALSE; +} + +/** + Discovers all PRM Modules loaded during boot. + + Each PRM Module discovered is placed into a linked list so the list ca= n 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 discovere= d successfully. + @retval EFI_INVALID_PARAMETER An actual pointer parameter wa= s passed as NULL. + @retval EFI_NOT_FOUND The gEfiLoadedImageProtocolGui= d 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 previo= usly and already discovered the PRM modules + loaded on this boot. + +**/ +EFI_STATUS +EFIAPI +DiscoverPrmModules ( + OUT UINTN *ModuleCount OPTIONAL, + OUT UINTN *HandlerCount OPTIONAL + ) +{ + EFI_STATUS Status; + PRM_MODULE_IMAGE_CONTEXT TempPrmModuleImageContext; + PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *PrmModuleImageContextListEntr= y; + EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProtocol; + EFI_HANDLE *HandleBuffer; + UINTN HandleCount; + UINTN Index; + UINTN PrmHandlerCount; + UINTN PrmModuleCount; + EFI_MM_ACCESS_PROTOCOL *MmAccess; + UINTN Size; + EFI_MMRAM_DESCRIPTOR *MmramRanges; + UINTN MmramRangeCount; + + DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); + + PrmHandlerCount =3D 0; + PrmModuleCount =3D 0; + + if (!IsListEmpty (&mPrmModuleList)) { + return EFI_ALREADY_STARTED; + } + + Status =3D gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiLoadedImageProtocolGuid, + NULL, + &HandleCount, + &HandleBuffer + ); + if (EFI_ERROR (Status) && (HandleCount =3D=3D 0)) { + DEBUG ((DEBUG_ERROR, "%a %a: No LoadedImageProtocol instances found!= \n", _DBGMSGID_, __FUNCTION__)); + return EFI_NOT_FOUND; + } + + MmramRanges =3D NULL; + MmramRangeCount =3D 0; + Status =3D gBS->LocateProtocol ( + &gEfiMmAccessProtocolGuid, + NULL, + (VOID **)&MmAccess + ); + if (Status =3D=3D EFI_SUCCESS) { + // + // Get MMRAM range information + // + Size =3D 0; + Status =3D MmAccess->GetCapabilities (MmAccess, &Size, NULL); + if ((Status =3D=3D EFI_BUFFER_TOO_SMALL) && (Size !=3D 0)) { + MmramRanges =3D (EFI_MMRAM_DESCRIPTOR *)AllocatePool (Size); + if (MmramRanges !=3D NULL) { + Status =3D MmAccess->GetCapabilities (MmAccess, &Size, MmramRang= es); + if (Status =3D=3D EFI_SUCCESS) { + MmramRangeCount =3D Size / sizeof (EFI_MMRAM_DESCRIPTOR); + } + } + } + } + + for (Index =3D 0; Index < HandleCount; Index++) { + Status =3D gBS->HandleProtocol ( + HandleBuffer[Index], + &gEfiLoadedImageProtocolGuid, + (VOID **) &LoadedImageProtocol + ); + if (EFI_ERROR (Status)) { + continue; + } + + if (IsAddressInMmram ((EFI_PHYSICAL_ADDRESS)(UINTN)(LoadedImageProto= col->ImageBase), MmramRanges, MmramRangeCount)) { + continue; + } + + ZeroMem (&TempPrmModuleImageContext, sizeof (TempPrmModuleImageConte= xt)); + TempPrmModuleImageContext.PeCoffImageContext.Handle =3D LoadedIma= geProtocol->ImageBase; + TempPrmModuleImageContext.PeCoffImageContext.ImageRead =3D PeCoffLoa= derImageReadFromMemory; + + Status =3D PeCoffLoaderGetImageInfo (&TempPrmModuleImageContext.PeCo= ffImageContext); + if (EFI_ERROR (Status) || TempPrmModuleImageContext.PeCoffImageConte= xt.ImageError !=3D IMAGE_ERROR_SUCCESS) { + DEBUG (( + DEBUG_WARN, + "%a %a: ImageHandle 0x%016lx is not a valid PE/COFF image. It ca= nnot be considered a PRM module.\n", + _DBGMSGID_, + __FUNCTION__, + (EFI_PHYSICAL_ADDRESS) (UINTN) LoadedImageProtocol->ImageBase + )); + continue; + } + if (TempPrmModuleImageContext.PeCoffImageContext.IsTeImage) { + // A PRM Module is not allowed to be a TE image + continue; + } + + // Attempt to find an export table in this image + Status =3D GetExportDirectoryInPeCoffImage ( + LoadedImageProtocol->ImageBase, + &TempPrmModuleImageContext.PeCoffImageContext, + &TempPrmModuleImageContext.ExportDirectory + ); + if (EFI_ERROR (Status)) { + continue; + } + + // Attempt to find the PRM Module Export Descriptor in the export ta= ble + Status =3D GetPrmModuleExportDescriptorTable ( + TempPrmModuleImageContext.ExportDirectory, + &TempPrmModuleImageContext.PeCoffImageContext, + &TempPrmModuleImageContext.ExportDescriptor + ); + if (EFI_ERROR (Status) || TempPrmModuleImageContext.ExportDescriptor= =3D=3D NULL) { + continue; + } + // A PRM Module Export Descriptor was successfully found, this is co= nsidered a PRM Module. + + // + // Create a new PRM Module image context node + // + PrmModuleImageContextListEntry =3D CreateNewPrmModuleImageContextLis= tEntry (); + if (PrmModuleImageContextListEntry =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem ( + &PrmModuleImageContextListEntry->Context, + &TempPrmModuleImageContext, + sizeof (PrmModuleImageContextListEntry->Context) + ); + InsertTailList (&mPrmModuleList, &PrmModuleImageContextListEntry->Li= nk); + PrmHandlerCount +=3D TempPrmModuleImageContext.ExportDescriptor->Hea= der.NumberPrmHandlers; + PrmModuleCount++; + DEBUG ((DEBUG_INFO, "%a %a: New PRM Module inserted into list to be = processed.\n", _DBGMSGID_, __FUNCTION__)); + } + + if (HandlerCount !=3D NULL) { + *HandlerCount =3D PrmHandlerCount; + } + if (ModuleCount !=3D NULL) { + *ModuleCount =3D PrmModuleCount; + } + + if (MmramRanges !=3D NULL) { + FreePool (MmramRanges); + } + + return EFI_SUCCESS; +} + +/** + The destructor function for this library instance. + + Frees global resources allocated by this library instance. + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The destructor always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +PrmModuleDiscoveryLibDestructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + LIST_ENTRY *Link; + LIST_ENTRY *NextLink; + PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *ListEntry; + + if (IsListEmpty (&mPrmModuleList)) { + return EFI_SUCCESS; + } + + Link =3D GetFirstNode (&mPrmModuleList); + while (!IsNull (&mPrmModuleList, Link)) { + ListEntry =3D CR (Link, PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY, Link, P= RM_MODULE_IMAGE_CONTEXT_LIST_ENTRY_SIGNATURE); + NextLink =3D GetNextNode (&mPrmModuleList, Link); + + RemoveEntryList (Link); + FreePool (ListEntry); + + Link =3D NextLink; + } + + return EFI_SUCCESS; +} + +/** + The constructor function for this library instance. + + Internally initializes data structures used later during library execu= tion. + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +PrmModuleDiscoveryLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + InitializeListHead (&mPrmModuleList); + + return EFI_SUCCESS; +} diff --git a/PrmPkg/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.c b/PrmPkg/Li= brary/DxePrmPeCoffLib/DxePrmPeCoffLib.c new file mode 100644 index 000000000000..d760d137dc2b --- /dev/null +++ b/PrmPkg/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.c @@ -0,0 +1,411 @@ +/** @file + + This file contains implementation for additional PE/COFF functionality= needed to use + Platform Runtime Mechanism (PRM) modules. + + Copyright (c) Microsoft Corporation + Copyright (c) 2020, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include + +#include +#include + +#define _DBGMSGID_ "[PRMPECOFFLIB]" + +/** + Gets a pointer to the export directory in a given PE/COFF image. + + @param[in] ImageExportDirectory A pointer to an export directo= ry 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 gi= ven. + + @retval EFI_SUCCESS The PRM Module Export Descript= or table was found successfully. + @retval EFI_INVALID_PARAMETER A required parameter is NULL. + @retval EFI_NOT_FOUND The PRM Module Export Descript= or 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 + ) +{ + UINTN Index; + EFI_PHYSICAL_ADDRESS CurrentImageAddress; + UINT16 PrmModuleExportDescriptorOrdin= al; + CONST CHAR8 *CurrentExportName; + UINT16 *OrdinalTable; + UINT32 *ExportNamePointerTable; + UINT32 *ExportAddressTable; + PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *TempExportDescriptor; + + DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); + + if (ImageExportDirectory =3D=3D NULL || + PeCoffLoaderImageContext =3D=3D NULL || + PeCoffLoaderImageContext->ImageAddress =3D=3D 0 || + ExportDescriptor =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + *ExportDescriptor =3D NULL; + + DEBUG (( + DEBUG_INFO, + " %a %a: %d exported names found in this image.\n", + _DBGMSGID_, + __FUNCTION__, + ImageExportDirectory->NumberOfNames + )); + + // + // The export name pointer table and export ordinal table form two par= allel arrays associated by index. + // + CurrentImageAddress =3D PeCoffLoaderImageContext->ImageAddress; + ExportAddressTable =3D (UINT32 *) ((UINTN) CurrentImageAddress + Image= ExportDirectory->AddressOfFunctions); + ExportNamePointerTable =3D (UINT32 *) ((UINTN) CurrentImageAddress + I= mageExportDirectory->AddressOfNames); + OrdinalTable =3D (UINT16 *) ((UINTN) CurrentImageAddress + ImageExport= Directory->AddressOfNameOrdinals); + + for (Index =3D 0; Index < ImageExportDirectory->NumberOfNames; Index++= ) { + CurrentExportName =3D (CONST CHAR8 *) ((UINTN) CurrentImageAddress += ExportNamePointerTable[Index]); + DEBUG (( + DEBUG_INFO, + " %a %a: Export Name[0x%x] - %a.\n", + _DBGMSGID_, + __FUNCTION__, + Index, + CurrentExportName + )); + if ( + AsciiStrnCmp ( + PRM_STRING(PRM_MODULE_EXPORT_DESCRIPTOR_NAME), + CurrentExportName, + AsciiStrLen (PRM_STRING(PRM_MODULE_EXPORT_DESCRIPTOR_NAME)) + ) =3D=3D 0) { + PrmModuleExportDescriptorOrdinal =3D OrdinalTable[Index]; + DEBUG (( + DEBUG_INFO, + " %a %a: PRM Module Export Descriptor found. Ordinal =3D %d.\n"= , + _DBGMSGID_, + __FUNCTION__, + PrmModuleExportDescriptorOrdinal + )); + if (PrmModuleExportDescriptorOrdinal >=3D ImageExportDirectory->Nu= mberOfFunctions) { + DEBUG ((DEBUG_ERROR, "%a %a: The PRM Module Export Descriptor or= dinal value is invalid.\n", _DBGMSGID_, __FUNCTION__)); + return EFI_NOT_FOUND; + } + TempExportDescriptor =3D (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *) (= (UINTN) CurrentImageAddress + ExportAddressTable[PrmModuleExportDescripto= rOrdinal]); + if (TempExportDescriptor->Header.Signature =3D=3D PRM_MODULE_EXPOR= T_DESCRIPTOR_SIGNATURE) { + *ExportDescriptor =3D TempExportDescriptor; + DEBUG ((DEBUG_INFO, " %a %a: PRM Module Export Descriptor found= at 0x%x.\n", _DBGMSGID_, __FUNCTION__, (UINTN) ExportDescriptor)); + } else { + DEBUG (( + DEBUG_INFO, + " %a %a: PRM Module Export Descriptor found at 0x%x but signa= ture check failed.\n", + _DBGMSGID_, + __FUNCTION__, + (UINTN) TempExportDescriptor + )); + } + DEBUG ((DEBUG_INFO, " %a %a: Exiting export iteration since expor= t descriptor found.\n", _DBGMSGID_, __FUNCTION__)); + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + +/** + 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 m= emory 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 cou= ld 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 + ) +{ + UINT16 Magic; + UINT32 NumberOfRvaAndSizes; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION OptionalHeaderPtrUnion; + EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry; + EFI_IMAGE_EXPORT_DIRECTORY *ExportDirectory; + + if (Image =3D=3D NULL || PeCoffLoaderImageContext =3D=3D NULL || Image= ExportDirectory =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + DirectoryEntry =3D NULL; + ExportDirectory =3D NULL; + + // + // NOTE: For backward compatibility, use the Machine field to identify= a PE32/PE32+ + // image instead of using the Magic field. Some systems might ge= nerate a PE32+ + // image with PE32 magic. + // + switch (PeCoffLoaderImageContext->Machine) { + case EFI_IMAGE_MACHINE_IA32: + // + // Assume PE32 image with IA32 Machine field. + // + Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC; + break; + case EFI_IMAGE_MACHINE_X64: + case EFI_IMAGE_MACHINE_AARCH64: + // + // Assume PE32+ image with X64 Machine field + // + Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC; + break; + default: + // + // For unknown Machine field, use Magic in optional header + // + DEBUG (( + DEBUG_WARN, + "%a %a: The machine type for this image is not valid for a PRM mod= ule.\n", + _DBGMSGID_, + __FUNCTION__ + )); + return EFI_UNSUPPORTED; + } + + OptionalHeaderPtrUnion.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *) ( + (UINTN) Image + + PeCoffLoaderImageContext->PeCoffHeader= Offset + ); + + // + // Check the PE/COFF Header Signature. Determine if the image is valid= and/or a TE image. + // + if (OptionalHeaderPtrUnion.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE= ) { + DEBUG ((DEBUG_ERROR, "%a %a: The PE signature is not valid for the c= urrent image.\n", _DBGMSGID_, __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + if (Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use the PE32 offset to get the Export Directory Entry + // + NumberOfRvaAndSizes =3D OptionalHeaderPtrUnion.Pe32->OptionalHeader.= NumberOfRvaAndSizes; + DirectoryEntry =3D (EFI_IMAGE_DATA_DIRECTORY *) &(OptionalHeaderPtrU= nion.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT]= ); + } else if (OptionalHeaderPtrUnion.Pe32->OptionalHeader.Magic =3D=3D EF= I_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { + // + // Use the PE32+ offset get the Export Directory Entry + // + NumberOfRvaAndSizes =3D OptionalHeaderPtrUnion.Pe32Plus->OptionalHea= der.NumberOfRvaAndSizes; + DirectoryEntry =3D (EFI_IMAGE_DATA_DIRECTORY *) &(OptionalHeaderPtrU= nion.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXP= ORT]); + } else { + return EFI_UNSUPPORTED; + } + + if (NumberOfRvaAndSizes <=3D EFI_IMAGE_DIRECTORY_ENTRY_EXPORT || Direc= toryEntry->VirtualAddress =3D=3D 0) { + // + // The export directory is not present + // + return EFI_NOT_FOUND; + } else if (((UINT32) (~0) - DirectoryEntry->VirtualAddress) < Director= yEntry->Size) { + // + // The directory address overflows + // + DEBUG ((DEBUG_ERROR, "%a %a: The export directory entry in this imag= e results in overflow.\n", _DBGMSGID_, __FUNCTION__)); + return EFI_UNSUPPORTED; + } else { + DEBUG ((DEBUG_INFO, "%a %a: Export Directory Entry found in the imag= e at 0x%x.\n", _DBGMSGID_, __FUNCTION__, (UINTN) OptionalHeaderPtrUnion.P= e32)); + DEBUG ((DEBUG_INFO, " %a %a: Directory Entry Virtual Address =3D 0x= %x.\n", _DBGMSGID_, __FUNCTION__, DirectoryEntry->VirtualAddress)); + + ExportDirectory =3D (EFI_IMAGE_EXPORT_DIRECTORY *) ((UINTN) Image + = DirectoryEntry->VirtualAddress); + DEBUG (( + DEBUG_INFO, + " %a %a: Export Directory Table found successfully at 0x%x. Name = address =3D 0x%x. Name =3D %a.\n", + _DBGMSGID_, + __FUNCTION__, + (UINTN) ExportDirectory, + ((UINTN) Image + ExportDirectory->Name), + (CHAR8 *) ((UINTN) Image + ExportDirectory->Name) + )); + } + *ImageExportDirectory =3D ExportDirectory; + + return EFI_SUCCESS; +} + +/** + Returns the image major and image minor version in a given PE/COFF ima= ge. + + @param[in] Image A pointer to a PE32/COFF image= base address that is loaded into memory + and already relocated to the m= emory 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 t= o hold the image major version. + @param[out] ImageMinorVersion A pointer to a UINT16 buffer t= o hold the image minor version. + + @retval EFI_SUCCESS The image version was read suc= cessfully. + @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 + ) +{ + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION OptionalHeaderPtrUnion; + UINT16 Magic; + + DEBUG ((DEBUG_INFO, " %a %a - Entry.\n", _DBGMSGID_, __FUNCTION__))= ; + + if (Image =3D=3D NULL || PeCoffLoaderImageContext =3D=3D NULL || Image= MajorVersion =3D=3D NULL || ImageMinorVersion =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // NOTE: For backward compatibility, use the Machine field to identify= a PE32/PE32+ + // image instead of using the Magic field. Some systems might ge= nerate a PE32+ + // image with PE32 magic. + // + switch (PeCoffLoaderImageContext->Machine) { + case EFI_IMAGE_MACHINE_IA32: + // + // Assume PE32 image + // + Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC; + break; + case EFI_IMAGE_MACHINE_X64: + case EFI_IMAGE_MACHINE_AARCH64: + // + // Assume PE32+ image + // + Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC; + break; + default: + // + // For unknown Machine field, use Magic in optional header + // + DEBUG (( + DEBUG_WARN, + "%a %a: The machine type for this image is not valid for a PRM mod= ule.\n", + _DBGMSGID_, + __FUNCTION__ + )); + return EFI_UNSUPPORTED; + } + + OptionalHeaderPtrUnion.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *) ( + (UINTN) Image + + PeCoffLoaderImageContext->PeCoffHeader= Offset + ); + // + // Check the PE/COFF Header Signature. Determine if the image is valid= and/or a TE image. + // + if (OptionalHeaderPtrUnion.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE= ) { + DEBUG ((DEBUG_ERROR, "%a %a: The PE signature is not valid for the c= urrent image.\n", _DBGMSGID_, __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + if (Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + // + // Use the PE32 offset to get the Export Directory Entry + // + *ImageMajorVersion =3D OptionalHeaderPtrUnion.Pe32->OptionalHeader.M= ajorImageVersion; + *ImageMinorVersion =3D OptionalHeaderPtrUnion.Pe32->OptionalHeader.M= inorImageVersion; + } else { + // + // Use the PE32+ offset to get the Export Directory Entry + // + *ImageMajorVersion =3D OptionalHeaderPtrUnion.Pe32Plus->OptionalHead= er.MajorImageVersion; + *ImageMinorVersion =3D OptionalHeaderPtrUnion.Pe32Plus->OptionalHead= er.MinorImageVersion; + } + + DEBUG ((DEBUG_INFO, " %a %a - Image Major Version: 0x%02x.\n", _D= BGMSGID_, __FUNCTION__, *ImageMajorVersion)); + DEBUG ((DEBUG_INFO, " %a %a - Image Minor Version: 0x%02x.\n", _D= BGMSGID_, __FUNCTION__, *ImageMinorVersion)); + + return EFI_SUCCESS; +} + +/** + Gets the address of an entry in an image export table by ASCII name. + + @param[in] ExportName A pointer to an ASCII name str= ing of the entry name. + @param[in] ImageBaseAddress The base address of the PE/COF= F image. + @param[in] ImageExportDirectory A pointer to the export direct= ory 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 suc= cessfully. + @retval EFI_INVALID_PARAMETER A required pointer argument is= NULL. + @retval EFI_NOT_FOUND An entry with the given Export= Name 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 + ) +{ + UINTN ExportNameIndex; + UINT16 CurrentExportOrdinal; + UINT32 *ExportAddressTable; + UINT32 *ExportNamePointerTable; + UINT16 *OrdinalTable; + CONST CHAR8 *ExportNameTablePointerName; + + if (ExportName =3D=3D NULL || ImageBaseAddress =3D=3D 0 || ImageExport= Directory =3D=3D NULL || ExportPhysicalAddress =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + *ExportPhysicalAddress =3D 0; + + ExportAddressTable =3D (UINT32 *) ((UINTN) ImageBaseAddress + ImageExp= ortDirectory->AddressOfFunctions); + ExportNamePointerTable =3D (UINT32 *) ((UINTN) ImageBaseAddress + Imag= eExportDirectory->AddressOfNames); + OrdinalTable =3D (UINT16 *) ((UINTN) ImageBaseAddress + ImageExportDir= ectory->AddressOfNameOrdinals); + + for (ExportNameIndex =3D 0; ExportNameIndex < ImageExportDirectory->Nu= mberOfNames; ExportNameIndex++) { + ExportNameTablePointerName =3D (CONST CHAR8 *) ((UINTN) ImageBaseAdd= ress + ExportNamePointerTable[ExportNameIndex]); + + if (AsciiStrnCmp (ExportName, ExportNameTablePointerName, PRM_HANDLE= R_NAME_MAXIMUM_LENGTH) =3D=3D 0) { + CurrentExportOrdinal =3D OrdinalTable[ExportNameIndex]; + + ASSERT (CurrentExportOrdinal < ImageExportDirectory->NumberOfFunct= ions); + if (CurrentExportOrdinal >=3D ImageExportDirectory->NumberOfFuncti= ons) { + DEBUG ((DEBUG_ERROR, " %a %a: The export ordinal value is inval= id.\n", _DBGMSGID_, __FUNCTION__)); + break; + } + + *ExportPhysicalAddress =3D (EFI_PHYSICAL_ADDRESS) ((UINTN) ImageBa= seAddress + ExportAddressTable[CurrentExportOrdinal]); + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} diff --git a/PrmPkg/PrmLoaderDxe/PrmLoaderDxe.c b/PrmPkg/PrmLoaderDxe/Prm= LoaderDxe.c index 6416e388a668..407c48257432 100644 --- a/PrmPkg/PrmLoaderDxe/PrmLoaderDxe.c +++ b/PrmPkg/PrmLoaderDxe/PrmLoaderDxe.c @@ -10,7 +10,6 @@ **/ =20 #include "PrmAcpiTable.h" -#include "PrmLoader.h" =20 #include #include @@ -18,581 +17,20 @@ #include #include #include +#include +#include #include #include #include -#include #include =20 #include #include =20 -LIST_ENTRY mPrmModuleList; +#define _DBGMSGID_ "[PRMLOADER]" =20 -// Todo: Potentially refactor mPrmHandlerCount and mPrmModuleCount into = localized structures -// in the future. -UINT32 mPrmHandlerCount; -UINT32 mPrmModuleCount; - -/** - Gets a pointer to the export directory in a given PE/COFF image. - - @param[in] ImageExportDirectory A pointer to an export directo= ry 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 gi= ven. - - @retval EFI_SUCCESS The PRM Module Export Descript= or table was found successfully. - @retval EFI_INVALID_PARAMETER A required parameter is NULL. - @retval EFI_NOT_FOUND The PRM Module Export Descript= or 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 - ) -{ - UINTN Index; - EFI_PHYSICAL_ADDRESS CurrentImageAddress; - UINT16 PrmModuleExportDescriptorOrdin= al; - CONST CHAR8 *CurrentExportName; - UINT16 *OrdinalTable; - UINT32 *ExportNamePointerTable; - UINT32 *ExportAddressTable; - PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *TempExportDescriptor; - - DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); - - *ExportDescriptor =3D NULL; - - if (ImageExportDirectory =3D=3D NULL || - PeCoffLoaderImageContext =3D=3D NULL || - PeCoffLoaderImageContext->ImageAddress =3D=3D 0 || - ExportDescriptor =3D=3D NULL) { - return EFI_INVALID_PARAMETER; - } - - DEBUG (( - DEBUG_INFO, - " %a %a: %d exported names found in this image.\n", - _DBGMSGID_, - __FUNCTION__, - ImageExportDirectory->NumberOfNames - )); - - // - // The export name pointer table and export ordinal table form two par= allel arrays associated by index. - // - CurrentImageAddress =3D PeCoffLoaderImageContext->ImageAddress; - ExportAddressTable =3D (UINT32 *) ((UINTN) CurrentImageAddress + Image= ExportDirectory->AddressOfFunctions); - ExportNamePointerTable =3D (UINT32 *) ((UINTN) CurrentImageAddress + I= mageExportDirectory->AddressOfNames); - OrdinalTable =3D (UINT16 *) ((UINTN) CurrentImageAddress + ImageExport= Directory->AddressOfNameOrdinals); - - for (Index =3D 0; Index < ImageExportDirectory->NumberOfNames; Index++= ) { - CurrentExportName =3D (CONST CHAR8 *) ((UINTN) CurrentImageAddress += ExportNamePointerTable[Index]); - DEBUG (( - DEBUG_INFO, - " %a %a: Export Name[0x%x] - %a.\n", - _DBGMSGID_, - __FUNCTION__, - Index, - CurrentExportName - )); - if ( - AsciiStrnCmp ( - PRM_STRING(PRM_MODULE_EXPORT_DESCRIPTOR_NAME), - CurrentExportName, - AsciiStrLen (PRM_STRING(PRM_MODULE_EXPORT_DESCRIPTOR_NAME)) - ) =3D=3D 0) { - PrmModuleExportDescriptorOrdinal =3D OrdinalTable[Index]; - DEBUG (( - DEBUG_INFO, - " %a %a: PRM Module Export Descriptor found. Ordinal =3D %d.\n"= , - _DBGMSGID_, - __FUNCTION__, - PrmModuleExportDescriptorOrdinal - )); - if (PrmModuleExportDescriptorOrdinal >=3D ImageExportDirectory->Nu= mberOfFunctions) { - DEBUG ((DEBUG_ERROR, "%a %a: The PRM Module Export Descriptor or= dinal value is invalid.\n", _DBGMSGID_, __FUNCTION__)); - return EFI_NOT_FOUND; - } - TempExportDescriptor =3D (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *) (= (UINTN) CurrentImageAddress + ExportAddressTable[PrmModuleExportDescripto= rOrdinal]); - if (TempExportDescriptor->Header.Signature =3D=3D PRM_MODULE_EXPOR= T_DESCRIPTOR_SIGNATURE) { - *ExportDescriptor =3D TempExportDescriptor; - DEBUG ((DEBUG_INFO, " %a %a: PRM Module Export Descriptor found= at 0x%x.\n", _DBGMSGID_, __FUNCTION__, (UINTN) ExportDescriptor)); - } else { - DEBUG (( - DEBUG_INFO, - " %a %a: PRM Module Export Descriptor found at 0x%x but signa= ture check failed.\n", - _DBGMSGID_, - __FUNCTION__, - (UINTN) TempExportDescriptor - )); - } - DEBUG ((DEBUG_INFO, " %a %a: Exiting export iteration since expor= t descriptor found.\n", _DBGMSGID_, __FUNCTION__)); - return EFI_SUCCESS; - } - } - - return EFI_NOT_FOUND; -} - -/** - 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 m= emory 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 cou= ld 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 - ) -{ - UINT16 Magic; - UINT32 NumberOfRvaAndSizes; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION OptionalHeaderPtrUnion; - EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry; - EFI_IMAGE_EXPORT_DIRECTORY *ExportDirectory; - EFI_IMAGE_SECTION_HEADER *SectionHeader; - - if (Image =3D=3D NULL || PeCoffLoaderImageContext =3D=3D NULL || Image= ExportDirectory =3D=3D NULL) { - return EFI_INVALID_PARAMETER; - } - - DirectoryEntry =3D NULL; - ExportDirectory =3D NULL; - - // - // NOTE: For backward compatibility, use the Machine field to identify= a PE32/PE32+ - // image instead of using the Magic field. Some systems might ge= nerate a PE32+ - // image with PE32 magic. - // - switch (PeCoffLoaderImageContext->Machine) { - case EFI_IMAGE_MACHINE_IA32: - // Todo: Add EFI_IMAGE_MACHINE_ARMT - // - // Assume PE32 image with IA32 Machine field. - // - Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC; - break; - case EFI_IMAGE_MACHINE_X64: - // - // Assume PE32+ image with X64 Machine field - // - Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC; - break; - default: - // - // For unknown Machine field, use Magic in optional header - // - DEBUG (( - DEBUG_WARN, - "%a %a: The machine type for this image is not valid for a PRM mod= ule.\n", - _DBGMSGID_, - __FUNCTION__ - )); - return EFI_UNSUPPORTED; - } - - OptionalHeaderPtrUnion.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *) ( - (UINTN) Image + - PeCoffLoaderImageContext->PeCoffHeader= Offset - ); - - // - // Check the PE/COFF Header Signature. Determine if the image is valid= and/or a TE image. - // - if (OptionalHeaderPtrUnion.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE= ) { - DEBUG ((DEBUG_ERROR, "%a %a: The PE signature is not valid for the c= urrent image.\n", _DBGMSGID_, __FUNCTION__)); - return EFI_UNSUPPORTED; - } - - SectionHeader =3D (EFI_IMAGE_SECTION_HEADER *) ( - (UINTN) Image + - PeCoffLoaderImageContext->PeCoffHeaderOffset + - sizeof (UINT32) + - sizeof (EFI_IMAGE_FILE_HEADER) + - PeCoffLoaderImageContext->SizeOfHeaders - ); - if (Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { - // - // Use the PE32 offset to get the Export Directory Entry - // - NumberOfRvaAndSizes =3D OptionalHeaderPtrUnion.Pe32->OptionalHeader.= NumberOfRvaAndSizes; - DirectoryEntry =3D (EFI_IMAGE_DATA_DIRECTORY *) &(OptionalHeaderPtrU= nion.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT]= ); - } else if (OptionalHeaderPtrUnion.Pe32->OptionalHeader.Magic =3D=3D EF= I_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { - // - // Use the PE32+ offset get the Export Directory Entry - // - NumberOfRvaAndSizes =3D OptionalHeaderPtrUnion.Pe32Plus->OptionalHea= der.NumberOfRvaAndSizes; - DirectoryEntry =3D (EFI_IMAGE_DATA_DIRECTORY *) &(OptionalHeaderPtrU= nion.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXP= ORT]); - } else { - return EFI_UNSUPPORTED; - } - - if (NumberOfRvaAndSizes <=3D EFI_IMAGE_DIRECTORY_ENTRY_EXPORT || Direc= toryEntry->VirtualAddress =3D=3D 0) { - // - // The export directory is not present - // - return EFI_NOT_FOUND; - } else if (((UINT32) (~0) - DirectoryEntry->VirtualAddress) < Director= yEntry->Size) { - // - // The directory address overflows - // - DEBUG ((DEBUG_ERROR, "%a %a: The export directory entry in this imag= e results in overflow.\n", _DBGMSGID_, __FUNCTION__)); - return EFI_UNSUPPORTED; - } else { - DEBUG ((DEBUG_INFO, "%a %a: Export Directory Entry found in the imag= e at 0x%x.\n", _DBGMSGID_, __FUNCTION__, (UINTN) OptionalHeaderPtrUnion.P= e32)); - DEBUG ((DEBUG_INFO, " %a %a: Directory Entry Virtual Address =3D 0x= %x.\n", _DBGMSGID_, __FUNCTION__, DirectoryEntry->VirtualAddress)); - - ExportDirectory =3D (EFI_IMAGE_EXPORT_DIRECTORY *) ((UINTN) Image + = DirectoryEntry->VirtualAddress); - DEBUG (( - DEBUG_INFO, - " %a %a: Export Directory Table found successfully at 0x%x. Name = address =3D 0x%x. Name =3D %a.\n", - _DBGMSGID_, - __FUNCTION__, - (UINTN) ExportDirectory, - ((UINTN) Image + ExportDirectory->Name), - (CHAR8 *) ((UINTN) Image + ExportDirectory->Name) - )); - } - *ImageExportDirectory =3D ExportDirectory; - - return EFI_SUCCESS; -} - -/** - Returns the image major and image minor version in a given PE/COFF ima= ge. - - @param[in] Image A pointer to a PE32/COFF image= base address that is loaded into memory - and already relocated to the m= emory 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 t= o hold the image major version. - @param[out] ImageMinorVersion A pointer to a UINT16 buffer t= o hold the image minor version. - - @retval EFI_SUCCESS The image version was read suc= cessfully. - @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 - ) -{ - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION OptionalHeaderPtrUnion; - UINT16 Magic; - - DEBUG ((DEBUG_INFO, " %a %a - Entry.\n", _DBGMSGID_, __FUNCTION__))= ; - - if (Image =3D=3D NULL || PeCoffLoaderImageContext =3D=3D NULL || Image= MajorVersion =3D=3D NULL || ImageMinorVersion =3D=3D NULL) { - return EFI_INVALID_PARAMETER; - } - - // - // NOTE: For backward compatibility, use the Machine field to identify= a PE32/PE32+ - // image instead of using the Magic field. Some systems might ge= nerate a PE32+ - // image with PE32 magic. - // - switch (PeCoffLoaderImageContext->Machine) { - case EFI_IMAGE_MACHINE_IA32: - // Todo: Add EFI_IMAGE_MACHINE_ARMT - // - // Assume PE32 image with IA32 Machine field. - // - Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC; - break; - case EFI_IMAGE_MACHINE_X64: - // - // Assume PE32+ image with X64 Machine field - // - Magic =3D EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC; - break; - default: - // - // For unknown Machine field, use Magic in optional header - // - DEBUG (( - DEBUG_WARN, - "%a %a: The machine type for this image is not valid for a PRM mod= ule.\n", - _DBGMSGID_, - __FUNCTION__ - )); - return EFI_UNSUPPORTED; - } - - OptionalHeaderPtrUnion.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *) ( - (UINTN) Image + - PeCoffLoaderImageContext->PeCoffHeader= Offset - ); - // - // Check the PE/COFF Header Signature. Determine if the image is valid= and/or a TE image. - // - if (OptionalHeaderPtrUnion.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE= ) { - DEBUG ((DEBUG_ERROR, "%a %a: The PE signature is not valid for the c= urrent image.\n", _DBGMSGID_, __FUNCTION__)); - return EFI_UNSUPPORTED; - } - - if (Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { - // - // Use the PE32 offset to get the Export Directory Entry - // - *ImageMajorVersion =3D OptionalHeaderPtrUnion.Pe32->OptionalHeader.M= ajorImageVersion; - *ImageMinorVersion =3D OptionalHeaderPtrUnion.Pe32->OptionalHeader.M= inorImageVersion; - } else { - // - // Use the PE32+ offset to get the Export Directory Entry - // - *ImageMajorVersion =3D OptionalHeaderPtrUnion.Pe32Plus->OptionalHead= er.MajorImageVersion; - *ImageMinorVersion =3D OptionalHeaderPtrUnion.Pe32Plus->OptionalHead= er.MinorImageVersion; - } - - DEBUG ((DEBUG_INFO, " %a %a - Image Major Version: 0x%02x.\n", _D= BGMSGID_, __FUNCTION__, *ImageMajorVersion)); - DEBUG ((DEBUG_INFO, " %a %a - Image Minor Version: 0x%02x.\n", _D= BGMSGID_, __FUNCTION__, *ImageMinorVersion)); - - return EFI_SUCCESS; -} - -/** - Creates a new PRM Module Image Context linked list entry. - - @retval PrmModuleImageContextListEntry If successful, a pointer a PRM= Module Image Context linked list entry - otherwise, NULL is returned. - -**/ -STATIC -PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY * -CreateNewPrmModuleImageContextListEntry ( - VOID - ) -{ - PRM_MODULE_IMAGE_CONTEXT *PrmModuleImageContext; - PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *PrmModuleImageContextListEntr= y; - - DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); - - PrmModuleImageContext =3D AllocateZeroPool (sizeof (*PrmModuleImageCon= text)); - if (PrmModuleImageContext =3D=3D NULL) { - return NULL; - } - DEBUG (( - DEBUG_INFO, - " %a %a: Allocated PrmModuleImageContext at 0x%x of size 0x%x bytes= .\n", - _DBGMSGID_, - __FUNCTION__, - (UINTN) PrmModuleImageContext, - sizeof (*PrmModuleImageContext) - )); - - PrmModuleImageContextListEntry =3D AllocateZeroPool (sizeof (*PrmModul= eImageContextListEntry)); - if (PrmModuleImageContextListEntry =3D=3D NULL) { - FreePool (PrmModuleImageContext); - return NULL; - } - DEBUG (( - DEBUG_INFO, - " %a %a: Allocated PrmModuleImageContextListEntry at 0x%x of size 0= x%x bytes.\n", - _DBGMSGID_, - __FUNCTION__, - (UINTN) PrmModuleImageContextListEntry, - sizeof (*PrmModuleImageContextListEntry) - )); - - PrmModuleImageContextListEntry->Signature =3D PRM_MODULE_IMAGE_CONTEXT= _LIST_ENTRY_SIGNATURE; - PrmModuleImageContextListEntry->Context =3D PrmModuleImageContext; - - return PrmModuleImageContextListEntry; -} - -/** - Discovers all PRM Modules loaded during the DXE boot phase. - - Each PRM Module discovered is placed into a linked list so the list ca= n br processsed in the future. - - @retval EFI_SUCCESS All PRM Modules were discovere= d successfully. - @retval EFI_NOT_FOUND The gEfiLoadedImageProtocolGui= d protocol could not be found. - @retval EFI_OUT_OF_RESOURCES Insufficient memory resources = to allocate the new PRM Context - linked list nodes. - -**/ -EFI_STATUS -DiscoverPrmModules ( - VOID - ) -{ - EFI_STATUS Status; - PRM_MODULE_IMAGE_CONTEXT TempPrmModuleImageContext; - PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *PrmModuleImageContextListEntr= y; - EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProtocol; - EFI_HANDLE *HandleBuffer; - UINTN HandleCount; - UINTN Index; - - DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); - - Status =3D gBS->LocateHandleBuffer ( - ByProtocol, - &gEfiLoadedImageProtocolGuid, - NULL, - &HandleCount, - &HandleBuffer - ); - if (EFI_ERROR (Status) && (HandleCount =3D=3D 0)) { - DEBUG ((DEBUG_ERROR, "%a %a: No LoadedImageProtocol instances found!= \n", _DBGMSGID_, __FUNCTION__)); - return EFI_NOT_FOUND; - } - - for (Index =3D 0; Index < HandleCount; Index++) { - Status =3D gBS->HandleProtocol ( - HandleBuffer[Index], - &gEfiLoadedImageProtocolGuid, - (VOID **) &LoadedImageProtocol - ); - if (EFI_ERROR (Status)) { - continue; - } - - ZeroMem (&TempPrmModuleImageContext, sizeof (TempPrmModuleImageConte= xt)); - TempPrmModuleImageContext.PeCoffImageContext.Handle =3D LoadedIma= geProtocol->ImageBase; - TempPrmModuleImageContext.PeCoffImageContext.ImageRead =3D PeCoffLoa= derImageReadFromMemory; - - Status =3D PeCoffLoaderGetImageInfo (&TempPrmModuleImageContext.PeCo= ffImageContext); - if (EFI_ERROR (Status) || TempPrmModuleImageContext.PeCoffImageConte= xt.ImageError !=3D IMAGE_ERROR_SUCCESS) { - DEBUG (( - DEBUG_WARN, - "%a %a: ImageHandle 0x%016lx is not a valid PE/COFF image. It ca= nnot be considered a PRM module.\n", - _DBGMSGID_, - __FUNCTION__, - (EFI_PHYSICAL_ADDRESS) (UINTN) LoadedImageProtocol->ImageBase - )); - continue; - } - if (TempPrmModuleImageContext.PeCoffImageContext.IsTeImage) { - // A PRM Module is not allowed to be a TE image - continue; - } - - // Attempt to find an export table in this image - Status =3D GetExportDirectoryInPeCoffImage ( - LoadedImageProtocol->ImageBase, - &TempPrmModuleImageContext.PeCoffImageContext, - &TempPrmModuleImageContext.ExportDirectory - ); - if (EFI_ERROR (Status)) { - continue; - } - - // Attempt to find the PRM Module Export Descriptor in the export ta= ble - Status =3D GetPrmModuleExportDescriptorTable ( - TempPrmModuleImageContext.ExportDirectory, - &TempPrmModuleImageContext.PeCoffImageContext, - &TempPrmModuleImageContext.ExportDescriptor - ); - if (EFI_ERROR (Status)) { - continue; - } - // A PRM Module Export Descriptor was successfully found, this is co= nsidered a PRM Module. - - // - // Create a new PRM Module image context node - // - PrmModuleImageContextListEntry =3D CreateNewPrmModuleImageContextLis= tEntry (); - if (PrmModuleImageContextListEntry =3D=3D NULL) { - return EFI_OUT_OF_RESOURCES; - } - CopyMem ( - PrmModuleImageContextListEntry->Context, - &TempPrmModuleImageContext, - sizeof (*(PrmModuleImageContextListEntry->Context)) - ); - InsertTailList (&mPrmModuleList, &PrmModuleImageContextListEntry->Li= nk); - mPrmHandlerCount +=3D TempPrmModuleImageContext.ExportDescriptor->He= ader.NumberPrmHandlers; - mPrmModuleCount++; // Todo: Match with global variable refactor chan= ge in the future - DEBUG ((DEBUG_INFO, "%a %a: New PRM Module inserted into list to be = processed.\n", _DBGMSGID_, __FUNCTION__)); - } - - return EFI_SUCCESS; -} - -/** - Gets the address of an entry in an image export table by ASCII name. - - @param[in] ExportName A pointer to an ASCII name str= ing of the entry name. - @param[in] ImageBaseAddress The base address of the PE/COF= F image. - @param[in] ImageExportDirectory A pointer to the export direct= ory 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 suc= cessfully. - @retval EFI_INVALID_PARAMETER A required pointer argument is= NULL. - @retval EFI_NOT_FOUND An entry with the given Export= Name 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 - ) -{ - UINTN ExportNameIndex; - UINT16 CurrentExportOrdinal; - UINT32 *ExportAddressTable; - UINT32 *ExportNamePointerTable; - UINT16 *OrdinalTable; - CONST CHAR8 *ExportNameTablePointerName; - - if (ExportName =3D=3D NULL || ImageBaseAddress =3D=3D 0 || ImageExport= Directory =3D=3D NULL || ExportPhysicalAddress =3D=3D NULL) { - return EFI_INVALID_PARAMETER; - } - *ExportPhysicalAddress =3D 0; - - ExportAddressTable =3D (UINT32 *) ((UINTN) ImageBaseAddress + ImageExp= ortDirectory->AddressOfFunctions); - ExportNamePointerTable =3D (UINT32 *) ((UINTN) ImageBaseAddress + Imag= eExportDirectory->AddressOfNames); - OrdinalTable =3D (UINT16 *) ((UINTN) ImageBaseAddress + ImageExportDir= ectory->AddressOfNameOrdinals); - - for (ExportNameIndex =3D 0; ExportNameIndex < ImageExportDirectory->Nu= mberOfNames; ExportNameIndex++) { - ExportNameTablePointerName =3D (CONST CHAR8 *) ((UINTN) ImageBaseAdd= ress + ExportNamePointerTable[ExportNameIndex]); - - if (AsciiStrnCmp (ExportName, ExportNameTablePointerName, PRM_HANDLE= R_NAME_MAXIMUM_LENGTH) =3D=3D 0) { - CurrentExportOrdinal =3D OrdinalTable[ExportNameIndex]; - - ASSERT (CurrentExportOrdinal < ImageExportDirectory->NumberOfFunct= ions); - if (CurrentExportOrdinal >=3D ImageExportDirectory->NumberOfFuncti= ons) { - DEBUG ((DEBUG_ERROR, " %a %a: The export ordinal value is inval= id.\n", _DBGMSGID_, __FUNCTION__)); - break; - } - - *ExportPhysicalAddress =3D (EFI_PHYSICAL_ADDRESS) ((UINTN) ImageBa= seAddress + ExportAddressTable[CurrentExportOrdinal]); - return EFI_SUCCESS; - } - } - - return EFI_NOT_FOUND; -} +UINTN mPrmHandlerCount; +UINTN mPrmModuleCount; =20 /** Processes a list of PRM context entries to build a PRM ACPI table. @@ -616,9 +54,8 @@ ProcessPrmModules ( { EFI_IMAGE_EXPORT_DIRECTORY *CurrentImageExportDirectory; PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *CurrentExportDescriptorStruct= ; - LIST_ENTRY *Link; PRM_ACPI_DESCRIPTION_TABLE *PrmAcpiTable; - PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY *TempListEntry; + PRM_MODULE_IMAGE_CONTEXT *CurrentPrmModuleImageContext; CONST CHAR8 *CurrentExportDescriptorHandle= rName; =20 ACPI_PARAMETER_BUFFER_DESCRIPTOR *CurrentModuleAcpiParamDescrip= tors; @@ -640,13 +77,12 @@ ProcessPrmModules ( if (PrmAcpiDescriptionTable =3D=3D NULL) { return EFI_INVALID_PARAMETER; } - Link =3D NULL; *PrmAcpiDescriptionTable =3D NULL; =20 DEBUG ((DEBUG_INFO, " %a %a: %d total PRM modules to process.\n", _DB= GMSGID_, __FUNCTION__, mPrmModuleCount)); DEBUG ((DEBUG_INFO, " %a %a: %d total PRM handlers to process.\n", _D= BGMSGID_, __FUNCTION__, mPrmHandlerCount)); =20 - PrmAcpiDescriptionTableBufferSize =3D (OFFSET_OF (PRM_ACPI_DESCRIPTION= _TABLE, PrmModuleInfoStructure) + + PrmAcpiDescriptionTableBufferSize =3D (UINT32) (OFFSET_OF (PRM_ACPI_DE= SCRIPTION_TABLE, PrmModuleInfoStructure) + (OFFSET_OF (PRM_MODULE_INFORMATI= ON_STRUCT, HandlerInfoStructure) * mPrmModuleCount) + (sizeof (PRM_HANDLER_INFORMATION= _STRUCT) * mPrmHandlerCount) ); @@ -667,18 +103,20 @@ ProcessPrmModules ( PrmAcpiTable->Header.CreatorId =3D PcdGet32 (PcdAcpiDefaultCrea= torId); PrmAcpiTable->Header.CreatorRevision =3D PcdGet32 (PcdAcpiDefaultCrea= torRevision); PrmAcpiTable->PrmModuleInfoOffset =3D OFFSET_OF (PRM_ACPI_DESCRIPT= ION_TABLE, PrmModuleInfoStructure); - PrmAcpiTable->PrmModuleInfoCount =3D mPrmModuleCount; + PrmAcpiTable->PrmModuleInfoCount =3D (UINT32) mPrmModuleCount; =20 // // Iterate across all PRM Modules on the list // CurrentModuleInfoStruct =3D &PrmAcpiTable->PrmModuleInfoStructure[0]; - EFI_LIST_FOR_EACH(Link, &mPrmModuleList) - { - TempListEntry =3D CR(Link, PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY, Link= , PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY_SIGNATURE); - CurrentImageAddress =3D TempListEntry->Context->PeCoffImageContext.I= mageAddress; - CurrentImageExportDirectory =3D TempListEntry->Context->ExportDirect= ory; - CurrentExportDescriptorStruct =3D TempListEntry->Context->ExportDesc= riptor; + for ( + CurrentPrmModuleImageContext =3D NULL, Status =3D GetNextPrmModuleEn= try (&CurrentPrmModuleImageContext); + !EFI_ERROR (Status); + Status =3D GetNextPrmModuleEntry (&CurrentPrmModuleImageContext)) { + + CurrentImageAddress =3D CurrentPrmModuleImageContext->PeCoffImageCon= text.ImageAddress; + CurrentImageExportDirectory =3D CurrentPrmModuleImageContext->Export= Directory; + CurrentExportDescriptorStruct =3D CurrentPrmModuleImageContext->Expo= rtDescriptor; CurrentModuleAcpiParamDescriptors =3D NULL; =20 DEBUG (( @@ -703,7 +141,7 @@ ProcessPrmModules ( CurrentModuleInfoStruct->MinorRevision =3D 0; Status =3D GetImageVersionInPeCoffImage ( (VOID *) (UINTN) CurrentImageAddress, - &TempListEntry->Context->PeCoffImageContext, + &CurrentPrmModuleImageContext->PeCoffImageContext, &CurrentModuleInfoStruct->MajorRevision, &CurrentModuleInfoStruct->MinorRevision ); @@ -863,9 +301,7 @@ PrmLoaderEndOfDxeNotification ( =20 DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__)); =20 - InitializeListHead (&mPrmModuleList); - - Status =3D DiscoverPrmModules (); + Status =3D DiscoverPrmModules (&mPrmModuleCount, &mPrmHandlerCount); ASSERT_EFI_ERROR (Status); =20 Status =3D ProcessPrmModules (&PrmAcpiDescriptionTable); diff --git a/PrmPkg/Include/Library/PrmModuleDiscoveryLib.h b/PrmPkg/Incl= ude/Library/PrmModuleDiscoveryLib.h new file mode 100644 index 000000000000..fe3a42586afa --- /dev/null +++ b/PrmPkg/Include/Library/PrmModuleDiscoveryLib.h @@ -0,0 +1,60 @@ +/** @file + + The PRM Module Discovery library provides functionality to discover PR= M 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 +#include +#include +#include + +/** + Gets the next PRM module discovered after the given PRM module. + + @param[in,out] ModuleImageContext A pointer to a pointer to a PR= M module image context structure. + + @retval EFI_SUCCESS The next PRM module was found = successfully. + @retval EFI_INVALID_PARAMETER The given ModuleImageContext s= tructure is invalid or the pointer is NULL. + @retval EFI_NOT_FOUND The next PRM module was not fo= und. + +**/ +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 ca= n 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 discovere= d successfully. + @retval EFI_INVALID_PARAMETER An actual pointer parameter wa= s passed as NULL. + @retval EFI_NOT_FOUND The gEfiLoadedImageProtocolGui= d 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 previo= usly and already discovered the PRM modules + loaded on this boot. + +**/ +EFI_STATUS +EFIAPI +DiscoverPrmModules ( + OUT UINTN *ModuleCount OPTIONAL, + OUT UINTN *HandlerCount OPTIONAL + ); + +#endif diff --git a/PrmPkg/Include/Library/PrmPeCoffLib.h b/PrmPkg/Include/Libra= ry/PrmPeCoffLib.h new file mode 100644 index 000000000000..4698a9477699 --- /dev/null +++ b/PrmPkg/Include/Library/PrmPeCoffLib.h @@ -0,0 +1,111 @@ +/** @file + + The PRM PE/COFF library provides functionality to support additional P= E/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 +#include +#include +#include + +/** + Gets a pointer to the export directory in a given PE/COFF image. + + @param[in] ImageExportDirectory A pointer to an export directo= ry 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 gi= ven. + + @retval EFI_SUCCESS The PRM Module Export Descript= or table was found successfully. + @retval EFI_INVALID_PARAMETER A required parameter is NULL. + @retval EFI_NOT_FOUND The PRM Module Export Descript= or 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 m= emory 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 cou= ld 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 ima= ge. + + @param[in] Image A pointer to a PE32/COFF image= base address that is loaded into memory + and already relocated to the m= emory 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 t= o hold the image major version. + @param[out] ImageMinorVersion A pointer to a UINT16 buffer t= o hold the image minor version. + + @retval EFI_SUCCESS The image version was read suc= cessfully. + @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 str= ing of the entry name. + @param[in] ImageBaseAddress The base address of the PE/COF= F image. + @param[in] ImageExportDirectory A pointer to the export direct= ory 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 suc= cessfully. + @retval EFI_INVALID_PARAMETER A required pointer argument is= NULL. + @retval EFI_NOT_FOUND An entry with the given Export= Name 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 diff --git a/PrmPkg/Include/PrmModuleImageContext.h b/PrmPkg/Include/PrmM= oduleImageContext.h new file mode 100644 index 000000000000..10146a272b6f --- /dev/null +++ b/PrmPkg/Include/PrmModuleImageContext.h @@ -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 +#include + +#include + +#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 diff --git a/PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscover= yLib.inf b/PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscoveryL= ib.inf new file mode 100644 index 000000000000..1115df3a3446 --- /dev/null +++ b/PrmPkg/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDiscoveryLib.in= f @@ -0,0 +1,41 @@ +## @file +# PRM Module Discovery Library +# +# Provides functionality to discover PRM modules loaded in the system b= oot. +# +# Copyright (c) Microsoft Corporation +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxePrmModuleDiscoveryLib + FILE_GUID =3D 95D3893F-4CBA-4C20-92C1-D24BFE3CE7B9 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D PrmModuleDiscoveryLib|DXE_DRIVER UEFI_DRIVER U= EFI_APPLICATION + CONSTRUCTOR =3D PrmModuleDiscoveryLibConstructor + DESTRUCTOR =3D PrmModuleDiscoveryLibDestructor + +[Sources] + PrmModuleDiscovery.h + DxePrmModuleDiscoveryLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + PrmPkg/PrmPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + PrmPeCoffLib + UefiBootServicesTableLib + +[Protocols] + gEfiLoadedImageProtocolGuid + gEfiMmAccessProtocolGuid diff --git a/PrmPkg/Library/DxePrmModuleDiscoveryLib/PrmModuleDiscovery.h= b/PrmPkg/Library/DxePrmModuleDiscoveryLib/PrmModuleDiscovery.h new file mode 100644 index 000000000000..79058d15317e --- /dev/null +++ b/PrmPkg/Library/DxePrmModuleDiscoveryLib/PrmModuleDiscovery.h @@ -0,0 +1,27 @@ +/** @file + + Definitions internally used for Platform Runtime Mechanism (PRM) modul= e discovery. + + Copyright (c) Microsoft Corporation + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef PRM_MODULE_DISCOVERY_H_ +#define PRM_MODULE_DISCOVERY_H_ + +#include + +#define PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY_SIGNATURE SIGNATURE_32('P','= R','M','E') + +#pragma pack(push, 1) + +typedef struct { + UINTN Signature; + LIST_ENTRY Link; + PRM_MODULE_IMAGE_CONTEXT Context; +} PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY; + +#pragma pack(pop) + +#endif diff --git a/PrmPkg/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.inf b/PrmPkg/= Library/DxePrmPeCoffLib/DxePrmPeCoffLib.inf new file mode 100644 index 000000000000..f139d5380b98 --- /dev/null +++ b/PrmPkg/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.inf @@ -0,0 +1,32 @@ +## @file +# PRM PE/COFF Library +# +# Provides functionality to support additional PE/COFF functionality ne= eded to use Platform Runtime Mechanism (PRM) +# modules. +# +# Copyright (c) Microsoft Corporation +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxePrmPeCoffLib + FILE_GUID =3D 0B9AEEAC-D79A-46A5-A784-84BDBC6291B5 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D PrmPeCoffLib|DXE_DRIVER UEFI_DRIVER UEFI_APPLI= CATION + +[Sources] + DxePrmPeCoffLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + PrmPkg/PrmPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + PeCoffLib diff --git a/PrmPkg/PrmLoaderDxe/PrmLoader.h b/PrmPkg/PrmLoaderDxe/PrmLoa= der.h deleted file mode 100644 index 1356c7a0c923..000000000000 --- a/PrmPkg/PrmLoaderDxe/PrmLoader.h +++ /dev/null @@ -1,51 +0,0 @@ -/** @file - - Definitions specific to the Platform Runtime Mechanism (PRM) loader.x - - Copyright (c) Microsoft Corporation - SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef PRM_LOADER_H_ -#define PRM_LOADER_H_ - -#include -#include - -#include - -#define _DBGMSGID_ "[PRMLOADER]" -#define PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY_SIGNATURE SIGNATURE_32('P','= R','M','E') - -#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; - -typedef struct { - UINTN Signature; - LIST_ENTRY Link; - PRM_MODULE_IMAGE_CONTEXT *Context; -} PRM_MODULE_IMAGE_CONTEXT_LIST_ENTRY; - -#pragma pack(pop) - -// -// Iterate through the double linked list. NOT delete safe. -// -#define EFI_LIST_FOR_EACH(Entry, ListHead) \ - for(Entry =3D (ListHead)->ForwardLink; Entry !=3D (ListHead); Entry =3D= Entry->ForwardLink) - -// -// Iterate through the double linked list. This is delete-safe. -// Don't touch NextEntry. -// -#define EFI_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \ - for(Entry =3D (ListHead)->ForwardLink, NextEntry =3D Entry->ForwardLin= k;\ - Entry !=3D (ListHead); Entry =3D NextEntry, NextEntry =3D Entry->F= orwardLin - -#endif diff --git a/PrmPkg/PrmLoaderDxe/PrmLoaderDxe.inf b/PrmPkg/PrmLoaderDxe/P= rmLoaderDxe.inf index 4d959ccd35a3..554d49685e2a 100644 --- a/PrmPkg/PrmLoaderDxe/PrmLoaderDxe.inf +++ b/PrmPkg/PrmLoaderDxe/PrmLoaderDxe.inf @@ -22,7 +22,6 @@ [Defines] =20 [Sources] PrmAcpiTable.h - PrmLoader.h PrmLoaderDxe.c =20 [Packages] @@ -39,8 +38,9 @@ [LibraryClasses] DebugLib MemoryAllocationLib PcdLib - PeCoffLib PrmContextBufferLib + PrmModuleDiscoveryLib + PrmPeCoffLib UefiBootServicesTableLib UefiDriverEntryPoint UefiLib @@ -54,7 +54,6 @@ [Pcd] =20 [Protocols] gEfiAcpiTableProtocolGuid - gEfiLoadedImageProtocolGuid gPrmConfigProtocolGuid =20 [Depex] diff --git a/PrmPkg/PrmPkg.dec b/PrmPkg/PrmPkg.dec index ff681d423d30..785e2c24c2f9 100644 --- a/PrmPkg/PrmPkg.dec +++ b/PrmPkg/PrmPkg.dec @@ -36,6 +36,14 @@ [LibraryClasses] # PrmContextBufferLib|Include/Library/PrmContextBufferLib.h =20 + ## @libraryclass Provides functionality to discover PRM modules instal= led by platform firmware + # + PrmModuleDiscoveryLib|Include/Library/PrmModuleDiscoveryLib.h + + ## @libraryclass Provides additional PE/COFF functionality needed to s= upport the Platform Runtime Mechanism (PRM) loader driver. + # + PrmPeCoffLib|Include/Library/PrmPeCoffLib.h + [Protocols] ## PRM Configuration Protocol # diff --git a/PrmPkg/PrmPkg.dsc b/PrmPkg/PrmPkg.dsc index 19b996eb3a02..e876f2053a8e 100644 --- a/PrmPkg/PrmPkg.dsc +++ b/PrmPkg/PrmPkg.dsc @@ -55,6 +55,9 @@ [LibraryClasses.common.DXE_DRIVER, LibraryClasses.commo= n.DXE_RUNTIME_DRIVER] # PRM Package # PrmContextBufferLib|$(PLATFORM_PACKAGE)/Library/DxePrmContextBufferLib= /DxePrmContextBufferLib.inf + PrmModuleDiscoveryLib|$(PLATFORM_PACKAGE)/Library/DxePrmModuleDiscover= yLib/DxePrmModuleDiscoveryLib.inf + PrmPeCoffLib|$(PLATFORM_PACKAGE)/Library/DxePrmPeCoffLib/DxePrmPeCoffL= ib.inf + =20 ########################################################################= ########################### # @@ -72,6 +75,16 @@ [Components] $(PLATFORM_PACKAGE)/Samples/PrmSampleContextBufferModule/Library/DxeCo= ntextBufferModuleConfigLib/DxeContextBufferModuleConfigLib.inf $(PLATFORM_PACKAGE)/Samples/PrmSampleHardwareAccessModule/Library/DxeH= ardwareAccessModuleConfigLib/DxeHardwareAccessModuleConfigLib.inf =20 + # + # PRM Module Discovery Library + # + $(PLATFORM_PACKAGE)/Library/DxePrmModuleDiscoveryLib/DxePrmModuleDisco= veryLib.inf + + # + # PRM PE/COFF Library + # + $(PLATFORM_PACKAGE)/Library/DxePrmPeCoffLib/DxePrmPeCoffLib.inf + # # PRM Configuration Driver # --=20 2.28.0.windows.1