2 Pei Core Load Image Support
4 Copyright (c) 2006 - 2016, 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
) && PcdGetBool (PcdShadowPeimOnBoot
)) ||
121 ((Private
->HobList
.HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
) && PcdGetBool (PcdShadowPeimOnS3Boot
))) &&
122 (EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_X64
) || EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_IA32
))) {
124 // Shadow algorithm makes lots of non ANSI C assumptions and only works for IA32 and X64
125 // compilers that have been tested
127 if (Private
->ShadowedImageRead
== NULL
) {
128 MemoryBuffer
= AllocatePages (0x400 / EFI_PAGE_SIZE
+ 1);
129 ASSERT (MemoryBuffer
!= NULL
);
130 CopyMem (MemoryBuffer
, (CONST VOID
*) (UINTN
) PeiImageReadForShadow
, 0x400);
131 Private
->ShadowedImageRead
= (PE_COFF_LOADER_READ_FILE
) (UINTN
) MemoryBuffer
;
134 ImageContext
->ImageRead
= Private
->ShadowedImageRead
;
136 ImageContext
->ImageRead
= PeiImageRead
;
142 To check memory usage bit map array to figure out if the memory range the image will be loaded in is available or not. If
143 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.
144 The function is only invoked when load modules at fixed address feature is enabled.
146 @param Private Pointer to the private data passed in from caller
147 @param ImageBase The base address the image will be loaded at.
148 @param ImageSize The size of the image
150 @retval EFI_SUCCESS The memory range the image will be loaded in is available
151 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
154 CheckAndMarkFixLoadingMemoryUsageBitMap (
155 IN PEI_CORE_INSTANCE
*Private
,
156 IN EFI_PHYSICAL_ADDRESS ImageBase
,
160 UINT32 DxeCodePageNumber
;
161 UINT64 ReservedCodeSize
;
162 EFI_PHYSICAL_ADDRESS PeiCodeBase
;
163 UINT32 BaseOffsetPageNumber
;
164 UINT32 TopOffsetPageNumber
;
166 UINT64
*MemoryUsageBitMap
;
170 // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.
172 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
173 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
174 ReservedCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
+ PcdGet32(PcdLoadFixAddressPeiCodePageNumber
));
175 PeiCodeBase
= Private
->LoadModuleAtFixAddressTopAddress
- ReservedCodeSize
;
178 // Test the memory range for loading the image in the PEI code range.
180 if ((Private
->LoadModuleAtFixAddressTopAddress
- EFI_PAGES_TO_SIZE(DxeCodePageNumber
)) < (ImageBase
+ ImageSize
) ||
181 (PeiCodeBase
> ImageBase
)) {
182 return EFI_NOT_FOUND
;
186 // Test if the memory is avalaible or not.
188 MemoryUsageBitMap
= Private
->PeiCodeMemoryRangeUsageBitMap
;
189 BaseOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- PeiCodeBase
));
190 TopOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- PeiCodeBase
));
191 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
192 if ((MemoryUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
194 // This page is already used.
196 return EFI_NOT_FOUND
;
201 // Being here means the memory range is available. So mark the bits for the memory range
203 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
204 MemoryUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
210 Get the fixed loading address from image header assigned by build tool. This function only be called
211 when Loading module at Fixed address feature enabled.
213 @param ImageContext Pointer to the image context structure that describes the PE/COFF
214 image that needs to be examined by this function.
215 @param Private Pointer to the private data passed in from caller
217 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
218 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
222 GetPeCoffImageFixLoadingAssignedAddress(
223 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
224 IN PEI_CORE_INSTANCE
*Private
227 UINTN SectionHeaderOffset
;
229 EFI_IMAGE_SECTION_HEADER SectionHeader
;
230 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
231 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
234 UINT16 NumberOfSections
;
235 UINT64 ValueInSectionHeader
;
238 FixLoadingAddress
= 0;
239 Status
= EFI_NOT_FOUND
;
242 // Get PeHeader pointer
244 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
245 if (ImageContext
->IsTeImage
) {
247 // for TE image, the fix loading address is saved in first section header that doesn't point
250 SectionHeaderOffset
= sizeof (EFI_TE_IMAGE_HEADER
);
251 NumberOfSections
= ImgHdr
->Te
.NumberOfSections
;
253 SectionHeaderOffset
= (UINTN
)(
254 ImageContext
->PeCoffHeaderOffset
+
256 sizeof (EFI_IMAGE_FILE_HEADER
) +
257 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
259 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
262 // Get base address from the first section header that doesn't point to code section.
264 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
266 // Read section header from file
268 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
269 Status
= ImageContext
->ImageRead (
270 ImageContext
->Handle
,
275 if (EFI_ERROR (Status
)) {
279 Status
= EFI_NOT_FOUND
;
281 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
283 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
284 // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is
285 // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because
286 // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers
287 // 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
288 // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or
289 // else, these 2 fields should be set to Zero
291 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
292 if (ValueInSectionHeader
!= 0) {
294 // Found first section header that doesn't point to code section.
296 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) > 0) {
298 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field
299 // hold the absolute address of image base running in memory
301 FixLoadingAddress
= ValueInSectionHeader
;
304 // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field
305 // hold the offset relative to a platform-specific top address.
307 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(Private
->LoadModuleAtFixAddressTopAddress
+ (INT64
)ValueInSectionHeader
);
310 // Check if the memory range is available.
312 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (Private
, FixLoadingAddress
, (UINT32
) ImageContext
->ImageSize
);
313 if (!EFI_ERROR(Status
)) {
315 // The assigned address is valid. Return the specified loading address
317 ImageContext
->ImageAddress
= FixLoadingAddress
;
322 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
324 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status= %r \n", (VOID
*)(UINTN
)FixLoadingAddress
, Status
));
329 Loads and relocates a PE/COFF image into memory.
330 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.
332 @param FileHandle - Pointer to the FFS file header of the image.
333 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
334 @param ImageAddress - The base address of the relocated PE/COFF image
335 @param ImageSize - The size of the relocated PE/COFF image
336 @param EntryPoint - The entry point of the relocated PE/COFF image
338 @retval EFI_SUCCESS The file was loaded and relocated
339 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
340 @retval EFI_WARN_BUFFER_TOO_SMALL
341 There is not enough heap to allocate the requested size.
342 This will not prevent the XIP image from being invoked.
346 LoadAndRelocatePeCoffImage (
347 IN EFI_PEI_FILE_HANDLE FileHandle
,
349 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
350 OUT UINT64
*ImageSize
,
351 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
355 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
356 PEI_CORE_INSTANCE
*Private
;
357 UINT64 AlignImageSize
;
359 EFI_STATUS ReturnStatus
;
362 BOOLEAN IsRegisterForShadow
;
363 EFI_FV_FILE_INFO FileInfo
;
365 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
367 ReturnStatus
= EFI_SUCCESS
;
369 ZeroMem (&ImageContext
, sizeof (ImageContext
));
370 ImageContext
.Handle
= Pe32Data
;
371 Status
= GetImageReadFunction (&ImageContext
);
373 ASSERT_EFI_ERROR (Status
);
375 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
376 if (EFI_ERROR (Status
)) {
381 // Initilize local IsS3Boot and IsRegisterForShadow variable
384 if (Private
->HobList
.HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
) {
387 IsRegisterForShadow
= FALSE
;
388 if ((Private
->CurrentFileHandle
== FileHandle
)
389 && (Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] == PEIM_STATE_REGISITER_FOR_SHADOW
)) {
390 IsRegisterForShadow
= TRUE
;
394 // XIP image that ImageAddress is same to Image handle.
396 if (ImageContext
.ImageAddress
== (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
401 // Get file type first
403 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &FileInfo
);
404 ASSERT_EFI_ERROR (Status
);
407 // Check whether the file type is PEI module.
410 if (FileInfo
.FileType
== EFI_FV_FILETYPE_PEI_CORE
||
411 FileInfo
.FileType
== EFI_FV_FILETYPE_PEIM
||
412 FileInfo
.FileType
== EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
) {
417 // When Image has no reloc section, it can't be relocated into memory.
419 if (ImageContext
.RelocationsStripped
&& (Private
->PeiMemoryInstalled
) && ((!IsPeiModule
) ||
420 (!IsS3Boot
&& (PcdGetBool (PcdShadowPeimOnBoot
) || IsRegisterForShadow
)) || (IsS3Boot
&& PcdGetBool (PcdShadowPeimOnS3Boot
)))) {
421 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN
) Pe32Data
));
425 // Set default base address to current image address.
427 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
430 // Allocate Memory for the image when memory is ready, and image is relocatable.
431 // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.
432 // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.
434 if ((!ImageContext
.RelocationsStripped
) && (Private
->PeiMemoryInstalled
) && ((!IsPeiModule
) ||
435 (!IsS3Boot
&& (PcdGetBool (PcdShadowPeimOnBoot
) || IsRegisterForShadow
)) || (IsS3Boot
&& PcdGetBool (PcdShadowPeimOnS3Boot
)))) {
437 // Allocate more buffer to avoid buffer overflow.
439 if (ImageContext
.IsTeImage
) {
440 AlignImageSize
= ImageContext
.ImageSize
+ ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
442 AlignImageSize
= ImageContext
.ImageSize
;
445 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
446 AlignImageSize
+= ImageContext
.SectionAlignment
;
449 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
450 Status
= GetPeCoffImageFixLoadingAssignedAddress(&ImageContext
, Private
);
451 if (EFI_ERROR (Status
)){
452 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
454 // The PEIM is not assiged valid address, try to allocate page to load it.
456 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) AlignImageSize
));
459 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) AlignImageSize
));
461 if (ImageContext
.ImageAddress
!= 0) {
463 // Adjust the Image Address to make sure it is section alignment.
465 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
466 ImageContext
.ImageAddress
=
467 (ImageContext
.ImageAddress
+ ImageContext
.SectionAlignment
- 1) &
468 ~((UINTN
)ImageContext
.SectionAlignment
- 1);
471 // Fix alignment requirement when Load IPF TeImage into memory.
472 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.
474 if (ImageContext
.IsTeImage
) {
475 ImageContext
.ImageAddress
= ImageContext
.ImageAddress
+
476 ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
-
477 sizeof (EFI_TE_IMAGE_HEADER
);
481 // No enough memory resource.
485 // XIP image can still be invoked.
487 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
488 ReturnStatus
= EFI_WARN_BUFFER_TOO_SMALL
;
491 // Non XIP image can't be loaded because no enough memory is allocated.
494 return EFI_OUT_OF_RESOURCES
;
500 // Load the image to our new buffer
502 Status
= PeCoffLoaderLoadImage (&ImageContext
);
503 if (EFI_ERROR (Status
)) {
507 // Relocate the image in our new buffer
509 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
510 if (EFI_ERROR (Status
)) {
515 // Flush the instruction cache so the image data is written before we execute it
517 if (ImageContext
.ImageAddress
!= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
518 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
521 *ImageAddress
= ImageContext
.ImageAddress
;
522 *ImageSize
= ImageContext
.ImageSize
;
523 *EntryPoint
= ImageContext
.EntryPoint
;
529 Loads a PEIM into memory for subsequent execution. If there are compressed
530 images or images that need to be relocated into memory for performance reasons,
531 this service performs that transformation.
533 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
534 @param FileHandle Pointer to the FFS file header of the image.
535 @param ImageAddressArg Pointer to PE/TE image.
536 @param ImageSizeArg Size of PE/TE image.
537 @param EntryPoint Pointer to entry point of specified image file for output.
538 @param AuthenticationState - Pointer to attestation authentication state of image.
540 @retval EFI_SUCCESS Image is successfully loaded.
541 @retval EFI_NOT_FOUND Fail to locate necessary PPI.
542 @retval EFI_UNSUPPORTED Image Machine Type is not supported.
543 @retval EFI_WARN_BUFFER_TOO_SMALL
544 There is not enough heap to allocate the requested size.
545 This will not prevent the XIP image from being invoked.
549 PeiLoadImageLoadImage (
550 IN CONST EFI_PEI_SERVICES
**PeiServices
,
551 IN EFI_PEI_FILE_HANDLE FileHandle
,
552 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
553 OUT UINT64
*ImageSizeArg
, OPTIONAL
554 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
555 OUT UINT32
*AuthenticationState
560 EFI_PHYSICAL_ADDRESS ImageAddress
;
562 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
564 EFI_SECTION_TYPE SearchType1
;
565 EFI_SECTION_TYPE SearchType2
;
569 *AuthenticationState
= 0;
571 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst
)) {
572 SearchType1
= EFI_SECTION_TE
;
573 SearchType2
= EFI_SECTION_PE32
;
575 SearchType1
= EFI_SECTION_PE32
;
576 SearchType2
= EFI_SECTION_TE
;
580 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
581 // is true, TE will be searched first).
583 Status
= PeiServicesFfsFindSectionData3 (
591 // If we didn't find a first exe section, try to find the second exe section.
593 if (EFI_ERROR (Status
)) {
594 Status
= PeiServicesFfsFindSectionData3 (
601 if (EFI_ERROR (Status
)) {
603 // PEI core only carry the loader function for TE and PE32 executables
604 // If this two section does not exist, just return.
611 // If memory is installed, perform the shadow operations
613 Status
= LoadAndRelocatePeCoffImage (
621 ASSERT_EFI_ERROR (Status
);
624 if (EFI_ERROR (Status
)) {
629 // Got the entry point from the loaded Pe32Data
631 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
632 *EntryPoint
= ImageEntryPoint
;
634 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
636 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
637 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine
)) {
638 return EFI_UNSUPPORTED
;
642 if (ImageAddressArg
!= NULL
) {
643 *ImageAddressArg
= ImageAddress
;
646 if (ImageSizeArg
!= NULL
) {
647 *ImageSizeArg
= ImageSize
;
652 CHAR8 EfiFileName
[512];
657 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
659 if (Machine
!= EFI_IMAGE_MACHINE_IA64
) {
660 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)*EntryPoint
));
663 // For IPF Image, the real entry point should be print.
665 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)*EntryPoint
)));
669 // Print Module Name by PeImage PDB file name.
671 AsciiString
= PeCoffLoaderGetPdbPointer (Pe32Data
);
673 if (AsciiString
!= NULL
) {
675 for (Index
= 0; AsciiString
[Index
] != 0; Index
++) {
676 if (AsciiString
[Index
] == '\\' || AsciiString
[Index
] == '/') {
677 StartIndex
= Index
+ 1;
682 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
683 // The PDB file name is limited in the range of 0~511.
684 // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.
686 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
687 EfiFileName
[Index
] = AsciiString
[Index
+ StartIndex
];
688 if (EfiFileName
[Index
] == 0) {
689 EfiFileName
[Index
] = '.';
691 if (EfiFileName
[Index
] == '.') {
692 EfiFileName
[Index
+ 1] = 'e';
693 EfiFileName
[Index
+ 2] = 'f';
694 EfiFileName
[Index
+ 3] = 'i';
695 EfiFileName
[Index
+ 4] = 0;
700 if (Index
== sizeof (EfiFileName
) - 4) {
701 EfiFileName
[Index
] = 0;
704 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a", EfiFileName
));
709 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
717 The wrapper function of PeiLoadImageLoadImage().
719 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
720 @param FileHandle - Pointer to the FFS file header of the image.
721 @param ImageAddressArg - Pointer to PE/TE image.
722 @param ImageSizeArg - Size of PE/TE image.
723 @param EntryPoint - Pointer to entry point of specified image file for output.
724 @param AuthenticationState - Pointer to attestation authentication state of image.
726 @return Status of PeiLoadImageLoadImage().
731 PeiLoadImageLoadImageWrapper (
732 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
733 IN EFI_PEI_FILE_HANDLE FileHandle
,
734 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
735 OUT UINT64
*ImageSizeArg
, OPTIONAL
736 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
737 OUT UINT32
*AuthenticationState
740 return PeiLoadImageLoadImage (
741 GetPeiServicesTablePointer (),
751 Check whether the input image has the relocation.
753 @param Pe32Data Pointer to the PE/COFF or TE image.
755 @retval TRUE Relocation is stripped.
756 @retval FALSE Relocation is not stripped.
764 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
765 EFI_IMAGE_DOS_HEADER
*DosHdr
;
767 ASSERT (Pe32Data
!= NULL
);
769 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
770 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
772 // DOS image header is present, so read the PE header after the DOS image header.
774 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
777 // DOS image header is not present, so PE header is at the image base.
779 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
783 // Three cases with regards to relocations:
784 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
785 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
786 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
787 // has no base relocs to apply
788 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
790 // Look at the file header to determine if relocations have been stripped, and
791 // save this info in the image context for later use.
793 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
794 if ((Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
799 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
800 if ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0) {
811 Routine to load image file for subsequent execution by LoadFile Ppi.
812 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE
813 XIP image format is used.
815 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
816 @param FileHandle - Pointer to the FFS file header of the image.
817 @param PeimState - The dispatch state of the input PEIM handle.
818 @param EntryPoint - Pointer to entry point of specified image file for output.
819 @param AuthenticationState - Pointer to attestation authentication state of image.
821 @retval EFI_SUCCESS - Image is successfully loaded.
822 @retval EFI_NOT_FOUND - Fail to locate necessary PPI
823 @retval Others - Fail to load file.
828 IN CONST EFI_PEI_SERVICES
**PeiServices
,
829 IN EFI_PEI_FILE_HANDLE FileHandle
,
831 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
832 OUT UINT32
*AuthenticationState
835 EFI_STATUS PpiStatus
;
838 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
839 EFI_PHYSICAL_ADDRESS ImageAddress
;
845 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
846 // one at a time, until one reports EFI_SUCCESS.
850 PpiStatus
= PeiServicesLocatePpi (
851 &gEfiPeiLoadFilePpiGuid
,
856 if (!EFI_ERROR (PpiStatus
)) {
857 Status
= LoadFile
->LoadFile (
865 if (!EFI_ERROR (Status
) || Status
== EFI_WARN_BUFFER_TOO_SMALL
) {
867 // The shadowed PEIM must be relocatable.
869 if (PeimState
== PEIM_STATE_REGISITER_FOR_SHADOW
) {
870 IsStrip
= RelocationIsStrip ((VOID
*) (UINTN
) ImageAddress
);
873 return EFI_UNSUPPORTED
;
878 // The image to be started must have the machine type supported by PeiCore.
880 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
)));
881 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
))) {
882 return EFI_UNSUPPORTED
;
888 } while (!EFI_ERROR (PpiStatus
));
896 Install Pei Load File PPI.
899 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
900 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.
904 InitializeImageServices (
905 IN PEI_CORE_INSTANCE
*PrivateData
,
906 IN PEI_CORE_INSTANCE
*OldCoreData
909 if (OldCoreData
== NULL
) {
911 // The first time we are XIP (running from FLASH). We need to remember the
912 // FLASH address so we can reinstall the memory version that runs faster
914 PrivateData
->XipLoadFile
= &gPpiLoadFilePpiList
;
915 PeiServicesInstallPpi (PrivateData
->XipLoadFile
);
918 // 2nd time we are running from memory so replace the XIP version with the
919 // new memory version.
921 PeiServicesReInstallPpi (PrivateData
->XipLoadFile
, &gPpiLoadFilePpiList
);