2 Pei Core Load Image Support
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi
= {
13 PeiLoadImageLoadImageWrapper
17 EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList
= {
18 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
19 &gEfiPeiLoadFilePpiGuid
,
25 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file.
26 The function is used for XIP code to have optimized memory copy.
28 @param FileHandle - The handle to the PE/COFF file
29 @param FileOffset - The offset, in bytes, into the file to read
30 @param ReadSize - The number of bytes to read from the file starting at FileOffset
31 @param Buffer - A pointer to the buffer to read the data into.
33 @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
48 Destination8
= Buffer
;
49 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
50 if (Destination8
!= Source8
) {
51 CopyMem (Destination8
, Source8
, *ReadSize
);
58 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
59 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.
60 The function is only invoked when load modules at fixed address feature is enabled.
62 @param Private Pointer to the private data passed in from caller
63 @param ImageBase The base address the image will be loaded at.
64 @param ImageSize The size of the image
66 @retval EFI_SUCCESS The memory range the image will be loaded in is available
67 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
70 CheckAndMarkFixLoadingMemoryUsageBitMap (
71 IN PEI_CORE_INSTANCE
*Private
,
72 IN EFI_PHYSICAL_ADDRESS ImageBase
,
76 UINT32 DxeCodePageNumber
;
77 UINT64 ReservedCodeSize
;
78 EFI_PHYSICAL_ADDRESS PeiCodeBase
;
79 UINT32 BaseOffsetPageNumber
;
80 UINT32 TopOffsetPageNumber
;
82 UINT64
*MemoryUsageBitMap
;
86 // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.
88 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
89 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
90 ReservedCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
+ PcdGet32(PcdLoadFixAddressPeiCodePageNumber
));
91 PeiCodeBase
= Private
->LoadModuleAtFixAddressTopAddress
- ReservedCodeSize
;
94 // Test the memory range for loading the image in the PEI code range.
96 if ((Private
->LoadModuleAtFixAddressTopAddress
- EFI_PAGES_TO_SIZE(DxeCodePageNumber
)) < (ImageBase
+ ImageSize
) ||
97 (PeiCodeBase
> ImageBase
)) {
102 // Test if the memory is available or not.
104 MemoryUsageBitMap
= Private
->PeiCodeMemoryRangeUsageBitMap
;
105 BaseOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- PeiCodeBase
));
106 TopOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- PeiCodeBase
));
107 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
108 if ((MemoryUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
110 // This page is already used.
112 return EFI_NOT_FOUND
;
117 // Being here means the memory range is available. So mark the bits for the memory range
119 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
120 MemoryUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
126 Get the fixed loading address from image header assigned by build tool. This function only be called
127 when Loading module at Fixed address feature enabled.
129 @param ImageContext Pointer to the image context structure that describes the PE/COFF
130 image that needs to be examined by this function.
131 @param Private Pointer to the private data passed in from caller
133 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
134 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
138 GetPeCoffImageFixLoadingAssignedAddress(
139 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
140 IN PEI_CORE_INSTANCE
*Private
143 UINTN SectionHeaderOffset
;
145 EFI_IMAGE_SECTION_HEADER SectionHeader
;
146 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
147 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
150 UINT16 NumberOfSections
;
151 UINT64 ValueInSectionHeader
;
154 FixLoadingAddress
= 0;
155 Status
= EFI_NOT_FOUND
;
158 // Get PeHeader pointer
160 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
161 if (ImageContext
->IsTeImage
) {
163 // for TE image, the fix loading address is saved in first section header that doesn't point
166 SectionHeaderOffset
= sizeof (EFI_TE_IMAGE_HEADER
);
167 NumberOfSections
= ImgHdr
->Te
.NumberOfSections
;
169 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
171 sizeof (EFI_IMAGE_FILE_HEADER
) +
172 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
173 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
176 // Get base address from the first section header that doesn't point to code section.
178 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
180 // Read section header from file
182 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
183 Status
= ImageContext
->ImageRead (
184 ImageContext
->Handle
,
189 if (EFI_ERROR (Status
)) {
193 Status
= EFI_NOT_FOUND
;
195 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
197 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
198 // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is
199 // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because
200 // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers
201 // 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
202 // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or
203 // else, these 2 fields should be set to Zero
205 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
206 if (ValueInSectionHeader
!= 0) {
208 // Found first section header that doesn't point to code section.
210 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) > 0) {
212 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field
213 // hold the absolute address of image base running in memory
215 FixLoadingAddress
= ValueInSectionHeader
;
218 // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field
219 // hold the offset relative to a platform-specific top address.
221 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(Private
->LoadModuleAtFixAddressTopAddress
+ (INT64
)ValueInSectionHeader
);
224 // Check if the memory range is available.
226 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (Private
, FixLoadingAddress
, (UINT32
) ImageContext
->ImageSize
);
227 if (!EFI_ERROR(Status
)) {
229 // The assigned address is valid. Return the specified loading address
231 ImageContext
->ImageAddress
= FixLoadingAddress
;
236 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
238 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
));
243 Loads and relocates a PE/COFF image into memory.
244 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.
246 @param FileHandle - Pointer to the FFS file header of the image.
247 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
248 @param ImageAddress - The base address of the relocated PE/COFF image
249 @param ImageSize - The size of the relocated PE/COFF image
250 @param EntryPoint - The entry point of the relocated PE/COFF image
252 @retval EFI_SUCCESS The file was loaded and relocated
253 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
254 @retval EFI_WARN_BUFFER_TOO_SMALL
255 There is not enough heap to allocate the requested size.
256 This will not prevent the XIP image from being invoked.
260 LoadAndRelocatePeCoffImage (
261 IN EFI_PEI_FILE_HANDLE FileHandle
,
263 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
264 OUT UINT64
*ImageSize
,
265 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
269 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
270 PEI_CORE_INSTANCE
*Private
;
271 UINT64 AlignImageSize
;
273 EFI_STATUS ReturnStatus
;
276 BOOLEAN IsRegisterForShadow
;
277 EFI_FV_FILE_INFO FileInfo
;
279 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
281 ReturnStatus
= EFI_SUCCESS
;
283 ZeroMem (&ImageContext
, sizeof (ImageContext
));
284 ImageContext
.Handle
= Pe32Data
;
285 ImageContext
.ImageRead
= PeiImageRead
;
287 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
288 if (EFI_ERROR (Status
)) {
293 // Initialize local IsS3Boot and IsRegisterForShadow variable
296 if (Private
->HobList
.HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
) {
299 IsRegisterForShadow
= FALSE
;
300 if ((Private
->CurrentFileHandle
== FileHandle
)
301 && (Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] == PEIM_STATE_REGISTER_FOR_SHADOW
)) {
302 IsRegisterForShadow
= TRUE
;
306 // XIP image that ImageAddress is same to Image handle.
308 if (ImageContext
.ImageAddress
== (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
313 // Get file type first
315 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &FileInfo
);
316 ASSERT_EFI_ERROR (Status
);
319 // Check whether the file type is PEI module.
322 if (FileInfo
.FileType
== EFI_FV_FILETYPE_PEI_CORE
||
323 FileInfo
.FileType
== EFI_FV_FILETYPE_PEIM
||
324 FileInfo
.FileType
== EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
) {
329 // When Image has no reloc section, it can't be relocated into memory.
331 if (ImageContext
.RelocationsStripped
&& (Private
->PeiMemoryInstalled
) &&
332 ((!IsPeiModule
) || PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes
) ||
333 (!IsS3Boot
&& (PcdGetBool (PcdShadowPeimOnBoot
) || IsRegisterForShadow
)) ||
334 (IsS3Boot
&& PcdGetBool (PcdShadowPeimOnS3Boot
)))
336 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN
) Pe32Data
));
340 // Set default base address to current image address.
342 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
345 // Allocate Memory for the image when memory is ready, and image is relocatable.
346 // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.
347 // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.
349 if ((!ImageContext
.RelocationsStripped
) && (Private
->PeiMemoryInstalled
) &&
350 ((!IsPeiModule
) || PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes
) ||
351 (!IsS3Boot
&& (PcdGetBool (PcdShadowPeimOnBoot
) || IsRegisterForShadow
)) ||
352 (IsS3Boot
&& PcdGetBool (PcdShadowPeimOnS3Boot
)))
355 // Allocate more buffer to avoid buffer overflow.
357 if (ImageContext
.IsTeImage
) {
358 AlignImageSize
= ImageContext
.ImageSize
+ ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
360 AlignImageSize
= ImageContext
.ImageSize
;
363 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
364 AlignImageSize
+= ImageContext
.SectionAlignment
;
367 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
368 Status
= GetPeCoffImageFixLoadingAssignedAddress(&ImageContext
, Private
);
369 if (EFI_ERROR (Status
)){
370 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
372 // The PEIM is not assigned valid address, try to allocate page to load it.
374 Status
= PeiServicesAllocatePages (EfiBootServicesCode
,
375 EFI_SIZE_TO_PAGES ((UINT32
) AlignImageSize
),
376 &ImageContext
.ImageAddress
);
379 Status
= PeiServicesAllocatePages (EfiBootServicesCode
,
380 EFI_SIZE_TO_PAGES ((UINT32
) AlignImageSize
),
381 &ImageContext
.ImageAddress
);
383 if (!EFI_ERROR (Status
)) {
385 // Adjust the Image Address to make sure it is section alignment.
387 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
388 ImageContext
.ImageAddress
=
389 (ImageContext
.ImageAddress
+ ImageContext
.SectionAlignment
- 1) &
390 ~((UINTN
)ImageContext
.SectionAlignment
- 1);
393 // Fix alignment requirement when Load IPF TeImage into memory.
394 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.
396 if (ImageContext
.IsTeImage
) {
397 ImageContext
.ImageAddress
= ImageContext
.ImageAddress
+
398 ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
-
399 sizeof (EFI_TE_IMAGE_HEADER
);
403 // No enough memory resource.
407 // XIP image can still be invoked.
409 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
410 ReturnStatus
= EFI_WARN_BUFFER_TOO_SMALL
;
413 // Non XIP image can't be loaded because no enough memory is allocated.
416 return EFI_OUT_OF_RESOURCES
;
422 // Load the image to our new buffer
424 Status
= PeCoffLoaderLoadImage (&ImageContext
);
425 if (EFI_ERROR (Status
)) {
426 if (ImageContext
.ImageError
== IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
) {
427 DEBUG ((DEBUG_ERROR
, "PEIM Image Address 0x%11p doesn't meet with section alignment 0x%x.\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
, ImageContext
.SectionAlignment
));
432 // Relocate the image in our new buffer
434 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
435 if (EFI_ERROR (Status
)) {
440 // Flush the instruction cache so the image data is written before we execute it
442 if (ImageContext
.ImageAddress
!= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
443 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
446 *ImageAddress
= ImageContext
.ImageAddress
;
447 *ImageSize
= ImageContext
.ImageSize
;
448 *EntryPoint
= ImageContext
.EntryPoint
;
454 Loads and relocates a PE/COFF image in place.
456 @param Pe32Data The base address of the PE/COFF file that is to be loaded and relocated
457 @param ImageAddress The base address of the relocated PE/COFF image
459 @retval EFI_SUCCESS The file was loaded and relocated.
460 @retval Others The file not be loaded and error occurred.
464 LoadAndRelocatePeCoffImageInPlace (
466 IN VOID
*ImageAddress
470 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
472 ZeroMem (&ImageContext
, sizeof (ImageContext
));
473 ImageContext
.Handle
= Pe32Data
;
474 ImageContext
.ImageRead
= PeiImageRead
;
476 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
477 if (EFI_ERROR (Status
)) {
478 ASSERT_EFI_ERROR (Status
);
482 ImageContext
.ImageAddress
= (PHYSICAL_ADDRESS
)(UINTN
) ImageAddress
;
485 // Load the image in place
487 Status
= PeCoffLoaderLoadImage (&ImageContext
);
488 if (EFI_ERROR (Status
)) {
489 ASSERT_EFI_ERROR (Status
);
494 // Relocate the image in place
496 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
497 if (EFI_ERROR (Status
)) {
498 ASSERT_EFI_ERROR (Status
);
503 // Flush the instruction cache so the image data is written before we execute it
505 if (ImageContext
.ImageAddress
!= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
506 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
513 Find the PE32 Data for an FFS file.
515 @param FileHandle Pointer to the FFS file header of the image.
516 @param Pe32Data Pointer to a (VOID *) PE32 Data pointer.
518 @retval EFI_SUCCESS Image is successfully loaded.
519 @retval EFI_NOT_FOUND Fail to locate PE32 Data.
524 IN EFI_PEI_FILE_HANDLE FileHandle
,
529 EFI_SECTION_TYPE SearchType1
;
530 EFI_SECTION_TYPE SearchType2
;
531 UINT32 AuthenticationState
;
535 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst
)) {
536 SearchType1
= EFI_SECTION_TE
;
537 SearchType2
= EFI_SECTION_PE32
;
539 SearchType1
= EFI_SECTION_PE32
;
540 SearchType2
= EFI_SECTION_TE
;
544 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
545 // is true, TE will be searched first).
547 Status
= PeiServicesFfsFindSectionData3 (
555 // If we didn't find a first exe section, try to find the second exe section.
557 if (EFI_ERROR (Status
)) {
558 Status
= PeiServicesFfsFindSectionData3 (
570 Loads a PEIM into memory for subsequent execution. If there are compressed
571 images or images that need to be relocated into memory for performance reasons,
572 this service performs that transformation.
574 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
575 @param FileHandle Pointer to the FFS file header of the image.
576 @param ImageAddressArg Pointer to PE/TE image.
577 @param ImageSizeArg Size of PE/TE image.
578 @param EntryPoint Pointer to entry point of specified image file for output.
579 @param AuthenticationState - Pointer to attestation authentication state of image.
581 @retval EFI_SUCCESS Image is successfully loaded.
582 @retval EFI_NOT_FOUND Fail to locate necessary PPI.
583 @retval EFI_UNSUPPORTED Image Machine Type is not supported.
584 @retval EFI_WARN_BUFFER_TOO_SMALL
585 There is not enough heap to allocate the requested size.
586 This will not prevent the XIP image from being invoked.
590 PeiLoadImageLoadImage (
591 IN CONST EFI_PEI_SERVICES
**PeiServices
,
592 IN EFI_PEI_FILE_HANDLE FileHandle
,
593 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
594 OUT UINT64
*ImageSizeArg
, OPTIONAL
595 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
596 OUT UINT32
*AuthenticationState
601 EFI_PHYSICAL_ADDRESS ImageAddress
;
603 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
605 EFI_SECTION_TYPE SearchType1
;
606 EFI_SECTION_TYPE SearchType2
;
610 *AuthenticationState
= 0;
612 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst
)) {
613 SearchType1
= EFI_SECTION_TE
;
614 SearchType2
= EFI_SECTION_PE32
;
616 SearchType1
= EFI_SECTION_PE32
;
617 SearchType2
= EFI_SECTION_TE
;
621 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
622 // is true, TE will be searched first).
624 Status
= PeiServicesFfsFindSectionData3 (
632 // If we didn't find a first exe section, try to find the second exe section.
634 if (EFI_ERROR (Status
)) {
635 Status
= PeiServicesFfsFindSectionData3 (
642 if (EFI_ERROR (Status
)) {
644 // PEI core only carry the loader function for TE and PE32 executables
645 // If this two section does not exist, just return.
651 DEBUG ((DEBUG_INFO
, "Loading PEIM %g\n", FileHandle
));
654 // If memory is installed, perform the shadow operations
656 Status
= LoadAndRelocatePeCoffImage (
664 ASSERT_EFI_ERROR (Status
);
667 if (EFI_ERROR (Status
)) {
672 // Got the entry point from the loaded Pe32Data
674 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
675 *EntryPoint
= ImageEntryPoint
;
677 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
679 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
680 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine
)) {
681 return EFI_UNSUPPORTED
;
685 if (ImageAddressArg
!= NULL
) {
686 *ImageAddressArg
= ImageAddress
;
689 if (ImageSizeArg
!= NULL
) {
690 *ImageSizeArg
= ImageSize
;
695 CHAR8 EfiFileName
[512];
700 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
702 if (Machine
!= EFI_IMAGE_MACHINE_IA64
) {
703 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)*EntryPoint
));
706 // For IPF Image, the real entry point should be print.
708 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)*EntryPoint
)));
712 // Print Module Name by PeImage PDB file name.
714 AsciiString
= PeCoffLoaderGetPdbPointer (Pe32Data
);
716 if (AsciiString
!= NULL
) {
718 for (Index
= 0; AsciiString
[Index
] != 0; Index
++) {
719 if (AsciiString
[Index
] == '\\' || AsciiString
[Index
] == '/') {
720 StartIndex
= Index
+ 1;
725 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
726 // The PDB file name is limited in the range of 0~511.
727 // If the length is bigger than 511, trim the redundant characters to avoid overflow in array boundary.
729 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
730 EfiFileName
[Index
] = AsciiString
[Index
+ StartIndex
];
731 if (EfiFileName
[Index
] == 0) {
732 EfiFileName
[Index
] = '.';
734 if (EfiFileName
[Index
] == '.') {
735 EfiFileName
[Index
+ 1] = 'e';
736 EfiFileName
[Index
+ 2] = 'f';
737 EfiFileName
[Index
+ 3] = 'i';
738 EfiFileName
[Index
+ 4] = 0;
743 if (Index
== sizeof (EfiFileName
) - 4) {
744 EfiFileName
[Index
] = 0;
747 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a", EfiFileName
));
752 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
760 The wrapper function of PeiLoadImageLoadImage().
762 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
763 @param FileHandle - Pointer to the FFS file header of the image.
764 @param ImageAddressArg - Pointer to PE/TE image.
765 @param ImageSizeArg - Size of PE/TE image.
766 @param EntryPoint - Pointer to entry point of specified image file for output.
767 @param AuthenticationState - Pointer to attestation authentication state of image.
769 @return Status of PeiLoadImageLoadImage().
774 PeiLoadImageLoadImageWrapper (
775 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
776 IN EFI_PEI_FILE_HANDLE FileHandle
,
777 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
778 OUT UINT64
*ImageSizeArg
, OPTIONAL
779 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
780 OUT UINT32
*AuthenticationState
783 return PeiLoadImageLoadImage (
784 GetPeiServicesTablePointer (),
794 Check whether the input image has the relocation.
796 @param Pe32Data Pointer to the PE/COFF or TE image.
798 @retval TRUE Relocation is stripped.
799 @retval FALSE Relocation is not stripped.
807 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
808 EFI_IMAGE_DOS_HEADER
*DosHdr
;
810 ASSERT (Pe32Data
!= NULL
);
812 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
813 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
815 // DOS image header is present, so read the PE header after the DOS image header.
817 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
820 // DOS image header is not present, so PE header is at the image base.
822 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
826 // Three cases with regards to relocations:
827 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
828 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
829 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
830 // has no base relocs to apply
831 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
833 // Look at the file header to determine if relocations have been stripped, and
834 // save this info in the image context for later use.
836 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
837 if ((Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
842 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
843 if ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0) {
854 Routine to load image file for subsequent execution by LoadFile Ppi.
855 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE
856 XIP image format is used.
858 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
859 @param FileHandle - Pointer to the FFS file header of the image.
860 @param PeimState - The dispatch state of the input PEIM handle.
861 @param EntryPoint - Pointer to entry point of specified image file for output.
862 @param AuthenticationState - Pointer to attestation authentication state of image.
864 @retval EFI_SUCCESS - Image is successfully loaded.
865 @retval EFI_NOT_FOUND - Fail to locate necessary PPI
866 @retval Others - Fail to load file.
871 IN CONST EFI_PEI_SERVICES
**PeiServices
,
872 IN EFI_PEI_FILE_HANDLE FileHandle
,
874 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
875 OUT UINT32
*AuthenticationState
878 EFI_STATUS PpiStatus
;
881 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
882 EFI_PHYSICAL_ADDRESS ImageAddress
;
888 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
889 // one at a time, until one reports EFI_SUCCESS.
893 PpiStatus
= PeiServicesLocatePpi (
894 &gEfiPeiLoadFilePpiGuid
,
899 if (!EFI_ERROR (PpiStatus
)) {
900 Status
= LoadFile
->LoadFile (
908 if (!EFI_ERROR (Status
) || Status
== EFI_WARN_BUFFER_TOO_SMALL
) {
910 // The shadowed PEIM must be relocatable.
912 if (PeimState
== PEIM_STATE_REGISTER_FOR_SHADOW
) {
913 IsStrip
= RelocationIsStrip ((VOID
*) (UINTN
) ImageAddress
);
916 return EFI_UNSUPPORTED
;
921 // The image to be started must have the machine type supported by PeiCore.
923 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
)));
924 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
))) {
925 return EFI_UNSUPPORTED
;
931 } while (!EFI_ERROR (PpiStatus
));
939 Install Pei Load File PPI.
942 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
943 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.
947 InitializeImageServices (
948 IN PEI_CORE_INSTANCE
*PrivateData
,
949 IN PEI_CORE_INSTANCE
*OldCoreData
952 if (OldCoreData
== NULL
) {
954 // The first time we are XIP (running from FLASH). We need to remember the
955 // FLASH address so we can reinstall the memory version that runs faster
957 PrivateData
->XipLoadFile
= &gPpiLoadFilePpiList
;
958 PeiServicesInstallPpi (PrivateData
->XipLoadFile
);
961 // 2nd time we are running from memory so replace the XIP version with the
962 // new memory version.
964 PeiServicesReInstallPpi (PrivateData
->XipLoadFile
, &gPpiLoadFilePpiList
);