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
;
102 // Searching for image hob
104 DxeCoreHob
.Raw
= HobStart
;
105 while ((DxeCoreHob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, DxeCoreHob
.Raw
)) != NULL
) {
106 if (CompareGuid (&DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
, &gEfiHobMemoryAllocModuleGuid
)) {
112 DxeCoreHob
.Raw
= GET_NEXT_HOB (DxeCoreHob
);
114 ASSERT (DxeCoreHob
.Raw
!= NULL
);
116 DxeCoreImageBaseAddress
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
;
117 DxeCoreImageLength
= DxeCoreHob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
;
118 DxeCoreEntryPoint
= (VOID
*) (UINTN
) DxeCoreHob
.MemoryAllocationModule
->EntryPoint
;
119 gDxeCoreFileName
= &DxeCoreHob
.MemoryAllocationModule
->ModuleName
;
122 // Initialize the fields for an internal driver
124 Image
= &mCorePrivateImage
;
126 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
;
127 Image
->ImageBasePage
= DxeCoreImageBaseAddress
;
128 Image
->NumberOfPages
= (UINTN
)(EFI_SIZE_TO_PAGES((UINTN
)(DxeCoreImageLength
)));
129 Image
->Tpl
= gEfiCurrentTpl
;
130 Image
->Info
.SystemTable
= gDxeCoreST
;
131 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)DxeCoreImageBaseAddress
;
132 Image
->Info
.ImageSize
= DxeCoreImageLength
;
135 // Install the protocol interfaces for this image
137 Status
= CoreInstallProtocolInterface (
139 &gEfiLoadedImageProtocolGuid
,
140 EFI_NATIVE_INTERFACE
,
143 ASSERT_EFI_ERROR (Status
);
145 mCurrentImage
= Image
;
148 // Fill in DXE globals
150 gDxeCoreImageHandle
= Image
->Handle
;
151 gDxeCoreLoadedImage
= &Image
->Info
;
153 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
155 // Export DXE Core PE Loader functionality for backward compatibility.
157 Status
= CoreInstallProtocolInterface (
158 &mLoadPe32PrivateData
.Handle
,
159 &gEfiLoadPeImageProtocolGuid
,
160 EFI_NATIVE_INTERFACE
,
161 &mLoadPe32PrivateData
.Pe32Image
169 Read image file (specified by UserHandle) into user specified buffer with specified offset
172 @param UserHandle Image file handle
173 @param Offset Offset to the source file
174 @param ReadSize For input, pointer of size to read; For output,
175 pointer of size actually read.
176 @param Buffer Buffer to write into
178 @retval EFI_SUCCESS Successfully read the specified part of file
187 IN OUT UINTN
*ReadSize
,
192 IMAGE_FILE_HANDLE
*FHand
;
194 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
195 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
198 // Move data from our local copy of the file
200 EndPosition
= Offset
+ *ReadSize
;
201 if (EndPosition
> FHand
->SourceSize
) {
202 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
204 if (Offset
>= FHand
->SourceSize
) {
208 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
212 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
213 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
214 The function is only invoked when load modules at fixed address feature is enabled.
216 @param ImageBase The base addres the image will be loaded at.
217 @param ImageSize The size of the image
219 @retval EFI_SUCCESS The memory range the image will be loaded in is available
220 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
223 CheckAndMarkFixLoadingMemoryUsageBitMap (
224 IN EFI_PHYSICAL_ADDRESS ImageBase
,
228 UINT32 DxeCodePageNumber
;
230 EFI_PHYSICAL_ADDRESS DxeCodeBase
;
231 UINTN BaseOffsetPageNumber
;
232 UINTN TopOffsetPageNumber
;
235 // The DXE code range includes RuntimeCodePage range and Boot time code range.
237 DxeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
238 DxeCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
239 DxeCodeSize
= EFI_PAGES_TO_SIZE(DxeCodePageNumber
);
240 DxeCodeBase
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- DxeCodeSize
;
243 // If the memory usage bit map is not initialized, do it. Every bit in the array
244 // indicate the status of the corresponding memory page, available or not
246 if (mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
247 mDxeCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((DxeCodePageNumber
/64) + 1)*sizeof(UINT64
));
250 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
252 if (!gLoadFixedAddressCodeMemoryReady
|| mDxeCodeMemoryRangeUsageBitMap
== NULL
) {
253 return EFI_NOT_FOUND
;
256 // Test the memory range for loading the image in the DXE code range.
258 if (gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
< ImageBase
+ ImageSize
||
259 DxeCodeBase
> ImageBase
) {
260 return EFI_NOT_FOUND
;
263 // Test if the memory is avalaible or not.
265 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- DxeCodeBase
));
266 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- DxeCodeBase
));
267 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
268 if ((mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
270 // This page is already used.
272 return EFI_NOT_FOUND
;
277 // Being here means the memory range is available. So mark the bits for the memory range
279 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
280 mDxeCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
286 Get the fixed loadding address from image header assigned by build tool. This function only be called
287 when Loading module at Fixed address feature enabled.
289 @param ImageContext Pointer to the image context structure that describes the PE/COFF
290 image that needs to be examined by this function.
291 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
292 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
296 GetPeCoffImageFixLoadingAssignedAddress(
297 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
300 UINTN SectionHeaderOffset
;
302 EFI_IMAGE_SECTION_HEADER SectionHeader
;
303 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
306 UINT16 NumberOfSections
;
307 IMAGE_FILE_HANDLE
*Handle
;
308 UINT64 ValueInSectionHeader
;
311 Status
= EFI_NOT_FOUND
;
314 // Get PeHeader pointer
316 Handle
= (IMAGE_FILE_HANDLE
*)ImageContext
->Handle
;
317 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )Handle
->Source
+ ImageContext
->PeCoffHeaderOffset
);
318 SectionHeaderOffset
= (UINTN
)(
319 ImageContext
->PeCoffHeaderOffset
+
321 sizeof (EFI_IMAGE_FILE_HEADER
) +
322 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
324 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
327 // Get base address from the first section header that doesn't point to code section.
329 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
331 // Read section header from file
333 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
334 Status
= ImageContext
->ImageRead (
335 ImageContext
->Handle
,
340 if (EFI_ERROR (Status
)) {
344 Status
= EFI_NOT_FOUND
;
346 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
348 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
349 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
350 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
351 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero
353 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
354 if (ValueInSectionHeader
!= 0) {
356 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
357 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
358 // relative to top address
360 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) < 0) {
361 ImageContext
->ImageAddress
= gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
+ (INT64
)(INTN
)ImageContext
->ImageAddress
;
364 // Check if the memory range is avaliable.
366 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext
->ImageAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
370 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
372 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
));
376 Loads, relocates, and invokes a PE/COFF image
378 @param BootPolicy If TRUE, indicates that the request originates
379 from the boot manager, and that the boot
380 manager is attempting to load FilePath as a
382 @param Pe32Handle The handle of PE32 image
383 @param Image PE image to be loaded
384 @param DstBuffer The buffer to store the image
385 @param EntryPoint A pointer to the entry point
386 @param Attribute The bit mask of attributes to set for the load
389 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
390 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
391 relocate the PE/COFF file
392 @retval EFI_INVALID_PARAMETER Invalid parameter
393 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
398 IN BOOLEAN BootPolicy
,
400 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
401 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
402 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
407 BOOLEAN DstBufAlocated
;
410 ZeroMem (&Image
->ImageContext
, sizeof (Image
->ImageContext
));
412 Image
->ImageContext
.Handle
= Pe32Handle
;
413 Image
->ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)CoreReadImageFile
;
416 // Get information about the image being loaded
418 Status
= PeCoffLoaderGetImageInfo (&Image
->ImageContext
);
419 if (EFI_ERROR (Status
)) {
423 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
424 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image
->ImageContext
.Machine
)) {
426 // The PE/COFF loader can support loading image types that can be executed.
427 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
429 return EFI_UNSUPPORTED
;
434 // Set EFI memory type based on ImageType
436 switch (Image
->ImageContext
.ImageType
) {
437 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
438 Image
->ImageContext
.ImageCodeMemoryType
= EfiLoaderCode
;
439 Image
->ImageContext
.ImageDataMemoryType
= EfiLoaderData
;
441 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
442 Image
->ImageContext
.ImageCodeMemoryType
= EfiBootServicesCode
;
443 Image
->ImageContext
.ImageDataMemoryType
= EfiBootServicesData
;
445 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
446 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
447 Image
->ImageContext
.ImageCodeMemoryType
= EfiRuntimeServicesCode
;
448 Image
->ImageContext
.ImageDataMemoryType
= EfiRuntimeServicesData
;
451 Image
->ImageContext
.ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
452 return EFI_UNSUPPORTED
;
456 // Allocate memory of the correct memory type aligned on the required image boundry
458 DstBufAlocated
= FALSE
;
459 if (DstBuffer
== 0) {
461 // Allocate Destination Buffer as caller did not pass it in
464 if (Image
->ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
465 Size
= (UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
;
467 Size
= (UINTN
)Image
->ImageContext
.ImageSize
;
470 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES (Size
);
473 // If the image relocations have not been stripped, then load at any address.
474 // Otherwise load at the address at which it was linked.
476 // Memory below 1MB should be treated reserved for CSM and there should be
477 // no modules whose preferred load addresses are below 1MB.
479 Status
= EFI_OUT_OF_RESOURCES
;
481 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
482 // a specified address.
484 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 ) {
485 Status
= GetPeCoffImageFixLoadingAssignedAddress (&(Image
->ImageContext
));
487 if (EFI_ERROR (Status
)) {
489 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
491 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
493 Status
= CoreAllocatePages (
495 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
496 Image
->NumberOfPages
,
497 &Image
->ImageContext
.ImageAddress
501 if (Image
->ImageContext
.ImageAddress
>= 0x100000 || Image
->ImageContext
.RelocationsStripped
) {
502 Status
= CoreAllocatePages (
504 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
505 Image
->NumberOfPages
,
506 &Image
->ImageContext
.ImageAddress
509 if (EFI_ERROR (Status
) && !Image
->ImageContext
.RelocationsStripped
) {
510 Status
= CoreAllocatePages (
512 (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
),
513 Image
->NumberOfPages
,
514 &Image
->ImageContext
.ImageAddress
518 if (EFI_ERROR (Status
)) {
521 DstBufAlocated
= TRUE
;
524 // Caller provided the destination buffer
527 if (Image
->ImageContext
.RelocationsStripped
&& (Image
->ImageContext
.ImageAddress
!= DstBuffer
)) {
529 // If the image relocations were stripped, and the caller provided a
530 // destination buffer address that does not match the address that the
531 // image is linked at, then the image cannot be loaded.
533 return EFI_INVALID_PARAMETER
;
536 if (Image
->NumberOfPages
!= 0 &&
537 Image
->NumberOfPages
<
538 (EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
))) {
539 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
540 return EFI_BUFFER_TOO_SMALL
;
543 Image
->NumberOfPages
= EFI_SIZE_TO_PAGES ((UINTN
)Image
->ImageContext
.ImageSize
+ Image
->ImageContext
.SectionAlignment
);
544 Image
->ImageContext
.ImageAddress
= DstBuffer
;
547 Image
->ImageBasePage
= Image
->ImageContext
.ImageAddress
;
548 if (!Image
->ImageContext
.IsTeImage
) {
549 Image
->ImageContext
.ImageAddress
=
550 (Image
->ImageContext
.ImageAddress
+ Image
->ImageContext
.SectionAlignment
- 1) &
551 ~((UINTN
)Image
->ImageContext
.SectionAlignment
- 1);
555 // Load the image from the file into the allocated memory
557 Status
= PeCoffLoaderLoadImage (&Image
->ImageContext
);
558 if (EFI_ERROR (Status
)) {
563 // If this is a Runtime Driver, then allocate memory for the FixupData that
564 // is used to relocate the image when SetVirtualAddressMap() is called. The
565 // relocation is done by the Runtime AP.
567 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
568 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
569 Image
->ImageContext
.FixupData
= AllocateRuntimePool ((UINTN
)(Image
->ImageContext
.FixupDataSize
));
570 if (Image
->ImageContext
.FixupData
== NULL
) {
571 Status
= EFI_OUT_OF_RESOURCES
;
578 // Relocate the image in memory
580 Status
= PeCoffLoaderRelocateImage (&Image
->ImageContext
);
581 if (EFI_ERROR (Status
)) {
586 // Flush the Instruction Cache
588 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
, (UINTN
)Image
->ImageContext
.ImageSize
);
591 // Copy the machine type from the context to the image private data. This
592 // is needed during image unload to know if we should call an EBC protocol
593 // to unload the image.
595 Image
->Machine
= Image
->ImageContext
.Machine
;
598 // Get the image entry point. If it's an EBC image, then call into the
599 // interpreter to create a thunk for the entry point and use the returned
600 // value for the entry point.
602 Image
->EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)Image
->ImageContext
.EntryPoint
;
603 if (Image
->ImageContext
.Machine
== EFI_IMAGE_MACHINE_EBC
) {
605 // Locate the EBC interpreter protocol
607 Status
= CoreLocateProtocol (&gEfiEbcProtocolGuid
, NULL
, (VOID
**)&Image
->Ebc
);
608 if (EFI_ERROR(Status
) || Image
->Ebc
== NULL
) {
609 DEBUG ((DEBUG_LOAD
| DEBUG_ERROR
, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
614 // Register a callback for flushing the instruction cache so that created
615 // thunks can be flushed.
617 Status
= Image
->Ebc
->RegisterICacheFlush (Image
->Ebc
, (EBC_ICACHE_FLUSH
)InvalidateInstructionCacheRange
);
618 if (EFI_ERROR(Status
)) {
623 // Create a thunk for the image's entry point. This will be the new
624 // entry point for the image.
626 Status
= Image
->Ebc
->CreateThunk (
629 (VOID
*)(UINTN
) Image
->ImageContext
.EntryPoint
,
630 (VOID
**) &Image
->EntryPoint
632 if (EFI_ERROR(Status
)) {
638 // Fill in the image information for the Loaded Image Protocol
640 Image
->Type
= Image
->ImageContext
.ImageType
;
641 Image
->Info
.ImageBase
= (VOID
*)(UINTN
)Image
->ImageContext
.ImageAddress
;
642 Image
->Info
.ImageSize
= Image
->ImageContext
.ImageSize
;
643 Image
->Info
.ImageCodeType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageCodeMemoryType
);
644 Image
->Info
.ImageDataType
= (EFI_MEMORY_TYPE
) (Image
->ImageContext
.ImageDataMemoryType
);
645 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
) != 0) {
646 if (Image
->ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
648 // Make a list off all the RT images so we can let the RT AP know about them.
650 Image
->RuntimeData
= AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY
));
651 if (Image
->RuntimeData
== NULL
) {
654 Image
->RuntimeData
->ImageBase
= Image
->Info
.ImageBase
;
655 Image
->RuntimeData
->ImageSize
= (UINT64
) (Image
->Info
.ImageSize
);
656 Image
->RuntimeData
->RelocationData
= Image
->ImageContext
.FixupData
;
657 Image
->RuntimeData
->Handle
= Image
->Handle
;
658 InsertTailList (&gRuntime
->ImageHead
, &Image
->RuntimeData
->Link
);
663 // Fill in the entry point of the image if it is available
665 if (EntryPoint
!= NULL
) {
666 *EntryPoint
= Image
->ImageContext
.EntryPoint
;
670 // Print the load address and the PDB file name if it is available
677 CHAR8 EfiFileName
[256];
680 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
681 "Loading driver at 0x%11p EntryPoint=0x%11p ",
682 (VOID
*)(UINTN
) Image
->ImageContext
.ImageAddress
,
683 FUNCTION_ENTRY_POINT (Image
->ImageContext
.EntryPoint
)));
687 // Print Module Name by Pdb file path.
688 // Windows and Unix style file path are all trimmed correctly.
690 if (Image
->ImageContext
.PdbPointer
!= NULL
) {
692 for (Index
= 0; Image
->ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
693 if ((Image
->ImageContext
.PdbPointer
[Index
] == '\\') || (Image
->ImageContext
.PdbPointer
[Index
] == '/')) {
694 StartIndex
= Index
+ 1;
698 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
699 // The PDB file name is limited in the range of 0~255.
700 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
702 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
703 EfiFileName
[Index
] = Image
->ImageContext
.PdbPointer
[Index
+ StartIndex
];
704 if (EfiFileName
[Index
] == 0) {
705 EfiFileName
[Index
] = '.';
707 if (EfiFileName
[Index
] == '.') {
708 EfiFileName
[Index
+ 1] = 'e';
709 EfiFileName
[Index
+ 2] = 'f';
710 EfiFileName
[Index
+ 3] = 'i';
711 EfiFileName
[Index
+ 4] = 0;
716 if (Index
== sizeof (EfiFileName
) - 4) {
717 EfiFileName
[Index
] = 0;
719 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
721 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
733 if (DstBufAlocated
) {
734 CoreFreePages (Image
->ImageContext
.ImageAddress
, Image
->NumberOfPages
);
737 if (Image
->ImageContext
.FixupData
!= NULL
) {
738 CoreFreePool (Image
->ImageContext
.FixupData
);
747 Get the image's private data from its handle.
749 @param ImageHandle The image handle
751 @return Return the image private data associated with ImageHandle.
754 LOADED_IMAGE_PRIVATE_DATA
*
755 CoreLoadedImageInfo (
756 IN EFI_HANDLE ImageHandle
760 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
761 LOADED_IMAGE_PRIVATE_DATA
*Image
;
763 Status
= CoreHandleProtocol (
765 &gEfiLoadedImageProtocolGuid
,
766 (VOID
**)&LoadedImage
768 if (!EFI_ERROR (Status
)) {
769 Image
= LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage
);
771 DEBUG ((DEBUG_LOAD
, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle
));
780 Unloads EFI image from memory.
782 @param Image EFI image
783 @param FreePage Free allocated pages
787 CoreUnloadAndCloseImage (
788 IN LOADED_IMAGE_PRIVATE_DATA
*Image
,
794 EFI_HANDLE
*HandleBuffer
;
796 EFI_GUID
**ProtocolGuidArray
;
799 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
803 if (Image
->Ebc
!= NULL
) {
805 // If EBC protocol exists we must perform cleanups for this image.
807 Image
->Ebc
->UnloadImage (Image
->Ebc
, Image
->Handle
);
811 // Unload image, free Image->ImageContext->ModHandle
813 PeCoffLoaderUnloadImage (&Image
->ImageContext
);
816 // Free our references to the image handle
818 if (Image
->Handle
!= NULL
) {
820 Status
= CoreLocateHandleBuffer (
827 if (!EFI_ERROR (Status
)) {
828 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
829 Status
= CoreProtocolsPerHandle (
830 HandleBuffer
[HandleIndex
],
834 if (!EFI_ERROR (Status
)) {
835 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
836 Status
= CoreOpenProtocolInformation (
837 HandleBuffer
[HandleIndex
],
838 ProtocolGuidArray
[ProtocolIndex
],
842 if (!EFI_ERROR (Status
)) {
843 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
844 if (OpenInfo
[OpenInfoIndex
].AgentHandle
== Image
->Handle
) {
845 Status
= CoreCloseProtocol (
846 HandleBuffer
[HandleIndex
],
847 ProtocolGuidArray
[ProtocolIndex
],
849 OpenInfo
[OpenInfoIndex
].ControllerHandle
853 if (OpenInfo
!= NULL
) {
854 CoreFreePool(OpenInfo
);
858 if (ProtocolGuidArray
!= NULL
) {
859 CoreFreePool(ProtocolGuidArray
);
863 if (HandleBuffer
!= NULL
) {
864 CoreFreePool (HandleBuffer
);
868 CoreRemoveDebugImageInfoEntry (Image
->Handle
);
870 Status
= CoreUninstallProtocolInterface (
872 &gEfiLoadedImageDevicePathProtocolGuid
,
873 Image
->LoadedImageDevicePath
876 Status
= CoreUninstallProtocolInterface (
878 &gEfiLoadedImageProtocolGuid
,
882 if (Image
->ImageContext
.HiiResourceData
!= 0) {
883 Status
= CoreUninstallProtocolInterface (
885 &gEfiHiiPackageListProtocolGuid
,
886 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
892 if (Image
->RuntimeData
!= NULL
) {
893 if (Image
->RuntimeData
->Link
.ForwardLink
!= NULL
) {
895 // Remove the Image from the Runtime Image list as we are about to Free it!
897 RemoveEntryList (&Image
->RuntimeData
->Link
);
899 CoreFreePool (Image
->RuntimeData
);
903 // Free the Image from memory
905 if ((Image
->ImageBasePage
!= 0) && FreePage
) {
906 CoreFreePages (Image
->ImageBasePage
, Image
->NumberOfPages
);
910 // Done with the Image structure
912 if (Image
->Info
.FilePath
!= NULL
) {
913 CoreFreePool (Image
->Info
.FilePath
);
916 if (Image
->LoadedImageDevicePath
!= NULL
) {
917 CoreFreePool (Image
->LoadedImageDevicePath
);
920 if (Image
->FixupData
!= NULL
) {
921 CoreFreePool (Image
->FixupData
);
924 CoreFreePool (Image
);
929 Loads an EFI image into memory and returns a handle to the image.
931 @param BootPolicy If TRUE, indicates that the request originates
932 from the boot manager, and that the boot
933 manager is attempting to load FilePath as a
935 @param ParentImageHandle The caller's image handle.
936 @param FilePath The specific file path from which the image is
938 @param SourceBuffer If not NULL, a pointer to the memory location
939 containing a copy of the image to be loaded.
940 @param SourceSize The size in bytes of SourceBuffer.
941 @param DstBuffer The buffer to store the image
942 @param NumberOfPages If not NULL, it inputs a pointer to the page
943 number of DstBuffer and outputs a pointer to
944 the page number of the image. If this number is
945 not enough, return EFI_BUFFER_TOO_SMALL and
946 this parameter contains the required number.
947 @param ImageHandle Pointer to the returned image handle that is
948 created when the image is successfully loaded.
949 @param EntryPoint A pointer to the entry point
950 @param Attribute The bit mask of attributes to set for the load
953 @retval EFI_SUCCESS The image was loaded into memory.
954 @retval EFI_NOT_FOUND The FilePath was not found.
955 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
956 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
957 @retval EFI_UNSUPPORTED The image type is not supported, or the device
958 path cannot be parsed to locate the proper
959 protocol for loading the file.
960 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
965 CoreLoadImageCommon (
966 IN BOOLEAN BootPolicy
,
967 IN EFI_HANDLE ParentImageHandle
,
968 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
969 IN VOID
*SourceBuffer OPTIONAL
,
971 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
972 IN OUT UINTN
*NumberOfPages OPTIONAL
,
973 OUT EFI_HANDLE
*ImageHandle
,
974 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
978 LOADED_IMAGE_PRIVATE_DATA
*Image
;
979 LOADED_IMAGE_PRIVATE_DATA
*ParentImage
;
980 IMAGE_FILE_HANDLE FHand
;
982 EFI_STATUS SecurityStatus
;
983 EFI_HANDLE DeviceHandle
;
984 UINT32 AuthenticationStatus
;
985 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
986 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
989 SecurityStatus
= EFI_SUCCESS
;
991 ASSERT (gEfiCurrentTpl
< TPL_NOTIFY
);
995 // The caller must pass in a valid ParentImageHandle
997 if (ImageHandle
== NULL
|| ParentImageHandle
== NULL
) {
998 return EFI_INVALID_PARAMETER
;
1001 ParentImage
= CoreLoadedImageInfo (ParentImageHandle
);
1002 if (ParentImage
== NULL
) {
1003 DEBUG((DEBUG_LOAD
|DEBUG_ERROR
, "LoadImageEx: Parent handle not an image handle\n"));
1004 return EFI_INVALID_PARAMETER
;
1007 ZeroMem (&FHand
, sizeof (IMAGE_FILE_HANDLE
));
1008 FHand
.Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
1009 OriginalFilePath
= FilePath
;
1010 HandleFilePath
= FilePath
;
1011 DeviceHandle
= NULL
;
1012 Status
= EFI_SUCCESS
;
1013 AuthenticationStatus
= 0;
1015 // If the caller passed a copy of the file, then just use it
1017 if (SourceBuffer
!= NULL
) {
1018 FHand
.Source
= SourceBuffer
;
1019 FHand
.SourceSize
= SourceSize
;
1020 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1021 if (SourceSize
> 0) {
1022 Status
= EFI_SUCCESS
;
1024 Status
= EFI_LOAD_ERROR
;
1027 if (FilePath
== NULL
) {
1028 return EFI_INVALID_PARAMETER
;
1031 // Get the source file buffer by its device path.
1033 FHand
.Source
= GetFileBufferByFilePath (
1037 &AuthenticationStatus
1039 if (FHand
.Source
== NULL
) {
1040 Status
= EFI_NOT_FOUND
;
1043 // Try to get the image device handle by checking the match protocol.
1045 FHand
.FreeBuffer
= TRUE
;
1046 Status
= CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1047 if (EFI_ERROR (Status
)) {
1048 HandleFilePath
= FilePath
;
1049 Status
= CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1050 if (EFI_ERROR (Status
)) {
1052 HandleFilePath
= FilePath
;
1053 Status
= CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1055 if (EFI_ERROR (Status
)) {
1056 HandleFilePath
= FilePath
;
1057 Status
= CoreLocateDevicePath (&gEfiLoadFileProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
1064 if (Status
== EFI_ALREADY_STARTED
) {
1067 } else if (EFI_ERROR (Status
)) {
1072 // Verify the Authentication Status through the Security Architectural Protocol
1074 if ((gSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
1075 SecurityStatus
= gSecurity
->FileAuthenticationState (
1077 AuthenticationStatus
,
1080 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
1081 Status
= SecurityStatus
;
1089 // Allocate a new image structure
1091 Image
= AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA
));
1092 if (Image
== NULL
) {
1093 return EFI_OUT_OF_RESOURCES
;
1097 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1099 FilePath
= OriginalFilePath
;
1100 if (DeviceHandle
!= NULL
) {
1101 Status
= CoreHandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
1102 if (!EFI_ERROR (Status
)) {
1103 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1104 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
1108 // Initialize the fields for an internal driver
1110 Image
->Signature
= LOADED_IMAGE_PRIVATE_DATA_SIGNATURE
;
1111 Image
->Info
.SystemTable
= gDxeCoreST
;
1112 Image
->Info
.DeviceHandle
= DeviceHandle
;
1113 Image
->Info
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1114 Image
->Info
.FilePath
= DuplicateDevicePath (FilePath
);
1115 Image
->Info
.ParentHandle
= ParentImageHandle
;
1118 if (NumberOfPages
!= NULL
) {
1119 Image
->NumberOfPages
= *NumberOfPages
;
1121 Image
->NumberOfPages
= 0 ;
1125 // Install the protocol interfaces for this image
1126 // don't fire notifications yet
1128 Status
= CoreInstallProtocolInterfaceNotify (
1130 &gEfiLoadedImageProtocolGuid
,
1131 EFI_NATIVE_INTERFACE
,
1135 if (EFI_ERROR (Status
)) {
1140 // Load the image. If EntryPoint is Null, it will not be set.
1142 Status
= CoreLoadPeImage (BootPolicy
, &FHand
, Image
, DstBuffer
, EntryPoint
, Attribute
);
1143 if (EFI_ERROR (Status
)) {
1144 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_OUT_OF_RESOURCES
)) {
1145 if (NumberOfPages
!= NULL
) {
1146 *NumberOfPages
= Image
->NumberOfPages
;
1152 if (NumberOfPages
!= NULL
) {
1153 *NumberOfPages
= Image
->NumberOfPages
;
1157 // Register the image in the Debug Image Info Table if the attribute is set
1159 if ((Attribute
& EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
) != 0) {
1160 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
, &Image
->Info
, Image
->Handle
);
1164 //Reinstall loaded image protocol to fire any notifications
1166 Status
= CoreReinstallProtocolInterface (
1168 &gEfiLoadedImageProtocolGuid
,
1172 if (EFI_ERROR (Status
)) {
1177 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1178 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1180 if (OriginalFilePath
!= NULL
) {
1181 Image
->LoadedImageDevicePath
= DuplicateDevicePath (OriginalFilePath
);
1185 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1187 Status
= CoreInstallProtocolInterface (
1189 &gEfiLoadedImageDevicePathProtocolGuid
,
1190 EFI_NATIVE_INTERFACE
,
1191 Image
->LoadedImageDevicePath
1193 if (EFI_ERROR (Status
)) {
1198 // Install HII Package List Protocol onto the image handle
1200 if (Image
->ImageContext
.HiiResourceData
!= 0) {
1201 Status
= CoreInstallProtocolInterface (
1203 &gEfiHiiPackageListProtocolGuid
,
1204 EFI_NATIVE_INTERFACE
,
1205 (VOID
*) (UINTN
) Image
->ImageContext
.HiiResourceData
1207 if (EFI_ERROR (Status
)) {
1213 // Success. Return the image handle
1215 *ImageHandle
= Image
->Handle
;
1219 // All done accessing the source file
1220 // If we allocated the Source buffer, free it
1222 if (FHand
.FreeBuffer
) {
1223 CoreFreePool (FHand
.Source
);
1227 // There was an error. If there's an Image structure, free it
1229 if (EFI_ERROR (Status
)) {
1230 if (Image
!= NULL
) {
1231 CoreUnloadAndCloseImage (Image
, (BOOLEAN
)(DstBuffer
== 0));
1232 *ImageHandle
= NULL
;
1234 } else if (EFI_ERROR (SecurityStatus
)) {
1235 Status
= SecurityStatus
;
1245 Loads an EFI image into memory and returns a handle to the image.
1247 @param BootPolicy If TRUE, indicates that the request originates
1248 from the boot manager, and that the boot
1249 manager is attempting to load FilePath as a
1251 @param ParentImageHandle The caller's image handle.
1252 @param FilePath The specific file path from which the image is
1254 @param SourceBuffer If not NULL, a pointer to the memory location
1255 containing a copy of the image to be loaded.
1256 @param SourceSize The size in bytes of SourceBuffer.
1257 @param ImageHandle Pointer to the returned image handle that is
1258 created when the image is successfully loaded.
1260 @retval EFI_SUCCESS The image was loaded into memory.
1261 @retval EFI_NOT_FOUND The FilePath was not found.
1262 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1263 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1264 path cannot be parsed to locate the proper
1265 protocol for loading the file.
1266 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1273 IN BOOLEAN BootPolicy
,
1274 IN EFI_HANDLE ParentImageHandle
,
1275 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1276 IN VOID
*SourceBuffer OPTIONAL
,
1277 IN UINTN SourceSize
,
1278 OUT EFI_HANDLE
*ImageHandle
1286 Tick
= GetPerformanceCounter ();
1289 Status
= CoreLoadImageCommon (
1295 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1299 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1302 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1303 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1311 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1313 @param This Calling context
1314 @param ParentImageHandle The caller's image handle.
1315 @param FilePath The specific file path from which the image is
1317 @param SourceBuffer If not NULL, a pointer to the memory location
1318 containing a copy of the image to be loaded.
1319 @param SourceSize The size in bytes of SourceBuffer.
1320 @param DstBuffer The buffer to store the image.
1321 @param NumberOfPages For input, specifies the space size of the
1322 image by caller if not NULL. For output,
1323 specifies the actual space size needed.
1324 @param ImageHandle Image handle for output.
1325 @param EntryPoint Image entry point for output.
1326 @param Attribute The bit mask of attributes to set for the load
1329 @retval EFI_SUCCESS The image was loaded into memory.
1330 @retval EFI_NOT_FOUND The FilePath was not found.
1331 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1332 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1333 path cannot be parsed to locate the proper
1334 protocol for loading the file.
1335 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1342 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1343 IN EFI_HANDLE ParentImageHandle
,
1344 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1345 IN VOID
*SourceBuffer OPTIONAL
,
1346 IN UINTN SourceSize
,
1347 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1348 OUT UINTN
*NumberOfPages OPTIONAL
,
1349 OUT EFI_HANDLE
*ImageHandle
,
1350 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1354 return CoreLoadImageCommon (
1370 Transfer control to a loaded image's entry point.
1372 @param ImageHandle Handle of image to be started.
1373 @param ExitDataSize Pointer of the size to ExitData
1374 @param ExitData Pointer to a pointer to a data buffer that
1375 includes a Null-terminated Unicode string,
1376 optionally followed by additional binary data.
1377 The string is a description that the caller may
1378 use to further indicate the reason for the
1381 @retval EFI_INVALID_PARAMETER Invalid parameter
1382 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1383 @retval EFI_SUCCESS Successfully transfer control to the image's
1390 IN EFI_HANDLE ImageHandle
,
1391 OUT UINTN
*ExitDataSize
,
1392 OUT CHAR16
**ExitData OPTIONAL
1396 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1397 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1398 UINT64 HandleDatabaseKey
;
1401 Image
= CoreLoadedImageInfo (ImageHandle
);
1402 if (Image
== NULL
|| Image
->Started
) {
1403 return EFI_INVALID_PARAMETER
;
1407 // The image to be started must have the machine type supported by DxeCore.
1409 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
));
1410 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1411 return EFI_UNSUPPORTED
;
1415 // Don't profile Objects or invalid start requests
1417 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1421 // Push the current start image context, and
1422 // link the current image to the head. This is the
1423 // only image that can call Exit()
1425 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1426 LastImage
= mCurrentImage
;
1427 mCurrentImage
= Image
;
1428 Image
->Tpl
= gEfiCurrentTpl
;
1431 // Set long jump for Exit() support
1432 // JumpContext must be aligned on a CPU specific boundary.
1433 // Overallocate the buffer and force the required alignment
1435 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1436 if (Image
->JumpBuffer
== NULL
) {
1437 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1438 return EFI_OUT_OF_RESOURCES
;
1440 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1442 SetJumpFlag
= SetJump (Image
->JumpContext
);
1444 // The initial call to SetJump() must always return 0.
1445 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1447 if (SetJumpFlag
== 0) {
1449 // Call the image's entry point
1451 Image
->Started
= TRUE
;
1452 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1455 // Add some debug information if the image returned with error.
1456 // This make the user aware and check if the driver image have already released
1457 // all the resource in this situation.
1459 DEBUG_CODE_BEGIN ();
1460 if (EFI_ERROR (Image
->Status
)) {
1461 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1466 // If the image returns, exit it through Exit()
1468 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1472 // Image has completed. Verify the tpl is the same
1474 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1475 CoreRestoreTpl (Image
->Tpl
);
1477 CoreFreePool (Image
->JumpBuffer
);
1480 // Pop the current start image context
1482 mCurrentImage
= LastImage
;
1485 // Go connect any handles that were created or modified while the image executed.
1487 CoreConnectHandlesByKey (HandleDatabaseKey
);
1490 // Handle the image's returned ExitData
1492 DEBUG_CODE_BEGIN ();
1493 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1495 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1496 if (Image
->ExitData
!= NULL
) {
1497 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1499 DEBUG ((DEBUG_LOAD
, "\n"));
1504 // Return the exit data to the caller
1506 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1507 *ExitDataSize
= Image
->ExitDataSize
;
1508 *ExitData
= Image
->ExitData
;
1511 // Caller doesn't want the exit data, free it
1513 CoreFreePool (Image
->ExitData
);
1514 Image
->ExitData
= NULL
;
1518 // Save the Status because Image will get destroyed if it is unloaded.
1520 Status
= Image
->Status
;
1523 // If the image returned an error, or if the image is an application
1526 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1527 CoreUnloadAndCloseImage (Image
, TRUE
);
1533 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1538 Terminates the currently loaded EFI image and returns control to boot services.
1540 @param ImageHandle Handle that identifies the image. This
1541 parameter is passed to the image on entry.
1542 @param Status The image's exit code.
1543 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1544 ExitStatus is EFI_SUCCESS.
1545 @param ExitData Pointer to a data buffer that includes a
1546 Null-terminated Unicode string, optionally
1547 followed by additional binary data. The string
1548 is a description that the caller may use to
1549 further indicate the reason for the image's
1552 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1554 @retval EFI_SUCCESS Successfully terminates the currently loaded
1556 @retval EFI_ACCESS_DENIED Should never reach there.
1557 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1563 IN EFI_HANDLE ImageHandle
,
1564 IN EFI_STATUS Status
,
1565 IN UINTN ExitDataSize
,
1566 IN CHAR16
*ExitData OPTIONAL
1569 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1573 // Prevent possible reentrance to this function
1574 // for the same ImageHandle
1576 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1578 Image
= CoreLoadedImageInfo (ImageHandle
);
1579 if (Image
== NULL
) {
1580 Status
= EFI_INVALID_PARAMETER
;
1584 if (!Image
->Started
) {
1586 // The image has not been started so just free its resources
1588 CoreUnloadAndCloseImage (Image
, TRUE
);
1589 Status
= EFI_SUCCESS
;
1594 // Image has been started, verify this image can exit
1596 if (Image
!= mCurrentImage
) {
1597 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1598 Status
= EFI_INVALID_PARAMETER
;
1605 Image
->Status
= Status
;
1608 // If there's ExitData info, move it
1610 if (ExitData
!= NULL
) {
1611 Image
->ExitDataSize
= ExitDataSize
;
1612 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1613 if (Image
->ExitData
== NULL
) {
1614 Status
= EFI_OUT_OF_RESOURCES
;
1617 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1620 CoreRestoreTpl (OldTpl
);
1622 // return to StartImage
1624 LongJump (Image
->JumpContext
, (UINTN
)-1);
1627 // If we return from LongJump, then it is an error
1630 Status
= EFI_ACCESS_DENIED
;
1632 CoreRestoreTpl (OldTpl
);
1642 @param ImageHandle Handle that identifies the image to be
1645 @retval EFI_SUCCESS The image has been unloaded.
1646 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1648 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1654 IN EFI_HANDLE ImageHandle
1658 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1660 Image
= CoreLoadedImageInfo (ImageHandle
);
1661 if (Image
== NULL
) {
1663 // The image handle is not valid
1665 Status
= EFI_INVALID_PARAMETER
;
1669 if (Image
->Started
) {
1671 // The image has been started, request it to unload.
1673 Status
= EFI_UNSUPPORTED
;
1674 if (Image
->Info
.Unload
!= NULL
) {
1675 Status
= Image
->Info
.Unload (ImageHandle
);
1680 // This Image hasn't been started, thus it can be unloaded
1682 Status
= EFI_SUCCESS
;
1686 if (!EFI_ERROR (Status
)) {
1688 // if the Image was not started or Unloaded O.K. then clean up
1690 CoreUnloadAndCloseImage (Image
, TRUE
);
1700 Unload the specified image.
1702 @param This Indicates the calling context.
1703 @param ImageHandle The specified image handle.
1705 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1706 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1707 @retval EFI_SUCCESS Image successfully unloaded.
1713 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1714 IN EFI_HANDLE ImageHandle
1717 return CoreUnloadImage (ImageHandle
);