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