From 56dfab9a8a143aa486d07eafc3d5a78bff540228 Mon Sep 17 00:00:00 2001 From: Wei6 Xu Date: Mon, 21 Oct 2024 10:13:54 +0800 Subject: [PATCH] StandaloneMmPkg/Core: Shadow Standalone BFV into MMRAM BFV is outside the MMRAM. Currently, StandaloneMmIplPei uses the API MmUnblockMemoryRequest() to unblock the access for the BFV. However, the BFV's memory might be gone after ExitBootService event. If any access to the memory, unexpected error would happen. To fix the above issue, StandaloneMmCore should shadow standalone BFV into MMRAM before processing it, then free the shadowed BFV after MM driver dispatch is done. Cc: Ard Biesheuvel Cc: Sami Mujawar Cc: Ray Ni Cc: Jiaxin Wu Signed-off-by: Wei6 Xu --- StandaloneMmPkg/Core/Dispatcher.c | 7 ++++++ StandaloneMmPkg/Core/StandaloneMmCore.c | 23 ++++++++++++++----- StandaloneMmPkg/Core/StandaloneMmCore.h | 7 +++--- .../StandaloneMmIplPei/StandaloneMmIplPei.c | 8 ------- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c index e55fdbabf2..8e6b85caee 100644 --- a/StandaloneMmPkg/Core/Dispatcher.c +++ b/StandaloneMmPkg/Core/Dispatcher.c @@ -766,6 +766,13 @@ MmDriverDispatchHandler ( MmiHandlerUnRegister (DispatchHandle); + // + // Free shadowed standalone BFV + // + if (mBfv != NULL) { + FreePool (mBfv); + } + return EFI_SUCCESS; } diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c index 970a06045b..127248afab 100644 --- a/StandaloneMmPkg/Core/StandaloneMmCore.c +++ b/StandaloneMmPkg/Core/StandaloneMmCore.c @@ -83,9 +83,10 @@ MM_CORE_MMI_HANDLERS mMmCoreMmiHandlers[] = { { NULL, NULL, NULL, FALSE }, }; -BOOLEAN mMmEntryPointRegistered = FALSE; -MM_COMM_BUFFER *mMmCommunicationBuffer; -VOID *mInternalCommBufferCopy; +BOOLEAN mMmEntryPointRegistered = FALSE; +MM_COMM_BUFFER *mMmCommunicationBuffer; +VOID *mInternalCommBufferCopy; +EFI_FIRMWARE_VOLUME_HEADER *mBfv = NULL; /** Place holder function until all the MM System Table Service are available. @@ -844,9 +845,19 @@ StandaloneMmMain ( // Dispatch standalone BFV // if (BfvHob->BaseAddress != 0) { - DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", BfvHob->BaseAddress)); - MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)BfvHob->BaseAddress, 0); - MmDispatcher (); + // + // Shadow standalone BFV into MMRAM + // + mBfv = AllocatePool (BfvHob->Length); + if (mBfv != NULL) { + CopyMem ((VOID *)mBfv, (VOID *)(UINTN)BfvHob->BaseAddress, BfvHob->Length); + DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", mBfv)); + MmCoreFfsFindMmDriver (mBfv, 0); + MmDispatcher (); + if (!FeaturePcdGet (PcdRestartMmDispatcherOnceMmEntryRegistered)) { + FreePool (mBfv); + } + } } } diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h index 093a35fb56..7bff1cde14 100644 --- a/StandaloneMmPkg/Core/StandaloneMmCore.h +++ b/StandaloneMmPkg/Core/StandaloneMmCore.h @@ -178,9 +178,10 @@ typedef struct { // // MM Core Global Variables // -extern EFI_MM_SYSTEM_TABLE gMmCoreMmst; -extern LIST_ENTRY gHandleList; -extern BOOLEAN mMmEntryPointRegistered; +extern EFI_MM_SYSTEM_TABLE gMmCoreMmst; +extern LIST_ENTRY gHandleList; +extern BOOLEAN mMmEntryPointRegistered; +extern EFI_FIRMWARE_VOLUME_HEADER *mBfv; /** Called to initialize the memory service. diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c index b1cd3c1d81..1a01d7a465 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c @@ -513,14 +513,6 @@ ExecuteMmCoreFromMmram ( Status = LocateMmCoreFv (&MmFvBase, &MmFvSize, &MmCoreFileName, &ImageContext.Handle); ASSERT_EFI_ERROR (Status); - // - // Unblock the MM FV range to be accessible from inside MM - // - if ((MmFvBase != 0) && (MmFvSize != 0)) { - Status = MmUnblockMemoryRequest (MmFvBase, EFI_SIZE_TO_PAGES (MmFvSize)); - ASSERT_EFI_ERROR (Status); - } - // // Initialize ImageContext //