/** @file\r
Pei Core Load Image Support\r
\r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 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
\r
Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
\r
- if ((Private->PeiMemoryInstalled && !(Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME)) &&\r
+ if (Private->PeiMemoryInstalled && (((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) && PcdGetBool (PcdShadowPeimOnBoot)) || \r
+ ((Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) && PcdGetBool (PcdShadowPeimOnS3Boot))) &&\r
(EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_X64) || EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_IA32))) {\r
// \r
// Shadow algorithm makes lots of non ANSI C assumptions and only works for IA32 and X64 \r
Loads and relocates a PE/COFF image into memory.\r
If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r
\r
+ @param FileHandle - Pointer to the FFS file header of the image.\r
@param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
@param ImageAddress - The base address of the relocated PE/COFF image\r
@param ImageSize - The size of the relocated PE/COFF image\r
\r
@retval EFI_SUCCESS The file was loaded and relocated\r
@retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r
+ @retval EFI_WARN_BUFFER_TOO_SMALL \r
+ There is not enough heap to allocate the requested size.\r
+ This will not prevent the XIP image from being invoked.\r
\r
**/\r
EFI_STATUS\r
LoadAndRelocatePeCoffImage (\r
+ IN EFI_PEI_FILE_HANDLE FileHandle,\r
IN VOID *Pe32Data,\r
OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
OUT UINT64 *ImageSize,\r
EFI_STATUS Status;\r
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
PEI_CORE_INSTANCE *Private;\r
+ UINT64 AlignImageSize;\r
+ BOOLEAN IsXipImage;\r
+ EFI_STATUS ReturnStatus;\r
+ BOOLEAN IsS3Boot;\r
+ BOOLEAN IsRegisterForShadow;\r
\r
Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
\r
+ ReturnStatus = EFI_SUCCESS;\r
+ IsXipImage = FALSE;\r
ZeroMem (&ImageContext, sizeof (ImageContext));\r
ImageContext.Handle = Pe32Data;\r
Status = GetImageReadFunction (&ImageContext);\r
if (EFI_ERROR (Status)) {\r
return Status;\r
}\r
+ \r
+ //\r
+ // Initilize local IsS3Boot and IsRegisterForShadow variable\r
+ //\r
+ IsS3Boot = FALSE;\r
+ if (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) {\r
+ IsS3Boot = TRUE;\r
+ }\r
+ IsRegisterForShadow = FALSE;\r
+ if ((Private->CurrentFileHandle == FileHandle) \r
+ && (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW)) {\r
+ IsRegisterForShadow = TRUE;\r
+ }\r
+\r
+ //\r
+ // XIP image that ImageAddress is same to Image handle.\r
+ //\r
+ if (ImageContext.ImageAddress == (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
+ IsXipImage = TRUE;\r
+ }\r
+\r
//\r
// When Image has no reloc section, it can't be relocated into memory.\r
//\r
- if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
+ if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (\r
+ (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
DEBUG ((EFI_D_INFO|EFI_D_LOAD, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r
}\r
\r
ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
\r
//\r
- // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.\r
+ // Allocate Memory for the image when memory is ready, and image is relocatable.\r
+ // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.\r
+ // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.\r
//\r
- if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
- if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+ if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (\r
+ (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
+ //\r
+ // Allocate more buffer to avoid buffer overflow.\r
+ //\r
+ if (ImageContext.IsTeImage) {\r
+ AlignImageSize = ImageContext.ImageSize + ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
+ } else {\r
+ AlignImageSize = ImageContext.ImageSize;\r
+ }\r
+\r
+ if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
+ AlignImageSize += ImageContext.SectionAlignment;\r
+ }\r
+\r
+ if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
if (EFI_ERROR (Status)){\r
DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
//\r
// The PEIM is not assiged valid address, try to allocate page to load it.\r
//\r
- ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
+ ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\r
}\r
} else {\r
- ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
- }\r
- ASSERT (ImageContext.ImageAddress != 0);\r
- if (ImageContext.ImageAddress == 0) {\r
- return EFI_OUT_OF_RESOURCES;\r
+ ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\r
}\r
-\r
- //\r
- // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
- //\r
- if (ImageContext.IsTeImage) {\r
- ImageContext.ImageAddress = ImageContext.ImageAddress +\r
- ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
- sizeof (EFI_TE_IMAGE_HEADER);\r
+ if (ImageContext.ImageAddress != 0) {\r
+ //\r
+ // Adjust the Image Address to make sure it is section alignment.\r
+ //\r
+ if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
+ ImageContext.ImageAddress =\r
+ (ImageContext.ImageAddress + ImageContext.SectionAlignment - 1) &\r
+ ~((UINTN)ImageContext.SectionAlignment - 1);\r
+ }\r
+ //\r
+ // Fix alignment requirement when Load IPF TeImage into memory.\r
+ // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
+ //\r
+ if (ImageContext.IsTeImage) {\r
+ ImageContext.ImageAddress = ImageContext.ImageAddress +\r
+ ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
+ sizeof (EFI_TE_IMAGE_HEADER);\r
+ }\r
+ } else {\r
+ //\r
+ // No enough memory resource.\r
+ //\r
+ if (IsXipImage) {\r
+ //\r
+ // XIP image can still be invoked.\r
+ //\r
+ ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
+ ReturnStatus = EFI_WARN_BUFFER_TOO_SMALL;\r
+ } else {\r
+ //\r
+ // Non XIP image can't be loaded because no enough memory is allocated.\r
+ //\r
+ ASSERT (FALSE);\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
}\r
}\r
\r
//\r
// Flush the instruction cache so the image data is written before we execute it\r
//\r
- if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
+ if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
}\r
\r
*ImageSize = ImageContext.ImageSize;\r
*EntryPoint = ImageContext.EntryPoint;\r
\r
- return EFI_SUCCESS;\r
+ return ReturnStatus;\r
}\r
\r
/**\r
@retval EFI_SUCCESS Image is successfully loaded.\r
@retval EFI_NOT_FOUND Fail to locate necessary PPI.\r
@retval EFI_UNSUPPORTED Image Machine Type is not supported.\r
+ @retval EFI_WARN_BUFFER_TOO_SMALL \r
+ There is not enough heap to allocate the requested size.\r
+ This will not prevent the XIP image from being invoked.\r
\r
**/\r
EFI_STATUS\r
// Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
// is true, TE will be searched first).\r
//\r
- Status = PeiServicesFfsFindSectionData (\r
+ Status = PeiServicesFfsFindSectionData3 (\r
SearchType1,\r
+ 0,\r
FileHandle,\r
- &Pe32Data\r
+ &Pe32Data,\r
+ AuthenticationState\r
);\r
//\r
// If we didn't find a first exe section, try to find the second exe section.\r
//\r
if (EFI_ERROR (Status)) {\r
- Status = PeiServicesFfsFindSectionData (\r
+ Status = PeiServicesFfsFindSectionData3 (\r
SearchType2,\r
+ 0,\r
FileHandle,\r
- &Pe32Data\r
+ &Pe32Data,\r
+ AuthenticationState\r
);\r
if (EFI_ERROR (Status)) {\r
//\r
- // PEI core only carry the loader function fro TE and PE32 executables\r
+ // PEI core only carry the loader function for TE and PE32 executables\r
// If this two section does not exist, just return.\r
//\r
return Status;\r
// If memory is installed, perform the shadow operations\r
//\r
Status = LoadAndRelocatePeCoffImage (\r
+ FileHandle,\r
Pe32Data,\r
&ImageAddress,\r
&ImageSize,\r
\r
DEBUG_CODE_BEGIN ();\r
CHAR8 *AsciiString;\r
- CHAR8 AsciiBuffer[512];\r
+ CHAR8 EfiFileName[512];\r
INT32 Index;\r
- INT32 Index1;\r
+ INT32 StartIndex;\r
\r
//\r
// Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
\r
if (AsciiString != NULL) {\r
- for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
- if (AsciiString[Index] == '\\') {\r
- break;\r
+ StartIndex = 0;\r
+ for (Index = 0; AsciiString[Index] != 0; Index++) {\r
+ if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
+ StartIndex = Index + 1;\r
}\r
}\r
\r
- if (Index != 0) {\r
- for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
- AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r
+ //\r
+ // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
+ // The PDB file name is limited in the range of 0~511.\r
+ // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.\r
+ //\r
+ for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
+ EfiFileName[Index] = AsciiString[Index + StartIndex];\r
+ if (EfiFileName[Index] == 0) {\r
+ EfiFileName[Index] = '.';\r
+ }\r
+ if (EfiFileName[Index] == '.') {\r
+ EfiFileName[Index + 1] = 'e';\r
+ EfiFileName[Index + 2] = 'f';\r
+ EfiFileName[Index + 3] = 'i';\r
+ EfiFileName[Index + 4] = 0;\r
+ break;\r
}\r
- AsciiBuffer [Index1] = '\0';\r
- DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
}\r
+\r
+ if (Index == sizeof (EfiFileName) - 4) {\r
+ EfiFileName[Index] = 0;\r
+ }\r
+\r
+ DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r
}\r
\r
DEBUG_CODE_END ();\r
EntryPoint,\r
AuthenticationState\r
);\r
- if (!EFI_ERROR (Status)) {\r
+ if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
//\r
// The shadowed PEIM must be relocatable.\r
//\r
if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
return EFI_UNSUPPORTED;\r
}\r
- return Status;\r
+ return EFI_SUCCESS;\r
}\r
}\r
Index++;\r