]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Image/Image.c
MdeModulePkg/Dxe/Image: Restore mCurrentImage on all paths
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
1 /** @file
2 Core image handling services to load and unload PeImage.
3
4 Copyright (c) 2006 - 2017, 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
9
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.
12
13 **/
14
15 #include "DxeMain.h"
16 #include "Image.h"
17
18 //
19 // Module Globals
20 //
21 LOADED_IMAGE_PRIVATE_DATA *mCurrentImage = NULL;
22
23 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData = {
24 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE,
25 NULL,
26 {
27 CoreLoadImageEx,
28 CoreUnloadImageEx
29 }
30 };
31
32
33 //
34 // This code is needed to build the Image handle for the DXE Core
35 //
36 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = {
37 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE, // Signature
38 NULL, // Image handle
39 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER, // Image type
40 TRUE, // If entrypoint has been called
41 NULL, // EntryPoint
42 {
43 EFI_LOADED_IMAGE_INFORMATION_REVISION, // Revision
44 NULL, // Parent handle
45 NULL, // System handle
46
47 NULL, // Device handle
48 NULL, // File path
49 NULL, // Reserved
50
51 0, // LoadOptionsSize
52 NULL, // LoadOptions
53
54 NULL, // ImageBase
55 0, // ImageSize
56 EfiBootServicesCode, // ImageCodeType
57 EfiBootServicesData // ImageDataType
58 },
59 (EFI_PHYSICAL_ADDRESS)0, // ImageBasePage
60 0, // NumberOfPages
61 NULL, // FixupData
62 0, // Tpl
63 EFI_SUCCESS, // Status
64 0, // ExitDataSize
65 NULL, // ExitData
66 NULL, // JumpBuffer
67 NULL, // JumpContext
68 0, // Machine
69 NULL, // Ebc
70 NULL, // RuntimeData
71 NULL // LoadedImageDevicePath
72 };
73 //
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
76 // available or not.
77 //
78 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mDxeCodeMemoryRangeUsageBitMap=NULL;
79
80 typedef struct {
81 UINT16 MachineType;
82 CHAR16 *MachineTypeName;
83 } MACHINE_TYPE_INFO;
84
85 //
86 // EBC machine is not listed in this table, because EBC is in the default supported scopes of other machine type.
87 //
88 GLOBAL_REMOVE_IF_UNREFERENCED MACHINE_TYPE_INFO mMachineTypeInfo[] = {
89 {EFI_IMAGE_MACHINE_IA32, L"IA32"},
90 {EFI_IMAGE_MACHINE_IA64, L"IA64"},
91 {EFI_IMAGE_MACHINE_X64, L"X64"},
92 {EFI_IMAGE_MACHINE_ARMTHUMB_MIXED, L"ARM"},
93 {EFI_IMAGE_MACHINE_AARCH64, L"AARCH64"}
94 };
95
96 UINT16 mDxeCoreImageMachineType = 0;
97
98 /**
99 Return machine type name.
100
101 @param MachineType The machine type
102
103 @return machine type name
104 **/
105 CHAR16 *
106 GetMachineTypeName (
107 UINT16 MachineType
108 )
109 {
110 UINTN Index;
111
112 for (Index = 0; Index < sizeof(mMachineTypeInfo)/sizeof(mMachineTypeInfo[0]); Index++) {
113 if (mMachineTypeInfo[Index].MachineType == MachineType) {
114 return mMachineTypeInfo[Index].MachineTypeName;
115 }
116 }
117
118 return L"<Unknown>";
119 }
120
121 /**
122 Add the Image Services to EFI Boot Services Table and install the protocol
123 interfaces for this image.
124
125 @param HobStart The HOB to initialize
126
127 @return Status code.
128
129 **/
130 EFI_STATUS
131 CoreInitializeImageServices (
132 IN VOID *HobStart
133 )
134 {
135 EFI_STATUS Status;
136 LOADED_IMAGE_PRIVATE_DATA *Image;
137 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress;
138 UINT64 DxeCoreImageLength;
139 VOID *DxeCoreEntryPoint;
140 EFI_PEI_HOB_POINTERS DxeCoreHob;
141
142 //
143 // Searching for image hob
144 //
145 DxeCoreHob.Raw = HobStart;
146 while ((DxeCoreHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, DxeCoreHob.Raw)) != NULL) {
147 if (CompareGuid (&DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.Name, &gEfiHobMemoryAllocModuleGuid)) {
148 //
149 // Find Dxe Core HOB
150 //
151 break;
152 }
153 DxeCoreHob.Raw = GET_NEXT_HOB (DxeCoreHob);
154 }
155 ASSERT (DxeCoreHob.Raw != NULL);
156
157 DxeCoreImageBaseAddress = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryBaseAddress;
158 DxeCoreImageLength = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength;
159 DxeCoreEntryPoint = (VOID *) (UINTN) DxeCoreHob.MemoryAllocationModule->EntryPoint;
160 gDxeCoreFileName = &DxeCoreHob.MemoryAllocationModule->ModuleName;
161
162 //
163 // Initialize the fields for an internal driver
164 //
165 Image = &mCorePrivateImage;
166
167 Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)DxeCoreEntryPoint;
168 Image->ImageBasePage = DxeCoreImageBaseAddress;
169 Image->NumberOfPages = (UINTN)(EFI_SIZE_TO_PAGES((UINTN)(DxeCoreImageLength)));
170 Image->Tpl = gEfiCurrentTpl;
171 Image->Info.SystemTable = gDxeCoreST;
172 Image->Info.ImageBase = (VOID *)(UINTN)DxeCoreImageBaseAddress;
173 Image->Info.ImageSize = DxeCoreImageLength;
174
175 //
176 // Install the protocol interfaces for this image
177 //
178 Status = CoreInstallProtocolInterface (
179 &Image->Handle,
180 &gEfiLoadedImageProtocolGuid,
181 EFI_NATIVE_INTERFACE,
182 &Image->Info
183 );
184 ASSERT_EFI_ERROR (Status);
185
186 mCurrentImage = Image;
187
188 //
189 // Fill in DXE globals
190 //
191 mDxeCoreImageMachineType = PeCoffLoaderGetMachineType (Image->Info.ImageBase);
192 gDxeCoreImageHandle = Image->Handle;
193 gDxeCoreLoadedImage = &Image->Info;
194
195 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
196 //
197 // Export DXE Core PE Loader functionality for backward compatibility.
198 //
199 Status = CoreInstallProtocolInterface (
200 &mLoadPe32PrivateData.Handle,
201 &gEfiLoadPeImageProtocolGuid,
202 EFI_NATIVE_INTERFACE,
203 &mLoadPe32PrivateData.Pe32Image
204 );
205 }
206
207 ProtectUefiImage (&Image->Info, Image->LoadedImageDevicePath);
208
209 return Status;
210 }
211
212 /**
213 Read image file (specified by UserHandle) into user specified buffer with specified offset
214 and length.
215
216 @param UserHandle Image file handle
217 @param Offset Offset to the source file
218 @param ReadSize For input, pointer of size to read; For output,
219 pointer of size actually read.
220 @param Buffer Buffer to write into
221
222 @retval EFI_SUCCESS Successfully read the specified part of file
223 into buffer.
224
225 **/
226 EFI_STATUS
227 EFIAPI
228 CoreReadImageFile (
229 IN VOID *UserHandle,
230 IN UINTN Offset,
231 IN OUT UINTN *ReadSize,
232 OUT VOID *Buffer
233 )
234 {
235 UINTN EndPosition;
236 IMAGE_FILE_HANDLE *FHand;
237
238 if (UserHandle == NULL || ReadSize == NULL || Buffer == NULL) {
239 return EFI_INVALID_PARAMETER;
240 }
241
242 if (MAX_ADDRESS - Offset < *ReadSize) {
243 return EFI_INVALID_PARAMETER;
244 }
245
246 FHand = (IMAGE_FILE_HANDLE *)UserHandle;
247 ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);
248
249 //
250 // Move data from our local copy of the file
251 //
252 EndPosition = Offset + *ReadSize;
253 if (EndPosition > FHand->SourceSize) {
254 *ReadSize = (UINT32)(FHand->SourceSize - Offset);
255 }
256 if (Offset >= FHand->SourceSize) {
257 *ReadSize = 0;
258 }
259
260 CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);
261 return EFI_SUCCESS;
262 }
263 /**
264 To check memory usage bit map array to figure out if the memory range the image will be loaded in is available or not. If
265 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.
266 The function is only invoked when load modules at fixed address feature is enabled.
267
268 @param ImageBase The base address the image will be loaded at.
269 @param ImageSize The size of the image
270
271 @retval EFI_SUCCESS The memory range the image will be loaded in is available
272 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
273 **/
274 EFI_STATUS
275 CheckAndMarkFixLoadingMemoryUsageBitMap (
276 IN EFI_PHYSICAL_ADDRESS ImageBase,
277 IN UINTN ImageSize
278 )
279 {
280 UINT32 DxeCodePageNumber;
281 UINT64 DxeCodeSize;
282 EFI_PHYSICAL_ADDRESS DxeCodeBase;
283 UINTN BaseOffsetPageNumber;
284 UINTN TopOffsetPageNumber;
285 UINTN Index;
286 //
287 // The DXE code range includes RuntimeCodePage range and Boot time code range.
288 //
289 DxeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);
290 DxeCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);
291 DxeCodeSize = EFI_PAGES_TO_SIZE(DxeCodePageNumber);
292 DxeCodeBase = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - DxeCodeSize;
293
294 //
295 // If the memory usage bit map is not initialized, do it. Every bit in the array
296 // indicate the status of the corresponding memory page, available or not
297 //
298 if (mDxeCodeMemoryRangeUsageBitMap == NULL) {
299 mDxeCodeMemoryRangeUsageBitMap = AllocateZeroPool(((DxeCodePageNumber/64) + 1)*sizeof(UINT64));
300 }
301 //
302 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
303 //
304 if (!gLoadFixedAddressCodeMemoryReady || mDxeCodeMemoryRangeUsageBitMap == NULL) {
305 return EFI_NOT_FOUND;
306 }
307 //
308 // Test the memory range for loading the image in the DXE code range.
309 //
310 if (gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress < ImageBase + ImageSize ||
311 DxeCodeBase > ImageBase) {
312 return EFI_NOT_FOUND;
313 }
314 //
315 // Test if the memory is avalaible or not.
316 //
317 BaseOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase - DxeCodeBase));
318 TopOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - DxeCodeBase));
319 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
320 if ((mDxeCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
321 //
322 // This page is already used.
323 //
324 return EFI_NOT_FOUND;
325 }
326 }
327
328 //
329 // Being here means the memory range is available. So mark the bits for the memory range
330 //
331 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
332 mDxeCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
333 }
334 return EFI_SUCCESS;
335 }
336 /**
337
338 Get the fixed loading address from image header assigned by build tool. This function only be called
339 when Loading module at Fixed address feature enabled.
340
341 @param ImageContext Pointer to the image context structure that describes the PE/COFF
342 image that needs to be examined by this function.
343 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
344 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
345
346 **/
347 EFI_STATUS
348 GetPeCoffImageFixLoadingAssignedAddress(
349 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
350 )
351 {
352 UINTN SectionHeaderOffset;
353 EFI_STATUS Status;
354 EFI_IMAGE_SECTION_HEADER SectionHeader;
355 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
356 UINT16 Index;
357 UINTN Size;
358 UINT16 NumberOfSections;
359 IMAGE_FILE_HANDLE *Handle;
360 UINT64 ValueInSectionHeader;
361
362
363 Status = EFI_NOT_FOUND;
364
365 //
366 // Get PeHeader pointer
367 //
368 Handle = (IMAGE_FILE_HANDLE*)ImageContext->Handle;
369 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )Handle->Source + ImageContext->PeCoffHeaderOffset);
370 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
371 sizeof (UINT32) +
372 sizeof (EFI_IMAGE_FILE_HEADER) +
373 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
374 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
375
376 //
377 // Get base address from the first section header that doesn't point to code section.
378 //
379 for (Index = 0; Index < NumberOfSections; Index++) {
380 //
381 // Read section header from file
382 //
383 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
384 Status = ImageContext->ImageRead (
385 ImageContext->Handle,
386 SectionHeaderOffset,
387 &Size,
388 &SectionHeader
389 );
390 if (EFI_ERROR (Status)) {
391 return Status;
392 }
393 if (Size != sizeof (EFI_IMAGE_SECTION_HEADER)) {
394 return EFI_NOT_FOUND;
395 }
396
397 Status = EFI_NOT_FOUND;
398
399 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
400 //
401 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
402 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
403 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
404 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fields should be set to Zero
405 //
406 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
407 if (ValueInSectionHeader != 0) {
408 //
409 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
410 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
411 // relative to top address
412 //
413 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) < 0) {
414 ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress;
415 }
416 //
417 // Check if the memory range is available.
418 //
419 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext->ImageAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
420 }
421 break;
422 }
423 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
424 }
425 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));
426 return Status;
427 }
428 /**
429 Loads, relocates, and invokes a PE/COFF image
430
431 @param BootPolicy If TRUE, indicates that the request originates
432 from the boot manager, and that the boot
433 manager is attempting to load FilePath as a
434 boot selection.
435 @param Pe32Handle The handle of PE32 image
436 @param Image PE image to be loaded
437 @param DstBuffer The buffer to store the image
438 @param EntryPoint A pointer to the entry point
439 @param Attribute The bit mask of attributes to set for the load
440 PE image
441
442 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
443 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
444 relocate the PE/COFF file
445 @retval EFI_INVALID_PARAMETER Invalid parameter
446 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
447
448 **/
449 EFI_STATUS
450 CoreLoadPeImage (
451 IN BOOLEAN BootPolicy,
452 IN VOID *Pe32Handle,
453 IN LOADED_IMAGE_PRIVATE_DATA *Image,
454 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
455 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
456 IN UINT32 Attribute
457 )
458 {
459 EFI_STATUS Status;
460 BOOLEAN DstBufAlocated;
461 UINTN Size;
462
463 ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));
464
465 Image->ImageContext.Handle = Pe32Handle;
466 Image->ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)CoreReadImageFile;
467
468 //
469 // Get information about the image being loaded
470 //
471 Status = PeCoffLoaderGetImageInfo (&Image->ImageContext);
472 if (EFI_ERROR (Status)) {
473 return Status;
474 }
475
476 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) {
477 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image->ImageContext.Machine)) {
478 //
479 // The PE/COFF loader can support loading image types that can be executed.
480 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
481 //
482 DEBUG ((EFI_D_ERROR, "Image type %s can't be loaded ", GetMachineTypeName(Image->ImageContext.Machine)));
483 DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType)));
484 return EFI_UNSUPPORTED;
485 }
486 }
487
488 //
489 // Set EFI memory type based on ImageType
490 //
491 switch (Image->ImageContext.ImageType) {
492 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
493 Image->ImageContext.ImageCodeMemoryType = EfiLoaderCode;
494 Image->ImageContext.ImageDataMemoryType = EfiLoaderData;
495 break;
496 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
497 Image->ImageContext.ImageCodeMemoryType = EfiBootServicesCode;
498 Image->ImageContext.ImageDataMemoryType = EfiBootServicesData;
499 break;
500 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
501 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
502 Image->ImageContext.ImageCodeMemoryType = EfiRuntimeServicesCode;
503 Image->ImageContext.ImageDataMemoryType = EfiRuntimeServicesData;
504 break;
505 default:
506 Image->ImageContext.ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
507 return EFI_UNSUPPORTED;
508 }
509
510 //
511 // Allocate memory of the correct memory type aligned on the required image boundary
512 //
513 DstBufAlocated = FALSE;
514 if (DstBuffer == 0) {
515 //
516 // Allocate Destination Buffer as caller did not pass it in
517 //
518
519 if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {
520 Size = (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment;
521 } else {
522 Size = (UINTN)Image->ImageContext.ImageSize;
523 }
524
525 Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size);
526
527 //
528 // If the image relocations have not been stripped, then load at any address.
529 // Otherwise load at the address at which it was linked.
530 //
531 // Memory below 1MB should be treated reserved for CSM and there should be
532 // no modules whose preferred load addresses are below 1MB.
533 //
534 Status = EFI_OUT_OF_RESOURCES;
535 //
536 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
537 // a specified address.
538 //
539 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 ) {
540 Status = GetPeCoffImageFixLoadingAssignedAddress (&(Image->ImageContext));
541
542 if (EFI_ERROR (Status)) {
543 //
544 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
545 //
546 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
547
548 Status = CoreAllocatePages (
549 AllocateAnyPages,
550 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
551 Image->NumberOfPages,
552 &Image->ImageContext.ImageAddress
553 );
554 }
555 } else {
556 if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) {
557 Status = CoreAllocatePages (
558 AllocateAddress,
559 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
560 Image->NumberOfPages,
561 &Image->ImageContext.ImageAddress
562 );
563 }
564 if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {
565 Status = CoreAllocatePages (
566 AllocateAnyPages,
567 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
568 Image->NumberOfPages,
569 &Image->ImageContext.ImageAddress
570 );
571 }
572 }
573 if (EFI_ERROR (Status)) {
574 return Status;
575 }
576 DstBufAlocated = TRUE;
577 } else {
578 //
579 // Caller provided the destination buffer
580 //
581
582 if (Image->ImageContext.RelocationsStripped && (Image->ImageContext.ImageAddress != DstBuffer)) {
583 //
584 // If the image relocations were stripped, and the caller provided a
585 // destination buffer address that does not match the address that the
586 // image is linked at, then the image cannot be loaded.
587 //
588 return EFI_INVALID_PARAMETER;
589 }
590
591 if (Image->NumberOfPages != 0 &&
592 Image->NumberOfPages <
593 (EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment))) {
594 Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);
595 return EFI_BUFFER_TOO_SMALL;
596 }
597
598 Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);
599 Image->ImageContext.ImageAddress = DstBuffer;
600 }
601
602 Image->ImageBasePage = Image->ImageContext.ImageAddress;
603 if (!Image->ImageContext.IsTeImage) {
604 Image->ImageContext.ImageAddress =
605 (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &
606 ~((UINTN)Image->ImageContext.SectionAlignment - 1);
607 }
608
609 //
610 // Load the image from the file into the allocated memory
611 //
612 Status = PeCoffLoaderLoadImage (&Image->ImageContext);
613 if (EFI_ERROR (Status)) {
614 goto Done;
615 }
616
617 //
618 // If this is a Runtime Driver, then allocate memory for the FixupData that
619 // is used to relocate the image when SetVirtualAddressMap() is called. The
620 // relocation is done by the Runtime AP.
621 //
622 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) {
623 if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
624 Image->ImageContext.FixupData = AllocateRuntimePool ((UINTN)(Image->ImageContext.FixupDataSize));
625 if (Image->ImageContext.FixupData == NULL) {
626 Status = EFI_OUT_OF_RESOURCES;
627 goto Done;
628 }
629 }
630 }
631
632 //
633 // Relocate the image in memory
634 //
635 Status = PeCoffLoaderRelocateImage (&Image->ImageContext);
636 if (EFI_ERROR (Status)) {
637 goto Done;
638 }
639
640 //
641 // Flush the Instruction Cache
642 //
643 InvalidateInstructionCacheRange ((VOID *)(UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.ImageSize);
644
645 //
646 // Copy the machine type from the context to the image private data. This
647 // is needed during image unload to know if we should call an EBC protocol
648 // to unload the image.
649 //
650 Image->Machine = Image->ImageContext.Machine;
651
652 //
653 // Get the image entry point. If it's an EBC image, then call into the
654 // interpreter to create a thunk for the entry point and use the returned
655 // value for the entry point.
656 //
657 Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)Image->ImageContext.EntryPoint;
658 if (Image->ImageContext.Machine == EFI_IMAGE_MACHINE_EBC) {
659 //
660 // Locate the EBC interpreter protocol
661 //
662 Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);
663 if (EFI_ERROR(Status) || Image->Ebc == NULL) {
664 DEBUG ((DEBUG_LOAD | DEBUG_ERROR, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
665 goto Done;
666 }
667
668 //
669 // Register a callback for flushing the instruction cache so that created
670 // thunks can be flushed.
671 //
672 Status = Image->Ebc->RegisterICacheFlush (Image->Ebc, (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange);
673 if (EFI_ERROR(Status)) {
674 goto Done;
675 }
676
677 //
678 // Create a thunk for the image's entry point. This will be the new
679 // entry point for the image.
680 //
681 Status = Image->Ebc->CreateThunk (
682 Image->Ebc,
683 Image->Handle,
684 (VOID *)(UINTN) Image->ImageContext.EntryPoint,
685 (VOID **) &Image->EntryPoint
686 );
687 if (EFI_ERROR(Status)) {
688 goto Done;
689 }
690 }
691
692 //
693 // Fill in the image information for the Loaded Image Protocol
694 //
695 Image->Type = Image->ImageContext.ImageType;
696 Image->Info.ImageBase = (VOID *)(UINTN)Image->ImageContext.ImageAddress;
697 Image->Info.ImageSize = Image->ImageContext.ImageSize;
698 Image->Info.ImageCodeType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType);
699 Image->Info.ImageDataType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageDataMemoryType);
700 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) {
701 if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
702 //
703 // Make a list off all the RT images so we can let the RT AP know about them.
704 //
705 Image->RuntimeData = AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY));
706 if (Image->RuntimeData == NULL) {
707 goto Done;
708 }
709 Image->RuntimeData->ImageBase = Image->Info.ImageBase;
710 Image->RuntimeData->ImageSize = (UINT64) (Image->Info.ImageSize);
711 Image->RuntimeData->RelocationData = Image->ImageContext.FixupData;
712 Image->RuntimeData->Handle = Image->Handle;
713 InsertTailList (&gRuntime->ImageHead, &Image->RuntimeData->Link);
714 InsertImageRecord (Image->RuntimeData);
715 }
716 }
717
718 //
719 // Fill in the entry point of the image if it is available
720 //
721 if (EntryPoint != NULL) {
722 *EntryPoint = Image->ImageContext.EntryPoint;
723 }
724
725 //
726 // Print the load address and the PDB file name if it is available
727 //
728
729 DEBUG_CODE_BEGIN ();
730
731 UINTN Index;
732 UINTN StartIndex;
733 CHAR8 EfiFileName[256];
734
735
736 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
737 "Loading driver at 0x%11p EntryPoint=0x%11p ",
738 (VOID *)(UINTN) Image->ImageContext.ImageAddress,
739 FUNCTION_ENTRY_POINT (Image->ImageContext.EntryPoint)));
740
741
742 //
743 // Print Module Name by Pdb file path.
744 // Windows and Unix style file path are all trimmed correctly.
745 //
746 if (Image->ImageContext.PdbPointer != NULL) {
747 StartIndex = 0;
748 for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) {
749 if ((Image->ImageContext.PdbPointer[Index] == '\\') || (Image->ImageContext.PdbPointer[Index] == '/')) {
750 StartIndex = Index + 1;
751 }
752 }
753 //
754 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
755 // The PDB file name is limited in the range of 0~255.
756 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
757 //
758 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
759 EfiFileName[Index] = Image->ImageContext.PdbPointer[Index + StartIndex];
760 if (EfiFileName[Index] == 0) {
761 EfiFileName[Index] = '.';
762 }
763 if (EfiFileName[Index] == '.') {
764 EfiFileName[Index + 1] = 'e';
765 EfiFileName[Index + 2] = 'f';
766 EfiFileName[Index + 3] = 'i';
767 EfiFileName[Index + 4] = 0;
768 break;
769 }
770 }
771
772 if (Index == sizeof (EfiFileName) - 4) {
773 EfiFileName[Index] = 0;
774 }
775 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
776 }
777 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
778
779 DEBUG_CODE_END ();
780
781 return EFI_SUCCESS;
782
783 Done:
784
785 //
786 // Free memory.
787 //
788
789 if (DstBufAlocated) {
790 CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);
791 }
792
793 if (Image->ImageContext.FixupData != NULL) {
794 CoreFreePool (Image->ImageContext.FixupData);
795 }
796
797 return Status;
798 }
799
800
801
802 /**
803 Get the image's private data from its handle.
804
805 @param ImageHandle The image handle
806
807 @return Return the image private data associated with ImageHandle.
808
809 **/
810 LOADED_IMAGE_PRIVATE_DATA *
811 CoreLoadedImageInfo (
812 IN EFI_HANDLE ImageHandle
813 )
814 {
815 EFI_STATUS Status;
816 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
817 LOADED_IMAGE_PRIVATE_DATA *Image;
818
819 Status = CoreHandleProtocol (
820 ImageHandle,
821 &gEfiLoadedImageProtocolGuid,
822 (VOID **)&LoadedImage
823 );
824 if (!EFI_ERROR (Status)) {
825 Image = LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage);
826 } else {
827 DEBUG ((DEBUG_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle));
828 Image = NULL;
829 }
830
831 return Image;
832 }
833
834
835 /**
836 Unloads EFI image from memory.
837
838 @param Image EFI image
839 @param FreePage Free allocated pages
840
841 **/
842 VOID
843 CoreUnloadAndCloseImage (
844 IN LOADED_IMAGE_PRIVATE_DATA *Image,
845 IN BOOLEAN FreePage
846 )
847 {
848 EFI_STATUS Status;
849 UINTN HandleCount;
850 EFI_HANDLE *HandleBuffer;
851 UINTN HandleIndex;
852 EFI_GUID **ProtocolGuidArray;
853 UINTN ArrayCount;
854 UINTN ProtocolIndex;
855 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
856 UINTN OpenInfoCount;
857 UINTN OpenInfoIndex;
858
859 HandleBuffer = NULL;
860 ProtocolGuidArray = NULL;
861
862 if (Image->Started) {
863 UnregisterMemoryProfileImage (Image);
864 }
865
866 UnprotectUefiImage (&Image->Info, Image->LoadedImageDevicePath);
867
868 if (Image->Ebc != NULL) {
869 //
870 // If EBC protocol exists we must perform cleanups for this image.
871 //
872 Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);
873 }
874
875 //
876 // Unload image, free Image->ImageContext->ModHandle
877 //
878 PeCoffLoaderUnloadImage (&Image->ImageContext);
879
880 //
881 // Free our references to the image handle
882 //
883 if (Image->Handle != NULL) {
884
885 Status = CoreLocateHandleBuffer (
886 AllHandles,
887 NULL,
888 NULL,
889 &HandleCount,
890 &HandleBuffer
891 );
892 if (!EFI_ERROR (Status)) {
893 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
894 Status = CoreProtocolsPerHandle (
895 HandleBuffer[HandleIndex],
896 &ProtocolGuidArray,
897 &ArrayCount
898 );
899 if (!EFI_ERROR (Status)) {
900 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
901 Status = CoreOpenProtocolInformation (
902 HandleBuffer[HandleIndex],
903 ProtocolGuidArray[ProtocolIndex],
904 &OpenInfo,
905 &OpenInfoCount
906 );
907 if (!EFI_ERROR (Status)) {
908 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
909 if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {
910 Status = CoreCloseProtocol (
911 HandleBuffer[HandleIndex],
912 ProtocolGuidArray[ProtocolIndex],
913 Image->Handle,
914 OpenInfo[OpenInfoIndex].ControllerHandle
915 );
916 }
917 }
918 if (OpenInfo != NULL) {
919 CoreFreePool(OpenInfo);
920 }
921 }
922 }
923 if (ProtocolGuidArray != NULL) {
924 CoreFreePool(ProtocolGuidArray);
925 }
926 }
927 }
928 if (HandleBuffer != NULL) {
929 CoreFreePool (HandleBuffer);
930 }
931 }
932
933 CoreRemoveDebugImageInfoEntry (Image->Handle);
934
935 Status = CoreUninstallProtocolInterface (
936 Image->Handle,
937 &gEfiLoadedImageDevicePathProtocolGuid,
938 Image->LoadedImageDevicePath
939 );
940
941 Status = CoreUninstallProtocolInterface (
942 Image->Handle,
943 &gEfiLoadedImageProtocolGuid,
944 &Image->Info
945 );
946
947 if (Image->ImageContext.HiiResourceData != 0) {
948 Status = CoreUninstallProtocolInterface (
949 Image->Handle,
950 &gEfiHiiPackageListProtocolGuid,
951 (VOID *) (UINTN) Image->ImageContext.HiiResourceData
952 );
953 }
954
955 }
956
957 if (Image->RuntimeData != NULL) {
958 if (Image->RuntimeData->Link.ForwardLink != NULL) {
959 //
960 // Remove the Image from the Runtime Image list as we are about to Free it!
961 //
962 RemoveEntryList (&Image->RuntimeData->Link);
963 RemoveImageRecord (Image->RuntimeData);
964 }
965 CoreFreePool (Image->RuntimeData);
966 }
967
968 //
969 // Free the Image from memory
970 //
971 if ((Image->ImageBasePage != 0) && FreePage) {
972 CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);
973 }
974
975 //
976 // Done with the Image structure
977 //
978 if (Image->Info.FilePath != NULL) {
979 CoreFreePool (Image->Info.FilePath);
980 }
981
982 if (Image->LoadedImageDevicePath != NULL) {
983 CoreFreePool (Image->LoadedImageDevicePath);
984 }
985
986 if (Image->FixupData != NULL) {
987 CoreFreePool (Image->FixupData);
988 }
989
990 CoreFreePool (Image);
991 }
992
993
994 /**
995 Loads an EFI image into memory and returns a handle to the image.
996
997 @param BootPolicy If TRUE, indicates that the request originates
998 from the boot manager, and that the boot
999 manager is attempting to load FilePath as a
1000 boot selection.
1001 @param ParentImageHandle The caller's image handle.
1002 @param FilePath The specific file path from which the image is
1003 loaded.
1004 @param SourceBuffer If not NULL, a pointer to the memory location
1005 containing a copy of the image to be loaded.
1006 @param SourceSize The size in bytes of SourceBuffer.
1007 @param DstBuffer The buffer to store the image
1008 @param NumberOfPages If not NULL, it inputs a pointer to the page
1009 number of DstBuffer and outputs a pointer to
1010 the page number of the image. If this number is
1011 not enough, return EFI_BUFFER_TOO_SMALL and
1012 this parameter contains the required number.
1013 @param ImageHandle Pointer to the returned image handle that is
1014 created when the image is successfully loaded.
1015 @param EntryPoint A pointer to the entry point
1016 @param Attribute The bit mask of attributes to set for the load
1017 PE image
1018
1019 @retval EFI_SUCCESS The image was loaded into memory.
1020 @retval EFI_NOT_FOUND The FilePath was not found.
1021 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1022 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
1023 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1024 path cannot be parsed to locate the proper
1025 protocol for loading the file.
1026 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1027 resources.
1028 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1029 understood.
1030 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1031 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1032 image from being loaded. NULL is returned in *ImageHandle.
1033 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1034 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1035 platform policy specifies that the image should not be started.
1036
1037 **/
1038 EFI_STATUS
1039 CoreLoadImageCommon (
1040 IN BOOLEAN BootPolicy,
1041 IN EFI_HANDLE ParentImageHandle,
1042 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1043 IN VOID *SourceBuffer OPTIONAL,
1044 IN UINTN SourceSize,
1045 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
1046 IN OUT UINTN *NumberOfPages OPTIONAL,
1047 OUT EFI_HANDLE *ImageHandle,
1048 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
1049 IN UINT32 Attribute
1050 )
1051 {
1052 LOADED_IMAGE_PRIVATE_DATA *Image;
1053 LOADED_IMAGE_PRIVATE_DATA *ParentImage;
1054 IMAGE_FILE_HANDLE FHand;
1055 EFI_STATUS Status;
1056 EFI_STATUS SecurityStatus;
1057 EFI_HANDLE DeviceHandle;
1058 UINT32 AuthenticationStatus;
1059 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
1060 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
1061 EFI_DEVICE_PATH_PROTOCOL *InputFilePath;
1062 EFI_DEVICE_PATH_PROTOCOL *Node;
1063 UINTN FilePathSize;
1064 BOOLEAN ImageIsFromFv;
1065 BOOLEAN ImageIsFromLoadFile;
1066
1067 SecurityStatus = EFI_SUCCESS;
1068
1069 ASSERT (gEfiCurrentTpl < TPL_NOTIFY);
1070 ParentImage = NULL;
1071
1072 //
1073 // The caller must pass in a valid ParentImageHandle
1074 //
1075 if (ImageHandle == NULL || ParentImageHandle == NULL) {
1076 return EFI_INVALID_PARAMETER;
1077 }
1078
1079 ParentImage = CoreLoadedImageInfo (ParentImageHandle);
1080 if (ParentImage == NULL) {
1081 DEBUG((DEBUG_LOAD|DEBUG_ERROR, "LoadImageEx: Parent handle not an image handle\n"));
1082 return EFI_INVALID_PARAMETER;
1083 }
1084
1085 ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE));
1086 FHand.Signature = IMAGE_FILE_HANDLE_SIGNATURE;
1087 OriginalFilePath = FilePath;
1088 InputFilePath = FilePath;
1089 HandleFilePath = FilePath;
1090 DeviceHandle = NULL;
1091 Status = EFI_SUCCESS;
1092 AuthenticationStatus = 0;
1093 ImageIsFromFv = FALSE;
1094 ImageIsFromLoadFile = FALSE;
1095
1096 //
1097 // If the caller passed a copy of the file, then just use it
1098 //
1099 if (SourceBuffer != NULL) {
1100 FHand.Source = SourceBuffer;
1101 FHand.SourceSize = SourceSize;
1102 Status = CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle);
1103 if (EFI_ERROR (Status)) {
1104 DeviceHandle = NULL;
1105 }
1106 if (SourceSize > 0) {
1107 Status = EFI_SUCCESS;
1108 } else {
1109 Status = EFI_LOAD_ERROR;
1110 }
1111 } else {
1112 if (FilePath == NULL) {
1113 return EFI_INVALID_PARAMETER;
1114 }
1115
1116 //
1117 // Try to get the image device handle by checking the match protocol.
1118 //
1119 Node = NULL;
1120 Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);
1121 if (!EFI_ERROR (Status)) {
1122 ImageIsFromFv = TRUE;
1123 } else {
1124 HandleFilePath = FilePath;
1125 Status = CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &HandleFilePath, &DeviceHandle);
1126 if (EFI_ERROR (Status)) {
1127 if (!BootPolicy) {
1128 HandleFilePath = FilePath;
1129 Status = CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid, &HandleFilePath, &DeviceHandle);
1130 }
1131 if (EFI_ERROR (Status)) {
1132 HandleFilePath = FilePath;
1133 Status = CoreLocateDevicePath (&gEfiLoadFileProtocolGuid, &HandleFilePath, &DeviceHandle);
1134 if (!EFI_ERROR (Status)) {
1135 ImageIsFromLoadFile = TRUE;
1136 Node = HandleFilePath;
1137 }
1138 }
1139 }
1140 }
1141
1142 //
1143 // Get the source file buffer by its device path.
1144 //
1145 FHand.Source = GetFileBufferByFilePath (
1146 BootPolicy,
1147 FilePath,
1148 &FHand.SourceSize,
1149 &AuthenticationStatus
1150 );
1151 if (FHand.Source == NULL) {
1152 Status = EFI_NOT_FOUND;
1153 } else {
1154 FHand.FreeBuffer = TRUE;
1155 if (ImageIsFromLoadFile) {
1156 //
1157 // LoadFile () may cause the device path of the Handle be updated.
1158 //
1159 OriginalFilePath = AppendDevicePath (DevicePathFromHandle (DeviceHandle), Node);
1160 }
1161 }
1162 }
1163
1164 if (EFI_ERROR (Status)) {
1165 Image = NULL;
1166 goto Done;
1167 }
1168
1169 if (gSecurity2 != NULL) {
1170 //
1171 // Verify File Authentication through the Security2 Architectural Protocol
1172 //
1173 SecurityStatus = gSecurity2->FileAuthentication (
1174 gSecurity2,
1175 OriginalFilePath,
1176 FHand.Source,
1177 FHand.SourceSize,
1178 BootPolicy
1179 );
1180 if (!EFI_ERROR (SecurityStatus) && ImageIsFromFv) {
1181 //
1182 // When Security2 is installed, Security Architectural Protocol must be published.
1183 //
1184 ASSERT (gSecurity != NULL);
1185
1186 //
1187 // Verify the Authentication Status through the Security Architectural Protocol
1188 // Only on images that have been read using Firmware Volume protocol.
1189 //
1190 SecurityStatus = gSecurity->FileAuthenticationState (
1191 gSecurity,
1192 AuthenticationStatus,
1193 OriginalFilePath
1194 );
1195 }
1196 } else if ((gSecurity != NULL) && (OriginalFilePath != NULL)) {
1197 //
1198 // Verify the Authentication Status through the Security Architectural Protocol
1199 //
1200 SecurityStatus = gSecurity->FileAuthenticationState (
1201 gSecurity,
1202 AuthenticationStatus,
1203 OriginalFilePath
1204 );
1205 }
1206
1207 //
1208 // Check Security Status.
1209 //
1210 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
1211 if (SecurityStatus == EFI_ACCESS_DENIED) {
1212 //
1213 // Image was not loaded because the platform policy prohibits the image from being loaded.
1214 // It's the only place we could meet EFI_ACCESS_DENIED.
1215 //
1216 *ImageHandle = NULL;
1217 }
1218 Status = SecurityStatus;
1219 Image = NULL;
1220 goto Done;
1221 }
1222
1223 //
1224 // Allocate a new image structure
1225 //
1226 Image = AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA));
1227 if (Image == NULL) {
1228 Status = EFI_OUT_OF_RESOURCES;
1229 goto Done;
1230 }
1231
1232 //
1233 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1234 //
1235 FilePath = OriginalFilePath;
1236 if (DeviceHandle != NULL) {
1237 Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
1238 if (!EFI_ERROR (Status)) {
1239 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
1240 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );
1241 }
1242 }
1243 //
1244 // Initialize the fields for an internal driver
1245 //
1246 Image->Signature = LOADED_IMAGE_PRIVATE_DATA_SIGNATURE;
1247 Image->Info.SystemTable = gDxeCoreST;
1248 Image->Info.DeviceHandle = DeviceHandle;
1249 Image->Info.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1250 Image->Info.FilePath = DuplicateDevicePath (FilePath);
1251 Image->Info.ParentHandle = ParentImageHandle;
1252
1253
1254 if (NumberOfPages != NULL) {
1255 Image->NumberOfPages = *NumberOfPages ;
1256 } else {
1257 Image->NumberOfPages = 0 ;
1258 }
1259
1260 //
1261 // Install the protocol interfaces for this image
1262 // don't fire notifications yet
1263 //
1264 Status = CoreInstallProtocolInterfaceNotify (
1265 &Image->Handle,
1266 &gEfiLoadedImageProtocolGuid,
1267 EFI_NATIVE_INTERFACE,
1268 &Image->Info,
1269 FALSE
1270 );
1271 if (EFI_ERROR (Status)) {
1272 goto Done;
1273 }
1274
1275 //
1276 // Load the image. If EntryPoint is Null, it will not be set.
1277 //
1278 Status = CoreLoadPeImage (BootPolicy, &FHand, Image, DstBuffer, EntryPoint, Attribute);
1279 if (EFI_ERROR (Status)) {
1280 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_OUT_OF_RESOURCES)) {
1281 if (NumberOfPages != NULL) {
1282 *NumberOfPages = Image->NumberOfPages;
1283 }
1284 }
1285 goto Done;
1286 }
1287
1288 if (NumberOfPages != NULL) {
1289 *NumberOfPages = Image->NumberOfPages;
1290 }
1291
1292 //
1293 // Register the image in the Debug Image Info Table if the attribute is set
1294 //
1295 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) != 0) {
1296 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL, &Image->Info, Image->Handle);
1297 }
1298
1299 //
1300 //Reinstall loaded image protocol to fire any notifications
1301 //
1302 Status = CoreReinstallProtocolInterface (
1303 Image->Handle,
1304 &gEfiLoadedImageProtocolGuid,
1305 &Image->Info,
1306 &Image->Info
1307 );
1308 if (EFI_ERROR (Status)) {
1309 goto Done;
1310 }
1311
1312 //
1313 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1314 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1315 //
1316 if (OriginalFilePath != NULL) {
1317 Image->LoadedImageDevicePath = DuplicateDevicePath (OriginalFilePath);
1318 }
1319
1320 //
1321 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1322 //
1323 Status = CoreInstallProtocolInterface (
1324 &Image->Handle,
1325 &gEfiLoadedImageDevicePathProtocolGuid,
1326 EFI_NATIVE_INTERFACE,
1327 Image->LoadedImageDevicePath
1328 );
1329 if (EFI_ERROR (Status)) {
1330 goto Done;
1331 }
1332
1333 //
1334 // Install HII Package List Protocol onto the image handle
1335 //
1336 if (Image->ImageContext.HiiResourceData != 0) {
1337 Status = CoreInstallProtocolInterface (
1338 &Image->Handle,
1339 &gEfiHiiPackageListProtocolGuid,
1340 EFI_NATIVE_INTERFACE,
1341 (VOID *) (UINTN) Image->ImageContext.HiiResourceData
1342 );
1343 if (EFI_ERROR (Status)) {
1344 goto Done;
1345 }
1346 }
1347 ProtectUefiImage (&Image->Info, Image->LoadedImageDevicePath);
1348
1349 //
1350 // Success. Return the image handle
1351 //
1352 *ImageHandle = Image->Handle;
1353
1354 Done:
1355 //
1356 // All done accessing the source file
1357 // If we allocated the Source buffer, free it
1358 //
1359 if (FHand.FreeBuffer) {
1360 CoreFreePool (FHand.Source);
1361 }
1362 if (OriginalFilePath != InputFilePath) {
1363 CoreFreePool (OriginalFilePath);
1364 }
1365
1366 //
1367 // There was an error. If there's an Image structure, free it
1368 //
1369 if (EFI_ERROR (Status)) {
1370 if (Image != NULL) {
1371 CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer == 0));
1372 Image = NULL;
1373 }
1374 } else if (EFI_ERROR (SecurityStatus)) {
1375 Status = SecurityStatus;
1376 }
1377
1378 //
1379 // Track the return status from LoadImage.
1380 //
1381 if (Image != NULL) {
1382 Image->LoadImageStatus = Status;
1383 }
1384
1385 return Status;
1386 }
1387
1388
1389
1390
1391 /**
1392 Loads an EFI image into memory and returns a handle to the image.
1393
1394 @param BootPolicy If TRUE, indicates that the request originates
1395 from the boot manager, and that the boot
1396 manager is attempting to load FilePath as a
1397 boot selection.
1398 @param ParentImageHandle The caller's image handle.
1399 @param FilePath The specific file path from which the image is
1400 loaded.
1401 @param SourceBuffer If not NULL, a pointer to the memory location
1402 containing a copy of the image to be loaded.
1403 @param SourceSize The size in bytes of SourceBuffer.
1404 @param ImageHandle Pointer to the returned image handle that is
1405 created when the image is successfully loaded.
1406
1407 @retval EFI_SUCCESS The image was loaded into memory.
1408 @retval EFI_NOT_FOUND The FilePath was not found.
1409 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1410 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1411 path cannot be parsed to locate the proper
1412 protocol for loading the file.
1413 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1414 resources.
1415 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1416 understood.
1417 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1418 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1419 image from being loaded. NULL is returned in *ImageHandle.
1420 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1421 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1422 platform policy specifies that the image should not be started.
1423
1424 **/
1425 EFI_STATUS
1426 EFIAPI
1427 CoreLoadImage (
1428 IN BOOLEAN BootPolicy,
1429 IN EFI_HANDLE ParentImageHandle,
1430 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1431 IN VOID *SourceBuffer OPTIONAL,
1432 IN UINTN SourceSize,
1433 OUT EFI_HANDLE *ImageHandle
1434 )
1435 {
1436 EFI_STATUS Status;
1437 UINT64 Tick;
1438 EFI_HANDLE Handle;
1439
1440 Tick = 0;
1441 PERF_CODE (
1442 Tick = GetPerformanceCounter ();
1443 );
1444
1445 Status = CoreLoadImageCommon (
1446 BootPolicy,
1447 ParentImageHandle,
1448 FilePath,
1449 SourceBuffer,
1450 SourceSize,
1451 (EFI_PHYSICAL_ADDRESS) (UINTN) NULL,
1452 NULL,
1453 ImageHandle,
1454 NULL,
1455 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1456 );
1457
1458 Handle = NULL;
1459 if (!EFI_ERROR (Status)) {
1460 //
1461 // ImageHandle will be valid only Status is success.
1462 //
1463 Handle = *ImageHandle;
1464 }
1465
1466 PERF_START (Handle, "LoadImage:", NULL, Tick);
1467 PERF_END (Handle, "LoadImage:", NULL, 0);
1468
1469 return Status;
1470 }
1471
1472
1473
1474 /**
1475 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1476
1477 @param This Calling context
1478 @param ParentImageHandle The caller's image handle.
1479 @param FilePath The specific file path from which the image is
1480 loaded.
1481 @param SourceBuffer If not NULL, a pointer to the memory location
1482 containing a copy of the image to be loaded.
1483 @param SourceSize The size in bytes of SourceBuffer.
1484 @param DstBuffer The buffer to store the image.
1485 @param NumberOfPages For input, specifies the space size of the
1486 image by caller if not NULL. For output,
1487 specifies the actual space size needed.
1488 @param ImageHandle Image handle for output.
1489 @param EntryPoint Image entry point for output.
1490 @param Attribute The bit mask of attributes to set for the load
1491 PE image.
1492
1493 @retval EFI_SUCCESS The image was loaded into memory.
1494 @retval EFI_NOT_FOUND The FilePath was not found.
1495 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1496 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1497 path cannot be parsed to locate the proper
1498 protocol for loading the file.
1499 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1500 resources.
1501 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1502 understood.
1503 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1504 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1505 image from being loaded. NULL is returned in *ImageHandle.
1506 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1507 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1508 platform policy specifies that the image should not be started.
1509
1510 **/
1511 EFI_STATUS
1512 EFIAPI
1513 CoreLoadImageEx (
1514 IN EFI_PE32_IMAGE_PROTOCOL *This,
1515 IN EFI_HANDLE ParentImageHandle,
1516 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1517 IN VOID *SourceBuffer OPTIONAL,
1518 IN UINTN SourceSize,
1519 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
1520 OUT UINTN *NumberOfPages OPTIONAL,
1521 OUT EFI_HANDLE *ImageHandle,
1522 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
1523 IN UINT32 Attribute
1524 )
1525 {
1526 EFI_STATUS Status;
1527 UINT64 Tick;
1528 EFI_HANDLE Handle;
1529
1530 Tick = 0;
1531 PERF_CODE (
1532 Tick = GetPerformanceCounter ();
1533 );
1534
1535 Status = CoreLoadImageCommon (
1536 TRUE,
1537 ParentImageHandle,
1538 FilePath,
1539 SourceBuffer,
1540 SourceSize,
1541 DstBuffer,
1542 NumberOfPages,
1543 ImageHandle,
1544 EntryPoint,
1545 Attribute
1546 );
1547
1548 Handle = NULL;
1549 if (!EFI_ERROR (Status)) {
1550 //
1551 // ImageHandle will be valid only Status is success.
1552 //
1553 Handle = *ImageHandle;
1554 }
1555
1556 PERF_START (Handle, "LoadImage:", NULL, Tick);
1557 PERF_END (Handle, "LoadImage:", NULL, 0);
1558
1559 return Status;
1560 }
1561
1562
1563 /**
1564 Transfer control to a loaded image's entry point.
1565
1566 @param ImageHandle Handle of image to be started.
1567 @param ExitDataSize Pointer of the size to ExitData
1568 @param ExitData Pointer to a pointer to a data buffer that
1569 includes a Null-terminated string,
1570 optionally followed by additional binary data.
1571 The string is a description that the caller may
1572 use to further indicate the reason for the
1573 image's exit.
1574
1575 @retval EFI_INVALID_PARAMETER Invalid parameter
1576 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1577 @retval EFI_SECURITY_VIOLATION The current platform policy specifies that the image should not be started.
1578 @retval EFI_SUCCESS Successfully transfer control to the image's
1579 entry point.
1580
1581 **/
1582 EFI_STATUS
1583 EFIAPI
1584 CoreStartImage (
1585 IN EFI_HANDLE ImageHandle,
1586 OUT UINTN *ExitDataSize,
1587 OUT CHAR16 **ExitData OPTIONAL
1588 )
1589 {
1590 EFI_STATUS Status;
1591 LOADED_IMAGE_PRIVATE_DATA *Image;
1592 LOADED_IMAGE_PRIVATE_DATA *LastImage;
1593 UINT64 HandleDatabaseKey;
1594 UINTN SetJumpFlag;
1595 UINT64 Tick;
1596 EFI_HANDLE Handle;
1597
1598 Tick = 0;
1599 Handle = ImageHandle;
1600
1601 Image = CoreLoadedImageInfo (ImageHandle);
1602 if (Image == NULL || Image->Started) {
1603 return EFI_INVALID_PARAMETER;
1604 }
1605 if (EFI_ERROR (Image->LoadImageStatus)) {
1606 return Image->LoadImageStatus;
1607 }
1608
1609 //
1610 // The image to be started must have the machine type supported by DxeCore.
1611 //
1612 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) {
1613 //
1614 // Do not ASSERT here, because image might be loaded via EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED
1615 // But it can not be started.
1616 //
1617 DEBUG ((EFI_D_ERROR, "Image type %s can't be started ", GetMachineTypeName(Image->Machine)));
1618 DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType)));
1619 return EFI_UNSUPPORTED;
1620 }
1621
1622 PERF_CODE (
1623 Tick = GetPerformanceCounter ();
1624 );
1625
1626
1627 //
1628 // Push the current start image context, and
1629 // link the current image to the head. This is the
1630 // only image that can call Exit()
1631 //
1632 HandleDatabaseKey = CoreGetHandleDatabaseKey ();
1633 LastImage = mCurrentImage;
1634 mCurrentImage = Image;
1635 Image->Tpl = gEfiCurrentTpl;
1636
1637 //
1638 // Set long jump for Exit() support
1639 // JumpContext must be aligned on a CPU specific boundary.
1640 // Overallocate the buffer and force the required alignment
1641 //
1642 Image->JumpBuffer = AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);
1643 if (Image->JumpBuffer == NULL) {
1644 //
1645 // Image may be unloaded after return with failure,
1646 // then ImageHandle may be invalid, so use NULL handle to record perf log.
1647 //
1648 PERF_START (NULL, "StartImage:", NULL, Tick);
1649 PERF_END (NULL, "StartImage:", NULL, 0);
1650
1651 //
1652 // Pop the current start image context
1653 //
1654 mCurrentImage = LastImage;
1655
1656 return EFI_OUT_OF_RESOURCES;
1657 }
1658 Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);
1659
1660 SetJumpFlag = SetJump (Image->JumpContext);
1661 //
1662 // The initial call to SetJump() must always return 0.
1663 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1664 //
1665 if (SetJumpFlag == 0) {
1666 RegisterMemoryProfileImage (Image, (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION ? EFI_FV_FILETYPE_APPLICATION : EFI_FV_FILETYPE_DRIVER));
1667 //
1668 // Call the image's entry point
1669 //
1670 Image->Started = TRUE;
1671 Image->Status = Image->EntryPoint (ImageHandle, Image->Info.SystemTable);
1672
1673 //
1674 // Add some debug information if the image returned with error.
1675 // This make the user aware and check if the driver image have already released
1676 // all the resource in this situation.
1677 //
1678 DEBUG_CODE_BEGIN ();
1679 if (EFI_ERROR (Image->Status)) {
1680 DEBUG ((DEBUG_ERROR, "Error: Image at %11p start failed: %r\n", Image->Info.ImageBase, Image->Status));
1681 }
1682 DEBUG_CODE_END ();
1683
1684 //
1685 // If the image returns, exit it through Exit()
1686 //
1687 CoreExit (ImageHandle, Image->Status, 0, NULL);
1688 }
1689
1690 //
1691 // Image has completed. Verify the tpl is the same
1692 //
1693 ASSERT (Image->Tpl == gEfiCurrentTpl);
1694 CoreRestoreTpl (Image->Tpl);
1695
1696 CoreFreePool (Image->JumpBuffer);
1697
1698 //
1699 // Pop the current start image context
1700 //
1701 mCurrentImage = LastImage;
1702
1703 //
1704 // Go connect any handles that were created or modified while the image executed.
1705 //
1706 CoreConnectHandlesByKey (HandleDatabaseKey);
1707
1708 //
1709 // Handle the image's returned ExitData
1710 //
1711 DEBUG_CODE_BEGIN ();
1712 if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {
1713
1714 DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %p", (UINT32)Image->ExitDataSize, Image->ExitData));
1715 if (Image->ExitData != NULL) {
1716 DEBUG ((DEBUG_LOAD, " (%hs)", Image->ExitData));
1717 }
1718 DEBUG ((DEBUG_LOAD, "\n"));
1719 }
1720 DEBUG_CODE_END ();
1721
1722 //
1723 // Return the exit data to the caller
1724 //
1725 if (ExitData != NULL && ExitDataSize != NULL) {
1726 *ExitDataSize = Image->ExitDataSize;
1727 *ExitData = Image->ExitData;
1728 } else {
1729 //
1730 // Caller doesn't want the exit data, free it
1731 //
1732 CoreFreePool (Image->ExitData);
1733 Image->ExitData = NULL;
1734 }
1735
1736 //
1737 // Save the Status because Image will get destroyed if it is unloaded.
1738 //
1739 Status = Image->Status;
1740
1741 //
1742 // If the image returned an error, or if the image is an application
1743 // unload it
1744 //
1745 if (EFI_ERROR (Image->Status) || Image->Type == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1746 CoreUnloadAndCloseImage (Image, TRUE);
1747 //
1748 // ImageHandle may be invalid after the image is unloaded, so use NULL handle to record perf log.
1749 //
1750 Handle = NULL;
1751 }
1752
1753 //
1754 // Done
1755 //
1756 PERF_START (Handle, "StartImage:", NULL, Tick);
1757 PERF_END (Handle, "StartImage:", NULL, 0);
1758 return Status;
1759 }
1760
1761 /**
1762 Terminates the currently loaded EFI image and returns control to boot services.
1763
1764 @param ImageHandle Handle that identifies the image. This
1765 parameter is passed to the image on entry.
1766 @param Status The image's exit code.
1767 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1768 ExitStatus is EFI_SUCCESS.
1769 @param ExitData Pointer to a data buffer that includes a
1770 Null-terminated Unicode string, optionally
1771 followed by additional binary data. The string
1772 is a description that the caller may use to
1773 further indicate the reason for the image's
1774 exit.
1775
1776 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1777 image.
1778 @retval EFI_SUCCESS Successfully terminates the currently loaded
1779 EFI image.
1780 @retval EFI_ACCESS_DENIED Should never reach there.
1781 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1782
1783 **/
1784 EFI_STATUS
1785 EFIAPI
1786 CoreExit (
1787 IN EFI_HANDLE ImageHandle,
1788 IN EFI_STATUS Status,
1789 IN UINTN ExitDataSize,
1790 IN CHAR16 *ExitData OPTIONAL
1791 )
1792 {
1793 LOADED_IMAGE_PRIVATE_DATA *Image;
1794 EFI_TPL OldTpl;
1795
1796 //
1797 // Prevent possible reentrance to this function
1798 // for the same ImageHandle
1799 //
1800 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
1801
1802 Image = CoreLoadedImageInfo (ImageHandle);
1803 if (Image == NULL) {
1804 Status = EFI_INVALID_PARAMETER;
1805 goto Done;
1806 }
1807
1808 if (!Image->Started) {
1809 //
1810 // The image has not been started so just free its resources
1811 //
1812 CoreUnloadAndCloseImage (Image, TRUE);
1813 Status = EFI_SUCCESS;
1814 goto Done;
1815 }
1816
1817 //
1818 // Image has been started, verify this image can exit
1819 //
1820 if (Image != mCurrentImage) {
1821 DEBUG ((DEBUG_LOAD|DEBUG_ERROR, "Exit: Image is not exitable image\n"));
1822 Status = EFI_INVALID_PARAMETER;
1823 goto Done;
1824 }
1825
1826 //
1827 // Set status
1828 //
1829 Image->Status = Status;
1830
1831 //
1832 // If there's ExitData info, move it
1833 //
1834 if (ExitData != NULL) {
1835 Image->ExitDataSize = ExitDataSize;
1836 Image->ExitData = AllocatePool (Image->ExitDataSize);
1837 if (Image->ExitData == NULL) {
1838 Status = EFI_OUT_OF_RESOURCES;
1839 goto Done;
1840 }
1841 CopyMem (Image->ExitData, ExitData, Image->ExitDataSize);
1842 }
1843
1844 CoreRestoreTpl (OldTpl);
1845 //
1846 // return to StartImage
1847 //
1848 LongJump (Image->JumpContext, (UINTN)-1);
1849
1850 //
1851 // If we return from LongJump, then it is an error
1852 //
1853 ASSERT (FALSE);
1854 Status = EFI_ACCESS_DENIED;
1855 Done:
1856 CoreRestoreTpl (OldTpl);
1857 return Status;
1858 }
1859
1860
1861
1862
1863 /**
1864 Unloads an image.
1865
1866 @param ImageHandle Handle that identifies the image to be
1867 unloaded.
1868
1869 @retval EFI_SUCCESS The image has been unloaded.
1870 @retval EFI_UNSUPPORTED The image has been started, and does not support
1871 unload.
1872 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1873
1874 **/
1875 EFI_STATUS
1876 EFIAPI
1877 CoreUnloadImage (
1878 IN EFI_HANDLE ImageHandle
1879 )
1880 {
1881 EFI_STATUS Status;
1882 LOADED_IMAGE_PRIVATE_DATA *Image;
1883
1884 Image = CoreLoadedImageInfo (ImageHandle);
1885 if (Image == NULL ) {
1886 //
1887 // The image handle is not valid
1888 //
1889 Status = EFI_INVALID_PARAMETER;
1890 goto Done;
1891 }
1892
1893 if (Image->Started) {
1894 //
1895 // The image has been started, request it to unload.
1896 //
1897 Status = EFI_UNSUPPORTED;
1898 if (Image->Info.Unload != NULL) {
1899 Status = Image->Info.Unload (ImageHandle);
1900 }
1901
1902 } else {
1903 //
1904 // This Image hasn't been started, thus it can be unloaded
1905 //
1906 Status = EFI_SUCCESS;
1907 }
1908
1909
1910 if (!EFI_ERROR (Status)) {
1911 //
1912 // if the Image was not started or Unloaded O.K. then clean up
1913 //
1914 CoreUnloadAndCloseImage (Image, TRUE);
1915 }
1916
1917 Done:
1918 return Status;
1919 }
1920
1921
1922
1923 /**
1924 Unload the specified image.
1925
1926 @param This Indicates the calling context.
1927 @param ImageHandle The specified image handle.
1928
1929 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1930 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1931 @retval EFI_SUCCESS Image successfully unloaded.
1932
1933 **/
1934 EFI_STATUS
1935 EFIAPI
1936 CoreUnloadImageEx (
1937 IN EFI_PE32_IMAGE_PROTOCOL *This,
1938 IN EFI_HANDLE ImageHandle
1939 )
1940 {
1941 return CoreUnloadImage (ImageHandle);
1942 }