@@ -454,7 +454,10 @@ CreatMmHobList (
454454 MmProfileDataHob ,
455455 Block
456456 );
457- FreePages (PlatformHobList , EFI_SIZE_TO_PAGES (PlatformHobSize ));
457+ if (PlatformHobSize != 0 ) {
458+ FreePages (PlatformHobList , EFI_SIZE_TO_PAGES (PlatformHobSize ));
459+ }
460+
458461 ASSERT (Status == RETURN_BUFFER_TOO_SMALL );
459462 ASSERT (FoundationHobSize != 0 );
460463
@@ -652,6 +655,7 @@ ExecuteMmCoreFromMmram (
652655 )
653656{
654657 EFI_STATUS Status ;
658+ EFI_STATUS AccessStatus ; // MU_CHANGE: Separate variable to avoid overwriting Status
655659 UINTN PageCount ;
656660 VOID * MmHobList ;
657661 UINTN MmHobSize ;
@@ -661,6 +665,10 @@ ExecuteMmCoreFromMmram (
661665 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext ;
662666 STANDALONE_MM_FOUNDATION_ENTRY_POINT Entry ;
663667 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK * Block ;
668+ EFI_PEI_MM_ACCESS_PPI * MmAccess ;
669+ UINTN Size ;
670+ UINTN Index ;
671+ UINTN MmramRangeCount ;
664672
665673 MmFvBase = 0 ;
666674 MmFvSize = 0 ;
@@ -670,6 +678,40 @@ ExecuteMmCoreFromMmram (
670678 Status = LocateMmCoreFv (& MmFvBase , & MmFvSize , & MmCoreFileName , & ImageContext .Handle );
671679 ASSERT_EFI_ERROR (Status );
672680
681+ //
682+ // Prepare an MM access PPI for MM RAM.
683+ //
684+ MmAccess = NULL ;
685+ MmramRangeCount = 0 ;
686+ Status = PeiServicesLocatePpi (
687+ & gEfiPeiMmAccessPpiGuid ,
688+ 0 ,
689+ NULL ,
690+ (VOID * * )& MmAccess
691+ );
692+ if (!EFI_ERROR (Status )) {
693+ //
694+ // Open all MMRAM ranges, if MmAccess is available.
695+ //
696+ Size = 0 ;
697+ Status = MmAccess -> GetCapabilities ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , & Size , NULL );
698+ if (Status != EFI_BUFFER_TOO_SMALL ) {
699+ // This is not right...
700+ ASSERT (Status == EFI_BUFFER_TOO_SMALL );
701+ return EFI_DEVICE_ERROR ;
702+ }
703+
704+ MmramRangeCount = Size / sizeof (EFI_MMRAM_DESCRIPTOR );
705+ for (Index = 0 ; Index < MmramRangeCount ; Index ++ ) {
706+ Status = MmAccess -> Open ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , Index );
707+ if (EFI_ERROR (Status )) {
708+ DEBUG ((DEBUG_ERROR , "MM IPL failed to open MMRAM windows index %d - %r\n" , Index , Status ));
709+ ASSERT_EFI_ERROR (Status );
710+ goto Done ;
711+ }
712+ }
713+ }
714+
673715 //
674716 // Initialize ImageContext
675717 //
@@ -680,7 +722,7 @@ ExecuteMmCoreFromMmram (
680722 //
681723 Status = PeCoffLoaderGetImageInfo (& ImageContext );
682724 if (EFI_ERROR (Status )) {
683- return Status ;
725+ goto Done ;
684726 }
685727
686728 PageCount = (UINTN )EFI_SIZE_TO_PAGES ((UINTN )ImageContext .ImageSize + ImageContext .SectionAlignment );
@@ -690,7 +732,8 @@ ExecuteMmCoreFromMmram (
690732 //
691733 ImageContext .ImageAddress = MmIplAllocateMmramPage (PageCount , & Block );
692734 if (ImageContext .ImageAddress == 0 ) {
693- return EFI_NOT_FOUND ;
735+ Status = EFI_NOT_FOUND ;
736+ goto Done ;
694737 }
695738
696739 //
@@ -753,6 +796,48 @@ ExecuteMmCoreFromMmram (
753796 }
754797 }
755798
799+ Done :
800+ if (MmAccess != NULL ) {
801+ //
802+ // Close all MMRAM ranges, if MmAccess is available.
803+ //
804+ for (Index = 0 ; Index < MmramRangeCount ; Index ++ ) {
805+ // MU_CHANGE START: Will not return if error occurs
806+ AccessStatus = MmAccess -> Close ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , Index );
807+ if (EFI_ERROR (AccessStatus )) {
808+ DEBUG ((DEBUG_ERROR , "MM IPL failed to close MMRAM windows index %d - %r\n" , Index , AccessStatus ));
809+ ASSERT (FALSE);
810+ }
811+
812+ // MU_CHANGE END: Will not return if error occurs
813+
814+ //
815+ // Print debug message that the MMRAM window is now closed.
816+ //
817+ DEBUG ((DEBUG_INFO , "MM IPL closed MMRAM window index %d\n" , Index ));
818+
819+ //
820+ // Lock the MMRAM (Note: Locking MMRAM may not be supported on all platforms)
821+ //
822+ // MU_CHANGE START: Will not return if error occurs and allow UNSUPPORTED
823+ AccessStatus = MmAccess -> Lock ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , Index );
824+ if (EFI_ERROR (AccessStatus ) && (AccessStatus != EFI_UNSUPPORTED )) {
825+ //
826+ // Print error message that the MMRAM failed to lock...
827+ //
828+ DEBUG ((DEBUG_ERROR , "MM IPL could not lock MMRAM (Index %d) after executing MM Core %r\n" , Index , AccessStatus ));
829+ ASSERT (FALSE);
830+ }
831+
832+ // MU_CHANGE END: Will not return if error occurs and allow UNSUPPORTED
833+
834+ //
835+ // Print debug message that the MMRAM window is now locked.
836+ //
837+ DEBUG ((DEBUG_INFO , "MM IPL locked MMRAM window index %d\n" , Index ));
838+ }
839+ }
840+
756841 return Status ;
757842}
758843
0 commit comments