2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2012, 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
)) {
393 Status
= EFI_NOT_FOUND
;
395 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
397 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
398 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
399 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
400 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero
402 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
403 if (ValueInSectionHeader
!= 0) {
405 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
406 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
407 // relative to top address
409 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) < 0) {
410 ImageContext
->ImageAddress
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
+ (INT64
)(INTN
)ImageContext
->ImageAddress
;
413 // Check if the memory range is avaliable.
415 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext
->ImageAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
419 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
421 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
));
425 Loads, relocates, and invokes a PE/COFF image
427 @param BootPolicy If TRUE, indicates that the request originates
428 from the boot manager, and that the boot
429 manager is attempting to load FilePath as a
431 @param Pe32Handle The handle of PE32 image
432 @param Image PE image to be loaded
433 @param DstBuffer The buffer to store the image
434 @param EntryPoint A pointer to the entry point
435 @param Attribute The bit mask of attributes to set for the load
438 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
439 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
440 relocate the PE/COFF file
441 @retval EFI_INVALID_PARAMETER Invalid parameter
442 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
447 IN BOOLEAN BootPolicy
,
449 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
450 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
451 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
456 BOOLEAN DstBufAlocated
;
459 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
461 Image
->ImageContext
.Handle
= Pe32Handle
;
462 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
465 // Get information about the image being loaded
467 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
468 if (EFI_ERROR (Status
)) {
472 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
473 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
475 // The PE/COFF loader can support loading image types that can be executed.
476 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
478 DEBUG ((EFI_D_ERROR
, "Image type %s can't be loaded ", GetMachineTypeName(Image
->ImageContext
.Machine
)));
479 DEBUG ((EFI_D_ERROR
, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType
)));
480 return EFI_UNSUPPORTED
;
485 // Set EFI memory type based on ImageType
487 switch (Image
->ImageContext
.ImageType
) {
488 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
489 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
490 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
492 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
493 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
494 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
496 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
497 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
498 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
499 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
502 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
503 return EFI_UNSUPPORTED
;
507 // Allocate memory of the correct memory type aligned on the required image boundry
509 DstBufAlocated
= FALSE
;
510 if (DstBuffer
== 0) {
512 // Allocate Destination Buffer as caller did not pass it in
515 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
516 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
518 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
521 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
524 // If the image relocations have not been stripped, then load at any address.
525 // Otherwise load at the address at which it was linked.
527 // Memory below 1MB should be treated reserved for CSM and there should be
528 // no modules whose preferred load addresses are below 1MB.
530 Status
= EFI_OUT_OF_RESOURCES
;
532 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
533 // a specified address.
535 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 ) {
536 Status
= GetPeCoffImageFixLoadingAssignedAddress (&(Image
->ImageContext
));
538 if (EFI_ERROR (Status
)) {
540 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
542 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
544 Status
= CoreAllocatePages (
546 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
547 Image
->NumberOfPages
,
548 &Image
->ImageContext
.ImageAddress
552 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
553 Status
= CoreAllocatePages (
555 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
556 Image
->NumberOfPages
,
557 &Image
->ImageContext
.ImageAddress
560 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
561 Status
= CoreAllocatePages (
563 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
564 Image
->NumberOfPages
,
565 &Image
->ImageContext
.ImageAddress
569 if (EFI_ERROR (Status
)) {
572 DstBufAlocated
= TRUE
;
575 // Caller provided the destination buffer
578 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
580 // If the image relocations were stripped, and the caller provided a
581 // destination buffer address that does not match the address that the
582 // image is linked at, then the image cannot be loaded.
584 return EFI_INVALID_PARAMETER
;
587 if (Image
->NumberOfPages
!= 0 &&
588 Image
->NumberOfPages
<
589 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
590 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
591 return EFI_BUFFER_TOO_SMALL
;
594 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
595 Image
->ImageContext
.ImageAddress
= DstBuffer
;
598 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
599 if (!Image
->ImageContext
.IsTeImage
) {
600 Image
->ImageContext
.ImageAddress
=
601 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
602 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
606 // Load the image from the file into the allocated memory
608 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
609 if (EFI_ERROR (Status
)) {
614 // If this is a Runtime Driver, then allocate memory for the FixupData that
615 // is used to relocate the image when SetVirtualAddressMap() is called. The
616 // relocation is done by the Runtime AP.
618 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
619 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
620 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
621 if (Image
->ImageContext
.FixupData
== NULL
) {
622 Status
= EFI_OUT_OF_RESOURCES
;
629 // Relocate the image in memory
631 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
632 if (EFI_ERROR (Status
)) {
637 // Flush the Instruction Cache
639 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
642 // Copy the machine type from the context to the image private data. This
643 // is needed during image unload to know if we should call an EBC protocol
644 // to unload the image.
646 Image
->Machine
= Image
->ImageContext
.Machine
;
649 // Get the image entry point. If it's an EBC image, then call into the
650 // interpreter to create a thunk for the entry point and use the returned
651 // value for the entry point.
653 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
654 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
656 // Locate the EBC interpreter protocol
658 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
659 if (EFI_ERROR(Status
) || Image
->Ebc
== NULL
) {
660 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
665 // Register a callback for flushing the instruction cache so that created
666 // thunks can be flushed.
668 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
669 if (EFI_ERROR(Status
)) {
674 // Create a thunk for the image's entry point. This will be the new
675 // entry point for the image.
677 Status
= Image
->Ebc
->CreateThunk (
680 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
681 (VOID
**) &Image
->EntryPoint
683 if (EFI_ERROR(Status
)) {
689 // Fill in the image information for the Loaded Image Protocol
691 Image
->Type
= Image
->ImageContext
.ImageType
;
692 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
693 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
694 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
695 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
696 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
697 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
699 // Make a list off all the RT images so we can let the RT AP know about them.
701 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
702 if (Image
->RuntimeData
== NULL
) {
705 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
706 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
707 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
708 Image
->RuntimeData
->Handle
= Image
->Handle
;
709 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
714 // Fill in the entry point of the image if it is available
716 if (EntryPoint
!= NULL
) {
717 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
721 // Print the load address and the PDB file name if it is available
728 CHAR8 EfiFileName
[256];
731 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
732 "Loading driver at 0x%11p EntryPoint=0x%11p ",
733 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
734 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
738 // Print Module Name by Pdb file path.
739 // Windows and Unix style file path are all trimmed correctly.
741 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
743 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
744 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
745 StartIndex
= Index
+ 1;
749 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
750 // The PDB file name is limited in the range of 0~255.
751 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
753 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
754 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
755 if (EfiFileName
[Index
] == 0) {
756 EfiFileName
[Index
] = '.';
758 if (EfiFileName
[Index
] == '.') {
759 EfiFileName
[Index
+ 1] = 'e';
760 EfiFileName
[Index
+ 2] = 'f';
761 EfiFileName
[Index
+ 3] = 'i';
762 EfiFileName
[Index
+ 4] = 0;
767 if (Index
== sizeof (EfiFileName
) - 4) {
768 EfiFileName
[Index
] = 0;
770 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
772 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
784 if (DstBufAlocated
) {
785 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
788 if (Image
->ImageContext
.FixupData
!= NULL
) {
789 CoreFreePool (Image
->ImageContext
.FixupData
);
798 Get the image's private data from its handle.
800 @param ImageHandle The image handle
802 @return Return the image private data associated with ImageHandle.
805 LOADED_IMAGE_PRIVATE_DATA
*
806 CoreLoadedImageInfo (
807 IN EFI_HANDLE ImageHandle
811 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
812 LOADED_IMAGE_PRIVATE_DATA
*Image
;
814 Status
= CoreHandleProtocol (
816 &gEfiLoadedImageProtocolGuid
,
817 (VOID
**)&LoadedImage
819 if (!EFI_ERROR (Status
)) {
820 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
822 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
831 Unloads EFI image from memory.
833 @param Image EFI image
834 @param FreePage Free allocated pages
838 CoreUnloadAndCloseImage (
839 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
845 EFI_HANDLE
*HandleBuffer
;
847 EFI_GUID
**ProtocolGuidArray
;
850 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
854 if (Image
->Ebc
!= NULL
) {
856 // If EBC protocol exists we must perform cleanups for this image.
858 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
862 // Unload image, free Image->ImageContext->ModHandle
864 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
867 // Free our references to the image handle
869 if (Image
->Handle
!= NULL
) {
871 Status
= CoreLocateHandleBuffer (
878 if (!EFI_ERROR (Status
)) {
879 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
880 Status
= CoreProtocolsPerHandle (
881 HandleBuffer
[HandleIndex
],
885 if (!EFI_ERROR (Status
)) {
886 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
887 Status
= CoreOpenProtocolInformation (
888 HandleBuffer
[HandleIndex
],
889 ProtocolGuidArray
[ProtocolIndex
],
893 if (!EFI_ERROR (Status
)) {
894 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
895 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
896 Status
= CoreCloseProtocol (
897 HandleBuffer
[HandleIndex
],
898 ProtocolGuidArray
[ProtocolIndex
],
900 OpenInfo
[OpenInfoIndex
].ControllerHandle
904 if (OpenInfo
!= NULL
) {
905 CoreFreePool(OpenInfo
);
909 if (ProtocolGuidArray
!= NULL
) {
910 CoreFreePool(ProtocolGuidArray
);
914 if (HandleBuffer
!= NULL
) {
915 CoreFreePool (HandleBuffer
);
919 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
921 Status
= CoreUninstallProtocolInterface (
923 &gEfiLoadedImageDevicePathProtocolGuid
,
924 Image
->LoadedImageDevicePath
927 Status
= CoreUninstallProtocolInterface (
929 &gEfiLoadedImageProtocolGuid
,
933 if (Image
->ImageContext
.HiiResourceData
!= 0) {
934 Status
= CoreUninstallProtocolInterface (
936 &gEfiHiiPackageListProtocolGuid
,
937 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
943 if (Image
->RuntimeData
!= NULL
) {
944 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
946 // Remove the Image from the Runtime Image list as we are about to Free it!
948 RemoveEntryList (&Image
->RuntimeData
->Link
);
950 CoreFreePool (Image
->RuntimeData
);
954 // Free the Image from memory
956 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
957 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
961 // Done with the Image structure
963 if (Image
->Info
.FilePath
!= NULL
) {
964 CoreFreePool (Image
->Info
.FilePath
);
967 if (Image
->LoadedImageDevicePath
!= NULL
) {
968 CoreFreePool (Image
->LoadedImageDevicePath
);
971 if (Image
->FixupData
!= NULL
) {
972 CoreFreePool (Image
->FixupData
);
975 CoreFreePool (Image
);
980 Loads an EFI image into memory and returns a handle to the image.
982 @param BootPolicy If TRUE, indicates that the request originates
983 from the boot manager, and that the boot
984 manager is attempting to load FilePath as a
986 @param ParentImageHandle The caller's image handle.
987 @param FilePath The specific file path from which the image is
989 @param SourceBuffer If not NULL, a pointer to the memory location
990 containing a copy of the image to be loaded.
991 @param SourceSize The size in bytes of SourceBuffer.
992 @param DstBuffer The buffer to store the image
993 @param NumberOfPages If not NULL, it inputs a pointer to the page
994 number of DstBuffer and outputs a pointer to
995 the page number of the image. If this number is
996 not enough, return EFI_BUFFER_TOO_SMALL and
997 this parameter contains the required number.
998 @param ImageHandle Pointer to the returned image handle that is
999 created when the image is successfully loaded.
1000 @param EntryPoint A pointer to the entry point
1001 @param Attribute The bit mask of attributes to set for the load
1004 @retval EFI_SUCCESS The image was loaded into memory.
1005 @retval EFI_NOT_FOUND The FilePath was not found.
1006 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1007 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
1008 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1009 path cannot be parsed to locate the proper
1010 protocol for loading the file.
1011 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1013 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1015 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1016 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1017 image from being loaded. NULL is returned in *ImageHandle.
1018 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1019 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1020 platform policy specifies that the image should not be started.
1024 CoreLoadImageCommon (
1025 IN BOOLEAN BootPolicy
,
1026 IN EFI_HANDLE ParentImageHandle
,
1027 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1028 IN VOID
*SourceBuffer OPTIONAL
,
1029 IN UINTN SourceSize
,
1030 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1031 IN OUT UINTN
*NumberOfPages OPTIONAL
,
1032 OUT EFI_HANDLE
*ImageHandle
,
1033 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1037 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1038 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
1039 IMAGE_FILE_HANDLE FHand
;
1041 EFI_STATUS SecurityStatus
;
1042 EFI_HANDLE DeviceHandle
;
1043 UINT32 AuthenticationStatus
;
1044 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
1045 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
1048 SecurityStatus
= EFI_SUCCESS
;
1050 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
1054 // The caller must pass in a valid ParentImageHandle
1056 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
1057 return EFI_INVALID_PARAMETER
;
1060 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
1061 if (ParentImage
== NULL
) {
1062 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
1063 return EFI_INVALID_PARAMETER
;
1066 ZeroMem (&FHand
, sizeof (IMAGE_FILE_HANDLE
));
1067 FHand
.Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
1068 OriginalFilePath
= FilePath
;
1069 HandleFilePath
= FilePath
;
1070 DeviceHandle
= NULL
;
1071 Status
= EFI_SUCCESS
;
1072 AuthenticationStatus
= 0;
1074 // If the caller passed a copy of the file, then just use it
1076 if (SourceBuffer
!= NULL
) {
1077 FHand
.Source
= SourceBuffer
;
1078 FHand
.SourceSize
= SourceSize
;
1079 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1080 if (SourceSize
> 0) {
1081 Status
= EFI_SUCCESS
;
1083 Status
= EFI_LOAD_ERROR
;
1086 if (FilePath
== NULL
) {
1087 return EFI_INVALID_PARAMETER
;
1090 // Get the source file buffer by its device path.
1092 FHand
.Source
= GetFileBufferByFilePath (
1096 &AuthenticationStatus
1098 if (FHand
.Source
== NULL
) {
1099 Status
= EFI_NOT_FOUND
;
1102 // Try to get the image device handle by checking the match protocol.
1104 FHand
.FreeBuffer
= TRUE
;
1105 Status
= CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1106 if (EFI_ERROR (Status
)) {
1107 HandleFilePath
= FilePath
;
1108 Status
= CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1109 if (EFI_ERROR (Status
)) {
1111 HandleFilePath
= FilePath
;
1112 Status
= CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1114 if (EFI_ERROR (Status
)) {
1115 HandleFilePath
= FilePath
;
1116 Status
= CoreLocateDevicePath (&gEfiLoadFileProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1123 if (Status
== EFI_ALREADY_STARTED
) {
1126 } else if (EFI_ERROR (Status
)) {
1131 // Verify the Authentication Status through the Security Architectural Protocol
1133 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
1134 SecurityStatus
= gSecurity
->FileAuthenticationState (
1136 AuthenticationStatus
,
1139 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
1140 if (SecurityStatus
== EFI_ACCESS_DENIED
) {
1142 // Image was not loaded because the platform policy prohibits the image from being loaded.
1143 // It's the only place we could meet EFI_ACCESS_DENIED.
1145 *ImageHandle
= NULL
;
1147 Status
= SecurityStatus
;
1155 // Allocate a new image structure
1157 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
1158 if (Image
== NULL
) {
1159 return EFI_OUT_OF_RESOURCES
;
1163 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1165 FilePath
= OriginalFilePath
;
1166 if (DeviceHandle
!= NULL
) {
1167 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
1168 if (!EFI_ERROR (Status
)) {
1169 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1170 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
1174 // Initialize the fields for an internal driver
1176 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
1177 Image
->Info
.SystemTable
= gDxeCoreST
;
1178 Image
->Info
.DeviceHandle
= DeviceHandle
;
1179 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1180 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
1181 Image
->Info
.ParentHandle
= ParentImageHandle
;
1184 if (NumberOfPages
!= NULL
) {
1185 Image
->NumberOfPages
= *NumberOfPages
;
1187 Image
->NumberOfPages
= 0 ;
1191 // Install the protocol interfaces for this image
1192 // don't fire notifications yet
1194 Status
= CoreInstallProtocolInterfaceNotify (
1196 &gEfiLoadedImageProtocolGuid
,
1197 EFI_NATIVE_INTERFACE
,
1201 if (EFI_ERROR (Status
)) {
1206 // Load the image. If EntryPoint is Null, it will not be set.
1208 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
1209 if (EFI_ERROR (Status
)) {
1210 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
1211 if (NumberOfPages
!= NULL
) {
1212 *NumberOfPages
= Image
->NumberOfPages
;
1218 if (NumberOfPages
!= NULL
) {
1219 *NumberOfPages
= Image
->NumberOfPages
;
1223 // Register the image in the Debug Image Info Table if the attribute is set
1225 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
1226 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
1230 //Reinstall loaded image protocol to fire any notifications
1232 Status
= CoreReinstallProtocolInterface (
1234 &gEfiLoadedImageProtocolGuid
,
1238 if (EFI_ERROR (Status
)) {
1243 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1244 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1246 if (OriginalFilePath
!= NULL
) {
1247 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
1251 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1253 Status
= CoreInstallProtocolInterface (
1255 &gEfiLoadedImageDevicePathProtocolGuid
,
1256 EFI_NATIVE_INTERFACE
,
1257 Image
->LoadedImageDevicePath
1259 if (EFI_ERROR (Status
)) {
1264 // Install HII Package List Protocol onto the image handle
1266 if (Image
->ImageContext
.HiiResourceData
!= 0) {
1267 Status
= CoreInstallProtocolInterface (
1269 &gEfiHiiPackageListProtocolGuid
,
1270 EFI_NATIVE_INTERFACE
,
1271 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
1273 if (EFI_ERROR (Status
)) {
1279 // Success. Return the image handle
1281 *ImageHandle
= Image
->Handle
;
1285 // All done accessing the source file
1286 // If we allocated the Source buffer, free it
1288 if (FHand
.FreeBuffer
) {
1289 CoreFreePool (FHand
.Source
);
1293 // There was an error. If there's an Image structure, free it
1295 if (EFI_ERROR (Status
)) {
1296 if (Image
!= NULL
) {
1297 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
1299 } else if (EFI_ERROR (SecurityStatus
)) {
1300 Status
= SecurityStatus
;
1310 Loads an EFI image into memory and returns a handle to the image.
1312 @param BootPolicy If TRUE, indicates that the request originates
1313 from the boot manager, and that the boot
1314 manager is attempting to load FilePath as a
1316 @param ParentImageHandle The caller's image handle.
1317 @param FilePath The specific file path from which the image is
1319 @param SourceBuffer If not NULL, a pointer to the memory location
1320 containing a copy of the image to be loaded.
1321 @param SourceSize The size in bytes of SourceBuffer.
1322 @param ImageHandle Pointer to the returned image handle that is
1323 created when the image is successfully loaded.
1325 @retval EFI_SUCCESS The image was loaded into memory.
1326 @retval EFI_NOT_FOUND The FilePath was not found.
1327 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1328 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1329 path cannot be parsed to locate the proper
1330 protocol for loading the file.
1331 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1333 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1335 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1336 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1337 image from being loaded. NULL is returned in *ImageHandle.
1338 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1339 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1340 platform policy specifies that the image should not be started.
1346 IN BOOLEAN BootPolicy
,
1347 IN EFI_HANDLE ParentImageHandle
,
1348 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1349 IN VOID
*SourceBuffer OPTIONAL
,
1350 IN UINTN SourceSize
,
1351 OUT EFI_HANDLE
*ImageHandle
1360 Tick
= GetPerformanceCounter ();
1363 Status
= CoreLoadImageCommon (
1369 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1373 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1377 if (!EFI_ERROR (Status
)) {
1379 // ImageHandle will be valid only Status is success.
1381 Handle
= *ImageHandle
;
1384 PERF_START (Handle
, "LoadImage:", NULL
, Tick
);
1385 PERF_END (Handle
, "LoadImage:", NULL
, 0);
1393 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1395 @param This Calling context
1396 @param ParentImageHandle The caller's image handle.
1397 @param FilePath The specific file path from which the image is
1399 @param SourceBuffer If not NULL, a pointer to the memory location
1400 containing a copy of the image to be loaded.
1401 @param SourceSize The size in bytes of SourceBuffer.
1402 @param DstBuffer The buffer to store the image.
1403 @param NumberOfPages For input, specifies the space size of the
1404 image by caller if not NULL. For output,
1405 specifies the actual space size needed.
1406 @param ImageHandle Image handle for output.
1407 @param EntryPoint Image entry point for output.
1408 @param Attribute The bit mask of attributes to set for the load
1411 @retval EFI_SUCCESS The image was loaded into memory.
1412 @retval EFI_NOT_FOUND The FilePath was not found.
1413 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1414 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1415 path cannot be parsed to locate the proper
1416 protocol for loading the file.
1417 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1419 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1421 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1422 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1423 image from being loaded. NULL is returned in *ImageHandle.
1424 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1425 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1426 platform policy specifies that the image should not be started.
1432 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1433 IN EFI_HANDLE ParentImageHandle
,
1434 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1435 IN VOID
*SourceBuffer OPTIONAL
,
1436 IN UINTN SourceSize
,
1437 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1438 OUT UINTN
*NumberOfPages OPTIONAL
,
1439 OUT EFI_HANDLE
*ImageHandle
,
1440 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1450 Tick
= GetPerformanceCounter ();
1453 Status
= CoreLoadImageCommon (
1467 if (!EFI_ERROR (Status
)) {
1469 // ImageHandle will be valid only Status is success.
1471 Handle
= *ImageHandle
;
1474 PERF_START (Handle
, "LoadImage:", NULL
, Tick
);
1475 PERF_END (Handle
, "LoadImage:", NULL
, 0);
1482 Transfer control to a loaded image's entry point.
1484 @param ImageHandle Handle of image to be started.
1485 @param ExitDataSize Pointer of the size to ExitData
1486 @param ExitData Pointer to a pointer to a data buffer that
1487 includes a Null-terminated string,
1488 optionally followed by additional binary data.
1489 The string is a description that the caller may
1490 use to further indicate the reason for the
1493 @retval EFI_INVALID_PARAMETER Invalid parameter
1494 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1495 @retval EFI_SUCCESS Successfully transfer control to the image's
1502 IN EFI_HANDLE ImageHandle
,
1503 OUT UINTN
*ExitDataSize
,
1504 OUT CHAR16
**ExitData OPTIONAL
1508 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1509 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1510 UINT64 HandleDatabaseKey
;
1516 Handle
= ImageHandle
;
1518 Image
= CoreLoadedImageInfo (ImageHandle
);
1519 if (Image
== NULL
|| Image
->Started
) {
1520 return EFI_INVALID_PARAMETER
;
1524 // The image to be started must have the machine type supported by DxeCore.
1526 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1528 // Do not ASSERT here, because image might be loaded via EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED
1529 // But it can not be started.
1531 DEBUG ((EFI_D_ERROR
, "Image type %s can't be started ", GetMachineTypeName(Image
->Machine
)));
1532 DEBUG ((EFI_D_ERROR
, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType
)));
1533 return EFI_UNSUPPORTED
;
1537 Tick
= GetPerformanceCounter ();
1542 // Push the current start image context, and
1543 // link the current image to the head. This is the
1544 // only image that can call Exit()
1546 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1547 LastImage
= mCurrentImage
;
1548 mCurrentImage
= Image
;
1549 Image
->Tpl
= gEfiCurrentTpl
;
1552 // Set long jump for Exit() support
1553 // JumpContext must be aligned on a CPU specific boundary.
1554 // Overallocate the buffer and force the required alignment
1556 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1557 if (Image
->JumpBuffer
== NULL
) {
1559 // Image may be unloaded after return with failure,
1560 // then ImageHandle may be invalid, so use NULL handle to record perf log.
1562 PERF_START (NULL
, "StartImage:", NULL
, Tick
);
1563 PERF_END (NULL
, "StartImage:", NULL
, 0);
1564 return EFI_OUT_OF_RESOURCES
;
1566 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1568 SetJumpFlag
= SetJump (Image
->JumpContext
);
1570 // The initial call to SetJump() must always return 0.
1571 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1573 if (SetJumpFlag
== 0) {
1575 // Call the image's entry point
1577 Image
->Started
= TRUE
;
1578 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1581 // Add some debug information if the image returned with error.
1582 // This make the user aware and check if the driver image have already released
1583 // all the resource in this situation.
1585 DEBUG_CODE_BEGIN ();
1586 if (EFI_ERROR (Image
->Status
)) {
1587 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1592 // If the image returns, exit it through Exit()
1594 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1598 // Image has completed. Verify the tpl is the same
1600 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1601 CoreRestoreTpl (Image
->Tpl
);
1603 CoreFreePool (Image
->JumpBuffer
);
1606 // Pop the current start image context
1608 mCurrentImage
= LastImage
;
1611 // Go connect any handles that were created or modified while the image executed.
1613 CoreConnectHandlesByKey (HandleDatabaseKey
);
1616 // Handle the image's returned ExitData
1618 DEBUG_CODE_BEGIN ();
1619 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1621 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1622 if (Image
->ExitData
!= NULL
) {
1623 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1625 DEBUG ((DEBUG_LOAD
, "\n"));
1630 // Return the exit data to the caller
1632 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1633 *ExitDataSize
= Image
->ExitDataSize
;
1634 *ExitData
= Image
->ExitData
;
1637 // Caller doesn't want the exit data, free it
1639 CoreFreePool (Image
->ExitData
);
1640 Image
->ExitData
= NULL
;
1644 // Save the Status because Image will get destroyed if it is unloaded.
1646 Status
= Image
->Status
;
1649 // If the image returned an error, or if the image is an application
1652 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1653 CoreUnloadAndCloseImage (Image
, TRUE
);
1655 // ImageHandle may be invalid after the image is unloaded, so use NULL handle to record perf log.
1663 PERF_START (Handle
, "StartImage:", NULL
, Tick
);
1664 PERF_END (Handle
, "StartImage:", NULL
, 0);
1669 Terminates the currently loaded EFI image and returns control to boot services.
1671 @param ImageHandle Handle that identifies the image. This
1672 parameter is passed to the image on entry.
1673 @param Status The image's exit code.
1674 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1675 ExitStatus is EFI_SUCCESS.
1676 @param ExitData Pointer to a data buffer that includes a
1677 Null-terminated Unicode string, optionally
1678 followed by additional binary data. The string
1679 is a description that the caller may use to
1680 further indicate the reason for the image's
1683 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1685 @retval EFI_SUCCESS Successfully terminates the currently loaded
1687 @retval EFI_ACCESS_DENIED Should never reach there.
1688 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1694 IN EFI_HANDLE ImageHandle
,
1695 IN EFI_STATUS Status
,
1696 IN UINTN ExitDataSize
,
1697 IN CHAR16
*ExitData OPTIONAL
1700 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1704 // Prevent possible reentrance to this function
1705 // for the same ImageHandle
1707 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1709 Image
= CoreLoadedImageInfo (ImageHandle
);
1710 if (Image
== NULL
) {
1711 Status
= EFI_INVALID_PARAMETER
;
1715 if (!Image
->Started
) {
1717 // The image has not been started so just free its resources
1719 CoreUnloadAndCloseImage (Image
, TRUE
);
1720 Status
= EFI_SUCCESS
;
1725 // Image has been started, verify this image can exit
1727 if (Image
!= mCurrentImage
) {
1728 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1729 Status
= EFI_INVALID_PARAMETER
;
1736 Image
->Status
= Status
;
1739 // If there's ExitData info, move it
1741 if (ExitData
!= NULL
) {
1742 Image
->ExitDataSize
= ExitDataSize
;
1743 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1744 if (Image
->ExitData
== NULL
) {
1745 Status
= EFI_OUT_OF_RESOURCES
;
1748 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1751 CoreRestoreTpl (OldTpl
);
1753 // return to StartImage
1755 LongJump (Image
->JumpContext
, (UINTN
)-1);
1758 // If we return from LongJump, then it is an error
1761 Status
= EFI_ACCESS_DENIED
;
1763 CoreRestoreTpl (OldTpl
);
1773 @param ImageHandle Handle that identifies the image to be
1776 @retval EFI_SUCCESS The image has been unloaded.
1777 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1779 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1785 IN EFI_HANDLE ImageHandle
1789 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1791 Image
= CoreLoadedImageInfo (ImageHandle
);
1792 if (Image
== NULL
) {
1794 // The image handle is not valid
1796 Status
= EFI_INVALID_PARAMETER
;
1800 if (Image
->Started
) {
1802 // The image has been started, request it to unload.
1804 Status
= EFI_UNSUPPORTED
;
1805 if (Image
->Info
.Unload
!= NULL
) {
1806 Status
= Image
->Info
.Unload (ImageHandle
);
1811 // This Image hasn't been started, thus it can be unloaded
1813 Status
= EFI_SUCCESS
;
1817 if (!EFI_ERROR (Status
)) {
1819 // if the Image was not started or Unloaded O.K. then clean up
1821 CoreUnloadAndCloseImage (Image
, TRUE
);
1831 Unload the specified image.
1833 @param This Indicates the calling context.
1834 @param ImageHandle The specified image handle.
1836 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1837 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1838 @retval EFI_SUCCESS Image successfully unloaded.
1844 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1845 IN EFI_HANDLE ImageHandle
1848 return CoreUnloadImage (ImageHandle
);