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