From 6e560071844dd07d2e1928c3c102aada6dae59f6 Mon Sep 17 00:00:00 2001 From: Chris Fernald Date: Thu, 16 Feb 2023 09:56:11 -0800 Subject: [PATCH] Create ARM platform library function for customizing PEI memory (#63) ## Description Creates a new function in the ARM platform library to allow the platform to customize the location of the initial PEI memory installation region. - [ ] Impacts functionality? - **Functionality** - Does the change ultimately impact how firmware functions? - Examples: Add a new library, publish a new PPI, update an algorithm, ... - [ ] Impacts security? - **Security** - Does the change have a direct security impact on an application, flow, or firmware? - Examples: Crypto algorithm change, buffer overflow fix, parameter validation improvement, ... - [x] Breaking change? - **Breaking change** - Will anyone consuming this change experience a break in build or boot behavior? - Examples: Add a new library class, move a module to a different repo, call a function in a new library class in a pre-existing module, ... - [ ] Includes tests? - **Tests** - Does the change include any explicit test code? - Examples: Unit tests, integration tests, robot tests, ... - [ ] Includes documentation? - **Documentation** - Does the change contain explicit documentation additions outside direct code modifications (and comments)? - Examples: Update readme file, add feature readme file, link to documentation on an a separate Web page, ... ## How This Was Tested Tested against the SBSA platform with custom memory initialization logic. ## Integration Instructions Platforms with their own arm platform package will need to add implementation of ArmPlatformGetPeiMemory that returns FALSE to be unaffected. --- .../Include/Library/ArmPlatformLib.h | 20 ++++++++++++++++ .../ArmPlatformLibNullMem.c | 23 +++++++++++++++++++ ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c | 15 +++++++++--- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/ArmPlatformPkg/Include/Library/ArmPlatformLib.h b/ArmPlatformPkg/Include/Library/ArmPlatformLib.h index cd87743eba..1b58c31b16 100644 --- a/ArmPlatformPkg/Include/Library/ArmPlatformLib.h +++ b/ArmPlatformPkg/Include/Library/ArmPlatformLib.h @@ -136,4 +136,24 @@ ArmPlatformGetPlatformPpiList ( OUT EFI_PEI_PPI_DESCRIPTOR **PpiList ); +// MU_CHANGE START: Allow platform to customize initial memory region. + +/** + Checks if the platform requires a special initial EFI memory region. + + @param[out] EfiMemoryBase The custom memory base, will be unchanged if FALSE is returned. + @param[out] EfiMemorySize The custom memory size, will be unchanged if FALSE is returned. + + @retval TRUE A custom memory region was set. + @retval FALSE A custom memory region was not set. +**/ +BOOLEAN +EFIAPI +ArmPlatformGetPeiMemory ( + OUT UINTN *EfiMemoryBase, + OUT UINT32 *EfiMemorySize + ); + +// MU_CHANGE END + #endif diff --git a/ArmPlatformPkg/Library/ArmPlatformLibNull/ArmPlatformLibNullMem.c b/ArmPlatformPkg/Library/ArmPlatformLibNull/ArmPlatformLibNullMem.c index 7151c96625..4d123b91e5 100644 --- a/ArmPlatformPkg/Library/ArmPlatformLibNull/ArmPlatformLibNullMem.c +++ b/ArmPlatformPkg/Library/ArmPlatformLibNull/ArmPlatformLibNullMem.c @@ -26,3 +26,26 @@ ArmPlatformGetVirtualMemoryMap ( { ASSERT (0); } + +// MU_CHANGE START + +/** + Checks if the platform requires a special initial EFI memory region. + + @param[out] EfiMemoryBase The custom memory base, will be unchanged if FALSE is returned. + @param[out] EfiMemorySize The custom memory size, will be unchanged if FALSE is returned. + + @retval TRUE A custom memory region was set. + @retval FALSE A custom memory region was not set. +**/ +BOOLEAN +EFIAPI +ArmPlatformGetPeiMemory ( + OUT UINTN *EfiMemoryBase, + OUT UINT32 *EfiMemorySize + ) +{ + return FALSE; +} + +// MU_CHANGE END diff --git a/ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c b/ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c index bc8eb713ef..e1920ee324 100644 --- a/ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c +++ b/ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c @@ -89,6 +89,7 @@ InitializeMemory ( UINTN FdBase; UINTN FdTop; UINTN UefiMemoryBase; + UINT32 UefiMemorySize; // MU_CHANGE DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Memory Init PEIM Loaded\n")); @@ -109,8 +110,16 @@ InitializeMemory ( // Declare the UEFI memory to PEI // - // In case the firmware has been shadowed in the System Memory - if ((FdBase >= SystemMemoryBase) && (FdTop <= SystemMemoryTop)) { + // MU_CHANGE START: Allow platform to customize initial memory region. + UefiMemorySize = FixedPcdGet32 (PcdSystemMemoryUefiRegionSize); + if (ArmPlatformGetPeiMemory (&UefiMemoryBase, &UefiMemorySize)) { + // Check the Firmware does not intersect with the provided memory region. + ASSERT ((FdBase < UefiMemoryBase) || (FdBase >= (UefiMemoryBase + UefiMemorySize))); + ASSERT ((FdTop <= UefiMemoryBase) || (FdTop > (UefiMemoryBase + UefiMemorySize))); + } else if ((FdBase >= SystemMemoryBase) && (FdTop <= SystemMemoryTop)) { + // In case the firmware has been shadowed in the System Memory + // MU_CHANGE END + // Check if there is enough space between the top of the system memory and the top of the // firmware to place the UEFI memory (for PEI & DXE phases) if (SystemMemoryTop - FdTop >= FixedPcdGet32 (PcdSystemMemoryUefiRegionSize)) { @@ -129,7 +138,7 @@ InitializeMemory ( UefiMemoryBase = SystemMemoryTop - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize); } - Status = PeiServicesInstallPeiMemory (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize)); + Status = PeiServicesInstallPeiMemory (UefiMemoryBase, UefiMemorySize); // MU_CHANGE: Allow platform to customize initial memory region. ASSERT_EFI_ERROR (Status); // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)