2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 LOADED_IMAGE_PRIVATE_DATA
*mCurrentImage
= NULL
;
23 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData
= {
24 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE
,
34 // This code is needed to build the Image handle for the DXE Core
36 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage
= {
37 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
, // Signature
39 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
, // Image type
40 TRUE
, // If entrypoint has been called
43 EFI_LOADED_IMAGE_INFORMATION_REVISION
, // Revision
44 NULL
, // Parent handle
45 NULL
, // System handle
47 NULL
, // Device handle
56 EfiBootServicesCode
, // ImageCodeType
57 EfiBootServicesData
// ImageDataType
59 (EFI_PHYSICAL_ADDRESS
)0, // ImageBasePage
63 EFI_SUCCESS
, // Status
71 NULL
// LoadedImageDevicePath
77 Add the Image Services to EFI Boot Services Table and install the protocol
78 interfaces for this image.
80 @param HobStart The HOB to initialize
86 CoreInitializeImageServices (
91 LOADED_IMAGE_PRIVATE_DATA
*Image
;
92 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
93 UINT64 DxeCoreImageLength
;
94 VOID
*DxeCoreEntryPoint
;
95 EFI_PEI_HOB_POINTERS DxeCoreHob
;
97 // Searching for image hob
99 DxeCoreHob
.Raw
= HobStart
;
100 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
101 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
107 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
109 ASSERT (DxeCoreHob
.Raw
!= NULL
);
111 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
112 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
113 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
114 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
116 // Initialize the fields for an internal driver
118 Image
= &mCorePrivateImage
;
120 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
121 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
122 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
123 Image
->Tpl
= gEfiCurrentTpl
;
124 Image
->Info
.SystemTable
= gDxeCoreST
;
125 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
126 Image
->Info
.ImageSize
= DxeCoreImageLength
;
129 // Install the protocol interfaces for this image
131 Status
= CoreInstallProtocolInterface (
133 &gEfiLoadedImageProtocolGuid
,
134 EFI_NATIVE_INTERFACE
,
137 ASSERT_EFI_ERROR (Status
);
139 mCurrentImage
= Image
;
142 // Fill in DXE globals
144 gDxeCoreImageHandle
= Image
->Handle
;
145 gDxeCoreLoadedImage
= &Image
->Info
;
148 // Export DXE Core PE Loader functionality
150 return CoreInstallProtocolInterface (
151 &mLoadPe32PrivateData
.Handle
,
152 &gEfiLoadPeImageProtocolGuid
,
153 EFI_NATIVE_INTERFACE
,
154 &mLoadPe32PrivateData
.Pe32Image
160 Loads, relocates, and invokes a PE/COFF image
162 @param BootPolicy If TRUE, indicates that the request originates
163 from the boot manager, and that the boot
164 manager is attempting to load FilePath as a
166 @param Pe32Handle The handle of PE32 image
167 @param Image PE image to be loaded
168 @param DstBuffer The buffer to store the image
169 @param EntryPoint A pointer to the entry point
170 @param Attribute The bit mask of attributes to set for the load
173 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
174 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
175 relocate the PE/COFF file
176 @retval EFI_INVALID_PARAMETER Invalid parameter
177 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
182 IN BOOLEAN BootPolicy
,
184 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
185 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
186 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
191 BOOLEAN DstBufAlocated
;
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 if (!Image
->ImageContext
.IsTeImage
) {
319 Image
->ImageContext
.ImageAddress
=
320 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
321 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
325 // Load the image from the file into the allocated memory
327 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
328 if (EFI_ERROR (Status
)) {
333 // If this is a Runtime Driver, then allocate memory for the FixupData that
334 // is used to relocate the image when SetVirtualAddressMap() is called. The
335 // relocation is done by the Runtime AP.
337 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
338 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
339 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
340 if (Image
->ImageContext
.FixupData
== NULL
) {
341 Status
= EFI_OUT_OF_RESOURCES
;
348 // Measure the image before applying fixup
350 Status
= CoreLocateProtocol (
351 &gEfiTcgPlatformProtocolGuid
,
353 (VOID
**) &TcgPlatformProtocol
355 if (!EFI_ERROR (Status
)) {
356 FHandle
= (IMAGE_FILE_HANDLE
*) Image
->ImageContext
.Handle
;
357 Status
= TcgPlatformProtocol
->MeasurePeImage (
359 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FHandle
->Source
,
362 Image
->ImageContext
.ImageType
,
363 Image
->Info
.DeviceHandle
,
367 ASSERT_EFI_ERROR (Status
);
371 // Relocate the image in memory
373 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
374 if (EFI_ERROR (Status
)) {
379 // Flush the Instruction Cache
381 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
384 // Copy the machine type from the context to the image private data. This
385 // is needed during image unload to know if we should call an EBC protocol
386 // to unload the image.
388 Image
->Machine
= Image
->ImageContext
.Machine
;
391 // Get the image entry point. If it's an EBC image, then call into the
392 // interpreter to create a thunk for the entry point and use the returned
393 // value for the entry point.
395 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
396 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
398 // Locate the EBC interpreter protocol
400 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
401 if (EFI_ERROR(Status
)) {
402 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
407 // Register a callback for flushing the instruction cache so that created
408 // thunks can be flushed.
410 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
411 if (EFI_ERROR(Status
)) {
416 // Create a thunk for the image's entry point. This will be the new
417 // entry point for the image.
419 Status
= Image
->Ebc
->CreateThunk (
422 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
423 (VOID
**) &Image
->EntryPoint
425 if (EFI_ERROR(Status
)) {
431 // Fill in the image information for the Loaded Image Protocol
433 Image
->Type
= Image
->ImageContext
.ImageType
;
434 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
435 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
436 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
437 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
438 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
439 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
441 // Make a list off all the RT images so we can let the RT AP know about them.
443 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
444 if (Image
->RuntimeData
== NULL
) {
447 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
448 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
449 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
450 Image
->RuntimeData
->Handle
= Image
->Handle
;
451 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
456 // Fill in the entry point of the image if it is available
458 if (EntryPoint
!= NULL
) {
459 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
463 // Print the load address and the PDB file name if it is available
470 CHAR8 EfiFileName
[256];
473 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
474 "Loading driver at 0x%11p EntryPoint=0x%11p ",
475 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
476 FUNCTION_ENTRY_POINT ((UINTN
) Image
->ImageContext
.EntryPoint
)));
480 // Print Module Name by Pdb file path
482 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
484 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
485 if (Image
->ImageContext
.PdbPointer
[Index
] == '\\') {
486 StartIndex
= Index
+ 1;
490 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
492 for (Index
= 0; Index
< sizeof (EfiFileName
); Index
++) {
493 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
494 if (EfiFileName
[Index
] == 0) {
495 EfiFileName
[Index
] = '.';
497 if (EfiFileName
[Index
] == '.') {
498 EfiFileName
[Index
+ 1] = 'e';
499 EfiFileName
[Index
+ 2] = 'f';
500 EfiFileName
[Index
+ 3] = 'i';
501 EfiFileName
[Index
+ 4] = 0;
505 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
507 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
519 if (DstBufAlocated
) {
520 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
523 if (Image
->ImageContext
.FixupData
!= NULL
) {
524 CoreFreePool (Image
->ImageContext
.FixupData
);
533 Get the image's private data from its handle.
535 @param ImageHandle The image handle
537 @return Return the image private data associated with ImageHandle.
540 LOADED_IMAGE_PRIVATE_DATA
*
541 CoreLoadedImageInfo (
542 IN EFI_HANDLE ImageHandle
546 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
547 LOADED_IMAGE_PRIVATE_DATA
*Image
;
549 Status
= CoreHandleProtocol (
551 &gEfiLoadedImageProtocolGuid
,
552 (VOID
**)&LoadedImage
554 if (!EFI_ERROR (Status
)) {
555 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
557 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
566 Unloads EFI image from memory.
568 @param Image EFI image
569 @param FreePage Free allocated pages
573 CoreUnloadAndCloseImage (
574 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
580 EFI_HANDLE
*HandleBuffer
;
582 EFI_GUID
**ProtocolGuidArray
;
585 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
589 if (Image
->Ebc
!= NULL
) {
591 // If EBC protocol exists we must perform cleanups for this image.
593 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
597 // Unload image, free Image->ImageContext->ModHandle
599 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
602 // Free our references to the image handle
604 if (Image
->Handle
!= NULL
) {
606 Status
= CoreLocateHandleBuffer (
613 if (!EFI_ERROR (Status
)) {
614 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
615 Status
= CoreProtocolsPerHandle (
616 HandleBuffer
[HandleIndex
],
620 if (!EFI_ERROR (Status
)) {
621 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
622 Status
= CoreOpenProtocolInformation (
623 HandleBuffer
[HandleIndex
],
624 ProtocolGuidArray
[ProtocolIndex
],
628 if (!EFI_ERROR (Status
)) {
629 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
630 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
631 Status
= CoreCloseProtocol (
632 HandleBuffer
[HandleIndex
],
633 ProtocolGuidArray
[ProtocolIndex
],
635 OpenInfo
[OpenInfoIndex
].ControllerHandle
639 if (OpenInfo
!= NULL
) {
640 CoreFreePool(OpenInfo
);
644 if (ProtocolGuidArray
!= NULL
) {
645 CoreFreePool(ProtocolGuidArray
);
649 if (HandleBuffer
!= NULL
) {
650 CoreFreePool (HandleBuffer
);
654 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
656 Status
= CoreUninstallProtocolInterface (
658 &gEfiLoadedImageDevicePathProtocolGuid
,
659 Image
->LoadedImageDevicePath
662 Status
= CoreUninstallProtocolInterface (
664 &gEfiLoadedImageProtocolGuid
,
670 if (Image
->RuntimeData
!= NULL
) {
671 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
673 // Remove the Image from the Runtime Image list as we are about to Free it!
675 RemoveEntryList (&Image
->RuntimeData
->Link
);
677 CoreFreePool (Image
->RuntimeData
);
681 // Free the Image from memory
683 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
684 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
688 // Done with the Image structure
690 if (Image
->Info
.FilePath
!= NULL
) {
691 CoreFreePool (Image
->Info
.FilePath
);
694 if (Image
->LoadedImageDevicePath
!= NULL
) {
695 CoreFreePool (Image
->LoadedImageDevicePath
);
698 if (Image
->FixupData
!= NULL
) {
699 CoreFreePool (Image
->FixupData
);
702 CoreFreePool (Image
);
707 Loads an EFI image into memory and returns a handle to the image.
709 @param BootPolicy If TRUE, indicates that the request originates
710 from the boot manager, and that the boot
711 manager is attempting to load FilePath as a
713 @param ParentImageHandle The caller's image handle.
714 @param FilePath The specific file path from which the image is
716 @param SourceBuffer If not NULL, a pointer to the memory location
717 containing a copy of the image to be loaded.
718 @param SourceSize The size in bytes of SourceBuffer.
719 @param DstBuffer The buffer to store the image
720 @param NumberOfPages If not NULL, it inputs a pointer to the page
721 number of DstBuffer and outputs a pointer to
722 the page number of the image. If this number is
723 not enough, return EFI_BUFFER_TOO_SMALL and
724 this parameter contains the required number.
725 @param ImageHandle Pointer to the returned image handle that is
726 created when the image is successfully loaded.
727 @param EntryPoint A pointer to the entry point
728 @param Attribute The bit mask of attributes to set for the load
731 @retval EFI_SUCCESS The image was loaded into memory.
732 @retval EFI_NOT_FOUND The FilePath was not found.
733 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
734 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
735 @retval EFI_UNSUPPORTED The image type is not supported, or the device
736 path cannot be parsed to locate the proper
737 protocol for loading the file.
738 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
743 CoreLoadImageCommon (
744 IN BOOLEAN BootPolicy
,
745 IN EFI_HANDLE ParentImageHandle
,
746 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
747 IN VOID
*SourceBuffer OPTIONAL
,
749 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
750 IN OUT UINTN
*NumberOfPages OPTIONAL
,
751 OUT EFI_HANDLE
*ImageHandle
,
752 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
756 LOADED_IMAGE_PRIVATE_DATA
*Image
;
757 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
758 IMAGE_FILE_HANDLE FHand
;
760 EFI_STATUS SecurityStatus
;
761 EFI_HANDLE DeviceHandle
;
762 UINT32 AuthenticationStatus
;
763 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
764 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
767 SecurityStatus
= EFI_SUCCESS
;
769 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
773 // The caller must pass in a valid ParentImageHandle
775 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
776 return EFI_INVALID_PARAMETER
;
779 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
780 if (ParentImage
== NULL
) {
781 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
782 return EFI_INVALID_PARAMETER
;
786 // Get simple read access to the source file
788 OriginalFilePath
= FilePath
;
789 Status
= CoreOpenImageFile (
796 &AuthenticationStatus
798 if (Status
== EFI_ALREADY_STARTED
) {
801 } else if (EFI_ERROR (Status
)) {
806 // Verify the Authentication Status through the Security Architectural Protocol
808 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
809 SecurityStatus
= gSecurity
->FileAuthenticationState (
811 AuthenticationStatus
,
814 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
815 Status
= SecurityStatus
;
823 // Allocate a new image structure
825 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
827 return EFI_OUT_OF_RESOURCES
;
831 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
833 FilePath
= OriginalFilePath
;
834 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
835 if (!EFI_ERROR (Status
)) {
836 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
837 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
841 // Initialize the fields for an internal driver
843 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
844 Image
->Info
.SystemTable
= gDxeCoreST
;
845 Image
->Info
.DeviceHandle
= DeviceHandle
;
846 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
847 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
848 Image
->Info
.ParentHandle
= ParentImageHandle
;
851 if (NumberOfPages
!= NULL
) {
852 Image
->NumberOfPages
= *NumberOfPages
;
854 Image
->NumberOfPages
= 0 ;
858 // Install the protocol interfaces for this image
859 // don't fire notifications yet
861 Status
= CoreInstallProtocolInterfaceNotify (
863 &gEfiLoadedImageProtocolGuid
,
864 EFI_NATIVE_INTERFACE
,
868 if (EFI_ERROR (Status
)) {
873 // Load the image. If EntryPoint is Null, it will not be set.
875 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
876 if (EFI_ERROR (Status
)) {
877 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
878 if (NumberOfPages
!= NULL
) {
879 *NumberOfPages
= Image
->NumberOfPages
;
885 if (NumberOfPages
!= NULL
) {
886 *NumberOfPages
= Image
->NumberOfPages
;
890 // Register the image in the Debug Image Info Table if the attribute is set
892 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
893 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
897 //Reinstall loaded image protocol to fire any notifications
899 Status
= CoreReinstallProtocolInterface (
901 &gEfiLoadedImageProtocolGuid
,
905 if (EFI_ERROR (Status
)) {
910 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
911 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
913 if (OriginalFilePath
!= NULL
) {
914 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
918 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
920 Status
= CoreInstallProtocolInterface (
922 &gEfiLoadedImageDevicePathProtocolGuid
,
923 EFI_NATIVE_INTERFACE
,
924 Image
->LoadedImageDevicePath
926 if (EFI_ERROR (Status
)) {
931 // Success. Return the image handle
933 *ImageHandle
= Image
->Handle
;
937 // All done accessing the source file
938 // If we allocated the Source buffer, free it
940 if (FHand
.FreeBuffer
) {
941 CoreFreePool (FHand
.Source
);
945 // There was an error. If there's an Image structure, free it
947 if (EFI_ERROR (Status
)) {
949 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
952 } else if (EFI_ERROR (SecurityStatus
)) {
953 Status
= SecurityStatus
;
963 Loads an EFI image into memory and returns a handle to the image.
965 @param BootPolicy If TRUE, indicates that the request originates
966 from the boot manager, and that the boot
967 manager is attempting to load FilePath as a
969 @param ParentImageHandle The caller's image handle.
970 @param FilePath The specific file path from which the image is
972 @param SourceBuffer If not NULL, a pointer to the memory location
973 containing a copy of the image to be loaded.
974 @param SourceSize The size in bytes of SourceBuffer.
975 @param ImageHandle Pointer to the returned image handle that is
976 created when the image is successfully loaded.
978 @retval EFI_SUCCESS The image was loaded into memory.
979 @retval EFI_NOT_FOUND The FilePath was not found.
980 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
981 @retval EFI_UNSUPPORTED The image type is not supported, or the device
982 path cannot be parsed to locate the proper
983 protocol for loading the file.
984 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
991 IN BOOLEAN BootPolicy
,
992 IN EFI_HANDLE ParentImageHandle
,
993 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
994 IN VOID
*SourceBuffer OPTIONAL
,
996 OUT EFI_HANDLE
*ImageHandle
1001 PERF_START (NULL
, "LoadImage", NULL
, 0);
1003 Status
= CoreLoadImageCommon (
1009 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1013 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1016 PERF_END (NULL
, "LoadImage", NULL
, 0);
1024 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1026 @param This Calling context
1027 @param ParentImageHandle The caller's image handle.
1028 @param FilePath The specific file path from which the image is
1030 @param SourceBuffer If not NULL, a pointer to the memory location
1031 containing a copy of the image to be loaded.
1032 @param SourceSize The size in bytes of SourceBuffer.
1033 @param DstBuffer The buffer to store the image.
1034 @param NumberOfPages For input, specifies the space size of the
1035 image by caller if not NULL. For output,
1036 specifies the actual space size needed.
1037 @param ImageHandle Image handle for output.
1038 @param EntryPoint Image entry point for output.
1039 @param Attribute The bit mask of attributes to set for the load
1042 @retval EFI_SUCCESS The image was loaded into memory.
1043 @retval EFI_NOT_FOUND The FilePath was not found.
1044 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1045 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1046 path cannot be parsed to locate the proper
1047 protocol for loading the file.
1048 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1055 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1056 IN EFI_HANDLE ParentImageHandle
,
1057 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1058 IN VOID
*SourceBuffer OPTIONAL
,
1059 IN UINTN SourceSize
,
1060 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1061 OUT UINTN
*NumberOfPages OPTIONAL
,
1062 OUT EFI_HANDLE
*ImageHandle
,
1063 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1067 return CoreLoadImageCommon (
1083 Transfer control to a loaded image's entry point.
1085 @param ImageHandle Handle of image to be started.
1086 @param ExitDataSize Pointer of the size to ExitData
1087 @param ExitData Pointer to a pointer to a data buffer that
1088 includes a Null-terminated Unicode string,
1089 optionally followed by additional binary data.
1090 The string is a description that the caller may
1091 use to further indicate the reason for the
1094 @retval EFI_INVALID_PARAMETER Invalid parameter
1095 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1096 @retval EFI_SUCCESS Successfully transfer control to the image's
1103 IN EFI_HANDLE ImageHandle
,
1104 OUT UINTN
*ExitDataSize
,
1105 OUT CHAR16
**ExitData OPTIONAL
1109 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1110 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1111 UINT64 HandleDatabaseKey
;
1114 Image
= CoreLoadedImageInfo (ImageHandle
);
1115 if (Image
== NULL
|| Image
->Started
) {
1116 return EFI_INVALID_PARAMETER
;
1120 // Don't profile Objects or invalid start requests
1122 PERF_START (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1126 // Push the current start image context, and
1127 // link the current image to the head. This is the
1128 // only image that can call Exit()
1130 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1131 LastImage
= mCurrentImage
;
1132 mCurrentImage
= Image
;
1133 Image
->Tpl
= gEfiCurrentTpl
;
1136 // Set long jump for Exit() support
1137 // JumpContext must be aligned on a CPU specific boundary.
1138 // Overallocate the buffer and force the required alignment
1140 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1141 if (Image
->JumpBuffer
== NULL
) {
1142 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1143 return EFI_OUT_OF_RESOURCES
;
1145 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1147 SetJumpFlag
= SetJump (Image
->JumpContext
);
1149 // The initial call to SetJump() must always return 0.
1150 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1152 if (SetJumpFlag
== 0) {
1154 // Call the image's entry point
1156 Image
->Started
= TRUE
;
1157 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1160 // Add some debug information if the image returned with error.
1161 // This make the user aware and check if the driver image have already released
1162 // all the resource in this situation.
1164 DEBUG_CODE_BEGIN ();
1165 if (EFI_ERROR (Image
->Status
)) {
1166 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1171 // If the image returns, exit it through Exit()
1173 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1177 // Image has completed. Verify the tpl is the same
1179 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1180 CoreRestoreTpl (Image
->Tpl
);
1182 CoreFreePool (Image
->JumpBuffer
);
1185 // Pop the current start image context
1187 mCurrentImage
= LastImage
;
1190 // Go connect any handles that were created or modified while the image executed.
1192 CoreConnectHandlesByKey (HandleDatabaseKey
);
1195 // Handle the image's returned ExitData
1197 DEBUG_CODE_BEGIN ();
1198 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1200 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", Image
->ExitDataSize
, Image
->ExitData
));
1201 if (Image
->ExitData
!= NULL
) {
1202 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1204 DEBUG ((DEBUG_LOAD
, "\n"));
1209 // Return the exit data to the caller
1211 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1212 *ExitDataSize
= Image
->ExitDataSize
;
1213 *ExitData
= Image
->ExitData
;
1216 // Caller doesn't want the exit data, free it
1218 CoreFreePool (Image
->ExitData
);
1219 Image
->ExitData
= NULL
;
1223 // Save the Status because Image will get destroyed if it is unloaded.
1225 Status
= Image
->Status
;
1228 // If the image returned an error, or if the image is an application
1231 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1232 CoreUnloadAndCloseImage (Image
, TRUE
);
1238 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1243 Terminates the currently loaded EFI image and returns control to boot services.
1245 @param ImageHandle Handle that identifies the image. This
1246 parameter is passed to the image on entry.
1247 @param Status The image's exit code.
1248 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1249 ExitStatus is EFI_SUCCESS.
1250 @param ExitData Pointer to a data buffer that includes a
1251 Null-terminated Unicode string, optionally
1252 followed by additional binary data. The string
1253 is a description that the caller may use to
1254 further indicate the reason for the image's
1257 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1259 @retval EFI_SUCCESS Successfully terminates the currently loaded
1261 @retval EFI_ACCESS_DENIED Should never reach there.
1262 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1268 IN EFI_HANDLE ImageHandle
,
1269 IN EFI_STATUS Status
,
1270 IN UINTN ExitDataSize
,
1271 IN CHAR16
*ExitData OPTIONAL
1274 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1278 // Prevent possible reentrance to this function
1279 // for the same ImageHandle
1281 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1283 Image
= CoreLoadedImageInfo (ImageHandle
);
1284 if (Image
== NULL
) {
1285 Status
= EFI_INVALID_PARAMETER
;
1289 if (!Image
->Started
) {
1291 // The image has not been started so just free its resources
1293 CoreUnloadAndCloseImage (Image
, TRUE
);
1294 Status
= EFI_SUCCESS
;
1299 // Image has been started, verify this image can exit
1301 if (Image
!= mCurrentImage
) {
1302 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1303 Status
= EFI_INVALID_PARAMETER
;
1310 Image
->Status
= Status
;
1313 // If there's ExitData info, move it
1315 if (ExitData
!= NULL
) {
1316 Image
->ExitDataSize
= ExitDataSize
;
1317 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1318 if (Image
->ExitData
== NULL
) {
1319 Status
= EFI_OUT_OF_RESOURCES
;
1322 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1325 CoreRestoreTpl (OldTpl
);
1327 // return to StartImage
1329 LongJump (Image
->JumpContext
, (UINTN
)-1);
1332 // If we return from LongJump, then it is an error
1335 Status
= EFI_ACCESS_DENIED
;
1337 CoreRestoreTpl (OldTpl
);
1347 @param ImageHandle Handle that identifies the image to be
1350 @retval EFI_SUCCESS The image has been unloaded.
1351 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1353 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1359 IN EFI_HANDLE ImageHandle
1363 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1365 Image
= CoreLoadedImageInfo (ImageHandle
);
1366 if (Image
== NULL
) {
1368 // The image handle is not valid
1370 Status
= EFI_INVALID_PARAMETER
;
1374 if (Image
->Started
) {
1376 // The image has been started, request it to unload.
1378 Status
= EFI_UNSUPPORTED
;
1379 if (Image
->Info
.Unload
!= NULL
) {
1380 Status
= Image
->Info
.Unload (ImageHandle
);
1385 // This Image hasn't been started, thus it can be unloaded
1387 Status
= EFI_SUCCESS
;
1391 if (!EFI_ERROR (Status
)) {
1393 // if the Image was not started or Unloaded O.K. then clean up
1395 CoreUnloadAndCloseImage (Image
, TRUE
);
1405 Unload the specified image.
1407 @param This Indicates the calling context.
1408 @param ImageHandle The specified image handle.
1410 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1411 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1412 @retval EFI_SUCCESS Image successfully unloaded.
1418 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1419 IN EFI_HANDLE ImageHandle
1422 return CoreUnloadImage (ImageHandle
);