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 IMAGE_FILE_HANDLE
*FHandle
;
197 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
199 Image
->ImageContext
.Handle
= Pe32Handle
;
200 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
203 // Get information about the image being loaded
205 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
206 if (EFI_ERROR (Status
)) {
210 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
211 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
213 // The PE/COFF loader can support loading image types that can be executed.
214 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
216 return EFI_UNSUPPORTED
;
221 // Set EFI memory type based on ImageType
223 switch (Image
->ImageContext
.ImageType
) {
224 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
225 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
226 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
228 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
229 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
230 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
232 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
233 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
234 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
235 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
238 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
239 return EFI_UNSUPPORTED
;
242 // Get the image base address in the original PeImage.
244 LinkTimeBase
= (UINTN
) Image
->ImageContext
.ImageAddress
;
247 // Allocate memory of the correct memory type aligned on the required image boundry
249 DstBufAlocated
= FALSE
;
250 if (DstBuffer
== 0) {
252 // Allocate Destination Buffer as caller did not pass it in
255 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
256 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
258 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
261 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
264 // If the image relocations have not been stripped, then load at any address.
265 // Otherwise load at the address at which it was linked.
267 // Memory below 1MB should be treated reserved for CSM and there should be
268 // no modules whose preferred load addresses are below 1MB.
270 Status
= EFI_OUT_OF_RESOURCES
;
271 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
272 Status
= CoreAllocatePages (
274 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
275 Image
->NumberOfPages
,
276 &Image
->ImageContext
.ImageAddress
279 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
280 Status
= CoreAllocatePages (
282 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
283 Image
->NumberOfPages
,
284 &Image
->ImageContext
.ImageAddress
287 if (EFI_ERROR (Status
)) {
290 DstBufAlocated
= TRUE
;
293 // Caller provided the destination buffer
296 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
298 // If the image relocations were stripped, and the caller provided a
299 // destination buffer address that does not match the address that the
300 // image is linked at, then the image cannot be loaded.
302 return EFI_INVALID_PARAMETER
;
305 if (Image
->NumberOfPages
!= 0 &&
306 Image
->NumberOfPages
<
307 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
308 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
309 return EFI_BUFFER_TOO_SMALL
;
312 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
313 Image
->ImageContext
.ImageAddress
= DstBuffer
;
316 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
317 if (!Image
->ImageContext
.IsTeImage
) {
318 Image
->ImageContext
.ImageAddress
=
319 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
320 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
324 // Load the image from the file into the allocated memory
326 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
327 if (EFI_ERROR (Status
)) {
332 // If this is a Runtime Driver, then allocate memory for the FixupData that
333 // is used to relocate the image when SetVirtualAddressMap() is called. The
334 // relocation is done by the Runtime AP.
336 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
337 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
338 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
339 if (Image
->ImageContext
.FixupData
== NULL
) {
340 Status
= EFI_OUT_OF_RESOURCES
;
347 // Relocate the image in memory
349 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
350 if (EFI_ERROR (Status
)) {
355 // Flush the Instruction Cache
357 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
360 // Copy the machine type from the context to the image private data. This
361 // is needed during image unload to know if we should call an EBC protocol
362 // to unload the image.
364 Image
->Machine
= Image
->ImageContext
.Machine
;
367 // Get the image entry point. If it's an EBC image, then call into the
368 // interpreter to create a thunk for the entry point and use the returned
369 // value for the entry point.
371 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
372 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
374 // Locate the EBC interpreter protocol
376 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
377 if (EFI_ERROR(Status
)) {
378 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
383 // Register a callback for flushing the instruction cache so that created
384 // thunks can be flushed.
386 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
387 if (EFI_ERROR(Status
)) {
392 // Create a thunk for the image's entry point. This will be the new
393 // entry point for the image.
395 Status
= Image
->Ebc
->CreateThunk (
398 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
399 (VOID
**) &Image
->EntryPoint
401 if (EFI_ERROR(Status
)) {
407 // Fill in the image information for the Loaded Image Protocol
409 Image
->Type
= Image
->ImageContext
.ImageType
;
410 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
411 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
412 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
413 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
414 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
415 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
417 // Make a list off all the RT images so we can let the RT AP know about them.
419 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
420 if (Image
->RuntimeData
== NULL
) {
423 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
424 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
425 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
426 Image
->RuntimeData
->Handle
= Image
->Handle
;
427 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
432 // Fill in the entry point of the image if it is available
434 if (EntryPoint
!= NULL
) {
435 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
439 // Print the load address and the PDB file name if it is available
446 CHAR8 EfiFileName
[256];
449 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
450 "Loading driver at 0x%11p EntryPoint=0x%11p ",
451 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
452 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
456 // Print Module Name by Pdb file path.
457 // Windows and Unix style file path are all trimmed correctly.
459 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
461 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
462 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
463 StartIndex
= Index
+ 1;
467 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
468 // The PDB file name is limited in the range of 0~255.
469 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
471 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
472 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
473 if (EfiFileName
[Index
] == 0) {
474 EfiFileName
[Index
] = '.';
476 if (EfiFileName
[Index
] == '.') {
477 EfiFileName
[Index
+ 1] = 'e';
478 EfiFileName
[Index
+ 2] = 'f';
479 EfiFileName
[Index
+ 3] = 'i';
480 EfiFileName
[Index
+ 4] = 0;
485 if (Index
== sizeof (EfiFileName
) - 4) {
486 EfiFileName
[Index
] = 0;
488 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
490 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
502 if (DstBufAlocated
) {
503 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
506 if (Image
->ImageContext
.FixupData
!= NULL
) {
507 CoreFreePool (Image
->ImageContext
.FixupData
);
516 Get the image's private data from its handle.
518 @param ImageHandle The image handle
520 @return Return the image private data associated with ImageHandle.
523 LOADED_IMAGE_PRIVATE_DATA
*
524 CoreLoadedImageInfo (
525 IN EFI_HANDLE ImageHandle
529 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
530 LOADED_IMAGE_PRIVATE_DATA
*Image
;
532 Status
= CoreHandleProtocol (
534 &gEfiLoadedImageProtocolGuid
,
535 (VOID
**)&LoadedImage
537 if (!EFI_ERROR (Status
)) {
538 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
540 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
549 Unloads EFI image from memory.
551 @param Image EFI image
552 @param FreePage Free allocated pages
556 CoreUnloadAndCloseImage (
557 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
563 EFI_HANDLE
*HandleBuffer
;
565 EFI_GUID
**ProtocolGuidArray
;
568 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
572 if (Image
->Ebc
!= NULL
) {
574 // If EBC protocol exists we must perform cleanups for this image.
576 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
580 // Unload image, free Image->ImageContext->ModHandle
582 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
585 // Free our references to the image handle
587 if (Image
->Handle
!= NULL
) {
589 Status
= CoreLocateHandleBuffer (
596 if (!EFI_ERROR (Status
)) {
597 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
598 Status
= CoreProtocolsPerHandle (
599 HandleBuffer
[HandleIndex
],
603 if (!EFI_ERROR (Status
)) {
604 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
605 Status
= CoreOpenProtocolInformation (
606 HandleBuffer
[HandleIndex
],
607 ProtocolGuidArray
[ProtocolIndex
],
611 if (!EFI_ERROR (Status
)) {
612 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
613 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
614 Status
= CoreCloseProtocol (
615 HandleBuffer
[HandleIndex
],
616 ProtocolGuidArray
[ProtocolIndex
],
618 OpenInfo
[OpenInfoIndex
].ControllerHandle
622 if (OpenInfo
!= NULL
) {
623 CoreFreePool(OpenInfo
);
627 if (ProtocolGuidArray
!= NULL
) {
628 CoreFreePool(ProtocolGuidArray
);
632 if (HandleBuffer
!= NULL
) {
633 CoreFreePool (HandleBuffer
);
637 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
639 Status
= CoreUninstallProtocolInterface (
641 &gEfiLoadedImageDevicePathProtocolGuid
,
642 Image
->LoadedImageDevicePath
645 Status
= CoreUninstallProtocolInterface (
647 &gEfiLoadedImageProtocolGuid
,
653 if (Image
->RuntimeData
!= NULL
) {
654 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
656 // Remove the Image from the Runtime Image list as we are about to Free it!
658 RemoveEntryList (&Image
->RuntimeData
->Link
);
660 CoreFreePool (Image
->RuntimeData
);
664 // Free the Image from memory
666 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
667 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
671 // Done with the Image structure
673 if (Image
->Info
.FilePath
!= NULL
) {
674 CoreFreePool (Image
->Info
.FilePath
);
677 if (Image
->LoadedImageDevicePath
!= NULL
) {
678 CoreFreePool (Image
->LoadedImageDevicePath
);
681 if (Image
->FixupData
!= NULL
) {
682 CoreFreePool (Image
->FixupData
);
685 CoreFreePool (Image
);
690 Loads an EFI image into memory and returns a handle to the image.
692 @param BootPolicy If TRUE, indicates that the request originates
693 from the boot manager, and that the boot
694 manager is attempting to load FilePath as a
696 @param ParentImageHandle The caller's image handle.
697 @param FilePath The specific file path from which the image is
699 @param SourceBuffer If not NULL, a pointer to the memory location
700 containing a copy of the image to be loaded.
701 @param SourceSize The size in bytes of SourceBuffer.
702 @param DstBuffer The buffer to store the image
703 @param NumberOfPages If not NULL, it inputs a pointer to the page
704 number of DstBuffer and outputs a pointer to
705 the page number of the image. If this number is
706 not enough, return EFI_BUFFER_TOO_SMALL and
707 this parameter contains the required number.
708 @param ImageHandle Pointer to the returned image handle that is
709 created when the image is successfully loaded.
710 @param EntryPoint A pointer to the entry point
711 @param Attribute The bit mask of attributes to set for the load
714 @retval EFI_SUCCESS The image was loaded into memory.
715 @retval EFI_NOT_FOUND The FilePath was not found.
716 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
717 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
718 @retval EFI_UNSUPPORTED The image type is not supported, or the device
719 path cannot be parsed to locate the proper
720 protocol for loading the file.
721 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
726 CoreLoadImageCommon (
727 IN BOOLEAN BootPolicy
,
728 IN EFI_HANDLE ParentImageHandle
,
729 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
730 IN VOID
*SourceBuffer OPTIONAL
,
732 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
733 IN OUT UINTN
*NumberOfPages OPTIONAL
,
734 OUT EFI_HANDLE
*ImageHandle
,
735 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
739 LOADED_IMAGE_PRIVATE_DATA
*Image
;
740 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
741 IMAGE_FILE_HANDLE FHand
;
743 EFI_STATUS SecurityStatus
;
744 EFI_HANDLE DeviceHandle
;
745 UINT32 AuthenticationStatus
;
746 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
747 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
750 SecurityStatus
= EFI_SUCCESS
;
752 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
756 // The caller must pass in a valid ParentImageHandle
758 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
759 return EFI_INVALID_PARAMETER
;
762 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
763 if (ParentImage
== NULL
) {
764 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
765 return EFI_INVALID_PARAMETER
;
769 // Get simple read access to the source file
771 OriginalFilePath
= FilePath
;
772 Status
= CoreOpenImageFile (
779 &AuthenticationStatus
781 if (Status
== EFI_ALREADY_STARTED
) {
784 } else if (EFI_ERROR (Status
)) {
789 // Verify the Authentication Status through the Security Architectural Protocol
791 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
792 SecurityStatus
= gSecurity
->FileAuthenticationState (
794 AuthenticationStatus
,
797 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
798 Status
= SecurityStatus
;
806 // Allocate a new image structure
808 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
810 return EFI_OUT_OF_RESOURCES
;
814 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
816 FilePath
= OriginalFilePath
;
817 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
818 if (!EFI_ERROR (Status
)) {
819 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
820 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
824 // Initialize the fields for an internal driver
826 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
827 Image
->Info
.SystemTable
= gDxeCoreST
;
828 Image
->Info
.DeviceHandle
= DeviceHandle
;
829 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
830 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
831 Image
->Info
.ParentHandle
= ParentImageHandle
;
834 if (NumberOfPages
!= NULL
) {
835 Image
->NumberOfPages
= *NumberOfPages
;
837 Image
->NumberOfPages
= 0 ;
841 // Install the protocol interfaces for this image
842 // don't fire notifications yet
844 Status
= CoreInstallProtocolInterfaceNotify (
846 &gEfiLoadedImageProtocolGuid
,
847 EFI_NATIVE_INTERFACE
,
851 if (EFI_ERROR (Status
)) {
856 // Load the image. If EntryPoint is Null, it will not be set.
858 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
859 if (EFI_ERROR (Status
)) {
860 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
861 if (NumberOfPages
!= NULL
) {
862 *NumberOfPages
= Image
->NumberOfPages
;
868 if (NumberOfPages
!= NULL
) {
869 *NumberOfPages
= Image
->NumberOfPages
;
873 // Register the image in the Debug Image Info Table if the attribute is set
875 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
876 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
880 //Reinstall loaded image protocol to fire any notifications
882 Status
= CoreReinstallProtocolInterface (
884 &gEfiLoadedImageProtocolGuid
,
888 if (EFI_ERROR (Status
)) {
893 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
894 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
896 if (OriginalFilePath
!= NULL
) {
897 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
901 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
903 Status
= CoreInstallProtocolInterface (
905 &gEfiLoadedImageDevicePathProtocolGuid
,
906 EFI_NATIVE_INTERFACE
,
907 Image
->LoadedImageDevicePath
909 if (EFI_ERROR (Status
)) {
914 // Success. Return the image handle
916 *ImageHandle
= Image
->Handle
;
920 // All done accessing the source file
921 // If we allocated the Source buffer, free it
923 if (FHand
.FreeBuffer
) {
924 CoreFreePool (FHand
.Source
);
928 // There was an error. If there's an Image structure, free it
930 if (EFI_ERROR (Status
)) {
932 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
935 } else if (EFI_ERROR (SecurityStatus
)) {
936 Status
= SecurityStatus
;
946 Loads an EFI image into memory and returns a handle to the image.
948 @param BootPolicy If TRUE, indicates that the request originates
949 from the boot manager, and that the boot
950 manager is attempting to load FilePath as a
952 @param ParentImageHandle The caller's image handle.
953 @param FilePath The specific file path from which the image is
955 @param SourceBuffer If not NULL, a pointer to the memory location
956 containing a copy of the image to be loaded.
957 @param SourceSize The size in bytes of SourceBuffer.
958 @param ImageHandle Pointer to the returned image handle that is
959 created when the image is successfully loaded.
961 @retval EFI_SUCCESS The image was loaded into memory.
962 @retval EFI_NOT_FOUND The FilePath was not found.
963 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
964 @retval EFI_UNSUPPORTED The image type is not supported, or the device
965 path cannot be parsed to locate the proper
966 protocol for loading the file.
967 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
974 IN BOOLEAN BootPolicy
,
975 IN EFI_HANDLE ParentImageHandle
,
976 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
977 IN VOID
*SourceBuffer OPTIONAL
,
979 OUT EFI_HANDLE
*ImageHandle
987 Tick
= GetPerformanceCounter ();
990 Status
= CoreLoadImageCommon (
996 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1000 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1003 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1004 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1012 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1014 @param This Calling context
1015 @param ParentImageHandle The caller's image handle.
1016 @param FilePath The specific file path from which the image is
1018 @param SourceBuffer If not NULL, a pointer to the memory location
1019 containing a copy of the image to be loaded.
1020 @param SourceSize The size in bytes of SourceBuffer.
1021 @param DstBuffer The buffer to store the image.
1022 @param NumberOfPages For input, specifies the space size of the
1023 image by caller if not NULL. For output,
1024 specifies the actual space size needed.
1025 @param ImageHandle Image handle for output.
1026 @param EntryPoint Image entry point for output.
1027 @param Attribute The bit mask of attributes to set for the load
1030 @retval EFI_SUCCESS The image was loaded into memory.
1031 @retval EFI_NOT_FOUND The FilePath was not found.
1032 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1033 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1034 path cannot be parsed to locate the proper
1035 protocol for loading the file.
1036 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1043 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1044 IN EFI_HANDLE ParentImageHandle
,
1045 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1046 IN VOID
*SourceBuffer OPTIONAL
,
1047 IN UINTN SourceSize
,
1048 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1049 OUT UINTN
*NumberOfPages OPTIONAL
,
1050 OUT EFI_HANDLE
*ImageHandle
,
1051 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1055 return CoreLoadImageCommon (
1071 Transfer control to a loaded image's entry point.
1073 @param ImageHandle Handle of image to be started.
1074 @param ExitDataSize Pointer of the size to ExitData
1075 @param ExitData Pointer to a pointer to a data buffer that
1076 includes a Null-terminated Unicode string,
1077 optionally followed by additional binary data.
1078 The string is a description that the caller may
1079 use to further indicate the reason for the
1082 @retval EFI_INVALID_PARAMETER Invalid parameter
1083 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1084 @retval EFI_SUCCESS Successfully transfer control to the image's
1091 IN EFI_HANDLE ImageHandle
,
1092 OUT UINTN
*ExitDataSize
,
1093 OUT CHAR16
**ExitData OPTIONAL
1097 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1098 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1099 UINT64 HandleDatabaseKey
;
1102 Image
= CoreLoadedImageInfo (ImageHandle
);
1103 if (Image
== NULL
|| Image
->Started
) {
1104 return EFI_INVALID_PARAMETER
;
1108 // Don't profile Objects or invalid start requests
1110 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1114 // Push the current start image context, and
1115 // link the current image to the head. This is the
1116 // only image that can call Exit()
1118 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1119 LastImage
= mCurrentImage
;
1120 mCurrentImage
= Image
;
1121 Image
->Tpl
= gEfiCurrentTpl
;
1124 // Set long jump for Exit() support
1125 // JumpContext must be aligned on a CPU specific boundary.
1126 // Overallocate the buffer and force the required alignment
1128 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1129 if (Image
->JumpBuffer
== NULL
) {
1130 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1131 return EFI_OUT_OF_RESOURCES
;
1133 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1135 SetJumpFlag
= SetJump (Image
->JumpContext
);
1137 // The initial call to SetJump() must always return 0.
1138 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1140 if (SetJumpFlag
== 0) {
1142 // Call the image's entry point
1144 Image
->Started
= TRUE
;
1145 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1148 // Add some debug information if the image returned with error.
1149 // This make the user aware and check if the driver image have already released
1150 // all the resource in this situation.
1152 DEBUG_CODE_BEGIN ();
1153 if (EFI_ERROR (Image
->Status
)) {
1154 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1159 // If the image returns, exit it through Exit()
1161 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1165 // Image has completed. Verify the tpl is the same
1167 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1168 CoreRestoreTpl (Image
->Tpl
);
1170 CoreFreePool (Image
->JumpBuffer
);
1173 // Pop the current start image context
1175 mCurrentImage
= LastImage
;
1178 // Go connect any handles that were created or modified while the image executed.
1180 CoreConnectHandlesByKey (HandleDatabaseKey
);
1183 // Handle the image's returned ExitData
1185 DEBUG_CODE_BEGIN ();
1186 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1188 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1189 if (Image
->ExitData
!= NULL
) {
1190 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1192 DEBUG ((DEBUG_LOAD
, "\n"));
1197 // Return the exit data to the caller
1199 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1200 *ExitDataSize
= Image
->ExitDataSize
;
1201 *ExitData
= Image
->ExitData
;
1204 // Caller doesn't want the exit data, free it
1206 CoreFreePool (Image
->ExitData
);
1207 Image
->ExitData
= NULL
;
1211 // Save the Status because Image will get destroyed if it is unloaded.
1213 Status
= Image
->Status
;
1216 // If the image returned an error, or if the image is an application
1219 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1220 CoreUnloadAndCloseImage (Image
, TRUE
);
1226 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1231 Terminates the currently loaded EFI image and returns control to boot services.
1233 @param ImageHandle Handle that identifies the image. This
1234 parameter is passed to the image on entry.
1235 @param Status The image's exit code.
1236 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1237 ExitStatus is EFI_SUCCESS.
1238 @param ExitData Pointer to a data buffer that includes a
1239 Null-terminated Unicode string, optionally
1240 followed by additional binary data. The string
1241 is a description that the caller may use to
1242 further indicate the reason for the image's
1245 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1247 @retval EFI_SUCCESS Successfully terminates the currently loaded
1249 @retval EFI_ACCESS_DENIED Should never reach there.
1250 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1256 IN EFI_HANDLE ImageHandle
,
1257 IN EFI_STATUS Status
,
1258 IN UINTN ExitDataSize
,
1259 IN CHAR16
*ExitData OPTIONAL
1262 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1266 // Prevent possible reentrance to this function
1267 // for the same ImageHandle
1269 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1271 Image
= CoreLoadedImageInfo (ImageHandle
);
1272 if (Image
== NULL
) {
1273 Status
= EFI_INVALID_PARAMETER
;
1277 if (!Image
->Started
) {
1279 // The image has not been started so just free its resources
1281 CoreUnloadAndCloseImage (Image
, TRUE
);
1282 Status
= EFI_SUCCESS
;
1287 // Image has been started, verify this image can exit
1289 if (Image
!= mCurrentImage
) {
1290 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1291 Status
= EFI_INVALID_PARAMETER
;
1298 Image
->Status
= Status
;
1301 // If there's ExitData info, move it
1303 if (ExitData
!= NULL
) {
1304 Image
->ExitDataSize
= ExitDataSize
;
1305 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1306 if (Image
->ExitData
== NULL
) {
1307 Status
= EFI_OUT_OF_RESOURCES
;
1310 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1313 CoreRestoreTpl (OldTpl
);
1315 // return to StartImage
1317 LongJump (Image
->JumpContext
, (UINTN
)-1);
1320 // If we return from LongJump, then it is an error
1323 Status
= EFI_ACCESS_DENIED
;
1325 CoreRestoreTpl (OldTpl
);
1335 @param ImageHandle Handle that identifies the image to be
1338 @retval EFI_SUCCESS The image has been unloaded.
1339 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1341 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1347 IN EFI_HANDLE ImageHandle
1351 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1353 Image
= CoreLoadedImageInfo (ImageHandle
);
1354 if (Image
== NULL
) {
1356 // The image handle is not valid
1358 Status
= EFI_INVALID_PARAMETER
;
1362 if (Image
->Started
) {
1364 // The image has been started, request it to unload.
1366 Status
= EFI_UNSUPPORTED
;
1367 if (Image
->Info
.Unload
!= NULL
) {
1368 Status
= Image
->Info
.Unload (ImageHandle
);
1373 // This Image hasn't been started, thus it can be unloaded
1375 Status
= EFI_SUCCESS
;
1379 if (!EFI_ERROR (Status
)) {
1381 // if the Image was not started or Unloaded O.K. then clean up
1383 CoreUnloadAndCloseImage (Image
, TRUE
);
1393 Unload the specified image.
1395 @param This Indicates the calling context.
1396 @param ImageHandle The specified image handle.
1398 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1399 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1400 @retval EFI_SUCCESS Image successfully unloaded.
1406 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1407 IN EFI_HANDLE ImageHandle
1410 return CoreUnloadImage (ImageHandle
);