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.
20 LOADED_IMAGE_PRIVATE_DATA
*mCurrentImage
= NULL
;
22 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData
= {
23 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE
,
33 // This code is needed to build the Image handle for the DXE Core
35 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage
= {
36 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
, // Signature
38 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
, // Image type
39 TRUE
, // If entrypoint has been called
42 EFI_LOADED_IMAGE_INFORMATION_REVISION
, // Revision
43 NULL
, // Parent handle
44 NULL
, // System handle
46 NULL
, // Device handle
55 EfiBootServicesCode
, // ImageCodeType
56 EfiBootServicesData
// ImageDataType
58 (EFI_PHYSICAL_ADDRESS
)0, // ImageBasePage
62 EFI_SUCCESS
, // Status
70 NULL
// LoadedImageDevicePath
76 Add the Image Services to EFI Boot Services Table and install the protocol
77 interfaces for this image.
79 @param HobStart The HOB to initialize
85 CoreInitializeImageServices (
90 LOADED_IMAGE_PRIVATE_DATA
*Image
;
91 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
92 UINT64 DxeCoreImageLength
;
93 VOID
*DxeCoreEntryPoint
;
94 EFI_PEI_HOB_POINTERS DxeCoreHob
;
96 // Searching for image hob
98 DxeCoreHob
.Raw
= HobStart
;
99 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
100 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
106 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
108 ASSERT (DxeCoreHob
.Raw
!= NULL
);
110 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
111 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
112 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
113 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
115 // Initialize the fields for an internal driver
117 Image
= &mCorePrivateImage
;
119 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
120 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
121 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
122 Image
->Tpl
= gEfiCurrentTpl
;
123 Image
->Info
.SystemTable
= gDxeCoreST
;
124 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
125 Image
->Info
.ImageSize
= DxeCoreImageLength
;
128 // Install the protocol interfaces for this image
130 Status
= CoreInstallProtocolInterface (
132 &gEfiLoadedImageProtocolGuid
,
133 EFI_NATIVE_INTERFACE
,
136 ASSERT_EFI_ERROR (Status
);
138 mCurrentImage
= Image
;
141 // Fill in DXE globals
143 gDxeCoreImageHandle
= Image
->Handle
;
144 gDxeCoreLoadedImage
= &Image
->Info
;
147 // Export DXE Core PE Loader functionality
149 return CoreInstallProtocolInterface (
150 &mLoadPe32PrivateData
.Handle
,
151 &gEfiLoadPeImageProtocolGuid
,
152 EFI_NATIVE_INTERFACE
,
153 &mLoadPe32PrivateData
.Pe32Image
159 Loads, relocates, and invokes a PE/COFF image
161 @param BootPolicy If TRUE, indicates that the request originates
162 from the boot manager, and that the boot
163 manager is attempting to load FilePath as a
165 @param Pe32Handle The handle of PE32 image
166 @param Image PE image to be loaded
167 @param DstBuffer The buffer to store the image
168 @param EntryPoint A pointer to the entry point
169 @param Attribute The bit mask of attributes to set for the load
172 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
173 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
174 relocate the PE/COFF file
175 @retval EFI_INVALID_PARAMETER Invalid parameter
176 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
181 IN BOOLEAN BootPolicy
,
183 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
184 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
185 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
190 BOOLEAN DstBufAlocated
;
193 EFI_TCG_PLATFORM_PROTOCOL
*TcgPlatformProtocol
;
194 IMAGE_FILE_HANDLE
*FHandle
;
197 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
199 Image
->ImageContext
.Handle
= Pe32Handle
;
200 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
203 // Get information about the image being loaded
205 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
206 if (EFI_ERROR (Status
)) {
210 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
211 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
213 // The PE/COFF loader can support loading image types that can be executed.
214 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
216 return EFI_UNSUPPORTED
;
221 // Set EFI memory type based on ImageType
223 switch (Image
->ImageContext
.ImageType
) {
224 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
225 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
226 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
228 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
229 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
230 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
232 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
233 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
234 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
235 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
238 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
239 return EFI_UNSUPPORTED
;
242 // Get the image base address in the original PeImage.
244 LinkTimeBase
= (UINTN
) Image
->ImageContext
.ImageAddress
;
247 // Allocate memory of the correct memory type aligned on the required image boundry
249 DstBufAlocated
= FALSE
;
250 if (DstBuffer
== 0) {
252 // Allocate Destination Buffer as caller did not pass it in
255 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
256 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
258 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
261 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
264 // If the image relocations have not been stripped, then load at any address.
265 // Otherwise load at the address at which it was linked.
267 // Memory below 1MB should be treated reserved for CSM and there should be
268 // no modules whose preferred load addresses are below 1MB.
270 Status
= EFI_OUT_OF_RESOURCES
;
271 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
272 Status
= CoreAllocatePages (
274 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
275 Image
->NumberOfPages
,
276 &Image
->ImageContext
.ImageAddress
279 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
280 Status
= CoreAllocatePages (
282 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
283 Image
->NumberOfPages
,
284 &Image
->ImageContext
.ImageAddress
287 if (EFI_ERROR (Status
)) {
290 DstBufAlocated
= TRUE
;
293 // Caller provided the destination buffer
296 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
298 // If the image relocations were stripped, and the caller provided a
299 // destination buffer address that does not match the address that the
300 // image is linked at, then the image cannot be loaded.
302 return EFI_INVALID_PARAMETER
;
305 if (Image
->NumberOfPages
!= 0 &&
306 Image
->NumberOfPages
<
307 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
308 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
309 return EFI_BUFFER_TOO_SMALL
;
312 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
313 Image
->ImageContext
.ImageAddress
= DstBuffer
;
316 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
317 if (!Image
->ImageContext
.IsTeImage
) {
318 Image
->ImageContext
.ImageAddress
=
319 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
320 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
324 // Load the image from the file into the allocated memory
326 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
327 if (EFI_ERROR (Status
)) {
332 // If this is a Runtime Driver, then allocate memory for the FixupData that
333 // is used to relocate the image when SetVirtualAddressMap() is called. The
334 // relocation is done by the Runtime AP.
336 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
337 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
338 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
339 if (Image
->ImageContext
.FixupData
== NULL
) {
340 Status
= EFI_OUT_OF_RESOURCES
;
347 // Measure the image before applying fixup
349 Status
= CoreLocateProtocol (
350 &gEfiTcgPlatformProtocolGuid
,
352 (VOID
**) &TcgPlatformProtocol
354 if (!EFI_ERROR (Status
)) {
355 FHandle
= (IMAGE_FILE_HANDLE
*) Image
->ImageContext
.Handle
;
356 Status
= TcgPlatformProtocol
->MeasurePeImage (
358 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FHandle
->Source
,
361 Image
->ImageContext
.ImageType
,
362 Image
->Info
.DeviceHandle
,
366 ASSERT_EFI_ERROR (Status
);
370 // Relocate the image in memory
372 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
373 if (EFI_ERROR (Status
)) {
378 // Flush the Instruction Cache
380 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
383 // Copy the machine type from the context to the image private data. This
384 // is needed during image unload to know if we should call an EBC protocol
385 // to unload the image.
387 Image
->Machine
= Image
->ImageContext
.Machine
;
390 // Get the image entry point. If it's an EBC image, then call into the
391 // interpreter to create a thunk for the entry point and use the returned
392 // value for the entry point.
394 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
395 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
397 // Locate the EBC interpreter protocol
399 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
400 if (EFI_ERROR(Status
)) {
401 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
406 // Register a callback for flushing the instruction cache so that created
407 // thunks can be flushed.
409 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
410 if (EFI_ERROR(Status
)) {
415 // Create a thunk for the image's entry point. This will be the new
416 // entry point for the image.
418 Status
= Image
->Ebc
->CreateThunk (
421 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
422 (VOID
**) &Image
->EntryPoint
424 if (EFI_ERROR(Status
)) {
430 // Fill in the image information for the Loaded Image Protocol
432 Image
->Type
= Image
->ImageContext
.ImageType
;
433 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
434 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
435 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
436 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
437 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
438 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
440 // Make a list off all the RT images so we can let the RT AP know about them.
442 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
443 if (Image
->RuntimeData
== NULL
) {
446 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
447 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
448 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
449 Image
->RuntimeData
->Handle
= Image
->Handle
;
450 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
455 // Fill in the entry point of the image if it is available
457 if (EntryPoint
!= NULL
) {
458 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
462 // Print the load address and the PDB file name if it is available
469 CHAR8 EfiFileName
[256];
472 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
473 "Loading driver at 0x%11p EntryPoint=0x%11p ",
474 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
475 FUNCTION_ENTRY_POINT ((UINTN
) Image
->ImageContext
.EntryPoint
)));
479 // Print Module Name by Pdb file path
481 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
483 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
484 if (Image
->ImageContext
.PdbPointer
[Index
] == '\\') {
485 StartIndex
= Index
+ 1;
489 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
491 for (Index
= 0; Index
< sizeof (EfiFileName
); Index
++) {
492 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
493 if (EfiFileName
[Index
] == 0) {
494 EfiFileName
[Index
] = '.';
496 if (EfiFileName
[Index
] == '.') {
497 EfiFileName
[Index
+ 1] = 'e';
498 EfiFileName
[Index
+ 2] = 'f';
499 EfiFileName
[Index
+ 3] = 'i';
500 EfiFileName
[Index
+ 4] = 0;
504 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
506 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
518 if (DstBufAlocated
) {
519 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
522 if (Image
->ImageContext
.FixupData
!= NULL
) {
523 CoreFreePool (Image
->ImageContext
.FixupData
);
532 Get the image's private data from its handle.
534 @param ImageHandle The image handle
536 @return Return the image private data associated with ImageHandle.
539 LOADED_IMAGE_PRIVATE_DATA
*
540 CoreLoadedImageInfo (
541 IN EFI_HANDLE ImageHandle
545 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
546 LOADED_IMAGE_PRIVATE_DATA
*Image
;
548 Status
= CoreHandleProtocol (
550 &gEfiLoadedImageProtocolGuid
,
551 (VOID
**)&LoadedImage
553 if (!EFI_ERROR (Status
)) {
554 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
556 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
565 Unloads EFI image from memory.
567 @param Image EFI image
568 @param FreePage Free allocated pages
572 CoreUnloadAndCloseImage (
573 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
579 EFI_HANDLE
*HandleBuffer
;
581 EFI_GUID
**ProtocolGuidArray
;
584 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
588 if (Image
->Ebc
!= NULL
) {
590 // If EBC protocol exists we must perform cleanups for this image.
592 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
596 // Unload image, free Image->ImageContext->ModHandle
598 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
601 // Free our references to the image handle
603 if (Image
->Handle
!= NULL
) {
605 Status
= CoreLocateHandleBuffer (
612 if (!EFI_ERROR (Status
)) {
613 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
614 Status
= CoreProtocolsPerHandle (
615 HandleBuffer
[HandleIndex
],
619 if (!EFI_ERROR (Status
)) {
620 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
621 Status
= CoreOpenProtocolInformation (
622 HandleBuffer
[HandleIndex
],
623 ProtocolGuidArray
[ProtocolIndex
],
627 if (!EFI_ERROR (Status
)) {
628 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
629 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
630 Status
= CoreCloseProtocol (
631 HandleBuffer
[HandleIndex
],
632 ProtocolGuidArray
[ProtocolIndex
],
634 OpenInfo
[OpenInfoIndex
].ControllerHandle
638 if (OpenInfo
!= NULL
) {
639 CoreFreePool(OpenInfo
);
643 if (ProtocolGuidArray
!= NULL
) {
644 CoreFreePool(ProtocolGuidArray
);
648 if (HandleBuffer
!= NULL
) {
649 CoreFreePool (HandleBuffer
);
653 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
655 Status
= CoreUninstallProtocolInterface (
657 &gEfiLoadedImageDevicePathProtocolGuid
,
658 Image
->LoadedImageDevicePath
661 Status
= CoreUninstallProtocolInterface (
663 &gEfiLoadedImageProtocolGuid
,
669 if (Image
->RuntimeData
!= NULL
) {
670 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
672 // Remove the Image from the Runtime Image list as we are about to Free it!
674 RemoveEntryList (&Image
->RuntimeData
->Link
);
676 CoreFreePool (Image
->RuntimeData
);
680 // Free the Image from memory
682 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
683 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
687 // Done with the Image structure
689 if (Image
->Info
.FilePath
!= NULL
) {
690 CoreFreePool (Image
->Info
.FilePath
);
693 if (Image
->LoadedImageDevicePath
!= NULL
) {
694 CoreFreePool (Image
->LoadedImageDevicePath
);
697 if (Image
->FixupData
!= NULL
) {
698 CoreFreePool (Image
->FixupData
);
701 CoreFreePool (Image
);
706 Loads an EFI image into memory and returns a handle to the image.
708 @param BootPolicy If TRUE, indicates that the request originates
709 from the boot manager, and that the boot
710 manager is attempting to load FilePath as a
712 @param ParentImageHandle The caller's image handle.
713 @param FilePath The specific file path from which the image is
715 @param SourceBuffer If not NULL, a pointer to the memory location
716 containing a copy of the image to be loaded.
717 @param SourceSize The size in bytes of SourceBuffer.
718 @param DstBuffer The buffer to store the image
719 @param NumberOfPages If not NULL, it inputs a pointer to the page
720 number of DstBuffer and outputs a pointer to
721 the page number of the image. If this number is
722 not enough, return EFI_BUFFER_TOO_SMALL and
723 this parameter contains the required number.
724 @param ImageHandle Pointer to the returned image handle that is
725 created when the image is successfully loaded.
726 @param EntryPoint A pointer to the entry point
727 @param Attribute The bit mask of attributes to set for the load
730 @retval EFI_SUCCESS The image was loaded into memory.
731 @retval EFI_NOT_FOUND The FilePath was not found.
732 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
733 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
734 @retval EFI_UNSUPPORTED The image type is not supported, or the device
735 path cannot be parsed to locate the proper
736 protocol for loading the file.
737 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
742 CoreLoadImageCommon (
743 IN BOOLEAN BootPolicy
,
744 IN EFI_HANDLE ParentImageHandle
,
745 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
746 IN VOID
*SourceBuffer OPTIONAL
,
748 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
749 IN OUT UINTN
*NumberOfPages OPTIONAL
,
750 OUT EFI_HANDLE
*ImageHandle
,
751 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
755 LOADED_IMAGE_PRIVATE_DATA
*Image
;
756 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
757 IMAGE_FILE_HANDLE FHand
;
759 EFI_STATUS SecurityStatus
;
760 EFI_HANDLE DeviceHandle
;
761 UINT32 AuthenticationStatus
;
762 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
763 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
766 SecurityStatus
= EFI_SUCCESS
;
768 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
772 // The caller must pass in a valid ParentImageHandle
774 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
775 return EFI_INVALID_PARAMETER
;
778 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
779 if (ParentImage
== NULL
) {
780 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
781 return EFI_INVALID_PARAMETER
;
785 // Get simple read access to the source file
787 OriginalFilePath
= FilePath
;
788 Status
= CoreOpenImageFile (
795 &AuthenticationStatus
797 if (Status
== EFI_ALREADY_STARTED
) {
800 } else if (EFI_ERROR (Status
)) {
805 // Verify the Authentication Status through the Security Architectural Protocol
807 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
808 SecurityStatus
= gSecurity
->FileAuthenticationState (
810 AuthenticationStatus
,
813 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
814 Status
= SecurityStatus
;
822 // Allocate a new image structure
824 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
826 return EFI_OUT_OF_RESOURCES
;
830 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
832 FilePath
= OriginalFilePath
;
833 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
834 if (!EFI_ERROR (Status
)) {
835 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
836 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
840 // Initialize the fields for an internal driver
842 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
843 Image
->Info
.SystemTable
= gDxeCoreST
;
844 Image
->Info
.DeviceHandle
= DeviceHandle
;
845 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
846 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
847 Image
->Info
.ParentHandle
= ParentImageHandle
;
850 if (NumberOfPages
!= NULL
) {
851 Image
->NumberOfPages
= *NumberOfPages
;
853 Image
->NumberOfPages
= 0 ;
857 // Install the protocol interfaces for this image
858 // don't fire notifications yet
860 Status
= CoreInstallProtocolInterfaceNotify (
862 &gEfiLoadedImageProtocolGuid
,
863 EFI_NATIVE_INTERFACE
,
867 if (EFI_ERROR (Status
)) {
872 // Load the image. If EntryPoint is Null, it will not be set.
874 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
875 if (EFI_ERROR (Status
)) {
876 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
877 if (NumberOfPages
!= NULL
) {
878 *NumberOfPages
= Image
->NumberOfPages
;
884 if (NumberOfPages
!= NULL
) {
885 *NumberOfPages
= Image
->NumberOfPages
;
889 // Register the image in the Debug Image Info Table if the attribute is set
891 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
892 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
896 //Reinstall loaded image protocol to fire any notifications
898 Status
= CoreReinstallProtocolInterface (
900 &gEfiLoadedImageProtocolGuid
,
904 if (EFI_ERROR (Status
)) {
909 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
910 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
912 if (OriginalFilePath
!= NULL
) {
913 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
917 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
919 Status
= CoreInstallProtocolInterface (
921 &gEfiLoadedImageDevicePathProtocolGuid
,
922 EFI_NATIVE_INTERFACE
,
923 Image
->LoadedImageDevicePath
925 if (EFI_ERROR (Status
)) {
930 // Success. Return the image handle
932 *ImageHandle
= Image
->Handle
;
936 // All done accessing the source file
937 // If we allocated the Source buffer, free it
939 if (FHand
.FreeBuffer
) {
940 CoreFreePool (FHand
.Source
);
944 // There was an error. If there's an Image structure, free it
946 if (EFI_ERROR (Status
)) {
948 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
951 } else if (EFI_ERROR (SecurityStatus
)) {
952 Status
= SecurityStatus
;
962 Loads an EFI image into memory and returns a handle to the image.
964 @param BootPolicy If TRUE, indicates that the request originates
965 from the boot manager, and that the boot
966 manager is attempting to load FilePath as a
968 @param ParentImageHandle The caller's image handle.
969 @param FilePath The specific file path from which the image is
971 @param SourceBuffer If not NULL, a pointer to the memory location
972 containing a copy of the image to be loaded.
973 @param SourceSize The size in bytes of SourceBuffer.
974 @param ImageHandle Pointer to the returned image handle that is
975 created when the image is successfully loaded.
977 @retval EFI_SUCCESS The image was loaded into memory.
978 @retval EFI_NOT_FOUND The FilePath was not found.
979 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
980 @retval EFI_UNSUPPORTED The image type is not supported, or the device
981 path cannot be parsed to locate the proper
982 protocol for loading the file.
983 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
990 IN BOOLEAN BootPolicy
,
991 IN EFI_HANDLE ParentImageHandle
,
992 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
993 IN VOID
*SourceBuffer OPTIONAL
,
995 OUT EFI_HANDLE
*ImageHandle
1000 PERF_START (NULL
, "LoadImage", NULL
, 0);
1002 Status
= CoreLoadImageCommon (
1008 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1012 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1015 PERF_END (NULL
, "LoadImage", NULL
, 0);
1023 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1025 @param This Calling context
1026 @param ParentImageHandle The caller's image handle.
1027 @param FilePath The specific file path from which the image is
1029 @param SourceBuffer If not NULL, a pointer to the memory location
1030 containing a copy of the image to be loaded.
1031 @param SourceSize The size in bytes of SourceBuffer.
1032 @param DstBuffer The buffer to store the image.
1033 @param NumberOfPages For input, specifies the space size of the
1034 image by caller if not NULL. For output,
1035 specifies the actual space size needed.
1036 @param ImageHandle Image handle for output.
1037 @param EntryPoint Image entry point for output.
1038 @param Attribute The bit mask of attributes to set for the load
1041 @retval EFI_SUCCESS The image was loaded into memory.
1042 @retval EFI_NOT_FOUND The FilePath was not found.
1043 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1044 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1045 path cannot be parsed to locate the proper
1046 protocol for loading the file.
1047 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1054 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1055 IN EFI_HANDLE ParentImageHandle
,
1056 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1057 IN VOID
*SourceBuffer OPTIONAL
,
1058 IN UINTN SourceSize
,
1059 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1060 OUT UINTN
*NumberOfPages OPTIONAL
,
1061 OUT EFI_HANDLE
*ImageHandle
,
1062 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1066 return CoreLoadImageCommon (
1082 Transfer control to a loaded image's entry point.
1084 @param ImageHandle Handle of image to be started.
1085 @param ExitDataSize Pointer of the size to ExitData
1086 @param ExitData Pointer to a pointer to a data buffer that
1087 includes a Null-terminated Unicode string,
1088 optionally followed by additional binary data.
1089 The string is a description that the caller may
1090 use to further indicate the reason for the
1093 @retval EFI_INVALID_PARAMETER Invalid parameter
1094 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1095 @retval EFI_SUCCESS Successfully transfer control to the image's
1102 IN EFI_HANDLE ImageHandle
,
1103 OUT UINTN
*ExitDataSize
,
1104 OUT CHAR16
**ExitData OPTIONAL
1108 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1109 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1110 UINT64 HandleDatabaseKey
;
1113 Image
= CoreLoadedImageInfo (ImageHandle
);
1114 if (Image
== NULL
|| Image
->Started
) {
1115 return EFI_INVALID_PARAMETER
;
1119 // Don't profile Objects or invalid start requests
1121 PERF_START (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1125 // Push the current start image context, and
1126 // link the current image to the head. This is the
1127 // only image that can call Exit()
1129 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1130 LastImage
= mCurrentImage
;
1131 mCurrentImage
= Image
;
1132 Image
->Tpl
= gEfiCurrentTpl
;
1135 // Set long jump for Exit() support
1136 // JumpContext must be aligned on a CPU specific boundary.
1137 // Overallocate the buffer and force the required alignment
1139 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1140 if (Image
->JumpBuffer
== NULL
) {
1141 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1142 return EFI_OUT_OF_RESOURCES
;
1144 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1146 SetJumpFlag
= SetJump (Image
->JumpContext
);
1148 // The initial call to SetJump() must always return 0.
1149 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1151 if (SetJumpFlag
== 0) {
1153 // Call the image's entry point
1155 Image
->Started
= TRUE
;
1156 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1159 // Add some debug information if the image returned with error.
1160 // This make the user aware and check if the driver image have already released
1161 // all the resource in this situation.
1163 DEBUG_CODE_BEGIN ();
1164 if (EFI_ERROR (Image
->Status
)) {
1165 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1170 // If the image returns, exit it through Exit()
1172 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1176 // Image has completed. Verify the tpl is the same
1178 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1179 CoreRestoreTpl (Image
->Tpl
);
1181 CoreFreePool (Image
->JumpBuffer
);
1184 // Pop the current start image context
1186 mCurrentImage
= LastImage
;
1189 // Go connect any handles that were created or modified while the image executed.
1191 CoreConnectHandlesByKey (HandleDatabaseKey
);
1194 // Handle the image's returned ExitData
1196 DEBUG_CODE_BEGIN ();
1197 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1199 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %x", Image
->ExitDataSize
, Image
->ExitData
));
1200 if (Image
->ExitData
!= NULL
) {
1201 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1203 DEBUG ((DEBUG_LOAD
, "\n"));
1208 // Return the exit data to the caller
1210 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1211 *ExitDataSize
= Image
->ExitDataSize
;
1212 *ExitData
= Image
->ExitData
;
1215 // Caller doesn't want the exit data, free it
1217 CoreFreePool (Image
->ExitData
);
1218 Image
->ExitData
= NULL
;
1222 // Save the Status because Image will get destroyed if it is unloaded.
1224 Status
= Image
->Status
;
1227 // If the image returned an error, or if the image is an application
1230 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1231 CoreUnloadAndCloseImage (Image
, TRUE
);
1237 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1242 Terminates the currently loaded EFI image and returns control to boot services.
1244 @param ImageHandle Handle that identifies the image. This
1245 parameter is passed to the image on entry.
1246 @param Status The image's exit code.
1247 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1248 ExitStatus is EFI_SUCCESS.
1249 @param ExitData Pointer to a data buffer that includes a
1250 Null-terminated Unicode string, optionally
1251 followed by additional binary data. The string
1252 is a description that the caller may use to
1253 further indicate the reason for the image's
1256 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1258 @retval EFI_SUCCESS Successfully terminates the currently loaded
1260 @retval EFI_ACCESS_DENIED Should never reach there.
1261 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1267 IN EFI_HANDLE ImageHandle
,
1268 IN EFI_STATUS Status
,
1269 IN UINTN ExitDataSize
,
1270 IN CHAR16
*ExitData OPTIONAL
1273 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1277 // Prevent possible reentrance to this function
1278 // for the same ImageHandle
1280 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1282 Image
= CoreLoadedImageInfo (ImageHandle
);
1283 if (Image
== NULL
) {
1284 Status
= EFI_INVALID_PARAMETER
;
1288 if (!Image
->Started
) {
1290 // The image has not been started so just free its resources
1292 CoreUnloadAndCloseImage (Image
, TRUE
);
1293 Status
= EFI_SUCCESS
;
1298 // Image has been started, verify this image can exit
1300 if (Image
!= mCurrentImage
) {
1301 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1302 Status
= EFI_INVALID_PARAMETER
;
1309 Image
->Status
= Status
;
1312 // If there's ExitData info, move it
1314 if (ExitData
!= NULL
) {
1315 Image
->ExitDataSize
= ExitDataSize
;
1316 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1317 if (Image
->ExitData
== NULL
) {
1318 Status
= EFI_OUT_OF_RESOURCES
;
1321 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1324 CoreRestoreTpl (OldTpl
);
1326 // return to StartImage
1328 LongJump (Image
->JumpContext
, (UINTN
)-1);
1331 // If we return from LongJump, then it is an error
1334 Status
= EFI_ACCESS_DENIED
;
1336 CoreRestoreTpl (OldTpl
);
1346 @param ImageHandle Handle that identifies the image to be
1349 @retval EFI_SUCCESS The image has been unloaded.
1350 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1352 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1358 IN EFI_HANDLE ImageHandle
1362 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1364 Image
= CoreLoadedImageInfo (ImageHandle
);
1365 if (Image
== NULL
) {
1367 // The image handle is not valid
1369 Status
= EFI_INVALID_PARAMETER
;
1373 if (Image
->Started
) {
1375 // The image has been started, request it to unload.
1377 Status
= EFI_UNSUPPORTED
;
1378 if (Image
->Info
.Unload
!= NULL
) {
1379 Status
= Image
->Info
.Unload (ImageHandle
);
1384 // This Image hasn't been started, thus it can be unloaded
1386 Status
= EFI_SUCCESS
;
1390 if (!EFI_ERROR (Status
)) {
1392 // if the Image was not started or Unloaded O.K. then clean up
1394 CoreUnloadAndCloseImage (Image
, TRUE
);
1404 Unload the specified image.
1406 @param This Indicates the calling context.
1407 @param ImageHandle The specified image handle.
1409 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1410 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1411 @retval EFI_SUCCESS Image successfully unloaded.
1417 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1418 IN EFI_HANDLE ImageHandle
1421 return CoreUnloadImage (ImageHandle
);