]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/AArch64/StandaloneMmCoreEntryPoint.c
StandaloneMmPkg/StandaloneMmCoreEntryPoint: relocate StMM core on the fly
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreEntryPoint / AArch64 / StandaloneMmCoreEntryPoint.c
index 20723385113f90dd20ad83c8e286a17a3aab72a2..9cecfa667b905c975ddd348513c51fc8a5955718 100644 (file)
@@ -225,6 +225,7 @@ _ModuleEntryPoint (
   VOID                                    *HobStart;\r
   VOID                                    *TeData;\r
   UINTN                                   TeDataSize;\r
+  EFI_PHYSICAL_ADDRESS                    ImageBase;\r
 \r
   // Get Secure Partition Manager Version Information\r
   Status = GetSpmVersion ();\r
@@ -253,6 +254,7 @@ _ModuleEntryPoint (
   Status = GetStandaloneMmCorePeCoffSections (\r
              TeData,\r
              &ImageContext,\r
+             &ImageBase,\r
              &SectionHeaderOffset,\r
              &NumberOfSections\r
              );\r
@@ -261,10 +263,21 @@ _ModuleEntryPoint (
     goto finish;\r
   }\r
 \r
+  //\r
+  // ImageBase may deviate from ImageContext.ImageAddress if we are dealing\r
+  // with a TE image, in which case the latter points to the actual offset\r
+  // of the image, whereas ImageBase refers to the address where the image\r
+  // would start if the stripped PE headers were still in place. In either\r
+  // case, we need to fix up ImageBase so it refers to the actual current\r
+  // load address.\r
+  //\r
+  ImageBase += (UINTN)TeData - ImageContext.ImageAddress;\r
+\r
   // Update the memory access permissions of individual sections in the\r
   // Standalone MM core module\r
   Status = UpdateMmFoundationPeCoffPermissions (\r
              &ImageContext,\r
+             ImageBase,\r
              SectionHeaderOffset,\r
              NumberOfSections,\r
              ArmSetMemoryRegionNoExec,\r
@@ -276,6 +289,15 @@ _ModuleEntryPoint (
     goto finish;\r
   }\r
 \r
+  if (ImageContext.ImageAddress != (UINTN)TeData) {\r
+    ImageContext.ImageAddress = (UINTN)TeData;\r
+    ArmSetMemoryRegionNoExec (ImageBase, SIZE_4KB);\r
+    ArmClearMemoryRegionReadOnly (ImageBase, SIZE_4KB);\r
+\r
+    Status = PeCoffLoaderRelocateImage (&ImageContext);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
   //\r
   // Create Hoblist based upon boot information passed by privileged software\r
   //\r