2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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
77 Add the Image Services to EFI Boot Services Table and install the protocol
78 interfaces for this image.
80 @param HobStart The HOB to initialize
86 CoreInitializeImageServices (
91 LOADED_IMAGE_PRIVATE_DATA
*Image
;
92 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
93 UINT64 DxeCoreImageLength
;
94 VOID
*DxeCoreEntryPoint
;
95 EFI_PEI_HOB_POINTERS DxeCoreHob
;
97 // Searching for image hob
99 DxeCoreHob
.Raw
= HobStart
;
100 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
101 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
107 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
109 ASSERT (DxeCoreHob
.Raw
!= NULL
);
111 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
112 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
113 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
114 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
116 // Initialize the fields for an internal driver
118 Image
= &mCorePrivateImage
;
120 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
121 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
122 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
123 Image
->Tpl
= gEfiCurrentTpl
;
124 Image
->Info
.SystemTable
= gDxeCoreST
;
125 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
126 Image
->Info
.ImageSize
= DxeCoreImageLength
;
129 // Install the protocol interfaces for this image
131 Status
= CoreInstallProtocolInterface (
133 &gEfiLoadedImageProtocolGuid
,
134 EFI_NATIVE_INTERFACE
,
137 ASSERT_EFI_ERROR (Status
);
139 mCurrentImage
= Image
;
142 // Fill in DXE globals
144 gDxeCoreImageHandle
= Image
->Handle
;
145 gDxeCoreLoadedImage
= &Image
->Info
;
148 // Export DXE Core PE Loader functionality
150 return CoreInstallProtocolInterface (
151 &mLoadPe32PrivateData
.Handle
,
152 &gEfiLoadPeImageProtocolGuid
,
153 EFI_NATIVE_INTERFACE
,
154 &mLoadPe32PrivateData
.Pe32Image
160 Loads, relocates, and invokes a PE/COFF image
162 @param BootPolicy If TRUE, indicates that the request originates
163 from the boot manager, and that the boot
164 manager is attempting to load FilePath as a
166 @param Pe32Handle The handle of PE32 image
167 @param Image PE image to be loaded
168 @param DstBuffer The buffer to store the image
169 @param EntryPoint A pointer to the entry point
170 @param Attribute The bit mask of attributes to set for the load
173 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
174 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
175 relocate the PE/COFF file
176 @retval EFI_INVALID_PARAMETER Invalid parameter
177 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
182 IN BOOLEAN BootPolicy
,
184 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
185 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
186 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
191 BOOLEAN DstBufAlocated
;
195 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
197 Image
->ImageContext
.Handle
= Pe32Handle
;
198 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
201 // Get information about the image being loaded
203 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
204 if (EFI_ERROR (Status
)) {
208 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
209 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
211 // The PE/COFF loader can support loading image types that can be executed.
212 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
214 return EFI_UNSUPPORTED
;
219 // Set EFI memory type based on ImageType
221 switch (Image
->ImageContext
.ImageType
) {
222 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
223 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
224 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
226 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
227 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
228 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
230 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
231 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
232 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
233 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
236 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
237 return EFI_UNSUPPORTED
;
241 // Allocate memory of the correct memory type aligned on the required image boundry
243 DstBufAlocated
= FALSE
;
244 if (DstBuffer
== 0) {
246 // Allocate Destination Buffer as caller did not pass it in
249 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
250 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
252 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
255 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
258 // If the image relocations have not been stripped, then load at any address.
259 // Otherwise load at the address at which it was linked.
261 // Memory below 1MB should be treated reserved for CSM and there should be
262 // no modules whose preferred load addresses are below 1MB.
264 Status
= EFI_OUT_OF_RESOURCES
;
265 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
266 Status
= CoreAllocatePages (
268 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
269 Image
->NumberOfPages
,
270 &Image
->ImageContext
.ImageAddress
273 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
274 Status
= CoreAllocatePages (
276 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
277 Image
->NumberOfPages
,
278 &Image
->ImageContext
.ImageAddress
281 if (EFI_ERROR (Status
)) {
284 DstBufAlocated
= TRUE
;
287 // Caller provided the destination buffer
290 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
292 // If the image relocations were stripped, and the caller provided a
293 // destination buffer address that does not match the address that the
294 // image is linked at, then the image cannot be loaded.
296 return EFI_INVALID_PARAMETER
;
299 if (Image
->NumberOfPages
!= 0 &&
300 Image
->NumberOfPages
<
301 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
302 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
303 return EFI_BUFFER_TOO_SMALL
;
306 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
307 Image
->ImageContext
.ImageAddress
= DstBuffer
;
310 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
311 if (!Image
->ImageContext
.IsTeImage
) {
312 Image
->ImageContext
.ImageAddress
=
313 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
314 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
318 // Load the image from the file into the allocated memory
320 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
321 if (EFI_ERROR (Status
)) {
326 // If this is a Runtime Driver, then allocate memory for the FixupData that
327 // is used to relocate the image when SetVirtualAddressMap() is called. The
328 // relocation is done by the Runtime AP.
330 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
331 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
332 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
333 if (Image
->ImageContext
.FixupData
== NULL
) {
334 Status
= EFI_OUT_OF_RESOURCES
;
341 // Relocate the image in memory
343 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
344 if (EFI_ERROR (Status
)) {
349 // Flush the Instruction Cache
351 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
354 // Copy the machine type from the context to the image private data. This
355 // is needed during image unload to know if we should call an EBC protocol
356 // to unload the image.
358 Image
->Machine
= Image
->ImageContext
.Machine
;
361 // Get the image entry point. If it's an EBC image, then call into the
362 // interpreter to create a thunk for the entry point and use the returned
363 // value for the entry point.
365 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
366 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
368 // Locate the EBC interpreter protocol
370 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
371 if (EFI_ERROR(Status
)) {
372 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
377 // Register a callback for flushing the instruction cache so that created
378 // thunks can be flushed.
380 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
381 if (EFI_ERROR(Status
)) {
386 // Create a thunk for the image's entry point. This will be the new
387 // entry point for the image.
389 Status
= Image
->Ebc
->CreateThunk (
392 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
393 (VOID
**) &Image
->EntryPoint
395 if (EFI_ERROR(Status
)) {
401 // Fill in the image information for the Loaded Image Protocol
403 Image
->Type
= Image
->ImageContext
.ImageType
;
404 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
405 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
406 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
407 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
408 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
409 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
411 // Make a list off all the RT images so we can let the RT AP know about them.
413 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
414 if (Image
->RuntimeData
== NULL
) {
417 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
418 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
419 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
420 Image
->RuntimeData
->Handle
= Image
->Handle
;
421 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
426 // Fill in the entry point of the image if it is available
428 if (EntryPoint
!= NULL
) {
429 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
433 // Print the load address and the PDB file name if it is available
440 CHAR8 EfiFileName
[256];
443 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
444 "Loading driver at 0x%11p EntryPoint=0x%11p ",
445 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
446 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
450 // Print Module Name by Pdb file path.
451 // Windows and Unix style file path are all trimmed correctly.
453 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
455 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
456 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
457 StartIndex
= Index
+ 1;
461 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
462 // The PDB file name is limited in the range of 0~255.
463 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
465 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
466 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
467 if (EfiFileName
[Index
] == 0) {
468 EfiFileName
[Index
] = '.';
470 if (EfiFileName
[Index
] == '.') {
471 EfiFileName
[Index
+ 1] = 'e';
472 EfiFileName
[Index
+ 2] = 'f';
473 EfiFileName
[Index
+ 3] = 'i';
474 EfiFileName
[Index
+ 4] = 0;
479 if (Index
== sizeof (EfiFileName
) - 4) {
480 EfiFileName
[Index
] = 0;
482 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
484 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
496 if (DstBufAlocated
) {
497 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
500 if (Image
->ImageContext
.FixupData
!= NULL
) {
501 CoreFreePool (Image
->ImageContext
.FixupData
);
510 Get the image's private data from its handle.
512 @param ImageHandle The image handle
514 @return Return the image private data associated with ImageHandle.
517 LOADED_IMAGE_PRIVATE_DATA
*
518 CoreLoadedImageInfo (
519 IN EFI_HANDLE ImageHandle
523 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
524 LOADED_IMAGE_PRIVATE_DATA
*Image
;
526 Status
= CoreHandleProtocol (
528 &gEfiLoadedImageProtocolGuid
,
529 (VOID
**)&LoadedImage
531 if (!EFI_ERROR (Status
)) {
532 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
534 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
543 Unloads EFI image from memory.
545 @param Image EFI image
546 @param FreePage Free allocated pages
550 CoreUnloadAndCloseImage (
551 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
557 EFI_HANDLE
*HandleBuffer
;
559 EFI_GUID
**ProtocolGuidArray
;
562 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
566 if (Image
->Ebc
!= NULL
) {
568 // If EBC protocol exists we must perform cleanups for this image.
570 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
574 // Unload image, free Image->ImageContext->ModHandle
576 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
579 // Free our references to the image handle
581 if (Image
->Handle
!= NULL
) {
583 Status
= CoreLocateHandleBuffer (
590 if (!EFI_ERROR (Status
)) {
591 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
592 Status
= CoreProtocolsPerHandle (
593 HandleBuffer
[HandleIndex
],
597 if (!EFI_ERROR (Status
)) {
598 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
599 Status
= CoreOpenProtocolInformation (
600 HandleBuffer
[HandleIndex
],
601 ProtocolGuidArray
[ProtocolIndex
],
605 if (!EFI_ERROR (Status
)) {
606 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
607 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
608 Status
= CoreCloseProtocol (
609 HandleBuffer
[HandleIndex
],
610 ProtocolGuidArray
[ProtocolIndex
],
612 OpenInfo
[OpenInfoIndex
].ControllerHandle
616 if (OpenInfo
!= NULL
) {
617 CoreFreePool(OpenInfo
);
621 if (ProtocolGuidArray
!= NULL
) {
622 CoreFreePool(ProtocolGuidArray
);
626 if (HandleBuffer
!= NULL
) {
627 CoreFreePool (HandleBuffer
);
631 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
633 Status
= CoreUninstallProtocolInterface (
635 &gEfiLoadedImageDevicePathProtocolGuid
,
636 Image
->LoadedImageDevicePath
639 Status
= CoreUninstallProtocolInterface (
641 &gEfiLoadedImageProtocolGuid
,
647 if (Image
->RuntimeData
!= NULL
) {
648 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
650 // Remove the Image from the Runtime Image list as we are about to Free it!
652 RemoveEntryList (&Image
->RuntimeData
->Link
);
654 CoreFreePool (Image
->RuntimeData
);
658 // Free the Image from memory
660 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
661 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
665 // Done with the Image structure
667 if (Image
->Info
.FilePath
!= NULL
) {
668 CoreFreePool (Image
->Info
.FilePath
);
671 if (Image
->LoadedImageDevicePath
!= NULL
) {
672 CoreFreePool (Image
->LoadedImageDevicePath
);
675 if (Image
->FixupData
!= NULL
) {
676 CoreFreePool (Image
->FixupData
);
679 CoreFreePool (Image
);
684 Loads an EFI image into memory and returns a handle to the image.
686 @param BootPolicy If TRUE, indicates that the request originates
687 from the boot manager, and that the boot
688 manager is attempting to load FilePath as a
690 @param ParentImageHandle The caller's image handle.
691 @param FilePath The specific file path from which the image is
693 @param SourceBuffer If not NULL, a pointer to the memory location
694 containing a copy of the image to be loaded.
695 @param SourceSize The size in bytes of SourceBuffer.
696 @param DstBuffer The buffer to store the image
697 @param NumberOfPages If not NULL, it inputs a pointer to the page
698 number of DstBuffer and outputs a pointer to
699 the page number of the image. If this number is
700 not enough, return EFI_BUFFER_TOO_SMALL and
701 this parameter contains the required number.
702 @param ImageHandle Pointer to the returned image handle that is
703 created when the image is successfully loaded.
704 @param EntryPoint A pointer to the entry point
705 @param Attribute The bit mask of attributes to set for the load
708 @retval EFI_SUCCESS The image was loaded into memory.
709 @retval EFI_NOT_FOUND The FilePath was not found.
710 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
711 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
712 @retval EFI_UNSUPPORTED The image type is not supported, or the device
713 path cannot be parsed to locate the proper
714 protocol for loading the file.
715 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
720 CoreLoadImageCommon (
721 IN BOOLEAN BootPolicy
,
722 IN EFI_HANDLE ParentImageHandle
,
723 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
724 IN VOID
*SourceBuffer OPTIONAL
,
726 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
727 IN OUT UINTN
*NumberOfPages OPTIONAL
,
728 OUT EFI_HANDLE
*ImageHandle
,
729 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
733 LOADED_IMAGE_PRIVATE_DATA
*Image
;
734 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
735 IMAGE_FILE_HANDLE FHand
;
737 EFI_STATUS SecurityStatus
;
738 EFI_HANDLE DeviceHandle
;
739 UINT32 AuthenticationStatus
;
740 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
741 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
744 SecurityStatus
= EFI_SUCCESS
;
746 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
750 // The caller must pass in a valid ParentImageHandle
752 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
753 return EFI_INVALID_PARAMETER
;
756 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
757 if (ParentImage
== NULL
) {
758 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
759 return EFI_INVALID_PARAMETER
;
763 // Get simple read access to the source file
765 OriginalFilePath
= FilePath
;
766 Status
= CoreOpenImageFile (
773 &AuthenticationStatus
775 if (Status
== EFI_ALREADY_STARTED
) {
778 } else if (EFI_ERROR (Status
)) {
783 // Verify the Authentication Status through the Security Architectural Protocol
785 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
786 SecurityStatus
= gSecurity
->FileAuthenticationState (
788 AuthenticationStatus
,
791 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
792 Status
= SecurityStatus
;
800 // Allocate a new image structure
802 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
804 return EFI_OUT_OF_RESOURCES
;
808 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
810 FilePath
= OriginalFilePath
;
811 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
812 if (!EFI_ERROR (Status
)) {
813 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
814 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
818 // Initialize the fields for an internal driver
820 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
821 Image
->Info
.SystemTable
= gDxeCoreST
;
822 Image
->Info
.DeviceHandle
= DeviceHandle
;
823 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
824 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
825 Image
->Info
.ParentHandle
= ParentImageHandle
;
828 if (NumberOfPages
!= NULL
) {
829 Image
->NumberOfPages
= *NumberOfPages
;
831 Image
->NumberOfPages
= 0 ;
835 // Install the protocol interfaces for this image
836 // don't fire notifications yet
838 Status
= CoreInstallProtocolInterfaceNotify (
840 &gEfiLoadedImageProtocolGuid
,
841 EFI_NATIVE_INTERFACE
,
845 if (EFI_ERROR (Status
)) {
850 // Load the image. If EntryPoint is Null, it will not be set.
852 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
853 if (EFI_ERROR (Status
)) {
854 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
855 if (NumberOfPages
!= NULL
) {
856 *NumberOfPages
= Image
->NumberOfPages
;
862 if (NumberOfPages
!= NULL
) {
863 *NumberOfPages
= Image
->NumberOfPages
;
867 // Register the image in the Debug Image Info Table if the attribute is set
869 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
870 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
874 //Reinstall loaded image protocol to fire any notifications
876 Status
= CoreReinstallProtocolInterface (
878 &gEfiLoadedImageProtocolGuid
,
882 if (EFI_ERROR (Status
)) {
887 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
888 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
890 if (OriginalFilePath
!= NULL
) {
891 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
895 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
897 Status
= CoreInstallProtocolInterface (
899 &gEfiLoadedImageDevicePathProtocolGuid
,
900 EFI_NATIVE_INTERFACE
,
901 Image
->LoadedImageDevicePath
903 if (EFI_ERROR (Status
)) {
908 // Success. Return the image handle
910 *ImageHandle
= Image
->Handle
;
914 // All done accessing the source file
915 // If we allocated the Source buffer, free it
917 if (FHand
.FreeBuffer
) {
918 CoreFreePool (FHand
.Source
);
922 // There was an error. If there's an Image structure, free it
924 if (EFI_ERROR (Status
)) {
926 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
929 } else if (EFI_ERROR (SecurityStatus
)) {
930 Status
= SecurityStatus
;
940 Loads an EFI image into memory and returns a handle to the image.
942 @param BootPolicy If TRUE, indicates that the request originates
943 from the boot manager, and that the boot
944 manager is attempting to load FilePath as a
946 @param ParentImageHandle The caller's image handle.
947 @param FilePath The specific file path from which the image is
949 @param SourceBuffer If not NULL, a pointer to the memory location
950 containing a copy of the image to be loaded.
951 @param SourceSize The size in bytes of SourceBuffer.
952 @param ImageHandle Pointer to the returned image handle that is
953 created when the image is successfully loaded.
955 @retval EFI_SUCCESS The image was loaded into memory.
956 @retval EFI_NOT_FOUND The FilePath was not found.
957 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
958 @retval EFI_UNSUPPORTED The image type is not supported, or the device
959 path cannot be parsed to locate the proper
960 protocol for loading the file.
961 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
968 IN BOOLEAN BootPolicy
,
969 IN EFI_HANDLE ParentImageHandle
,
970 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
971 IN VOID
*SourceBuffer OPTIONAL
,
973 OUT EFI_HANDLE
*ImageHandle
981 Tick
= GetPerformanceCounter ();
984 Status
= CoreLoadImageCommon (
990 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
994 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
997 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
998 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1006 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1008 @param This Calling context
1009 @param ParentImageHandle The caller's image handle.
1010 @param FilePath The specific file path from which the image is
1012 @param SourceBuffer If not NULL, a pointer to the memory location
1013 containing a copy of the image to be loaded.
1014 @param SourceSize The size in bytes of SourceBuffer.
1015 @param DstBuffer The buffer to store the image.
1016 @param NumberOfPages For input, specifies the space size of the
1017 image by caller if not NULL. For output,
1018 specifies the actual space size needed.
1019 @param ImageHandle Image handle for output.
1020 @param EntryPoint Image entry point for output.
1021 @param Attribute The bit mask of attributes to set for the load
1024 @retval EFI_SUCCESS The image was loaded into memory.
1025 @retval EFI_NOT_FOUND The FilePath was not found.
1026 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1027 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1028 path cannot be parsed to locate the proper
1029 protocol for loading the file.
1030 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1037 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
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 OUT UINTN
*NumberOfPages OPTIONAL
,
1044 OUT EFI_HANDLE
*ImageHandle
,
1045 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1049 return CoreLoadImageCommon (
1065 Transfer control to a loaded image's entry point.
1067 @param ImageHandle Handle of image to be started.
1068 @param ExitDataSize Pointer of the size to ExitData
1069 @param ExitData Pointer to a pointer to a data buffer that
1070 includes a Null-terminated Unicode string,
1071 optionally followed by additional binary data.
1072 The string is a description that the caller may
1073 use to further indicate the reason for the
1076 @retval EFI_INVALID_PARAMETER Invalid parameter
1077 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1078 @retval EFI_SUCCESS Successfully transfer control to the image's
1085 IN EFI_HANDLE ImageHandle
,
1086 OUT UINTN
*ExitDataSize
,
1087 OUT CHAR16
**ExitData OPTIONAL
1091 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1092 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1093 UINT64 HandleDatabaseKey
;
1096 Image
= CoreLoadedImageInfo (ImageHandle
);
1097 if (Image
== NULL
|| Image
->Started
) {
1098 return EFI_INVALID_PARAMETER
;
1102 // Don't profile Objects or invalid start requests
1104 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1108 // Push the current start image context, and
1109 // link the current image to the head. This is the
1110 // only image that can call Exit()
1112 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1113 LastImage
= mCurrentImage
;
1114 mCurrentImage
= Image
;
1115 Image
->Tpl
= gEfiCurrentTpl
;
1118 // Set long jump for Exit() support
1119 // JumpContext must be aligned on a CPU specific boundary.
1120 // Overallocate the buffer and force the required alignment
1122 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1123 if (Image
->JumpBuffer
== NULL
) {
1124 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1125 return EFI_OUT_OF_RESOURCES
;
1127 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1129 SetJumpFlag
= SetJump (Image
->JumpContext
);
1131 // The initial call to SetJump() must always return 0.
1132 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1134 if (SetJumpFlag
== 0) {
1136 // Call the image's entry point
1138 Image
->Started
= TRUE
;
1139 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1142 // Add some debug information if the image returned with error.
1143 // This make the user aware and check if the driver image have already released
1144 // all the resource in this situation.
1146 DEBUG_CODE_BEGIN ();
1147 if (EFI_ERROR (Image
->Status
)) {
1148 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1153 // If the image returns, exit it through Exit()
1155 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1159 // Image has completed. Verify the tpl is the same
1161 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1162 CoreRestoreTpl (Image
->Tpl
);
1164 CoreFreePool (Image
->JumpBuffer
);
1167 // Pop the current start image context
1169 mCurrentImage
= LastImage
;
1172 // Go connect any handles that were created or modified while the image executed.
1174 CoreConnectHandlesByKey (HandleDatabaseKey
);
1177 // Handle the image's returned ExitData
1179 DEBUG_CODE_BEGIN ();
1180 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1182 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1183 if (Image
->ExitData
!= NULL
) {
1184 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1186 DEBUG ((DEBUG_LOAD
, "\n"));
1191 // Return the exit data to the caller
1193 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1194 *ExitDataSize
= Image
->ExitDataSize
;
1195 *ExitData
= Image
->ExitData
;
1198 // Caller doesn't want the exit data, free it
1200 CoreFreePool (Image
->ExitData
);
1201 Image
->ExitData
= NULL
;
1205 // Save the Status because Image will get destroyed if it is unloaded.
1207 Status
= Image
->Status
;
1210 // If the image returned an error, or if the image is an application
1213 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1214 CoreUnloadAndCloseImage (Image
, TRUE
);
1220 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1225 Terminates the currently loaded EFI image and returns control to boot services.
1227 @param ImageHandle Handle that identifies the image. This
1228 parameter is passed to the image on entry.
1229 @param Status The image's exit code.
1230 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1231 ExitStatus is EFI_SUCCESS.
1232 @param ExitData Pointer to a data buffer that includes a
1233 Null-terminated Unicode string, optionally
1234 followed by additional binary data. The string
1235 is a description that the caller may use to
1236 further indicate the reason for the image's
1239 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1241 @retval EFI_SUCCESS Successfully terminates the currently loaded
1243 @retval EFI_ACCESS_DENIED Should never reach there.
1244 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1250 IN EFI_HANDLE ImageHandle
,
1251 IN EFI_STATUS Status
,
1252 IN UINTN ExitDataSize
,
1253 IN CHAR16
*ExitData OPTIONAL
1256 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1260 // Prevent possible reentrance to this function
1261 // for the same ImageHandle
1263 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1265 Image
= CoreLoadedImageInfo (ImageHandle
);
1266 if (Image
== NULL
) {
1267 Status
= EFI_INVALID_PARAMETER
;
1271 if (!Image
->Started
) {
1273 // The image has not been started so just free its resources
1275 CoreUnloadAndCloseImage (Image
, TRUE
);
1276 Status
= EFI_SUCCESS
;
1281 // Image has been started, verify this image can exit
1283 if (Image
!= mCurrentImage
) {
1284 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1285 Status
= EFI_INVALID_PARAMETER
;
1292 Image
->Status
= Status
;
1295 // If there's ExitData info, move it
1297 if (ExitData
!= NULL
) {
1298 Image
->ExitDataSize
= ExitDataSize
;
1299 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1300 if (Image
->ExitData
== NULL
) {
1301 Status
= EFI_OUT_OF_RESOURCES
;
1304 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1307 CoreRestoreTpl (OldTpl
);
1309 // return to StartImage
1311 LongJump (Image
->JumpContext
, (UINTN
)-1);
1314 // If we return from LongJump, then it is an error
1317 Status
= EFI_ACCESS_DENIED
;
1319 CoreRestoreTpl (OldTpl
);
1329 @param ImageHandle Handle that identifies the image to be
1332 @retval EFI_SUCCESS The image has been unloaded.
1333 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1335 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1341 IN EFI_HANDLE ImageHandle
1345 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1347 Image
= CoreLoadedImageInfo (ImageHandle
);
1348 if (Image
== NULL
) {
1350 // The image handle is not valid
1352 Status
= EFI_INVALID_PARAMETER
;
1356 if (Image
->Started
) {
1358 // The image has been started, request it to unload.
1360 Status
= EFI_UNSUPPORTED
;
1361 if (Image
->Info
.Unload
!= NULL
) {
1362 Status
= Image
->Info
.Unload (ImageHandle
);
1367 // This Image hasn't been started, thus it can be unloaded
1369 Status
= EFI_SUCCESS
;
1373 if (!EFI_ERROR (Status
)) {
1375 // if the Image was not started or Unloaded O.K. then clean up
1377 CoreUnloadAndCloseImage (Image
, TRUE
);
1387 Unload the specified image.
1389 @param This Indicates the calling context.
1390 @param ImageHandle The specified image handle.
1392 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1393 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1394 @retval EFI_SUCCESS Image successfully unloaded.
1400 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1401 IN EFI_HANDLE ImageHandle
1404 return CoreUnloadImage (ImageHandle
);