#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
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
--- /dev/null
+/** @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