2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2015, 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.
21 LOADED_IMAGE_PRIVATE_DATA
*mCurrentImage
= NULL
;
23 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData
= {
24 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE
,
34 // This code is needed to build the Image handle for the DXE Core
36 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage
= {
37 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
, // Signature
39 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
, // Image type
40 TRUE
, // If entrypoint has been called
43 EFI_LOADED_IMAGE_INFORMATION_REVISION
, // Revision
44 NULL
, // Parent handle
45 NULL
, // System handle
47 NULL
, // Device handle
56 EfiBootServicesCode
, // ImageCodeType
57 EfiBootServicesData
// ImageDataType
59 (EFI_PHYSICAL_ADDRESS
)0, // ImageBasePage
63 EFI_SUCCESS
, // Status
71 NULL
// LoadedImageDevicePath
74 // The field is define for Loading modules at fixed address feature to tracker the PEI code
75 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding memory page
78 GLOBAL_REMOVE_IF_UNREFERENCED UINT64
*mDxeCodeMemoryRangeUsageBitMap
=NULL
;
82 CHAR16
*MachineTypeName
;
86 // EBC machine is not listed in this table, because EBC is in the default supported scopes of other machine type.
88 GLOBAL_REMOVE_IF_UNREFERENCED MACHINE_TYPE_INFO mMachineTypeInfo
[] = {
89 {EFI_IMAGE_MACHINE_IA32
, L
"IA32"},
90 {EFI_IMAGE_MACHINE_IA64
, L
"IA64"},
91 {EFI_IMAGE_MACHINE_X64
, L
"X64"},
92 {EFI_IMAGE_MACHINE_ARMTHUMB_MIXED
, L
"ARM"}
95 UINT16 mDxeCoreImageMachineType
= 0;
98 Return machine type name.
100 @param MachineType The machine type
102 @return machine type name
111 for (Index
= 0; Index
< sizeof(mMachineTypeInfo
)/sizeof(mMachineTypeInfo
[0]); Index
++) {
112 if (mMachineTypeInfo
[Index
].MachineType
== MachineType
) {
113 return mMachineTypeInfo
[Index
].MachineTypeName
;
121 Add the Image Services to EFI Boot Services Table and install the protocol
122 interfaces for this image.
124 @param HobStart The HOB to initialize
130 CoreInitializeImageServices (
135 LOADED_IMAGE_PRIVATE_DATA
*Image
;
136 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
137 UINT64 DxeCoreImageLength
;
138 VOID
*DxeCoreEntryPoint
;
139 EFI_PEI_HOB_POINTERS DxeCoreHob
;
142 // Searching for image hob
144 DxeCoreHob
.Raw
= HobStart
;
145 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
146 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
152 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
154 ASSERT (DxeCoreHob
.Raw
!= NULL
);
156 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
157 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
158 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
159 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
162 // Initialize the fields for an internal driver
164 Image
= &mCorePrivateImage
;
166 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
167 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
168 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
169 Image
->Tpl
= gEfiCurrentTpl
;
170 Image
->Info
.SystemTable
= gDxeCoreST
;
171 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
172 Image
->Info
.ImageSize
= DxeCoreImageLength
;
175 // Install the protocol interfaces for this image
177 Status
= CoreInstallProtocolInterface (
179 &gEfiLoadedImageProtocolGuid
,
180 EFI_NATIVE_INTERFACE
,
183 ASSERT_EFI_ERROR (Status
);
185 mCurrentImage
= Image
;
188 // Fill in DXE globals
190 mDxeCoreImageMachineType
= PeCoffLoaderGetMachineType (Image
->Info
.ImageBase
);
191 gDxeCoreImageHandle
= Image
->Handle
;
192 gDxeCoreLoadedImage
= &Image
->Info
;
194 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
196 // Export DXE Core PE Loader functionality for backward compatibility.
198 Status
= CoreInstallProtocolInterface (
199 &mLoadPe32PrivateData
.Handle
,
200 &gEfiLoadPeImageProtocolGuid
,
201 EFI_NATIVE_INTERFACE
,
202 &mLoadPe32PrivateData
.Pe32Image
210 Read image file (specified by UserHandle) into user specified buffer with specified offset
213 @param UserHandle Image file handle
214 @param Offset Offset to the source file
215 @param ReadSize For input, pointer of size to read; For output,
216 pointer of size actually read.
217 @param Buffer Buffer to write into
219 @retval EFI_SUCCESS Successfully read the specified part of file
228 IN OUT UINTN
*ReadSize
,
233 IMAGE_FILE_HANDLE
*FHand
;
235 if (UserHandle
== NULL
|| ReadSize
== NULL
|| Buffer
== NULL
) {
236 return EFI_INVALID_PARAMETER
;
239 if (MAX_ADDRESS
- Offset
< *ReadSize
) {
240 return EFI_INVALID_PARAMETER
;
243 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
244 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
247 // Move data from our local copy of the file
249 EndPosition
= Offset
+ *ReadSize
;
250 if (EndPosition
> FHand
->SourceSize
) {
251 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
253 if (Offset
>= FHand
->SourceSize
) {
257 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
261 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
262 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
263 The function is only invoked when load modules at fixed address feature is enabled.
265 @param ImageBase The base addres the image will be loaded at.
266 @param ImageSize The size of the image
268 @retval EFI_SUCCESS The memory range the image will be loaded in is available
269 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
272 CheckAndMarkFixLoadingMemoryUsageBitMap (
273 IN EFI_PHYSICAL_ADDRESS ImageBase
,
277 UINT32 DxeCodePageNumber
;
279 EFI_PHYSICAL_ADDRESS DxeCodeBase
;
280 UINTN BaseOffsetPageNumber
;
281 UINTN TopOffsetPageNumber
;
284 // The DXE code range includes RuntimeCodePage range and Boot time code range.
286 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
287 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
288 DxeCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
);
289 DxeCodeBase
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- DxeCodeSize
;
292 // If the memory usage bit map is not initialized, do it. Every bit in the array
293 // indicate the status of the corresponding memory page, available or not
295 if (mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
296 mDxeCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((DxeCodePageNumber
/64) + 1)*sizeof(UINT64
));
299 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
301 if (!gLoadFixedAddressCodeMemoryReady
|| mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
302 return EFI_NOT_FOUND
;
305 // Test the memory range for loading the image in the DXE code range.
307 if (gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
< ImageBase
+ ImageSize
||
308 DxeCodeBase
> ImageBase
) {
309 return EFI_NOT_FOUND
;
312 // Test if the memory is avalaible or not.
314 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- DxeCodeBase
));
315 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- DxeCodeBase
));
316 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
317 if ((mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
319 // This page is already used.
321 return EFI_NOT_FOUND
;
326 // Being here means the memory range is available. So mark the bits for the memory range
328 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
329 mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
335 Get the fixed loadding address from image header assigned by build tool. This function only be called
336 when Loading module at Fixed address feature enabled.
338 @param ImageContext Pointer to the image context structure that describes the PE/COFF
339 image that needs to be examined by this function.
340 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
341 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
345 GetPeCoffImageFixLoadingAssignedAddress(
346 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
349 UINTN SectionHeaderOffset
;
351 EFI_IMAGE_SECTION_HEADER SectionHeader
;
352 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
355 UINT16 NumberOfSections
;
356 IMAGE_FILE_HANDLE
*Handle
;
357 UINT64 ValueInSectionHeader
;
360 Status
= EFI_NOT_FOUND
;
363 // Get PeHeader pointer
365 Handle
= (IMAGE_FILE_HANDLE
*)ImageContext
->Handle
;
366 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )Handle
->Source
+ ImageContext
->PeCoffHeaderOffset
);
367 SectionHeaderOffset
= (UINTN
)(
368 ImageContext
->PeCoffHeaderOffset
+
370 sizeof (EFI_IMAGE_FILE_HEADER
) +
371 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
373 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
376 // Get base address from the first section header that doesn't point to code section.
378 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
380 // Read section header from file
382 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
383 Status
= ImageContext
->ImageRead (
384 ImageContext
->Handle
,
389 if (EFI_ERROR (Status
)) {
392 if (Size
!= sizeof (EFI_IMAGE_SECTION_HEADER
)) {
393 return EFI_NOT_FOUND
;
396 Status
= EFI_NOT_FOUND
;
398 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
400 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
401 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
402 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
403 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero
405 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
406 if (ValueInSectionHeader
!= 0) {
408 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
409 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
410 // relative to top address
412 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) < 0) {
413 ImageContext
->ImageAddress
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
+ (INT64
)(INTN
)ImageContext
->ImageAddress
;
416 // Check if the memory range is avaliable.
418 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext
->ImageAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
422 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
424 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID
*)(UINTN
)(ImageContext
->ImageAddress
), Status
));
428 Loads, relocates, and invokes a PE/COFF image
430 @param BootPolicy If TRUE, indicates that the request originates
431 from the boot manager, and that the boot
432 manager is attempting to load FilePath as a
434 @param Pe32Handle The handle of PE32 image
435 @param Image PE image to be loaded
436 @param DstBuffer The buffer to store the image
437 @param EntryPoint A pointer to the entry point
438 @param Attribute The bit mask of attributes to set for the load
441 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
442 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
443 relocate the PE/COFF file
444 @retval EFI_INVALID_PARAMETER Invalid parameter
445 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
450 IN BOOLEAN BootPolicy
,
452 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
453 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
454 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
459 BOOLEAN DstBufAlocated
;
462 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
464 Image
->ImageContext
.Handle
= Pe32Handle
;
465 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
468 // Get information about the image being loaded
470 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
471 if (EFI_ERROR (Status
)) {
475 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
476 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
478 // The PE/COFF loader can support loading image types that can be executed.
479 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
481 DEBUG ((EFI_D_ERROR
, "Image type %s can't be loaded ", GetMachineTypeName(Image
->ImageContext
.Machine
)));
482 DEBUG ((EFI_D_ERROR
, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType
)));
483 return EFI_UNSUPPORTED
;
488 // Set EFI memory type based on ImageType
490 switch (Image
->ImageContext
.ImageType
) {
491 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
492 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
493 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
495 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
496 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
497 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
499 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
500 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
501 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
502 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
505 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
506 return EFI_UNSUPPORTED
;
510 // Allocate memory of the correct memory type aligned on the required image boundry
512 DstBufAlocated
= FALSE
;
513 if (DstBuffer
== 0) {
515 // Allocate Destination Buffer as caller did not pass it in
518 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
519 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
521 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
524 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
527 // If the image relocations have not been stripped, then load at any address.
528 // Otherwise load at the address at which it was linked.
530 // Memory below 1MB should be treated reserved for CSM and there should be
531 // no modules whose preferred load addresses are below 1MB.
533 Status
= EFI_OUT_OF_RESOURCES
;
535 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
536 // a specified address.
538 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 ) {
539 Status
= GetPeCoffImageFixLoadingAssignedAddress (&(Image
->ImageContext
));
541 if (EFI_ERROR (Status
)) {
543 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
545 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
547 Status
= CoreAllocatePages (
549 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
550 Image
->NumberOfPages
,
551 &Image
->ImageContext
.ImageAddress
555 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
556 Status
= CoreAllocatePages (
558 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
559 Image
->NumberOfPages
,
560 &Image
->ImageContext
.ImageAddress
563 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
564 Status
= CoreAllocatePages (
566 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
567 Image
->NumberOfPages
,
568 &Image
->ImageContext
.ImageAddress
572 if (EFI_ERROR (Status
)) {
575 DstBufAlocated
= TRUE
;
578 // Caller provided the destination buffer
581 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
583 // If the image relocations were stripped, and the caller provided a
584 // destination buffer address that does not match the address that the
585 // image is linked at, then the image cannot be loaded.
587 return EFI_INVALID_PARAMETER
;
590 if (Image
->NumberOfPages
!= 0 &&
591 Image
->NumberOfPages
<
592 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
593 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
594 return EFI_BUFFER_TOO_SMALL
;
597 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
598 Image
->ImageContext
.ImageAddress
= DstBuffer
;
601 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
602 if (!Image
->ImageContext
.IsTeImage
) {
603 Image
->ImageContext
.ImageAddress
=
604 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
605 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
609 // Load the image from the file into the allocated memory
611 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
612 if (EFI_ERROR (Status
)) {
617 // If this is a Runtime Driver, then allocate memory for the FixupData that
618 // is used to relocate the image when SetVirtualAddressMap() is called. The
619 // relocation is done by the Runtime AP.
621 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
622 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
623 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
624 if (Image
->ImageContext
.FixupData
== NULL
) {
625 Status
= EFI_OUT_OF_RESOURCES
;
632 // Relocate the image in memory
634 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
635 if (EFI_ERROR (Status
)) {
640 // Flush the Instruction Cache
642 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
645 // Copy the machine type from the context to the image private data. This
646 // is needed during image unload to know if we should call an EBC protocol
647 // to unload the image.
649 Image
->Machine
= Image
->ImageContext
.Machine
;
652 // Get the image entry point. If it's an EBC image, then call into the
653 // interpreter to create a thunk for the entry point and use the returned
654 // value for the entry point.
656 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
657 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
659 // Locate the EBC interpreter protocol
661 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
662 if (EFI_ERROR(Status
) || Image
->Ebc
== NULL
) {
663 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
668 // Register a callback for flushing the instruction cache so that created
669 // thunks can be flushed.
671 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
672 if (EFI_ERROR(Status
)) {
677 // Create a thunk for the image's entry point. This will be the new
678 // entry point for the image.
680 Status
= Image
->Ebc
->CreateThunk (
683 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
684 (VOID
**) &Image
->EntryPoint
686 if (EFI_ERROR(Status
)) {
692 // Fill in the image information for the Loaded Image Protocol
694 Image
->Type
= Image
->ImageContext
.ImageType
;
695 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
696 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
697 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
698 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
699 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
700 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
702 // Make a list off all the RT images so we can let the RT AP know about them.
704 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
705 if (Image
->RuntimeData
== NULL
) {
708 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
709 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
710 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
711 Image
->RuntimeData
->Handle
= Image
->Handle
;
712 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
713 InsertImageRecord (Image
->RuntimeData
);
718 // Fill in the entry point of the image if it is available
720 if (EntryPoint
!= NULL
) {
721 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
725 // Print the load address and the PDB file name if it is available
732 CHAR8 EfiFileName
[256];
735 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
736 "Loading driver at 0x%11p EntryPoint=0x%11p ",
737 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
738 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
742 // Print Module Name by Pdb file path.
743 // Windows and Unix style file path are all trimmed correctly.
745 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
747 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
748 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
749 StartIndex
= Index
+ 1;
753 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
754 // The PDB file name is limited in the range of 0~255.
755 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
757 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
758 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
759 if (EfiFileName
[Index
] == 0) {
760 EfiFileName
[Index
] = '.';
762 if (EfiFileName
[Index
] == '.') {
763 EfiFileName
[Index
+ 1] = 'e';
764 EfiFileName
[Index
+ 2] = 'f';
765 EfiFileName
[Index
+ 3] = 'i';
766 EfiFileName
[Index
+ 4] = 0;
771 if (Index
== sizeof (EfiFileName
) - 4) {
772 EfiFileName
[Index
] = 0;
774 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
776 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
788 if (DstBufAlocated
) {
789 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
792 if (Image
->ImageContext
.FixupData
!= NULL
) {
793 CoreFreePool (Image
->ImageContext
.FixupData
);
802 Get the image's private data from its handle.
804 @param ImageHandle The image handle
806 @return Return the image private data associated with ImageHandle.
809 LOADED_IMAGE_PRIVATE_DATA
*
810 CoreLoadedImageInfo (
811 IN EFI_HANDLE ImageHandle
815 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
816 LOADED_IMAGE_PRIVATE_DATA
*Image
;
818 Status
= CoreHandleProtocol (
820 &gEfiLoadedImageProtocolGuid
,
821 (VOID
**)&LoadedImage
823 if (!EFI_ERROR (Status
)) {
824 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
826 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
835 Unloads EFI image from memory.
837 @param Image EFI image
838 @param FreePage Free allocated pages
842 CoreUnloadAndCloseImage (
843 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
849 EFI_HANDLE
*HandleBuffer
;
851 EFI_GUID
**ProtocolGuidArray
;
854 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
859 ProtocolGuidArray
= NULL
;
861 if (Image
->Started
) {
862 UnregisterMemoryProfileImage (Image
);
865 if (Image
->Ebc
!= NULL
) {
867 // If EBC protocol exists we must perform cleanups for this image.
869 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
873 // Unload image, free Image->ImageContext->ModHandle
875 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
878 // Free our references to the image handle
880 if (Image
->Handle
!= NULL
) {
882 Status
= CoreLocateHandleBuffer (
889 if (!EFI_ERROR (Status
)) {
890 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
891 Status
= CoreProtocolsPerHandle (
892 HandleBuffer
[HandleIndex
],
896 if (!EFI_ERROR (Status
)) {
897 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
898 Status
= CoreOpenProtocolInformation (
899 HandleBuffer
[HandleIndex
],
900 ProtocolGuidArray
[ProtocolIndex
],
904 if (!EFI_ERROR (Status
)) {
905 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
906 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
907 Status
= CoreCloseProtocol (
908 HandleBuffer
[HandleIndex
],
909 ProtocolGuidArray
[ProtocolIndex
],
911 OpenInfo
[OpenInfoIndex
].ControllerHandle
915 if (OpenInfo
!= NULL
) {
916 CoreFreePool(OpenInfo
);
920 if (ProtocolGuidArray
!= NULL
) {
921 CoreFreePool(ProtocolGuidArray
);
925 if (HandleBuffer
!= NULL
) {
926 CoreFreePool (HandleBuffer
);
930 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
932 Status
= CoreUninstallProtocolInterface (
934 &gEfiLoadedImageDevicePathProtocolGuid
,
935 Image
->LoadedImageDevicePath
938 Status
= CoreUninstallProtocolInterface (
940 &gEfiLoadedImageProtocolGuid
,
944 if (Image
->ImageContext
.HiiResourceData
!= 0) {
945 Status
= CoreUninstallProtocolInterface (
947 &gEfiHiiPackageListProtocolGuid
,
948 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
954 if (Image
->RuntimeData
!= NULL
) {
955 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
957 // Remove the Image from the Runtime Image list as we are about to Free it!
959 RemoveEntryList (&Image
->RuntimeData
->Link
);
960 RemoveImageRecord (Image
->RuntimeData
);
962 CoreFreePool (Image
->RuntimeData
);
966 // Free the Image from memory
968 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
969 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
973 // Done with the Image structure
975 if (Image
->Info
.FilePath
!= NULL
) {
976 CoreFreePool (Image
->Info
.FilePath
);
979 if (Image
->LoadedImageDevicePath
!= NULL
) {
980 CoreFreePool (Image
->LoadedImageDevicePath
);
983 if (Image
->FixupData
!= NULL
) {
984 CoreFreePool (Image
->FixupData
);
987 CoreFreePool (Image
);
992 Loads an EFI image into memory and returns a handle to the image.
994 @param BootPolicy If TRUE, indicates that the request originates
995 from the boot manager, and that the boot
996 manager is attempting to load FilePath as a
998 @param ParentImageHandle The caller's image handle.
999 @param FilePath The specific file path from which the image is
1001 @param SourceBuffer If not NULL, a pointer to the memory location
1002 containing a copy of the image to be loaded.
1003 @param SourceSize The size in bytes of SourceBuffer.
1004 @param DstBuffer The buffer to store the image
1005 @param NumberOfPages If not NULL, it inputs a pointer to the page
1006 number of DstBuffer and outputs a pointer to
1007 the page number of the image. If this number is
1008 not enough, return EFI_BUFFER_TOO_SMALL and
1009 this parameter contains the required number.
1010 @param ImageHandle Pointer to the returned image handle that is
1011 created when the image is successfully loaded.
1012 @param EntryPoint A pointer to the entry point
1013 @param Attribute The bit mask of attributes to set for the load
1016 @retval EFI_SUCCESS The image was loaded into memory.
1017 @retval EFI_NOT_FOUND The FilePath was not found.
1018 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1019 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
1020 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1021 path cannot be parsed to locate the proper
1022 protocol for loading the file.
1023 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1025 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1027 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1028 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1029 image from being loaded. NULL is returned in *ImageHandle.
1030 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1031 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1032 platform policy specifies that the image should not be started.
1036 CoreLoadImageCommon (
1037 IN BOOLEAN BootPolicy
,
1038 IN EFI_HANDLE ParentImageHandle
,
1039 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1040 IN VOID
*SourceBuffer OPTIONAL
,
1041 IN UINTN SourceSize
,
1042 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1043 IN OUT UINTN
*NumberOfPages OPTIONAL
,
1044 OUT EFI_HANDLE
*ImageHandle
,
1045 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1049 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1050 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
1051 IMAGE_FILE_HANDLE FHand
;
1053 EFI_STATUS SecurityStatus
;
1054 EFI_HANDLE DeviceHandle
;
1055 UINT32 AuthenticationStatus
;
1056 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
1057 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
1059 BOOLEAN ImageIsFromFv
;
1061 SecurityStatus
= EFI_SUCCESS
;
1063 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
1067 // The caller must pass in a valid ParentImageHandle
1069 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
1070 return EFI_INVALID_PARAMETER
;
1073 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
1074 if (ParentImage
== NULL
) {
1075 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
1076 return EFI_INVALID_PARAMETER
;
1079 ZeroMem (&FHand
, sizeof (IMAGE_FILE_HANDLE
));
1080 FHand
.Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
1081 OriginalFilePath
= FilePath
;
1082 HandleFilePath
= FilePath
;
1083 DeviceHandle
= NULL
;
1084 Status
= EFI_SUCCESS
;
1085 AuthenticationStatus
= 0;
1086 ImageIsFromFv
= FALSE
;
1089 // If the caller passed a copy of the file, then just use it
1091 if (SourceBuffer
!= NULL
) {
1092 FHand
.Source
= SourceBuffer
;
1093 FHand
.SourceSize
= SourceSize
;
1094 Status
= CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1095 if (EFI_ERROR (Status
)) {
1096 DeviceHandle
= NULL
;
1098 if (SourceSize
> 0) {
1099 Status
= EFI_SUCCESS
;
1101 Status
= EFI_LOAD_ERROR
;
1104 if (FilePath
== NULL
) {
1105 return EFI_INVALID_PARAMETER
;
1108 // Get the source file buffer by its device path.
1110 FHand
.Source
= GetFileBufferByFilePath (
1114 &AuthenticationStatus
1116 if (FHand
.Source
== NULL
) {
1117 Status
= EFI_NOT_FOUND
;
1120 // Try to get the image device handle by checking the match protocol.
1122 FHand
.FreeBuffer
= TRUE
;
1123 Status
= CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1124 if (!EFI_ERROR (Status
)) {
1125 ImageIsFromFv
= TRUE
;
1127 HandleFilePath
= FilePath
;
1128 Status
= CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1129 if (EFI_ERROR (Status
)) {
1131 HandleFilePath
= FilePath
;
1132 Status
= CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1134 if (EFI_ERROR (Status
)) {
1135 HandleFilePath
= FilePath
;
1136 Status
= CoreLocateDevicePath (&gEfiLoadFileProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1143 if (EFI_ERROR (Status
)) {
1148 if (gSecurity2
!= NULL
) {
1150 // Verify File Authentication through the Security2 Architectural Protocol
1152 SecurityStatus
= gSecurity2
->FileAuthentication (
1159 if (!EFI_ERROR (SecurityStatus
) && ImageIsFromFv
) {
1161 // When Security2 is installed, Security Architectural Protocol must be published.
1163 ASSERT (gSecurity
!= NULL
);
1166 // Verify the Authentication Status through the Security Architectural Protocol
1167 // Only on images that have been read using Firmware Volume protocol.
1169 SecurityStatus
= gSecurity
->FileAuthenticationState (
1171 AuthenticationStatus
,
1175 } else if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
1177 // Verify the Authentication Status through the Security Architectural Protocol
1179 SecurityStatus
= gSecurity
->FileAuthenticationState (
1181 AuthenticationStatus
,
1187 // Check Security Status.
1189 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
1190 if (SecurityStatus
== EFI_ACCESS_DENIED
) {
1192 // Image was not loaded because the platform policy prohibits the image from being loaded.
1193 // It's the only place we could meet EFI_ACCESS_DENIED.
1195 *ImageHandle
= NULL
;
1197 Status
= SecurityStatus
;
1203 // Allocate a new image structure
1205 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
1206 if (Image
== NULL
) {
1207 Status
= EFI_OUT_OF_RESOURCES
;
1212 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1214 FilePath
= OriginalFilePath
;
1215 if (DeviceHandle
!= NULL
) {
1216 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
1217 if (!EFI_ERROR (Status
)) {
1218 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1219 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
1223 // Initialize the fields for an internal driver
1225 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
1226 Image
->Info
.SystemTable
= gDxeCoreST
;
1227 Image
->Info
.DeviceHandle
= DeviceHandle
;
1228 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1229 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
1230 Image
->Info
.ParentHandle
= ParentImageHandle
;
1233 if (NumberOfPages
!= NULL
) {
1234 Image
->NumberOfPages
= *NumberOfPages
;
1236 Image
->NumberOfPages
= 0 ;
1240 // Install the protocol interfaces for this image
1241 // don't fire notifications yet
1243 Status
= CoreInstallProtocolInterfaceNotify (
1245 &gEfiLoadedImageProtocolGuid
,
1246 EFI_NATIVE_INTERFACE
,
1250 if (EFI_ERROR (Status
)) {
1255 // Load the image. If EntryPoint is Null, it will not be set.
1257 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
1258 if (EFI_ERROR (Status
)) {
1259 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
1260 if (NumberOfPages
!= NULL
) {
1261 *NumberOfPages
= Image
->NumberOfPages
;
1267 if (NumberOfPages
!= NULL
) {
1268 *NumberOfPages
= Image
->NumberOfPages
;
1272 // Register the image in the Debug Image Info Table if the attribute is set
1274 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
1275 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
1279 //Reinstall loaded image protocol to fire any notifications
1281 Status
= CoreReinstallProtocolInterface (
1283 &gEfiLoadedImageProtocolGuid
,
1287 if (EFI_ERROR (Status
)) {
1292 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1293 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1295 if (OriginalFilePath
!= NULL
) {
1296 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
1300 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1302 Status
= CoreInstallProtocolInterface (
1304 &gEfiLoadedImageDevicePathProtocolGuid
,
1305 EFI_NATIVE_INTERFACE
,
1306 Image
->LoadedImageDevicePath
1308 if (EFI_ERROR (Status
)) {
1313 // Install HII Package List Protocol onto the image handle
1315 if (Image
->ImageContext
.HiiResourceData
!= 0) {
1316 Status
= CoreInstallProtocolInterface (
1318 &gEfiHiiPackageListProtocolGuid
,
1319 EFI_NATIVE_INTERFACE
,
1320 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
1322 if (EFI_ERROR (Status
)) {
1328 // Success. Return the image handle
1330 *ImageHandle
= Image
->Handle
;
1334 // All done accessing the source file
1335 // If we allocated the Source buffer, free it
1337 if (FHand
.FreeBuffer
) {
1338 CoreFreePool (FHand
.Source
);
1342 // There was an error. If there's an Image structure, free it
1344 if (EFI_ERROR (Status
)) {
1345 if (Image
!= NULL
) {
1346 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
1349 } else if (EFI_ERROR (SecurityStatus
)) {
1350 Status
= SecurityStatus
;
1354 // Track the return status from LoadImage.
1356 if (Image
!= NULL
) {
1357 Image
->LoadImageStatus
= Status
;
1367 Loads an EFI image into memory and returns a handle to the image.
1369 @param BootPolicy If TRUE, indicates that the request originates
1370 from the boot manager, and that the boot
1371 manager is attempting to load FilePath as a
1373 @param ParentImageHandle The caller's image handle.
1374 @param FilePath The specific file path from which the image is
1376 @param SourceBuffer If not NULL, a pointer to the memory location
1377 containing a copy of the image to be loaded.
1378 @param SourceSize The size in bytes of SourceBuffer.
1379 @param ImageHandle Pointer to the returned image handle that is
1380 created when the image is successfully loaded.
1382 @retval EFI_SUCCESS The image was loaded into memory.
1383 @retval EFI_NOT_FOUND The FilePath was not found.
1384 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1385 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1386 path cannot be parsed to locate the proper
1387 protocol for loading the file.
1388 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1390 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1392 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1393 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1394 image from being loaded. NULL is returned in *ImageHandle.
1395 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1396 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1397 platform policy specifies that the image should not be started.
1403 IN BOOLEAN BootPolicy
,
1404 IN EFI_HANDLE ParentImageHandle
,
1405 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1406 IN VOID
*SourceBuffer OPTIONAL
,
1407 IN UINTN SourceSize
,
1408 OUT EFI_HANDLE
*ImageHandle
1417 Tick
= GetPerformanceCounter ();
1420 Status
= CoreLoadImageCommon (
1426 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1430 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1434 if (!EFI_ERROR (Status
)) {
1436 // ImageHandle will be valid only Status is success.
1438 Handle
= *ImageHandle
;
1441 PERF_START (Handle
, "LoadImage:", NULL
, Tick
);
1442 PERF_END (Handle
, "LoadImage:", NULL
, 0);
1450 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1452 @param This Calling context
1453 @param ParentImageHandle The caller's image handle.
1454 @param FilePath The specific file path from which the image is
1456 @param SourceBuffer If not NULL, a pointer to the memory location
1457 containing a copy of the image to be loaded.
1458 @param SourceSize The size in bytes of SourceBuffer.
1459 @param DstBuffer The buffer to store the image.
1460 @param NumberOfPages For input, specifies the space size of the
1461 image by caller if not NULL. For output,
1462 specifies the actual space size needed.
1463 @param ImageHandle Image handle for output.
1464 @param EntryPoint Image entry point for output.
1465 @param Attribute The bit mask of attributes to set for the load
1468 @retval EFI_SUCCESS The image was loaded into memory.
1469 @retval EFI_NOT_FOUND The FilePath was not found.
1470 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1471 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1472 path cannot be parsed to locate the proper
1473 protocol for loading the file.
1474 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1476 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1478 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1479 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1480 image from being loaded. NULL is returned in *ImageHandle.
1481 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1482 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1483 platform policy specifies that the image should not be started.
1489 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1490 IN EFI_HANDLE ParentImageHandle
,
1491 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1492 IN VOID
*SourceBuffer OPTIONAL
,
1493 IN UINTN SourceSize
,
1494 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1495 OUT UINTN
*NumberOfPages OPTIONAL
,
1496 OUT EFI_HANDLE
*ImageHandle
,
1497 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1507 Tick
= GetPerformanceCounter ();
1510 Status
= CoreLoadImageCommon (
1524 if (!EFI_ERROR (Status
)) {
1526 // ImageHandle will be valid only Status is success.
1528 Handle
= *ImageHandle
;
1531 PERF_START (Handle
, "LoadImage:", NULL
, Tick
);
1532 PERF_END (Handle
, "LoadImage:", NULL
, 0);
1539 Transfer control to a loaded image's entry point.
1541 @param ImageHandle Handle of image to be started.
1542 @param ExitDataSize Pointer of the size to ExitData
1543 @param ExitData Pointer to a pointer to a data buffer that
1544 includes a Null-terminated string,
1545 optionally followed by additional binary data.
1546 The string is a description that the caller may
1547 use to further indicate the reason for the
1550 @retval EFI_INVALID_PARAMETER Invalid parameter
1551 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1552 @retval EFI_SECURITY_VIOLATION The current platform policy specifies that the image should not be started.
1553 @retval EFI_SUCCESS Successfully transfer control to the image's
1560 IN EFI_HANDLE ImageHandle
,
1561 OUT UINTN
*ExitDataSize
,
1562 OUT CHAR16
**ExitData OPTIONAL
1566 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1567 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1568 UINT64 HandleDatabaseKey
;
1574 Handle
= ImageHandle
;
1576 Image
= CoreLoadedImageInfo (ImageHandle
);
1577 if (Image
== NULL
|| Image
->Started
) {
1578 return EFI_INVALID_PARAMETER
;
1580 if (EFI_ERROR (Image
->LoadImageStatus
)) {
1581 return Image
->LoadImageStatus
;
1585 // The image to be started must have the machine type supported by DxeCore.
1587 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1589 // Do not ASSERT here, because image might be loaded via EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED
1590 // But it can not be started.
1592 DEBUG ((EFI_D_ERROR
, "Image type %s can't be started ", GetMachineTypeName(Image
->Machine
)));
1593 DEBUG ((EFI_D_ERROR
, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType
)));
1594 return EFI_UNSUPPORTED
;
1598 Tick
= GetPerformanceCounter ();
1603 // Push the current start image context, and
1604 // link the current image to the head. This is the
1605 // only image that can call Exit()
1607 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1608 LastImage
= mCurrentImage
;
1609 mCurrentImage
= Image
;
1610 Image
->Tpl
= gEfiCurrentTpl
;
1613 // Set long jump for Exit() support
1614 // JumpContext must be aligned on a CPU specific boundary.
1615 // Overallocate the buffer and force the required alignment
1617 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1618 if (Image
->JumpBuffer
== NULL
) {
1620 // Image may be unloaded after return with failure,
1621 // then ImageHandle may be invalid, so use NULL handle to record perf log.
1623 PERF_START (NULL
, "StartImage:", NULL
, Tick
);
1624 PERF_END (NULL
, "StartImage:", NULL
, 0);
1625 return EFI_OUT_OF_RESOURCES
;
1627 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1629 SetJumpFlag
= SetJump (Image
->JumpContext
);
1631 // The initial call to SetJump() must always return 0.
1632 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1634 if (SetJumpFlag
== 0) {
1635 RegisterMemoryProfileImage (Image
, (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
? EFI_FV_FILETYPE_APPLICATION
: EFI_FV_FILETYPE_DRIVER
));
1637 // Call the image's entry point
1639 Image
->Started
= TRUE
;
1640 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1643 // Add some debug information if the image returned with error.
1644 // This make the user aware and check if the driver image have already released
1645 // all the resource in this situation.
1647 DEBUG_CODE_BEGIN ();
1648 if (EFI_ERROR (Image
->Status
)) {
1649 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1654 // If the image returns, exit it through Exit()
1656 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1660 // Image has completed. Verify the tpl is the same
1662 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1663 CoreRestoreTpl (Image
->Tpl
);
1665 CoreFreePool (Image
->JumpBuffer
);
1668 // Pop the current start image context
1670 mCurrentImage
= LastImage
;
1673 // Go connect any handles that were created or modified while the image executed.
1675 CoreConnectHandlesByKey (HandleDatabaseKey
);
1678 // Handle the image's returned ExitData
1680 DEBUG_CODE_BEGIN ();
1681 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1683 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1684 if (Image
->ExitData
!= NULL
) {
1685 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1687 DEBUG ((DEBUG_LOAD
, "\n"));
1692 // Return the exit data to the caller
1694 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1695 *ExitDataSize
= Image
->ExitDataSize
;
1696 *ExitData
= Image
->ExitData
;
1699 // Caller doesn't want the exit data, free it
1701 CoreFreePool (Image
->ExitData
);
1702 Image
->ExitData
= NULL
;
1706 // Save the Status because Image will get destroyed if it is unloaded.
1708 Status
= Image
->Status
;
1711 // If the image returned an error, or if the image is an application
1714 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1715 CoreUnloadAndCloseImage (Image
, TRUE
);
1717 // ImageHandle may be invalid after the image is unloaded, so use NULL handle to record perf log.
1725 PERF_START (Handle
, "StartImage:", NULL
, Tick
);
1726 PERF_END (Handle
, "StartImage:", NULL
, 0);
1731 Terminates the currently loaded EFI image and returns control to boot services.
1733 @param ImageHandle Handle that identifies the image. This
1734 parameter is passed to the image on entry.
1735 @param Status The image's exit code.
1736 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1737 ExitStatus is EFI_SUCCESS.
1738 @param ExitData Pointer to a data buffer that includes a
1739 Null-terminated Unicode string, optionally
1740 followed by additional binary data. The string
1741 is a description that the caller may use to
1742 further indicate the reason for the image's
1745 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1747 @retval EFI_SUCCESS Successfully terminates the currently loaded
1749 @retval EFI_ACCESS_DENIED Should never reach there.
1750 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1756 IN EFI_HANDLE ImageHandle
,
1757 IN EFI_STATUS Status
,
1758 IN UINTN ExitDataSize
,
1759 IN CHAR16
*ExitData OPTIONAL
1762 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1766 // Prevent possible reentrance to this function
1767 // for the same ImageHandle
1769 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1771 Image
= CoreLoadedImageInfo (ImageHandle
);
1772 if (Image
== NULL
) {
1773 Status
= EFI_INVALID_PARAMETER
;
1777 if (!Image
->Started
) {
1779 // The image has not been started so just free its resources
1781 CoreUnloadAndCloseImage (Image
, TRUE
);
1782 Status
= EFI_SUCCESS
;
1787 // Image has been started, verify this image can exit
1789 if (Image
!= mCurrentImage
) {
1790 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1791 Status
= EFI_INVALID_PARAMETER
;
1798 Image
->Status
= Status
;
1801 // If there's ExitData info, move it
1803 if (ExitData
!= NULL
) {
1804 Image
->ExitDataSize
= ExitDataSize
;
1805 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1806 if (Image
->ExitData
== NULL
) {
1807 Status
= EFI_OUT_OF_RESOURCES
;
1810 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1813 CoreRestoreTpl (OldTpl
);
1815 // return to StartImage
1817 LongJump (Image
->JumpContext
, (UINTN
)-1);
1820 // If we return from LongJump, then it is an error
1823 Status
= EFI_ACCESS_DENIED
;
1825 CoreRestoreTpl (OldTpl
);
1835 @param ImageHandle Handle that identifies the image to be
1838 @retval EFI_SUCCESS The image has been unloaded.
1839 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1841 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1847 IN EFI_HANDLE ImageHandle
1851 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1853 Image
= CoreLoadedImageInfo (ImageHandle
);
1854 if (Image
== NULL
) {
1856 // The image handle is not valid
1858 Status
= EFI_INVALID_PARAMETER
;
1862 if (Image
->Started
) {
1864 // The image has been started, request it to unload.
1866 Status
= EFI_UNSUPPORTED
;
1867 if (Image
->Info
.Unload
!= NULL
) {
1868 Status
= Image
->Info
.Unload (ImageHandle
);
1873 // This Image hasn't been started, thus it can be unloaded
1875 Status
= EFI_SUCCESS
;
1879 if (!EFI_ERROR (Status
)) {
1881 // if the Image was not started or Unloaded O.K. then clean up
1883 CoreUnloadAndCloseImage (Image
, TRUE
);
1893 Unload the specified image.
1895 @param This Indicates the calling context.
1896 @param ImageHandle The specified image handle.
1898 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1899 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1900 @retval EFI_SUCCESS Image successfully unloaded.
1906 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1907 IN EFI_HANDLE ImageHandle
1910 return CoreUnloadImage (ImageHandle
);