3 Core image handling services to load and unload PeImage.
5 Copyright (c) 2006 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 LOADED_IMAGE_PRIVATE_DATA
*mCurrentImage
= NULL
;
23 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData
= {
24 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE
,
34 // This code is needed to build the Image handle for the DXE Core
36 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage
= {
37 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
, // Signature
39 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
, // Image type
40 TRUE
, // If entrypoint has been called
43 EFI_LOADED_IMAGE_INFORMATION_REVISION
, // Revision
44 NULL
, // Parent handle
45 NULL
, // System handle
47 NULL
, // Device handle
56 EfiBootServicesCode
, // ImageCodeType
57 EfiBootServicesData
// ImageDataType
59 (EFI_PHYSICAL_ADDRESS
)0, // ImageBasePage
63 EFI_SUCCESS
, // Status
71 NULL
// LoadedImageDevicePath
77 Add the Image Services to EFI Boot Services Table and install the protocol
78 interfaces for this image.
80 @param HobStart The HOB to initialize
86 CoreInitializeImageServices (
91 LOADED_IMAGE_PRIVATE_DATA
*Image
;
92 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
93 UINT64 DxeCoreImageLength
;
94 VOID
*DxeCoreEntryPoint
;
95 EFI_PEI_HOB_POINTERS DxeCoreHob
;
97 // Searching for image hob
99 DxeCoreHob
.Raw
= HobStart
;
100 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
101 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
107 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
109 ASSERT (DxeCoreHob
.Raw
!= NULL
);
111 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
112 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
113 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
114 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
116 // Initialize the fields for an internal driver
118 Image
= &mCorePrivateImage
;
120 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
121 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
122 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
123 Image
->Tpl
= gEfiCurrentTpl
;
124 Image
->Info
.SystemTable
= gDxeCoreST
;
125 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
126 Image
->Info
.ImageSize
= DxeCoreImageLength
;
129 // Install the protocol interfaces for this image
131 Status
= CoreInstallProtocolInterface (
133 &gEfiLoadedImageProtocolGuid
,
134 EFI_NATIVE_INTERFACE
,
137 ASSERT_EFI_ERROR (Status
);
139 mCurrentImage
= Image
;
142 // Fill in DXE globals
144 gDxeCoreImageHandle
= Image
->Handle
;
145 gDxeCoreLoadedImage
= &Image
->Info
;
148 // Export DXE Core PE Loader functionality
150 return CoreInstallProtocolInterface (
151 &mLoadPe32PrivateData
.Handle
,
152 &gEfiLoadPeImageProtocolGuid
,
153 EFI_NATIVE_INTERFACE
,
154 &mLoadPe32PrivateData
.Pe32Image
160 Loads, relocates, and invokes a PE/COFF image
162 @param BootPolicy If TRUE, indicates that the request originates
163 from the boot manager, and that the boot
164 manager is attempting to load FilePath as a
166 @param Pe32Handle The handle of PE32 image
167 @param Image PE image to be loaded
168 @param DstBuffer The buffer to store the image
169 @param EntryPoint A pointer to the entry point
170 @param Attribute The bit mask of attributes to set for the load
173 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
174 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
175 relocate the PE/COFF file
176 @retval EFI_INVALID_PARAMETER Invalid parameter
177 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
182 IN BOOLEAN BootPolicy
,
184 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
185 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
186 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
191 BOOLEAN DstBufAlocated
;
194 EFI_TCG_PLATFORM_PROTOCOL
*TcgPlatformProtocol
;
195 IMAGE_FILE_HANDLE
*FHandle
;
198 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
200 Image
->ImageContext
.Handle
= Pe32Handle
;
201 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
204 // Get information about the image being loaded
206 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
207 if (EFI_ERROR (Status
)) {
211 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
212 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
214 // The PE/COFF loader can support loading image types that can be executed.
215 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
217 return EFI_UNSUPPORTED
;
222 // Set EFI memory type based on ImageType
224 switch (Image
->ImageContext
.ImageType
) {
225 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
226 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
227 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
229 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
230 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
231 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
233 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
234 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
235 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
236 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
239 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
240 return EFI_UNSUPPORTED
;
243 // Get the image base address in the original PeImage.
245 LinkTimeBase
= (UINTN
) Image
->ImageContext
.ImageAddress
;
248 // Allocate memory of the correct memory type aligned on the required image boundry
250 DstBufAlocated
= FALSE
;
251 if (DstBuffer
== 0) {
253 // Allocate Destination Buffer as caller did not pass it in
256 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
257 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
259 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
262 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
265 // If the image relocations have not been stripped, then load at any address.
266 // Otherwise load at the address at which it was linked.
268 // Memory below 1MB should be treated reserved for CSM and there should be
269 // no modules whose preferred load addresses are below 1MB.
271 Status
= EFI_OUT_OF_RESOURCES
;
272 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
273 Status
= CoreAllocatePages (
275 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
276 Image
->NumberOfPages
,
277 &Image
->ImageContext
.ImageAddress
280 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
281 Status
= CoreAllocatePages (
283 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
284 Image
->NumberOfPages
,
285 &Image
->ImageContext
.ImageAddress
288 if (EFI_ERROR (Status
)) {
291 DstBufAlocated
= TRUE
;
294 // Caller provided the destination buffer
297 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
299 // If the image relocations were stripped, and the caller provided a
300 // destination buffer address that does not match the address that the
301 // image is linked at, then the image cannot be loaded.
303 return EFI_INVALID_PARAMETER
;
306 if (Image
->NumberOfPages
!= 0 &&
307 Image
->NumberOfPages
<
308 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
309 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
310 return EFI_BUFFER_TOO_SMALL
;
313 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
314 Image
->ImageContext
.ImageAddress
= DstBuffer
;
317 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
318 Image
->ImageContext
.ImageAddress
=
319 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
320 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
323 // Load the image from the file into the allocated memory
325 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
326 if (EFI_ERROR (Status
)) {
331 // If this is a Runtime Driver, then allocate memory for the FixupData that
332 // is used to relocate the image when SetVirtualAddressMap() is called. The
333 // relocation is done by the Runtime AP.
335 if (Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) {
336 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
337 Image
->ImageContext
.FixupData
= CoreAllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
338 if (Image
->ImageContext
.FixupData
== NULL
) {
339 Status
= EFI_OUT_OF_RESOURCES
;
346 // Measure the image before applying fixup
348 Status
= CoreLocateProtocol (
349 &gEfiTcgPlatformProtocolGuid
,
351 (VOID
**) &TcgPlatformProtocol
353 if (!EFI_ERROR (Status
)) {
354 FHandle
= (IMAGE_FILE_HANDLE
*) Image
->ImageContext
.Handle
;
355 Status
= TcgPlatformProtocol
->MeasurePeImage (
357 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FHandle
->Source
,
360 Image
->ImageContext
.ImageType
,
361 Image
->Info
.DeviceHandle
,
365 ASSERT_EFI_ERROR (Status
);
369 // Relocate the image in memory
371 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
372 if (EFI_ERROR (Status
)) {
377 // Flush the Instruction Cache
379 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
382 // Copy the machine type from the context to the image private data. This
383 // is needed during image unload to know if we should call an EBC protocol
384 // to unload the image.
386 Image
->Machine
= Image
->ImageContext
.Machine
;
389 // Get the image entry point. If it's an EBC image, then call into the
390 // interpreter to create a thunk for the entry point and use the returned
391 // value for the entry point.
393 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
394 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
396 // Locate the EBC interpreter protocol
398 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
399 if (EFI_ERROR(Status
)) {
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
) {
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
= CoreAllocateRuntimePool (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];
469 if (Image
->ImageContext
.Machine
!= IMAGE_FILE_MACHINE_IA64
) {
470 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
471 "Loading driver at 0x%10p EntryPoint=0x%10p ",
472 (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
,
473 (VOID
*)(UINTN
)Image
->ImageContext
.EntryPoint
));
476 // For IPF Image, the real entry point should be print.
478 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
479 "Loading driver at 0x%10p EntryPoint=0x%10p ",
480 (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
,
481 (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)Image
->ImageContext
.EntryPoint
)));
485 // Print Module Name by Pdb file path
487 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
489 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
490 if (Image
->ImageContext
.PdbPointer
[Index
] == '\\') {
491 StartIndex
= Index
+ 1;
495 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
497 for (Index
= 0; Index
< sizeof (EfiFileName
); Index
++) {
498 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
499 if (EfiFileName
[Index
] == 0) {
500 EfiFileName
[Index
] = '.';
502 if (EfiFileName
[Index
] == '.') {
503 EfiFileName
[Index
+ 1] = 'e';
504 EfiFileName
[Index
+ 2] = 'f';
505 EfiFileName
[Index
+ 3] = 'i';
506 EfiFileName
[Index
+ 4] = 0;
510 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
512 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
524 if (DstBufAlocated
) {
525 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
528 if (Image
->ImageContext
.FixupData
!= NULL
) {
529 CoreFreePool (Image
->ImageContext
.FixupData
);
538 Get the image's private data from its handle.
540 @param ImageHandle The image handle
542 @return Return the image private data associated with ImageHandle.
545 LOADED_IMAGE_PRIVATE_DATA
*
546 CoreLoadedImageInfo (
547 IN EFI_HANDLE ImageHandle
551 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
552 LOADED_IMAGE_PRIVATE_DATA
*Image
;
554 Status
= CoreHandleProtocol (
556 &gEfiLoadedImageProtocolGuid
,
557 (VOID
**)&LoadedImage
559 if (!EFI_ERROR (Status
)) {
560 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
562 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %x\n", ImageHandle
));
571 Loads an EFI image into memory and returns a handle to the image.
573 @param BootPolicy If TRUE, indicates that the request originates
574 from the boot manager, and that the boot
575 manager is attempting to load FilePath as a
577 @param ParentImageHandle The caller's image handle.
578 @param FilePath The specific file path from which the image is
580 @param SourceBuffer If not NULL, a pointer to the memory location
581 containing a copy of the image to be loaded.
582 @param SourceSize The size in bytes of SourceBuffer.
583 @param DstBuffer The buffer to store the image
584 @param NumberOfPages If not NULL, it inputs a pointer to the page
585 number of DstBuffer and outputs a pointer to
586 the page number of the image. If this number is
587 not enough, return EFI_BUFFER_TOO_SMALL and
588 this parameter contains the required number.
589 @param ImageHandle Pointer to the returned image handle that is
590 created when the image is successfully loaded.
591 @param EntryPoint A pointer to the entry point
592 @param Attribute The bit mask of attributes to set for the load
595 @retval EFI_SUCCESS The image was loaded into memory.
596 @retval EFI_NOT_FOUND The FilePath was not found.
597 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
598 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
599 @retval EFI_UNSUPPORTED The image type is not supported, or the device
600 path cannot be parsed to locate the proper
601 protocol for loading the file.
602 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
608 CoreLoadImageCommon (
609 IN BOOLEAN BootPolicy
,
610 IN EFI_HANDLE ParentImageHandle
,
611 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
612 IN VOID
*SourceBuffer OPTIONAL
,
614 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
615 IN OUT UINTN
*NumberOfPages OPTIONAL
,
616 OUT EFI_HANDLE
*ImageHandle
,
617 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
621 LOADED_IMAGE_PRIVATE_DATA
*Image
;
622 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
623 IMAGE_FILE_HANDLE FHand
;
625 EFI_STATUS SecurityStatus
;
626 EFI_HANDLE DeviceHandle
;
627 UINT32 AuthenticationStatus
;
628 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
629 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
632 SecurityStatus
= EFI_SUCCESS
;
634 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
638 // The caller must pass in a valid ParentImageHandle
640 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
641 return EFI_INVALID_PARAMETER
;
644 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
645 if (ParentImage
== NULL
) {
646 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
647 return EFI_INVALID_PARAMETER
;
651 // Get simple read access to the source file
653 OriginalFilePath
= FilePath
;
654 Status
= CoreOpenImageFile (
661 &AuthenticationStatus
663 if (Status
== EFI_ALREADY_STARTED
) {
666 } else if (EFI_ERROR (Status
)) {
671 // Verify the Authentication Status through the Security Architectural Protocol
673 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
674 SecurityStatus
= gSecurity
->FileAuthenticationState (
676 AuthenticationStatus
,
679 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
680 Status
= SecurityStatus
;
688 // Allocate a new image structure
690 Image
= CoreAllocateZeroBootServicesPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
692 return EFI_OUT_OF_RESOURCES
;
696 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
698 FilePath
= OriginalFilePath
;
699 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
700 if (!EFI_ERROR (Status
)) {
701 FilePathSize
= CoreDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
702 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) ( ((UINT8
*)FilePath
) + FilePathSize
);
706 // Initialize the fields for an internal driver
708 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
709 Image
->Info
.SystemTable
= gDxeCoreST
;
710 Image
->Info
.DeviceHandle
= DeviceHandle
;
711 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
712 Image
->Info
.FilePath
= CoreDuplicateDevicePath (FilePath
);
713 Image
->Info
.ParentHandle
= ParentImageHandle
;
716 if (NumberOfPages
!= NULL
) {
717 Image
->NumberOfPages
= *NumberOfPages
;
719 Image
->NumberOfPages
= 0 ;
723 // Install the protocol interfaces for this image
724 // don't fire notifications yet
726 Status
= CoreInstallProtocolInterfaceNotify (
728 &gEfiLoadedImageProtocolGuid
,
729 EFI_NATIVE_INTERFACE
,
733 if (EFI_ERROR (Status
)) {
738 // Load the image. If EntryPoint is Null, it will not be set.
740 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
741 if (EFI_ERROR (Status
)) {
742 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
743 if (NumberOfPages
!= NULL
) {
744 *NumberOfPages
= Image
->NumberOfPages
;
750 if (NumberOfPages
!= NULL
) {
751 *NumberOfPages
= Image
->NumberOfPages
;
755 // Register the image in the Debug Image Info Table if the attribute is set
757 if (Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) {
758 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
762 //Reinstall loaded image protocol to fire any notifications
764 Status
= CoreReinstallProtocolInterface (
766 &gEfiLoadedImageProtocolGuid
,
770 if (EFI_ERROR (Status
)) {
775 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
776 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
778 if (OriginalFilePath
!= NULL
) {
779 Image
->LoadedImageDevicePath
= CoreDuplicateDevicePath (OriginalFilePath
);
783 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
785 Status
= CoreInstallProtocolInterface (
787 &gEfiLoadedImageDevicePathProtocolGuid
,
788 EFI_NATIVE_INTERFACE
,
789 Image
->LoadedImageDevicePath
791 if (EFI_ERROR (Status
)) {
796 // Success. Return the image handle
798 *ImageHandle
= Image
->Handle
;
802 // All done accessing the source file
803 // If we allocated the Source buffer, free it
805 if (FHand
.FreeBuffer
) {
806 CoreFreePool (FHand
.Source
);
810 // There was an error. If there's an Image structure, free it
812 if (EFI_ERROR (Status
)) {
814 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
817 } else if (EFI_ERROR (SecurityStatus
)) {
818 Status
= SecurityStatus
;
828 Loads an EFI image into memory and returns a handle to the image.
830 @param BootPolicy If TRUE, indicates that the request originates
831 from the boot manager, and that the boot
832 manager is attempting to load FilePath as a
834 @param ParentImageHandle The caller's image handle.
835 @param FilePath The specific file path from which the image is
837 @param SourceBuffer If not NULL, a pointer to the memory location
838 containing a copy of the image to be loaded.
839 @param SourceSize The size in bytes of SourceBuffer.
840 @param ImageHandle Pointer to the returned image handle that is
841 created when the image is successfully loaded.
843 @retval EFI_SUCCESS The image was loaded into memory.
844 @retval EFI_NOT_FOUND The FilePath was not found.
845 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
846 @retval EFI_UNSUPPORTED The image type is not supported, or the device
847 path cannot be parsed to locate the proper
848 protocol for loading the file.
849 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
856 IN BOOLEAN BootPolicy
,
857 IN EFI_HANDLE ParentImageHandle
,
858 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
859 IN VOID
*SourceBuffer OPTIONAL
,
861 OUT EFI_HANDLE
*ImageHandle
866 PERF_START (NULL
, "LoadImage", NULL
, 0);
868 Status
= CoreLoadImageCommon (
874 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
878 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
881 PERF_END (NULL
, "LoadImage", NULL
, 0);
889 Loads an EFI image into memory and returns a handle to the image with extended parameters.
891 @param This Calling context
892 @param ParentImageHandle The caller's image handle.
893 @param FilePath The specific file path from which the image is
895 @param SourceBuffer If not NULL, a pointer to the memory location
896 containing a copy of the image to be loaded.
897 @param SourceSize The size in bytes of SourceBuffer.
898 @param DstBuffer The buffer to store the image.
899 @param NumberOfPages For input, specifies the space size of the
900 image by caller if not NULL. For output,
901 specifies the actual space size needed.
902 @param ImageHandle Image handle for output.
903 @param EntryPoint Image entry point for output.
904 @param Attribute The bit mask of attributes to set for the load
907 @retval EFI_SUCCESS The image was loaded into memory.
908 @retval EFI_NOT_FOUND The FilePath was not found.
909 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
910 @retval EFI_UNSUPPORTED The image type is not supported, or the device
911 path cannot be parsed to locate the proper
912 protocol for loading the file.
913 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
920 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
921 IN EFI_HANDLE ParentImageHandle
,
922 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
923 IN VOID
*SourceBuffer OPTIONAL
,
925 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
926 OUT UINTN
*NumberOfPages OPTIONAL
,
927 OUT EFI_HANDLE
*ImageHandle
,
928 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
932 return CoreLoadImageCommon (
948 Transfer control to a loaded image's entry point.
950 @param ImageHandle Handle of image to be started.
951 @param ExitDataSize Pointer of the size to ExitData
952 @param ExitData Pointer to a pointer to a data buffer that
953 includes a Null-terminated Unicode string,
954 optionally followed by additional binary data.
955 The string is a description that the caller may
956 use to further indicate the reason for the
959 @retval EFI_INVALID_PARAMETER Invalid parameter
960 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
961 @retval EFI_SUCCESS Successfully transfer control to the image's
968 IN EFI_HANDLE ImageHandle
,
969 OUT UINTN
*ExitDataSize
,
970 OUT CHAR16
**ExitData OPTIONAL
974 LOADED_IMAGE_PRIVATE_DATA
*Image
;
975 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
976 UINT64 HandleDatabaseKey
;
979 Image
= CoreLoadedImageInfo (ImageHandle
);
980 if (Image
== NULL_HANDLE
|| Image
->Started
) {
981 return EFI_INVALID_PARAMETER
;
985 // Don't profile Objects or invalid start requests
987 PERF_START (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
991 // Push the current start image context, and
992 // link the current image to the head. This is the
993 // only image that can call Exit()
995 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
996 LastImage
= mCurrentImage
;
997 mCurrentImage
= Image
;
998 Image
->Tpl
= gEfiCurrentTpl
;
1001 // Set long jump for Exit() support
1002 // JumpContext must be aligned on a CPU specific boundary.
1003 // Overallocate the buffer and force the required alignment
1005 Image
->JumpBuffer
= CoreAllocateBootServicesPool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1006 if (Image
->JumpBuffer
== NULL
) {
1007 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1008 return EFI_OUT_OF_RESOURCES
;
1010 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1012 SetJumpFlag
= SetJump (Image
->JumpContext
);
1014 // The initial call to SetJump() must always return 0.
1015 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1019 // Call the image's entry point
1021 Image
->Started
= TRUE
;
1022 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1025 // Add some debug information if the image returned with error.
1026 // This make the user aware and check if the driver image have already released
1027 // all the resource in this situation.
1029 DEBUG_CODE_BEGIN ();
1030 if (EFI_ERROR (Image
->Status
)) {
1031 DEBUG ((DEBUG_ERROR
, "Error: Image at %10p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1036 // If the image returns, exit it through Exit()
1038 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1042 // Image has completed. Verify the tpl is the same
1044 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1045 CoreRestoreTpl (Image
->Tpl
);
1047 CoreFreePool (Image
->JumpBuffer
);
1050 // Pop the current start image context
1052 mCurrentImage
= LastImage
;
1055 // Go connect any handles that were created or modified while the image executed.
1057 CoreConnectHandlesByKey (HandleDatabaseKey
);
1060 // Handle the image's returned ExitData
1062 DEBUG_CODE_BEGIN ();
1063 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1067 "StartImage: ExitDataSize %d, ExitData %x",
1068 Image
->ExitDataSize
,
1071 if (Image
->ExitData
!= NULL
) {
1072 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1074 DEBUG ((DEBUG_LOAD
, "\n"));
1079 // Return the exit data to the caller
1081 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1082 *ExitDataSize
= Image
->ExitDataSize
;
1083 *ExitData
= Image
->ExitData
;
1086 // Caller doesn't want the exit data, free it
1088 CoreFreePool (Image
->ExitData
);
1089 Image
->ExitData
= NULL
;
1093 // Save the Status because Image will get destroyed if it is unloaded.
1095 Status
= Image
->Status
;
1098 // If the image returned an error, or if the image is an application
1101 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1102 CoreUnloadAndCloseImage (Image
, TRUE
);
1108 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1115 Unloads EFI image from memory.
1117 @param Image EFI image
1118 @param FreePage Free allocated pages
1122 CoreUnloadAndCloseImage (
1123 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
1129 EFI_HANDLE
*HandleBuffer
;
1131 EFI_GUID
**ProtocolGuidArray
;
1133 UINTN ProtocolIndex
;
1134 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
1135 UINTN OpenInfoCount
;
1136 UINTN OpenInfoIndex
;
1138 if (Image
->Ebc
!= NULL
) {
1140 // If EBC protocol exists we must perform cleanups for this image.
1142 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
1146 // Unload image, free Image->ImageContext->ModHandle
1148 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
1151 // Free our references to the image handle
1153 if (Image
->Handle
!= NULL_HANDLE
) {
1155 Status
= CoreLocateHandleBuffer (
1162 if (!EFI_ERROR (Status
)) {
1163 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
1164 Status
= CoreProtocolsPerHandle (
1165 HandleBuffer
[HandleIndex
],
1169 if (!EFI_ERROR (Status
)) {
1170 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
1171 Status
= CoreOpenProtocolInformation (
1172 HandleBuffer
[HandleIndex
],
1173 ProtocolGuidArray
[ProtocolIndex
],
1177 if (!EFI_ERROR (Status
)) {
1178 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
1179 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
1180 Status
= CoreCloseProtocol (
1181 HandleBuffer
[HandleIndex
],
1182 ProtocolGuidArray
[ProtocolIndex
],
1184 OpenInfo
[OpenInfoIndex
].ControllerHandle
1188 if (OpenInfo
!= NULL
) {
1189 CoreFreePool(OpenInfo
);
1193 if (ProtocolGuidArray
!= NULL
) {
1194 CoreFreePool(ProtocolGuidArray
);
1198 if (HandleBuffer
!= NULL
) {
1199 CoreFreePool (HandleBuffer
);
1203 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
1205 Status
= CoreUninstallProtocolInterface (
1207 &gEfiLoadedImageDevicePathProtocolGuid
,
1208 Image
->LoadedImageDevicePath
1211 Status
= CoreUninstallProtocolInterface (
1213 &gEfiLoadedImageProtocolGuid
,
1219 if (Image
->RuntimeData
!= NULL
) {
1220 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
1222 // Remove the Image from the Runtime Image list as we are about to Free it!
1224 RemoveEntryList (&Image
->RuntimeData
->Link
);
1226 CoreFreePool (Image
->RuntimeData
);
1230 // Free the Image from memory
1232 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
1233 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
1237 // Done with the Image structure
1239 if (Image
->Info
.FilePath
!= NULL
) {
1240 CoreFreePool (Image
->Info
.FilePath
);
1243 if (Image
->LoadedImageDevicePath
!= NULL
) {
1244 CoreFreePool (Image
->LoadedImageDevicePath
);
1247 if (Image
->FixupData
!= NULL
) {
1248 CoreFreePool (Image
->FixupData
);
1251 CoreFreePool (Image
);
1258 Terminates the currently loaded EFI image and returns control to boot services.
1260 @param ImageHandle Handle that identifies the image. This
1261 parameter is passed to the image on entry.
1262 @param Status The image's exit code.
1263 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1264 ExitStatus is EFI_SUCCESS.
1265 @param ExitData Pointer to a data buffer that includes a
1266 Null-terminated Unicode string, optionally
1267 followed by additional binary data. The string
1268 is a description that the caller may use to
1269 further indicate the reason for the image's
1272 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1274 @retval EFI_SUCCESS Successfully terminates the currently loaded
1276 @retval EFI_ACCESS_DENIED Should never reach there.
1277 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1283 IN EFI_HANDLE ImageHandle
,
1284 IN EFI_STATUS Status
,
1285 IN UINTN ExitDataSize
,
1286 IN CHAR16
*ExitData OPTIONAL
1289 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1293 // Prevent possible reentrance to this function
1294 // for the same ImageHandle
1296 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1298 Image
= CoreLoadedImageInfo (ImageHandle
);
1299 if (Image
== NULL_HANDLE
) {
1300 Status
= EFI_INVALID_PARAMETER
;
1304 if (!Image
->Started
) {
1306 // The image has not been started so just free its resources
1308 CoreUnloadAndCloseImage (Image
, TRUE
);
1309 Status
= EFI_SUCCESS
;
1314 // Image has been started, verify this image can exit
1316 if (Image
!= mCurrentImage
) {
1317 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1318 Status
= EFI_INVALID_PARAMETER
;
1325 Image
->Status
= Status
;
1328 // If there's ExitData info, move it
1330 if (ExitData
!= NULL
) {
1331 Image
->ExitDataSize
= ExitDataSize
;
1332 Image
->ExitData
= CoreAllocateBootServicesPool (Image
->ExitDataSize
);
1333 if (Image
->ExitData
== NULL
) {
1334 Status
= EFI_OUT_OF_RESOURCES
;
1337 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1340 CoreRestoreTpl (OldTpl
);
1342 // return to StartImage
1344 LongJump (Image
->JumpContext
, (UINTN
)-1);
1347 // If we return from LongJump, then it is an error
1350 Status
= EFI_ACCESS_DENIED
;
1352 CoreRestoreTpl (OldTpl
);
1362 @param ImageHandle Handle that identifies the image to be
1365 @retval EFI_SUCCESS The image has been unloaded.
1366 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1368 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1374 IN EFI_HANDLE ImageHandle
1378 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1382 // Prevent possible reentrance to this function
1383 // for the same ImageHandle
1385 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1387 Image
= CoreLoadedImageInfo (ImageHandle
);
1388 if (Image
== NULL
) {
1390 // The image handle is not valid
1392 Status
= EFI_INVALID_PARAMETER
;
1396 if (Image
->Started
) {
1398 // The image has been started, request it to unload.
1400 Status
= EFI_UNSUPPORTED
;
1401 if (Image
->Info
.Unload
!= NULL
) {
1402 Status
= Image
->Info
.Unload (ImageHandle
);
1407 // This Image hasn't been started, thus it can be unloaded
1409 Status
= EFI_SUCCESS
;
1413 if (!EFI_ERROR (Status
)) {
1415 // if the Image was not started or Unloaded O.K. then clean up
1417 CoreUnloadAndCloseImage (Image
, TRUE
);
1421 CoreRestoreTpl (OldTpl
);
1428 Unload the specified image.
1430 @param This Indicates the calling context.
1431 @param ImageHandle The specified image handle.
1433 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1434 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1435 @retval EFI_SUCCESS Image successfully unloaded.
1441 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1442 IN EFI_HANDLE ImageHandle
1445 return CoreUnloadImage (ImageHandle
);