]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Compatibility/BootScriptSaveOnS3SaveStateThunk/ScriptSave.c
EdkCompatibilityPkg: Fix typos in comments
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / BootScriptSaveOnS3SaveStateThunk / ScriptSave.c
index de4f38ff6cafbabcb4c4f0ea914125f132584d5c..a3503a5d300353af948f09708fb901f9f2ec62d5 100644 (file)
@@ -2,8 +2,8 @@
   Implementation for S3 Boot Script Save thunk driver.\r
   This thunk driver consumes PI S3SaveState protocol to produce framework S3BootScriptSave Protocol \r
   \r
-  Copyright (c) 2010 Intel Corporation\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
@@ -20,6 +20,60 @@ EFI_BOOT_SCRIPT_SAVE_PROTOCOL mS3ScriptSave = {
                                   BootScriptCloseTable\r
                                  };\r
 EFI_S3_SAVE_STATE_PROTOCOL    *mS3SaveState;\r
+\r
+/**\r
+  Wrapper for a thunk  to transition from long mode to compatibility mode to execute 32-bit code and then transit back to\r
+  long mode.\r
+  \r
+  @param  Function     The 32bit code entry to be executed.\r
+  @param  Param1       The first parameter to pass to 32bit code\r
+  @param  Param2       The second parameter to pass to 32bit code\r
+  @retval EFI_SUCCESS  Execute 32bit code successfully.\r
+  @retval other        Something wrong when execute the 32bit code \r
+              \r
+**/  \r
+EFI_STATUS\r
+Execute32BitCode (\r
+  IN UINT64      Function,\r
+  IN UINT64      Param1,\r
+  IN UINT64      Param2\r
+  );\r
+\r
+/**\r
+  A stub to convert framework boot script dispatch to PI boot script dispatch.\r
+  \r
+  @param  ImageHandle  It should be is NULL.\r
+  @param  Context      The first parameter to pass to 32bit code\r
+\r
+  @return dispatch value.\r
+              \r
+**/  \r
+EFI_STATUS\r
+EFIAPI\r
+FrameworkBootScriptDispatchStub (\r
+  IN EFI_HANDLE ImageHandle,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  DISPATCH_ENTRYPOINT_FUNC  EntryFunc;\r
+  VOID                      *PeiServices;\r
+  IA32_DESCRIPTOR           Idtr;\r
+\r
+  DEBUG ((EFI_D_ERROR, "FrameworkBootScriptDispatchStub - 0x%08x\n", (UINTN)Context));\r
+\r
+  EntryFunc = (DISPATCH_ENTRYPOINT_FUNC) (UINTN) (Context);\r
+  AsmReadIdtr (&Idtr);\r
+  PeiServices = (VOID *)(UINTN)(*(UINT32 *)(Idtr.Base - sizeof (UINT32)));\r
+\r
+  //\r
+  // ECP assumes first parameter is NULL, and second parameter is PeiServices.\r
+  //\r
+  Status = Execute32BitCode ((UINT64)(UINTN)EntryFunc, 0, (UINT64)(UINTN)PeiServices);\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Internal function to add IO write opcode to the table.\r
 \r
@@ -304,7 +358,7 @@ BootScriptPciCfg2ReadWrite (
                           );\r
 }\r
 /**\r
-  Internal function to add smbus excute opcode to the table.\r
+  Internal function to add smbus execute opcode to the table.\r
 \r
   @param  Marker                The variable argument list to get the opcode\r
                                 and associated attributes.\r
@@ -395,6 +449,42 @@ BootScriptDispatch (
                          );\r
 }\r
 \r
+/**\r
+  Internal function to add Save jmp address according to DISPATCH_OPCODE. \r
+  We ignore "Context" parameter.\r
+  We need create thunk stub to convert PEI entrypoint (used in Framework version)\r
+  to DXE entrypoint (defined in PI spec).\r
+\r
+  @param  Marker                The variable argument list to get the opcode\r
+                                and associated attributes.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  Not enough resource to do operation.\r
+  @retval EFI_SUCCESS           Opcode is added.\r
+\r
+**/\r
+EFI_STATUS\r
+FrameworkBootScriptDispatch (\r
+  IN VA_LIST                       Marker\r
+  )\r
+{\r
+  VOID           *EntryPoint;\r
+  VOID           *Context;\r
+\r
+  EntryPoint = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);\r
+\r
+  //\r
+  // Register callback\r
+  //\r
+  Context    = EntryPoint;\r
+  EntryPoint = (VOID *)(UINTN)FrameworkBootScriptDispatchStub;\r
+  return mS3SaveState->Write (\r
+                         mS3SaveState,\r
+                         EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE,\r
+                         EntryPoint,\r
+                         Context\r
+                         );\r
+}\r
+\r
 /**\r
   Internal function to add memory pool operation to the table. \r
  \r
@@ -424,15 +514,29 @@ BootScriptMemPoll (
   BitValue    = VA_ARG (Marker, UINT8 *);\r
   Duration    = (UINT64)VA_ARG (Marker, UINT64);\r
   LoopTimes   = (UINT64)VA_ARG (Marker, UINT64);\r
-  Delay       = MultU64x64 (DivU64x32(Duration, 100), LoopTimes);\r
+  //\r
+  // Framework version: Duration is used for Stall(), which is Microseconds.\r
+  //                    Total time is: Duration(Microseconds) * LoopTimes.\r
+  // PI version:        Duration is always 100ns. Delay is LoopTimes.\r
+  //                    Total time is: 100ns * Delay.\r
+  // So Delay = Duration(Microseconds) * LoopTimes / 100ns\r
+  //          = Duration * 1000ns * LoopTimes / 100ns\r
+  //          = Duration * 10 * LoopTimes\r
+  //\r
+  Delay       = MultU64x64 (MultU64x32 (Duration, 10), LoopTimes);\r
   \r
+  //\r
+  // Framework version: First BitMask, then BitValue\r
+  // PI version: First Data, then DataMask\r
+  // So we revert their order in function call\r
+  //\r
   return mS3SaveState->Write (\r
                           mS3SaveState,\r
                           EFI_BOOT_SCRIPT_MEM_POLL_OPCODE,\r
-                          Width, \r
-                          Address, \r
-                          BitMask, \r
-                          BitValue, \r
+                          Width,\r
+                          Address,\r
+                          BitValue,\r
+                          BitMask,\r
                           Delay\r
                           );\r
 }\r
@@ -539,9 +643,9 @@ BootScriptWrite (
   VA_LIST                   Marker;\r
   \r
   if (TableName != FRAMEWORK_EFI_ACPI_S3_RESUME_SCRIPT_TABLE) {\r
-       //\r
-       // Only S3 boot script is supported for now\r
-       //\r
+    //\r
+    // Only S3 boot script is supported for now\r
+    //\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
@@ -600,7 +704,7 @@ BootScriptWrite (
 \r
   case EFI_BOOT_SCRIPT_DISPATCH_OPCODE:\r
     VA_START (Marker, OpCode);\r
-    Status = BootScriptDispatch (Marker);\r
+    Status = FrameworkBootScriptDispatch (Marker);\r
     VA_END (Marker);\r
     break;\r
 \r
@@ -687,6 +791,47 @@ BootScriptCloseTable (
   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 routine is entry point of ScriptSave driver.\r
 \r
@@ -705,24 +850,144 @@ InitializeScriptSaveOnS3SaveState (
   IN EFI_SYSTEM_TABLE     *SystemTable\r
   )\r
 {\r
-   EFI_STATUS                Status;\r
+  UINT8                                         *Buffer;\r
+  UINTN                                         BufferSize;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT                  ImageContext;\r
+  BOOT_SCRIPT_THUNK_DATA                        *BootScriptThunkData;\r
+  EFI_STATUS                                    Status;\r
+  VOID                                          *DevicePath;\r
+  EFI_PHYSICAL_ADDRESS                          MemoryAddress;\r
+  UINTN                                         PageNumber;\r
+  EFI_HANDLE                                    NewImageHandle;\r
+\r
   //\r
-  // Locate and cache PI S3 Save State Protocol.\r
+  // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry\r
+  // point is loaded by DXE code which is the first time loaded. or else, it is already\r
+  // be reloaded be itself.This is a work-around\r
   //\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiS3SaveStateProtocolGuid, \r
-                  NULL, \r
-                  (VOID **) &mS3SaveState\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
+  Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &DevicePath);\r
+  if (EFI_ERROR (Status)) {\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
+\r
+    MemoryAddress = SIZE_4GB - 1;\r
+    if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
+      PageNumber = EFI_SIZE_TO_PAGES ((UINTN) (ImageContext.ImageSize + ImageContext.SectionAlignment));\r
+    } else {\r
+      PageNumber = EFI_SIZE_TO_PAGES ((UINTN) ImageContext.ImageSize);\r
+    }\r
+    Status  = gBS->AllocatePages (\r
+                     AllocateMaxAddress,\r
+                     EfiReservedMemoryType,\r
+                     PageNumber,\r
+                     &MemoryAddress\r
+                     );\r
+    ASSERT_EFI_ERROR (Status);\r
+    ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)MemoryAddress;\r
+    //\r
+    // Align buffer on section boundary\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
-  return  gBS->InstallProtocolInterface (\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, SystemTable);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    //\r
+    // Additional step for BootScriptThunk integrity\r
+    //\r
+\r
+    //\r
+    // Allocate BootScriptThunkData\r
+    //\r
+    BootScriptThunkData = AllocatePool (sizeof (BOOT_SCRIPT_THUNK_DATA));\r
+    ASSERT (BootScriptThunkData != NULL);\r
+\r
+    BootScriptThunkData->BootScriptThunkBase   = ImageContext.ImageAddress;\r
+    BootScriptThunkData->BootScriptThunkLength = ImageContext.ImageSize;\r
+    //\r
+    // Set BootScriptThunkData\r
+    //\r
+    PcdSet64 (BootScriptThunkDataPtr, (UINT64)(UINTN)BootScriptThunkData); \r
+    return EFI_SUCCESS;\r
+  } else {\r
+    //\r
+    // the entry point is invoked after reloading. following code only run in RESERVED mem\r
+    //\r
+\r
+    //\r
+    // Locate and cache PI S3 Save State Protocol.\r
+    //\r
+    Status = gBS->LocateProtocol (\r
+                    &gEfiS3SaveStateProtocolGuid, \r
+                    NULL, \r
+                    (VOID **) &mS3SaveState\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    return gBS->InstallProtocolInterface (\r
                   &mHandle,\r
                   &gEfiBootScriptSaveProtocolGuid,\r
                   EFI_NATIVE_INTERFACE,\r
                   &mS3ScriptSave\r
                   );\r
-\r
+  }\r
 }\r
 \r
 \r