2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 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
;
100 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
103 // Searching for image hob
105 DxeCoreHob
.Raw
= HobStart
;
106 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
107 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
113 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
115 ASSERT (DxeCoreHob
.Raw
!= NULL
);
117 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
118 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
119 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
120 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
123 // Report DXE Core image information to the PE/COFF Extra Action Library
125 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)DxeCoreImageBaseAddress
;
126 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
127 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
130 // Initialize the fields for an internal driver
132 Image
= &mCorePrivateImage
;
134 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
135 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
136 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
137 Image
->Tpl
= gEfiCurrentTpl
;
138 Image
->Info
.SystemTable
= gDxeCoreST
;
139 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
140 Image
->Info
.ImageSize
= DxeCoreImageLength
;
143 // Install the protocol interfaces for this image
145 Status
= CoreInstallProtocolInterface (
147 &gEfiLoadedImageProtocolGuid
,
148 EFI_NATIVE_INTERFACE
,
151 ASSERT_EFI_ERROR (Status
);
153 mCurrentImage
= Image
;
156 // Fill in DXE globals
158 gDxeCoreImageHandle
= Image
->Handle
;
159 gDxeCoreLoadedImage
= &Image
->Info
;
161 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
163 // Export DXE Core PE Loader functionality for backward compatibility.
165 Status
= CoreInstallProtocolInterface (
166 &mLoadPe32PrivateData
.Handle
,
167 &gEfiLoadPeImageProtocolGuid
,
168 EFI_NATIVE_INTERFACE
,
169 &mLoadPe32PrivateData
.Pe32Image
177 Read image file (specified by UserHandle) into user specified buffer with specified offset
180 @param UserHandle Image file handle
181 @param Offset Offset to the source file
182 @param ReadSize For input, pointer of size to read; For output,
183 pointer of size actually read.
184 @param Buffer Buffer to write into
186 @retval EFI_SUCCESS Successfully read the specified part of file
195 IN OUT UINTN
*ReadSize
,
200 IMAGE_FILE_HANDLE
*FHand
;
202 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
203 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
206 // Move data from our local copy of the file
208 EndPosition
= Offset
+ *ReadSize
;
209 if (EndPosition
> FHand
->SourceSize
) {
210 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
212 if (Offset
>= FHand
->SourceSize
) {
216 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
220 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
221 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
222 The function is only invoked when load modules at fixed address feature is enabled.
224 @param ImageBase The base addres the image will be loaded at.
225 @param ImageSize The size of the image
227 @retval EFI_SUCCESS The memory range the image will be loaded in is available
228 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
231 CheckAndMarkFixLoadingMemoryUsageBitMap (
232 IN EFI_PHYSICAL_ADDRESS ImageBase
,
236 UINT32 DxeCodePageNumber
;
238 EFI_PHYSICAL_ADDRESS DxeCodeBase
;
239 UINTN BaseOffsetPageNumber
;
240 UINTN TopOffsetPageNumber
;
243 // The DXE code range includes RuntimeCodePage range and Boot time code range.
245 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
246 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
247 DxeCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
);
248 DxeCodeBase
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- DxeCodeSize
;
251 // If the memory usage bit map is not initialized, do it. Every bit in the array
252 // indicate the status of the corresponding memory page, available or not
254 if (mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
255 mDxeCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((DxeCodePageNumber
/64) + 1)*sizeof(UINT64
));
258 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
260 if (!gLoadFixedAddressCodeMemoryReady
|| mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
261 return EFI_NOT_FOUND
;
264 // Test the memory range for loading the image in the DXE code range.
266 if (gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
< ImageBase
+ ImageSize
||
267 DxeCodeBase
> ImageBase
) {
268 return EFI_NOT_FOUND
;
271 // Test if the memory is avalaible or not.
273 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- DxeCodeBase
));
274 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- DxeCodeBase
));
275 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
276 if ((mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
278 // This page is already used.
280 return EFI_NOT_FOUND
;
285 // Being here means the memory range is available. So mark the bits for the memory range
287 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
288 mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
294 Get the fixed loadding address from image header assigned by build tool. This function only be called
295 when Loading module at Fixed address feature enabled.
297 @param ImageContext Pointer to the image context structure that describes the PE/COFF
298 image that needs to be examined by this function.
299 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
300 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
304 GetPeCoffImageFixLoadingAssignedAddress(
305 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
308 UINTN SectionHeaderOffset
;
310 EFI_IMAGE_SECTION_HEADER SectionHeader
;
311 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
314 UINT16 NumberOfSections
;
315 IMAGE_FILE_HANDLE
*Handle
;
316 UINT64 ValueInSectionHeader
;
319 Status
= EFI_NOT_FOUND
;
322 // Get PeHeader pointer
324 Handle
= (IMAGE_FILE_HANDLE
*)ImageContext
->Handle
;
325 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )Handle
->Source
+ ImageContext
->PeCoffHeaderOffset
);
326 SectionHeaderOffset
= (UINTN
)(
327 ImageContext
->PeCoffHeaderOffset
+
329 sizeof (EFI_IMAGE_FILE_HEADER
) +
330 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
332 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
335 // Get base address from the first section header that doesn't point to code section.
337 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
339 // Read section header from file
341 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
342 Status
= ImageContext
->ImageRead (
343 ImageContext
->Handle
,
348 if (EFI_ERROR (Status
)) {
352 Status
= EFI_NOT_FOUND
;
354 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
356 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
357 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
358 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
359 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero
361 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
362 if (ValueInSectionHeader
!= 0) {
364 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
365 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
366 // relative to top address
368 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) < 0) {
369 ImageContext
->ImageAddress
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
+ (INT64
)(INTN
)ImageContext
->ImageAddress
;
372 // Check if the memory range is avaliable.
374 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext
->ImageAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
378 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
380 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
));
384 Loads, relocates, and invokes a PE/COFF image
386 @param BootPolicy If TRUE, indicates that the request originates
387 from the boot manager, and that the boot
388 manager is attempting to load FilePath as a
390 @param Pe32Handle The handle of PE32 image
391 @param Image PE image to be loaded
392 @param DstBuffer The buffer to store the image
393 @param EntryPoint A pointer to the entry point
394 @param Attribute The bit mask of attributes to set for the load
397 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
398 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
399 relocate the PE/COFF file
400 @retval EFI_INVALID_PARAMETER Invalid parameter
401 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
406 IN BOOLEAN BootPolicy
,
408 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
409 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
410 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
415 BOOLEAN DstBufAlocated
;
418 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
420 Image
->ImageContext
.Handle
= Pe32Handle
;
421 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
424 // Get information about the image being loaded
426 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
427 if (EFI_ERROR (Status
)) {
431 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
432 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
434 // The PE/COFF loader can support loading image types that can be executed.
435 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
437 return EFI_UNSUPPORTED
;
442 // Set EFI memory type based on ImageType
444 switch (Image
->ImageContext
.ImageType
) {
445 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
446 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
447 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
449 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
450 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
451 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
453 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
454 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
455 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
456 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
459 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
460 return EFI_UNSUPPORTED
;
464 // Allocate memory of the correct memory type aligned on the required image boundry
466 DstBufAlocated
= FALSE
;
467 if (DstBuffer
== 0) {
469 // Allocate Destination Buffer as caller did not pass it in
472 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
473 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
475 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
478 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
481 // If the image relocations have not been stripped, then load at any address.
482 // Otherwise load at the address at which it was linked.
484 // Memory below 1MB should be treated reserved for CSM and there should be
485 // no modules whose preferred load addresses are below 1MB.
487 Status
= EFI_OUT_OF_RESOURCES
;
489 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
490 // a specified address.
492 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 ) {
493 Status
= GetPeCoffImageFixLoadingAssignedAddress (&(Image
->ImageContext
));
495 if (EFI_ERROR (Status
)) {
497 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
499 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
501 Status
= CoreAllocatePages (
503 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
504 Image
->NumberOfPages
,
505 &Image
->ImageContext
.ImageAddress
509 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
510 Status
= CoreAllocatePages (
512 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
513 Image
->NumberOfPages
,
514 &Image
->ImageContext
.ImageAddress
517 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
518 Status
= CoreAllocatePages (
520 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
521 Image
->NumberOfPages
,
522 &Image
->ImageContext
.ImageAddress
526 if (EFI_ERROR (Status
)) {
529 DstBufAlocated
= TRUE
;
532 // Caller provided the destination buffer
535 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
537 // If the image relocations were stripped, and the caller provided a
538 // destination buffer address that does not match the address that the
539 // image is linked at, then the image cannot be loaded.
541 return EFI_INVALID_PARAMETER
;
544 if (Image
->NumberOfPages
!= 0 &&
545 Image
->NumberOfPages
<
546 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
547 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
548 return EFI_BUFFER_TOO_SMALL
;
551 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
552 Image
->ImageContext
.ImageAddress
= DstBuffer
;
555 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
556 if (!Image
->ImageContext
.IsTeImage
) {
557 Image
->ImageContext
.ImageAddress
=
558 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
559 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
563 // Load the image from the file into the allocated memory
565 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
566 if (EFI_ERROR (Status
)) {
571 // If this is a Runtime Driver, then allocate memory for the FixupData that
572 // is used to relocate the image when SetVirtualAddressMap() is called. The
573 // relocation is done by the Runtime AP.
575 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
576 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
577 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
578 if (Image
->ImageContext
.FixupData
== NULL
) {
579 Status
= EFI_OUT_OF_RESOURCES
;
586 // Relocate the image in memory
588 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
589 if (EFI_ERROR (Status
)) {
594 // Flush the Instruction Cache
596 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
599 // Copy the machine type from the context to the image private data. This
600 // is needed during image unload to know if we should call an EBC protocol
601 // to unload the image.
603 Image
->Machine
= Image
->ImageContext
.Machine
;
606 // Get the image entry point. If it's an EBC image, then call into the
607 // interpreter to create a thunk for the entry point and use the returned
608 // value for the entry point.
610 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
611 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
613 // Locate the EBC interpreter protocol
615 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
616 if (EFI_ERROR(Status
) || Image
->Ebc
== NULL
) {
617 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
622 // Register a callback for flushing the instruction cache so that created
623 // thunks can be flushed.
625 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
626 if (EFI_ERROR(Status
)) {
631 // Create a thunk for the image's entry point. This will be the new
632 // entry point for the image.
634 Status
= Image
->Ebc
->CreateThunk (
637 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
638 (VOID
**) &Image
->EntryPoint
640 if (EFI_ERROR(Status
)) {
646 // Fill in the image information for the Loaded Image Protocol
648 Image
->Type
= Image
->ImageContext
.ImageType
;
649 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
650 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
651 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
652 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
653 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
654 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
656 // Make a list off all the RT images so we can let the RT AP know about them.
658 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
659 if (Image
->RuntimeData
== NULL
) {
662 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
663 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
664 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
665 Image
->RuntimeData
->Handle
= Image
->Handle
;
666 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
671 // Fill in the entry point of the image if it is available
673 if (EntryPoint
!= NULL
) {
674 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
678 // Print the load address and the PDB file name if it is available
685 CHAR8 EfiFileName
[256];
688 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
689 "Loading driver at 0x%11p EntryPoint=0x%11p ",
690 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
691 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
695 // Print Module Name by Pdb file path.
696 // Windows and Unix style file path are all trimmed correctly.
698 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
700 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
701 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
702 StartIndex
= Index
+ 1;
706 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
707 // The PDB file name is limited in the range of 0~255.
708 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
710 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
711 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
712 if (EfiFileName
[Index
] == 0) {
713 EfiFileName
[Index
] = '.';
715 if (EfiFileName
[Index
] == '.') {
716 EfiFileName
[Index
+ 1] = 'e';
717 EfiFileName
[Index
+ 2] = 'f';
718 EfiFileName
[Index
+ 3] = 'i';
719 EfiFileName
[Index
+ 4] = 0;
724 if (Index
== sizeof (EfiFileName
) - 4) {
725 EfiFileName
[Index
] = 0;
727 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
729 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
741 if (DstBufAlocated
) {
742 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
745 if (Image
->ImageContext
.FixupData
!= NULL
) {
746 CoreFreePool (Image
->ImageContext
.FixupData
);
755 Get the image's private data from its handle.
757 @param ImageHandle The image handle
759 @return Return the image private data associated with ImageHandle.
762 LOADED_IMAGE_PRIVATE_DATA
*
763 CoreLoadedImageInfo (
764 IN EFI_HANDLE ImageHandle
768 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
769 LOADED_IMAGE_PRIVATE_DATA
*Image
;
771 Status
= CoreHandleProtocol (
773 &gEfiLoadedImageProtocolGuid
,
774 (VOID
**)&LoadedImage
776 if (!EFI_ERROR (Status
)) {
777 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
779 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
788 Unloads EFI image from memory.
790 @param Image EFI image
791 @param FreePage Free allocated pages
795 CoreUnloadAndCloseImage (
796 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
802 EFI_HANDLE
*HandleBuffer
;
804 EFI_GUID
**ProtocolGuidArray
;
807 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
811 if (Image
->Ebc
!= NULL
) {
813 // If EBC protocol exists we must perform cleanups for this image.
815 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
819 // Unload image, free Image->ImageContext->ModHandle
821 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
824 // Free our references to the image handle
826 if (Image
->Handle
!= NULL
) {
828 Status
= CoreLocateHandleBuffer (
835 if (!EFI_ERROR (Status
)) {
836 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
837 Status
= CoreProtocolsPerHandle (
838 HandleBuffer
[HandleIndex
],
842 if (!EFI_ERROR (Status
)) {
843 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
844 Status
= CoreOpenProtocolInformation (
845 HandleBuffer
[HandleIndex
],
846 ProtocolGuidArray
[ProtocolIndex
],
850 if (!EFI_ERROR (Status
)) {
851 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
852 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
853 Status
= CoreCloseProtocol (
854 HandleBuffer
[HandleIndex
],
855 ProtocolGuidArray
[ProtocolIndex
],
857 OpenInfo
[OpenInfoIndex
].ControllerHandle
861 if (OpenInfo
!= NULL
) {
862 CoreFreePool(OpenInfo
);
866 if (ProtocolGuidArray
!= NULL
) {
867 CoreFreePool(ProtocolGuidArray
);
871 if (HandleBuffer
!= NULL
) {
872 CoreFreePool (HandleBuffer
);
876 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
878 Status
= CoreUninstallProtocolInterface (
880 &gEfiLoadedImageDevicePathProtocolGuid
,
881 Image
->LoadedImageDevicePath
884 Status
= CoreUninstallProtocolInterface (
886 &gEfiLoadedImageProtocolGuid
,
890 if (Image
->ImageContext
.HiiResourceData
!= 0) {
891 Status
= CoreUninstallProtocolInterface (
893 &gEfiHiiPackageListProtocolGuid
,
894 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
900 if (Image
->RuntimeData
!= NULL
) {
901 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
903 // Remove the Image from the Runtime Image list as we are about to Free it!
905 RemoveEntryList (&Image
->RuntimeData
->Link
);
907 CoreFreePool (Image
->RuntimeData
);
911 // Free the Image from memory
913 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
914 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
918 // Done with the Image structure
920 if (Image
->Info
.FilePath
!= NULL
) {
921 CoreFreePool (Image
->Info
.FilePath
);
924 if (Image
->LoadedImageDevicePath
!= NULL
) {
925 CoreFreePool (Image
->LoadedImageDevicePath
);
928 if (Image
->FixupData
!= NULL
) {
929 CoreFreePool (Image
->FixupData
);
932 CoreFreePool (Image
);
937 Loads an EFI image into memory and returns a handle to the image.
939 @param BootPolicy If TRUE, indicates that the request originates
940 from the boot manager, and that the boot
941 manager is attempting to load FilePath as a
943 @param ParentImageHandle The caller's image handle.
944 @param FilePath The specific file path from which the image is
946 @param SourceBuffer If not NULL, a pointer to the memory location
947 containing a copy of the image to be loaded.
948 @param SourceSize The size in bytes of SourceBuffer.
949 @param DstBuffer The buffer to store the image
950 @param NumberOfPages If not NULL, it inputs a pointer to the page
951 number of DstBuffer and outputs a pointer to
952 the page number of the image. If this number is
953 not enough, return EFI_BUFFER_TOO_SMALL and
954 this parameter contains the required number.
955 @param ImageHandle Pointer to the returned image handle that is
956 created when the image is successfully loaded.
957 @param EntryPoint A pointer to the entry point
958 @param Attribute The bit mask of attributes to set for the load
961 @retval EFI_SUCCESS The image was loaded into memory.
962 @retval EFI_NOT_FOUND The FilePath was not found.
963 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
964 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
965 @retval EFI_UNSUPPORTED The image type is not supported, or the device
966 path cannot be parsed to locate the proper
967 protocol for loading the file.
968 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
973 CoreLoadImageCommon (
974 IN BOOLEAN BootPolicy
,
975 IN EFI_HANDLE ParentImageHandle
,
976 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
977 IN VOID
*SourceBuffer OPTIONAL
,
979 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
980 IN OUT UINTN
*NumberOfPages OPTIONAL
,
981 OUT EFI_HANDLE
*ImageHandle
,
982 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
986 LOADED_IMAGE_PRIVATE_DATA
*Image
;
987 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
988 IMAGE_FILE_HANDLE FHand
;
990 EFI_STATUS SecurityStatus
;
991 EFI_HANDLE DeviceHandle
;
992 UINT32 AuthenticationStatus
;
993 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
994 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
997 SecurityStatus
= EFI_SUCCESS
;
999 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
1003 // The caller must pass in a valid ParentImageHandle
1005 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
1006 return EFI_INVALID_PARAMETER
;
1009 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
1010 if (ParentImage
== NULL
) {
1011 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
1012 return EFI_INVALID_PARAMETER
;
1015 ZeroMem (&FHand
, sizeof (IMAGE_FILE_HANDLE
));
1016 FHand
.Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
1017 OriginalFilePath
= FilePath
;
1018 HandleFilePath
= FilePath
;
1019 DeviceHandle
= NULL
;
1020 Status
= EFI_SUCCESS
;
1021 AuthenticationStatus
= 0;
1023 // If the caller passed a copy of the file, then just use it
1025 if (SourceBuffer
!= NULL
) {
1026 FHand
.Source
= SourceBuffer
;
1027 FHand
.SourceSize
= SourceSize
;
1028 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1029 if (SourceSize
> 0) {
1030 Status
= EFI_SUCCESS
;
1032 Status
= EFI_LOAD_ERROR
;
1035 if (FilePath
== NULL
) {
1036 return EFI_INVALID_PARAMETER
;
1039 // Get the source file buffer by its device path.
1041 FHand
.Source
= GetFileBufferByFilePath (
1045 &AuthenticationStatus
1047 if (FHand
.Source
== NULL
) {
1048 Status
= EFI_NOT_FOUND
;
1051 // Try to get the image device handle by checking the match protocol.
1053 FHand
.FreeBuffer
= TRUE
;
1054 Status
= CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1055 if (EFI_ERROR (Status
)) {
1056 HandleFilePath
= FilePath
;
1057 Status
= CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1058 if (EFI_ERROR (Status
)) {
1060 HandleFilePath
= FilePath
;
1061 Status
= CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1063 if (EFI_ERROR (Status
)) {
1064 HandleFilePath
= FilePath
;
1065 Status
= CoreLocateDevicePath (&gEfiLoadFileProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1072 if (Status
== EFI_ALREADY_STARTED
) {
1075 } else if (EFI_ERROR (Status
)) {
1080 // Verify the Authentication Status through the Security Architectural Protocol
1082 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
1083 SecurityStatus
= gSecurity
->FileAuthenticationState (
1085 AuthenticationStatus
,
1088 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
1089 Status
= SecurityStatus
;
1097 // Allocate a new image structure
1099 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
1100 if (Image
== NULL
) {
1101 return EFI_OUT_OF_RESOURCES
;
1105 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1107 FilePath
= OriginalFilePath
;
1108 if (DeviceHandle
!= NULL
) {
1109 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
1110 if (!EFI_ERROR (Status
)) {
1111 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1112 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
1116 // Initialize the fields for an internal driver
1118 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
1119 Image
->Info
.SystemTable
= gDxeCoreST
;
1120 Image
->Info
.DeviceHandle
= DeviceHandle
;
1121 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1122 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
1123 Image
->Info
.ParentHandle
= ParentImageHandle
;
1126 if (NumberOfPages
!= NULL
) {
1127 Image
->NumberOfPages
= *NumberOfPages
;
1129 Image
->NumberOfPages
= 0 ;
1133 // Install the protocol interfaces for this image
1134 // don't fire notifications yet
1136 Status
= CoreInstallProtocolInterfaceNotify (
1138 &gEfiLoadedImageProtocolGuid
,
1139 EFI_NATIVE_INTERFACE
,
1143 if (EFI_ERROR (Status
)) {
1148 // Load the image. If EntryPoint is Null, it will not be set.
1150 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
1151 if (EFI_ERROR (Status
)) {
1152 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
1153 if (NumberOfPages
!= NULL
) {
1154 *NumberOfPages
= Image
->NumberOfPages
;
1160 if (NumberOfPages
!= NULL
) {
1161 *NumberOfPages
= Image
->NumberOfPages
;
1165 // Register the image in the Debug Image Info Table if the attribute is set
1167 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
1168 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
1172 //Reinstall loaded image protocol to fire any notifications
1174 Status
= CoreReinstallProtocolInterface (
1176 &gEfiLoadedImageProtocolGuid
,
1180 if (EFI_ERROR (Status
)) {
1185 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1186 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1188 if (OriginalFilePath
!= NULL
) {
1189 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
1193 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1195 Status
= CoreInstallProtocolInterface (
1197 &gEfiLoadedImageDevicePathProtocolGuid
,
1198 EFI_NATIVE_INTERFACE
,
1199 Image
->LoadedImageDevicePath
1201 if (EFI_ERROR (Status
)) {
1206 // Install HII Package List Protocol onto the image handle
1208 if (Image
->ImageContext
.HiiResourceData
!= 0) {
1209 Status
= CoreInstallProtocolInterface (
1211 &gEfiHiiPackageListProtocolGuid
,
1212 EFI_NATIVE_INTERFACE
,
1213 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
1215 if (EFI_ERROR (Status
)) {
1221 // Success. Return the image handle
1223 *ImageHandle
= Image
->Handle
;
1227 // All done accessing the source file
1228 // If we allocated the Source buffer, free it
1230 if (FHand
.FreeBuffer
) {
1231 CoreFreePool (FHand
.Source
);
1235 // There was an error. If there's an Image structure, free it
1237 if (EFI_ERROR (Status
)) {
1238 if (Image
!= NULL
) {
1239 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
1240 *ImageHandle
= NULL
;
1242 } else if (EFI_ERROR (SecurityStatus
)) {
1243 Status
= SecurityStatus
;
1253 Loads an EFI image into memory and returns a handle to the image.
1255 @param BootPolicy If TRUE, indicates that the request originates
1256 from the boot manager, and that the boot
1257 manager is attempting to load FilePath as a
1259 @param ParentImageHandle The caller's image handle.
1260 @param FilePath The specific file path from which the image is
1262 @param SourceBuffer If not NULL, a pointer to the memory location
1263 containing a copy of the image to be loaded.
1264 @param SourceSize The size in bytes of SourceBuffer.
1265 @param ImageHandle Pointer to the returned image handle that is
1266 created when the image is successfully loaded.
1268 @retval EFI_SUCCESS The image was loaded into memory.
1269 @retval EFI_NOT_FOUND The FilePath was not found.
1270 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1271 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1272 path cannot be parsed to locate the proper
1273 protocol for loading the file.
1274 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1281 IN BOOLEAN BootPolicy
,
1282 IN EFI_HANDLE ParentImageHandle
,
1283 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1284 IN VOID
*SourceBuffer OPTIONAL
,
1285 IN UINTN SourceSize
,
1286 OUT EFI_HANDLE
*ImageHandle
1294 Tick
= GetPerformanceCounter ();
1297 Status
= CoreLoadImageCommon (
1303 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1307 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1310 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1311 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1319 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1321 @param This Calling context
1322 @param ParentImageHandle The caller's image handle.
1323 @param FilePath The specific file path from which the image is
1325 @param SourceBuffer If not NULL, a pointer to the memory location
1326 containing a copy of the image to be loaded.
1327 @param SourceSize The size in bytes of SourceBuffer.
1328 @param DstBuffer The buffer to store the image.
1329 @param NumberOfPages For input, specifies the space size of the
1330 image by caller if not NULL. For output,
1331 specifies the actual space size needed.
1332 @param ImageHandle Image handle for output.
1333 @param EntryPoint Image entry point for output.
1334 @param Attribute The bit mask of attributes to set for the load
1337 @retval EFI_SUCCESS The image was loaded into memory.
1338 @retval EFI_NOT_FOUND The FilePath was not found.
1339 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1340 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1341 path cannot be parsed to locate the proper
1342 protocol for loading the file.
1343 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1350 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1351 IN EFI_HANDLE ParentImageHandle
,
1352 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1353 IN VOID
*SourceBuffer OPTIONAL
,
1354 IN UINTN SourceSize
,
1355 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1356 OUT UINTN
*NumberOfPages OPTIONAL
,
1357 OUT EFI_HANDLE
*ImageHandle
,
1358 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1362 return CoreLoadImageCommon (
1378 Transfer control to a loaded image's entry point.
1380 @param ImageHandle Handle of image to be started.
1381 @param ExitDataSize Pointer of the size to ExitData
1382 @param ExitData Pointer to a pointer to a data buffer that
1383 includes a Null-terminated Unicode string,
1384 optionally followed by additional binary data.
1385 The string is a description that the caller may
1386 use to further indicate the reason for the
1389 @retval EFI_INVALID_PARAMETER Invalid parameter
1390 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1391 @retval EFI_SUCCESS Successfully transfer control to the image's
1398 IN EFI_HANDLE ImageHandle
,
1399 OUT UINTN
*ExitDataSize
,
1400 OUT CHAR16
**ExitData OPTIONAL
1404 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1405 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1406 UINT64 HandleDatabaseKey
;
1409 Image
= CoreLoadedImageInfo (ImageHandle
);
1410 if (Image
== NULL
|| Image
->Started
) {
1411 return EFI_INVALID_PARAMETER
;
1415 // The image to be started must have the machine type supported by DxeCore.
1417 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
));
1418 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1419 return EFI_UNSUPPORTED
;
1423 // Don't profile Objects or invalid start requests
1425 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1429 // Push the current start image context, and
1430 // link the current image to the head. This is the
1431 // only image that can call Exit()
1433 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1434 LastImage
= mCurrentImage
;
1435 mCurrentImage
= Image
;
1436 Image
->Tpl
= gEfiCurrentTpl
;
1439 // Set long jump for Exit() support
1440 // JumpContext must be aligned on a CPU specific boundary.
1441 // Overallocate the buffer and force the required alignment
1443 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1444 if (Image
->JumpBuffer
== NULL
) {
1445 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1446 return EFI_OUT_OF_RESOURCES
;
1448 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1450 SetJumpFlag
= SetJump (Image
->JumpContext
);
1452 // The initial call to SetJump() must always return 0.
1453 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1455 if (SetJumpFlag
== 0) {
1457 // Call the image's entry point
1459 Image
->Started
= TRUE
;
1460 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1463 // Add some debug information if the image returned with error.
1464 // This make the user aware and check if the driver image have already released
1465 // all the resource in this situation.
1467 DEBUG_CODE_BEGIN ();
1468 if (EFI_ERROR (Image
->Status
)) {
1469 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1474 // If the image returns, exit it through Exit()
1476 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1480 // Image has completed. Verify the tpl is the same
1482 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1483 CoreRestoreTpl (Image
->Tpl
);
1485 CoreFreePool (Image
->JumpBuffer
);
1488 // Pop the current start image context
1490 mCurrentImage
= LastImage
;
1493 // Go connect any handles that were created or modified while the image executed.
1495 CoreConnectHandlesByKey (HandleDatabaseKey
);
1498 // Handle the image's returned ExitData
1500 DEBUG_CODE_BEGIN ();
1501 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1503 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1504 if (Image
->ExitData
!= NULL
) {
1505 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1507 DEBUG ((DEBUG_LOAD
, "\n"));
1512 // Return the exit data to the caller
1514 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1515 *ExitDataSize
= Image
->ExitDataSize
;
1516 *ExitData
= Image
->ExitData
;
1519 // Caller doesn't want the exit data, free it
1521 CoreFreePool (Image
->ExitData
);
1522 Image
->ExitData
= NULL
;
1526 // Save the Status because Image will get destroyed if it is unloaded.
1528 Status
= Image
->Status
;
1531 // If the image returned an error, or if the image is an application
1534 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1535 CoreUnloadAndCloseImage (Image
, TRUE
);
1541 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1546 Terminates the currently loaded EFI image and returns control to boot services.
1548 @param ImageHandle Handle that identifies the image. This
1549 parameter is passed to the image on entry.
1550 @param Status The image's exit code.
1551 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1552 ExitStatus is EFI_SUCCESS.
1553 @param ExitData Pointer to a data buffer that includes a
1554 Null-terminated Unicode string, optionally
1555 followed by additional binary data. The string
1556 is a description that the caller may use to
1557 further indicate the reason for the image's
1560 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1562 @retval EFI_SUCCESS Successfully terminates the currently loaded
1564 @retval EFI_ACCESS_DENIED Should never reach there.
1565 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1571 IN EFI_HANDLE ImageHandle
,
1572 IN EFI_STATUS Status
,
1573 IN UINTN ExitDataSize
,
1574 IN CHAR16
*ExitData OPTIONAL
1577 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1581 // Prevent possible reentrance to this function
1582 // for the same ImageHandle
1584 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1586 Image
= CoreLoadedImageInfo (ImageHandle
);
1587 if (Image
== NULL
) {
1588 Status
= EFI_INVALID_PARAMETER
;
1592 if (!Image
->Started
) {
1594 // The image has not been started so just free its resources
1596 CoreUnloadAndCloseImage (Image
, TRUE
);
1597 Status
= EFI_SUCCESS
;
1602 // Image has been started, verify this image can exit
1604 if (Image
!= mCurrentImage
) {
1605 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1606 Status
= EFI_INVALID_PARAMETER
;
1613 Image
->Status
= Status
;
1616 // If there's ExitData info, move it
1618 if (ExitData
!= NULL
) {
1619 Image
->ExitDataSize
= ExitDataSize
;
1620 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1621 if (Image
->ExitData
== NULL
) {
1622 Status
= EFI_OUT_OF_RESOURCES
;
1625 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1628 CoreRestoreTpl (OldTpl
);
1630 // return to StartImage
1632 LongJump (Image
->JumpContext
, (UINTN
)-1);
1635 // If we return from LongJump, then it is an error
1638 Status
= EFI_ACCESS_DENIED
;
1640 CoreRestoreTpl (OldTpl
);
1650 @param ImageHandle Handle that identifies the image to be
1653 @retval EFI_SUCCESS The image has been unloaded.
1654 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1656 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1662 IN EFI_HANDLE ImageHandle
1666 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1668 Image
= CoreLoadedImageInfo (ImageHandle
);
1669 if (Image
== NULL
) {
1671 // The image handle is not valid
1673 Status
= EFI_INVALID_PARAMETER
;
1677 if (Image
->Started
) {
1679 // The image has been started, request it to unload.
1681 Status
= EFI_UNSUPPORTED
;
1682 if (Image
->Info
.Unload
!= NULL
) {
1683 Status
= Image
->Info
.Unload (ImageHandle
);
1688 // This Image hasn't been started, thus it can be unloaded
1690 Status
= EFI_SUCCESS
;
1694 if (!EFI_ERROR (Status
)) {
1696 // if the Image was not started or Unloaded O.K. then clean up
1698 CoreUnloadAndCloseImage (Image
, TRUE
);
1708 Unload the specified image.
1710 @param This Indicates the calling context.
1711 @param ImageHandle The specified image handle.
1713 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1714 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1715 @retval EFI_SUCCESS Image successfully unloaded.
1721 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1722 IN EFI_HANDLE ImageHandle
1725 return CoreUnloadImage (ImageHandle
);