3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Pei Core Load Image Support
28 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
32 FileHandle - The handle to the PE/COFF file
33 FileOffset - The offset, in bytes, into the file to read
34 ReadSize - The number of bytes to read from the file starting at FileOffset
35 Buffer - A pointer to the buffer to read the data into.
39 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
44 PeiLoadImageLoadImage (
45 IN EFI_PEI_SERVICES
**PeiServices
,
46 IN EFI_PEI_FILE_HANDLE FileHandle
,
47 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
48 OUT UINT64
*ImageSizeArg
, OPTIONAL
49 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
50 OUT UINT32
*AuthenticationState
56 Routine for loading file image.
60 PeiServices - The PEI core services table.
61 FileHandle - Pointer to the FFS file header of the image.
62 ImageAddressArg - Pointer to PE/TE image.
63 ImageSizeArg - Size of PE/TE image.
64 EntryPoint - Pointer to entry point of specified image file for output.
65 AuthenticationState - Pointer to attestation authentication state of image.
69 Status - EFI_SUCCESS - Image is successfully loaded.
70 EFI_NOT_FOUND - Fail to locate necessary PPI
71 Others - Fail to load file.
78 PeiLoadImageLoadImageWrapper (
79 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
80 IN EFI_PEI_FILE_HANDLE FileHandle
,
81 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
82 OUT UINT64
*ImageSizeArg
, OPTIONAL
83 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
84 OUT UINT32
*AuthenticationState
90 The wrapper function of PeiLoadImageLoadImage().
94 This - Pointer to EFI_PEI_LOAD_FILE_PPI.
95 PeiServices - The PEI core services table.
96 FileHandle - Pointer to the FFS file header of the image.
97 ImageAddressArg - Pointer to PE/TE image.
98 ImageSizeArg - Size of PE/TE image.
99 EntryPoint - Pointer to entry point of specified image file for output.
100 AuthenticationState - Pointer to attestation authentication state of image.
109 STATIC EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi
= {
110 PeiLoadImageLoadImageWrapper
114 STATIC EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList
= {
115 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
116 &gEfiPeiLoadFilePpiGuid
,
125 IN OUT UINTN
*ReadSize
,
132 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
136 FileHandle - The handle to the PE/COFF file
137 FileOffset - The offset, in bytes, into the file to read
138 ReadSize - The number of bytes to read from the file starting at FileOffset
139 Buffer - A pointer to the buffer to read the data into.
143 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
151 Destination8
= Buffer
;
152 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
155 *(Destination8
++) = *(Source8
++);
162 GetImageReadFunction (
163 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
169 Support routine to return the Image Read
173 PeiServices - PEI Services Table
175 ImageContext - The context of the image being loaded
179 EFI_SUCCESS - If Image function location is found
185 MemoryBuffer
= AllocatePages (0x400 / EFI_PAGE_SIZE
+ 1);
186 ASSERT (MemoryBuffer
!= NULL
);
188 CopyMem (MemoryBuffer
, (CONST VOID
*) (UINTN
) PeiImageRead
, 0x400);
190 ImageContext
->ImageRead
= (PE_COFF_LOADER_READ_FILE
) (UINTN
) MemoryBuffer
;
197 LoadAndRelocatePeCoffImage (
199 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
200 OUT UINT64
*ImageSize
,
201 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
207 Loads and relocates a PE/COFF image into memory.
211 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
213 ImageAddress - The base address of the relocated PE/COFF image
215 ImageSize - The size of the relocated PE/COFF image
217 EntryPoint - The entry point of the relocated PE/COFF image
221 EFI_SUCCESS - The file was loaded and relocated
223 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
228 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
230 ZeroMem (&ImageContext
, sizeof (ImageContext
));
231 ImageContext
.Handle
= Pe32Data
;
232 Status
= GetImageReadFunction (&ImageContext
);
234 ASSERT_EFI_ERROR (Status
);
236 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
237 if (EFI_ERROR (Status
)) {
241 // When Image has no reloc section, it can't be relocated into memory.
243 if (ImageContext
.RelocationsStripped
) {
244 DEBUG ((EFI_D_ERROR
, "The image at 0x%08x without reloc section can't be loaded into memory", (UINTN
) Pe32Data
));
245 return EFI_INVALID_PARAMETER
;
248 // Allocate Memory for the image
250 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) ImageContext
.ImageSize
));
251 ASSERT (ImageContext
.ImageAddress
!= 0);
254 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.
256 if (ImageContext
.IsTeImage
) {
257 ImageContext
.ImageAddress
= ImageContext
.ImageAddress
+
258 ((EFI_TE_IMAGE_HEADER
*) Pe32Data
)->StrippedSize
-
259 sizeof (EFI_TE_IMAGE_HEADER
);
263 // Load the image to our new buffer
265 Status
= PeCoffLoaderLoadImage (&ImageContext
);
266 if (EFI_ERROR (Status
)) {
270 // Relocate the image in our new buffer
272 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
273 if (EFI_ERROR (Status
)) {
278 // Flush the instruction cache so the image data is written before we execute it
280 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
282 *ImageAddress
= ImageContext
.ImageAddress
;
283 *ImageSize
= ImageContext
.ImageSize
;
284 *EntryPoint
= ImageContext
.EntryPoint
;
290 PeiLoadImageLoadImage (
291 IN EFI_PEI_SERVICES
**PeiServices
,
292 IN EFI_PEI_FILE_HANDLE FileHandle
,
293 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
294 OUT UINT64
*ImageSizeArg
, OPTIONAL
295 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
296 OUT UINT32
*AuthenticationState
302 Routine for loading file image.
306 PeiServices - The PEI core services table.
307 FileHandle - Pointer to the FFS file header of the image.
308 ImageAddressArg - Pointer to PE/TE image.
309 ImageSizeArg - Size of PE/TE image.
310 EntryPoint - Pointer to entry point of specified image file for output.
311 AuthenticationState - Pointer to attestation authentication state of image.
315 Status - EFI_SUCCESS - Image is successfully loaded.
316 EFI_NOT_FOUND - Fail to locate necessary PPI
317 Others - Fail to load file.
323 EFI_PHYSICAL_ADDRESS ImageAddress
;
325 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
327 PEI_CORE_INSTANCE
*Private
;
332 *AuthenticationState
= 0;
335 // Try to find a TE section.
337 Status
= PeiServicesFfsFindSectionData (
343 // If we didn't find a TE section, try to find a PE32 section.
345 if (EFI_ERROR (Status
)) {
346 Status
= PeiServicesFfsFindSectionData (
351 if (EFI_ERROR (Status
)) {
353 // PEI core only carry the loader function fro TE and PE32 executables
354 // If this two section does not exist, just return.
360 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
362 if (Private
->PeiMemoryInstalled
&&
363 (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
365 // If memory is installed, perform the shadow operations
367 Status
= LoadAndRelocatePeCoffImage (
374 if (EFI_ERROR (Status
)) {
379 // Got the entry point from the loaded Pe32Data
381 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
382 *EntryPoint
= ImageEntryPoint
;
385 // Retrieve the entry point from the PE/COFF or TE image header
387 ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Pe32Data
;
388 Status
= PeCoffLoaderGetEntryPoint (Pe32Data
, &EntryPointArg
);
389 if (EFI_ERROR (Status
)) {
392 *EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) EntryPointArg
;
395 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
397 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
398 return EFI_UNSUPPORTED
;
401 if (ImageAddressArg
!= NULL
) {
402 *ImageAddressArg
= ImageAddress
;
405 if (ImageSizeArg
!= NULL
) {
406 *ImageSizeArg
= ImageSize
;
410 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
412 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN
) ImageAddress
, *EntryPoint
));
415 CHAR8 AsciiBuffer
[512];
419 AsciiString
= PeCoffLoaderGetPdbPointer (Pe32Data
);
421 if (AsciiString
!= NULL
) {
422 for (Index
= (INT32
) AsciiStrLen (AsciiString
) - 1; Index
>= 0; Index
--) {
423 if (AsciiString
[Index
] == '\\') {
429 for (Index1
= 0; AsciiString
[Index
+ 1 + Index1
] != '.'; Index1
++) {
430 AsciiBuffer
[Index1
] = AsciiString
[Index
+ 1 + Index1
];
432 AsciiBuffer
[Index1
] = '\0';
433 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a.efi", AsciiBuffer
));
439 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
448 PeiLoadImageLoadImageWrapper (
449 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
450 IN EFI_PEI_FILE_HANDLE FileHandle
,
451 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg
, OPTIONAL
452 OUT UINT64
*ImageSizeArg
, OPTIONAL
453 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
454 OUT UINT32
*AuthenticationState
460 The wrapper function of PeiLoadImageLoadImage().
464 This - Pointer to EFI_PEI_LOAD_FILE_PPI.
465 PeiServices - The PEI core services table.
466 FileHandle - Pointer to the FFS file header of the image.
467 ImageAddressArg - Pointer to PE/TE image.
468 ImageSizeArg - Size of PE/TE image.
469 EntryPoint - Pointer to entry point of specified image file for output.
470 AuthenticationState - Pointer to attestation authentication state of image.
478 return PeiLoadImageLoadImage (
479 GetPeiServicesTablePointer (),
490 IN EFI_PEI_SERVICES
**PeiServices
,
491 IN EFI_PEI_FILE_HANDLE FileHandle
,
492 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
493 OUT UINT32
*AuthenticationState
499 Routine for load image file.
503 PeiServices - The PEI core services table.
504 FileHandle - Pointer to the FFS file header of the image.
505 EntryPoint - Pointer to entry point of specified image file for output.
506 AuthenticationState - Pointer to attestation authentication state of image.
510 Status - EFI_SUCCESS - Image is successfully loaded.
511 EFI_NOT_FOUND - Fail to locate necessary PPI
512 Others - Fail to load file.
516 EFI_STATUS PpiStatus
;
519 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
520 EFI_PHYSICAL_ADDRESS ImageAddress
;
524 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
525 // one at a time, until one reports EFI_SUCCESS.
529 PpiStatus
= PeiServicesLocatePpi (
530 &gEfiPeiLoadFilePpiGuid
,
535 if (!EFI_ERROR (PpiStatus
)) {
536 Status
= LoadFile
->LoadFile (
544 if (!EFI_ERROR (Status
)) {
549 } while (!EFI_ERROR (PpiStatus
));
552 // If no instances reports EFI_SUCCESS, then build-in support for
553 // the PE32+/TE XIP image format is used.
555 Status
= PeiLoadImageLoadImage (
568 InitializeImageServices (
569 IN PEI_CORE_INSTANCE
*PrivateData
,
570 IN PEI_CORE_INSTANCE
*OldCoreData
576 Install Pei Load File PPI.
580 PrivateData - Pointer to PEI_CORE_INSTANCE.
581 OldCoreData - Pointer to PEI_CORE_INSTANCE.
589 if (OldCoreData
== NULL
) {
591 // The first time we are XIP (running from FLASH). We need to remember the
592 // FLASH address so we can reinstall the memory version that runs faster
594 PrivateData
->XipLoadFile
= &gPpiLoadFilePpiList
;
595 PeiServicesInstallPpi (PrivateData
->XipLoadFile
);
598 // 2nd time we are running from memory so replace the XIP version with the
599 // new memory version.
601 PeiServicesReInstallPpi (PrivateData
->XipLoadFile
, &gPpiLoadFilePpiList
);