2 Pei Core Load Image Support
4 Copyright (c) 2006 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 The wrapper function of PeiLoadImageLoadImage().
20 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
21 @param FileHandle - Pointer to the FFS file header of the image.
22 @param ImageAddressArg - Pointer to PE/TE image.
23 @param ImageSizeArg - Size of PE/TE image.
24 @param EntryPoint - Pointer to entry point of specified image file for output.
25 @param AuthenticationState - Pointer to attestation authentication state of image.
27 @return Status of PeiLoadImageLoadImage().
32 PeiLoadImageLoadImageWrapper (
33 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
34 IN EFI_PEI_FILE_HANDLE FileHandle
,
35 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
36 OUT UINT64
*ImageSizeArg
, OPTIONAL
37 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
38 OUT UINT32
*AuthenticationState
41 STATIC EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi
= {
42 PeiLoadImageLoadImageWrapper
46 STATIC EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList
= {
47 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
48 &gEfiPeiLoadFilePpiGuid
,
54 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
57 @param FileHandle - The handle to the PE/COFF file
58 @param FileOffset - The offset, in bytes, into the file to read
59 @param ReadSize - The number of bytes to read from the file starting at FileOffset
60 @param Buffer - A pointer to the buffer to read the data into.
62 @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
78 Destination8
= Buffer
;
79 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
81 while ((Length
--) > 0) {
82 *(Destination8
++) = *(Source8
++);
90 Support routine to get the Image read file function.
92 @param ImageContext - The context of the image being loaded
94 @retval EFI_SUCCESS - If Image function location is found
98 GetImageReadFunction (
99 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
104 MemoryBuffer
= AllocatePages (0x400 / EFI_PAGE_SIZE
+ 1);
105 ASSERT (MemoryBuffer
!= NULL
);
107 CopyMem (MemoryBuffer
, (CONST VOID
*) (UINTN
) PeiImageRead
, 0x400);
109 ImageContext
->ImageRead
= (PE_COFF_LOADER_READ_FILE
) (UINTN
) MemoryBuffer
;
116 Loads and relocates a PE/COFF image into memory.
119 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
120 @param ImageAddress - The base address of the relocated PE/COFF image
121 @param ImageSize - The size of the relocated PE/COFF image
122 @param EntryPoint - The entry point of the relocated PE/COFF image
124 @retval EFI_SUCCESS The file was loaded and relocated
125 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
126 @retval EFI_INVALID_PARAMETER The image withou .reloc section can't be relocated.
130 LoadAndRelocatePeCoffImage (
132 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
133 OUT UINT64
*ImageSize
,
134 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
138 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
140 ZeroMem (&ImageContext
, sizeof (ImageContext
));
141 ImageContext
.Handle
= Pe32Data
;
142 Status
= GetImageReadFunction (&ImageContext
);
144 ASSERT_EFI_ERROR (Status
);
146 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
147 if (EFI_ERROR (Status
)) {
151 // When Image has no reloc section, it can't be relocated into memory.
153 if (ImageContext
.RelocationsStripped
) {
154 DEBUG ((EFI_D_ERROR
, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN
) Pe32Data
));
155 return EFI_INVALID_PARAMETER
;
158 // Allocate Memory for the image
160 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) ImageContext
.ImageSize
));
161 ASSERT (ImageContext
.ImageAddress
!= 0);
164 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.
166 if (ImageContext
.IsTeImage
) {
167 ImageContext
.ImageAddress
= ImageContext
.ImageAddress
+
168 ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
-
169 sizeof (EFI_TE_IMAGE_HEADER
);
173 // Load the image to our new buffer
175 Status
= PeCoffLoaderLoadImage (&ImageContext
);
176 if (EFI_ERROR (Status
)) {
180 // Relocate the image in our new buffer
182 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
183 if (EFI_ERROR (Status
)) {
188 // Flush the instruction cache so the image data is written before we execute it
190 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
192 *ImageAddress
= ImageContext
.ImageAddress
;
193 *ImageSize
= ImageContext
.ImageSize
;
194 *EntryPoint
= ImageContext
.EntryPoint
;
200 Loads a PEIM into memory for subsequent execution. If there are compressed
201 images or images that need to be relocated into memory for performance reasons,
202 this service performs that transformation.
204 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
205 @param FileHandle Pointer to the FFS file header of the image.
206 @param ImageAddressArg Pointer to PE/TE image.
207 @param ImageSizeArg Size of PE/TE image.
208 @param EntryPoint Pointer to entry point of specified image file for output.
209 @param AuthenticationState - Pointer to attestation authentication state of image.
211 @retval EFI_SUCCESS Image is successfully loaded.
212 @retval EFI_NOT_FOUND Fail to locate necessary PPI.
213 @retval EFI_UNSUPPORTED Image Machine Type is not supported.
217 PeiLoadImageLoadImage (
218 IN CONST EFI_PEI_SERVICES
**PeiServices
,
219 IN EFI_PEI_FILE_HANDLE FileHandle
,
220 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
221 OUT UINT64
*ImageSizeArg
, OPTIONAL
222 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
223 OUT UINT32
*AuthenticationState
228 EFI_PHYSICAL_ADDRESS ImageAddress
;
230 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
232 PEI_CORE_INSTANCE
*Private
;
234 EFI_SECTION_TYPE SearchType1
;
235 EFI_SECTION_TYPE SearchType2
;
239 *AuthenticationState
= 0;
241 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst
)) {
242 SearchType1
= EFI_SECTION_TE
;
243 SearchType2
= EFI_SECTION_PE32
;
245 SearchType1
= EFI_SECTION_PE32
;
246 SearchType2
= EFI_SECTION_TE
;
249 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
250 // is true, TE will be searched first).
252 Status
= PeiServicesFfsFindSectionData (
258 // If we didn't find a first exe section, try to find the second exe section.
260 if (EFI_ERROR (Status
)) {
261 Status
= PeiServicesFfsFindSectionData (
266 if (EFI_ERROR (Status
)) {
268 // PEI core only carry the loader function fro TE and PE32 executables
269 // If this two section does not exist, just return.
275 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
277 if (Private
->PeiMemoryInstalled
&&
278 (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
280 // If memory is installed, perform the shadow operations
282 Status
= LoadAndRelocatePeCoffImage (
289 if (EFI_ERROR (Status
)) {
294 // Got the entry point from the loaded Pe32Data
296 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
297 *EntryPoint
= ImageEntryPoint
;
300 // Retrieve the entry point from the PE/COFF or TE image header
302 ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Pe32Data
;
303 Status
= PeCoffLoaderGetEntryPoint (Pe32Data
, &EntryPointArg
);
304 if (EFI_ERROR (Status
)) {
307 *EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) EntryPointArg
;
310 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
312 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
313 return EFI_UNSUPPORTED
;
316 if (ImageAddressArg
!= NULL
) {
317 *ImageAddressArg
= ImageAddress
;
320 if (ImageSizeArg
!= NULL
) {
321 *ImageSizeArg
= ImageSize
;
326 CHAR8 AsciiBuffer
[512];
331 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
333 if (Machine
!= IMAGE_FILE_MACHINE_IA64
) {
334 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)*EntryPoint
));
337 // For IPF Image, the real entry point should be print.
339 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)*EntryPoint
)));
343 // Print Module Name by PeImage PDB file name.
345 AsciiString
= PeCoffLoaderGetPdbPointer (Pe32Data
);
347 if (AsciiString
!= NULL
) {
348 for (Index
= (INT32
) AsciiStrLen (AsciiString
) - 1; Index
>= 0; Index
--) {
349 if (AsciiString
[Index
] == '\\') {
355 for (Index1
= 0; AsciiString
[Index
+ 1 + Index1
] != '.'; Index1
++) {
356 AsciiBuffer
[Index1
] = AsciiString
[Index
+ 1 + Index1
];
358 AsciiBuffer
[Index1
] = '\0';
359 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a.efi", AsciiBuffer
));
365 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
373 The wrapper function of PeiLoadImageLoadImage().
375 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
376 @param FileHandle - Pointer to the FFS file header of the image.
377 @param ImageAddressArg - Pointer to PE/TE image.
378 @param ImageSizeArg - Size of PE/TE image.
379 @param EntryPoint - Pointer to entry point of specified image file for output.
380 @param AuthenticationState - Pointer to attestation authentication state of image.
382 @return Status of PeiLoadImageLoadImage().
387 PeiLoadImageLoadImageWrapper (
388 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
389 IN EFI_PEI_FILE_HANDLE FileHandle
,
390 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
391 OUT UINT64
*ImageSizeArg
, OPTIONAL
392 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
393 OUT UINT32
*AuthenticationState
396 return PeiLoadImageLoadImage (
397 GetPeiServicesTablePointer (),
407 Routine to load image file for subsequent execution by LoadFile Ppi.
408 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE
409 XIP image format is used.
411 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
412 @param FileHandle - Pointer to the FFS file header of the image.
413 @param EntryPoint - Pointer to entry point of specified image file for output.
414 @param AuthenticationState - Pointer to attestation authentication state of image.
416 @retval EFI_SUCCESS - Image is successfully loaded.
417 @retval EFI_NOT_FOUND - Fail to locate necessary PPI
418 @retval Others - Fail to load file.
423 IN CONST EFI_PEI_SERVICES
**PeiServices
,
424 IN EFI_PEI_FILE_HANDLE FileHandle
,
425 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
426 OUT UINT32
*AuthenticationState
429 EFI_STATUS PpiStatus
;
432 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
433 EFI_PHYSICAL_ADDRESS ImageAddress
;
437 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
438 // one at a time, until one reports EFI_SUCCESS.
442 PpiStatus
= PeiServicesLocatePpi (
443 &gEfiPeiLoadFilePpiGuid
,
448 if (!EFI_ERROR (PpiStatus
)) {
449 Status
= LoadFile
->LoadFile (
457 if (!EFI_ERROR (Status
)) {
462 } while (!EFI_ERROR (PpiStatus
));
465 // If no instances reports EFI_SUCCESS, then build-in support for
466 // the PE32+/TE XIP image format is used.
468 Status
= PeiLoadImageLoadImage (
482 Install Pei Load File PPI.
485 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
486 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.
490 InitializeImageServices (
491 IN PEI_CORE_INSTANCE
*PrivateData
,
492 IN PEI_CORE_INSTANCE
*OldCoreData
495 if (OldCoreData
== NULL
) {
497 // The first time we are XIP (running from FLASH). We need to remember the
498 // FLASH address so we can reinstall the memory version that runs faster
500 PrivateData
->XipLoadFile
= &gPpiLoadFilePpiList
;
501 PeiServicesInstallPpi (PrivateData
->XipLoadFile
);
504 // 2nd time we are running from memory so replace the XIP version with the
505 // new memory version.
507 PeiServicesReInstallPpi (PrivateData
->XipLoadFile
, &gPpiLoadFilePpiList
);