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 Image
->ImageContext
.ImageAddress
=
318 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
319 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
322 // Load the image from the file into the allocated memory
324 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
325 if (EFI_ERROR (Status
)) {
330 // If this is a Runtime Driver, then allocate memory for the FixupData that
331 // is used to relocate the image when SetVirtualAddressMap() is called. The
332 // relocation is done by the Runtime AP.
334 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
335 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
336 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
337 if (Image
->ImageContext
.FixupData
== NULL
) {
338 Status
= EFI_OUT_OF_RESOURCES
;
345 // Measure the image before applying fixup
347 Status
= CoreLocateProtocol (
348 &gEfiTcgPlatformProtocolGuid
,
350 (VOID
**) &TcgPlatformProtocol
352 if (!EFI_ERROR (Status
)) {
353 FHandle
= (IMAGE_FILE_HANDLE
*) Image
->ImageContext
.Handle
;
354 Status
= TcgPlatformProtocol
->MeasurePeImage (
356 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FHandle
->Source
,
359 Image
->ImageContext
.ImageType
,
360 Image
->Info
.DeviceHandle
,
364 ASSERT_EFI_ERROR (Status
);
368 // Relocate the image in memory
370 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
371 if (EFI_ERROR (Status
)) {
376 // Flush the Instruction Cache
378 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
381 // Copy the machine type from the context to the image private data. This
382 // is needed during image unload to know if we should call an EBC protocol
383 // to unload the image.
385 Image
->Machine
= Image
->ImageContext
.Machine
;
388 // Get the image entry point. If it's an EBC image, then call into the
389 // interpreter to create a thunk for the entry point and use the returned
390 // value for the entry point.
392 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
393 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
395 // Locate the EBC interpreter protocol
397 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
398 if (EFI_ERROR(Status
)) {
399 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
404 // Register a callback for flushing the instruction cache so that created
405 // thunks can be flushed.
407 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
408 if (EFI_ERROR(Status
)) {
413 // Create a thunk for the image's entry point. This will be the new
414 // entry point for the image.
416 Status
= Image
->Ebc
->CreateThunk (
419 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
420 (VOID
**) &Image
->EntryPoint
422 if (EFI_ERROR(Status
)) {
428 // Fill in the image information for the Loaded Image Protocol
430 Image
->Type
= Image
->ImageContext
.ImageType
;
431 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
432 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
433 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
434 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
435 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
436 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
438 // Make a list off all the RT images so we can let the RT AP know about them.
440 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
441 if (Image
->RuntimeData
== NULL
) {
444 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
445 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
446 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
447 Image
->RuntimeData
->Handle
= Image
->Handle
;
448 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
453 // Fill in the entry point of the image if it is available
455 if (EntryPoint
!= NULL
) {
456 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
460 // Print the load address and the PDB file name if it is available
467 CHAR8 EfiFileName
[256];
470 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
471 "Loading driver at 0x%11p EntryPoint=0x%11p ",
472 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
473 FUNCTION_ENTRY_POINT ((UINTN
) Image
->ImageContext
.EntryPoint
)));
477 // Print Module Name by Pdb file path
479 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
481 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
482 if (Image
->ImageContext
.PdbPointer
[Index
] == '\\') {
483 StartIndex
= Index
+ 1;
487 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
489 for (Index
= 0; Index
< sizeof (EfiFileName
); Index
++) {
490 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
491 if (EfiFileName
[Index
] == 0) {
492 EfiFileName
[Index
] = '.';
494 if (EfiFileName
[Index
] == '.') {
495 EfiFileName
[Index
+ 1] = 'e';
496 EfiFileName
[Index
+ 2] = 'f';
497 EfiFileName
[Index
+ 3] = 'i';
498 EfiFileName
[Index
+ 4] = 0;
502 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
504 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
516 if (DstBufAlocated
) {
517 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
520 if (Image
->ImageContext
.FixupData
!= NULL
) {
521 CoreFreePool (Image
->ImageContext
.FixupData
);
530 Get the image's private data from its handle.
532 @param ImageHandle The image handle
534 @return Return the image private data associated with ImageHandle.
537 LOADED_IMAGE_PRIVATE_DATA
*
538 CoreLoadedImageInfo (
539 IN EFI_HANDLE ImageHandle
543 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
544 LOADED_IMAGE_PRIVATE_DATA
*Image
;
546 Status
= CoreHandleProtocol (
548 &gEfiLoadedImageProtocolGuid
,
549 (VOID
**)&LoadedImage
551 if (!EFI_ERROR (Status
)) {
552 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
554 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
563 Unloads EFI image from memory.
565 @param Image EFI image
566 @param FreePage Free allocated pages
570 CoreUnloadAndCloseImage (
571 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
577 EFI_HANDLE
*HandleBuffer
;
579 EFI_GUID
**ProtocolGuidArray
;
582 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
586 if (Image
->Ebc
!= NULL
) {
588 // If EBC protocol exists we must perform cleanups for this image.
590 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
594 // Unload image, free Image->ImageContext->ModHandle
596 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
599 // Free our references to the image handle
601 if (Image
->Handle
!= NULL
) {
603 Status
= CoreLocateHandleBuffer (
610 if (!EFI_ERROR (Status
)) {
611 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
612 Status
= CoreProtocolsPerHandle (
613 HandleBuffer
[HandleIndex
],
617 if (!EFI_ERROR (Status
)) {
618 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
619 Status
= CoreOpenProtocolInformation (
620 HandleBuffer
[HandleIndex
],
621 ProtocolGuidArray
[ProtocolIndex
],
625 if (!EFI_ERROR (Status
)) {
626 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
627 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
628 Status
= CoreCloseProtocol (
629 HandleBuffer
[HandleIndex
],
630 ProtocolGuidArray
[ProtocolIndex
],
632 OpenInfo
[OpenInfoIndex
].ControllerHandle
636 if (OpenInfo
!= NULL
) {
637 CoreFreePool(OpenInfo
);
641 if (ProtocolGuidArray
!= NULL
) {
642 CoreFreePool(ProtocolGuidArray
);
646 if (HandleBuffer
!= NULL
) {
647 CoreFreePool (HandleBuffer
);
651 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
653 Status
= CoreUninstallProtocolInterface (
655 &gEfiLoadedImageDevicePathProtocolGuid
,
656 Image
->LoadedImageDevicePath
659 Status
= CoreUninstallProtocolInterface (
661 &gEfiLoadedImageProtocolGuid
,
667 if (Image
->RuntimeData
!= NULL
) {
668 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
670 // Remove the Image from the Runtime Image list as we are about to Free it!
672 RemoveEntryList (&Image
->RuntimeData
->Link
);
674 CoreFreePool (Image
->RuntimeData
);
678 // Free the Image from memory
680 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
681 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
685 // Done with the Image structure
687 if (Image
->Info
.FilePath
!= NULL
) {
688 CoreFreePool (Image
->Info
.FilePath
);
691 if (Image
->LoadedImageDevicePath
!= NULL
) {
692 CoreFreePool (Image
->LoadedImageDevicePath
);
695 if (Image
->FixupData
!= NULL
) {
696 CoreFreePool (Image
->FixupData
);
699 CoreFreePool (Image
);
704 Loads an EFI image into memory and returns a handle to the image.
706 @param BootPolicy If TRUE, indicates that the request originates
707 from the boot manager, and that the boot
708 manager is attempting to load FilePath as a
710 @param ParentImageHandle The caller's image handle.
711 @param FilePath The specific file path from which the image is
713 @param SourceBuffer If not NULL, a pointer to the memory location
714 containing a copy of the image to be loaded.
715 @param SourceSize The size in bytes of SourceBuffer.
716 @param DstBuffer The buffer to store the image
717 @param NumberOfPages If not NULL, it inputs a pointer to the page
718 number of DstBuffer and outputs a pointer to
719 the page number of the image. If this number is
720 not enough, return EFI_BUFFER_TOO_SMALL and
721 this parameter contains the required number.
722 @param ImageHandle Pointer to the returned image handle that is
723 created when the image is successfully loaded.
724 @param EntryPoint A pointer to the entry point
725 @param Attribute The bit mask of attributes to set for the load
728 @retval EFI_SUCCESS The image was loaded into memory.
729 @retval EFI_NOT_FOUND The FilePath was not found.
730 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
731 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
732 @retval EFI_UNSUPPORTED The image type is not supported, or the device
733 path cannot be parsed to locate the proper
734 protocol for loading the file.
735 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
740 CoreLoadImageCommon (
741 IN BOOLEAN BootPolicy
,
742 IN EFI_HANDLE ParentImageHandle
,
743 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
744 IN VOID
*SourceBuffer OPTIONAL
,
746 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
747 IN OUT UINTN
*NumberOfPages OPTIONAL
,
748 OUT EFI_HANDLE
*ImageHandle
,
749 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
753 LOADED_IMAGE_PRIVATE_DATA
*Image
;
754 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
755 IMAGE_FILE_HANDLE FHand
;
757 EFI_STATUS SecurityStatus
;
758 EFI_HANDLE DeviceHandle
;
759 UINT32 AuthenticationStatus
;
760 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
761 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
764 SecurityStatus
= EFI_SUCCESS
;
766 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
770 // The caller must pass in a valid ParentImageHandle
772 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
773 return EFI_INVALID_PARAMETER
;
776 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
777 if (ParentImage
== NULL
) {
778 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
779 return EFI_INVALID_PARAMETER
;
783 // Get simple read access to the source file
785 OriginalFilePath
= FilePath
;
786 Status
= CoreOpenImageFile (
793 &AuthenticationStatus
795 if (Status
== EFI_ALREADY_STARTED
) {
798 } else if (EFI_ERROR (Status
)) {
803 // Verify the Authentication Status through the Security Architectural Protocol
805 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
806 SecurityStatus
= gSecurity
->FileAuthenticationState (
808 AuthenticationStatus
,
811 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
812 Status
= SecurityStatus
;
820 // Allocate a new image structure
822 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
824 return EFI_OUT_OF_RESOURCES
;
828 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
830 FilePath
= OriginalFilePath
;
831 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
832 if (!EFI_ERROR (Status
)) {
833 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
834 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
838 // Initialize the fields for an internal driver
840 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
841 Image
->Info
.SystemTable
= gDxeCoreST
;
842 Image
->Info
.DeviceHandle
= DeviceHandle
;
843 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
844 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
845 Image
->Info
.ParentHandle
= ParentImageHandle
;
848 if (NumberOfPages
!= NULL
) {
849 Image
->NumberOfPages
= *NumberOfPages
;
851 Image
->NumberOfPages
= 0 ;
855 // Install the protocol interfaces for this image
856 // don't fire notifications yet
858 Status
= CoreInstallProtocolInterfaceNotify (
860 &gEfiLoadedImageProtocolGuid
,
861 EFI_NATIVE_INTERFACE
,
865 if (EFI_ERROR (Status
)) {
870 // Load the image. If EntryPoint is Null, it will not be set.
872 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
873 if (EFI_ERROR (Status
)) {
874 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
875 if (NumberOfPages
!= NULL
) {
876 *NumberOfPages
= Image
->NumberOfPages
;
882 if (NumberOfPages
!= NULL
) {
883 *NumberOfPages
= Image
->NumberOfPages
;
887 // Register the image in the Debug Image Info Table if the attribute is set
889 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
890 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
894 //Reinstall loaded image protocol to fire any notifications
896 Status
= CoreReinstallProtocolInterface (
898 &gEfiLoadedImageProtocolGuid
,
902 if (EFI_ERROR (Status
)) {
907 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
908 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
910 if (OriginalFilePath
!= NULL
) {
911 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
915 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
917 Status
= CoreInstallProtocolInterface (
919 &gEfiLoadedImageDevicePathProtocolGuid
,
920 EFI_NATIVE_INTERFACE
,
921 Image
->LoadedImageDevicePath
923 if (EFI_ERROR (Status
)) {
928 // Success. Return the image handle
930 *ImageHandle
= Image
->Handle
;
934 // All done accessing the source file
935 // If we allocated the Source buffer, free it
937 if (FHand
.FreeBuffer
) {
938 CoreFreePool (FHand
.Source
);
942 // There was an error. If there's an Image structure, free it
944 if (EFI_ERROR (Status
)) {
946 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
949 } else if (EFI_ERROR (SecurityStatus
)) {
950 Status
= SecurityStatus
;
960 Loads an EFI image into memory and returns a handle to the image.
962 @param BootPolicy If TRUE, indicates that the request originates
963 from the boot manager, and that the boot
964 manager is attempting to load FilePath as a
966 @param ParentImageHandle The caller's image handle.
967 @param FilePath The specific file path from which the image is
969 @param SourceBuffer If not NULL, a pointer to the memory location
970 containing a copy of the image to be loaded.
971 @param SourceSize The size in bytes of SourceBuffer.
972 @param ImageHandle Pointer to the returned image handle that is
973 created when the image is successfully loaded.
975 @retval EFI_SUCCESS The image was loaded into memory.
976 @retval EFI_NOT_FOUND The FilePath was not found.
977 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
978 @retval EFI_UNSUPPORTED The image type is not supported, or the device
979 path cannot be parsed to locate the proper
980 protocol for loading the file.
981 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
988 IN BOOLEAN BootPolicy
,
989 IN EFI_HANDLE ParentImageHandle
,
990 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
991 IN VOID
*SourceBuffer OPTIONAL
,
993 OUT EFI_HANDLE
*ImageHandle
998 PERF_START (NULL
, "LoadImage", NULL
, 0);
1000 Status
= CoreLoadImageCommon (
1006 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1010 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1013 PERF_END (NULL
, "LoadImage", NULL
, 0);
1021 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1023 @param This Calling context
1024 @param ParentImageHandle The caller's image handle.
1025 @param FilePath The specific file path from which the image is
1027 @param SourceBuffer If not NULL, a pointer to the memory location
1028 containing a copy of the image to be loaded.
1029 @param SourceSize The size in bytes of SourceBuffer.
1030 @param DstBuffer The buffer to store the image.
1031 @param NumberOfPages For input, specifies the space size of the
1032 image by caller if not NULL. For output,
1033 specifies the actual space size needed.
1034 @param ImageHandle Image handle for output.
1035 @param EntryPoint Image entry point for output.
1036 @param Attribute The bit mask of attributes to set for the load
1039 @retval EFI_SUCCESS The image was loaded into memory.
1040 @retval EFI_NOT_FOUND The FilePath was not found.
1041 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1042 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1043 path cannot be parsed to locate the proper
1044 protocol for loading the file.
1045 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1052 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1053 IN EFI_HANDLE ParentImageHandle
,
1054 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1055 IN VOID
*SourceBuffer OPTIONAL
,
1056 IN UINTN SourceSize
,
1057 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1058 OUT UINTN
*NumberOfPages OPTIONAL
,
1059 OUT EFI_HANDLE
*ImageHandle
,
1060 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1064 return CoreLoadImageCommon (
1080 Transfer control to a loaded image's entry point.
1082 @param ImageHandle Handle of image to be started.
1083 @param ExitDataSize Pointer of the size to ExitData
1084 @param ExitData Pointer to a pointer to a data buffer that
1085 includes a Null-terminated Unicode string,
1086 optionally followed by additional binary data.
1087 The string is a description that the caller may
1088 use to further indicate the reason for the
1091 @retval EFI_INVALID_PARAMETER Invalid parameter
1092 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1093 @retval EFI_SUCCESS Successfully transfer control to the image's
1100 IN EFI_HANDLE ImageHandle
,
1101 OUT UINTN
*ExitDataSize
,
1102 OUT CHAR16
**ExitData OPTIONAL
1106 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1107 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1108 UINT64 HandleDatabaseKey
;
1111 Image
= CoreLoadedImageInfo (ImageHandle
);
1112 if (Image
== NULL
|| Image
->Started
) {
1113 return EFI_INVALID_PARAMETER
;
1117 // Don't profile Objects or invalid start requests
1119 PERF_START (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1123 // Push the current start image context, and
1124 // link the current image to the head. This is the
1125 // only image that can call Exit()
1127 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1128 LastImage
= mCurrentImage
;
1129 mCurrentImage
= Image
;
1130 Image
->Tpl
= gEfiCurrentTpl
;
1133 // Set long jump for Exit() support
1134 // JumpContext must be aligned on a CPU specific boundary.
1135 // Overallocate the buffer and force the required alignment
1137 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1138 if (Image
->JumpBuffer
== NULL
) {
1139 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1140 return EFI_OUT_OF_RESOURCES
;
1142 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1144 SetJumpFlag
= SetJump (Image
->JumpContext
);
1146 // The initial call to SetJump() must always return 0.
1147 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1149 if (SetJumpFlag
== 0) {
1151 // Call the image's entry point
1153 Image
->Started
= TRUE
;
1154 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1157 // Add some debug information if the image returned with error.
1158 // This make the user aware and check if the driver image have already released
1159 // all the resource in this situation.
1161 DEBUG_CODE_BEGIN ();
1162 if (EFI_ERROR (Image
->Status
)) {
1163 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1168 // If the image returns, exit it through Exit()
1170 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1174 // Image has completed. Verify the tpl is the same
1176 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1177 CoreRestoreTpl (Image
->Tpl
);
1179 CoreFreePool (Image
->JumpBuffer
);
1182 // Pop the current start image context
1184 mCurrentImage
= LastImage
;
1187 // Go connect any handles that were created or modified while the image executed.
1189 CoreConnectHandlesByKey (HandleDatabaseKey
);
1192 // Handle the image's returned ExitData
1194 DEBUG_CODE_BEGIN ();
1195 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1197 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %x", Image
->ExitDataSize
, Image
->ExitData
));
1198 if (Image
->ExitData
!= NULL
) {
1199 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1201 DEBUG ((DEBUG_LOAD
, "\n"));
1206 // Return the exit data to the caller
1208 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1209 *ExitDataSize
= Image
->ExitDataSize
;
1210 *ExitData
= Image
->ExitData
;
1213 // Caller doesn't want the exit data, free it
1215 CoreFreePool (Image
->ExitData
);
1216 Image
->ExitData
= NULL
;
1220 // Save the Status because Image will get destroyed if it is unloaded.
1222 Status
= Image
->Status
;
1225 // If the image returned an error, or if the image is an application
1228 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1229 CoreUnloadAndCloseImage (Image
, TRUE
);
1235 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1240 Terminates the currently loaded EFI image and returns control to boot services.
1242 @param ImageHandle Handle that identifies the image. This
1243 parameter is passed to the image on entry.
1244 @param Status The image's exit code.
1245 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1246 ExitStatus is EFI_SUCCESS.
1247 @param ExitData Pointer to a data buffer that includes a
1248 Null-terminated Unicode string, optionally
1249 followed by additional binary data. The string
1250 is a description that the caller may use to
1251 further indicate the reason for the image's
1254 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1256 @retval EFI_SUCCESS Successfully terminates the currently loaded
1258 @retval EFI_ACCESS_DENIED Should never reach there.
1259 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1265 IN EFI_HANDLE ImageHandle
,
1266 IN EFI_STATUS Status
,
1267 IN UINTN ExitDataSize
,
1268 IN CHAR16
*ExitData OPTIONAL
1271 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1275 // Prevent possible reentrance to this function
1276 // for the same ImageHandle
1278 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1280 Image
= CoreLoadedImageInfo (ImageHandle
);
1281 if (Image
== NULL
) {
1282 Status
= EFI_INVALID_PARAMETER
;
1286 if (!Image
->Started
) {
1288 // The image has not been started so just free its resources
1290 CoreUnloadAndCloseImage (Image
, TRUE
);
1291 Status
= EFI_SUCCESS
;
1296 // Image has been started, verify this image can exit
1298 if (Image
!= mCurrentImage
) {
1299 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1300 Status
= EFI_INVALID_PARAMETER
;
1307 Image
->Status
= Status
;
1310 // If there's ExitData info, move it
1312 if (ExitData
!= NULL
) {
1313 Image
->ExitDataSize
= ExitDataSize
;
1314 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1315 if (Image
->ExitData
== NULL
) {
1316 Status
= EFI_OUT_OF_RESOURCES
;
1319 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1322 CoreRestoreTpl (OldTpl
);
1324 // return to StartImage
1326 LongJump (Image
->JumpContext
, (UINTN
)-1);
1329 // If we return from LongJump, then it is an error
1332 Status
= EFI_ACCESS_DENIED
;
1334 CoreRestoreTpl (OldTpl
);
1344 @param ImageHandle Handle that identifies the image to be
1347 @retval EFI_SUCCESS The image has been unloaded.
1348 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1350 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1356 IN EFI_HANDLE ImageHandle
1360 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1362 Image
= CoreLoadedImageInfo (ImageHandle
);
1363 if (Image
== NULL
) {
1365 // The image handle is not valid
1367 Status
= EFI_INVALID_PARAMETER
;
1371 if (Image
->Started
) {
1373 // The image has been started, request it to unload.
1375 Status
= EFI_UNSUPPORTED
;
1376 if (Image
->Info
.Unload
!= NULL
) {
1377 Status
= Image
->Info
.Unload (ImageHandle
);
1382 // This Image hasn't been started, thus it can be unloaded
1384 Status
= EFI_SUCCESS
;
1388 if (!EFI_ERROR (Status
)) {
1390 // if the Image was not started or Unloaded O.K. then clean up
1392 CoreUnloadAndCloseImage (Image
, TRUE
);
1402 Unload the specified image.
1404 @param This Indicates the calling context.
1405 @param ImageHandle The specified image handle.
1407 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1408 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1409 @retval EFI_SUCCESS Image successfully unloaded.
1415 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1416 IN EFI_HANDLE ImageHandle
1419 return CoreUnloadImage (ImageHandle
);