diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c index 4483aaef99..496cf21396 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c @@ -454,7 +454,10 @@ CreatMmHobList ( MmProfileDataHob, Block ); - FreePages (PlatformHobList, EFI_SIZE_TO_PAGES (PlatformHobSize)); + if (PlatformHobSize != 0) { + FreePages (PlatformHobList, EFI_SIZE_TO_PAGES (PlatformHobSize)); + } + ASSERT (Status == RETURN_BUFFER_TOO_SMALL); ASSERT (FoundationHobSize != 0); @@ -652,6 +655,7 @@ ExecuteMmCoreFromMmram ( ) { EFI_STATUS Status; + EFI_STATUS AccessStatus; // MU_CHANGE: Separate variable to avoid overwriting Status UINTN PageCount; VOID *MmHobList; UINTN MmHobSize; @@ -661,6 +665,10 @@ ExecuteMmCoreFromMmram ( PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; STANDALONE_MM_FOUNDATION_ENTRY_POINT Entry; EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *Block; + EFI_PEI_MM_ACCESS_PPI *MmAccess; + UINTN Size; + UINTN Index; + UINTN MmramRangeCount; MmFvBase = 0; MmFvSize = 0; @@ -670,6 +678,40 @@ ExecuteMmCoreFromMmram ( Status = LocateMmCoreFv (&MmFvBase, &MmFvSize, &MmCoreFileName, &ImageContext.Handle); ASSERT_EFI_ERROR (Status); + // + // Prepare an MM access PPI for MM RAM. + // + MmAccess = NULL; + MmramRangeCount = 0; + Status = PeiServicesLocatePpi ( + &gEfiPeiMmAccessPpiGuid, + 0, + NULL, + (VOID **)&MmAccess + ); + if (!EFI_ERROR (Status)) { + // + // Open all MMRAM ranges, if MmAccess is available. + // + Size = 0; + Status = MmAccess->GetCapabilities ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), MmAccess, &Size, NULL); + if (Status != EFI_BUFFER_TOO_SMALL) { + // This is not right... + ASSERT (Status == EFI_BUFFER_TOO_SMALL); + return EFI_DEVICE_ERROR; + } + + MmramRangeCount = Size / sizeof (EFI_MMRAM_DESCRIPTOR); + for (Index = 0; Index < MmramRangeCount; Index++) { + Status = MmAccess->Open ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), MmAccess, Index); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MM IPL failed to open MMRAM windows index %d - %r\n", Index, Status)); + ASSERT_EFI_ERROR (Status); + goto Done; + } + } + } + // // Initialize ImageContext // @@ -680,7 +722,7 @@ ExecuteMmCoreFromMmram ( // Status = PeCoffLoaderGetImageInfo (&ImageContext); if (EFI_ERROR (Status)) { - return Status; + goto Done; } PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment); @@ -690,7 +732,8 @@ ExecuteMmCoreFromMmram ( // ImageContext.ImageAddress = MmIplAllocateMmramPage (PageCount, &Block); if (ImageContext.ImageAddress == 0) { - return EFI_NOT_FOUND; + Status = EFI_NOT_FOUND; + goto Done; } // @@ -753,6 +796,48 @@ ExecuteMmCoreFromMmram ( } } +Done: + if (MmAccess != NULL) { + // + // Close all MMRAM ranges, if MmAccess is available. + // + for (Index = 0; Index < MmramRangeCount; Index++) { + // MU_CHANGE START: Will not return if error occurs + AccessStatus = MmAccess->Close ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), MmAccess, Index); + if (EFI_ERROR (AccessStatus)) { + DEBUG ((DEBUG_ERROR, "MM IPL failed to close MMRAM windows index %d - %r\n", Index, AccessStatus)); + ASSERT (FALSE); + } + + // MU_CHANGE END: Will not return if error occurs + + // + // Print debug message that the MMRAM window is now closed. + // + DEBUG ((DEBUG_INFO, "MM IPL closed MMRAM window index %d\n", Index)); + + // + // Lock the MMRAM (Note: Locking MMRAM may not be supported on all platforms) + // + // MU_CHANGE START: Will not return if error occurs and allow UNSUPPORTED + AccessStatus = MmAccess->Lock ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), MmAccess, Index); + if (EFI_ERROR (AccessStatus) && (AccessStatus != EFI_UNSUPPORTED)) { + // + // Print error message that the MMRAM failed to lock... + // + DEBUG ((DEBUG_ERROR, "MM IPL could not lock MMRAM (Index %d) after executing MM Core %r\n", Index, AccessStatus)); + ASSERT (FALSE); + } + + // MU_CHANGE END: Will not return if error occurs and allow UNSUPPORTED + + // + // Print debug message that the MMRAM window is now locked. + // + DEBUG ((DEBUG_INFO, "MM IPL locked MMRAM window index %d\n", Index)); + } + } + return Status; } diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h index 1f3b49e6a7..35861133e7 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf index 84781d6c59..09908bdeaf 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf @@ -65,6 +65,7 @@ gEfiPeiMmCommunication3PpiGuid gEfiEndOfPeiSignalPpiGuid gMmCoreFvLocationPpiGuid + gEfiPeiMmAccessPpiGuid [Protocols] gEfiMmEndOfPeiProtocol