2 Pei Core Load Image Support
4 Copyright (c) 2006 - 2018, 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 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
116 PEI_CORE_INSTANCE
*Private
;
117 EFI_PHYSICAL_ADDRESS MemoryBuffer
;
119 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
122 if (Private
->PeiMemoryInstalled
&& (((Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
) && PcdGetBool (PcdShadowPeimOnBoot
)) ||
123 ((Private
->HobList
.HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
) && PcdGetBool (PcdShadowPeimOnS3Boot
)))) {
125 // Shadow algorithm makes lots of non ANSI C assumptions and only works for IA32 and X64
126 // compilers that have been tested
128 if (Private
->ShadowedImageRead
== NULL
) {
129 PeiServicesAllocatePages (EfiBootServicesCode
, 0x400 / EFI_PAGE_SIZE
+ 1, &MemoryBuffer
);
130 ASSERT (MemoryBuffer
!= 0);
131 CopyMem ((VOID
*)(UINTN
)MemoryBuffer
, (CONST VOID
*) (UINTN
) PeiImageReadForShadow
, 0x400);
132 Private
->ShadowedImageRead
= (PE_COFF_LOADER_READ_FILE
) (UINTN
) MemoryBuffer
;
135 ImageContext
->ImageRead
= Private
->ShadowedImageRead
;
137 ImageContext
->ImageRead
= PeiImageRead
;
140 ImageContext
->ImageRead
= PeiImageRead
;
145 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
146 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.
147 The function is only invoked when load modules at fixed address feature is enabled.
149 @param Private Pointer to the private data passed in from caller
150 @param ImageBase The base address the image will be loaded at.
151 @param ImageSize The size of the image
153 @retval EFI_SUCCESS The memory range the image will be loaded in is available
154 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
157 CheckAndMarkFixLoadingMemoryUsageBitMap (
158 IN PEI_CORE_INSTANCE
*Private
,
159 IN EFI_PHYSICAL_ADDRESS ImageBase
,
163 UINT32 DxeCodePageNumber
;
164 UINT64 ReservedCodeSize
;
165 EFI_PHYSICAL_ADDRESS PeiCodeBase
;
166 UINT32 BaseOffsetPageNumber
;
167 UINT32 TopOffsetPageNumber
;
169 UINT64
*MemoryUsageBitMap
;
173 // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.
175 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
176 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
177 ReservedCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
+ PcdGet32(PcdLoadFixAddressPeiCodePageNumber
));
178 PeiCodeBase
= Private
->LoadModuleAtFixAddressTopAddress
- ReservedCodeSize
;
181 // Test the memory range for loading the image in the PEI code range.
183 if ((Private
->LoadModuleAtFixAddressTopAddress
- EFI_PAGES_TO_SIZE(DxeCodePageNumber
)) < (ImageBase
+ ImageSize
) ||
184 (PeiCodeBase
> ImageBase
)) {
185 return EFI_NOT_FOUND
;
189 // Test if the memory is avalaible or not.
191 MemoryUsageBitMap
= Private
->PeiCodeMemoryRangeUsageBitMap
;
192 BaseOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- PeiCodeBase
));
193 TopOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- PeiCodeBase
));
194 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
195 if ((MemoryUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
197 // This page is already used.
199 return EFI_NOT_FOUND
;
204 // Being here means the memory range is available. So mark the bits for the memory range
206 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
207 MemoryUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
213 Get the fixed loading address from image header assigned by build tool. This function only be called
214 when Loading module at Fixed address feature enabled.
216 @param ImageContext Pointer to the image context structure that describes the PE/COFF
217 image that needs to be examined by this function.
218 @param Private Pointer to the private data passed in from caller
220 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
221 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
225 GetPeCoffImageFixLoadingAssignedAddress(
226 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
227 IN PEI_CORE_INSTANCE
*Private
230 UINTN SectionHeaderOffset
;
232 EFI_IMAGE_SECTION_HEADER SectionHeader
;
233 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
234 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
237 UINT16 NumberOfSections
;
238 UINT64 ValueInSectionHeader
;
241 FixLoadingAddress
= 0;
242 Status
= EFI_NOT_FOUND
;
245 // Get PeHeader pointer
247 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
248 if (ImageContext
->IsTeImage
) {
250 // for TE image, the fix loading address is saved in first section header that doesn't point
253 SectionHeaderOffset
= sizeof (EFI_TE_IMAGE_HEADER
);
254 NumberOfSections
= ImgHdr
->Te
.NumberOfSections
;
256 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
258 sizeof (EFI_IMAGE_FILE_HEADER
) +
259 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
260 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
263 // Get base address from the first section header that doesn't point to code section.
265 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
267 // Read section header from file
269 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
270 Status
= ImageContext
->ImageRead (
271 ImageContext
->Handle
,
276 if (EFI_ERROR (Status
)) {
280 Status
= EFI_NOT_FOUND
;
282 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
284 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
285 // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is
286 // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because
287 // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers
288 // 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
289 // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or
290 // else, these 2 fields should be set to Zero
292 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
293 if (ValueInSectionHeader
!= 0) {
295 // Found first section header that doesn't point to code section.
297 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) > 0) {
299 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field
300 // hold the absolute address of image base running in memory
302 FixLoadingAddress
= ValueInSectionHeader
;
305 // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field
306 // hold the offset relative to a platform-specific top address.
308 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(Private
->LoadModuleAtFixAddressTopAddress
+ (INT64
)ValueInSectionHeader
);
311 // Check if the memory range is available.
313 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (Private
, FixLoadingAddress
, (UINT32
) ImageContext
->ImageSize
);
314 if (!EFI_ERROR(Status
)) {
316 // The assigned address is valid. Return the specified loading address
318 ImageContext
->ImageAddress
= FixLoadingAddress
;
323 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
325 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
));
330 Loads and relocates a PE/COFF image into memory.
331 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.
333 @param FileHandle - Pointer to the FFS file header of the image.
334 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
335 @param ImageAddress - The base address of the relocated PE/COFF image
336 @param ImageSize - The size of the relocated PE/COFF image
337 @param EntryPoint - The entry point of the relocated PE/COFF image
339 @retval EFI_SUCCESS The file was loaded and relocated
340 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
341 @retval EFI_WARN_BUFFER_TOO_SMALL
342 There is not enough heap to allocate the requested size.
343 This will not prevent the XIP image from being invoked.
347 LoadAndRelocatePeCoffImage (
348 IN EFI_PEI_FILE_HANDLE FileHandle
,
350 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
351 OUT UINT64
*ImageSize
,
352 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
356 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
357 PEI_CORE_INSTANCE
*Private
;
358 UINT64 AlignImageSize
;
360 EFI_STATUS ReturnStatus
;
363 BOOLEAN IsRegisterForShadow
;
364 EFI_FV_FILE_INFO FileInfo
;
366 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
368 ReturnStatus
= EFI_SUCCESS
;
370 ZeroMem (&ImageContext
, sizeof (ImageContext
));
371 ImageContext
.Handle
= Pe32Data
;
372 Status
= GetImageReadFunction (&ImageContext
);
374 ASSERT_EFI_ERROR (Status
);
376 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
377 if (EFI_ERROR (Status
)) {
382 // Initilize local IsS3Boot and IsRegisterForShadow variable
385 if (Private
->HobList
.HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
) {
388 IsRegisterForShadow
= FALSE
;
389 if ((Private
->CurrentFileHandle
== FileHandle
)
390 && (Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] == PEIM_STATE_REGISTER_FOR_SHADOW
)) {
391 IsRegisterForShadow
= TRUE
;
395 // XIP image that ImageAddress is same to Image handle.
397 if (ImageContext
.ImageAddress
== (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
402 // Get file type first
404 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &FileInfo
);
405 ASSERT_EFI_ERROR (Status
);
408 // Check whether the file type is PEI module.
411 if (FileInfo
.FileType
== EFI_FV_FILETYPE_PEI_CORE
||
412 FileInfo
.FileType
== EFI_FV_FILETYPE_PEIM
||
413 FileInfo
.FileType
== EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
) {
418 // When Image has no reloc section, it can't be relocated into memory.
420 if (ImageContext
.RelocationsStripped
&& (Private
->PeiMemoryInstalled
) && ((!IsPeiModule
) ||
421 (!IsS3Boot
&& (PcdGetBool (PcdShadowPeimOnBoot
) || IsRegisterForShadow
)) || (IsS3Boot
&& PcdGetBool (PcdShadowPeimOnS3Boot
)))) {
422 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN
) Pe32Data
));
426 // Set default base address to current image address.
428 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
431 // Allocate Memory for the image when memory is ready, and image is relocatable.
432 // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.
433 // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.
435 if ((!ImageContext
.RelocationsStripped
) && (Private
->PeiMemoryInstalled
) && ((!IsPeiModule
) ||
436 (!IsS3Boot
&& (PcdGetBool (PcdShadowPeimOnBoot
) || IsRegisterForShadow
)) || (IsS3Boot
&& PcdGetBool (PcdShadowPeimOnS3Boot
)))) {
438 // Allocate more buffer to avoid buffer overflow.
440 if (ImageContext
.IsTeImage
) {
441 AlignImageSize
= ImageContext
.ImageSize
+ ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
443 AlignImageSize
= ImageContext
.ImageSize
;
446 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
447 AlignImageSize
+= ImageContext
.SectionAlignment
;
450 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
451 Status
= GetPeCoffImageFixLoadingAssignedAddress(&ImageContext
, Private
);
452 if (EFI_ERROR (Status
)){
453 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
455 // The PEIM is not assiged valid address, try to allocate page to load it.
457 Status
= PeiServicesAllocatePages (EfiBootServicesCode
,
458 EFI_SIZE_TO_PAGES ((UINT32
) AlignImageSize
),
459 &ImageContext
.ImageAddress
);
462 Status
= PeiServicesAllocatePages (EfiBootServicesCode
,
463 EFI_SIZE_TO_PAGES ((UINT32
) AlignImageSize
),
464 &ImageContext
.ImageAddress
);
466 if (!EFI_ERROR (Status
)) {
468 // Adjust the Image Address to make sure it is section alignment.
470 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
471 ImageContext
.ImageAddress
=
472 (ImageContext
.ImageAddress
+ ImageContext
.SectionAlignment
- 1) &
473 ~((UINTN
)ImageContext
.SectionAlignment
- 1);
476 // Fix alignment requirement when Load IPF TeImage into memory.
477 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.
479 if (ImageContext
.IsTeImage
) {
480 ImageContext
.ImageAddress
= ImageContext
.ImageAddress
+
481 ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
-
482 sizeof (EFI_TE_IMAGE_HEADER
);
486 // No enough memory resource.
490 // XIP image can still be invoked.
492 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
;
493 ReturnStatus
= EFI_WARN_BUFFER_TOO_SMALL
;
496 // Non XIP image can't be loaded because no enough memory is allocated.
499 return EFI_OUT_OF_RESOURCES
;
505 // Load the image to our new buffer
507 Status
= PeCoffLoaderLoadImage (&ImageContext
);
508 if (EFI_ERROR (Status
)) {
509 if (ImageContext
.ImageError
== IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
) {
510 DEBUG ((DEBUG_ERROR
, "PEIM Image Address 0x%11p doesn't meet with section alignment 0x%x.\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
, ImageContext
.SectionAlignment
));
515 // Relocate the image in our new buffer
517 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
518 if (EFI_ERROR (Status
)) {
523 // Flush the instruction cache so the image data is written before we execute it
525 if (ImageContext
.ImageAddress
!= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Pe32Data
) {
526 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
529 *ImageAddress
= ImageContext
.ImageAddress
;
530 *ImageSize
= ImageContext
.ImageSize
;
531 *EntryPoint
= ImageContext
.EntryPoint
;
537 Loads a PEIM into memory for subsequent execution. If there are compressed
538 images or images that need to be relocated into memory for performance reasons,
539 this service performs that transformation.
541 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
542 @param FileHandle Pointer to the FFS file header of the image.
543 @param ImageAddressArg Pointer to PE/TE image.
544 @param ImageSizeArg Size of PE/TE image.
545 @param EntryPoint Pointer to entry point of specified image file for output.
546 @param AuthenticationState - Pointer to attestation authentication state of image.
548 @retval EFI_SUCCESS Image is successfully loaded.
549 @retval EFI_NOT_FOUND Fail to locate necessary PPI.
550 @retval EFI_UNSUPPORTED Image Machine Type is not supported.
551 @retval EFI_WARN_BUFFER_TOO_SMALL
552 There is not enough heap to allocate the requested size.
553 This will not prevent the XIP image from being invoked.
557 PeiLoadImageLoadImage (
558 IN CONST EFI_PEI_SERVICES
**PeiServices
,
559 IN EFI_PEI_FILE_HANDLE FileHandle
,
560 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
561 OUT UINT64
*ImageSizeArg
, OPTIONAL
562 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
563 OUT UINT32
*AuthenticationState
568 EFI_PHYSICAL_ADDRESS ImageAddress
;
570 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
572 EFI_SECTION_TYPE SearchType1
;
573 EFI_SECTION_TYPE SearchType2
;
577 *AuthenticationState
= 0;
579 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst
)) {
580 SearchType1
= EFI_SECTION_TE
;
581 SearchType2
= EFI_SECTION_PE32
;
583 SearchType1
= EFI_SECTION_PE32
;
584 SearchType2
= EFI_SECTION_TE
;
588 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
589 // is true, TE will be searched first).
591 Status
= PeiServicesFfsFindSectionData3 (
599 // If we didn't find a first exe section, try to find the second exe section.
601 if (EFI_ERROR (Status
)) {
602 Status
= PeiServicesFfsFindSectionData3 (
609 if (EFI_ERROR (Status
)) {
611 // PEI core only carry the loader function for TE and PE32 executables
612 // If this two section does not exist, just return.
618 DEBUG ((DEBUG_INFO
, "Loading PEIM %g\n", FileHandle
));
621 // If memory is installed, perform the shadow operations
623 Status
= LoadAndRelocatePeCoffImage (
631 ASSERT_EFI_ERROR (Status
);
634 if (EFI_ERROR (Status
)) {
639 // Got the entry point from the loaded Pe32Data
641 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
642 *EntryPoint
= ImageEntryPoint
;
644 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
646 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
647 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine
)) {
648 return EFI_UNSUPPORTED
;
652 if (ImageAddressArg
!= NULL
) {
653 *ImageAddressArg
= ImageAddress
;
656 if (ImageSizeArg
!= NULL
) {
657 *ImageSizeArg
= ImageSize
;
662 CHAR8 EfiFileName
[512];
667 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
669 if (Machine
!= EFI_IMAGE_MACHINE_IA64
) {
670 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)*EntryPoint
));
673 // For IPF Image, the real entry point should be print.
675 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)*EntryPoint
)));
679 // Print Module Name by PeImage PDB file name.
681 AsciiString
= PeCoffLoaderGetPdbPointer (Pe32Data
);
683 if (AsciiString
!= NULL
) {
685 for (Index
= 0; AsciiString
[Index
] != 0; Index
++) {
686 if (AsciiString
[Index
] == '\\' || AsciiString
[Index
] == '/') {
687 StartIndex
= Index
+ 1;
692 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
693 // The PDB file name is limited in the range of 0~511.
694 // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.
696 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
697 EfiFileName
[Index
] = AsciiString
[Index
+ StartIndex
];
698 if (EfiFileName
[Index
] == 0) {
699 EfiFileName
[Index
] = '.';
701 if (EfiFileName
[Index
] == '.') {
702 EfiFileName
[Index
+ 1] = 'e';
703 EfiFileName
[Index
+ 2] = 'f';
704 EfiFileName
[Index
+ 3] = 'i';
705 EfiFileName
[Index
+ 4] = 0;
710 if (Index
== sizeof (EfiFileName
) - 4) {
711 EfiFileName
[Index
] = 0;
714 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a", EfiFileName
));
719 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
727 The wrapper function of PeiLoadImageLoadImage().
729 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
730 @param FileHandle - Pointer to the FFS file header of the image.
731 @param ImageAddressArg - Pointer to PE/TE image.
732 @param ImageSizeArg - Size of PE/TE image.
733 @param EntryPoint - Pointer to entry point of specified image file for output.
734 @param AuthenticationState - Pointer to attestation authentication state of image.
736 @return Status of PeiLoadImageLoadImage().
741 PeiLoadImageLoadImageWrapper (
742 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
743 IN EFI_PEI_FILE_HANDLE FileHandle
,
744 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
745 OUT UINT64
*ImageSizeArg
, OPTIONAL
746 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
747 OUT UINT32
*AuthenticationState
750 return PeiLoadImageLoadImage (
751 GetPeiServicesTablePointer (),
761 Check whether the input image has the relocation.
763 @param Pe32Data Pointer to the PE/COFF or TE image.
765 @retval TRUE Relocation is stripped.
766 @retval FALSE Relocation is not stripped.
774 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
775 EFI_IMAGE_DOS_HEADER
*DosHdr
;
777 ASSERT (Pe32Data
!= NULL
);
779 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
780 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
782 // DOS image header is present, so read the PE header after the DOS image header.
784 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
787 // DOS image header is not present, so PE header is at the image base.
789 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
793 // Three cases with regards to relocations:
794 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
795 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
796 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
797 // has no base relocs to apply
798 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
800 // Look at the file header to determine if relocations have been stripped, and
801 // save this info in the image context for later use.
803 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
804 if ((Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
809 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
810 if ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0) {
821 Routine to load image file for subsequent execution by LoadFile Ppi.
822 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE
823 XIP image format is used.
825 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
826 @param FileHandle - Pointer to the FFS file header of the image.
827 @param PeimState - The dispatch state of the input PEIM handle.
828 @param EntryPoint - Pointer to entry point of specified image file for output.
829 @param AuthenticationState - Pointer to attestation authentication state of image.
831 @retval EFI_SUCCESS - Image is successfully loaded.
832 @retval EFI_NOT_FOUND - Fail to locate necessary PPI
833 @retval Others - Fail to load file.
838 IN CONST EFI_PEI_SERVICES
**PeiServices
,
839 IN EFI_PEI_FILE_HANDLE FileHandle
,
841 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
842 OUT UINT32
*AuthenticationState
845 EFI_STATUS PpiStatus
;
848 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
849 EFI_PHYSICAL_ADDRESS ImageAddress
;
855 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
856 // one at a time, until one reports EFI_SUCCESS.
860 PpiStatus
= PeiServicesLocatePpi (
861 &gEfiPeiLoadFilePpiGuid
,
866 if (!EFI_ERROR (PpiStatus
)) {
867 Status
= LoadFile
->LoadFile (
875 if (!EFI_ERROR (Status
) || Status
== EFI_WARN_BUFFER_TOO_SMALL
) {
877 // The shadowed PEIM must be relocatable.
879 if (PeimState
== PEIM_STATE_REGISTER_FOR_SHADOW
) {
880 IsStrip
= RelocationIsStrip ((VOID
*) (UINTN
) ImageAddress
);
883 return EFI_UNSUPPORTED
;
888 // The image to be started must have the machine type supported by PeiCore.
890 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
)));
891 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID
*) (UINTN
) ImageAddress
))) {
892 return EFI_UNSUPPORTED
;
898 } while (!EFI_ERROR (PpiStatus
));
906 Install Pei Load File PPI.
909 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
910 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.
914 InitializeImageServices (
915 IN PEI_CORE_INSTANCE
*PrivateData
,
916 IN PEI_CORE_INSTANCE
*OldCoreData
919 if (OldCoreData
== NULL
) {
921 // The first time we are XIP (running from FLASH). We need to remember the
922 // FLASH address so we can reinstall the memory version that runs faster
924 PrivateData
->XipLoadFile
= &gPpiLoadFilePpiList
;
925 PeiServicesInstallPpi (PrivateData
->XipLoadFile
);
928 // 2nd time we are running from memory so replace the XIP version with the
929 // new memory version.
931 PeiServicesReInstallPpi (PrivateData
->XipLoadFile
, &gPpiLoadFilePpiList
);