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
;
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
)(INTN
)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
) || Image
->Ebc
== NULL
) {
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 if (DeviceHandle
!= NULL
) {
1099 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
1100 if (!EFI_ERROR (Status
)) {
1101 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1102 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
1106 // Initialize the fields for an internal driver
1108 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
1109 Image
->Info
.SystemTable
= gDxeCoreST
;
1110 Image
->Info
.DeviceHandle
= DeviceHandle
;
1111 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1112 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
1113 Image
->Info
.ParentHandle
= ParentImageHandle
;
1116 if (NumberOfPages
!= NULL
) {
1117 Image
->NumberOfPages
= *NumberOfPages
;
1119 Image
->NumberOfPages
= 0 ;
1123 // Install the protocol interfaces for this image
1124 // don't fire notifications yet
1126 Status
= CoreInstallProtocolInterfaceNotify (
1128 &gEfiLoadedImageProtocolGuid
,
1129 EFI_NATIVE_INTERFACE
,
1133 if (EFI_ERROR (Status
)) {
1138 // Load the image. If EntryPoint is Null, it will not be set.
1140 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
1141 if (EFI_ERROR (Status
)) {
1142 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
1143 if (NumberOfPages
!= NULL
) {
1144 *NumberOfPages
= Image
->NumberOfPages
;
1150 if (NumberOfPages
!= NULL
) {
1151 *NumberOfPages
= Image
->NumberOfPages
;
1155 // Register the image in the Debug Image Info Table if the attribute is set
1157 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
1158 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
1162 //Reinstall loaded image protocol to fire any notifications
1164 Status
= CoreReinstallProtocolInterface (
1166 &gEfiLoadedImageProtocolGuid
,
1170 if (EFI_ERROR (Status
)) {
1175 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1176 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1178 if (OriginalFilePath
!= NULL
) {
1179 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
1183 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1185 Status
= CoreInstallProtocolInterface (
1187 &gEfiLoadedImageDevicePathProtocolGuid
,
1188 EFI_NATIVE_INTERFACE
,
1189 Image
->LoadedImageDevicePath
1191 if (EFI_ERROR (Status
)) {
1196 // Install HII Package List Protocol onto the image handle
1198 if (Image
->ImageContext
.HiiResourceData
!= 0) {
1199 Status
= CoreInstallProtocolInterface (
1201 &gEfiHiiPackageListProtocolGuid
,
1202 EFI_NATIVE_INTERFACE
,
1203 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
1205 if (EFI_ERROR (Status
)) {
1211 // Success. Return the image handle
1213 *ImageHandle
= Image
->Handle
;
1217 // All done accessing the source file
1218 // If we allocated the Source buffer, free it
1220 if (FHand
.FreeBuffer
) {
1221 CoreFreePool (FHand
.Source
);
1225 // There was an error. If there's an Image structure, free it
1227 if (EFI_ERROR (Status
)) {
1228 if (Image
!= NULL
) {
1229 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
1230 *ImageHandle
= NULL
;
1232 } else if (EFI_ERROR (SecurityStatus
)) {
1233 Status
= SecurityStatus
;
1243 Loads an EFI image into memory and returns a handle to the image.
1245 @param BootPolicy If TRUE, indicates that the request originates
1246 from the boot manager, and that the boot
1247 manager is attempting to load FilePath as a
1249 @param ParentImageHandle The caller's image handle.
1250 @param FilePath The specific file path from which the image is
1252 @param SourceBuffer If not NULL, a pointer to the memory location
1253 containing a copy of the image to be loaded.
1254 @param SourceSize The size in bytes of SourceBuffer.
1255 @param ImageHandle Pointer to the returned image handle that is
1256 created when the image is successfully loaded.
1258 @retval EFI_SUCCESS The image was loaded into memory.
1259 @retval EFI_NOT_FOUND The FilePath was not found.
1260 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1261 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1262 path cannot be parsed to locate the proper
1263 protocol for loading the file.
1264 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1271 IN BOOLEAN BootPolicy
,
1272 IN EFI_HANDLE ParentImageHandle
,
1273 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1274 IN VOID
*SourceBuffer OPTIONAL
,
1275 IN UINTN SourceSize
,
1276 OUT EFI_HANDLE
*ImageHandle
1284 Tick
= GetPerformanceCounter ();
1287 Status
= CoreLoadImageCommon (
1293 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1297 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1300 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1301 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1309 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1311 @param This Calling context
1312 @param ParentImageHandle The caller's image handle.
1313 @param FilePath The specific file path from which the image is
1315 @param SourceBuffer If not NULL, a pointer to the memory location
1316 containing a copy of the image to be loaded.
1317 @param SourceSize The size in bytes of SourceBuffer.
1318 @param DstBuffer The buffer to store the image.
1319 @param NumberOfPages For input, specifies the space size of the
1320 image by caller if not NULL. For output,
1321 specifies the actual space size needed.
1322 @param ImageHandle Image handle for output.
1323 @param EntryPoint Image entry point for output.
1324 @param Attribute The bit mask of attributes to set for the load
1327 @retval EFI_SUCCESS The image was loaded into memory.
1328 @retval EFI_NOT_FOUND The FilePath was not found.
1329 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1330 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1331 path cannot be parsed to locate the proper
1332 protocol for loading the file.
1333 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1340 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1341 IN EFI_HANDLE ParentImageHandle
,
1342 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1343 IN VOID
*SourceBuffer OPTIONAL
,
1344 IN UINTN SourceSize
,
1345 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1346 OUT UINTN
*NumberOfPages OPTIONAL
,
1347 OUT EFI_HANDLE
*ImageHandle
,
1348 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1352 return CoreLoadImageCommon (
1368 Transfer control to a loaded image's entry point.
1370 @param ImageHandle Handle of image to be started.
1371 @param ExitDataSize Pointer of the size to ExitData
1372 @param ExitData Pointer to a pointer to a data buffer that
1373 includes a Null-terminated Unicode string,
1374 optionally followed by additional binary data.
1375 The string is a description that the caller may
1376 use to further indicate the reason for the
1379 @retval EFI_INVALID_PARAMETER Invalid parameter
1380 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1381 @retval EFI_SUCCESS Successfully transfer control to the image's
1388 IN EFI_HANDLE ImageHandle
,
1389 OUT UINTN
*ExitDataSize
,
1390 OUT CHAR16
**ExitData OPTIONAL
1394 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1395 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1396 UINT64 HandleDatabaseKey
;
1399 Image
= CoreLoadedImageInfo (ImageHandle
);
1400 if (Image
== NULL
|| Image
->Started
) {
1401 return EFI_INVALID_PARAMETER
;
1405 // The image to be started must have the machine type supported by DxeCore.
1407 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
));
1408 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1409 return EFI_UNSUPPORTED
;
1413 // Don't profile Objects or invalid start requests
1415 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1419 // Push the current start image context, and
1420 // link the current image to the head. This is the
1421 // only image that can call Exit()
1423 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1424 LastImage
= mCurrentImage
;
1425 mCurrentImage
= Image
;
1426 Image
->Tpl
= gEfiCurrentTpl
;
1429 // Set long jump for Exit() support
1430 // JumpContext must be aligned on a CPU specific boundary.
1431 // Overallocate the buffer and force the required alignment
1433 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1434 if (Image
->JumpBuffer
== NULL
) {
1435 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1436 return EFI_OUT_OF_RESOURCES
;
1438 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1440 SetJumpFlag
= SetJump (Image
->JumpContext
);
1442 // The initial call to SetJump() must always return 0.
1443 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1445 if (SetJumpFlag
== 0) {
1447 // Call the image's entry point
1449 Image
->Started
= TRUE
;
1450 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1453 // Add some debug information if the image returned with error.
1454 // This make the user aware and check if the driver image have already released
1455 // all the resource in this situation.
1457 DEBUG_CODE_BEGIN ();
1458 if (EFI_ERROR (Image
->Status
)) {
1459 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1464 // If the image returns, exit it through Exit()
1466 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1470 // Image has completed. Verify the tpl is the same
1472 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1473 CoreRestoreTpl (Image
->Tpl
);
1475 CoreFreePool (Image
->JumpBuffer
);
1478 // Pop the current start image context
1480 mCurrentImage
= LastImage
;
1483 // Go connect any handles that were created or modified while the image executed.
1485 CoreConnectHandlesByKey (HandleDatabaseKey
);
1488 // Handle the image's returned ExitData
1490 DEBUG_CODE_BEGIN ();
1491 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1493 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1494 if (Image
->ExitData
!= NULL
) {
1495 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1497 DEBUG ((DEBUG_LOAD
, "\n"));
1502 // Return the exit data to the caller
1504 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1505 *ExitDataSize
= Image
->ExitDataSize
;
1506 *ExitData
= Image
->ExitData
;
1509 // Caller doesn't want the exit data, free it
1511 CoreFreePool (Image
->ExitData
);
1512 Image
->ExitData
= NULL
;
1516 // Save the Status because Image will get destroyed if it is unloaded.
1518 Status
= Image
->Status
;
1521 // If the image returned an error, or if the image is an application
1524 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1525 CoreUnloadAndCloseImage (Image
, TRUE
);
1531 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1536 Terminates the currently loaded EFI image and returns control to boot services.
1538 @param ImageHandle Handle that identifies the image. This
1539 parameter is passed to the image on entry.
1540 @param Status The image's exit code.
1541 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1542 ExitStatus is EFI_SUCCESS.
1543 @param ExitData Pointer to a data buffer that includes a
1544 Null-terminated Unicode string, optionally
1545 followed by additional binary data. The string
1546 is a description that the caller may use to
1547 further indicate the reason for the image's
1550 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1552 @retval EFI_SUCCESS Successfully terminates the currently loaded
1554 @retval EFI_ACCESS_DENIED Should never reach there.
1555 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1561 IN EFI_HANDLE ImageHandle
,
1562 IN EFI_STATUS Status
,
1563 IN UINTN ExitDataSize
,
1564 IN CHAR16
*ExitData OPTIONAL
1567 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1571 // Prevent possible reentrance to this function
1572 // for the same ImageHandle
1574 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1576 Image
= CoreLoadedImageInfo (ImageHandle
);
1577 if (Image
== NULL
) {
1578 Status
= EFI_INVALID_PARAMETER
;
1582 if (!Image
->Started
) {
1584 // The image has not been started so just free its resources
1586 CoreUnloadAndCloseImage (Image
, TRUE
);
1587 Status
= EFI_SUCCESS
;
1592 // Image has been started, verify this image can exit
1594 if (Image
!= mCurrentImage
) {
1595 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1596 Status
= EFI_INVALID_PARAMETER
;
1603 Image
->Status
= Status
;
1606 // If there's ExitData info, move it
1608 if (ExitData
!= NULL
) {
1609 Image
->ExitDataSize
= ExitDataSize
;
1610 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1611 if (Image
->ExitData
== NULL
) {
1612 Status
= EFI_OUT_OF_RESOURCES
;
1615 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1618 CoreRestoreTpl (OldTpl
);
1620 // return to StartImage
1622 LongJump (Image
->JumpContext
, (UINTN
)-1);
1625 // If we return from LongJump, then it is an error
1628 Status
= EFI_ACCESS_DENIED
;
1630 CoreRestoreTpl (OldTpl
);
1640 @param ImageHandle Handle that identifies the image to be
1643 @retval EFI_SUCCESS The image has been unloaded.
1644 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1646 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1652 IN EFI_HANDLE ImageHandle
1656 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1658 Image
= CoreLoadedImageInfo (ImageHandle
);
1659 if (Image
== NULL
) {
1661 // The image handle is not valid
1663 Status
= EFI_INVALID_PARAMETER
;
1667 if (Image
->Started
) {
1669 // The image has been started, request it to unload.
1671 Status
= EFI_UNSUPPORTED
;
1672 if (Image
->Info
.Unload
!= NULL
) {
1673 Status
= Image
->Info
.Unload (ImageHandle
);
1678 // This Image hasn't been started, thus it can be unloaded
1680 Status
= EFI_SUCCESS
;
1684 if (!EFI_ERROR (Status
)) {
1686 // if the Image was not started or Unloaded O.K. then clean up
1688 CoreUnloadAndCloseImage (Image
, TRUE
);
1698 Unload the specified image.
1700 @param This Indicates the calling context.
1701 @param ImageHandle The specified image handle.
1703 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1704 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1705 @retval EFI_SUCCESS Image successfully unloaded.
1711 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1712 IN EFI_HANDLE ImageHandle
1715 return CoreUnloadImage (ImageHandle
);