@@ -664,6 +664,10 @@ ExecuteMmCoreFromMmram (
664664 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext ;
665665 STANDALONE_MM_FOUNDATION_ENTRY_POINT Entry ;
666666 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK * Block ;
667+ EFI_PEI_MM_ACCESS_PPI * MmAccess ;
668+ UINTN Size ;
669+ UINTN Index ;
670+ UINTN MmramRangeCount ;
667671
668672 MmFvBase = 0 ;
669673 MmFvSize = 0 ;
@@ -673,6 +677,40 @@ ExecuteMmCoreFromMmram (
673677 Status = LocateMmCoreFv (& MmFvBase , & MmFvSize , & MmCoreFileName , & ImageContext .Handle );
674678 ASSERT_EFI_ERROR (Status );
675679
680+ //
681+ // Prepare an MM access PPI for MM RAM.
682+ //
683+ MmAccess = NULL ;
684+ MmramRangeCount = 0 ;
685+ Status = PeiServicesLocatePpi (
686+ & gEfiPeiMmAccessPpiGuid ,
687+ 0 ,
688+ NULL ,
689+ (VOID * * )& MmAccess
690+ );
691+ if (!EFI_ERROR (Status )) {
692+ //
693+ // Open all MMRAM ranges, if MmAccess is available.
694+ //
695+ Size = 0 ;
696+ Status = MmAccess -> GetCapabilities ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , & Size , NULL );
697+ if (Status != EFI_BUFFER_TOO_SMALL ) {
698+ // This is not right...
699+ ASSERT (Status == EFI_BUFFER_TOO_SMALL );
700+ return EFI_DEVICE_ERROR ;
701+ }
702+
703+ MmramRangeCount = Size / sizeof (EFI_MMRAM_DESCRIPTOR );
704+ for (Index = 0 ; Index < MmramRangeCount ; Index ++ ) {
705+ Status = MmAccess -> Open ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , Index );
706+ if (EFI_ERROR (Status )) {
707+ DEBUG ((DEBUG_ERROR , "MM IPL failed to open MMRAM windows index %d - %r\n" , Index , Status ));
708+ ASSERT_EFI_ERROR (Status );
709+ goto Done ;
710+ }
711+ }
712+ }
713+
676714 //
677715 // Initialize ImageContext
678716 //
@@ -683,7 +721,7 @@ ExecuteMmCoreFromMmram (
683721 //
684722 Status = PeCoffLoaderGetImageInfo (& ImageContext );
685723 if (EFI_ERROR (Status )) {
686- return Status ;
724+ goto Done ;
687725 }
688726
689727 PageCount = (UINTN )EFI_SIZE_TO_PAGES ((UINTN )ImageContext .ImageSize + ImageContext .SectionAlignment );
@@ -693,7 +731,8 @@ ExecuteMmCoreFromMmram (
693731 //
694732 ImageContext .ImageAddress = MmIplAllocateMmramPage (PageCount , & Block );
695733 if (ImageContext .ImageAddress == 0 ) {
696- return EFI_NOT_FOUND ;
734+ Status = EFI_NOT_FOUND ;
735+ goto Done ;
697736 }
698737
699738 //
@@ -756,6 +795,44 @@ ExecuteMmCoreFromMmram (
756795 }
757796 }
758797
798+ Done :
799+ if (MmAccess != NULL ) {
800+ //
801+ // Close all MMRAM ranges, if MmAccess is available.
802+ //
803+ for (Index = 0 ; Index < MmramRangeCount ; Index ++ ) {
804+ Status = MmAccess -> Close ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , Index );
805+ if (EFI_ERROR (Status )) {
806+ DEBUG ((DEBUG_ERROR , "MM IPL failed to close MMRAM windows index %d - %r\n" , Index , Status ));
807+ ASSERT (FALSE);
808+ return Status ;
809+ }
810+
811+ //
812+ // Print debug message that the MMRAM window is now closed.
813+ //
814+ DEBUG ((DEBUG_INFO , "MM IPL closed MMRAM window index %d\n" , Index ));
815+
816+ //
817+ // Lock the MMRAM (Note: Locking MMRAM may not be supported on all platforms)
818+ //
819+ Status = MmAccess -> Lock ((EFI_PEI_SERVICES * * )GetPeiServicesTablePointer (), MmAccess , Index );
820+ if (EFI_ERROR (Status )) {
821+ //
822+ // Print error message that the MMRAM failed to lock...
823+ //
824+ DEBUG ((DEBUG_ERROR , "MM IPL could not lock MMRAM (Index %d) after executing MM Core %r\n" , Index , Status ));
825+ ASSERT (FALSE);
826+ return Status ;
827+ }
828+
829+ //
830+ // Print debug message that the MMRAM window is now closed.
831+ //
832+ DEBUG ((DEBUG_INFO , "MM IPL locked MMRAM window index %d\n" , Index ));
833+ }
834+ }
835+
759836 return Status ;
760837}
761838
0 commit comments