]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Compatibility/BootScriptSaveOnS3SaveStateThunk/ScriptSave.c
EdkCompatibilityPkg: Fix 32bit build failure.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / BootScriptSaveOnS3SaveStateThunk / ScriptSave.c
index 3a3b8c8be53f0b93fea22763127d4b416d0e9255..0bc2f75ca96feb53f81ca164f5fda60c7609afc7 100644 (file)
@@ -2,7 +2,7 @@
   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 - 2011, Intel Corporation. All rights reserved.<BR>\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
@@ -514,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
@@ -777,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
@@ -813,10 +868,10 @@ InitializeScriptSaveOnS3SaveState (
   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 NVS mem\r
+    // This is the first-time loaded by DXE core. reload itself to RESERVED mem\r
     //\r
     //\r
-    // A workarouond: Here we install a dummy handle\r
+    // A workaround: Here we install a dummy handle\r
     //\r
     NewImageHandle = NULL;\r
     Status = gBS->InstallProtocolInterface (\r
@@ -825,6 +880,7 @@ InitializeScriptSaveOnS3SaveState (
                     EFI_NATIVE_INTERFACE,\r
                     NULL\r
                     );\r
+    ASSERT_EFI_ERROR (Status);\r
 \r
     Status = GetSectionFromAnyFv  (\r
                &gEfiCallerIdGuid,\r
@@ -833,27 +889,28 @@ InitializeScriptSaveOnS3SaveState (
                (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
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+    ASSERT_EFI_ERROR (Status);\r
 \r
     MemoryAddress = SIZE_4GB - 1;\r
-    PageNumber    = EFI_SIZE_TO_PAGES (BufferSize + ImageContext.SectionAlignment);\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
-                     EfiACPIMemoryNVS,\r
+                     EfiReservedMemoryType,\r
                      PageNumber,\r
                      &MemoryAddress\r
                      );\r
-    if (EFI_ERROR (Status)) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
+    ASSERT_EFI_ERROR (Status);\r
     ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)MemoryAddress;\r
     //\r
     // Align buffer on section boundry\r
@@ -864,30 +921,34 @@ InitializeScriptSaveOnS3SaveState (
     // Load the image to our new buffer\r
     //\r
     Status = PeCoffLoaderLoadImage (&ImageContext);\r
-    if (EFI_ERROR (Status)) {\r
-      gBS->FreePages (MemoryAddress, PageNumber);\r
-      return Status;\r
-    }\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
-    if (EFI_ERROR (Status)) {\r
-      PeCoffLoaderUnloadImage (&ImageContext);\r
-      gBS->FreePages (MemoryAddress, PageNumber);\r
-      return Status;\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
-    if (EFI_ERROR (Status)) {\r
-      gBS->FreePages (MemoryAddress, PageNumber);\r
-      return Status;\r
-    }\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
     //\r
     // Additional step for BootScriptThunk integrity\r
     //\r
@@ -896,9 +957,7 @@ InitializeScriptSaveOnS3SaveState (
     // Allocate BootScriptThunkData\r
     //\r
     BootScriptThunkData = AllocatePool (sizeof (BOOT_SCRIPT_THUNK_DATA));\r
-    if (BootScriptThunkData == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
+    ASSERT (BootScriptThunkData != NULL);\r
 \r
     BootScriptThunkData->BootScriptThunkBase   = ImageContext.ImageAddress;\r
     BootScriptThunkData->BootScriptThunkLength = ImageContext.ImageSize;\r
@@ -909,7 +968,7 @@ InitializeScriptSaveOnS3SaveState (
     return EFI_SUCCESS;\r
   } else {\r
     //\r
-    // the entry point is invoked after reloading. following code only run in  ACPI NVS\r
+    // the entry point is invoked after reloading. following code only run in RESERVED mem\r
     //\r
 \r
     //\r