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