3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Core image handling services
27 LOADED_IMAGE_PRIVATE_DATA
*mCurrentImage
= NULL
;
29 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData
= {
30 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE
,
40 // This code is needed to build the Image handle for the DXE Core
42 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage
= {
43 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
, // Signature
45 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
, // Image type
46 TRUE
, // If entrypoint has been called
49 EFI_LOADED_IMAGE_INFORMATION_REVISION
, // Revision
50 NULL
, // Parent handle
51 NULL
, // System handle
53 NULL
, // Device handle
62 EfiBootServicesCode
, // ImageCodeType
63 EfiBootServicesData
// ImageDataType
65 (EFI_PHYSICAL_ADDRESS
)0, // ImageBasePage
69 EFI_SUCCESS
, // Status
81 CoreInitializeImageServices (
88 Add the Image Services to EFI Boot Services Table and install the protocol
89 interfaces for this image.
93 HobStart - The HOB to initialize
102 LOADED_IMAGE_PRIVATE_DATA
*Image
;
103 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
104 UINT64 DxeCoreImageLength
;
105 VOID
*DxeCoreEntryPoint
;
106 EFI_PEI_HOB_POINTERS DxeCoreHob
;
108 // Searching for image hob
110 DxeCoreHob
.Raw
= HobStart
;
111 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
112 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
118 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
120 ASSERT (DxeCoreHob
.Raw
!= NULL
);
122 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
123 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
124 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
125 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
127 // Initialize the fields for an internal driver
129 Image
= &mCorePrivateImage
;
131 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
132 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
133 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
134 Image
->Tpl
= gEfiCurrentTpl
;
135 Image
->Info
.SystemTable
= gST
;
136 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
137 Image
->Info
.ImageSize
= DxeCoreImageLength
;
140 // Install the protocol interfaces for this image
142 Status
= CoreInstallProtocolInterface (
144 &gEfiLoadedImageProtocolGuid
,
145 EFI_NATIVE_INTERFACE
,
148 ASSERT_EFI_ERROR (Status
);
150 mCurrentImage
= Image
;
153 // Fill in DXE globals
155 gDxeCoreImageHandle
= Image
->Handle
;
156 gDxeCoreLoadedImage
= &Image
->Info
;
159 // Export DXE Core PE Loader functionality
161 return CoreInstallProtocolInterface (
162 &mLoadPe32PrivateData
.Handle
,
163 &gEfiLoadPeImageProtocolGuid
,
164 EFI_NATIVE_INTERFACE
,
165 &mLoadPe32PrivateData
.Pe32Image
172 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
173 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
174 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
181 Loads, relocates, and invokes a PE/COFF image
185 Pe32Handle - The handle of PE32 image
186 Image - PE image to be loaded
187 DstBuffer - The buffer to store the image
188 EntryPoint - A pointer to the entry point
189 Attribute - The bit mask of attributes to set for the load PE image
193 EFI_SUCCESS - The file was loaded, relocated, and invoked
195 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
197 EFI_INVALID_PARAMETER - Invalid parameter
199 EFI_BUFFER_TOO_SMALL - Buffer for image is too small
204 BOOLEAN DstBufAlocated
;
207 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
209 Image
->ImageContext
.Handle
= Pe32Handle
;
210 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
213 // Get information about the image being loaded
215 Status
= gEfiPeiPeCoffLoader
->GetImageInfo (gEfiPeiPeCoffLoader
, &Image
->ImageContext
);
216 if (EFI_ERROR (Status
)) {
220 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
222 // The PE/COFF loader can support loading image types that can be executed.
223 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
225 return EFI_UNSUPPORTED
;
230 // Allocate memory of the correct memory type aligned on the required image boundry
232 DstBufAlocated
= FALSE
;
233 if (DstBuffer
== 0) {
235 // Allocate Destination Buffer as caller did not pass it in
238 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
239 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
241 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
244 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
247 // If the image relocations have not been stripped, then load at any address.
248 // Otherwise load at the address at which it was linked.
250 // Memory below 1MB should be treated reserved for CSM and there should be
251 // no modules whose preferred load addresses are below 1MB.
253 Status
= EFI_OUT_OF_RESOURCES
;
254 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
255 Status
= CoreAllocatePages (
257 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
258 Image
->NumberOfPages
,
259 &Image
->ImageContext
.ImageAddress
262 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
263 Status
= CoreAllocatePages (
265 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
266 Image
->NumberOfPages
,
267 &Image
->ImageContext
.ImageAddress
270 if (EFI_ERROR (Status
)) {
273 DstBufAlocated
= TRUE
;
276 // Caller provided the destination buffer
279 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
281 // If the image relocations were stripped, and the caller provided a
282 // destination buffer address that does not match the address that the
283 // image is linked at, then the image cannot be loaded.
285 return EFI_INVALID_PARAMETER
;
288 if (Image
->NumberOfPages
!= 0 &&
289 Image
->NumberOfPages
<
290 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
291 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
292 return EFI_BUFFER_TOO_SMALL
;
295 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
296 Image
->ImageContext
.ImageAddress
= DstBuffer
;
299 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
300 Image
->ImageContext
.ImageAddress
=
301 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
302 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
305 // Load the image from the file into the allocated memory
307 Status
= gEfiPeiPeCoffLoader
->LoadImage (gEfiPeiPeCoffLoader
, &Image
->ImageContext
);
308 if (EFI_ERROR (Status
)) {
313 // If this is a Runtime Driver, then allocate memory for the FixupData that
314 // is used to relocate the image when SetVirtualAddressMap() is called. The
315 // relocation is done by the Runtime AP.
317 if (Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) {
318 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
319 Image
->ImageContext
.FixupData
= CoreAllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
320 if (Image
->ImageContext
.FixupData
== NULL
) {
321 Status
= EFI_OUT_OF_RESOURCES
;
328 // Relocate the image in memory
330 Status
= gEfiPeiPeCoffLoader
->RelocateImage (gEfiPeiPeCoffLoader
, &Image
->ImageContext
);
331 if (EFI_ERROR (Status
)) {
336 // Flush the Instruction Cache
338 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
341 // Copy the machine type from the context to the image private data. This
342 // is needed during image unload to know if we should call an EBC protocol
343 // to unload the image.
345 Image
->Machine
= Image
->ImageContext
.Machine
;
348 // Get the image entry point. If it's an EBC image, then call into the
349 // interpreter to create a thunk for the entry point and use the returned
350 // value for the entry point.
352 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
353 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
355 // Locate the EBC interpreter protocol
357 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
358 if (EFI_ERROR(Status
)) {
363 // Register a callback for flushing the instruction cache so that created
364 // thunks can be flushed.
366 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
367 if (EFI_ERROR(Status
)) {
372 // Create a thunk for the image's entry point. This will be the new
373 // entry point for the image.
375 Status
= Image
->Ebc
->CreateThunk (
378 (VOID
*)(UINTN
)Image
->ImageContext
.EntryPoint
,
379 (VOID
**)&Image
->EntryPoint
381 if (EFI_ERROR(Status
)) {
387 // Fill in the image information for the Loaded Image Protocol
389 Image
->Type
= Image
->ImageContext
.ImageType
;
390 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
391 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
392 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
393 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
394 if (Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) {
395 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
397 // Make a list off all the RT images so we can let the RT AP know about them.
399 Image
->RuntimeData
= CoreAllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
400 if (Image
->RuntimeData
== NULL
) {
403 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
404 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
405 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
406 Image
->RuntimeData
->Handle
= Image
->Handle
;
407 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
412 // Fill in the entry point of the image if it is available
414 if (EntryPoint
!= NULL
) {
415 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
419 // Print the load address and the PDB file name if it is available
426 CHAR8 EfiFileName
[256];
428 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading driver at 0x%10p EntryPoint=0x%10p ", (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (VOID
*)(UINTN
)Image
->ImageContext
.EntryPoint
));
429 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
431 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
432 if (Image
->ImageContext
.PdbPointer
[Index
] == '\\') {
433 StartIndex
= Index
+ 1;
437 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
439 for (Index
= 0; Index
< sizeof (EfiFileName
); Index
++) {
440 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
441 if (EfiFileName
[Index
] == 0) {
442 EfiFileName
[Index
] = '.';
444 if (EfiFileName
[Index
] == '.') {
445 EfiFileName
[Index
+ 1] = 'e';
446 EfiFileName
[Index
+ 2] = 'f';
447 EfiFileName
[Index
+ 3] = 'i';
448 EfiFileName
[Index
+ 4] = 0;
452 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
454 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));
466 if (DstBufAlocated
) {
467 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
470 if (Image
->ImageContext
.FixupData
!= NULL
) {
471 CoreFreePool (Image
->ImageContext
.FixupData
);
478 LOADED_IMAGE_PRIVATE_DATA
*
479 CoreLoadedImageInfo (
480 IN EFI_HANDLE ImageHandle
486 Get the image's private data from its handle.
490 ImageHandle - The image handle
494 Return the image private data associated with ImageHandle.
499 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
500 LOADED_IMAGE_PRIVATE_DATA
*Image
;
502 Status
= CoreHandleProtocol (
504 &gEfiLoadedImageProtocolGuid
,
505 (VOID
**)&LoadedImage
507 if (!EFI_ERROR (Status
)) {
508 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
510 DEBUG ((EFI_D_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %x\n", ImageHandle
));
519 CoreLoadImageCommon (
520 IN BOOLEAN BootPolicy
,
521 IN EFI_HANDLE ParentImageHandle
,
522 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
523 IN VOID
*SourceBuffer OPTIONAL
,
525 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
526 IN OUT UINTN
*NumberOfPages OPTIONAL
,
527 OUT EFI_HANDLE
*ImageHandle
,
528 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
535 Loads an EFI image into memory and returns a handle to the image.
539 BootPolicy - If TRUE, indicates that the request originates from the boot manager,
540 and that the boot manager is attempting to load FilePath as a boot selection.
541 ParentImageHandle - The caller's image handle.
542 FilePath - The specific file path from which the image is loaded.
543 SourceBuffer - If not NULL, a pointer to the memory location containing a copy of
544 the image to be loaded.
545 SourceSize - The size in bytes of SourceBuffer.
546 DstBuffer - The buffer to store the image
547 NumberOfPages - If not NULL, a pointer to the image's page number, if this number
548 is not enough, return EFI_BUFFER_TOO_SMALL and this parameter contain
550 ImageHandle - Pointer to the returned image handle that is created when the image
551 is successfully loaded.
552 EntryPoint - A pointer to the entry point
553 Attribute - The bit mask of attributes to set for the load PE image
557 EFI_SUCCESS - The image was loaded into memory.
558 EFI_NOT_FOUND - The FilePath was not found.
559 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
560 EFI_BUFFER_TOO_SMALL - The buffer is too small
561 EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be
562 parsed to locate the proper protocol for loading the file.
563 EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.
566 LOADED_IMAGE_PRIVATE_DATA
*Image
;
567 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
568 IMAGE_FILE_HANDLE FHand
;
570 EFI_STATUS SecurityStatus
;
571 EFI_HANDLE DeviceHandle
;
572 UINT32 AuthenticationStatus
;
573 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
574 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
577 SecurityStatus
= EFI_SUCCESS
;
579 ASSERT (gEfiCurrentTpl
< EFI_TPL_NOTIFY
);
583 // The caller must pass in a valid ParentImageHandle
585 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
586 return EFI_INVALID_PARAMETER
;
589 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
590 if (ParentImage
== NULL
) {
591 DEBUG((EFI_D_LOAD
|EFI_D_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
592 return EFI_INVALID_PARAMETER
;
596 // Get simple read access to the source file
598 OriginalFilePath
= FilePath
;
599 Status
= CoreOpenImageFile (
606 &AuthenticationStatus
608 if (Status
== EFI_ALREADY_STARTED
) {
611 } else if (EFI_ERROR (Status
)) {
616 // Verify the Authentication Status through the Security Architectural Protocol
618 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
619 SecurityStatus
= gSecurity
->FileAuthenticationState (
621 AuthenticationStatus
,
624 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
625 Status
= SecurityStatus
;
633 // Allocate a new image structure
635 Image
= CoreAllocateZeroBootServicesPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
637 return EFI_OUT_OF_RESOURCES
;
641 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
643 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
644 if (!EFI_ERROR (Status
)) {
645 FilePathSize
= CoreDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
646 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) ( ((UINT8
*)FilePath
) + FilePathSize
);
650 // Initialize the fields for an internal driver
652 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
653 Image
->Info
.SystemTable
= gST
;
654 Image
->Info
.DeviceHandle
= DeviceHandle
;
655 Image
->Info
.Revision
= EFI_LOADED_IMAGE_INFORMATION_REVISION
;
656 Image
->Info
.FilePath
= CoreDuplicateDevicePath (FilePath
);
657 Image
->Info
.ParentHandle
= ParentImageHandle
;
659 if (NumberOfPages
!= NULL
) {
660 Image
->NumberOfPages
= *NumberOfPages
;
662 Image
->NumberOfPages
= 0 ;
666 // Install the protocol interfaces for this image
667 // don't fire notifications yet
669 Status
= CoreInstallProtocolInterfaceNotify (
671 &gEfiLoadedImageProtocolGuid
,
672 EFI_NATIVE_INTERFACE
,
676 if (EFI_ERROR (Status
)) {
681 // Load the image. If EntryPoint is Null, it will not be set.
683 Status
= CoreLoadPeImage (&FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
684 if (EFI_ERROR (Status
)) {
685 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
686 if (NumberOfPages
!= NULL
) {
687 *NumberOfPages
= Image
->NumberOfPages
;
694 // Register the image in the Debug Image Info Table if the attribute is set
696 if (Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) {
697 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
701 //Reinstall loaded image protocol to fire any notifications
703 Status
= CoreReinstallProtocolInterface (
705 &gEfiLoadedImageProtocolGuid
,
709 if (EFI_ERROR (Status
)) {
715 // Success. Return the image handle
717 *ImageHandle
= Image
->Handle
;
721 // All done accessing the source file
722 // If we allocated the Source buffer, free it
724 if (FHand
.FreeBuffer
) {
725 CoreFreePool (FHand
.Source
);
729 // There was an error. If there's an Image structure, free it
731 if (EFI_ERROR (Status
)) {
733 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
736 } else if (EFI_ERROR (SecurityStatus
)) {
737 Status
= SecurityStatus
;
748 IN BOOLEAN BootPolicy
,
749 IN EFI_HANDLE ParentImageHandle
,
750 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
751 IN VOID
*SourceBuffer OPTIONAL
,
753 OUT EFI_HANDLE
*ImageHandle
759 Loads an EFI image into memory and returns a handle to the image.
763 BootPolicy - If TRUE, indicates that the request originates from the boot manager,
764 and that the boot manager is attempting to load FilePath as a boot selection.
765 ParentImageHandle - The caller's image handle.
766 FilePath - The specific file path from which the image is loaded.
767 SourceBuffer - If not NULL, a pointer to the memory location containing a copy of
768 the image to be loaded.
769 SourceSize - The size in bytes of SourceBuffer.
770 ImageHandle - Pointer to the returned image handle that is created when the image
771 is successfully loaded.
775 EFI_SUCCESS - The image was loaded into memory.
776 EFI_NOT_FOUND - The FilePath was not found.
777 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
778 EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be
779 parsed to locate the proper protocol for loading the file.
780 EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.
785 PERF_START (NULL
, "LoadImage", NULL
, 0);
787 Status
= CoreLoadImageCommon (
793 (EFI_PHYSICAL_ADDRESS
)NULL
,
797 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
800 PERF_END (NULL
, "LoadImage", NULL
, 0);
809 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
810 IN EFI_HANDLE ParentImageHandle
,
811 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
812 IN VOID
*SourceBuffer OPTIONAL
,
814 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
815 OUT UINTN
*NumberOfPages OPTIONAL
,
816 OUT EFI_HANDLE
*ImageHandle
,
817 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
824 Loads an EFI image into memory and returns a handle to the image with extended parameters.
828 This - Calling context
829 ParentImageHandle - The caller's image handle.
830 FilePath - The specific file path from which the image is loaded.
831 SourceBuffer - If not NULL, a pointer to the memory location containing a copy of
832 the image to be loaded.
833 SourceSize - The size in bytes of SourceBuffer.
834 DstBuffer - The buffer to store the image.
835 NumberOfPages - For input, specifies the space size of the image by caller if not NULL.
836 For output, specifies the actual space size needed.
837 ImageHandle - Image handle for output.
838 EntryPoint - Image entry point for output.
839 Attribute - The bit mask of attributes to set for the load PE image.
843 EFI_SUCCESS - The image was loaded into memory.
844 EFI_NOT_FOUND - The FilePath was not found.
845 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
846 EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be
847 parsed to locate the proper protocol for loading the file.
848 EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.
851 return CoreLoadImageCommon (
871 IN EFI_HANDLE ImageHandle
,
872 OUT UINTN
*ExitDataSize
,
873 OUT CHAR16
**ExitData OPTIONAL
879 Transfer control to a loaded image's entry point.
883 ImageHandle - Handle of image to be started.
885 ExitDataSize - Pointer of the size to ExitData
887 ExitData - Pointer to a pointer to a data buffer that includes a Null-terminated
888 Unicode string, optionally followed by additional binary data. The string
889 is a description that the caller may use to further indicate the reason for
894 EFI_INVALID_PARAMETER - Invalid parameter
896 EFI_OUT_OF_RESOURCES - No enough buffer to allocate
898 EFI_SUCCESS - Successfully transfer control to the image's entry point.
903 LOADED_IMAGE_PRIVATE_DATA
*Image
;
904 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
905 UINT64 HandleDatabaseKey
;
908 Image
= CoreLoadedImageInfo (ImageHandle
);
909 if (Image
== NULL_HANDLE
|| Image
->Started
) {
910 return EFI_INVALID_PARAMETER
;
914 // Don't profile Objects or invalid start requests
916 PERF_START (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
920 // Push the current start image context, and
921 // link the current image to the head. This is the
922 // only image that can call Exit()
924 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
925 LastImage
= mCurrentImage
;
926 mCurrentImage
= Image
;
927 Image
->Tpl
= gEfiCurrentTpl
;
930 // Set long jump for Exit() support
931 // JumpContext must be aligned on a CPU specific boundary.
932 // Overallocate the buffer and force the required alignment
934 Image
->JumpBuffer
= CoreAllocateBootServicesPool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
935 if (Image
->JumpBuffer
== NULL
) {
936 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
937 return EFI_OUT_OF_RESOURCES
;
939 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
941 SetJumpFlag
= SetJump (Image
->JumpContext
);
943 // The initial call to SetJump() must always return 0.
944 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
948 // Call the image's entry point
950 Image
->Started
= TRUE
;
951 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
954 // Add some debug information if the image returned with error.
955 // This make the user aware and check if the driver image have already released
956 // all the resource in this situation.
959 if (EFI_ERROR (Image
->Status
)) {
960 DEBUG ((EFI_D_ERROR
, "Error: Image at %10p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
965 // If the image returns, exit it through Exit()
967 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
971 // Image has completed. Verify the tpl is the same
973 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
974 CoreRestoreTpl (Image
->Tpl
);
976 CoreFreePool (Image
->JumpBuffer
);
979 // Pop the current start image context
981 mCurrentImage
= LastImage
;
984 // Go connect any handles that were created or modified while the image executed.
986 CoreConnectHandlesByKey (HandleDatabaseKey
);
989 // Handle the image's returned ExitData
992 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
996 "StartImage: ExitDataSize %d, ExitData %x",
1000 if (Image
->ExitData
!= NULL
) {
1001 DEBUG ((EFI_D_LOAD
, " (%hs)", Image
->ExitData
));
1003 DEBUG ((EFI_D_LOAD
, "\n"));
1008 // Return the exit data to the caller
1010 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1011 *ExitDataSize
= Image
->ExitDataSize
;
1012 *ExitData
= Image
->ExitData
;
1015 // Caller doesn't want the exit data, free it
1017 CoreFreePool (Image
->ExitData
);
1018 Image
->ExitData
= NULL
;
1022 // Save the Status because Image will get destroyed if it is unloaded.
1024 Status
= Image
->Status
;
1027 // If the image returned an error, or if the image is an application
1030 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1031 CoreUnloadAndCloseImage (Image
, TRUE
);
1037 PERF_END (ImageHandle
, START_IMAGE_TOK
, NULL
, 0);
1043 CoreUnloadAndCloseImage (
1044 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
1049 Routine Description:
1051 Unloads EFI image from memory.
1056 FreePage - Free allocated pages
1066 EFI_HANDLE
*HandleBuffer
;
1068 EFI_GUID
**ProtocolGuidArray
;
1070 UINTN ProtocolIndex
;
1071 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
1072 UINTN OpenInfoCount
;
1073 UINTN OpenInfoIndex
;
1075 if (Image
->Ebc
!= NULL
) {
1077 // If EBC protocol exists we must perform cleanups for this image.
1079 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
1083 // Unload image, free Image->ImageContext->ModHandle
1085 gEfiPeiPeCoffLoader
->UnloadImage (gEfiPeiPeCoffLoader
, &Image
->ImageContext
);
1088 // Free our references to the image handle
1090 if (Image
->Handle
!= NULL_HANDLE
) {
1092 Status
= CoreLocateHandleBuffer (
1099 if (!EFI_ERROR (Status
)) {
1100 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
1101 Status
= CoreProtocolsPerHandle (
1102 HandleBuffer
[HandleIndex
],
1106 if (!EFI_ERROR (Status
)) {
1107 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
1108 Status
= CoreOpenProtocolInformation (
1109 HandleBuffer
[HandleIndex
],
1110 ProtocolGuidArray
[ProtocolIndex
],
1114 if (!EFI_ERROR (Status
)) {
1115 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
1116 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
1117 Status
= CoreCloseProtocol (
1118 HandleBuffer
[HandleIndex
],
1119 ProtocolGuidArray
[ProtocolIndex
],
1121 OpenInfo
[OpenInfoIndex
].ControllerHandle
1125 if (OpenInfo
!= NULL
) {
1126 CoreFreePool(OpenInfo
);
1130 if (ProtocolGuidArray
!= NULL
) {
1131 CoreFreePool(ProtocolGuidArray
);
1135 if (HandleBuffer
!= NULL
) {
1136 CoreFreePool (HandleBuffer
);
1140 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
1142 Status
= CoreUninstallProtocolInterface (
1144 &gEfiLoadedImageProtocolGuid
,
1149 if (Image
->RuntimeData
!= NULL
) {
1150 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
1152 // Remove the Image from the Runtime Image list as we are about to Free it!
1154 RemoveEntryList (&Image
->RuntimeData
->Link
);
1156 CoreFreePool (Image
->RuntimeData
);
1160 // Free the Image from memory
1162 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
1163 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
1167 // Done with the Image structure
1169 if (Image
->Info
.FilePath
!= NULL
) {
1170 CoreFreePool (Image
->Info
.FilePath
);
1173 if (Image
->FixupData
!= NULL
) {
1174 CoreFreePool (Image
->FixupData
);
1177 CoreFreePool (Image
);
1185 IN EFI_HANDLE ImageHandle
,
1186 IN EFI_STATUS Status
,
1187 IN UINTN ExitDataSize
,
1188 IN CHAR16
*ExitData OPTIONAL
1192 Routine Description:
1194 Terminates the currently loaded EFI image and returns control to boot services.
1198 ImageHandle - Handle that identifies the image. This parameter is passed to the image
1200 Status - The image's exit code.
1201 ExitDataSize - The size, in bytes, of ExitData. Ignored if ExitStatus is
1203 ExitData - Pointer to a data buffer that includes a Null-terminated Unicode string,
1204 optionally followed by additional binary data. The string is a
1205 description that the caller may use to further indicate the reason for
1210 EFI_INVALID_PARAMETER - Image handle is NULL or it is not current image.
1212 EFI_SUCCESS - Successfully terminates the currently loaded EFI image.
1214 EFI_ACCESS_DENIED - Should never reach there.
1216 EFI_OUT_OF_RESOURCES - Could not allocate pool
1220 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1224 // Prevent possible reentrance to this function
1225 // for the same ImageHandle
1227 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
1229 Image
= CoreLoadedImageInfo (ImageHandle
);
1230 if (Image
== NULL_HANDLE
) {
1231 Status
= EFI_INVALID_PARAMETER
;
1235 if (!Image
->Started
) {
1237 // The image has not been started so just free its resources
1239 CoreUnloadAndCloseImage (Image
, TRUE
);
1240 Status
= EFI_SUCCESS
;
1245 // Image has been started, verify this image can exit
1247 if (Image
!= mCurrentImage
) {
1248 DEBUG ((EFI_D_LOAD
|EFI_D_ERROR
, "Exit: Image is not exitable image\n"));
1249 Status
= EFI_INVALID_PARAMETER
;
1256 Image
->Status
= Status
;
1259 // If there's ExitData info, move it
1261 if (ExitData
!= NULL
) {
1262 Image
->ExitDataSize
= ExitDataSize
;
1263 Image
->ExitData
= CoreAllocateBootServicesPool (Image
->ExitDataSize
);
1264 if (Image
->ExitData
== NULL
) {
1265 Status
= EFI_OUT_OF_RESOURCES
;
1268 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1271 CoreRestoreTpl (OldTpl
);
1273 // return to StartImage
1275 LongJump (Image
->JumpContext
, (UINTN
)-1);
1278 // If we return from LongJump, then it is an error
1281 Status
= EFI_ACCESS_DENIED
;
1283 CoreRestoreTpl (OldTpl
);
1292 IN EFI_HANDLE ImageHandle
1296 Routine Description:
1302 ImageHandle - Handle that identifies the image to be unloaded.
1306 EFI_SUCCESS - The image has been unloaded.
1307 EFI_UNSUPPORTED - The image has been sarted, and does not support unload.
1308 EFI_INVALID_PARAMPETER - ImageHandle is not a valid image handle.
1313 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1317 // Prevent possible reentrance to this function
1318 // for the same ImageHandle
1320 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
1322 Image
= CoreLoadedImageInfo (ImageHandle
);
1323 if (Image
== NULL
) {
1325 // The image handle is not valid
1327 Status
= EFI_INVALID_PARAMETER
;
1331 if (Image
->Started
) {
1333 // The image has been started, request it to unload.
1335 Status
= EFI_UNSUPPORTED
;
1336 if (Image
->Info
.Unload
!= NULL
) {
1337 Status
= Image
->Info
.Unload (ImageHandle
);
1342 // This Image hasn't been started, thus it can be unloaded
1344 Status
= EFI_SUCCESS
;
1348 if (!EFI_ERROR (Status
)) {
1350 // if the Image was not started or Unloaded O.K. then clean up
1352 CoreUnloadAndCloseImage (Image
, TRUE
);
1356 CoreRestoreTpl (OldTpl
);
1364 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1365 IN EFI_HANDLE ImageHandle
1369 Routine Description:
1371 Unload the specified image.
1375 This - Indicates the calling context.
1377 ImageHandle - The specified image handle.
1381 EFI_INVALID_PARAMETER - Image handle is NULL.
1383 EFI_UNSUPPORTED - Attempt to unload an unsupported image.
1385 EFI_SUCCESS - Image successfully unloaded.
1389 return CoreUnloadImage (ImageHandle
);