2 Pei Core Load Image Support
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 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 EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi
= {
19 PeiLoadImageLoadImageWrapper
23 EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList
= {
24 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
25 &gEfiPeiLoadFilePpiGuid
,
31 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file.
32 The function is used for XIP code to have optimized memory copy.
34 @param FileHandle - The handle to the PE/COFF file
35 @param FileOffset - The offset, in bytes, into the file to read
36 @param ReadSize - The number of bytes to read from the file starting at FileOffset
37 @param Buffer - A pointer to the buffer to read the data into.
39 @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
54 Destination8
= Buffer
;
55 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
56 if (Destination8
!= Source8
) {
57 CopyMem (Destination8
, Source8
, *ReadSize
);
65 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file.
66 The function is implemented as PIC so as to support shadowing.
68 @param FileHandle - The handle to the PE/COFF file
69 @param FileOffset - The offset, in bytes, into the file to read
70 @param ReadSize - The number of bytes to read from the file starting at FileOffset
71 @param Buffer - A pointer to the buffer to read the data into.
73 @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
78 PeiImageReadForShadow (
85 volatile CHAR8
*Destination8
;
89 Destination8
= Buffer
;
90 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
91 if (Destination8
!= Source8
) {
93 while ((Length
--) > 0) {
94 *(Destination8
++) = *(Source8
++);
103 Support routine to get the Image read file function.
105 @param ImageContext - The context of the image being loaded
107 @retval EFI_SUCCESS - If Image function location is found
111 GetImageReadFunction (
112 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
115 PEI_CORE_INSTANCE
*Private
;
118 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
120 if ((Private
->PeiMemoryInstalled
&& !(Private
->HobList
.HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
)) &&
121 (EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_X64
) || EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_IA32
))) {
123 // Shadow algorithm makes lots of non ANSI C assumptions and only works for IA32 and X64
124 // compilers that have been tested
126 if (Private
->ShadowedImageRead
== NULL
) {
127 MemoryBuffer
= AllocatePages (0x400 / EFI_PAGE_SIZE
+ 1);
128 ASSERT (MemoryBuffer
!= NULL
);
129 CopyMem (MemoryBuffer
, (CONST VOID
*) (UINTN
) PeiImageReadForShadow
, 0x400);
130 Private
->ShadowedImageRead
= (PE_COFF_LOADER_READ_FILE
) (UINTN
) MemoryBuffer
;
133 ImageContext
->ImageRead
= Private
->ShadowedImageRead
;
135 ImageContext
->ImageRead
= PeiImageRead
;
141 To check memory usage bit map arry to figure out if the memory range the image will be loaded in is available or not. If
142 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
143 The function is only invoked when load modules at fixed address feature is enabled.
145 @param Private Pointer to the private data passed in from caller
146 @param ImageBase The base addres the image will be loaded at.
147 @param ImageSize The size of the image
149 @retval EFI_SUCCESS The memory range the image will be loaded in is available
150 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
153 CheckAndMarkFixLoadingMemoryUsageBitMap (
154 IN PEI_CORE_INSTANCE
*Private
,
155 IN EFI_PHYSICAL_ADDRESS ImageBase
,
159 UINT32 DxeCodePageNumber
;
160 UINT64 ReservedCodeSize
;
161 EFI_PHYSICAL_ADDRESS PeiCodeBase
;
162 UINT32 BaseOffsetPageNumber
;
163 UINT32 TopOffsetPageNumber
;
165 UINT64
*MemoryUsageBitMap
;
169 // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.
171 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
172 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
173 ReservedCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
+ PcdGet32(PcdLoadFixAddressPeiCodePageNumber
));
174 PeiCodeBase
= Private
->LoadModuleAtFixAddressTopAddress
- ReservedCodeSize
;
177 // Test the memory range for loading the image in the PEI code range.
179 if ((Private
->LoadModuleAtFixAddressTopAddress
- EFI_PAGES_TO_SIZE(DxeCodePageNumber
)) < (ImageBase
+ ImageSize
) ||
180 (PeiCodeBase
> ImageBase
)) {
181 return EFI_NOT_FOUND
;
185 // Test if the memory is avalaible or not.
187 MemoryUsageBitMap
= Private
->PeiCodeMemoryRangeUsageBitMap
;
188 BaseOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- PeiCodeBase
));
189 TopOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- PeiCodeBase
));
190 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
191 if ((MemoryUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
193 // This page is already used.
195 return EFI_NOT_FOUND
;
200 // Being here means the memory range is available. So mark the bits for the memory range
202 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
203 MemoryUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
209 Get the fixed loadding address from image header assigned by build tool. This function only be called
210 when Loading module at Fixed address feature enabled.
212 @param ImageContext Pointer to the image context structure that describes the PE/COFF
213 image that needs to be examined by this function.
214 @param Private Pointer to the private data passed in from caller
216 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
217 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
221 GetPeCoffImageFixLoadingAssignedAddress(
222 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
223 IN PEI_CORE_INSTANCE
*Private
226 UINTN SectionHeaderOffset
;
228 EFI_IMAGE_SECTION_HEADER SectionHeader
;
229 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
230 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
233 UINT16 NumberOfSections
;
234 UINT64 ValueInSectionHeader
;
237 FixLoaddingAddress
= 0;
238 Status
= EFI_NOT_FOUND
;
241 // Get PeHeader pointer
243 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
244 if (ImageContext
->IsTeImage
) {
246 // for TE image, the fix loadding address is saved in first section header that doesn't point
249 SectionHeaderOffset
= sizeof (EFI_TE_IMAGE_HEADER
);
250 NumberOfSections
= ImgHdr
->Te
.NumberOfSections
;
252 SectionHeaderOffset
= (UINTN
)(
253 ImageContext
->PeCoffHeaderOffset
+
255 sizeof (EFI_IMAGE_FILE_HEADER
) +
256 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
258 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
261 // Get base address from the first section header that doesn't point to code section.
263 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
265 // Read section header from file
267 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
268 Status
= ImageContext
->ImageRead (
269 ImageContext
->Handle
,
274 if (EFI_ERROR (Status
)) {
278 Status
= EFI_NOT_FOUND
;
280 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
282 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
283 // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is
284 // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because
285 // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers
286 // hold the image base address when it is shadow to the memory. And there is an assumption that when the feature is enabled, if a
287 // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or
288 // else, these 2 fileds should be set to Zero
290 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
291 if (ValueInSectionHeader
!= 0) {
293 // Found first section header that doesn't point to code section.
295 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) > 0) {
297 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field
298 // hold the absolute address of image base runing in memory
300 FixLoaddingAddress
= ValueInSectionHeader
;
303 // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field
304 // hold the offset relative to a platform-specific top address.
306 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(Private
->LoadModuleAtFixAddressTopAddress
+ (INT64
)ValueInSectionHeader
);
309 // Check if the memory range is avaliable.
311 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (Private
, FixLoaddingAddress
, (UINT32
) ImageContext
->ImageSize
);
312 if (!EFI_ERROR(Status
)) {
314 // The assigned address is valid. Return the specified loadding address
316 ImageContext
->ImageAddress
= FixLoaddingAddress
;
321 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
323 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status= %r \n", (VOID
*)(UINTN
)FixLoaddingAddress
, Status
));
328 Loads and relocates a PE/COFF image into memory.
329 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.
331 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
332 @param ImageAddress - The base address of the relocated PE/COFF image
333 @param ImageSize - The size of the relocated PE/COFF image
334 @param EntryPoint - The entry point of the relocated PE/COFF image
336 @retval EFI_SUCCESS The file was loaded and relocated
337 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
341 LoadAndRelocatePeCoffImage (
343 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
344 OUT UINT64
*ImageSize
,
345 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
349 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
350 PEI_CORE_INSTANCE
*Private
;
352 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
354 ZeroMem (&ImageContext
, sizeof (ImageContext
));
355 ImageContext
.Handle
= Pe32Data
;
356 Status
= GetImageReadFunction (&ImageContext
);
358 ASSERT_EFI_ERROR (Status
);
360 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
361 if (EFI_ERROR (Status
)) {
365 // When Image has no reloc section, it can't be relocated into memory.
367 if (ImageContext
.RelocationsStripped
&& (Private
->PeiMemoryInstalled
) && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
368 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN
) Pe32Data
));
372 // Set default base address to current image address.
374 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
377 // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.
379 if ((!ImageContext
.RelocationsStripped
) && (Private
->PeiMemoryInstalled
) && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
380 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
381 Status
= GetPeCoffImageFixLoadingAssignedAddress(&ImageContext
, Private
);
382 if (EFI_ERROR (Status
)){
383 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
385 // The PEIM is not assiged valid address, try to allocate page to load it.
387 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) ImageContext
.ImageSize
));
390 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) ImageContext
.ImageSize
));
392 ASSERT (ImageContext
.ImageAddress
!= 0);
393 if (ImageContext
.ImageAddress
== 0) {
394 return EFI_OUT_OF_RESOURCES
;
398 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.
400 if (ImageContext
.IsTeImage
) {
401 ImageContext
.ImageAddress
= ImageContext
.ImageAddress
+
402 ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
-
403 sizeof (EFI_TE_IMAGE_HEADER
);
408 // Load the image to our new buffer
410 Status
= PeCoffLoaderLoadImage (&ImageContext
);
411 if (EFI_ERROR (Status
)) {
415 // Relocate the image in our new buffer
417 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
418 if (EFI_ERROR (Status
)) {
423 // Flush the instruction cache so the image data is written before we execute it
425 if ((!ImageContext
.RelocationsStripped
) && (Private
->PeiMemoryInstalled
) && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
426 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
429 *ImageAddress
= ImageContext
.ImageAddress
;
430 *ImageSize
= ImageContext
.ImageSize
;
431 *EntryPoint
= ImageContext
.EntryPoint
;
437 Loads a PEIM into memory for subsequent execution. If there are compressed
438 images or images that need to be relocated into memory for performance reasons,
439 this service performs that transformation.
441 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
442 @param FileHandle Pointer to the FFS file header of the image.
443 @param ImageAddressArg Pointer to PE/TE image.
444 @param ImageSizeArg Size of PE/TE image.
445 @param EntryPoint Pointer to entry point of specified image file for output.
446 @param AuthenticationState - Pointer to attestation authentication state of image.
448 @retval EFI_SUCCESS Image is successfully loaded.
449 @retval EFI_NOT_FOUND Fail to locate necessary PPI.
450 @retval EFI_UNSUPPORTED Image Machine Type is not supported.
454 PeiLoadImageLoadImage (
455 IN CONST EFI_PEI_SERVICES
**PeiServices
,
456 IN EFI_PEI_FILE_HANDLE FileHandle
,
457 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
458 OUT UINT64
*ImageSizeArg
, OPTIONAL
459 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
460 OUT UINT32
*AuthenticationState
465 EFI_PHYSICAL_ADDRESS ImageAddress
;
467 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
469 EFI_SECTION_TYPE SearchType1
;
470 EFI_SECTION_TYPE SearchType2
;
474 *AuthenticationState
= 0;
476 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst
)) {
477 SearchType1
= EFI_SECTION_TE
;
478 SearchType2
= EFI_SECTION_PE32
;
480 SearchType1
= EFI_SECTION_PE32
;
481 SearchType2
= EFI_SECTION_TE
;
485 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
486 // is true, TE will be searched first).
488 Status
= PeiServicesFfsFindSectionData (
494 // If we didn't find a first exe section, try to find the second exe section.
496 if (EFI_ERROR (Status
)) {
497 Status
= PeiServicesFfsFindSectionData (
502 if (EFI_ERROR (Status
)) {
504 // PEI core only carry the loader function fro TE and PE32 executables
505 // If this two section does not exist, just return.
512 // If memory is installed, perform the shadow operations
514 Status
= LoadAndRelocatePeCoffImage (
521 ASSERT_EFI_ERROR (Status
);
524 if (EFI_ERROR (Status
)) {
529 // Got the entry point from the loaded Pe32Data
531 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
532 *EntryPoint
= ImageEntryPoint
;
534 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
536 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
537 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine
)) {
538 return EFI_UNSUPPORTED
;
542 if (ImageAddressArg
!= NULL
) {
543 *ImageAddressArg
= ImageAddress
;
546 if (ImageSizeArg
!= NULL
) {
547 *ImageSizeArg
= ImageSize
;
552 CHAR8 AsciiBuffer
[512];
557 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
559 if (Machine
!= EFI_IMAGE_MACHINE_IA64
) {
560 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)*EntryPoint
));
563 // For IPF Image, the real entry point should be print.
565 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)*EntryPoint
)));
569 // Print Module Name by PeImage PDB file name.
571 AsciiString
= PeCoffLoaderGetPdbPointer (Pe32Data
);
573 if (AsciiString
!= NULL
) {
574 for (Index
= (INT32
) AsciiStrLen (AsciiString
) - 1; Index
>= 0; Index
--) {
575 if (AsciiString
[Index
] == '\\') {
581 for (Index1
= 0; AsciiString
[Index
+ 1 + Index1
] != '.'; Index1
++) {
582 AsciiBuffer
[Index1
] = AsciiString
[Index
+ 1 + Index1
];
584 AsciiBuffer
[Index1
] = '\0';
585 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a.efi", AsciiBuffer
));
591 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
599 The wrapper function of PeiLoadImageLoadImage().
601 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
602 @param FileHandle - Pointer to the FFS file header of the image.
603 @param ImageAddressArg - Pointer to PE/TE image.
604 @param ImageSizeArg - Size of PE/TE image.
605 @param EntryPoint - Pointer to entry point of specified image file for output.
606 @param AuthenticationState - Pointer to attestation authentication state of image.
608 @return Status of PeiLoadImageLoadImage().
613 PeiLoadImageLoadImageWrapper (
614 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
615 IN EFI_PEI_FILE_HANDLE FileHandle
,
616 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
617 OUT UINT64
*ImageSizeArg
, OPTIONAL
618 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
619 OUT UINT32
*AuthenticationState
622 return PeiLoadImageLoadImage (
623 GetPeiServicesTablePointer (),
633 Check whether the input image has the relocation.
635 @param Pe32Data Pointer to the PE/COFF or TE image.
637 @retval TRUE Relocation is stripped.
638 @retval FALSE Relocation is not stripped.
646 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
647 EFI_IMAGE_DOS_HEADER
*DosHdr
;
649 ASSERT (Pe32Data
!= NULL
);
651 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
652 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
654 // DOS image header is present, so read the PE header after the DOS image header.
656 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
659 // DOS image header is not present, so PE header is at the image base.
661 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
665 // Three cases with regards to relocations:
666 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
667 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
668 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
669 // has no base relocs to apply
670 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
672 // Look at the file header to determine if relocations have been stripped, and
673 // save this info in the image context for later use.
675 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
676 if ((Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
681 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
682 if ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0) {
693 Routine to load image file for subsequent execution by LoadFile Ppi.
694 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE
695 XIP image format is used.
697 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
698 @param FileHandle - Pointer to the FFS file header of the image.
699 @param PeimState - The dispatch state of the input PEIM handle.
700 @param EntryPoint - Pointer to entry point of specified image file for output.
701 @param AuthenticationState - Pointer to attestation authentication state of image.
703 @retval EFI_SUCCESS - Image is successfully loaded.
704 @retval EFI_NOT_FOUND - Fail to locate necessary PPI
705 @retval Others - Fail to load file.
710 IN CONST EFI_PEI_SERVICES
**PeiServices
,
711 IN EFI_PEI_FILE_HANDLE FileHandle
,
713 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
714 OUT UINT32
*AuthenticationState
717 EFI_STATUS PpiStatus
;
720 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
721 EFI_PHYSICAL_ADDRESS ImageAddress
;
727 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
728 // one at a time, until one reports EFI_SUCCESS.
732 PpiStatus
= PeiServicesLocatePpi (
733 &gEfiPeiLoadFilePpiGuid
,
738 if (!EFI_ERROR (PpiStatus
)) {
739 Status
= LoadFile
->LoadFile (
747 if (!EFI_ERROR (Status
)) {
749 // The shadowed PEIM must be relocatable.
751 if (PeimState
== PEIM_STATE_REGISITER_FOR_SHADOW
) {
752 IsStrip
= RelocationIsStrip ((VOID
*) (UINTN
) ImageAddress
);
755 return EFI_UNSUPPORTED
;
760 // The image to be started must have the machine type supported by PeiCore.
762 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
)));
763 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
))) {
764 return EFI_UNSUPPORTED
;
770 } while (!EFI_ERROR (PpiStatus
));
778 Install Pei Load File PPI.
781 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
782 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.
786 InitializeImageServices (
787 IN PEI_CORE_INSTANCE
*PrivateData
,
788 IN PEI_CORE_INSTANCE
*OldCoreData
791 if (OldCoreData
== NULL
) {
793 // The first time we are XIP (running from FLASH). We need to remember the
794 // FLASH address so we can reinstall the memory version that runs faster
796 PrivateData
->XipLoadFile
= &gPpiLoadFilePpiList
;
797 PeiServicesInstallPpi (PrivateData
->XipLoadFile
);
800 // 2nd time we are running from memory so replace the XIP version with the
801 // new memory version.
803 PeiServicesReInstallPpi (PrivateData
->XipLoadFile
, &gPpiLoadFilePpiList
);