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 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
196 Image
->ImageContext
.Handle
= Pe32Handle
;
197 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
200 // Get information about the image being loaded
202 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
203 if (EFI_ERROR (Status
)) {
207 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
208 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
210 // The PE/COFF loader can support loading image types that can be executed.
211 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
213 return EFI_UNSUPPORTED
;
218 // Set EFI memory type based on ImageType
220 switch (Image
->ImageContext
.ImageType
) {
221 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
222 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
223 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
225 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
226 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
227 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
229 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
230 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
231 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
232 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
235 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
236 return EFI_UNSUPPORTED
;
240 // Allocate memory of the correct memory type aligned on the required image boundry
242 DstBufAlocated
= FALSE
;
243 if (DstBuffer
== 0) {
245 // Allocate Destination Buffer as caller did not pass it in
248 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
249 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
251 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
254 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
257 // If the image relocations have not been stripped, then load at any address.
258 // Otherwise load at the address at which it was linked.
260 // Memory below 1MB should be treated reserved for CSM and there should be
261 // no modules whose preferred load addresses are below 1MB.
263 Status
= EFI_OUT_OF_RESOURCES
;
264 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
265 Status
= CoreAllocatePages (
267 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
268 Image
->NumberOfPages
,
269 &Image
->ImageContext
.ImageAddress
272 if (EFI_ERROR (Status
) && !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
)) {
283 DstBufAlocated
= TRUE
;
286 // Caller provided the destination buffer
289 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
291 // If the image relocations were stripped, and the caller provided a
292 // destination buffer address that does not match the address that the
293 // image is linked at, then the image cannot be loaded.
295 return EFI_INVALID_PARAMETER
;
298 if (Image
->NumberOfPages
!= 0 &&
299 Image
->NumberOfPages
<
300 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
301 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
302 return EFI_BUFFER_TOO_SMALL
;
305 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
306 Image
->ImageContext
.ImageAddress
= DstBuffer
;
309 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
310 if (!Image
->ImageContext
.IsTeImage
) {
311 Image
->ImageContext
.ImageAddress
=
312 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
313 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
317 // Load the image from the file into the allocated memory
319 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
320 if (EFI_ERROR (Status
)) {
325 // If this is a Runtime Driver, then allocate memory for the FixupData that
326 // is used to relocate the image when SetVirtualAddressMap() is called. The
327 // relocation is done by the Runtime AP.
329 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
330 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
331 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
332 if (Image
->ImageContext
.FixupData
== NULL
) {
333 Status
= EFI_OUT_OF_RESOURCES
;
340 // Relocate the image in memory
342 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
343 if (EFI_ERROR (Status
)) {
348 // Flush the Instruction Cache
350 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
353 // Copy the machine type from the context to the image private data. This
354 // is needed during image unload to know if we should call an EBC protocol
355 // to unload the image.
357 Image
->Machine
= Image
->ImageContext
.Machine
;
360 // Get the image entry point. If it's an EBC image, then call into the
361 // interpreter to create a thunk for the entry point and use the returned
362 // value for the entry point.
364 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
365 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
367 // Locate the EBC interpreter protocol
369 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
370 if (EFI_ERROR(Status
)) {
371 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
376 // Register a callback for flushing the instruction cache so that created
377 // thunks can be flushed.
379 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
380 if (EFI_ERROR(Status
)) {
385 // Create a thunk for the image's entry point. This will be the new
386 // entry point for the image.
388 Status
= Image
->Ebc
->CreateThunk (
391 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
392 (VOID
**) &Image
->EntryPoint
394 if (EFI_ERROR(Status
)) {
400 // Fill in the image information for the Loaded Image Protocol
402 Image
->Type
= Image
->ImageContext
.ImageType
;
403 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
404 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
405 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
406 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
407 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
408 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
410 // Make a list off all the RT images so we can let the RT AP know about them.
412 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
413 if (Image
->RuntimeData
== NULL
) {
416 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
417 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
418 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
419 Image
->RuntimeData
->Handle
= Image
->Handle
;
420 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
425 // Fill in the entry point of the image if it is available
427 if (EntryPoint
!= NULL
) {
428 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
432 // Print the load address and the PDB file name if it is available
439 CHAR8 EfiFileName
[256];
442 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
443 "Loading driver at 0x%11p EntryPoint=0x%11p ",
444 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
445 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
449 // Print Module Name by Pdb file path.
450 // Windows and Unix style file path are all trimmed correctly.
452 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
454 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
455 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
456 StartIndex
= Index
+ 1;
460 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
461 // The PDB file name is limited in the range of 0~255.
462 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
464 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
465 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
466 if (EfiFileName
[Index
] == 0) {
467 EfiFileName
[Index
] = '.';
469 if (EfiFileName
[Index
] == '.') {
470 EfiFileName
[Index
+ 1] = 'e';
471 EfiFileName
[Index
+ 2] = 'f';
472 EfiFileName
[Index
+ 3] = 'i';
473 EfiFileName
[Index
+ 4] = 0;
478 if (Index
== sizeof (EfiFileName
) - 4) {
479 EfiFileName
[Index
] = 0;
481 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
483 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
495 if (DstBufAlocated
) {
496 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
499 if (Image
->ImageContext
.FixupData
!= NULL
) {
500 CoreFreePool (Image
->ImageContext
.FixupData
);
509 Get the image's private data from its handle.
511 @param ImageHandle The image handle
513 @return Return the image private data associated with ImageHandle.
516 LOADED_IMAGE_PRIVATE_DATA
*
517 CoreLoadedImageInfo (
518 IN EFI_HANDLE ImageHandle
522 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
523 LOADED_IMAGE_PRIVATE_DATA
*Image
;
525 Status
= CoreHandleProtocol (
527 &gEfiLoadedImageProtocolGuid
,
528 (VOID
**)&LoadedImage
530 if (!EFI_ERROR (Status
)) {
531 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
533 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
542 Unloads EFI image from memory.
544 @param Image EFI image
545 @param FreePage Free allocated pages
549 CoreUnloadAndCloseImage (
550 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
556 EFI_HANDLE
*HandleBuffer
;
558 EFI_GUID
**ProtocolGuidArray
;
561 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
565 if (Image
->Ebc
!= NULL
) {
567 // If EBC protocol exists we must perform cleanups for this image.
569 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
573 // Unload image, free Image->ImageContext->ModHandle
575 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
578 // Free our references to the image handle
580 if (Image
->Handle
!= NULL
) {
582 Status
= CoreLocateHandleBuffer (
589 if (!EFI_ERROR (Status
)) {
590 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
591 Status
= CoreProtocolsPerHandle (
592 HandleBuffer
[HandleIndex
],
596 if (!EFI_ERROR (Status
)) {
597 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
598 Status
= CoreOpenProtocolInformation (
599 HandleBuffer
[HandleIndex
],
600 ProtocolGuidArray
[ProtocolIndex
],
604 if (!EFI_ERROR (Status
)) {
605 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
606 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
607 Status
= CoreCloseProtocol (
608 HandleBuffer
[HandleIndex
],
609 ProtocolGuidArray
[ProtocolIndex
],
611 OpenInfo
[OpenInfoIndex
].ControllerHandle
615 if (OpenInfo
!= NULL
) {
616 CoreFreePool(OpenInfo
);
620 if (ProtocolGuidArray
!= NULL
) {
621 CoreFreePool(ProtocolGuidArray
);
625 if (HandleBuffer
!= NULL
) {
626 CoreFreePool (HandleBuffer
);
630 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
632 Status
= CoreUninstallProtocolInterface (
634 &gEfiLoadedImageDevicePathProtocolGuid
,
635 Image
->LoadedImageDevicePath
638 Status
= CoreUninstallProtocolInterface (
640 &gEfiLoadedImageProtocolGuid
,
644 if (Image
->ImageContext
.HiiResourceData
!= 0) {
645 Status
= CoreUninstallProtocolInterface (
647 &gEfiHiiPackageListProtocolGuid
,
648 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
654 if (Image
->RuntimeData
!= NULL
) {
655 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
657 // Remove the Image from the Runtime Image list as we are about to Free it!
659 RemoveEntryList (&Image
->RuntimeData
->Link
);
661 CoreFreePool (Image
->RuntimeData
);
665 // Free the Image from memory
667 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
668 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
672 // Done with the Image structure
674 if (Image
->Info
.FilePath
!= NULL
) {
675 CoreFreePool (Image
->Info
.FilePath
);
678 if (Image
->LoadedImageDevicePath
!= NULL
) {
679 CoreFreePool (Image
->LoadedImageDevicePath
);
682 if (Image
->FixupData
!= NULL
) {
683 CoreFreePool (Image
->FixupData
);
686 CoreFreePool (Image
);
691 Loads an EFI image into memory and returns a handle to the image.
693 @param BootPolicy If TRUE, indicates that the request originates
694 from the boot manager, and that the boot
695 manager is attempting to load FilePath as a
697 @param ParentImageHandle The caller's image handle.
698 @param FilePath The specific file path from which the image is
700 @param SourceBuffer If not NULL, a pointer to the memory location
701 containing a copy of the image to be loaded.
702 @param SourceSize The size in bytes of SourceBuffer.
703 @param DstBuffer The buffer to store the image
704 @param NumberOfPages If not NULL, it inputs a pointer to the page
705 number of DstBuffer and outputs a pointer to
706 the page number of the image. If this number is
707 not enough, return EFI_BUFFER_TOO_SMALL and
708 this parameter contains the required number.
709 @param ImageHandle Pointer to the returned image handle that is
710 created when the image is successfully loaded.
711 @param EntryPoint A pointer to the entry point
712 @param Attribute The bit mask of attributes to set for the load
715 @retval EFI_SUCCESS The image was loaded into memory.
716 @retval EFI_NOT_FOUND The FilePath was not found.
717 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
718 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
719 @retval EFI_UNSUPPORTED The image type is not supported, or the device
720 path cannot be parsed to locate the proper
721 protocol for loading the file.
722 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
727 CoreLoadImageCommon (
728 IN BOOLEAN BootPolicy
,
729 IN EFI_HANDLE ParentImageHandle
,
730 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
731 IN VOID
*SourceBuffer OPTIONAL
,
733 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
734 IN OUT UINTN
*NumberOfPages OPTIONAL
,
735 OUT EFI_HANDLE
*ImageHandle
,
736 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
740 LOADED_IMAGE_PRIVATE_DATA
*Image
;
741 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
742 IMAGE_FILE_HANDLE FHand
;
744 EFI_STATUS SecurityStatus
;
745 EFI_HANDLE DeviceHandle
;
746 UINT32 AuthenticationStatus
;
747 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
748 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
751 SecurityStatus
= EFI_SUCCESS
;
753 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
757 // The caller must pass in a valid ParentImageHandle
759 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
760 return EFI_INVALID_PARAMETER
;
763 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
764 if (ParentImage
== NULL
) {
765 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
766 return EFI_INVALID_PARAMETER
;
770 // Get simple read access to the source file
772 OriginalFilePath
= FilePath
;
773 Status
= CoreOpenImageFile (
780 &AuthenticationStatus
782 if (Status
== EFI_ALREADY_STARTED
) {
785 } else if (EFI_ERROR (Status
)) {
790 // Verify the Authentication Status through the Security Architectural Protocol
792 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
793 SecurityStatus
= gSecurity
->FileAuthenticationState (
795 AuthenticationStatus
,
798 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
799 Status
= SecurityStatus
;
807 // Allocate a new image structure
809 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
811 return EFI_OUT_OF_RESOURCES
;
815 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
817 FilePath
= OriginalFilePath
;
818 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
819 if (!EFI_ERROR (Status
)) {
820 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
821 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
825 // Initialize the fields for an internal driver
827 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
828 Image
->Info
.SystemTable
= gDxeCoreST
;
829 Image
->Info
.DeviceHandle
= DeviceHandle
;
830 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
831 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
832 Image
->Info
.ParentHandle
= ParentImageHandle
;
835 if (NumberOfPages
!= NULL
) {
836 Image
->NumberOfPages
= *NumberOfPages
;
838 Image
->NumberOfPages
= 0 ;
842 // Install the protocol interfaces for this image
843 // don't fire notifications yet
845 Status
= CoreInstallProtocolInterfaceNotify (
847 &gEfiLoadedImageProtocolGuid
,
848 EFI_NATIVE_INTERFACE
,
852 if (EFI_ERROR (Status
)) {
857 // Load the image. If EntryPoint is Null, it will not be set.
859 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
860 if (EFI_ERROR (Status
)) {
861 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
862 if (NumberOfPages
!= NULL
) {
863 *NumberOfPages
= Image
->NumberOfPages
;
869 if (NumberOfPages
!= NULL
) {
870 *NumberOfPages
= Image
->NumberOfPages
;
874 // Register the image in the Debug Image Info Table if the attribute is set
876 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
877 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
881 //Reinstall loaded image protocol to fire any notifications
883 Status
= CoreReinstallProtocolInterface (
885 &gEfiLoadedImageProtocolGuid
,
889 if (EFI_ERROR (Status
)) {
894 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
895 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
897 if (OriginalFilePath
!= NULL
) {
898 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
902 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
904 Status
= CoreInstallProtocolInterface (
906 &gEfiLoadedImageDevicePathProtocolGuid
,
907 EFI_NATIVE_INTERFACE
,
908 Image
->LoadedImageDevicePath
910 if (EFI_ERROR (Status
)) {
915 // Install HII Package List Protocol onto the image handle
917 if (Image
->ImageContext
.HiiResourceData
!= 0) {
918 Status
= CoreInstallProtocolInterface (
920 &gEfiHiiPackageListProtocolGuid
,
921 EFI_NATIVE_INTERFACE
,
922 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
924 if (EFI_ERROR (Status
)) {
930 // Success. Return the image handle
932 *ImageHandle
= Image
->Handle
;
936 // All done accessing the source file
937 // If we allocated the Source buffer, free it
939 if (FHand
.FreeBuffer
) {
940 CoreFreePool (FHand
.Source
);
944 // There was an error. If there's an Image structure, free it
946 if (EFI_ERROR (Status
)) {
948 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
951 } else if (EFI_ERROR (SecurityStatus
)) {
952 Status
= SecurityStatus
;
962 Loads an EFI image into memory and returns a handle to the image.
964 @param BootPolicy If TRUE, indicates that the request originates
965 from the boot manager, and that the boot
966 manager is attempting to load FilePath as a
968 @param ParentImageHandle The caller's image handle.
969 @param FilePath The specific file path from which the image is
971 @param SourceBuffer If not NULL, a pointer to the memory location
972 containing a copy of the image to be loaded.
973 @param SourceSize The size in bytes of SourceBuffer.
974 @param ImageHandle Pointer to the returned image handle that is
975 created when the image is successfully loaded.
977 @retval EFI_SUCCESS The image was loaded into memory.
978 @retval EFI_NOT_FOUND The FilePath was not found.
979 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
980 @retval EFI_UNSUPPORTED The image type is not supported, or the device
981 path cannot be parsed to locate the proper
982 protocol for loading the file.
983 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
990 IN BOOLEAN BootPolicy
,
991 IN EFI_HANDLE ParentImageHandle
,
992 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
993 IN VOID
*SourceBuffer OPTIONAL
,
995 OUT EFI_HANDLE
*ImageHandle
1003 Tick
= GetPerformanceCounter ();
1006 Status
= CoreLoadImageCommon (
1012 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1016 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1019 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1020 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1028 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1030 @param This Calling context
1031 @param ParentImageHandle The caller's image handle.
1032 @param FilePath The specific file path from which the image is
1034 @param SourceBuffer If not NULL, a pointer to the memory location
1035 containing a copy of the image to be loaded.
1036 @param SourceSize The size in bytes of SourceBuffer.
1037 @param DstBuffer The buffer to store the image.
1038 @param NumberOfPages For input, specifies the space size of the
1039 image by caller if not NULL. For output,
1040 specifies the actual space size needed.
1041 @param ImageHandle Image handle for output.
1042 @param EntryPoint Image entry point for output.
1043 @param Attribute The bit mask of attributes to set for the load
1046 @retval EFI_SUCCESS The image was loaded into memory.
1047 @retval EFI_NOT_FOUND The FilePath was not found.
1048 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1049 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1050 path cannot be parsed to locate the proper
1051 protocol for loading the file.
1052 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1059 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1060 IN EFI_HANDLE ParentImageHandle
,
1061 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1062 IN VOID
*SourceBuffer OPTIONAL
,
1063 IN UINTN SourceSize
,
1064 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1065 OUT UINTN
*NumberOfPages OPTIONAL
,
1066 OUT EFI_HANDLE
*ImageHandle
,
1067 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1071 return CoreLoadImageCommon (
1087 Transfer control to a loaded image's entry point.
1089 @param ImageHandle Handle of image to be started.
1090 @param ExitDataSize Pointer of the size to ExitData
1091 @param ExitData Pointer to a pointer to a data buffer that
1092 includes a Null-terminated Unicode string,
1093 optionally followed by additional binary data.
1094 The string is a description that the caller may
1095 use to further indicate the reason for the
1098 @retval EFI_INVALID_PARAMETER Invalid parameter
1099 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1100 @retval EFI_SUCCESS Successfully transfer control to the image's
1107 IN EFI_HANDLE ImageHandle
,
1108 OUT UINTN
*ExitDataSize
,
1109 OUT CHAR16
**ExitData OPTIONAL
1113 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1114 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1115 UINT64 HandleDatabaseKey
;
1118 Image
= CoreLoadedImageInfo (ImageHandle
);
1119 if (Image
== NULL
|| Image
->Started
) {
1120 return EFI_INVALID_PARAMETER
;
1124 // Don't profile Objects or invalid start requests
1126 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1130 // Push the current start image context, and
1131 // link the current image to the head. This is the
1132 // only image that can call Exit()
1134 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1135 LastImage
= mCurrentImage
;
1136 mCurrentImage
= Image
;
1137 Image
->Tpl
= gEfiCurrentTpl
;
1140 // Set long jump for Exit() support
1141 // JumpContext must be aligned on a CPU specific boundary.
1142 // Overallocate the buffer and force the required alignment
1144 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1145 if (Image
->JumpBuffer
== NULL
) {
1146 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1147 return EFI_OUT_OF_RESOURCES
;
1149 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1151 SetJumpFlag
= SetJump (Image
->JumpContext
);
1153 // The initial call to SetJump() must always return 0.
1154 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1156 if (SetJumpFlag
== 0) {
1158 // Call the image's entry point
1160 Image
->Started
= TRUE
;
1161 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1164 // Add some debug information if the image returned with error.
1165 // This make the user aware and check if the driver image have already released
1166 // all the resource in this situation.
1168 DEBUG_CODE_BEGIN ();
1169 if (EFI_ERROR (Image
->Status
)) {
1170 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1175 // If the image returns, exit it through Exit()
1177 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1181 // Image has completed. Verify the tpl is the same
1183 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1184 CoreRestoreTpl (Image
->Tpl
);
1186 CoreFreePool (Image
->JumpBuffer
);
1189 // Pop the current start image context
1191 mCurrentImage
= LastImage
;
1194 // Go connect any handles that were created or modified while the image executed.
1196 CoreConnectHandlesByKey (HandleDatabaseKey
);
1199 // Handle the image's returned ExitData
1201 DEBUG_CODE_BEGIN ();
1202 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1204 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1205 if (Image
->ExitData
!= NULL
) {
1206 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1208 DEBUG ((DEBUG_LOAD
, "\n"));
1213 // Return the exit data to the caller
1215 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1216 *ExitDataSize
= Image
->ExitDataSize
;
1217 *ExitData
= Image
->ExitData
;
1220 // Caller doesn't want the exit data, free it
1222 CoreFreePool (Image
->ExitData
);
1223 Image
->ExitData
= NULL
;
1227 // Save the Status because Image will get destroyed if it is unloaded.
1229 Status
= Image
->Status
;
1232 // If the image returned an error, or if the image is an application
1235 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1236 CoreUnloadAndCloseImage (Image
, TRUE
);
1242 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1247 Terminates the currently loaded EFI image and returns control to boot services.
1249 @param ImageHandle Handle that identifies the image. This
1250 parameter is passed to the image on entry.
1251 @param Status The image's exit code.
1252 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1253 ExitStatus is EFI_SUCCESS.
1254 @param ExitData Pointer to a data buffer that includes a
1255 Null-terminated Unicode string, optionally
1256 followed by additional binary data. The string
1257 is a description that the caller may use to
1258 further indicate the reason for the image's
1261 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1263 @retval EFI_SUCCESS Successfully terminates the currently loaded
1265 @retval EFI_ACCESS_DENIED Should never reach there.
1266 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1272 IN EFI_HANDLE ImageHandle
,
1273 IN EFI_STATUS Status
,
1274 IN UINTN ExitDataSize
,
1275 IN CHAR16
*ExitData OPTIONAL
1278 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1282 // Prevent possible reentrance to this function
1283 // for the same ImageHandle
1285 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1287 Image
= CoreLoadedImageInfo (ImageHandle
);
1288 if (Image
== NULL
) {
1289 Status
= EFI_INVALID_PARAMETER
;
1293 if (!Image
->Started
) {
1295 // The image has not been started so just free its resources
1297 CoreUnloadAndCloseImage (Image
, TRUE
);
1298 Status
= EFI_SUCCESS
;
1303 // Image has been started, verify this image can exit
1305 if (Image
!= mCurrentImage
) {
1306 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1307 Status
= EFI_INVALID_PARAMETER
;
1314 Image
->Status
= Status
;
1317 // If there's ExitData info, move it
1319 if (ExitData
!= NULL
) {
1320 Image
->ExitDataSize
= ExitDataSize
;
1321 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1322 if (Image
->ExitData
== NULL
) {
1323 Status
= EFI_OUT_OF_RESOURCES
;
1326 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1329 CoreRestoreTpl (OldTpl
);
1331 // return to StartImage
1333 LongJump (Image
->JumpContext
, (UINTN
)-1);
1336 // If we return from LongJump, then it is an error
1339 Status
= EFI_ACCESS_DENIED
;
1341 CoreRestoreTpl (OldTpl
);
1351 @param ImageHandle Handle that identifies the image to be
1354 @retval EFI_SUCCESS The image has been unloaded.
1355 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1357 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1363 IN EFI_HANDLE ImageHandle
1367 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1369 Image
= CoreLoadedImageInfo (ImageHandle
);
1370 if (Image
== NULL
) {
1372 // The image handle is not valid
1374 Status
= EFI_INVALID_PARAMETER
;
1378 if (Image
->Started
) {
1380 // The image has been started, request it to unload.
1382 Status
= EFI_UNSUPPORTED
;
1383 if (Image
->Info
.Unload
!= NULL
) {
1384 Status
= Image
->Info
.Unload (ImageHandle
);
1389 // This Image hasn't been started, thus it can be unloaded
1391 Status
= EFI_SUCCESS
;
1395 if (!EFI_ERROR (Status
)) {
1397 // if the Image was not started or Unloaded O.K. then clean up
1399 CoreUnloadAndCloseImage (Image
, TRUE
);
1409 Unload the specified image.
1411 @param This Indicates the calling context.
1412 @param ImageHandle The specified image handle.
1414 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1415 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1416 @retval EFI_SUCCESS Image successfully unloaded.
1422 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1423 IN EFI_HANDLE ImageHandle
1426 return CoreUnloadImage (ImageHandle
);