]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Image/Image.c
Update DxeCore to set efi memory type for the different image to be loaded.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Image.c
15
16 Abstract:
17
18 Core image handling services
19
20 --*/
21
22 #include <DxeMain.h>
23 //
24 // Module Globals
25 //
26
27 LOADED_IMAGE_PRIVATE_DATA *mCurrentImage = NULL;
28
29 LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData = {
30 LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE,
31 NULL,
32 {
33 CoreLoadImageEx,
34 CoreUnloadImageEx
35 }
36 };
37
38
39 //
40 // This code is needed to build the Image handle for the DXE Core
41 //
42 LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = {
43 LOADED_IMAGE_PRIVATE_DATA_SIGNATURE, // Signature
44 NULL, // Image handle
45 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER, // Image type
46 TRUE, // If entrypoint has been called
47 NULL, // EntryPoint
48 {
49 EFI_LOADED_IMAGE_INFORMATION_REVISION, // Revision
50 NULL, // Parent handle
51 NULL, // System handle
52
53 NULL, // Device handle
54 NULL, // File path
55 NULL, // Reserved
56
57 0, // LoadOptionsSize
58 NULL, // LoadOptions
59
60 NULL, // ImageBase
61 0, // ImageSize
62 EfiBootServicesCode, // ImageCodeType
63 EfiBootServicesData // ImageDataType
64 },
65 (EFI_PHYSICAL_ADDRESS)0, // ImageBasePage
66 0, // NumberOfPages
67 NULL, // FixupData
68 0, // Tpl
69 EFI_SUCCESS, // Status
70 0, // ExitDataSize
71 NULL, // ExitData
72 NULL, // JumpBuffer
73 NULL, // JumpContext
74 0, // Machine
75 NULL, // Ebc
76 NULL, // RuntimeData
77 NULL, // DeviceHandleDevicePath
78 };
79
80
81 EFI_STATUS
82 CoreInitializeImageServices (
83 IN VOID *HobStart
84 )
85 /*++
86
87 Routine Description:
88
89 Add the Image Services to EFI Boot Services Table and install the protocol
90 interfaces for this image.
91
92 Arguments:
93
94 HobStart - The HOB to initialize
95
96 Returns:
97
98 Status code.
99
100 --*/
101 {
102 EFI_STATUS Status;
103 LOADED_IMAGE_PRIVATE_DATA *Image;
104 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress;
105 UINT64 DxeCoreImageLength;
106 VOID *DxeCoreEntryPoint;
107 EFI_PEI_HOB_POINTERS DxeCoreHob;
108 //
109 // Searching for image hob
110 //
111 DxeCoreHob.Raw = HobStart;
112 while ((DxeCoreHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, DxeCoreHob.Raw)) != NULL) {
113 if (CompareGuid (&DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.Name, &gEfiHobMemoryAllocModuleGuid)) {
114 //
115 // Find Dxe Core HOB
116 //
117 break;
118 }
119 DxeCoreHob.Raw = GET_NEXT_HOB (DxeCoreHob);
120 }
121 ASSERT (DxeCoreHob.Raw != NULL);
122
123 DxeCoreImageBaseAddress = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryBaseAddress;
124 DxeCoreImageLength = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength;
125 DxeCoreEntryPoint = (VOID *) (UINTN) DxeCoreHob.MemoryAllocationModule->EntryPoint;
126 gDxeCoreFileName = &DxeCoreHob.MemoryAllocationModule->ModuleName;
127 //
128 // Initialize the fields for an internal driver
129 //
130 Image = &mCorePrivateImage;
131
132 Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)DxeCoreEntryPoint;
133 Image->ImageBasePage = DxeCoreImageBaseAddress;
134 Image->NumberOfPages = (UINTN)(EFI_SIZE_TO_PAGES((UINTN)(DxeCoreImageLength)));
135 Image->Tpl = gEfiCurrentTpl;
136 Image->Info.SystemTable = gDxeCoreST;
137 Image->Info.ImageBase = (VOID *)(UINTN)DxeCoreImageBaseAddress;
138 Image->Info.ImageSize = DxeCoreImageLength;
139
140 //
141 // Install the protocol interfaces for this image
142 //
143 Status = CoreInstallProtocolInterface (
144 &Image->Handle,
145 &gEfiLoadedImageProtocolGuid,
146 EFI_NATIVE_INTERFACE,
147 &Image->Info
148 );
149 ASSERT_EFI_ERROR (Status);
150
151 mCurrentImage = Image;
152
153 //
154 // Fill in DXE globals
155 //
156 gDxeCoreImageHandle = Image->Handle;
157 gDxeCoreLoadedImage = &Image->Info;
158
159 //
160 // Export DXE Core PE Loader functionality
161 //
162 return CoreInstallProtocolInterface (
163 &mLoadPe32PrivateData.Handle,
164 &gEfiLoadPeImageProtocolGuid,
165 EFI_NATIVE_INTERFACE,
166 &mLoadPe32PrivateData.Pe32Image
167 );
168 }
169
170 EFI_STATUS
171 CoreLoadPeImage (
172 IN VOID *Pe32Handle,
173 IN LOADED_IMAGE_PRIVATE_DATA *Image,
174 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
175 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
176 IN UINT32 Attribute
177 )
178 /*++
179
180 Routine Description:
181
182 Loads, relocates, and invokes a PE/COFF image
183
184 Arguments:
185
186 Pe32Handle - The handle of PE32 image
187 Image - PE image to be loaded
188 DstBuffer - The buffer to store the image
189 EntryPoint - A pointer to the entry point
190 Attribute - The bit mask of attributes to set for the load PE image
191
192 Returns:
193
194 EFI_SUCCESS - The file was loaded, relocated, and invoked
195
196 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
197
198 EFI_INVALID_PARAMETER - Invalid parameter
199
200 EFI_BUFFER_TOO_SMALL - Buffer for image is too small
201
202 --*/
203 {
204 EFI_STATUS Status;
205 BOOLEAN DstBufAlocated;
206 UINTN Size;
207
208 ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));
209
210 Image->ImageContext.Handle = Pe32Handle;
211 Image->ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)CoreReadImageFile;
212
213 //
214 // Get information about the image being loaded
215 //
216 Status = PeCoffLoaderGetImageInfo (&Image->ImageContext);
217 if (EFI_ERROR (Status)) {
218 return Status;
219 }
220
221 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) {
222 //
223 // The PE/COFF loader can support loading image types that can be executed.
224 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
225 //
226 return EFI_UNSUPPORTED;
227 }
228
229 //
230 // Set EFI memory type based on ImageType
231 //
232 switch (Image->ImageContext.ImageType) {
233 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
234 Image->ImageContext.ImageCodeMemoryType = EfiLoaderCode;
235 Image->ImageContext.ImageDataMemoryType = EfiLoaderData;
236 break;
237 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
238 Image->ImageContext.ImageCodeMemoryType = EfiBootServicesCode;
239 Image->ImageContext.ImageDataMemoryType = EfiBootServicesData;
240 break;
241 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
242 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
243 Image->ImageContext.ImageCodeMemoryType = EfiRuntimeServicesCode;
244 Image->ImageContext.ImageDataMemoryType = EfiRuntimeServicesData;
245 break;
246 default:
247 Image->ImageContext.ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
248 return EFI_UNSUPPORTED;
249 }
250
251 //
252 // Allocate memory of the correct memory type aligned on the required image boundry
253 //
254 DstBufAlocated = FALSE;
255 if (DstBuffer == 0) {
256 //
257 // Allocate Destination Buffer as caller did not pass it in
258 //
259
260 if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {
261 Size = (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment;
262 } else {
263 Size = (UINTN)Image->ImageContext.ImageSize;
264 }
265
266 Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size);
267
268 //
269 // If the image relocations have not been stripped, then load at any address.
270 // Otherwise load at the address at which it was linked.
271 //
272 // Memory below 1MB should be treated reserved for CSM and there should be
273 // no modules whose preferred load addresses are below 1MB.
274 //
275 Status = EFI_OUT_OF_RESOURCES;
276 if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) {
277 Status = CoreAllocatePages (
278 AllocateAddress,
279 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
280 Image->NumberOfPages,
281 &Image->ImageContext.ImageAddress
282 );
283 }
284 if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {
285 Status = CoreAllocatePages (
286 AllocateAnyPages,
287 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
288 Image->NumberOfPages,
289 &Image->ImageContext.ImageAddress
290 );
291 }
292 if (EFI_ERROR (Status)) {
293 return Status;
294 }
295 DstBufAlocated = TRUE;
296 } else {
297 //
298 // Caller provided the destination buffer
299 //
300
301 if (Image->ImageContext.RelocationsStripped && (Image->ImageContext.ImageAddress != DstBuffer)) {
302 //
303 // If the image relocations were stripped, and the caller provided a
304 // destination buffer address that does not match the address that the
305 // image is linked at, then the image cannot be loaded.
306 //
307 return EFI_INVALID_PARAMETER;
308 }
309
310 if (Image->NumberOfPages != 0 &&
311 Image->NumberOfPages <
312 (EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment))) {
313 Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);
314 return EFI_BUFFER_TOO_SMALL;
315 }
316
317 Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);
318 Image->ImageContext.ImageAddress = DstBuffer;
319 }
320
321 Image->ImageBasePage = Image->ImageContext.ImageAddress;
322 Image->ImageContext.ImageAddress =
323 (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &
324 ~((UINTN)Image->ImageContext.SectionAlignment - 1);
325
326 //
327 // Load the image from the file into the allocated memory
328 //
329 Status = PeCoffLoaderLoadImage (&Image->ImageContext);
330 if (EFI_ERROR (Status)) {
331 goto Done;
332 }
333
334 //
335 // If this is a Runtime Driver, then allocate memory for the FixupData that
336 // is used to relocate the image when SetVirtualAddressMap() is called. The
337 // relocation is done by the Runtime AP.
338 //
339 if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) {
340 if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
341 Image->ImageContext.FixupData = CoreAllocateRuntimePool ((UINTN)(Image->ImageContext.FixupDataSize));
342 if (Image->ImageContext.FixupData == NULL) {
343 Status = EFI_OUT_OF_RESOURCES;
344 goto Done;
345 }
346 }
347 }
348
349 //
350 // Relocate the image in memory
351 //
352 Status = PeCoffLoaderRelocateImage (&Image->ImageContext);
353 if (EFI_ERROR (Status)) {
354 goto Done;
355 }
356
357 //
358 // Flush the Instruction Cache
359 //
360 InvalidateInstructionCacheRange ((VOID *)(UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.ImageSize);
361
362 //
363 // Copy the machine type from the context to the image private data. This
364 // is needed during image unload to know if we should call an EBC protocol
365 // to unload the image.
366 //
367 Image->Machine = Image->ImageContext.Machine;
368
369 //
370 // Get the image entry point. If it's an EBC image, then call into the
371 // interpreter to create a thunk for the entry point and use the returned
372 // value for the entry point.
373 //
374 Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)Image->ImageContext.EntryPoint;
375 if (Image->ImageContext.Machine == EFI_IMAGE_MACHINE_EBC) {
376 //
377 // Locate the EBC interpreter protocol
378 //
379 Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);
380 if (EFI_ERROR(Status)) {
381 goto Done;
382 }
383
384 //
385 // Register a callback for flushing the instruction cache so that created
386 // thunks can be flushed.
387 //
388 Status = Image->Ebc->RegisterICacheFlush (Image->Ebc, (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange);
389 if (EFI_ERROR(Status)) {
390 goto Done;
391 }
392
393 //
394 // Create a thunk for the image's entry point. This will be the new
395 // entry point for the image.
396 //
397 Status = Image->Ebc->CreateThunk (
398 Image->Ebc,
399 Image->Handle,
400 (VOID *)(UINTN)Image->ImageContext.EntryPoint,
401 (VOID **)&Image->EntryPoint
402 );
403 if (EFI_ERROR(Status)) {
404 goto Done;
405 }
406 }
407
408 //
409 // Fill in the image information for the Loaded Image Protocol
410 //
411 Image->Type = Image->ImageContext.ImageType;
412 Image->Info.ImageBase = (VOID *)(UINTN)Image->ImageContext.ImageAddress;
413 Image->Info.ImageSize = Image->ImageContext.ImageSize;
414 Image->Info.ImageCodeType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType);
415 Image->Info.ImageDataType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageDataMemoryType);
416 if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) {
417 if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
418 //
419 // Make a list off all the RT images so we can let the RT AP know about them.
420 //
421 Image->RuntimeData = CoreAllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY));
422 if (Image->RuntimeData == NULL) {
423 goto Done;
424 }
425 Image->RuntimeData->ImageBase = Image->Info.ImageBase;
426 Image->RuntimeData->ImageSize = (UINT64) (Image->Info.ImageSize);
427 Image->RuntimeData->RelocationData = Image->ImageContext.FixupData;
428 Image->RuntimeData->Handle = Image->Handle;
429 InsertTailList (&gRuntime->ImageHead, &Image->RuntimeData->Link);
430 }
431 }
432
433 //
434 // Fill in the entry point of the image if it is available
435 //
436 if (EntryPoint != NULL) {
437 *EntryPoint = Image->ImageContext.EntryPoint;
438 }
439
440 //
441 // Print the load address and the PDB file name if it is available
442 //
443
444 DEBUG_CODE_BEGIN ();
445
446 UINTN Index;
447 UINTN StartIndex;
448 CHAR8 EfiFileName[256];
449
450 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading driver at 0x%10p EntryPoint=0x%10p ", (VOID *)(UINTN)Image->ImageContext.ImageAddress, (VOID *)(UINTN)Image->ImageContext.EntryPoint));
451 if (Image->ImageContext.PdbPointer != NULL) {
452 StartIndex = 0;
453 for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) {
454 if (Image->ImageContext.PdbPointer[Index] == '\\') {
455 StartIndex = Index + 1;
456 }
457 }
458 //
459 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
460 //
461 for (Index = 0; Index < sizeof (EfiFileName); Index++) {
462 EfiFileName[Index] = Image->ImageContext.PdbPointer[Index + StartIndex];
463 if (EfiFileName[Index] == 0) {
464 EfiFileName[Index] = '.';
465 }
466 if (EfiFileName[Index] == '.') {
467 EfiFileName[Index + 1] = 'e';
468 EfiFileName[Index + 2] = 'f';
469 EfiFileName[Index + 3] = 'i';
470 EfiFileName[Index + 4] = 0;
471 break;
472 }
473 }
474 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
475 }
476 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));
477
478 DEBUG_CODE_END ();
479
480 return EFI_SUCCESS;
481
482 Done:
483
484 //
485 // Free memory.
486 //
487
488 if (DstBufAlocated) {
489 CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);
490 }
491
492 if (Image->ImageContext.FixupData != NULL) {
493 CoreFreePool (Image->ImageContext.FixupData);
494 }
495
496 return Status;
497 }
498
499
500 LOADED_IMAGE_PRIVATE_DATA *
501 CoreLoadedImageInfo (
502 IN EFI_HANDLE ImageHandle
503 )
504 /*++
505
506 Routine Description:
507
508 Get the image's private data from its handle.
509
510 Arguments:
511
512 ImageHandle - The image handle
513
514 Returns:
515
516 Return the image private data associated with ImageHandle.
517
518 --*/
519 {
520 EFI_STATUS Status;
521 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
522 LOADED_IMAGE_PRIVATE_DATA *Image;
523
524 Status = CoreHandleProtocol (
525 ImageHandle,
526 &gEfiLoadedImageProtocolGuid,
527 (VOID **)&LoadedImage
528 );
529 if (!EFI_ERROR (Status)) {
530 Image = LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage);
531 } else {
532 DEBUG ((EFI_D_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %x\n", ImageHandle));
533 Image = NULL;
534 }
535
536 return Image;
537 }
538
539 STATIC
540 EFI_STATUS
541 CoreLoadImageCommon (
542 IN BOOLEAN BootPolicy,
543 IN EFI_HANDLE ParentImageHandle,
544 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
545 IN VOID *SourceBuffer OPTIONAL,
546 IN UINTN SourceSize,
547 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
548 IN OUT UINTN *NumberOfPages OPTIONAL,
549 OUT EFI_HANDLE *ImageHandle,
550 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
551 IN UINT32 Attribute
552 )
553 /*++
554
555 Routine Description:
556
557 Loads an EFI image into memory and returns a handle to the image.
558
559 Arguments:
560
561 BootPolicy - If TRUE, indicates that the request originates from the boot manager,
562 and that the boot manager is attempting to load FilePath as a boot selection.
563 ParentImageHandle - The caller's image handle.
564 FilePath - The specific file path from which the image is loaded.
565 SourceBuffer - If not NULL, a pointer to the memory location containing a copy of
566 the image to be loaded.
567 SourceSize - The size in bytes of SourceBuffer.
568 DstBuffer - The buffer to store the image
569 NumberOfPages - If not NULL, a pointer to the image's page number, if this number
570 is not enough, return EFI_BUFFER_TOO_SMALL and this parameter contain
571 the required number.
572 ImageHandle - Pointer to the returned image handle that is created when the image
573 is successfully loaded.
574 EntryPoint - A pointer to the entry point
575 Attribute - The bit mask of attributes to set for the load PE image
576
577 Returns:
578
579 EFI_SUCCESS - The image was loaded into memory.
580 EFI_NOT_FOUND - The FilePath was not found.
581 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
582 EFI_BUFFER_TOO_SMALL - The buffer is too small
583 EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be
584 parsed to locate the proper protocol for loading the file.
585 EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.
586 --*/
587 {
588 LOADED_IMAGE_PRIVATE_DATA *Image;
589 LOADED_IMAGE_PRIVATE_DATA *ParentImage;
590 IMAGE_FILE_HANDLE FHand;
591 EFI_STATUS Status;
592 EFI_STATUS SecurityStatus;
593 EFI_HANDLE DeviceHandle;
594 UINT32 AuthenticationStatus;
595 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
596 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
597 UINTN FilePathSize;
598
599 SecurityStatus = EFI_SUCCESS;
600
601 ASSERT (gEfiCurrentTpl < TPL_NOTIFY);
602 ParentImage = NULL;
603
604 //
605 // The caller must pass in a valid ParentImageHandle
606 //
607 if (ImageHandle == NULL || ParentImageHandle == NULL) {
608 return EFI_INVALID_PARAMETER;
609 }
610
611 ParentImage = CoreLoadedImageInfo (ParentImageHandle);
612 if (ParentImage == NULL) {
613 DEBUG((EFI_D_LOAD|EFI_D_ERROR, "LoadImageEx: Parent handle not an image handle\n"));
614 return EFI_INVALID_PARAMETER;
615 }
616
617 //
618 // Get simple read access to the source file
619 //
620 OriginalFilePath = FilePath;
621 Status = CoreOpenImageFile (
622 BootPolicy,
623 SourceBuffer,
624 SourceSize,
625 FilePath,
626 &DeviceHandle,
627 &FHand,
628 &AuthenticationStatus
629 );
630 if (Status == EFI_ALREADY_STARTED) {
631 Image = NULL;
632 goto Done;
633 } else if (EFI_ERROR (Status)) {
634 return Status;
635 }
636
637 //
638 // Verify the Authentication Status through the Security Architectural Protocol
639 //
640 if ((gSecurity != NULL) && (OriginalFilePath != NULL)) {
641 SecurityStatus = gSecurity->FileAuthenticationState (
642 gSecurity,
643 AuthenticationStatus,
644 OriginalFilePath
645 );
646 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
647 Status = SecurityStatus;
648 Image = NULL;
649 goto Done;
650 }
651 }
652
653
654 //
655 // Allocate a new image structure
656 //
657 Image = CoreAllocateZeroBootServicesPool (sizeof(LOADED_IMAGE_PRIVATE_DATA));
658 if (Image == NULL) {
659 return EFI_OUT_OF_RESOURCES;
660 }
661
662 //
663 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
664 //
665 Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
666 if (!EFI_ERROR (Status)) {
667 FilePathSize = CoreDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
668 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) ( ((UINT8 *)FilePath) + FilePathSize );
669 Image->DeviceHandleDevicePath = CoreDuplicateDevicePath (HandleFilePath);
670 }
671
672 //
673 // Initialize the fields for an internal driver
674 //
675 Image->Signature = LOADED_IMAGE_PRIVATE_DATA_SIGNATURE;
676 Image->Info.SystemTable = gDxeCoreST;
677 Image->Info.DeviceHandle = DeviceHandle;
678 Image->Info.Revision = EFI_LOADED_IMAGE_INFORMATION_REVISION;
679 Image->Info.FilePath = CoreDuplicateDevicePath (FilePath);
680 Image->Info.ParentHandle = ParentImageHandle;
681
682
683 if (NumberOfPages != NULL) {
684 Image->NumberOfPages = *NumberOfPages ;
685 } else {
686 Image->NumberOfPages = 0 ;
687 }
688
689 //
690 // Install the protocol interfaces for this image
691 // don't fire notifications yet
692 //
693 Status = CoreInstallProtocolInterfaceNotify (
694 &Image->Handle,
695 &gEfiLoadedImageProtocolGuid,
696 EFI_NATIVE_INTERFACE,
697 &Image->Info,
698 FALSE
699 );
700 if (EFI_ERROR (Status)) {
701 goto Done;
702 }
703
704 //
705 // Load the image. If EntryPoint is Null, it will not be set.
706 //
707 Status = CoreLoadPeImage (&FHand, Image, DstBuffer, EntryPoint, Attribute);
708 if (EFI_ERROR (Status)) {
709 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_OUT_OF_RESOURCES)) {
710 if (NumberOfPages != NULL) {
711 *NumberOfPages = Image->NumberOfPages;
712 }
713 }
714 goto Done;
715 }
716
717 //
718 // Register the image in the Debug Image Info Table if the attribute is set
719 //
720 if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) {
721 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL, &Image->Info, Image->Handle);
722 }
723
724 //
725 //Reinstall loaded image protocol to fire any notifications
726 //
727 Status = CoreReinstallProtocolInterface (
728 Image->Handle,
729 &gEfiLoadedImageProtocolGuid,
730 &Image->Info,
731 &Image->Info
732 );
733 if (EFI_ERROR (Status)) {
734 goto Done;
735 }
736
737
738 //
739 // Success. Return the image handle
740 //
741 *ImageHandle = Image->Handle;
742
743 Done:
744 //
745 // All done accessing the source file
746 // If we allocated the Source buffer, free it
747 //
748 if (FHand.FreeBuffer) {
749 CoreFreePool (FHand.Source);
750 }
751
752 //
753 // There was an error. If there's an Image structure, free it
754 //
755 if (EFI_ERROR (Status)) {
756 if (Image != NULL) {
757 CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer == 0));
758 *ImageHandle = NULL;
759 }
760 } else if (EFI_ERROR (SecurityStatus)) {
761 Status = SecurityStatus;
762 }
763
764 return Status;
765 }
766
767
768
769 EFI_STATUS
770 EFIAPI
771 CoreLoadImage (
772 IN BOOLEAN BootPolicy,
773 IN EFI_HANDLE ParentImageHandle,
774 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
775 IN VOID *SourceBuffer OPTIONAL,
776 IN UINTN SourceSize,
777 OUT EFI_HANDLE *ImageHandle
778 )
779 /*++
780
781 Routine Description:
782
783 Loads an EFI image into memory and returns a handle to the image.
784
785 Arguments:
786
787 BootPolicy - If TRUE, indicates that the request originates from the boot manager,
788 and that the boot manager is attempting to load FilePath as a boot selection.
789 ParentImageHandle - The caller's image handle.
790 FilePath - The specific file path from which the image is loaded.
791 SourceBuffer - If not NULL, a pointer to the memory location containing a copy of
792 the image to be loaded.
793 SourceSize - The size in bytes of SourceBuffer.
794 ImageHandle - Pointer to the returned image handle that is created when the image
795 is successfully loaded.
796
797 Returns:
798
799 EFI_SUCCESS - The image was loaded into memory.
800 EFI_NOT_FOUND - The FilePath was not found.
801 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
802 EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be
803 parsed to locate the proper protocol for loading the file.
804 EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.
805 --*/
806 {
807 EFI_STATUS Status;
808
809 PERF_START (NULL, "LoadImage", NULL, 0);
810
811 Status = CoreLoadImageCommon (
812 BootPolicy,
813 ParentImageHandle,
814 FilePath,
815 SourceBuffer,
816 SourceSize,
817 (EFI_PHYSICAL_ADDRESS)NULL,
818 NULL,
819 ImageHandle,
820 NULL,
821 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
822 );
823
824 PERF_END (NULL, "LoadImage", NULL, 0);
825
826 return Status;
827 }
828
829
830 EFI_STATUS
831 EFIAPI
832 CoreLoadImageEx (
833 IN EFI_PE32_IMAGE_PROTOCOL *This,
834 IN EFI_HANDLE ParentImageHandle,
835 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
836 IN VOID *SourceBuffer OPTIONAL,
837 IN UINTN SourceSize,
838 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
839 OUT UINTN *NumberOfPages OPTIONAL,
840 OUT EFI_HANDLE *ImageHandle,
841 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
842 IN UINT32 Attribute
843 )
844 /*++
845
846 Routine Description:
847
848 Loads an EFI image into memory and returns a handle to the image with extended parameters.
849
850 Arguments:
851
852 This - Calling context
853 ParentImageHandle - The caller's image handle.
854 FilePath - The specific file path from which the image is loaded.
855 SourceBuffer - If not NULL, a pointer to the memory location containing a copy of
856 the image to be loaded.
857 SourceSize - The size in bytes of SourceBuffer.
858 DstBuffer - The buffer to store the image.
859 NumberOfPages - For input, specifies the space size of the image by caller if not NULL.
860 For output, specifies the actual space size needed.
861 ImageHandle - Image handle for output.
862 EntryPoint - Image entry point for output.
863 Attribute - The bit mask of attributes to set for the load PE image.
864
865 Returns:
866
867 EFI_SUCCESS - The image was loaded into memory.
868 EFI_NOT_FOUND - The FilePath was not found.
869 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
870 EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be
871 parsed to locate the proper protocol for loading the file.
872 EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.
873 --*/
874 {
875 return CoreLoadImageCommon (
876 TRUE,
877 ParentImageHandle,
878 FilePath,
879 SourceBuffer,
880 SourceSize,
881 DstBuffer,
882 NumberOfPages,
883 ImageHandle,
884 EntryPoint,
885 Attribute
886 );
887 }
888
889
890
891
892 EFI_STATUS
893 EFIAPI
894 CoreStartImage (
895 IN EFI_HANDLE ImageHandle,
896 OUT UINTN *ExitDataSize,
897 OUT CHAR16 **ExitData OPTIONAL
898 )
899 /*++
900
901 Routine Description:
902
903 Transfer control to a loaded image's entry point.
904
905 Arguments:
906
907 ImageHandle - Handle of image to be started.
908
909 ExitDataSize - Pointer of the size to ExitData
910
911 ExitData - Pointer to a pointer to a data buffer that includes a Null-terminated
912 Unicode string, optionally followed by additional binary data. The string
913 is a description that the caller may use to further indicate the reason for
914 the image's exit.
915
916 Returns:
917
918 EFI_INVALID_PARAMETER - Invalid parameter
919
920 EFI_OUT_OF_RESOURCES - No enough buffer to allocate
921
922 EFI_SUCCESS - Successfully transfer control to the image's entry point.
923
924 --*/
925 {
926 EFI_STATUS Status;
927 LOADED_IMAGE_PRIVATE_DATA *Image;
928 LOADED_IMAGE_PRIVATE_DATA *LastImage;
929 UINT64 HandleDatabaseKey;
930 UINTN SetJumpFlag;
931
932 Image = CoreLoadedImageInfo (ImageHandle);
933 if (Image == NULL_HANDLE || Image->Started) {
934 return EFI_INVALID_PARAMETER;
935 }
936
937 //
938 // Don't profile Objects or invalid start requests
939 //
940 PERF_START (ImageHandle, START_IMAGE_TOK, NULL, 0);
941
942
943 //
944 // Push the current start image context, and
945 // link the current image to the head. This is the
946 // only image that can call Exit()
947 //
948 HandleDatabaseKey = CoreGetHandleDatabaseKey ();
949 LastImage = mCurrentImage;
950 mCurrentImage = Image;
951 Image->Tpl = gEfiCurrentTpl;
952
953 //
954 // Set long jump for Exit() support
955 // JumpContext must be aligned on a CPU specific boundary.
956 // Overallocate the buffer and force the required alignment
957 //
958 Image->JumpBuffer = CoreAllocateBootServicesPool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);
959 if (Image->JumpBuffer == NULL) {
960 PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);
961 return EFI_OUT_OF_RESOURCES;
962 }
963 Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);
964
965 SetJumpFlag = SetJump (Image->JumpContext);
966 //
967 // The initial call to SetJump() must always return 0.
968 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
969 //
970 if (!SetJumpFlag) {
971 //
972 // Call the image's entry point
973 //
974 Image->Started = TRUE;
975 Image->Status = Image->EntryPoint (ImageHandle, Image->Info.SystemTable);
976
977 //
978 // Add some debug information if the image returned with error.
979 // This make the user aware and check if the driver image have already released
980 // all the resource in this situation.
981 //
982 DEBUG_CODE_BEGIN ();
983 if (EFI_ERROR (Image->Status)) {
984 DEBUG ((EFI_D_ERROR, "Error: Image at %10p start failed: %r\n", Image->Info.ImageBase, Image->Status));
985 }
986 DEBUG_CODE_END ();
987
988 //
989 // If the image returns, exit it through Exit()
990 //
991 CoreExit (ImageHandle, Image->Status, 0, NULL);
992 }
993
994 //
995 // Image has completed. Verify the tpl is the same
996 //
997 ASSERT (Image->Tpl == gEfiCurrentTpl);
998 CoreRestoreTpl (Image->Tpl);
999
1000 CoreFreePool (Image->JumpBuffer);
1001
1002 //
1003 // Pop the current start image context
1004 //
1005 mCurrentImage = LastImage;
1006
1007 //
1008 // Go connect any handles that were created or modified while the image executed.
1009 //
1010 CoreConnectHandlesByKey (HandleDatabaseKey);
1011
1012 //
1013 // Handle the image's returned ExitData
1014 //
1015 DEBUG_CODE_BEGIN ();
1016 if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {
1017
1018 DEBUG (
1019 (EFI_D_LOAD,
1020 "StartImage: ExitDataSize %d, ExitData %x",
1021 Image->ExitDataSize,
1022 Image->ExitData)
1023 );
1024 if (Image->ExitData != NULL) {
1025 DEBUG ((EFI_D_LOAD, " (%hs)", Image->ExitData));
1026 }
1027 DEBUG ((EFI_D_LOAD, "\n"));
1028 }
1029 DEBUG_CODE_END ();
1030
1031 //
1032 // Return the exit data to the caller
1033 //
1034 if (ExitData != NULL && ExitDataSize != NULL) {
1035 *ExitDataSize = Image->ExitDataSize;
1036 *ExitData = Image->ExitData;
1037 } else {
1038 //
1039 // Caller doesn't want the exit data, free it
1040 //
1041 CoreFreePool (Image->ExitData);
1042 Image->ExitData = NULL;
1043 }
1044
1045 //
1046 // Save the Status because Image will get destroyed if it is unloaded.
1047 //
1048 Status = Image->Status;
1049
1050 //
1051 // If the image returned an error, or if the image is an application
1052 // unload it
1053 //
1054 if (EFI_ERROR (Image->Status) || Image->Type == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1055 CoreUnloadAndCloseImage (Image, TRUE);
1056 }
1057
1058 //
1059 // Done
1060 //
1061 PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);
1062 return Status;
1063 }
1064
1065
1066 VOID
1067 CoreUnloadAndCloseImage (
1068 IN LOADED_IMAGE_PRIVATE_DATA *Image,
1069 IN BOOLEAN FreePage
1070 )
1071 /*++
1072
1073 Routine Description:
1074
1075 Unloads EFI image from memory.
1076
1077 Arguments:
1078
1079 Image - EFI image
1080 FreePage - Free allocated pages
1081
1082 Returns:
1083
1084 None
1085
1086 --*/
1087 {
1088 EFI_STATUS Status;
1089 UINTN HandleCount;
1090 EFI_HANDLE *HandleBuffer;
1091 UINTN HandleIndex;
1092 EFI_GUID **ProtocolGuidArray;
1093 UINTN ArrayCount;
1094 UINTN ProtocolIndex;
1095 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
1096 UINTN OpenInfoCount;
1097 UINTN OpenInfoIndex;
1098
1099 if (Image->Ebc != NULL) {
1100 //
1101 // If EBC protocol exists we must perform cleanups for this image.
1102 //
1103 Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);
1104 }
1105
1106 //
1107 // Unload image, free Image->ImageContext->ModHandle
1108 //
1109 PeCoffLoaderUnloadImage (&Image->ImageContext);
1110
1111 //
1112 // Free our references to the image handle
1113 //
1114 if (Image->Handle != NULL_HANDLE) {
1115
1116 Status = CoreLocateHandleBuffer (
1117 AllHandles,
1118 NULL,
1119 NULL,
1120 &HandleCount,
1121 &HandleBuffer
1122 );
1123 if (!EFI_ERROR (Status)) {
1124 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
1125 Status = CoreProtocolsPerHandle (
1126 HandleBuffer[HandleIndex],
1127 &ProtocolGuidArray,
1128 &ArrayCount
1129 );
1130 if (!EFI_ERROR (Status)) {
1131 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
1132 Status = CoreOpenProtocolInformation (
1133 HandleBuffer[HandleIndex],
1134 ProtocolGuidArray[ProtocolIndex],
1135 &OpenInfo,
1136 &OpenInfoCount
1137 );
1138 if (!EFI_ERROR (Status)) {
1139 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
1140 if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {
1141 Status = CoreCloseProtocol (
1142 HandleBuffer[HandleIndex],
1143 ProtocolGuidArray[ProtocolIndex],
1144 Image->Handle,
1145 OpenInfo[OpenInfoIndex].ControllerHandle
1146 );
1147 }
1148 }
1149 if (OpenInfo != NULL) {
1150 CoreFreePool(OpenInfo);
1151 }
1152 }
1153 }
1154 if (ProtocolGuidArray != NULL) {
1155 CoreFreePool(ProtocolGuidArray);
1156 }
1157 }
1158 }
1159 if (HandleBuffer != NULL) {
1160 CoreFreePool (HandleBuffer);
1161 }
1162 }
1163
1164 CoreRemoveDebugImageInfoEntry (Image->Handle);
1165
1166 Status = CoreUninstallProtocolInterface (
1167 Image->Handle,
1168 &gEfiLoadedImageProtocolGuid,
1169 &Image->Info
1170 );
1171 }
1172
1173 if (Image->RuntimeData != NULL) {
1174 if (Image->RuntimeData->Link.ForwardLink != NULL) {
1175 //
1176 // Remove the Image from the Runtime Image list as we are about to Free it!
1177 //
1178 RemoveEntryList (&Image->RuntimeData->Link);
1179 }
1180 CoreFreePool (Image->RuntimeData);
1181 }
1182
1183 //
1184 // Free the Image from memory
1185 //
1186 if ((Image->ImageBasePage != 0) && FreePage) {
1187 CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);
1188 }
1189
1190 //
1191 // Done with the Image structure
1192 //
1193 if (Image->Info.FilePath != NULL) {
1194 CoreFreePool (Image->Info.FilePath);
1195 }
1196
1197 if (Image->DeviceHandleDevicePath != NULL) {
1198 CoreFreePool (Image->DeviceHandleDevicePath);
1199 }
1200
1201 if (Image->FixupData != NULL) {
1202 CoreFreePool (Image->FixupData);
1203 }
1204
1205 CoreFreePool (Image);
1206 }
1207
1208
1209
1210 EFI_STATUS
1211 EFIAPI
1212 CoreExit (
1213 IN EFI_HANDLE ImageHandle,
1214 IN EFI_STATUS Status,
1215 IN UINTN ExitDataSize,
1216 IN CHAR16 *ExitData OPTIONAL
1217 )
1218 /*++
1219
1220 Routine Description:
1221
1222 Terminates the currently loaded EFI image and returns control to boot services.
1223
1224 Arguments:
1225
1226 ImageHandle - Handle that identifies the image. This parameter is passed to the image
1227 on entry.
1228 Status - The image's exit code.
1229 ExitDataSize - The size, in bytes, of ExitData. Ignored if ExitStatus is
1230 EFI_SUCCESS.
1231 ExitData - Pointer to a data buffer that includes a Null-terminated Unicode string,
1232 optionally followed by additional binary data. The string is a
1233 description that the caller may use to further indicate the reason for
1234 the image's exit.
1235
1236 Returns:
1237
1238 EFI_INVALID_PARAMETER - Image handle is NULL or it is not current image.
1239
1240 EFI_SUCCESS - Successfully terminates the currently loaded EFI image.
1241
1242 EFI_ACCESS_DENIED - Should never reach there.
1243
1244 EFI_OUT_OF_RESOURCES - Could not allocate pool
1245
1246 --*/
1247 {
1248 LOADED_IMAGE_PRIVATE_DATA *Image;
1249 EFI_TPL OldTpl;
1250
1251 //
1252 // Prevent possible reentrance to this function
1253 // for the same ImageHandle
1254 //
1255 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
1256
1257 Image = CoreLoadedImageInfo (ImageHandle);
1258 if (Image == NULL_HANDLE) {
1259 Status = EFI_INVALID_PARAMETER;
1260 goto Done;
1261 }
1262
1263 if (!Image->Started) {
1264 //
1265 // The image has not been started so just free its resources
1266 //
1267 CoreUnloadAndCloseImage (Image, TRUE);
1268 Status = EFI_SUCCESS;
1269 goto Done;
1270 }
1271
1272 //
1273 // Image has been started, verify this image can exit
1274 //
1275 if (Image != mCurrentImage) {
1276 DEBUG ((EFI_D_LOAD|EFI_D_ERROR, "Exit: Image is not exitable image\n"));
1277 Status = EFI_INVALID_PARAMETER;
1278 goto Done;
1279 }
1280
1281 //
1282 // Set status
1283 //
1284 Image->Status = Status;
1285
1286 //
1287 // If there's ExitData info, move it
1288 //
1289 if (ExitData != NULL) {
1290 Image->ExitDataSize = ExitDataSize;
1291 Image->ExitData = CoreAllocateBootServicesPool (Image->ExitDataSize);
1292 if (Image->ExitData == NULL) {
1293 Status = EFI_OUT_OF_RESOURCES;
1294 goto Done;
1295 }
1296 CopyMem (Image->ExitData, ExitData, Image->ExitDataSize);
1297 }
1298
1299 CoreRestoreTpl (OldTpl);
1300 //
1301 // return to StartImage
1302 //
1303 LongJump (Image->JumpContext, (UINTN)-1);
1304
1305 //
1306 // If we return from LongJump, then it is an error
1307 //
1308 ASSERT (FALSE);
1309 Status = EFI_ACCESS_DENIED;
1310 Done:
1311 CoreRestoreTpl (OldTpl);
1312 return Status;
1313 }
1314
1315
1316
1317 EFI_STATUS
1318 EFIAPI
1319 CoreUnloadImage (
1320 IN EFI_HANDLE ImageHandle
1321 )
1322 /*++
1323
1324 Routine Description:
1325
1326 Unloads an image.
1327
1328 Arguments:
1329
1330 ImageHandle - Handle that identifies the image to be unloaded.
1331
1332 Returns:
1333
1334 EFI_SUCCESS - The image has been unloaded.
1335 EFI_UNSUPPORTED - The image has been sarted, and does not support unload.
1336 EFI_INVALID_PARAMPETER - ImageHandle is not a valid image handle.
1337
1338 --*/
1339 {
1340 EFI_STATUS Status;
1341 LOADED_IMAGE_PRIVATE_DATA *Image;
1342 EFI_TPL OldTpl;
1343
1344 //
1345 // Prevent possible reentrance to this function
1346 // for the same ImageHandle
1347 //
1348 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
1349
1350 Image = CoreLoadedImageInfo (ImageHandle);
1351 if (Image == NULL ) {
1352 //
1353 // The image handle is not valid
1354 //
1355 Status = EFI_INVALID_PARAMETER;
1356 goto Done;
1357 }
1358
1359 if (Image->Started) {
1360 //
1361 // The image has been started, request it to unload.
1362 //
1363 Status = EFI_UNSUPPORTED;
1364 if (Image->Info.Unload != NULL) {
1365 Status = Image->Info.Unload (ImageHandle);
1366 }
1367
1368 } else {
1369 //
1370 // This Image hasn't been started, thus it can be unloaded
1371 //
1372 Status = EFI_SUCCESS;
1373 }
1374
1375
1376 if (!EFI_ERROR (Status)) {
1377 //
1378 // if the Image was not started or Unloaded O.K. then clean up
1379 //
1380 CoreUnloadAndCloseImage (Image, TRUE);
1381 }
1382
1383 Done:
1384 CoreRestoreTpl (OldTpl);
1385 return Status;
1386 }
1387
1388
1389 EFI_STATUS
1390 EFIAPI
1391 CoreUnloadImageEx (
1392 IN EFI_PE32_IMAGE_PROTOCOL *This,
1393 IN EFI_HANDLE ImageHandle
1394 )
1395 /*++
1396
1397 Routine Description:
1398
1399 Unload the specified image.
1400
1401 Arguments:
1402
1403 This - Indicates the calling context.
1404
1405 ImageHandle - The specified image handle.
1406
1407 Returns:
1408
1409 EFI_INVALID_PARAMETER - Image handle is NULL.
1410
1411 EFI_UNSUPPORTED - Attempt to unload an unsupported image.
1412
1413 EFI_SUCCESS - Image successfully unloaded.
1414
1415 --*/
1416 {
1417 return CoreUnloadImage (ImageHandle);
1418 }