]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c
MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe: Add support for PCD PcdPteMemoryEn...
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / BootScriptExecutorDxe / ScriptExecute.c
index e34ead59fa1a820b96ea43dc93e90a993e0cba55..22d434914befa9c96700791af381fda4276ee1d2 100644 (file)
@@ -1,10 +1,11 @@
 /** @file\r
   This is the code for Boot Script Executer module.\r
 \r
-  This driver is dispatched by Dxe core and the driver will reload itself to ACPI NVS memory\r
+  This driver is dispatched by Dxe core and the driver will reload itself to ACPI reserved memory\r
   in the entry point. The functionality is to interpret and restore the S3 boot script\r
 \r
-Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -22,6 +23,9 @@ EFI_GUID              mBootScriptExecutorImageGuid = {
   0x9a8d3433, 0x9fe8, 0x42b6, { 0x87, 0xb, 0x1e, 0x31, 0xc8, 0x4e, 0xbe, 0x3b }\r
 };\r
 \r
+BOOLEAN               mPage1GSupport = FALSE;\r
+UINT64                mAddressEncMask = 0;\r
+\r
 /**\r
   Entry function of Boot script exector. This function will be executed in\r
   S3 boot path.\r
@@ -205,6 +209,174 @@ S3BootScriptExecutorEntryFunction (
   CpuDeadLoop();\r
   return EFI_UNSUPPORTED;\r
 }\r
+\r
+/**\r
+  Register image to memory profile.\r
+\r
+  @param FileName       File name of the image.\r
+  @param ImageBase      Image base address.\r
+  @param ImageSize      Image size.\r
+  @param FileType       File type of the image.\r
+\r
+**/\r
+VOID\r
+RegisterMemoryProfileImage (\r
+  IN EFI_GUID                       *FileName,\r
+  IN PHYSICAL_ADDRESS               ImageBase,\r
+  IN UINT64                         ImageSize,\r
+  IN EFI_FV_FILETYPE                FileType\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EDKII_MEMORY_PROFILE_PROTOCOL     *ProfileProtocol;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FilePath;\r
+  UINT8                             TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];\r
+\r
+  if ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT0) != 0) {\r
+\r
+    FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;\r
+    Status = gBS->LocateProtocol (&gEdkiiMemoryProfileGuid, NULL, (VOID **) &ProfileProtocol);\r
+    if (!EFI_ERROR (Status)) {\r
+      EfiInitializeFwVolDevicepathNode (FilePath, FileName);\r
+      SetDevicePathEndNode (FilePath + 1);\r
+\r
+      Status = ProfileProtocol->RegisterImage (\r
+                                  ProfileProtocol,\r
+                                  (EFI_DEVICE_PATH_PROTOCOL *) FilePath,\r
+                                  ImageBase,\r
+                                  ImageSize,\r
+                                  FileType\r
+                                  );\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  This is the Event notification function to reload BootScriptExecutor image\r
+  to RESERVED mem and save it to LockBox.\r
+  \r
+  @param    Event   Pointer to this event\r
+  @param    Context Event handler private data \r
+ **/\r
+VOID\r
+EFIAPI\r
+ReadyToLockEventNotify (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  EFI_STATUS                                    Status;\r
+  VOID                                          *Interface;\r
+  UINT8                                         *Buffer;\r
+  UINTN                                         BufferSize;\r
+  EFI_HANDLE                                    NewImageHandle;\r
+  UINTN                                         Pages;\r
+  EFI_PHYSICAL_ADDRESS                          FfsBuffer;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT                  ImageContext;\r
+\r
+  Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
+  if (EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // A workaround: Here we install a dummy handle\r
+  //\r
+  NewImageHandle = NULL;\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &NewImageHandle,\r
+                  &gEfiCallerIdGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Reload BootScriptExecutor image itself to RESERVED mem\r
+  //\r
+  Status = GetSectionFromAnyFv  (\r
+             &gEfiCallerIdGuid,\r
+             EFI_SECTION_PE32,\r
+             0,\r
+             (VOID **) &Buffer,\r
+             &BufferSize\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+  ImageContext.Handle    = Buffer;\r
+  ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
+  //\r
+  // Get information about the image being loaded\r
+  //\r
+  Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
+  ASSERT_EFI_ERROR (Status);\r
+  if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
+    Pages = EFI_SIZE_TO_PAGES ((UINTN) (ImageContext.ImageSize + ImageContext.SectionAlignment));\r
+  } else {\r
+    Pages = EFI_SIZE_TO_PAGES ((UINTN) ImageContext.ImageSize);\r
+  }\r
+  FfsBuffer = 0xFFFFFFFF;\r
+  Status = gBS->AllocatePages (\r
+                  AllocateMaxAddress,\r
+                  EfiReservedMemoryType,\r
+                  Pages,\r
+                  &FfsBuffer\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+  ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer;\r
+  //\r
+  // Align buffer on section boundary\r
+  //\r
+  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
+  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));\r
+  //\r
+  // Load the image to our new buffer\r
+  //\r
+  Status = PeCoffLoaderLoadImage (&ImageContext);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Relocate the image in our new buffer\r
+  //\r
+  Status = PeCoffLoaderRelocateImage (&ImageContext);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Free the buffer allocated by ReadSection since the image has been relocated in the new buffer\r
+  //\r
+  gBS->FreePool (Buffer);\r
+\r
+  //\r
+  // Flush the instruction cache so the image data is written before we execute it\r
+  //\r
+  InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
+\r
+  RegisterMemoryProfileImage (\r
+    &gEfiCallerIdGuid,\r
+    ImageContext.ImageAddress,\r
+    ImageContext.ImageSize,\r
+    EFI_FV_FILETYPE_DRIVER\r
+    );\r
+\r
+  Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, gST);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Additional step for BootScript integrity\r
+  // Save BootScriptExecutor image\r
+  //\r
+  Status = SaveLockBox (\r
+             &mBootScriptExecutorImageGuid,\r
+             (VOID *)(UINTN)ImageContext.ImageAddress,\r
+             (UINTN)ImageContext.ImageSize\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  gBS->CloseEvent (Event);\r
+}\r
+\r
 /**\r
   Entrypoint of Boot script exector driver, this function will be executed in\r
   normal boot phase and invoked by DXE dispatch.\r
@@ -222,16 +394,25 @@ BootScriptExecutorEntryPoint (
   IN EFI_SYSTEM_TABLE     *SystemTable\r
   )\r
 {\r
-  UINT8                                         *Buffer;\r
   UINTN                                         BufferSize;\r
   UINTN                                         Pages;\r
-  EFI_PHYSICAL_ADDRESS                          FfsBuffer;\r
-  PE_COFF_LOADER_IMAGE_CONTEXT                  ImageContext;\r
   BOOT_SCRIPT_EXECUTOR_VARIABLE                 *EfiBootScriptExecutorVariable;\r
   EFI_PHYSICAL_ADDRESS                          BootScriptExecutorBuffer;\r
   EFI_STATUS                                    Status;\r
   VOID                                          *DevicePath;\r
-  EFI_HANDLE                                    NewImageHandle;\r
+  EFI_EVENT                                     ReadyToLockEvent;\r
+  VOID                                          *Registration;\r
+  UINT32                                        RegEax;\r
+  UINT32                                        RegEdx;\r
+\r
+  if (!PcdGetBool (PcdAcpiS3Enable)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Make sure AddressEncMask is contained to smallest supported address field.\r
+  //\r
+  mAddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;\r
 \r
   //\r
   // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry\r
@@ -240,94 +421,32 @@ BootScriptExecutorEntryPoint (
   //\r
   Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &DevicePath);\r
   if (EFI_ERROR (Status)) {\r
-\r
-      //\r
-      // This is the first-time loaded by DXE core. reload itself to RESERVED mem\r
-      //\r
-      //\r
-      // A workaround: Here we install a dummy handle\r
-      //\r
-      NewImageHandle = NULL;\r
-      Status = gBS->InstallProtocolInterface (\r
-                  &NewImageHandle,\r
-                  &gEfiCallerIdGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  NULL\r
-                  );\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      Status = GetSectionFromAnyFv  (\r
-                 &gEfiCallerIdGuid,\r
-                 EFI_SECTION_PE32,\r
-                 0,\r
-                 (VOID **) &Buffer,\r
-                 &BufferSize\r
-                 );\r
-      ASSERT_EFI_ERROR (Status);\r
-      ImageContext.Handle    = Buffer;\r
-      ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
-      //\r
-      // Get information about the image being loaded\r
       //\r
-      Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
-      ASSERT_EFI_ERROR (Status);\r
-      Pages = EFI_SIZE_TO_PAGES(BufferSize + ImageContext.SectionAlignment);\r
-      FfsBuffer = 0xFFFFFFFF;\r
-      Status = gBS->AllocatePages (\r
-                    AllocateMaxAddress,\r
-                    EfiReservedMemoryType,\r
-                    Pages,\r
-                    &FfsBuffer\r
-                    );\r
-      ASSERT_EFI_ERROR (Status);\r
-      ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer;\r
-      //\r
-      // Align buffer on section boundry\r
-      //\r
-      ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
-      ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
-      //\r
-      // Load the image to our new buffer\r
-      //\r
-      Status = PeCoffLoaderLoadImage (&ImageContext);\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      //\r
-      // Relocate the image in our new buffer\r
-      //\r
-      Status = PeCoffLoaderRelocateImage (&ImageContext);\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      //\r
-      // Free the buffer allocated by ReadSection since the image has been relocated in the new buffer\r
-      //\r
-      gBS->FreePool (Buffer);\r
-\r
-      //\r
-      // Flush the instruction cache so the image data is written before we execute it\r
+      // Create ReadyToLock event to reload BootScriptExecutor image\r
+      // to RESERVED mem and save it to LockBox.\r
       //\r
-      InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
-      Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, SystemTable);\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      //\r
-      // Additional step for BootScript integrity\r
-      // Save BootScriptExecutor image\r
-      //\r
-      Status = SaveLockBox (\r
-                 &mBootScriptExecutorImageGuid,\r
-                 (VOID *)(UINTN)ImageContext.ImageAddress,\r
-                 (UINTN)ImageContext.ImageSize\r
-                 );\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
+      ReadyToLockEvent = EfiCreateProtocolNotifyEvent  (\r
+                           &gEfiDxeSmmReadyToLockProtocolGuid,\r
+                           TPL_NOTIFY,\r
+                           ReadyToLockEventNotify,\r
+                           NULL,\r
+                           &Registration\r
+                           );\r
+      ASSERT (ReadyToLockEvent != NULL);\r
     } else {\r
       //\r
       // the entry point is invoked after reloading. following code only run in RESERVED mem\r
       //\r
+      if (PcdGetBool(PcdUse1GPageTable)) {\r
+        AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
+        if (RegEax >= 0x80000001) {\r
+          AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
+          if ((RegEdx & BIT26) != 0) {\r
+            mPage1GSupport = TRUE;\r
+          }\r
+        }\r
+      }\r
+\r
       BufferSize = sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE);\r
 \r
       BootScriptExecutorBuffer = 0xFFFFFFFF;\r
@@ -363,11 +482,8 @@ BootScriptExecutorEntryPoint (
 \r
       Status = SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
       ASSERT_EFI_ERROR (Status);\r
-\r
     }\r
 \r
     return EFI_SUCCESS;\r
 }\r
 \r
-\r
-\r