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