2 Core image handling services to load and unload PeImage.
4 Copyright (c) 2006 - 2011, 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
962 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
964 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
965 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
966 image from being loaded. NULL is returned in *ImageHandle.
967 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
968 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
969 platform policy specifies that the image should not be started.
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
1276 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1278 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1279 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1280 image from being loaded. NULL is returned in *ImageHandle.
1281 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1282 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1283 platform policy specifies that the image should not be started.
1289 IN BOOLEAN BootPolicy
,
1290 IN EFI_HANDLE ParentImageHandle
,
1291 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1292 IN VOID
*SourceBuffer OPTIONAL
,
1293 IN UINTN SourceSize
,
1294 OUT EFI_HANDLE
*ImageHandle
1302 Tick
= GetPerformanceCounter ();
1305 Status
= CoreLoadImageCommon (
1311 (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
,
1315 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION
| EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1318 PERF_START (*ImageHandle
, "LoadImage:", NULL
, Tick
);
1319 PERF_END (*ImageHandle
, "LoadImage:", NULL
, 0);
1327 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1329 @param This Calling context
1330 @param ParentImageHandle The caller's image handle.
1331 @param FilePath The specific file path from which the image is
1333 @param SourceBuffer If not NULL, a pointer to the memory location
1334 containing a copy of the image to be loaded.
1335 @param SourceSize The size in bytes of SourceBuffer.
1336 @param DstBuffer The buffer to store the image.
1337 @param NumberOfPages For input, specifies the space size of the
1338 image by caller if not NULL. For output,
1339 specifies the actual space size needed.
1340 @param ImageHandle Image handle for output.
1341 @param EntryPoint Image entry point for output.
1342 @param Attribute The bit mask of attributes to set for the load
1345 @retval EFI_SUCCESS The image was loaded into memory.
1346 @retval EFI_NOT_FOUND The FilePath was not found.
1347 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1348 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1349 path cannot be parsed to locate the proper
1350 protocol for loading the file.
1351 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1353 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1355 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1356 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1357 image from being loaded. NULL is returned in *ImageHandle.
1358 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1359 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1360 platform policy specifies that the image should not be started.
1366 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1367 IN EFI_HANDLE ParentImageHandle
,
1368 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1369 IN VOID
*SourceBuffer OPTIONAL
,
1370 IN UINTN SourceSize
,
1371 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL
,
1372 OUT UINTN
*NumberOfPages OPTIONAL
,
1373 OUT EFI_HANDLE
*ImageHandle
,
1374 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint OPTIONAL
,
1378 return CoreLoadImageCommon (
1394 Transfer control to a loaded image's entry point.
1396 @param ImageHandle Handle of image to be started.
1397 @param ExitDataSize Pointer of the size to ExitData
1398 @param ExitData Pointer to a pointer to a data buffer that
1399 includes a Null-terminated string,
1400 optionally followed by additional binary data.
1401 The string is a description that the caller may
1402 use to further indicate the reason for the
1405 @retval EFI_INVALID_PARAMETER Invalid parameter
1406 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1407 @retval EFI_SUCCESS Successfully transfer control to the image's
1414 IN EFI_HANDLE ImageHandle
,
1415 OUT UINTN
*ExitDataSize
,
1416 OUT CHAR16
**ExitData OPTIONAL
1420 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1421 LOADED_IMAGE_PRIVATE_DATA
*LastImage
;
1422 UINT64 HandleDatabaseKey
;
1425 Image
= CoreLoadedImageInfo (ImageHandle
);
1426 if (Image
== NULL
|| Image
->Started
) {
1427 return EFI_INVALID_PARAMETER
;
1431 // The image to be started must have the machine type supported by DxeCore.
1433 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
));
1434 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image
->Machine
)) {
1435 return EFI_UNSUPPORTED
;
1439 // Don't profile Objects or invalid start requests
1441 PERF_START (ImageHandle
, "StartImage:", NULL
, 0);
1445 // Push the current start image context, and
1446 // link the current image to the head. This is the
1447 // only image that can call Exit()
1449 HandleDatabaseKey
= CoreGetHandleDatabaseKey ();
1450 LastImage
= mCurrentImage
;
1451 mCurrentImage
= Image
;
1452 Image
->Tpl
= gEfiCurrentTpl
;
1455 // Set long jump for Exit() support
1456 // JumpContext must be aligned on a CPU specific boundary.
1457 // Overallocate the buffer and force the required alignment
1459 Image
->JumpBuffer
= AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER
) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1460 if (Image
->JumpBuffer
== NULL
) {
1461 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1462 return EFI_OUT_OF_RESOURCES
;
1464 Image
->JumpContext
= ALIGN_POINTER (Image
->JumpBuffer
, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT
);
1466 SetJumpFlag
= SetJump (Image
->JumpContext
);
1468 // The initial call to SetJump() must always return 0.
1469 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1471 if (SetJumpFlag
== 0) {
1473 // Call the image's entry point
1475 Image
->Started
= TRUE
;
1476 Image
->Status
= Image
->EntryPoint (ImageHandle
, Image
->Info
.SystemTable
);
1479 // Add some debug information if the image returned with error.
1480 // This make the user aware and check if the driver image have already released
1481 // all the resource in this situation.
1483 DEBUG_CODE_BEGIN ();
1484 if (EFI_ERROR (Image
->Status
)) {
1485 DEBUG ((DEBUG_ERROR
, "Error: Image at %11p start failed: %r\n", Image
->Info
.ImageBase
, Image
->Status
));
1490 // If the image returns, exit it through Exit()
1492 CoreExit (ImageHandle
, Image
->Status
, 0, NULL
);
1496 // Image has completed. Verify the tpl is the same
1498 ASSERT (Image
->Tpl
== gEfiCurrentTpl
);
1499 CoreRestoreTpl (Image
->Tpl
);
1501 CoreFreePool (Image
->JumpBuffer
);
1504 // Pop the current start image context
1506 mCurrentImage
= LastImage
;
1509 // Go connect any handles that were created or modified while the image executed.
1511 CoreConnectHandlesByKey (HandleDatabaseKey
);
1514 // Handle the image's returned ExitData
1516 DEBUG_CODE_BEGIN ();
1517 if (Image
->ExitDataSize
!= 0 || Image
->ExitData
!= NULL
) {
1519 DEBUG ((DEBUG_LOAD
, "StartImage: ExitDataSize %d, ExitData %p", (UINT32
)Image
->ExitDataSize
, Image
->ExitData
));
1520 if (Image
->ExitData
!= NULL
) {
1521 DEBUG ((DEBUG_LOAD
, " (%hs)", Image
->ExitData
));
1523 DEBUG ((DEBUG_LOAD
, "\n"));
1528 // Return the exit data to the caller
1530 if (ExitData
!= NULL
&& ExitDataSize
!= NULL
) {
1531 *ExitDataSize
= Image
->ExitDataSize
;
1532 *ExitData
= Image
->ExitData
;
1535 // Caller doesn't want the exit data, free it
1537 CoreFreePool (Image
->ExitData
);
1538 Image
->ExitData
= NULL
;
1542 // Save the Status because Image will get destroyed if it is unloaded.
1544 Status
= Image
->Status
;
1547 // If the image returned an error, or if the image is an application
1550 if (EFI_ERROR (Image
->Status
) || Image
->Type
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1551 CoreUnloadAndCloseImage (Image
, TRUE
);
1557 PERF_END (ImageHandle
, "StartImage:", NULL
, 0);
1562 Terminates the currently loaded EFI image and returns control to boot services.
1564 @param ImageHandle Handle that identifies the image. This
1565 parameter is passed to the image on entry.
1566 @param Status The image's exit code.
1567 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1568 ExitStatus is EFI_SUCCESS.
1569 @param ExitData Pointer to a data buffer that includes a
1570 Null-terminated Unicode string, optionally
1571 followed by additional binary data. The string
1572 is a description that the caller may use to
1573 further indicate the reason for the image's
1576 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1578 @retval EFI_SUCCESS Successfully terminates the currently loaded
1580 @retval EFI_ACCESS_DENIED Should never reach there.
1581 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1587 IN EFI_HANDLE ImageHandle
,
1588 IN EFI_STATUS Status
,
1589 IN UINTN ExitDataSize
,
1590 IN CHAR16
*ExitData OPTIONAL
1593 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1597 // Prevent possible reentrance to this function
1598 // for the same ImageHandle
1600 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1602 Image
= CoreLoadedImageInfo (ImageHandle
);
1603 if (Image
== NULL
) {
1604 Status
= EFI_INVALID_PARAMETER
;
1608 if (!Image
->Started
) {
1610 // The image has not been started so just free its resources
1612 CoreUnloadAndCloseImage (Image
, TRUE
);
1613 Status
= EFI_SUCCESS
;
1618 // Image has been started, verify this image can exit
1620 if (Image
!= mCurrentImage
) {
1621 DEBUG ((DEBUG_LOAD
|DEBUG_ERROR
, "Exit: Image is not exitable image\n"));
1622 Status
= EFI_INVALID_PARAMETER
;
1629 Image
->Status
= Status
;
1632 // If there's ExitData info, move it
1634 if (ExitData
!= NULL
) {
1635 Image
->ExitDataSize
= ExitDataSize
;
1636 Image
->ExitData
= AllocatePool (Image
->ExitDataSize
);
1637 if (Image
->ExitData
== NULL
) {
1638 Status
= EFI_OUT_OF_RESOURCES
;
1641 CopyMem (Image
->ExitData
, ExitData
, Image
->ExitDataSize
);
1644 CoreRestoreTpl (OldTpl
);
1646 // return to StartImage
1648 LongJump (Image
->JumpContext
, (UINTN
)-1);
1651 // If we return from LongJump, then it is an error
1654 Status
= EFI_ACCESS_DENIED
;
1656 CoreRestoreTpl (OldTpl
);
1666 @param ImageHandle Handle that identifies the image to be
1669 @retval EFI_SUCCESS The image has been unloaded.
1670 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1672 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1678 IN EFI_HANDLE ImageHandle
1682 LOADED_IMAGE_PRIVATE_DATA
*Image
;
1684 Image
= CoreLoadedImageInfo (ImageHandle
);
1685 if (Image
== NULL
) {
1687 // The image handle is not valid
1689 Status
= EFI_INVALID_PARAMETER
;
1693 if (Image
->Started
) {
1695 // The image has been started, request it to unload.
1697 Status
= EFI_UNSUPPORTED
;
1698 if (Image
->Info
.Unload
!= NULL
) {
1699 Status
= Image
->Info
.Unload (ImageHandle
);
1704 // This Image hasn't been started, thus it can be unloaded
1706 Status
= EFI_SUCCESS
;
1710 if (!EFI_ERROR (Status
)) {
1712 // if the Image was not started or Unloaded O.K. then clean up
1714 CoreUnloadAndCloseImage (Image
, TRUE
);
1724 Unload the specified image.
1726 @param This Indicates the calling context.
1727 @param ImageHandle The specified image handle.
1729 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1730 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1731 @retval EFI_SUCCESS Image successfully unloaded.
1737 IN EFI_PE32_IMAGE_PROTOCOL
*This
,
1738 IN EFI_HANDLE ImageHandle
1741 return CoreUnloadImage (ImageHandle
);