FspNotifyDxe need handle >4G memory.
authorYao, Jiewen <Jiewen.Yao@intel.com>
Tue, 28 Jul 2015 07:26:57 +0000 (07:26 +0000)
committerjyao1 <jyao1@Edk2>
Tue, 28 Jul 2015 07:26:57 +0000 (07:26 +0000)
The FSP API is always 32bit, but FspNotifyDxe might load to >4G memory. In order to make thunk work, we need reload FspNotifyDxe to <4G memory.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: "Yao, Jiewen" <Jiewen.Yao@intel.com>
Reviewed-by: "Mudusuru, Giri P" <giri.p.mudusuru@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18094 6f19259b-4bc3-4df7-8a09-765794883524

IntelFspWrapperPkg/FspNotifyDxe/FspNotifyDxe.c
IntelFspWrapperPkg/FspNotifyDxe/FspNotifyDxe.inf
IntelFspWrapperPkg/FspNotifyDxe/LoadBelow4G.c [new file with mode: 0644]
IntelFspWrapperPkg/IntelFspWrapperPkg.dsc

index f8e8e82..1a1e4e6 100644 (file)
 #include <Library/UefiLib.h>\r
 #include <Library/FspApiLib.h>\r
 \r
 #include <Library/UefiLib.h>\r
 #include <Library/FspApiLib.h>\r
 \r
+/**\r
+  Relocate this image under 4G memory.\r
+\r
+  @param  ImageHandle  Handle of driver image.\r
+  @param  SystemTable  Pointer to system table.\r
+\r
+  @retval EFI_SUCCESS  Image successfully relocated.\r
+  @retval EFI_ABORTED  Failed to relocate image.\r
+\r
+**/\r
+EFI_STATUS\r
+RelocateImageUnder4GIfNeeded (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  );\r
+\r
 FSP_INFO_HEADER *mFspHeader = NULL;\r
 \r
 /**\r
 FSP_INFO_HEADER *mFspHeader = NULL;\r
 \r
 /**\r
@@ -120,6 +136,14 @@ FspDxeEntryPoint (
   VOID       *Registration;\r
   EFI_EVENT  ProtocolNotifyEvent;\r
 \r
   VOID       *Registration;\r
   EFI_EVENT  ProtocolNotifyEvent;\r
 \r
+  //\r
+  // Load this driver's image to memory\r
+  //\r
+  Status = RelocateImageUnder4GIfNeeded (ImageHandle, SystemTable);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   if (PcdGet32 (PcdFlashFvSecondFspBase) == 0) {\r
     mFspHeader = FspFindFspHeader (PcdGet32 (PcdFlashFvFspBase));\r
   } else {\r
   if (PcdGet32 (PcdFlashFvSecondFspBase) == 0) {\r
     mFspHeader = FspFindFspHeader (PcdGet32 (PcdFlashFvFspBase));\r
   } else {\r
index 8175dbd..66970cc 100644 (file)
@@ -30,6 +30,7 @@
 \r
 [Sources]\r
   FspNotifyDxe.c\r
 \r
 [Sources]\r
   FspNotifyDxe.c\r
+  LoadBelow4G.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
@@ -43,6 +44,9 @@
   BaseMemoryLib\r
   UefiLib\r
   FspApiLib\r
   BaseMemoryLib\r
   UefiLib\r
   FspApiLib\r
+  PeCoffLib\r
+  CacheMaintenanceLib\r
+  DxeServicesLib\r
 \r
 [Protocols]\r
   gEfiPciEnumerationCompleteProtocolGuid            ## CONSUMES\r
 \r
 [Protocols]\r
   gEfiPciEnumerationCompleteProtocolGuid            ## CONSUMES\r
diff --git a/IntelFspWrapperPkg/FspNotifyDxe/LoadBelow4G.c b/IntelFspWrapperPkg/FspNotifyDxe/LoadBelow4G.c
new file mode 100644 (file)
index 0000000..d39164e
--- /dev/null
@@ -0,0 +1,152 @@
+/** @file\r
+\r
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions\r
+of the BSD License which accompanies this distribution.  The\r
+full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PeCoffLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DxeServicesLib.h>\r
+#include <Library/CacheMaintenanceLib.h>\r
+#include <Library/UefiLib.h>\r
+\r
+/**\r
+  Relocate this image under 4G memory.\r
+\r
+  @param  ImageHandle  Handle of driver image.\r
+  @param  SystemTable  Pointer to system table.\r
+\r
+  @retval EFI_SUCCESS  Image successfully relocated.\r
+  @retval EFI_ABORTED  Failed to relocate image.\r
+\r
+**/\r
+EFI_STATUS\r
+RelocateImageUnder4GIfNeeded (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS                                    Status;\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
+  VOID                                          *Interface;\r
+\r
+  //\r
+  // If it is already <4G, no need do relocate\r
+  //\r
+  if ((UINTN)RelocateImageUnder4GIfNeeded < 0xFFFFFFFF) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // If locate gEfiCallerIdGuid success, it means 2nd entry.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &Interface);\r
+  if (!EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_INFO, "FspNotifyDxe - 2nd entry\n"));\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  DEBUG ((EFI_D_INFO, "FspNotifyDxe - 1st entry\n"));\r
+\r
+  //\r
+  // 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 image itself to <4G 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
+                  EfiBootServicesCode,\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 &= ~((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
+  DEBUG ((EFI_D_INFO, "Loading driver at 0x%08x EntryPoint=0x%08x\n", (UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.EntryPoint));\r
+  Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, gST);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "Error: Image at 0x%08x start failed: %r\n", ImageContext.ImageAddress, Status));\r
+    gBS->FreePages (FfsBuffer, Pages);\r
+  }\r
+\r
+  //\r
+  // return error to unload >4G copy, if we already relocate itself to <4G.\r
+  //\r
+  return EFI_ALREADY_STARTED;\r
+}\r
index dbd05a1..04677c3 100644 (file)
@@ -34,6 +34,7 @@
   PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf\r
   PeCoffExtraActionLib|MdePkg/Library/BasePeCoffExtraActionLibNull/BasePeCoffExtraActionLibNull.inf\r
   PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf\r
   PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf\r
   PeCoffExtraActionLib|MdePkg/Library/BasePeCoffExtraActionLibNull/BasePeCoffExtraActionLibNull.inf\r
   PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf\r
+  CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf\r
 \r
   # Dummy - test build only\r
   SerialPortLib|MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf\r
 \r
   # Dummy - test build only\r
   SerialPortLib|MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf\r
@@ -66,6 +67,7 @@
   UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf\r
   UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf\r
   UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf\r
   UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf\r
   UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf\r
   UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf\r
+  DxeServicesLib|MdePkg/Library/DxeServicesLib/DxeServicesLib.inf\r
   UefiLib|MdePkg/Library/UefiLib/UefiLib.inf\r
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf\r
   DevicePathLib|MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf\r
   UefiLib|MdePkg/Library/UefiLib/UefiLib.inf\r
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf\r
   DevicePathLib|MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf\r