2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2009, 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
74 // The field is define for Loading modules at fixed address feature to tracker the PEI code
75 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding memory page
78 GLOBAL_REMOVE_IF_UNREFERENCED UINT64
*mDxeCodeMemoryRangeUsageBitMap
=NULL
;
81 Add the Image Services to EFI Boot Services Table and install the protocol
82 interfaces for this image.
84 @param HobStart The HOB to initialize
90 CoreInitializeImageServices (
95 LOADED_IMAGE_PRIVATE_DATA
*Image
;
96 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress
;
97 UINT64 DxeCoreImageLength
;
98 VOID
*DxeCoreEntryPoint
;
99 EFI_PEI_HOB_POINTERS DxeCoreHob
;
101 // Searching for image hob
103 DxeCoreHob
.Raw
= HobStart
;
104 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
105 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
111 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
113 ASSERT (DxeCoreHob
.Raw
!= NULL
);
115 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
116 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
117 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
118 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
120 // Initialize the fields for an internal driver
122 Image
= &mCorePrivateImage
;
124 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
125 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
126 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
127 Image
->Tpl
= gEfiCurrentTpl
;
128 Image
->Info
.SystemTable
= gDxeCoreST
;
129 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
130 Image
->Info
.ImageSize
= DxeCoreImageLength
;
133 // Install the protocol interfaces for this image
135 Status
= CoreInstallProtocolInterface (
137 &gEfiLoadedImageProtocolGuid
,
138 EFI_NATIVE_INTERFACE
,
141 ASSERT_EFI_ERROR (Status
);
143 mCurrentImage
= Image
;
146 // Fill in DXE globals
148 gDxeCoreImageHandle
= Image
->Handle
;
149 gDxeCoreLoadedImage
= &Image
->Info
;
151 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
153 // Export DXE Core PE Loader functionality for backward compatibility.
155 Status
= CoreInstallProtocolInterface (
156 &mLoadPe32PrivateData
.Handle
,
157 &gEfiLoadPeImageProtocolGuid
,
158 EFI_NATIVE_INTERFACE
,
159 &mLoadPe32PrivateData
.Pe32Image
167 Read image file (specified by UserHandle) into user specified buffer with specified offset
170 @param UserHandle Image file handle
171 @param Offset Offset to the source file
172 @param ReadSize For input, pointer of size to read; For output,
173 pointer of size actually read.
174 @param Buffer Buffer to write into
176 @retval EFI_SUCCESS Successfully read the specified part of file
185 IN OUT UINTN
*ReadSize
,
190 IMAGE_FILE_HANDLE
*FHand
;
192 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
193 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
196 // Move data from our local copy of the file
198 EndPosition
= Offset
+ *ReadSize
;
199 if (EndPosition
> FHand
->SourceSize
) {
200 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
202 if (Offset
>= FHand
->SourceSize
) {
206 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
210 To check memory usage bit map arry to figure out if the memory range the image will be loaded in is available or not. If
211 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
212 The function is only invoked when load modules at fixed address feature is enabled.
214 @param ImageBase The base addres the image will be loaded at.
215 @param ImageSize The size of the image
217 @retval EFI_SUCCESS The memory range the image will be loaded in is available
218 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
221 CheckAndMarkFixLoadingMemoryUsageBitMap (
222 IN EFI_PHYSICAL_ADDRESS ImageBase
,
226 UINT32 DxeCodePageNumber
;
228 EFI_PHYSICAL_ADDRESS DxeCodeBase
;
229 UINTN BaseOffsetPageNumber
;
230 UINTN TopOffsetPageNumber
;
233 // The DXE code range includes RuntimeCodePage range and Boot time code range.
235 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
236 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
237 DxeCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
);
238 DxeCodeBase
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- DxeCodeSize
;
241 // If the memory usage bit map is not initialized, do it. Every bit in the array
242 // indicate the status of the corresponding memory page, available or not
244 if (mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
245 mDxeCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((DxeCodePageNumber
/64) + 1)*sizeof(UINT64
));
248 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
250 if (!gLoadFixedAddressCodeMemoryReady
|| mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
251 return EFI_NOT_FOUND
;
254 // Test the memory range for loading the image in the DXE code range.
256 if (gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
< ImageBase
+ ImageSize
||
257 DxeCodeBase
> ImageBase
) {
258 return EFI_NOT_FOUND
;
261 // Test if the memory is avalaible or not.
263 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- DxeCodeBase
));
264 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- DxeCodeBase
));
265 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
266 if ((mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
268 // This page is already used.
270 return EFI_NOT_FOUND
;
275 // Being here means the memory range is available. So mark the bits for the memory range
277 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
278 mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
284 Get the fixed loadding address from image header assigned by build tool. This function only be called
285 when Loading module at Fixed address feature enabled.
287 @param ImageContext Pointer to the image context structure that describes the PE/COFF
288 image that needs to be examined by this function.
289 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
290 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
294 GetPeCoffImageFixLoadingAssignedAddress(
295 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
298 UINTN SectionHeaderOffset
;
300 EFI_IMAGE_SECTION_HEADER SectionHeader
;
301 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
304 UINT16 NumberOfSections
;
305 IMAGE_FILE_HANDLE
*Handle
;
306 UINT64 ValueInSectionHeader
;
309 Status
= EFI_NOT_FOUND
;
312 // Get PeHeader pointer
314 Handle
= (IMAGE_FILE_HANDLE
*)ImageContext
->Handle
;
315 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )Handle
->Source
+ ImageContext
->PeCoffHeaderOffset
);
316 SectionHeaderOffset
= (UINTN
)(
317 ImageContext
->PeCoffHeaderOffset
+
319 sizeof (EFI_IMAGE_FILE_HEADER
) +
320 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
322 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
325 // Get base address from the first section header that doesn't point to code section.
327 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
329 // Read section header from file
331 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
332 Status
= ImageContext
->ImageRead (
333 ImageContext
->Handle
,
338 if (EFI_ERROR (Status
)) {
342 Status
= EFI_NOT_FOUND
;
344 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
346 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
347 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
348 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
349 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero
351 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
352 if (ValueInSectionHeader
!= 0) {
354 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
355 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
356 // relative to top address
358 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) < 0) {
359 ImageContext
->ImageAddress
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
+ (INT64
)ImageContext
->ImageAddress
;
362 // Check if the memory range is avaliable.
364 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext
->ImageAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
368 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
370 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID
*)(UINTN
)(ImageContext
->ImageAddress
), Status
));
374 Loads, relocates, and invokes a PE/COFF image
376 @param BootPolicy If TRUE, indicates that the request originates
377 from the boot manager, and that the boot
378 manager is attempting to load FilePath as a
380 @param Pe32Handle The handle of PE32 image
381 @param Image PE image to be loaded
382 @param DstBuffer The buffer to store the image
383 @param EntryPoint A pointer to the entry point
384 @param Attribute The bit mask of attributes to set for the load
387 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
388 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
389 relocate the PE/COFF file
390 @retval EFI_INVALID_PARAMETER Invalid parameter
391 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
396 IN BOOLEAN BootPolicy
,
398 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
399 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
400 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
405 BOOLEAN DstBufAlocated
;
408 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
410 Image
->ImageContext
.Handle
= Pe32Handle
;
411 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
414 // Get information about the image being loaded
416 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
417 if (EFI_ERROR (Status
)) {
421 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
422 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
424 // The PE/COFF loader can support loading image types that can be executed.
425 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
427 return EFI_UNSUPPORTED
;
432 // Set EFI memory type based on ImageType
434 switch (Image
->ImageContext
.ImageType
) {
435 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
436 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
437 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
439 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
440 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
441 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
443 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
444 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
445 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
446 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
449 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
450 return EFI_UNSUPPORTED
;
454 // Allocate memory of the correct memory type aligned on the required image boundry
456 DstBufAlocated
= FALSE
;
457 if (DstBuffer
== 0) {
459 // Allocate Destination Buffer as caller did not pass it in
462 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
463 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
465 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
468 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
471 // If the image relocations have not been stripped, then load at any address.
472 // Otherwise load at the address at which it was linked.
474 // Memory below 1MB should be treated reserved for CSM and there should be
475 // no modules whose preferred load addresses are below 1MB.
477 Status
= EFI_OUT_OF_RESOURCES
;
479 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
480 // a specified address.
482 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 ) {
483 Status
= GetPeCoffImageFixLoadingAssignedAddress (&(Image
->ImageContext
));
485 if (EFI_ERROR (Status
)) {
487 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
489 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
491 Status
= CoreAllocatePages (
493 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
494 Image
->NumberOfPages
,
495 &Image
->ImageContext
.ImageAddress
499 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
500 Status
= CoreAllocatePages (
502 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
503 Image
->NumberOfPages
,
504 &Image
->ImageContext
.ImageAddress
507 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
508 Status
= CoreAllocatePages (
510 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
511 Image
->NumberOfPages
,
512 &Image
->ImageContext
.ImageAddress
516 if (EFI_ERROR (Status
)) {
519 DstBufAlocated
= TRUE
;
522 // Caller provided the destination buffer
525 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
527 // If the image relocations were stripped, and the caller provided a
528 // destination buffer address that does not match the address that the
529 // image is linked at, then the image cannot be loaded.
531 return EFI_INVALID_PARAMETER
;
534 if (Image
->NumberOfPages
!= 0 &&
535 Image
->NumberOfPages
<
536 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
537 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
538 return EFI_BUFFER_TOO_SMALL
;
541 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
542 Image
->ImageContext
.ImageAddress
= DstBuffer
;
545 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
546 if (!Image
->ImageContext
.IsTeImage
) {
547 Image
->ImageContext
.ImageAddress
=
548 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
549 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
553 // Load the image from the file into the allocated memory
555 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
556 if (EFI_ERROR (Status
)) {
561 // If this is a Runtime Driver, then allocate memory for the FixupData that
562 // is used to relocate the image when SetVirtualAddressMap() is called. The
563 // relocation is done by the Runtime AP.
565 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
566 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
567 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
568 if (Image
->ImageContext
.FixupData
== NULL
) {
569 Status
= EFI_OUT_OF_RESOURCES
;
576 // Relocate the image in memory
578 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
579 if (EFI_ERROR (Status
)) {
584 // Flush the Instruction Cache
586 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
589 // Copy the machine type from the context to the image private data. This
590 // is needed during image unload to know if we should call an EBC protocol
591 // to unload the image.
593 Image
->Machine
= Image
->ImageContext
.Machine
;
596 // Get the image entry point. If it's an EBC image, then call into the
597 // interpreter to create a thunk for the entry point and use the returned
598 // value for the entry point.
600 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
601 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
603 // Locate the EBC interpreter protocol
605 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
606 if (EFI_ERROR(Status
)) {
607 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
612 // Register a callback for flushing the instruction cache so that created
613 // thunks can be flushed.
615 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
616 if (EFI_ERROR(Status
)) {
621 // Create a thunk for the image's entry point. This will be the new
622 // entry point for the image.
624 Status
= Image
->Ebc
->CreateThunk (
627 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
628 (VOID
**) &Image
->EntryPoint
630 if (EFI_ERROR(Status
)) {
636 // Fill in the image information for the Loaded Image Protocol
638 Image
->Type
= Image
->ImageContext
.ImageType
;
639 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
640 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
641 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
642 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
643 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
644 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
646 // Make a list off all the RT images so we can let the RT AP know about them.
648 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
649 if (Image
->RuntimeData
== NULL
) {
652 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
653 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
654 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
655 Image
->RuntimeData
->Handle
= Image
->Handle
;
656 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
661 // Fill in the entry point of the image if it is available
663 if (EntryPoint
!= NULL
) {
664 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
668 // Print the load address and the PDB file name if it is available
675 CHAR8 EfiFileName
[256];
678 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
679 "Loading driver at 0x%11p EntryPoint=0x%11p ",
680 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
681 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
685 // Print Module Name by Pdb file path.
686 // Windows and Unix style file path are all trimmed correctly.
688 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
690 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
691 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
692 StartIndex
= Index
+ 1;
696 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
697 // The PDB file name is limited in the range of 0~255.
698 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
700 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
701 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
702 if (EfiFileName
[Index
] == 0) {
703 EfiFileName
[Index
] = '.';
705 if (EfiFileName
[Index
] == '.') {
706 EfiFileName
[Index
+ 1] = 'e';
707 EfiFileName
[Index
+ 2] = 'f';
708 EfiFileName
[Index
+ 3] = 'i';
709 EfiFileName
[Index
+ 4] = 0;
714 if (Index
== sizeof (EfiFileName
) - 4) {
715 EfiFileName
[Index
] = 0;
717 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
719 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
731 if (DstBufAlocated
) {
732 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
735 if (Image
->ImageContext
.FixupData
!= NULL
) {
736 CoreFreePool (Image
->ImageContext
.FixupData
);
745 Get the image's private data from its handle.
747 @param ImageHandle The image handle
749 @return Return the image private data associated with ImageHandle.
752 LOADED_IMAGE_PRIVATE_DATA
*
753 CoreLoadedImageInfo (
754 IN EFI_HANDLE ImageHandle
758 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
759 LOADED_IMAGE_PRIVATE_DATA
*Image
;
761 Status
= CoreHandleProtocol (
763 &gEfiLoadedImageProtocolGuid
,
764 (VOID
**)&LoadedImage
766 if (!EFI_ERROR (Status
)) {
767 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
769 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
778 Unloads EFI image from memory.
780 @param Image EFI image
781 @param FreePage Free allocated pages
785 CoreUnloadAndCloseImage (
786 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
792 EFI_HANDLE
*HandleBuffer
;
794 EFI_GUID
**ProtocolGuidArray
;
797 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
801 if (Image
->Ebc
!= NULL
) {
803 // If EBC protocol exists we must perform cleanups for this image.
805 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
809 // Unload image, free Image->ImageContext->ModHandle
811 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
814 // Free our references to the image handle
816 if (Image
->Handle
!= NULL
) {
818 Status
= CoreLocateHandleBuffer (
825 if (!EFI_ERROR (Status
)) {
826 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
827 Status
= CoreProtocolsPerHandle (
828 HandleBuffer
[HandleIndex
],
832 if (!EFI_ERROR (Status
)) {
833 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
834 Status
= CoreOpenProtocolInformation (
835 HandleBuffer
[HandleIndex
],
836 ProtocolGuidArray
[ProtocolIndex
],
840 if (!EFI_ERROR (Status
)) {
841 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
842 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
843 Status
= CoreCloseProtocol (
844 HandleBuffer
[HandleIndex
],
845 ProtocolGuidArray
[ProtocolIndex
],
847 OpenInfo
[OpenInfoIndex
].ControllerHandle
851 if (OpenInfo
!= NULL
) {
852 CoreFreePool(OpenInfo
);
856 if (ProtocolGuidArray
!= NULL
) {
857 CoreFreePool(ProtocolGuidArray
);
861 if (HandleBuffer
!= NULL
) {
862 CoreFreePool (HandleBuffer
);
866 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
868 Status
= CoreUninstallProtocolInterface (
870 &gEfiLoadedImageDevicePathProtocolGuid
,
871 Image
->LoadedImageDevicePath
874 Status
= CoreUninstallProtocolInterface (
876 &gEfiLoadedImageProtocolGuid
,
880 if (Image
->ImageContext
.HiiResourceData
!= 0) {
881 Status
= CoreUninstallProtocolInterface (
883 &gEfiHiiPackageListProtocolGuid
,
884 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
890 if (Image
->RuntimeData
!= NULL
) {
891 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
893 // Remove the Image from the Runtime Image list as we are about to Free it!
895 RemoveEntryList (&Image
->RuntimeData
->Link
);
897 CoreFreePool (Image
->RuntimeData
);
901 // Free the Image from memory
903 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
904 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
908 // Done with the Image structure
910 if (Image
->Info
.FilePath
!= NULL
) {
911 CoreFreePool (Image
->Info
.FilePath
);
914 if (Image
->LoadedImageDevicePath
!= NULL
) {
915 CoreFreePool (Image
->LoadedImageDevicePath
);
918 if (Image
->FixupData
!= NULL
) {
919 CoreFreePool (Image
->FixupData
);
922 CoreFreePool (Image
);
927 Loads an EFI image into memory and returns a handle to the image.
929 @param BootPolicy If TRUE, indicates that the request originates
930 from the boot manager, and that the boot
931 manager is attempting to load FilePath as a
933 @param ParentImageHandle The caller's image handle.
934 @param FilePath The specific file path from which the image is
936 @param SourceBuffer If not NULL, a pointer to the memory location
937 containing a copy of the image to be loaded.
938 @param SourceSize The size in bytes of SourceBuffer.
939 @param DstBuffer The buffer to store the image
940 @param NumberOfPages If not NULL, it inputs a pointer to the page
941 number of DstBuffer and outputs a pointer to
942 the page number of the image. If this number is
943 not enough, return EFI_BUFFER_TOO_SMALL and
944 this parameter contains the required number.
945 @param ImageHandle Pointer to the returned image handle that is
946 created when the image is successfully loaded.
947 @param EntryPoint A pointer to the entry point
948 @param Attribute The bit mask of attributes to set for the load
951 @retval EFI_SUCCESS The image was loaded into memory.
952 @retval EFI_NOT_FOUND The FilePath was not found.
953 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
954 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
955 @retval EFI_UNSUPPORTED The image type is not supported, or the device
956 path cannot be parsed to locate the proper
957 protocol for loading the file.
958 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
963 CoreLoadImageCommon (
964 IN BOOLEAN BootPolicy
,
965 IN EFI_HANDLE ParentImageHandle
,
966 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
967 IN VOID
*SourceBuffer OPTIONAL
,
969 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
970 IN OUT UINTN
*NumberOfPages OPTIONAL
,
971 OUT EFI_HANDLE
*ImageHandle
,
972 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
976 LOADED_IMAGE_PRIVATE_DATA
*Image
;
977 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
978 IMAGE_FILE_HANDLE FHand
;
980 EFI_STATUS SecurityStatus
;
981 EFI_HANDLE DeviceHandle
;
982 UINT32 AuthenticationStatus
;
983 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
984 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
987 SecurityStatus
= EFI_SUCCESS
;
989 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
993 // The caller must pass in a valid ParentImageHandle
995 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
996 return EFI_INVALID_PARAMETER
;
999 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
1000 if (ParentImage
== NULL
) {
1001 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
1002 return EFI_INVALID_PARAMETER
;
1005 ZeroMem (&FHand
, sizeof (IMAGE_FILE_HANDLE
));
1006 FHand
.Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
1007 OriginalFilePath
= FilePath
;
1008 HandleFilePath
= FilePath
;
1009 DeviceHandle
= NULL
;
1010 Status
= EFI_SUCCESS
;
1011 AuthenticationStatus
= 0;
1013 // If the caller passed a copy of the file, then just use it
1015 if (SourceBuffer
!= NULL
) {
1016 FHand
.Source
= SourceBuffer
;
1017 FHand
.SourceSize
= SourceSize
;
1018 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1019 if (SourceSize
> 0) {
1020 Status
= EFI_SUCCESS
;
1022 Status
= EFI_LOAD_ERROR
;
1025 if (FilePath
== NULL
) {
1026 return EFI_INVALID_PARAMETER
;
1029 // Get the source file buffer by its device path.
1031 FHand
.Source
= GetFileBufferByFilePath (
1035 &AuthenticationStatus
1037 if (FHand
.Source
== NULL
) {
1038 Status
= EFI_NOT_FOUND
;
1041 // Try to get the image device handle by checking the match protocol.
1043 FHand
.FreeBuffer
= TRUE
;
1044 Status
= CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1045 if (EFI_ERROR (Status
)) {
1046 HandleFilePath
= FilePath
;
1047 Status
= CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1048 if (EFI_ERROR (Status
)) {
1050 HandleFilePath
= FilePath
;
1051 Status
= CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1053 if (EFI_ERROR (Status
)) {
1054 HandleFilePath
= FilePath
;
1055 Status
= CoreLocateDevicePath (&gEfiLoadFileProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1062 if (Status
== EFI_ALREADY_STARTED
) {
1065 } else if (EFI_ERROR (Status
)) {
1070 // Verify the Authentication Status through the Security Architectural Protocol
1072 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
1073 SecurityStatus
= gSecurity
->FileAuthenticationState (
1075 AuthenticationStatus
,
1078 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
1079 Status
= SecurityStatus
;
1087 // Allocate a new image structure
1089 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
1090 if (Image
== NULL
) {
1091 return EFI_OUT_OF_RESOURCES
;
1095 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1097 FilePath
= OriginalFilePath
;
1098 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
1099 if (!EFI_ERROR (Status
)) {
1100 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1101 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
1105 // Initialize the fields for an internal driver
1107 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
1108 Image
->Info
.SystemTable
= gDxeCoreST
;
1109 Image
->Info
.DeviceHandle
= DeviceHandle
;
1110 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1111 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
1112 Image
->Info
.ParentHandle
= ParentImageHandle
;
1115 if (NumberOfPages
!= NULL
) {
1116 Image
->NumberOfPages
= *NumberOfPages
;
1118 Image
->NumberOfPages
= 0 ;
1122 // Install the protocol interfaces for this image
1123 // don't fire notifications yet
1125 Status
= CoreInstallProtocolInterfaceNotify (
1127 &gEfiLoadedImageProtocolGuid
,
1128 EFI_NATIVE_INTERFACE
,
1132 if (EFI_ERROR (Status
)) {
1137 // Load the image. If EntryPoint is Null, it will not be set.
1139 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
1140 if (EFI_ERROR (Status
)) {
1141 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
1142 if (NumberOfPages
!= NULL
) {
1143 *NumberOfPages
= Image
->NumberOfPages
;
1149 if (NumberOfPages
!= NULL
) {
1150 *NumberOfPages
= Image
->NumberOfPages
;
1154 // Register the image in the Debug Image Info Table if the attribute is set
1156 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
1157 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
1161 //Reinstall loaded image protocol to fire any notifications
1163 Status
= CoreReinstallProtocolInterface (
1165 &gEfiLoadedImageProtocolGuid
,
1169 if (EFI_ERROR (Status
)) {
1174 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1175 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1177 if (OriginalFilePath
!= NULL
) {
1178 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
1182 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1184 Status
= CoreInstallProtocolInterface (
1186 &gEfiLoadedImageDevicePathProtocolGuid
,
1187 EFI_NATIVE_INTERFACE
,
1188 Image
->LoadedImageDevicePath
1190 if (EFI_ERROR (Status
)) {
1195 // Install HII Package List Protocol onto the image handle
1197 if (Image
->ImageContext
.HiiResourceData
!= 0) {
1198 Status
= CoreInstallProtocolInterface (
1200 &gEfiHiiPackageListProtocolGuid
,
1201 EFI_NATIVE_INTERFACE
,
1202 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
1204 if (EFI_ERROR (Status
)) {
1210 // Success. Return the image handle
1212 *ImageHandle
= Image
->Handle
;
1216 // All done accessing the source file
1217 // If we allocated the Source buffer, free it
1219 if (FHand
.FreeBuffer
) {
1220 CoreFreePool (FHand
.Source
);
1224 // There was an error. If there's an Image structure, free it
1226 if (EFI_ERROR (Status
)) {
1227 if (Image
!= NULL
) {
1228 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
1229 *ImageHandle
= NULL
;
1231 } else if (EFI_ERROR (SecurityStatus
)) {
1232 Status
= SecurityStatus
;
1242 Loads an EFI image into memory and returns a handle to the image.
1244 @param BootPolicy If TRUE, indicates that the request originates
1245 from the boot manager, and that the boot
1246 manager is attempting to load FilePath as a
1248 @param ParentImageHandle The caller's image handle.
1249 @param FilePath The specific file path from which the image is
1251 @param SourceBuffer If not NULL, a pointer to the memory location
1252 containing a copy of the image to be loaded.
1253 @param SourceSize The size in bytes of SourceBuffer.
1254 @param ImageHandle Pointer to the returned image handle that is
1255 created when the image is successfully loaded.
1257 @retval EFI_SUCCESS The image was loaded into memory.
1258 @retval EFI_NOT_FOUND The FilePath was not found.
1259 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1260 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1261 path cannot be parsed to locate the proper
1262 protocol for loading the file.
1263 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1270 IN BOOLEAN BootPolicy
,
1271 IN EFI_HANDLE ParentImageHandle
,
1272 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1273 IN VOID
*SourceBuffer OPTIONAL
,
1274 IN UINTN SourceSize
,
1275 OUT EFI_HANDLE
*ImageHandle
1283 Tick
= GetPerformanceCounter ();
1286 Status
= CoreLoadImageCommon (
1292 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1296 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1299 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1300 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1308 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1310 @param This Calling context
1311 @param ParentImageHandle The caller's image handle.
1312 @param FilePath The specific file path from which the image is
1314 @param SourceBuffer If not NULL, a pointer to the memory location
1315 containing a copy of the image to be loaded.
1316 @param SourceSize The size in bytes of SourceBuffer.
1317 @param DstBuffer The buffer to store the image.
1318 @param NumberOfPages For input, specifies the space size of the
1319 image by caller if not NULL. For output,
1320 specifies the actual space size needed.
1321 @param ImageHandle Image handle for output.
1322 @param EntryPoint Image entry point for output.
1323 @param Attribute The bit mask of attributes to set for the load
1326 @retval EFI_SUCCESS The image was loaded into memory.
1327 @retval EFI_NOT_FOUND The FilePath was not found.
1328 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1329 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1330 path cannot be parsed to locate the proper
1331 protocol for loading the file.
1332 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1339 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1340 IN EFI_HANDLE ParentImageHandle
,
1341 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1342 IN VOID
*SourceBuffer OPTIONAL
,
1343 IN UINTN SourceSize
,
1344 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1345 OUT UINTN
*NumberOfPages OPTIONAL
,
1346 OUT EFI_HANDLE
*ImageHandle
,
1347 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1351 return CoreLoadImageCommon (
1367 Transfer control to a loaded image's entry point.
1369 @param ImageHandle Handle of image to be started.
1370 @param ExitDataSize Pointer of the size to ExitData
1371 @param ExitData Pointer to a pointer to a data buffer that
1372 includes a Null-terminated Unicode string,
1373 optionally followed by additional binary data.
1374 The string is a description that the caller may
1375 use to further indicate the reason for the
1378 @retval EFI_INVALID_PARAMETER Invalid parameter
1379 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1380 @retval EFI_SUCCESS Successfully transfer control to the image's
1387 IN EFI_HANDLE ImageHandle
,
1388 OUT UINTN
*ExitDataSize
,
1389 OUT CHAR16
**ExitData OPTIONAL
1393 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1394 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1395 UINT64 HandleDatabaseKey
;
1398 Image
= CoreLoadedImageInfo (ImageHandle
);
1399 if (Image
== NULL
|| Image
->Started
) {
1400 return EFI_INVALID_PARAMETER
;
1404 // The image to be started must have the machine type supported by DxeCore.
1406 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
));
1407 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1408 return EFI_UNSUPPORTED
;
1412 // Don't profile Objects or invalid start requests
1414 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1418 // Push the current start image context, and
1419 // link the current image to the head. This is the
1420 // only image that can call Exit()
1422 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1423 LastImage
= mCurrentImage
;
1424 mCurrentImage
= Image
;
1425 Image
->Tpl
= gEfiCurrentTpl
;
1428 // Set long jump for Exit() support
1429 // JumpContext must be aligned on a CPU specific boundary.
1430 // Overallocate the buffer and force the required alignment
1432 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1433 if (Image
->JumpBuffer
== NULL
) {
1434 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1435 return EFI_OUT_OF_RESOURCES
;
1437 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1439 SetJumpFlag
= SetJump (Image
->JumpContext
);
1441 // The initial call to SetJump() must always return 0.
1442 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1444 if (SetJumpFlag
== 0) {
1446 // Call the image's entry point
1448 Image
->Started
= TRUE
;
1449 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1452 // Add some debug information if the image returned with error.
1453 // This make the user aware and check if the driver image have already released
1454 // all the resource in this situation.
1456 DEBUG_CODE_BEGIN ();
1457 if (EFI_ERROR (Image
->Status
)) {
1458 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1463 // If the image returns, exit it through Exit()
1465 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1469 // Image has completed. Verify the tpl is the same
1471 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1472 CoreRestoreTpl (Image
->Tpl
);
1474 CoreFreePool (Image
->JumpBuffer
);
1477 // Pop the current start image context
1479 mCurrentImage
= LastImage
;
1482 // Go connect any handles that were created or modified while the image executed.
1484 CoreConnectHandlesByKey (HandleDatabaseKey
);
1487 // Handle the image's returned ExitData
1489 DEBUG_CODE_BEGIN ();
1490 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1492 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1493 if (Image
->ExitData
!= NULL
) {
1494 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1496 DEBUG ((DEBUG_LOAD
, "\n"));
1501 // Return the exit data to the caller
1503 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1504 *ExitDataSize
= Image
->ExitDataSize
;
1505 *ExitData
= Image
->ExitData
;
1508 // Caller doesn't want the exit data, free it
1510 CoreFreePool (Image
->ExitData
);
1511 Image
->ExitData
= NULL
;
1515 // Save the Status because Image will get destroyed if it is unloaded.
1517 Status
= Image
->Status
;
1520 // If the image returned an error, or if the image is an application
1523 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1524 CoreUnloadAndCloseImage (Image
, TRUE
);
1530 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1535 Terminates the currently loaded EFI image and returns control to boot services.
1537 @param ImageHandle Handle that identifies the image. This
1538 parameter is passed to the image on entry.
1539 @param Status The image's exit code.
1540 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1541 ExitStatus is EFI_SUCCESS.
1542 @param ExitData Pointer to a data buffer that includes a
1543 Null-terminated Unicode string, optionally
1544 followed by additional binary data. The string
1545 is a description that the caller may use to
1546 further indicate the reason for the image's
1549 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1551 @retval EFI_SUCCESS Successfully terminates the currently loaded
1553 @retval EFI_ACCESS_DENIED Should never reach there.
1554 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1560 IN EFI_HANDLE ImageHandle
,
1561 IN EFI_STATUS Status
,
1562 IN UINTN ExitDataSize
,
1563 IN CHAR16
*ExitData OPTIONAL
1566 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1570 // Prevent possible reentrance to this function
1571 // for the same ImageHandle
1573 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1575 Image
= CoreLoadedImageInfo (ImageHandle
);
1576 if (Image
== NULL
) {
1577 Status
= EFI_INVALID_PARAMETER
;
1581 if (!Image
->Started
) {
1583 // The image has not been started so just free its resources
1585 CoreUnloadAndCloseImage (Image
, TRUE
);
1586 Status
= EFI_SUCCESS
;
1591 // Image has been started, verify this image can exit
1593 if (Image
!= mCurrentImage
) {
1594 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1595 Status
= EFI_INVALID_PARAMETER
;
1602 Image
->Status
= Status
;
1605 // If there's ExitData info, move it
1607 if (ExitData
!= NULL
) {
1608 Image
->ExitDataSize
= ExitDataSize
;
1609 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1610 if (Image
->ExitData
== NULL
) {
1611 Status
= EFI_OUT_OF_RESOURCES
;
1614 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1617 CoreRestoreTpl (OldTpl
);
1619 // return to StartImage
1621 LongJump (Image
->JumpContext
, (UINTN
)-1);
1624 // If we return from LongJump, then it is an error
1627 Status
= EFI_ACCESS_DENIED
;
1629 CoreRestoreTpl (OldTpl
);
1639 @param ImageHandle Handle that identifies the image to be
1642 @retval EFI_SUCCESS The image has been unloaded.
1643 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1645 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1651 IN EFI_HANDLE ImageHandle
1655 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1657 Image
= CoreLoadedImageInfo (ImageHandle
);
1658 if (Image
== NULL
) {
1660 // The image handle is not valid
1662 Status
= EFI_INVALID_PARAMETER
;
1666 if (Image
->Started
) {
1668 // The image has been started, request it to unload.
1670 Status
= EFI_UNSUPPORTED
;
1671 if (Image
->Info
.Unload
!= NULL
) {
1672 Status
= Image
->Info
.Unload (ImageHandle
);
1677 // This Image hasn't been started, thus it can be unloaded
1679 Status
= EFI_SUCCESS
;
1683 if (!EFI_ERROR (Status
)) {
1685 // if the Image was not started or Unloaded O.K. then clean up
1687 CoreUnloadAndCloseImage (Image
, TRUE
);
1697 Unload the specified image.
1699 @param This Indicates the calling context.
1700 @param ImageHandle The specified image handle.
1702 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1703 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1704 @retval EFI_SUCCESS Image successfully unloaded.
1710 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1711 IN EFI_HANDLE ImageHandle
1714 return CoreUnloadImage (ImageHandle
);