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 SPIN_LOCK mUnloadImageLock
;
22 LOADED_IMAGE_PRIVATE_DATA
*mCurrentImage
= NULL
;
24 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData
= {
25 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE
,
35 // This code is needed to build the Image handle for the DXE Core
37 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage
= {
38 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
, // Signature
40 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
, // Image type
41 TRUE
, // If entrypoint has been called
44 EFI_LOADED_IMAGE_INFORMATION_REVISION
, // Revision
45 NULL
, // Parent handle
46 NULL
, // System handle
48 NULL
, // Device handle
57 EfiBootServicesCode
, // ImageCodeType
58 EfiBootServicesData
// ImageDataType
60 (EFI_PHYSICAL_ADDRESS
)0, // ImageBasePage
64 EFI_SUCCESS
, // Status
72 NULL
// LoadedImageDevicePath
78 Add the Image Services to EFI Boot Services Table and install the protocol
79 interfaces for this image.
81 @param HobStart The HOB to initialize
87 CoreInitializeImageServices (
92 LOADED_IMAGE_PRIVATE_DATA
*Image
;
93 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
94 UINT64 DxeCoreImageLength
;
95 VOID
*DxeCoreEntryPoint
;
96 EFI_PEI_HOB_POINTERS DxeCoreHob
;
98 // Searching for image hob
100 DxeCoreHob
.Raw
= HobStart
;
101 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
102 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
108 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
110 ASSERT (DxeCoreHob
.Raw
!= NULL
);
112 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
113 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
114 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
115 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
117 // Initialize the fields for an internal driver
119 Image
= &mCorePrivateImage
;
121 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
122 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
123 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
124 Image
->Tpl
= gEfiCurrentTpl
;
125 Image
->Info
.SystemTable
= gDxeCoreST
;
126 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
127 Image
->Info
.ImageSize
= DxeCoreImageLength
;
130 // Install the protocol interfaces for this image
132 Status
= CoreInstallProtocolInterface (
134 &gEfiLoadedImageProtocolGuid
,
135 EFI_NATIVE_INTERFACE
,
138 ASSERT_EFI_ERROR (Status
);
140 mCurrentImage
= Image
;
143 // Initialize spin lock
145 InitializeSpinLock (&mUnloadImageLock
);
148 // Fill in DXE globals
150 gDxeCoreImageHandle
= Image
->Handle
;
151 gDxeCoreLoadedImage
= &Image
->Info
;
154 // Export DXE Core PE Loader functionality
156 return CoreInstallProtocolInterface (
157 &mLoadPe32PrivateData
.Handle
,
158 &gEfiLoadPeImageProtocolGuid
,
159 EFI_NATIVE_INTERFACE
,
160 &mLoadPe32PrivateData
.Pe32Image
166 Loads, relocates, and invokes a PE/COFF image
168 @param BootPolicy If TRUE, indicates that the request originates
169 from the boot manager, and that the boot
170 manager is attempting to load FilePath as a
172 @param Pe32Handle The handle of PE32 image
173 @param Image PE image to be loaded
174 @param DstBuffer The buffer to store the image
175 @param EntryPoint A pointer to the entry point
176 @param Attribute The bit mask of attributes to set for the load
179 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
180 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
181 relocate the PE/COFF file
182 @retval EFI_INVALID_PARAMETER Invalid parameter
183 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
188 IN BOOLEAN BootPolicy
,
190 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
191 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
192 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
197 BOOLEAN DstBufAlocated
;
200 EFI_TCG_PLATFORM_PROTOCOL
*TcgPlatformProtocol
;
201 IMAGE_FILE_HANDLE
*FHandle
;
204 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
206 Image
->ImageContext
.Handle
= Pe32Handle
;
207 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
210 // Get information about the image being loaded
212 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
213 if (EFI_ERROR (Status
)) {
217 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
218 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
220 // The PE/COFF loader can support loading image types that can be executed.
221 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
223 return EFI_UNSUPPORTED
;
228 // Set EFI memory type based on ImageType
230 switch (Image
->ImageContext
.ImageType
) {
231 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
232 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
233 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
235 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
236 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
237 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
239 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
240 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
241 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
242 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
245 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
246 return EFI_UNSUPPORTED
;
249 // Get the image base address in the original PeImage.
251 LinkTimeBase
= (UINTN
) Image
->ImageContext
.ImageAddress
;
254 // Allocate memory of the correct memory type aligned on the required image boundry
256 DstBufAlocated
= FALSE
;
257 if (DstBuffer
== 0) {
259 // Allocate Destination Buffer as caller did not pass it in
262 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
263 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
265 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
268 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
271 // If the image relocations have not been stripped, then load at any address.
272 // Otherwise load at the address at which it was linked.
274 // Memory below 1MB should be treated reserved for CSM and there should be
275 // no modules whose preferred load addresses are below 1MB.
277 Status
= EFI_OUT_OF_RESOURCES
;
278 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
279 Status
= CoreAllocatePages (
281 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
282 Image
->NumberOfPages
,
283 &Image
->ImageContext
.ImageAddress
286 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
287 Status
= CoreAllocatePages (
289 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
290 Image
->NumberOfPages
,
291 &Image
->ImageContext
.ImageAddress
294 if (EFI_ERROR (Status
)) {
297 DstBufAlocated
= TRUE
;
300 // Caller provided the destination buffer
303 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
305 // If the image relocations were stripped, and the caller provided a
306 // destination buffer address that does not match the address that the
307 // image is linked at, then the image cannot be loaded.
309 return EFI_INVALID_PARAMETER
;
312 if (Image
->NumberOfPages
!= 0 &&
313 Image
->NumberOfPages
<
314 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
315 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
316 return EFI_BUFFER_TOO_SMALL
;
319 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
320 Image
->ImageContext
.ImageAddress
= DstBuffer
;
323 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
324 Image
->ImageContext
.ImageAddress
=
325 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
326 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
329 // Load the image from the file into the allocated memory
331 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
332 if (EFI_ERROR (Status
)) {
337 // If this is a Runtime Driver, then allocate memory for the FixupData that
338 // is used to relocate the image when SetVirtualAddressMap() is called. The
339 // relocation is done by the Runtime AP.
341 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
342 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
343 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
344 if (Image
->ImageContext
.FixupData
== NULL
) {
345 Status
= EFI_OUT_OF_RESOURCES
;
352 // Measure the image before applying fixup
354 Status
= CoreLocateProtocol (
355 &gEfiTcgPlatformProtocolGuid
,
357 (VOID
**) &TcgPlatformProtocol
359 if (!EFI_ERROR (Status
)) {
360 FHandle
= (IMAGE_FILE_HANDLE
*) Image
->ImageContext
.Handle
;
361 Status
= TcgPlatformProtocol
->MeasurePeImage (
363 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FHandle
->Source
,
366 Image
->ImageContext
.ImageType
,
367 Image
->Info
.DeviceHandle
,
371 ASSERT_EFI_ERROR (Status
);
375 // Relocate the image in memory
377 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
378 if (EFI_ERROR (Status
)) {
383 // Flush the Instruction Cache
385 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
388 // Copy the machine type from the context to the image private data. This
389 // is needed during image unload to know if we should call an EBC protocol
390 // to unload the image.
392 Image
->Machine
= Image
->ImageContext
.Machine
;
395 // Get the image entry point. If it's an EBC image, then call into the
396 // interpreter to create a thunk for the entry point and use the returned
397 // value for the entry point.
399 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
400 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
402 // Locate the EBC interpreter protocol
404 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
405 if (EFI_ERROR(Status
)) {
406 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
411 // Register a callback for flushing the instruction cache so that created
412 // thunks can be flushed.
414 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
415 if (EFI_ERROR(Status
)) {
420 // Create a thunk for the image's entry point. This will be the new
421 // entry point for the image.
423 Status
= Image
->Ebc
->CreateThunk (
426 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
427 (VOID
**) &Image
->EntryPoint
429 if (EFI_ERROR(Status
)) {
435 // Fill in the image information for the Loaded Image Protocol
437 Image
->Type
= Image
->ImageContext
.ImageType
;
438 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
439 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
440 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
441 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
442 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
443 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
445 // Make a list off all the RT images so we can let the RT AP know about them.
447 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
448 if (Image
->RuntimeData
== NULL
) {
451 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
452 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
453 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
454 Image
->RuntimeData
->Handle
= Image
->Handle
;
455 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
460 // Fill in the entry point of the image if it is available
462 if (EntryPoint
!= NULL
) {
463 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
467 // Print the load address and the PDB file name if it is available
474 CHAR8 EfiFileName
[256];
477 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
478 "Loading driver at 0x%11p EntryPoint=0x%11p ",
479 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
480 FUNCTION_ENTRY_POINT ((UINTN
) Image
->ImageContext
.EntryPoint
)));
484 // Print Module Name by Pdb file path
486 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
488 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
489 if (Image
->ImageContext
.PdbPointer
[Index
] == '\\') {
490 StartIndex
= Index
+ 1;
494 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
496 for (Index
= 0; Index
< sizeof (EfiFileName
); Index
++) {
497 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
498 if (EfiFileName
[Index
] == 0) {
499 EfiFileName
[Index
] = '.';
501 if (EfiFileName
[Index
] == '.') {
502 EfiFileName
[Index
+ 1] = 'e';
503 EfiFileName
[Index
+ 2] = 'f';
504 EfiFileName
[Index
+ 3] = 'i';
505 EfiFileName
[Index
+ 4] = 0;
509 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
511 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
523 if (DstBufAlocated
) {
524 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
527 if (Image
->ImageContext
.FixupData
!= NULL
) {
528 CoreFreePool (Image
->ImageContext
.FixupData
);
537 Get the image's private data from its handle.
539 @param ImageHandle The image handle
541 @return Return the image private data associated with ImageHandle.
544 LOADED_IMAGE_PRIVATE_DATA
*
545 CoreLoadedImageInfo (
546 IN EFI_HANDLE ImageHandle
550 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
551 LOADED_IMAGE_PRIVATE_DATA
*Image
;
553 Status
= CoreHandleProtocol (
555 &gEfiLoadedImageProtocolGuid
,
556 (VOID
**)&LoadedImage
558 if (!EFI_ERROR (Status
)) {
559 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
561 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
570 Unloads EFI image from memory.
572 @param Image EFI image
573 @param FreePage Free allocated pages
577 CoreUnloadAndCloseImage (
578 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
584 EFI_HANDLE
*HandleBuffer
;
586 EFI_GUID
**ProtocolGuidArray
;
589 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
593 if (Image
->Ebc
!= NULL
) {
595 // If EBC protocol exists we must perform cleanups for this image.
597 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
601 // Unload image, free Image->ImageContext->ModHandle
603 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
606 // Free our references to the image handle
608 if (Image
->Handle
!= NULL
) {
610 Status
= CoreLocateHandleBuffer (
617 if (!EFI_ERROR (Status
)) {
618 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
619 Status
= CoreProtocolsPerHandle (
620 HandleBuffer
[HandleIndex
],
624 if (!EFI_ERROR (Status
)) {
625 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
626 Status
= CoreOpenProtocolInformation (
627 HandleBuffer
[HandleIndex
],
628 ProtocolGuidArray
[ProtocolIndex
],
632 if (!EFI_ERROR (Status
)) {
633 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
634 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
635 Status
= CoreCloseProtocol (
636 HandleBuffer
[HandleIndex
],
637 ProtocolGuidArray
[ProtocolIndex
],
639 OpenInfo
[OpenInfoIndex
].ControllerHandle
643 if (OpenInfo
!= NULL
) {
644 CoreFreePool(OpenInfo
);
648 if (ProtocolGuidArray
!= NULL
) {
649 CoreFreePool(ProtocolGuidArray
);
653 if (HandleBuffer
!= NULL
) {
654 CoreFreePool (HandleBuffer
);
658 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
660 Status
= CoreUninstallProtocolInterface (
662 &gEfiLoadedImageDevicePathProtocolGuid
,
663 Image
->LoadedImageDevicePath
666 Status
= CoreUninstallProtocolInterface (
668 &gEfiLoadedImageProtocolGuid
,
674 if (Image
->RuntimeData
!= NULL
) {
675 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
677 // Remove the Image from the Runtime Image list as we are about to Free it!
679 RemoveEntryList (&Image
->RuntimeData
->Link
);
681 CoreFreePool (Image
->RuntimeData
);
685 // Free the Image from memory
687 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
688 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
692 // Done with the Image structure
694 if (Image
->Info
.FilePath
!= NULL
) {
695 CoreFreePool (Image
->Info
.FilePath
);
698 if (Image
->LoadedImageDevicePath
!= NULL
) {
699 CoreFreePool (Image
->LoadedImageDevicePath
);
702 if (Image
->FixupData
!= NULL
) {
703 CoreFreePool (Image
->FixupData
);
706 CoreFreePool (Image
);
711 Loads an EFI image into memory and returns a handle to the image.
713 @param BootPolicy If TRUE, indicates that the request originates
714 from the boot manager, and that the boot
715 manager is attempting to load FilePath as a
717 @param ParentImageHandle The caller's image handle.
718 @param FilePath The specific file path from which the image is
720 @param SourceBuffer If not NULL, a pointer to the memory location
721 containing a copy of the image to be loaded.
722 @param SourceSize The size in bytes of SourceBuffer.
723 @param DstBuffer The buffer to store the image
724 @param NumberOfPages If not NULL, it inputs a pointer to the page
725 number of DstBuffer and outputs a pointer to
726 the page number of the image. If this number is
727 not enough, return EFI_BUFFER_TOO_SMALL and
728 this parameter contains the required number.
729 @param ImageHandle Pointer to the returned image handle that is
730 created when the image is successfully loaded.
731 @param EntryPoint A pointer to the entry point
732 @param Attribute The bit mask of attributes to set for the load
735 @retval EFI_SUCCESS The image was loaded into memory.
736 @retval EFI_NOT_FOUND The FilePath was not found.
737 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
738 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
739 @retval EFI_UNSUPPORTED The image type is not supported, or the device
740 path cannot be parsed to locate the proper
741 protocol for loading the file.
742 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
747 CoreLoadImageCommon (
748 IN BOOLEAN BootPolicy
,
749 IN EFI_HANDLE ParentImageHandle
,
750 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
751 IN VOID
*SourceBuffer OPTIONAL
,
753 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
754 IN OUT UINTN
*NumberOfPages OPTIONAL
,
755 OUT EFI_HANDLE
*ImageHandle
,
756 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
760 LOADED_IMAGE_PRIVATE_DATA
*Image
;
761 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
762 IMAGE_FILE_HANDLE FHand
;
764 EFI_STATUS SecurityStatus
;
765 EFI_HANDLE DeviceHandle
;
766 UINT32 AuthenticationStatus
;
767 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
768 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
771 SecurityStatus
= EFI_SUCCESS
;
773 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
777 // The caller must pass in a valid ParentImageHandle
779 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
780 return EFI_INVALID_PARAMETER
;
783 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
784 if (ParentImage
== NULL
) {
785 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
786 return EFI_INVALID_PARAMETER
;
790 // Get simple read access to the source file
792 OriginalFilePath
= FilePath
;
793 Status
= CoreOpenImageFile (
800 &AuthenticationStatus
802 if (Status
== EFI_ALREADY_STARTED
) {
805 } else if (EFI_ERROR (Status
)) {
810 // Verify the Authentication Status through the Security Architectural Protocol
812 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
813 SecurityStatus
= gSecurity
->FileAuthenticationState (
815 AuthenticationStatus
,
818 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
819 Status
= SecurityStatus
;
827 // Allocate a new image structure
829 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
831 return EFI_OUT_OF_RESOURCES
;
835 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
837 FilePath
= OriginalFilePath
;
838 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
839 if (!EFI_ERROR (Status
)) {
840 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
841 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
845 // Initialize the fields for an internal driver
847 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
848 Image
->Info
.SystemTable
= gDxeCoreST
;
849 Image
->Info
.DeviceHandle
= DeviceHandle
;
850 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
851 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
852 Image
->Info
.ParentHandle
= ParentImageHandle
;
855 if (NumberOfPages
!= NULL
) {
856 Image
->NumberOfPages
= *NumberOfPages
;
858 Image
->NumberOfPages
= 0 ;
862 // Install the protocol interfaces for this image
863 // don't fire notifications yet
865 Status
= CoreInstallProtocolInterfaceNotify (
867 &gEfiLoadedImageProtocolGuid
,
868 EFI_NATIVE_INTERFACE
,
872 if (EFI_ERROR (Status
)) {
877 // Load the image. If EntryPoint is Null, it will not be set.
879 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
880 if (EFI_ERROR (Status
)) {
881 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
882 if (NumberOfPages
!= NULL
) {
883 *NumberOfPages
= Image
->NumberOfPages
;
889 if (NumberOfPages
!= NULL
) {
890 *NumberOfPages
= Image
->NumberOfPages
;
894 // Register the image in the Debug Image Info Table if the attribute is set
896 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
897 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
901 //Reinstall loaded image protocol to fire any notifications
903 Status
= CoreReinstallProtocolInterface (
905 &gEfiLoadedImageProtocolGuid
,
909 if (EFI_ERROR (Status
)) {
914 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
915 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
917 if (OriginalFilePath
!= NULL
) {
918 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
922 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
924 Status
= CoreInstallProtocolInterface (
926 &gEfiLoadedImageDevicePathProtocolGuid
,
927 EFI_NATIVE_INTERFACE
,
928 Image
->LoadedImageDevicePath
930 if (EFI_ERROR (Status
)) {
935 // Success. Return the image handle
937 *ImageHandle
= Image
->Handle
;
941 // All done accessing the source file
942 // If we allocated the Source buffer, free it
944 if (FHand
.FreeBuffer
) {
945 CoreFreePool (FHand
.Source
);
949 // There was an error. If there's an Image structure, free it
951 if (EFI_ERROR (Status
)) {
953 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
956 } else if (EFI_ERROR (SecurityStatus
)) {
957 Status
= SecurityStatus
;
967 Loads an EFI image into memory and returns a handle to the image.
969 @param BootPolicy If TRUE, indicates that the request originates
970 from the boot manager, and that the boot
971 manager is attempting to load FilePath as a
973 @param ParentImageHandle The caller's image handle.
974 @param FilePath The specific file path from which the image is
976 @param SourceBuffer If not NULL, a pointer to the memory location
977 containing a copy of the image to be loaded.
978 @param SourceSize The size in bytes of SourceBuffer.
979 @param ImageHandle Pointer to the returned image handle that is
980 created when the image is successfully loaded.
982 @retval EFI_SUCCESS The image was loaded into memory.
983 @retval EFI_NOT_FOUND The FilePath was not found.
984 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
985 @retval EFI_UNSUPPORTED The image type is not supported, or the device
986 path cannot be parsed to locate the proper
987 protocol for loading the file.
988 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
995 IN BOOLEAN BootPolicy
,
996 IN EFI_HANDLE ParentImageHandle
,
997 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
998 IN VOID
*SourceBuffer OPTIONAL
,
1000 OUT EFI_HANDLE
*ImageHandle
1005 PERF_START (NULL
, "LoadImage", NULL
, 0);
1007 Status
= CoreLoadImageCommon (
1013 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1017 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1020 PERF_END (NULL
, "LoadImage", NULL
, 0);
1028 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1030 @param This Calling context
1031 @param ParentImageHandle The caller's image handle.
1032 @param FilePath The specific file path from which the image is
1034 @param SourceBuffer If not NULL, a pointer to the memory location
1035 containing a copy of the image to be loaded.
1036 @param SourceSize The size in bytes of SourceBuffer.
1037 @param DstBuffer The buffer to store the image.
1038 @param NumberOfPages For input, specifies the space size of the
1039 image by caller if not NULL. For output,
1040 specifies the actual space size needed.
1041 @param ImageHandle Image handle for output.
1042 @param EntryPoint Image entry point for output.
1043 @param Attribute The bit mask of attributes to set for the load
1046 @retval EFI_SUCCESS The image was loaded into memory.
1047 @retval EFI_NOT_FOUND The FilePath was not found.
1048 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1049 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1050 path cannot be parsed to locate the proper
1051 protocol for loading the file.
1052 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1059 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1060 IN EFI_HANDLE ParentImageHandle
,
1061 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1062 IN VOID
*SourceBuffer OPTIONAL
,
1063 IN UINTN SourceSize
,
1064 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1065 OUT UINTN
*NumberOfPages OPTIONAL
,
1066 OUT EFI_HANDLE
*ImageHandle
,
1067 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1071 return CoreLoadImageCommon (
1087 Transfer control to a loaded image's entry point.
1089 @param ImageHandle Handle of image to be started.
1090 @param ExitDataSize Pointer of the size to ExitData
1091 @param ExitData Pointer to a pointer to a data buffer that
1092 includes a Null-terminated Unicode string,
1093 optionally followed by additional binary data.
1094 The string is a description that the caller may
1095 use to further indicate the reason for the
1098 @retval EFI_INVALID_PARAMETER Invalid parameter
1099 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1100 @retval EFI_SUCCESS Successfully transfer control to the image's
1107 IN EFI_HANDLE ImageHandle
,
1108 OUT UINTN
*ExitDataSize
,
1109 OUT CHAR16
**ExitData OPTIONAL
1113 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1114 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1115 UINT64 HandleDatabaseKey
;
1118 Image
= CoreLoadedImageInfo (ImageHandle
);
1119 if (Image
== NULL
|| Image
->Started
) {
1120 return EFI_INVALID_PARAMETER
;
1124 // Don't profile Objects or invalid start requests
1126 PERF_START (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1130 // Push the current start image context, and
1131 // link the current image to the head. This is the
1132 // only image that can call Exit()
1134 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1135 LastImage
= mCurrentImage
;
1136 mCurrentImage
= Image
;
1137 Image
->Tpl
= gEfiCurrentTpl
;
1140 // Set long jump for Exit() support
1141 // JumpContext must be aligned on a CPU specific boundary.
1142 // Overallocate the buffer and force the required alignment
1144 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1145 if (Image
->JumpBuffer
== NULL
) {
1146 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1147 return EFI_OUT_OF_RESOURCES
;
1149 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1151 SetJumpFlag
= SetJump (Image
->JumpContext
);
1153 // The initial call to SetJump() must always return 0.
1154 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1156 if (SetJumpFlag
== 0) {
1158 // Call the image's entry point
1160 Image
->Started
= TRUE
;
1161 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1164 // Add some debug information if the image returned with error.
1165 // This make the user aware and check if the driver image have already released
1166 // all the resource in this situation.
1168 DEBUG_CODE_BEGIN ();
1169 if (EFI_ERROR (Image
->Status
)) {
1170 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1175 // If the image returns, exit it through Exit()
1177 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1181 // Image has completed. Verify the tpl is the same
1183 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1184 CoreRestoreTpl (Image
->Tpl
);
1186 CoreFreePool (Image
->JumpBuffer
);
1189 // Pop the current start image context
1191 mCurrentImage
= LastImage
;
1194 // Go connect any handles that were created or modified while the image executed.
1196 CoreConnectHandlesByKey (HandleDatabaseKey
);
1199 // Handle the image's returned ExitData
1201 DEBUG_CODE_BEGIN ();
1202 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1204 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %x", Image
->ExitDataSize
, Image
->ExitData
));
1205 if (Image
->ExitData
!= NULL
) {
1206 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1208 DEBUG ((DEBUG_LOAD
, "\n"));
1213 // Return the exit data to the caller
1215 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1216 *ExitDataSize
= Image
->ExitDataSize
;
1217 *ExitData
= Image
->ExitData
;
1220 // Caller doesn't want the exit data, free it
1222 CoreFreePool (Image
->ExitData
);
1223 Image
->ExitData
= NULL
;
1227 // Save the Status because Image will get destroyed if it is unloaded.
1229 Status
= Image
->Status
;
1232 // If the image returned an error, or if the image is an application
1235 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1236 CoreUnloadAndCloseImage (Image
, TRUE
);
1242 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1247 Terminates the currently loaded EFI image and returns control to boot services.
1249 @param ImageHandle Handle that identifies the image. This
1250 parameter is passed to the image on entry.
1251 @param Status The image's exit code.
1252 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1253 ExitStatus is EFI_SUCCESS.
1254 @param ExitData Pointer to a data buffer that includes a
1255 Null-terminated Unicode string, optionally
1256 followed by additional binary data. The string
1257 is a description that the caller may use to
1258 further indicate the reason for the image's
1261 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1263 @retval EFI_SUCCESS Successfully terminates the currently loaded
1265 @retval EFI_ACCESS_DENIED Should never reach there.
1266 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1272 IN EFI_HANDLE ImageHandle
,
1273 IN EFI_STATUS Status
,
1274 IN UINTN ExitDataSize
,
1275 IN CHAR16
*ExitData OPTIONAL
1278 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1282 // Prevent possible reentrance to this function
1283 // for the same ImageHandle
1285 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1287 Image
= CoreLoadedImageInfo (ImageHandle
);
1288 if (Image
== NULL
) {
1289 Status
= EFI_INVALID_PARAMETER
;
1293 if (!Image
->Started
) {
1295 // The image has not been started so just free its resources
1297 CoreUnloadAndCloseImage (Image
, TRUE
);
1298 Status
= EFI_SUCCESS
;
1303 // Image has been started, verify this image can exit
1305 if (Image
!= mCurrentImage
) {
1306 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1307 Status
= EFI_INVALID_PARAMETER
;
1314 Image
->Status
= Status
;
1317 // If there's ExitData info, move it
1319 if (ExitData
!= NULL
) {
1320 Image
->ExitDataSize
= ExitDataSize
;
1321 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1322 if (Image
->ExitData
== NULL
) {
1323 Status
= EFI_OUT_OF_RESOURCES
;
1326 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1329 CoreRestoreTpl (OldTpl
);
1331 // return to StartImage
1333 LongJump (Image
->JumpContext
, (UINTN
)-1);
1336 // If we return from LongJump, then it is an error
1339 Status
= EFI_ACCESS_DENIED
;
1341 CoreRestoreTpl (OldTpl
);
1351 @param ImageHandle Handle that identifies the image to be
1354 @retval EFI_SUCCESS The image has been unloaded.
1355 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1357 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1363 IN EFI_HANDLE ImageHandle
1367 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1370 // Prevent possible reentrance to this function
1371 // for the same ImageHandle
1373 if (!AcquireSpinLockOrFail (&mUnloadImageLock
)) {
1374 return EFI_UNSUPPORTED
;
1377 Image
= CoreLoadedImageInfo (ImageHandle
);
1378 if (Image
== NULL
) {
1380 // The image handle is not valid
1382 Status
= EFI_INVALID_PARAMETER
;
1386 if (Image
->Started
) {
1388 // The image has been started, request it to unload.
1390 Status
= EFI_UNSUPPORTED
;
1391 if (Image
->Info
.Unload
!= NULL
) {
1392 Status
= Image
->Info
.Unload (ImageHandle
);
1397 // This Image hasn't been started, thus it can be unloaded
1399 Status
= EFI_SUCCESS
;
1403 if (!EFI_ERROR (Status
)) {
1405 // if the Image was not started or Unloaded O.K. then clean up
1407 CoreUnloadAndCloseImage (Image
, TRUE
);
1411 ReleaseSpinLock (&mUnloadImageLock
);
1418 Unload the specified image.
1420 @param This Indicates the calling context.
1421 @param ImageHandle The specified image handle.
1423 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1424 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1425 @retval EFI_SUCCESS Image successfully unloaded.
1431 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1432 IN EFI_HANDLE ImageHandle
1435 return CoreUnloadImage (ImageHandle
);