]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Image/Image.c
Add PI1.2.1 SAP2 support and UEFI231B mantis 896
[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 - 2012, Intel Corporation. All rights reserved.<BR>
5 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 // The field is define for Loading modules at fixed address feature to tracker the PEI code
75 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding memory page
76 // available or not.
77 //
78 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mDxeCodeMemoryRangeUsageBitMap=NULL;
79
80 typedef struct {
81 UINT16 MachineType;
82 CHAR16 *MachineTypeName;
83 } MACHINE_TYPE_INFO;
84
85 //
86 // EBC machine is not listed in this table, because EBC is in the default supported scopes of other machine type.
87 //
88 GLOBAL_REMOVE_IF_UNREFERENCED MACHINE_TYPE_INFO mMachineTypeInfo[] = {
89 {EFI_IMAGE_MACHINE_IA32, L"IA32"},
90 {EFI_IMAGE_MACHINE_IA64, L"IA64"},
91 {EFI_IMAGE_MACHINE_X64, L"X64"},
92 {EFI_IMAGE_MACHINE_ARMTHUMB_MIXED, L"ARM"}
93 };
94
95 UINT16 mDxeCoreImageMachineType = 0;
96
97 /**
98 Return machine type name.
99
100 @param MachineType The machine type
101
102 @return machine type name
103 **/
104 CHAR16 *
105 GetMachineTypeName (
106 UINT16 MachineType
107 )
108 {
109 UINTN Index;
110
111 for (Index = 0; Index < sizeof(mMachineTypeInfo)/sizeof(mMachineTypeInfo[0]); Index++) {
112 if (mMachineTypeInfo[Index].MachineType == MachineType) {
113 return mMachineTypeInfo[Index].MachineTypeName;
114 }
115 }
116
117 return L"<Unknown>";
118 }
119
120 /**
121 Add the Image Services to EFI Boot Services Table and install the protocol
122 interfaces for this image.
123
124 @param HobStart The HOB to initialize
125
126 @return Status code.
127
128 **/
129 EFI_STATUS
130 CoreInitializeImageServices (
131 IN VOID *HobStart
132 )
133 {
134 EFI_STATUS Status;
135 LOADED_IMAGE_PRIVATE_DATA *Image;
136 EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress;
137 UINT64 DxeCoreImageLength;
138 VOID *DxeCoreEntryPoint;
139 EFI_PEI_HOB_POINTERS DxeCoreHob;
140
141 //
142 // Searching for image hob
143 //
144 DxeCoreHob.Raw = HobStart;
145 while ((DxeCoreHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, DxeCoreHob.Raw)) != NULL) {
146 if (CompareGuid (&DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.Name, &gEfiHobMemoryAllocModuleGuid)) {
147 //
148 // Find Dxe Core HOB
149 //
150 break;
151 }
152 DxeCoreHob.Raw = GET_NEXT_HOB (DxeCoreHob);
153 }
154 ASSERT (DxeCoreHob.Raw != NULL);
155
156 DxeCoreImageBaseAddress = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryBaseAddress;
157 DxeCoreImageLength = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength;
158 DxeCoreEntryPoint = (VOID *) (UINTN) DxeCoreHob.MemoryAllocationModule->EntryPoint;
159 gDxeCoreFileName = &DxeCoreHob.MemoryAllocationModule->ModuleName;
160
161 //
162 // Initialize the fields for an internal driver
163 //
164 Image = &mCorePrivateImage;
165
166 Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)DxeCoreEntryPoint;
167 Image->ImageBasePage = DxeCoreImageBaseAddress;
168 Image->NumberOfPages = (UINTN)(EFI_SIZE_TO_PAGES((UINTN)(DxeCoreImageLength)));
169 Image->Tpl = gEfiCurrentTpl;
170 Image->Info.SystemTable = gDxeCoreST;
171 Image->Info.ImageBase = (VOID *)(UINTN)DxeCoreImageBaseAddress;
172 Image->Info.ImageSize = DxeCoreImageLength;
173
174 //
175 // Install the protocol interfaces for this image
176 //
177 Status = CoreInstallProtocolInterface (
178 &Image->Handle,
179 &gEfiLoadedImageProtocolGuid,
180 EFI_NATIVE_INTERFACE,
181 &Image->Info
182 );
183 ASSERT_EFI_ERROR (Status);
184
185 mCurrentImage = Image;
186
187 //
188 // Fill in DXE globals
189 //
190 mDxeCoreImageMachineType = PeCoffLoaderGetMachineType (Image->Info.ImageBase);
191 gDxeCoreImageHandle = Image->Handle;
192 gDxeCoreLoadedImage = &Image->Info;
193
194 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
195 //
196 // Export DXE Core PE Loader functionality for backward compatibility.
197 //
198 Status = CoreInstallProtocolInterface (
199 &mLoadPe32PrivateData.Handle,
200 &gEfiLoadPeImageProtocolGuid,
201 EFI_NATIVE_INTERFACE,
202 &mLoadPe32PrivateData.Pe32Image
203 );
204 }
205
206 return Status;
207 }
208
209 /**
210 Read image file (specified by UserHandle) into user specified buffer with specified offset
211 and length.
212
213 @param UserHandle Image file handle
214 @param Offset Offset to the source file
215 @param ReadSize For input, pointer of size to read; For output,
216 pointer of size actually read.
217 @param Buffer Buffer to write into
218
219 @retval EFI_SUCCESS Successfully read the specified part of file
220 into buffer.
221
222 **/
223 EFI_STATUS
224 EFIAPI
225 CoreReadImageFile (
226 IN VOID *UserHandle,
227 IN UINTN Offset,
228 IN OUT UINTN *ReadSize,
229 OUT VOID *Buffer
230 )
231 {
232 UINTN EndPosition;
233 IMAGE_FILE_HANDLE *FHand;
234
235 if (UserHandle == NULL || ReadSize == NULL || Buffer == NULL) {
236 return EFI_INVALID_PARAMETER;
237 }
238
239 if (MAX_ADDRESS - Offset < *ReadSize) {
240 return EFI_INVALID_PARAMETER;
241 }
242
243 FHand = (IMAGE_FILE_HANDLE *)UserHandle;
244 ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);
245
246 //
247 // Move data from our local copy of the file
248 //
249 EndPosition = Offset + *ReadSize;
250 if (EndPosition > FHand->SourceSize) {
251 *ReadSize = (UINT32)(FHand->SourceSize - Offset);
252 }
253 if (Offset >= FHand->SourceSize) {
254 *ReadSize = 0;
255 }
256
257 CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);
258 return EFI_SUCCESS;
259 }
260 /**
261 To check memory usage bit map arry to figure out if the memory range the image will be loaded in is available or not. If
262 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
263 The function is only invoked when load modules at fixed address feature is enabled.
264
265 @param ImageBase The base addres the image will be loaded at.
266 @param ImageSize The size of the image
267
268 @retval EFI_SUCCESS The memory range the image will be loaded in is available
269 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
270 **/
271 EFI_STATUS
272 CheckAndMarkFixLoadingMemoryUsageBitMap (
273 IN EFI_PHYSICAL_ADDRESS ImageBase,
274 IN UINTN ImageSize
275 )
276 {
277 UINT32 DxeCodePageNumber;
278 UINT64 DxeCodeSize;
279 EFI_PHYSICAL_ADDRESS DxeCodeBase;
280 UINTN BaseOffsetPageNumber;
281 UINTN TopOffsetPageNumber;
282 UINTN Index;
283 //
284 // The DXE code range includes RuntimeCodePage range and Boot time code range.
285 //
286 DxeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);
287 DxeCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);
288 DxeCodeSize = EFI_PAGES_TO_SIZE(DxeCodePageNumber);
289 DxeCodeBase = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - DxeCodeSize;
290
291 //
292 // If the memory usage bit map is not initialized, do it. Every bit in the array
293 // indicate the status of the corresponding memory page, available or not
294 //
295 if (mDxeCodeMemoryRangeUsageBitMap == NULL) {
296 mDxeCodeMemoryRangeUsageBitMap = AllocateZeroPool(((DxeCodePageNumber/64) + 1)*sizeof(UINT64));
297 }
298 //
299 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
300 //
301 if (!gLoadFixedAddressCodeMemoryReady || mDxeCodeMemoryRangeUsageBitMap == NULL) {
302 return EFI_NOT_FOUND;
303 }
304 //
305 // Test the memory range for loading the image in the DXE code range.
306 //
307 if (gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress < ImageBase + ImageSize ||
308 DxeCodeBase > ImageBase) {
309 return EFI_NOT_FOUND;
310 }
311 //
312 // Test if the memory is avalaible or not.
313 //
314 BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - DxeCodeBase));
315 TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - DxeCodeBase));
316 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
317 if ((mDxeCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
318 //
319 // This page is already used.
320 //
321 return EFI_NOT_FOUND;
322 }
323 }
324
325 //
326 // Being here means the memory range is available. So mark the bits for the memory range
327 //
328 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
329 mDxeCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
330 }
331 return EFI_SUCCESS;
332 }
333 /**
334
335 Get the fixed loadding address from image header assigned by build tool. This function only be called
336 when Loading module at Fixed address feature enabled.
337
338 @param ImageContext Pointer to the image context structure that describes the PE/COFF
339 image that needs to be examined by this function.
340 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
341 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
342
343 **/
344 EFI_STATUS
345 GetPeCoffImageFixLoadingAssignedAddress(
346 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
347 )
348 {
349 UINTN SectionHeaderOffset;
350 EFI_STATUS Status;
351 EFI_IMAGE_SECTION_HEADER SectionHeader;
352 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
353 UINT16 Index;
354 UINTN Size;
355 UINT16 NumberOfSections;
356 IMAGE_FILE_HANDLE *Handle;
357 UINT64 ValueInSectionHeader;
358
359
360 Status = EFI_NOT_FOUND;
361
362 //
363 // Get PeHeader pointer
364 //
365 Handle = (IMAGE_FILE_HANDLE*)ImageContext->Handle;
366 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )Handle->Source + ImageContext->PeCoffHeaderOffset);
367 SectionHeaderOffset = (UINTN)(
368 ImageContext->PeCoffHeaderOffset +
369 sizeof (UINT32) +
370 sizeof (EFI_IMAGE_FILE_HEADER) +
371 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
372 );
373 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
374
375 //
376 // Get base address from the first section header that doesn't point to code section.
377 //
378 for (Index = 0; Index < NumberOfSections; Index++) {
379 //
380 // Read section header from file
381 //
382 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
383 Status = ImageContext->ImageRead (
384 ImageContext->Handle,
385 SectionHeaderOffset,
386 &Size,
387 &SectionHeader
388 );
389 if (EFI_ERROR (Status)) {
390 return Status;
391 }
392
393 Status = EFI_NOT_FOUND;
394
395 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
396 //
397 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
398 // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an
399 // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations
400 // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero
401 //
402 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
403 if (ValueInSectionHeader != 0) {
404 //
405 // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext
406 // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset
407 // relative to top address
408 //
409 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) < 0) {
410 ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress;
411 }
412 //
413 // Check if the memory range is avaliable.
414 //
415 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext->ImageAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
416 }
417 break;
418 }
419 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
420 }
421 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID *)(UINTN)(ImageContext->ImageAddress), Status));
422 return Status;
423 }
424 /**
425 Loads, relocates, and invokes a PE/COFF image
426
427 @param BootPolicy If TRUE, indicates that the request originates
428 from the boot manager, and that the boot
429 manager is attempting to load FilePath as a
430 boot selection.
431 @param Pe32Handle The handle of PE32 image
432 @param Image PE image to be loaded
433 @param DstBuffer The buffer to store the image
434 @param EntryPoint A pointer to the entry point
435 @param Attribute The bit mask of attributes to set for the load
436 PE image
437
438 @retval EFI_SUCCESS The file was loaded, relocated, and invoked
439 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and
440 relocate the PE/COFF file
441 @retval EFI_INVALID_PARAMETER Invalid parameter
442 @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small
443
444 **/
445 EFI_STATUS
446 CoreLoadPeImage (
447 IN BOOLEAN BootPolicy,
448 IN VOID *Pe32Handle,
449 IN LOADED_IMAGE_PRIVATE_DATA *Image,
450 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
451 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
452 IN UINT32 Attribute
453 )
454 {
455 EFI_STATUS Status;
456 BOOLEAN DstBufAlocated;
457 UINTN Size;
458
459 ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));
460
461 Image->ImageContext.Handle = Pe32Handle;
462 Image->ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)CoreReadImageFile;
463
464 //
465 // Get information about the image being loaded
466 //
467 Status = PeCoffLoaderGetImageInfo (&Image->ImageContext);
468 if (EFI_ERROR (Status)) {
469 return Status;
470 }
471
472 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) {
473 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image->ImageContext.Machine)) {
474 //
475 // The PE/COFF loader can support loading image types that can be executed.
476 // If we loaded an image type that we can not execute return EFI_UNSUPORTED.
477 //
478 DEBUG ((EFI_D_ERROR, "Image type %s can't be loaded ", GetMachineTypeName(Image->ImageContext.Machine)));
479 DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType)));
480 return EFI_UNSUPPORTED;
481 }
482 }
483
484 //
485 // Set EFI memory type based on ImageType
486 //
487 switch (Image->ImageContext.ImageType) {
488 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
489 Image->ImageContext.ImageCodeMemoryType = EfiLoaderCode;
490 Image->ImageContext.ImageDataMemoryType = EfiLoaderData;
491 break;
492 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
493 Image->ImageContext.ImageCodeMemoryType = EfiBootServicesCode;
494 Image->ImageContext.ImageDataMemoryType = EfiBootServicesData;
495 break;
496 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
497 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
498 Image->ImageContext.ImageCodeMemoryType = EfiRuntimeServicesCode;
499 Image->ImageContext.ImageDataMemoryType = EfiRuntimeServicesData;
500 break;
501 default:
502 Image->ImageContext.ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
503 return EFI_UNSUPPORTED;
504 }
505
506 //
507 // Allocate memory of the correct memory type aligned on the required image boundry
508 //
509 DstBufAlocated = FALSE;
510 if (DstBuffer == 0) {
511 //
512 // Allocate Destination Buffer as caller did not pass it in
513 //
514
515 if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {
516 Size = (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment;
517 } else {
518 Size = (UINTN)Image->ImageContext.ImageSize;
519 }
520
521 Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size);
522
523 //
524 // If the image relocations have not been stripped, then load at any address.
525 // Otherwise load at the address at which it was linked.
526 //
527 // Memory below 1MB should be treated reserved for CSM and there should be
528 // no modules whose preferred load addresses are below 1MB.
529 //
530 Status = EFI_OUT_OF_RESOURCES;
531 //
532 // If Loading Module At Fixed Address feature is enabled, the module should be loaded to
533 // a specified address.
534 //
535 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 ) {
536 Status = GetPeCoffImageFixLoadingAssignedAddress (&(Image->ImageContext));
537
538 if (EFI_ERROR (Status)) {
539 //
540 // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.
541 //
542 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));
543
544 Status = CoreAllocatePages (
545 AllocateAnyPages,
546 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
547 Image->NumberOfPages,
548 &Image->ImageContext.ImageAddress
549 );
550 }
551 } else {
552 if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) {
553 Status = CoreAllocatePages (
554 AllocateAddress,
555 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
556 Image->NumberOfPages,
557 &Image->ImageContext.ImageAddress
558 );
559 }
560 if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {
561 Status = CoreAllocatePages (
562 AllocateAnyPages,
563 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),
564 Image->NumberOfPages,
565 &Image->ImageContext.ImageAddress
566 );
567 }
568 }
569 if (EFI_ERROR (Status)) {
570 return Status;
571 }
572 DstBufAlocated = TRUE;
573 } else {
574 //
575 // Caller provided the destination buffer
576 //
577
578 if (Image->ImageContext.RelocationsStripped && (Image->ImageContext.ImageAddress != DstBuffer)) {
579 //
580 // If the image relocations were stripped, and the caller provided a
581 // destination buffer address that does not match the address that the
582 // image is linked at, then the image cannot be loaded.
583 //
584 return EFI_INVALID_PARAMETER;
585 }
586
587 if (Image->NumberOfPages != 0 &&
588 Image->NumberOfPages <
589 (EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment))) {
590 Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);
591 return EFI_BUFFER_TOO_SMALL;
592 }
593
594 Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);
595 Image->ImageContext.ImageAddress = DstBuffer;
596 }
597
598 Image->ImageBasePage = Image->ImageContext.ImageAddress;
599 if (!Image->ImageContext.IsTeImage) {
600 Image->ImageContext.ImageAddress =
601 (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &
602 ~((UINTN)Image->ImageContext.SectionAlignment - 1);
603 }
604
605 //
606 // Load the image from the file into the allocated memory
607 //
608 Status = PeCoffLoaderLoadImage (&Image->ImageContext);
609 if (EFI_ERROR (Status)) {
610 goto Done;
611 }
612
613 //
614 // If this is a Runtime Driver, then allocate memory for the FixupData that
615 // is used to relocate the image when SetVirtualAddressMap() is called. The
616 // relocation is done by the Runtime AP.
617 //
618 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) {
619 if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
620 Image->ImageContext.FixupData = AllocateRuntimePool ((UINTN)(Image->ImageContext.FixupDataSize));
621 if (Image->ImageContext.FixupData == NULL) {
622 Status = EFI_OUT_OF_RESOURCES;
623 goto Done;
624 }
625 }
626 }
627
628 //
629 // Relocate the image in memory
630 //
631 Status = PeCoffLoaderRelocateImage (&Image->ImageContext);
632 if (EFI_ERROR (Status)) {
633 goto Done;
634 }
635
636 //
637 // Flush the Instruction Cache
638 //
639 InvalidateInstructionCacheRange ((VOID *)(UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.ImageSize);
640
641 //
642 // Copy the machine type from the context to the image private data. This
643 // is needed during image unload to know if we should call an EBC protocol
644 // to unload the image.
645 //
646 Image->Machine = Image->ImageContext.Machine;
647
648 //
649 // Get the image entry point. If it's an EBC image, then call into the
650 // interpreter to create a thunk for the entry point and use the returned
651 // value for the entry point.
652 //
653 Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)Image->ImageContext.EntryPoint;
654 if (Image->ImageContext.Machine == EFI_IMAGE_MACHINE_EBC) {
655 //
656 // Locate the EBC interpreter protocol
657 //
658 Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);
659 if (EFI_ERROR(Status) || Image->Ebc == NULL) {
660 DEBUG ((DEBUG_LOAD | DEBUG_ERROR, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
661 goto Done;
662 }
663
664 //
665 // Register a callback for flushing the instruction cache so that created
666 // thunks can be flushed.
667 //
668 Status = Image->Ebc->RegisterICacheFlush (Image->Ebc, (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange);
669 if (EFI_ERROR(Status)) {
670 goto Done;
671 }
672
673 //
674 // Create a thunk for the image's entry point. This will be the new
675 // entry point for the image.
676 //
677 Status = Image->Ebc->CreateThunk (
678 Image->Ebc,
679 Image->Handle,
680 (VOID *)(UINTN) Image->ImageContext.EntryPoint,
681 (VOID **) &Image->EntryPoint
682 );
683 if (EFI_ERROR(Status)) {
684 goto Done;
685 }
686 }
687
688 //
689 // Fill in the image information for the Loaded Image Protocol
690 //
691 Image->Type = Image->ImageContext.ImageType;
692 Image->Info.ImageBase = (VOID *)(UINTN)Image->ImageContext.ImageAddress;
693 Image->Info.ImageSize = Image->ImageContext.ImageSize;
694 Image->Info.ImageCodeType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType);
695 Image->Info.ImageDataType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageDataMemoryType);
696 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) {
697 if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
698 //
699 // Make a list off all the RT images so we can let the RT AP know about them.
700 //
701 Image->RuntimeData = AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY));
702 if (Image->RuntimeData == NULL) {
703 goto Done;
704 }
705 Image->RuntimeData->ImageBase = Image->Info.ImageBase;
706 Image->RuntimeData->ImageSize = (UINT64) (Image->Info.ImageSize);
707 Image->RuntimeData->RelocationData = Image->ImageContext.FixupData;
708 Image->RuntimeData->Handle = Image->Handle;
709 InsertTailList (&gRuntime->ImageHead, &Image->RuntimeData->Link);
710 }
711 }
712
713 //
714 // Fill in the entry point of the image if it is available
715 //
716 if (EntryPoint != NULL) {
717 *EntryPoint = Image->ImageContext.EntryPoint;
718 }
719
720 //
721 // Print the load address and the PDB file name if it is available
722 //
723
724 DEBUG_CODE_BEGIN ();
725
726 UINTN Index;
727 UINTN StartIndex;
728 CHAR8 EfiFileName[256];
729
730
731 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
732 "Loading driver at 0x%11p EntryPoint=0x%11p ",
733 (VOID *)(UINTN) Image->ImageContext.ImageAddress,
734 FUNCTION_ENTRY_POINT (Image->ImageContext.EntryPoint)));
735
736
737 //
738 // Print Module Name by Pdb file path.
739 // Windows and Unix style file path are all trimmed correctly.
740 //
741 if (Image->ImageContext.PdbPointer != NULL) {
742 StartIndex = 0;
743 for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) {
744 if ((Image->ImageContext.PdbPointer[Index] == '\\') || (Image->ImageContext.PdbPointer[Index] == '/')) {
745 StartIndex = Index + 1;
746 }
747 }
748 //
749 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
750 // The PDB file name is limited in the range of 0~255.
751 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
752 //
753 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
754 EfiFileName[Index] = Image->ImageContext.PdbPointer[Index + StartIndex];
755 if (EfiFileName[Index] == 0) {
756 EfiFileName[Index] = '.';
757 }
758 if (EfiFileName[Index] == '.') {
759 EfiFileName[Index + 1] = 'e';
760 EfiFileName[Index + 2] = 'f';
761 EfiFileName[Index + 3] = 'i';
762 EfiFileName[Index + 4] = 0;
763 break;
764 }
765 }
766
767 if (Index == sizeof (EfiFileName) - 4) {
768 EfiFileName[Index] = 0;
769 }
770 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
771 }
772 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
773
774 DEBUG_CODE_END ();
775
776 return EFI_SUCCESS;
777
778 Done:
779
780 //
781 // Free memory.
782 //
783
784 if (DstBufAlocated) {
785 CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);
786 }
787
788 if (Image->ImageContext.FixupData != NULL) {
789 CoreFreePool (Image->ImageContext.FixupData);
790 }
791
792 return Status;
793 }
794
795
796
797 /**
798 Get the image's private data from its handle.
799
800 @param ImageHandle The image handle
801
802 @return Return the image private data associated with ImageHandle.
803
804 **/
805 LOADED_IMAGE_PRIVATE_DATA *
806 CoreLoadedImageInfo (
807 IN EFI_HANDLE ImageHandle
808 )
809 {
810 EFI_STATUS Status;
811 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
812 LOADED_IMAGE_PRIVATE_DATA *Image;
813
814 Status = CoreHandleProtocol (
815 ImageHandle,
816 &gEfiLoadedImageProtocolGuid,
817 (VOID **)&LoadedImage
818 );
819 if (!EFI_ERROR (Status)) {
820 Image = LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage);
821 } else {
822 DEBUG ((DEBUG_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle));
823 Image = NULL;
824 }
825
826 return Image;
827 }
828
829
830 /**
831 Unloads EFI image from memory.
832
833 @param Image EFI image
834 @param FreePage Free allocated pages
835
836 **/
837 VOID
838 CoreUnloadAndCloseImage (
839 IN LOADED_IMAGE_PRIVATE_DATA *Image,
840 IN BOOLEAN FreePage
841 )
842 {
843 EFI_STATUS Status;
844 UINTN HandleCount;
845 EFI_HANDLE *HandleBuffer;
846 UINTN HandleIndex;
847 EFI_GUID **ProtocolGuidArray;
848 UINTN ArrayCount;
849 UINTN ProtocolIndex;
850 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
851 UINTN OpenInfoCount;
852 UINTN OpenInfoIndex;
853
854 if (Image->Ebc != NULL) {
855 //
856 // If EBC protocol exists we must perform cleanups for this image.
857 //
858 Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);
859 }
860
861 //
862 // Unload image, free Image->ImageContext->ModHandle
863 //
864 PeCoffLoaderUnloadImage (&Image->ImageContext);
865
866 //
867 // Free our references to the image handle
868 //
869 if (Image->Handle != NULL) {
870
871 Status = CoreLocateHandleBuffer (
872 AllHandles,
873 NULL,
874 NULL,
875 &HandleCount,
876 &HandleBuffer
877 );
878 if (!EFI_ERROR (Status)) {
879 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
880 Status = CoreProtocolsPerHandle (
881 HandleBuffer[HandleIndex],
882 &ProtocolGuidArray,
883 &ArrayCount
884 );
885 if (!EFI_ERROR (Status)) {
886 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
887 Status = CoreOpenProtocolInformation (
888 HandleBuffer[HandleIndex],
889 ProtocolGuidArray[ProtocolIndex],
890 &OpenInfo,
891 &OpenInfoCount
892 );
893 if (!EFI_ERROR (Status)) {
894 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
895 if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {
896 Status = CoreCloseProtocol (
897 HandleBuffer[HandleIndex],
898 ProtocolGuidArray[ProtocolIndex],
899 Image->Handle,
900 OpenInfo[OpenInfoIndex].ControllerHandle
901 );
902 }
903 }
904 if (OpenInfo != NULL) {
905 CoreFreePool(OpenInfo);
906 }
907 }
908 }
909 if (ProtocolGuidArray != NULL) {
910 CoreFreePool(ProtocolGuidArray);
911 }
912 }
913 }
914 if (HandleBuffer != NULL) {
915 CoreFreePool (HandleBuffer);
916 }
917 }
918
919 CoreRemoveDebugImageInfoEntry (Image->Handle);
920
921 Status = CoreUninstallProtocolInterface (
922 Image->Handle,
923 &gEfiLoadedImageDevicePathProtocolGuid,
924 Image->LoadedImageDevicePath
925 );
926
927 Status = CoreUninstallProtocolInterface (
928 Image->Handle,
929 &gEfiLoadedImageProtocolGuid,
930 &Image->Info
931 );
932
933 if (Image->ImageContext.HiiResourceData != 0) {
934 Status = CoreUninstallProtocolInterface (
935 Image->Handle,
936 &gEfiHiiPackageListProtocolGuid,
937 (VOID *) (UINTN) Image->ImageContext.HiiResourceData
938 );
939 }
940
941 }
942
943 if (Image->RuntimeData != NULL) {
944 if (Image->RuntimeData->Link.ForwardLink != NULL) {
945 //
946 // Remove the Image from the Runtime Image list as we are about to Free it!
947 //
948 RemoveEntryList (&Image->RuntimeData->Link);
949 }
950 CoreFreePool (Image->RuntimeData);
951 }
952
953 //
954 // Free the Image from memory
955 //
956 if ((Image->ImageBasePage != 0) && FreePage) {
957 CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);
958 }
959
960 //
961 // Done with the Image structure
962 //
963 if (Image->Info.FilePath != NULL) {
964 CoreFreePool (Image->Info.FilePath);
965 }
966
967 if (Image->LoadedImageDevicePath != NULL) {
968 CoreFreePool (Image->LoadedImageDevicePath);
969 }
970
971 if (Image->FixupData != NULL) {
972 CoreFreePool (Image->FixupData);
973 }
974
975 CoreFreePool (Image);
976 }
977
978
979 /**
980 Loads an EFI image into memory and returns a handle to the image.
981
982 @param BootPolicy If TRUE, indicates that the request originates
983 from the boot manager, and that the boot
984 manager is attempting to load FilePath as a
985 boot selection.
986 @param ParentImageHandle The caller's image handle.
987 @param FilePath The specific file path from which the image is
988 loaded.
989 @param SourceBuffer If not NULL, a pointer to the memory location
990 containing a copy of the image to be loaded.
991 @param SourceSize The size in bytes of SourceBuffer.
992 @param DstBuffer The buffer to store the image
993 @param NumberOfPages If not NULL, it inputs a pointer to the page
994 number of DstBuffer and outputs a pointer to
995 the page number of the image. If this number is
996 not enough, return EFI_BUFFER_TOO_SMALL and
997 this parameter contains the required number.
998 @param ImageHandle Pointer to the returned image handle that is
999 created when the image is successfully loaded.
1000 @param EntryPoint A pointer to the entry point
1001 @param Attribute The bit mask of attributes to set for the load
1002 PE image
1003
1004 @retval EFI_SUCCESS The image was loaded into memory.
1005 @retval EFI_NOT_FOUND The FilePath was not found.
1006 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1007 @retval EFI_BUFFER_TOO_SMALL The buffer is too small
1008 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1009 path cannot be parsed to locate the proper
1010 protocol for loading the file.
1011 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1012 resources.
1013 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1014 understood.
1015 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1016 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1017 image from being loaded. NULL is returned in *ImageHandle.
1018 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1019 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1020 platform policy specifies that the image should not be started.
1021
1022 **/
1023 EFI_STATUS
1024 CoreLoadImageCommon (
1025 IN BOOLEAN BootPolicy,
1026 IN EFI_HANDLE ParentImageHandle,
1027 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1028 IN VOID *SourceBuffer OPTIONAL,
1029 IN UINTN SourceSize,
1030 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
1031 IN OUT UINTN *NumberOfPages OPTIONAL,
1032 OUT EFI_HANDLE *ImageHandle,
1033 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
1034 IN UINT32 Attribute
1035 )
1036 {
1037 LOADED_IMAGE_PRIVATE_DATA *Image;
1038 LOADED_IMAGE_PRIVATE_DATA *ParentImage;
1039 IMAGE_FILE_HANDLE FHand;
1040 EFI_STATUS Status;
1041 EFI_STATUS SecurityStatus;
1042 EFI_HANDLE DeviceHandle;
1043 UINT32 AuthenticationStatus;
1044 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
1045 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
1046 UINTN FilePathSize;
1047 BOOLEAN ImageIsFromFv;
1048
1049 SecurityStatus = EFI_SUCCESS;
1050
1051 ASSERT (gEfiCurrentTpl < TPL_NOTIFY);
1052 ParentImage = NULL;
1053
1054 //
1055 // The caller must pass in a valid ParentImageHandle
1056 //
1057 if (ImageHandle == NULL || ParentImageHandle == NULL) {
1058 return EFI_INVALID_PARAMETER;
1059 }
1060
1061 ParentImage = CoreLoadedImageInfo (ParentImageHandle);
1062 if (ParentImage == NULL) {
1063 DEBUG((DEBUG_LOAD|DEBUG_ERROR, "LoadImageEx: Parent handle not an image handle\n"));
1064 return EFI_INVALID_PARAMETER;
1065 }
1066
1067 ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE));
1068 FHand.Signature = IMAGE_FILE_HANDLE_SIGNATURE;
1069 OriginalFilePath = FilePath;
1070 HandleFilePath = FilePath;
1071 DeviceHandle = NULL;
1072 Status = EFI_SUCCESS;
1073 AuthenticationStatus = 0;
1074 ImageIsFromFv = FALSE;
1075
1076 //
1077 // If the caller passed a copy of the file, then just use it
1078 //
1079 if (SourceBuffer != NULL) {
1080 FHand.Source = SourceBuffer;
1081 FHand.SourceSize = SourceSize;
1082 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle);
1083 if (SourceSize > 0) {
1084 Status = EFI_SUCCESS;
1085 } else {
1086 Status = EFI_LOAD_ERROR;
1087 }
1088 } else {
1089 if (FilePath == NULL) {
1090 return EFI_INVALID_PARAMETER;
1091 }
1092 //
1093 // Get the source file buffer by its device path.
1094 //
1095 FHand.Source = GetFileBufferByFilePath (
1096 BootPolicy,
1097 FilePath,
1098 &FHand.SourceSize,
1099 &AuthenticationStatus
1100 );
1101 if (FHand.Source == NULL) {
1102 Status = EFI_NOT_FOUND;
1103 } else {
1104 //
1105 // Try to get the image device handle by checking the match protocol.
1106 //
1107 FHand.FreeBuffer = TRUE;
1108 Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);
1109 if (!EFI_ERROR (Status)) {
1110 ImageIsFromFv = TRUE;
1111 } else {
1112 HandleFilePath = FilePath;
1113 Status = CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &HandleFilePath, &DeviceHandle);
1114 if (EFI_ERROR (Status)) {
1115 if (!BootPolicy) {
1116 HandleFilePath = FilePath;
1117 Status = CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid, &HandleFilePath, &DeviceHandle);
1118 }
1119 if (EFI_ERROR (Status)) {
1120 HandleFilePath = FilePath;
1121 Status = CoreLocateDevicePath (&gEfiLoadFileProtocolGuid, &HandleFilePath, &DeviceHandle);
1122 }
1123 }
1124 }
1125 }
1126 }
1127
1128 if (Status == EFI_ALREADY_STARTED) {
1129 Image = NULL;
1130 goto Done;
1131 } else if (EFI_ERROR (Status)) {
1132 return Status;
1133 }
1134
1135 if (gSecurity2 != NULL) {
1136 //
1137 // Verify File Authentication through the Security2 Architectural Protocol
1138 //
1139 SecurityStatus = gSecurity2->FileAuthentication (
1140 gSecurity2,
1141 OriginalFilePath,
1142 FHand.Source,
1143 FHand.SourceSize,
1144 BootPolicy
1145 );
1146 if (!EFI_ERROR (SecurityStatus) && ImageIsFromFv) {
1147 //
1148 // When Security2 is installed, Security Architectural Protocol must be published.
1149 //
1150 ASSERT (gSecurity != NULL);
1151
1152 //
1153 // Verify the Authentication Status through the Security Architectural Protocol
1154 // Only on images that have been read using Firmware Volume protocol.
1155 //
1156 SecurityStatus = gSecurity->FileAuthenticationState (
1157 gSecurity,
1158 AuthenticationStatus,
1159 OriginalFilePath
1160 );
1161 }
1162 } else if ((gSecurity != NULL) && (OriginalFilePath != NULL)) {
1163 //
1164 // Verify the Authentication Status through the Security Architectural Protocol
1165 //
1166 SecurityStatus = gSecurity->FileAuthenticationState (
1167 gSecurity,
1168 AuthenticationStatus,
1169 OriginalFilePath
1170 );
1171 }
1172
1173 //
1174 // Check Security Status.
1175 //
1176 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
1177 if (SecurityStatus == EFI_ACCESS_DENIED) {
1178 //
1179 // Image was not loaded because the platform policy prohibits the image from being loaded.
1180 // It's the only place we could meet EFI_ACCESS_DENIED.
1181 //
1182 *ImageHandle = NULL;
1183 }
1184 Status = SecurityStatus;
1185 Image = NULL;
1186 goto Done;
1187 }
1188
1189 //
1190 // Allocate a new image structure
1191 //
1192 Image = AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA));
1193 if (Image == NULL) {
1194 return EFI_OUT_OF_RESOURCES;
1195 }
1196
1197 //
1198 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
1199 //
1200 FilePath = OriginalFilePath;
1201 if (DeviceHandle != NULL) {
1202 Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
1203 if (!EFI_ERROR (Status)) {
1204 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
1205 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );
1206 }
1207 }
1208 //
1209 // Initialize the fields for an internal driver
1210 //
1211 Image->Signature = LOADED_IMAGE_PRIVATE_DATA_SIGNATURE;
1212 Image->Info.SystemTable = gDxeCoreST;
1213 Image->Info.DeviceHandle = DeviceHandle;
1214 Image->Info.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1215 Image->Info.FilePath = DuplicateDevicePath (FilePath);
1216 Image->Info.ParentHandle = ParentImageHandle;
1217
1218
1219 if (NumberOfPages != NULL) {
1220 Image->NumberOfPages = *NumberOfPages ;
1221 } else {
1222 Image->NumberOfPages = 0 ;
1223 }
1224
1225 //
1226 // Install the protocol interfaces for this image
1227 // don't fire notifications yet
1228 //
1229 Status = CoreInstallProtocolInterfaceNotify (
1230 &Image->Handle,
1231 &gEfiLoadedImageProtocolGuid,
1232 EFI_NATIVE_INTERFACE,
1233 &Image->Info,
1234 FALSE
1235 );
1236 if (EFI_ERROR (Status)) {
1237 goto Done;
1238 }
1239
1240 //
1241 // Load the image. If EntryPoint is Null, it will not be set.
1242 //
1243 Status = CoreLoadPeImage (BootPolicy, &FHand, Image, DstBuffer, EntryPoint, Attribute);
1244 if (EFI_ERROR (Status)) {
1245 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_OUT_OF_RESOURCES)) {
1246 if (NumberOfPages != NULL) {
1247 *NumberOfPages = Image->NumberOfPages;
1248 }
1249 }
1250 goto Done;
1251 }
1252
1253 if (NumberOfPages != NULL) {
1254 *NumberOfPages = Image->NumberOfPages;
1255 }
1256
1257 //
1258 // Register the image in the Debug Image Info Table if the attribute is set
1259 //
1260 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) != 0) {
1261 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL, &Image->Info, Image->Handle);
1262 }
1263
1264 //
1265 //Reinstall loaded image protocol to fire any notifications
1266 //
1267 Status = CoreReinstallProtocolInterface (
1268 Image->Handle,
1269 &gEfiLoadedImageProtocolGuid,
1270 &Image->Info,
1271 &Image->Info
1272 );
1273 if (EFI_ERROR (Status)) {
1274 goto Done;
1275 }
1276
1277 //
1278 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,
1279 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.
1280 //
1281 if (OriginalFilePath != NULL) {
1282 Image->LoadedImageDevicePath = DuplicateDevicePath (OriginalFilePath);
1283 }
1284
1285 //
1286 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image
1287 //
1288 Status = CoreInstallProtocolInterface (
1289 &Image->Handle,
1290 &gEfiLoadedImageDevicePathProtocolGuid,
1291 EFI_NATIVE_INTERFACE,
1292 Image->LoadedImageDevicePath
1293 );
1294 if (EFI_ERROR (Status)) {
1295 goto Done;
1296 }
1297
1298 //
1299 // Install HII Package List Protocol onto the image handle
1300 //
1301 if (Image->ImageContext.HiiResourceData != 0) {
1302 Status = CoreInstallProtocolInterface (
1303 &Image->Handle,
1304 &gEfiHiiPackageListProtocolGuid,
1305 EFI_NATIVE_INTERFACE,
1306 (VOID *) (UINTN) Image->ImageContext.HiiResourceData
1307 );
1308 if (EFI_ERROR (Status)) {
1309 goto Done;
1310 }
1311 }
1312
1313 //
1314 // Success. Return the image handle
1315 //
1316 *ImageHandle = Image->Handle;
1317
1318 Done:
1319 //
1320 // All done accessing the source file
1321 // If we allocated the Source buffer, free it
1322 //
1323 if (FHand.FreeBuffer) {
1324 CoreFreePool (FHand.Source);
1325 }
1326
1327 //
1328 // There was an error. If there's an Image structure, free it
1329 //
1330 if (EFI_ERROR (Status)) {
1331 if (Image != NULL) {
1332 CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer == 0));
1333 Image = NULL;
1334 }
1335 } else if (EFI_ERROR (SecurityStatus)) {
1336 Status = SecurityStatus;
1337 }
1338
1339 //
1340 // Track the return status from LoadImage.
1341 //
1342 if (Image != NULL) {
1343 Image->LoadImageStatus = Status;
1344 }
1345
1346 return Status;
1347 }
1348
1349
1350
1351
1352 /**
1353 Loads an EFI image into memory and returns a handle to the image.
1354
1355 @param BootPolicy If TRUE, indicates that the request originates
1356 from the boot manager, and that the boot
1357 manager is attempting to load FilePath as a
1358 boot selection.
1359 @param ParentImageHandle The caller's image handle.
1360 @param FilePath The specific file path from which the image is
1361 loaded.
1362 @param SourceBuffer If not NULL, a pointer to the memory location
1363 containing a copy of the image to be loaded.
1364 @param SourceSize The size in bytes of SourceBuffer.
1365 @param ImageHandle Pointer to the returned image handle that is
1366 created when the image is successfully loaded.
1367
1368 @retval EFI_SUCCESS The image was loaded into memory.
1369 @retval EFI_NOT_FOUND The FilePath was not found.
1370 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1371 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1372 path cannot be parsed to locate the proper
1373 protocol for loading the file.
1374 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1375 resources.
1376 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1377 understood.
1378 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1379 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1380 image from being loaded. NULL is returned in *ImageHandle.
1381 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1382 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1383 platform policy specifies that the image should not be started.
1384
1385 **/
1386 EFI_STATUS
1387 EFIAPI
1388 CoreLoadImage (
1389 IN BOOLEAN BootPolicy,
1390 IN EFI_HANDLE ParentImageHandle,
1391 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1392 IN VOID *SourceBuffer OPTIONAL,
1393 IN UINTN SourceSize,
1394 OUT EFI_HANDLE *ImageHandle
1395 )
1396 {
1397 EFI_STATUS Status;
1398 UINT64 Tick;
1399 EFI_HANDLE Handle;
1400
1401 Tick = 0;
1402 PERF_CODE (
1403 Tick = GetPerformanceCounter ();
1404 );
1405
1406 Status = CoreLoadImageCommon (
1407 BootPolicy,
1408 ParentImageHandle,
1409 FilePath,
1410 SourceBuffer,
1411 SourceSize,
1412 (EFI_PHYSICAL_ADDRESS) (UINTN) NULL,
1413 NULL,
1414 ImageHandle,
1415 NULL,
1416 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION
1417 );
1418
1419 Handle = NULL;
1420 if (!EFI_ERROR (Status)) {
1421 //
1422 // ImageHandle will be valid only Status is success.
1423 //
1424 Handle = *ImageHandle;
1425 }
1426
1427 PERF_START (Handle, "LoadImage:", NULL, Tick);
1428 PERF_END (Handle, "LoadImage:", NULL, 0);
1429
1430 return Status;
1431 }
1432
1433
1434
1435 /**
1436 Loads an EFI image into memory and returns a handle to the image with extended parameters.
1437
1438 @param This Calling context
1439 @param ParentImageHandle The caller's image handle.
1440 @param FilePath The specific file path from which the image is
1441 loaded.
1442 @param SourceBuffer If not NULL, a pointer to the memory location
1443 containing a copy of the image to be loaded.
1444 @param SourceSize The size in bytes of SourceBuffer.
1445 @param DstBuffer The buffer to store the image.
1446 @param NumberOfPages For input, specifies the space size of the
1447 image by caller if not NULL. For output,
1448 specifies the actual space size needed.
1449 @param ImageHandle Image handle for output.
1450 @param EntryPoint Image entry point for output.
1451 @param Attribute The bit mask of attributes to set for the load
1452 PE image.
1453
1454 @retval EFI_SUCCESS The image was loaded into memory.
1455 @retval EFI_NOT_FOUND The FilePath was not found.
1456 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1457 @retval EFI_UNSUPPORTED The image type is not supported, or the device
1458 path cannot be parsed to locate the proper
1459 protocol for loading the file.
1460 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient
1461 resources.
1462 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not
1463 understood.
1464 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.
1465 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the
1466 image from being loaded. NULL is returned in *ImageHandle.
1467 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a
1468 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
1469 platform policy specifies that the image should not be started.
1470
1471 **/
1472 EFI_STATUS
1473 EFIAPI
1474 CoreLoadImageEx (
1475 IN EFI_PE32_IMAGE_PROTOCOL *This,
1476 IN EFI_HANDLE ParentImageHandle,
1477 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1478 IN VOID *SourceBuffer OPTIONAL,
1479 IN UINTN SourceSize,
1480 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,
1481 OUT UINTN *NumberOfPages OPTIONAL,
1482 OUT EFI_HANDLE *ImageHandle,
1483 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,
1484 IN UINT32 Attribute
1485 )
1486 {
1487 EFI_STATUS Status;
1488 UINT64 Tick;
1489 EFI_HANDLE Handle;
1490
1491 Tick = 0;
1492 PERF_CODE (
1493 Tick = GetPerformanceCounter ();
1494 );
1495
1496 Status = CoreLoadImageCommon (
1497 TRUE,
1498 ParentImageHandle,
1499 FilePath,
1500 SourceBuffer,
1501 SourceSize,
1502 DstBuffer,
1503 NumberOfPages,
1504 ImageHandle,
1505 EntryPoint,
1506 Attribute
1507 );
1508
1509 Handle = NULL;
1510 if (!EFI_ERROR (Status)) {
1511 //
1512 // ImageHandle will be valid only Status is success.
1513 //
1514 Handle = *ImageHandle;
1515 }
1516
1517 PERF_START (Handle, "LoadImage:", NULL, Tick);
1518 PERF_END (Handle, "LoadImage:", NULL, 0);
1519
1520 return Status;
1521 }
1522
1523
1524 /**
1525 Transfer control to a loaded image's entry point.
1526
1527 @param ImageHandle Handle of image to be started.
1528 @param ExitDataSize Pointer of the size to ExitData
1529 @param ExitData Pointer to a pointer to a data buffer that
1530 includes a Null-terminated string,
1531 optionally followed by additional binary data.
1532 The string is a description that the caller may
1533 use to further indicate the reason for the
1534 image's exit.
1535
1536 @retval EFI_INVALID_PARAMETER Invalid parameter
1537 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1538 @retval EFI_SECURITY_VIOLATION The current platform policy specifies that the image should not be started.
1539 @retval EFI_SUCCESS Successfully transfer control to the image's
1540 entry point.
1541
1542 **/
1543 EFI_STATUS
1544 EFIAPI
1545 CoreStartImage (
1546 IN EFI_HANDLE ImageHandle,
1547 OUT UINTN *ExitDataSize,
1548 OUT CHAR16 **ExitData OPTIONAL
1549 )
1550 {
1551 EFI_STATUS Status;
1552 LOADED_IMAGE_PRIVATE_DATA *Image;
1553 LOADED_IMAGE_PRIVATE_DATA *LastImage;
1554 UINT64 HandleDatabaseKey;
1555 UINTN SetJumpFlag;
1556 UINT64 Tick;
1557 EFI_HANDLE Handle;
1558
1559 Tick = 0;
1560 Handle = ImageHandle;
1561
1562 Image = CoreLoadedImageInfo (ImageHandle);
1563 if (Image == NULL || Image->Started) {
1564 return EFI_INVALID_PARAMETER;
1565 }
1566 if (EFI_ERROR (Image->LoadImageStatus)) {
1567 return Image->LoadImageStatus;
1568 }
1569
1570 //
1571 // The image to be started must have the machine type supported by DxeCore.
1572 //
1573 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) {
1574 //
1575 // Do not ASSERT here, because image might be loaded via EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED
1576 // But it can not be started.
1577 //
1578 DEBUG ((EFI_D_ERROR, "Image type %s can't be started ", GetMachineTypeName(Image->Machine)));
1579 DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType)));
1580 return EFI_UNSUPPORTED;
1581 }
1582
1583 PERF_CODE (
1584 Tick = GetPerformanceCounter ();
1585 );
1586
1587
1588 //
1589 // Push the current start image context, and
1590 // link the current image to the head. This is the
1591 // only image that can call Exit()
1592 //
1593 HandleDatabaseKey = CoreGetHandleDatabaseKey ();
1594 LastImage = mCurrentImage;
1595 mCurrentImage = Image;
1596 Image->Tpl = gEfiCurrentTpl;
1597
1598 //
1599 // Set long jump for Exit() support
1600 // JumpContext must be aligned on a CPU specific boundary.
1601 // Overallocate the buffer and force the required alignment
1602 //
1603 Image->JumpBuffer = AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);
1604 if (Image->JumpBuffer == NULL) {
1605 //
1606 // Image may be unloaded after return with failure,
1607 // then ImageHandle may be invalid, so use NULL handle to record perf log.
1608 //
1609 PERF_START (NULL, "StartImage:", NULL, Tick);
1610 PERF_END (NULL, "StartImage:", NULL, 0);
1611 return EFI_OUT_OF_RESOURCES;
1612 }
1613 Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);
1614
1615 SetJumpFlag = SetJump (Image->JumpContext);
1616 //
1617 // The initial call to SetJump() must always return 0.
1618 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().
1619 //
1620 if (SetJumpFlag == 0) {
1621 //
1622 // Call the image's entry point
1623 //
1624 Image->Started = TRUE;
1625 Image->Status = Image->EntryPoint (ImageHandle, Image->Info.SystemTable);
1626
1627 //
1628 // Add some debug information if the image returned with error.
1629 // This make the user aware and check if the driver image have already released
1630 // all the resource in this situation.
1631 //
1632 DEBUG_CODE_BEGIN ();
1633 if (EFI_ERROR (Image->Status)) {
1634 DEBUG ((DEBUG_ERROR, "Error: Image at %11p start failed: %r\n", Image->Info.ImageBase, Image->Status));
1635 }
1636 DEBUG_CODE_END ();
1637
1638 //
1639 // If the image returns, exit it through Exit()
1640 //
1641 CoreExit (ImageHandle, Image->Status, 0, NULL);
1642 }
1643
1644 //
1645 // Image has completed. Verify the tpl is the same
1646 //
1647 ASSERT (Image->Tpl == gEfiCurrentTpl);
1648 CoreRestoreTpl (Image->Tpl);
1649
1650 CoreFreePool (Image->JumpBuffer);
1651
1652 //
1653 // Pop the current start image context
1654 //
1655 mCurrentImage = LastImage;
1656
1657 //
1658 // Go connect any handles that were created or modified while the image executed.
1659 //
1660 CoreConnectHandlesByKey (HandleDatabaseKey);
1661
1662 //
1663 // Handle the image's returned ExitData
1664 //
1665 DEBUG_CODE_BEGIN ();
1666 if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {
1667
1668 DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %p", (UINT32)Image->ExitDataSize, Image->ExitData));
1669 if (Image->ExitData != NULL) {
1670 DEBUG ((DEBUG_LOAD, " (%hs)", Image->ExitData));
1671 }
1672 DEBUG ((DEBUG_LOAD, "\n"));
1673 }
1674 DEBUG_CODE_END ();
1675
1676 //
1677 // Return the exit data to the caller
1678 //
1679 if (ExitData != NULL && ExitDataSize != NULL) {
1680 *ExitDataSize = Image->ExitDataSize;
1681 *ExitData = Image->ExitData;
1682 } else {
1683 //
1684 // Caller doesn't want the exit data, free it
1685 //
1686 CoreFreePool (Image->ExitData);
1687 Image->ExitData = NULL;
1688 }
1689
1690 //
1691 // Save the Status because Image will get destroyed if it is unloaded.
1692 //
1693 Status = Image->Status;
1694
1695 //
1696 // If the image returned an error, or if the image is an application
1697 // unload it
1698 //
1699 if (EFI_ERROR (Image->Status) || Image->Type == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1700 CoreUnloadAndCloseImage (Image, TRUE);
1701 //
1702 // ImageHandle may be invalid after the image is unloaded, so use NULL handle to record perf log.
1703 //
1704 Handle = NULL;
1705 }
1706
1707 //
1708 // Done
1709 //
1710 PERF_START (Handle, "StartImage:", NULL, Tick);
1711 PERF_END (Handle, "StartImage:", NULL, 0);
1712 return Status;
1713 }
1714
1715 /**
1716 Terminates the currently loaded EFI image and returns control to boot services.
1717
1718 @param ImageHandle Handle that identifies the image. This
1719 parameter is passed to the image on entry.
1720 @param Status The image's exit code.
1721 @param ExitDataSize The size, in bytes, of ExitData. Ignored if
1722 ExitStatus is EFI_SUCCESS.
1723 @param ExitData Pointer to a data buffer that includes a
1724 Null-terminated Unicode string, optionally
1725 followed by additional binary data. The string
1726 is a description that the caller may use to
1727 further indicate the reason for the image's
1728 exit.
1729
1730 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current
1731 image.
1732 @retval EFI_SUCCESS Successfully terminates the currently loaded
1733 EFI image.
1734 @retval EFI_ACCESS_DENIED Should never reach there.
1735 @retval EFI_OUT_OF_RESOURCES Could not allocate pool
1736
1737 **/
1738 EFI_STATUS
1739 EFIAPI
1740 CoreExit (
1741 IN EFI_HANDLE ImageHandle,
1742 IN EFI_STATUS Status,
1743 IN UINTN ExitDataSize,
1744 IN CHAR16 *ExitData OPTIONAL
1745 )
1746 {
1747 LOADED_IMAGE_PRIVATE_DATA *Image;
1748 EFI_TPL OldTpl;
1749
1750 //
1751 // Prevent possible reentrance to this function
1752 // for the same ImageHandle
1753 //
1754 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
1755
1756 Image = CoreLoadedImageInfo (ImageHandle);
1757 if (Image == NULL) {
1758 Status = EFI_INVALID_PARAMETER;
1759 goto Done;
1760 }
1761
1762 if (!Image->Started) {
1763 //
1764 // The image has not been started so just free its resources
1765 //
1766 CoreUnloadAndCloseImage (Image, TRUE);
1767 Status = EFI_SUCCESS;
1768 goto Done;
1769 }
1770
1771 //
1772 // Image has been started, verify this image can exit
1773 //
1774 if (Image != mCurrentImage) {
1775 DEBUG ((DEBUG_LOAD|DEBUG_ERROR, "Exit: Image is not exitable image\n"));
1776 Status = EFI_INVALID_PARAMETER;
1777 goto Done;
1778 }
1779
1780 //
1781 // Set status
1782 //
1783 Image->Status = Status;
1784
1785 //
1786 // If there's ExitData info, move it
1787 //
1788 if (ExitData != NULL) {
1789 Image->ExitDataSize = ExitDataSize;
1790 Image->ExitData = AllocatePool (Image->ExitDataSize);
1791 if (Image->ExitData == NULL) {
1792 Status = EFI_OUT_OF_RESOURCES;
1793 goto Done;
1794 }
1795 CopyMem (Image->ExitData, ExitData, Image->ExitDataSize);
1796 }
1797
1798 CoreRestoreTpl (OldTpl);
1799 //
1800 // return to StartImage
1801 //
1802 LongJump (Image->JumpContext, (UINTN)-1);
1803
1804 //
1805 // If we return from LongJump, then it is an error
1806 //
1807 ASSERT (FALSE);
1808 Status = EFI_ACCESS_DENIED;
1809 Done:
1810 CoreRestoreTpl (OldTpl);
1811 return Status;
1812 }
1813
1814
1815
1816
1817 /**
1818 Unloads an image.
1819
1820 @param ImageHandle Handle that identifies the image to be
1821 unloaded.
1822
1823 @retval EFI_SUCCESS The image has been unloaded.
1824 @retval EFI_UNSUPPORTED The image has been sarted, and does not support
1825 unload.
1826 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.
1827
1828 **/
1829 EFI_STATUS
1830 EFIAPI
1831 CoreUnloadImage (
1832 IN EFI_HANDLE ImageHandle
1833 )
1834 {
1835 EFI_STATUS Status;
1836 LOADED_IMAGE_PRIVATE_DATA *Image;
1837
1838 Image = CoreLoadedImageInfo (ImageHandle);
1839 if (Image == NULL ) {
1840 //
1841 // The image handle is not valid
1842 //
1843 Status = EFI_INVALID_PARAMETER;
1844 goto Done;
1845 }
1846
1847 if (Image->Started) {
1848 //
1849 // The image has been started, request it to unload.
1850 //
1851 Status = EFI_UNSUPPORTED;
1852 if (Image->Info.Unload != NULL) {
1853 Status = Image->Info.Unload (ImageHandle);
1854 }
1855
1856 } else {
1857 //
1858 // This Image hasn't been started, thus it can be unloaded
1859 //
1860 Status = EFI_SUCCESS;
1861 }
1862
1863
1864 if (!EFI_ERROR (Status)) {
1865 //
1866 // if the Image was not started or Unloaded O.K. then clean up
1867 //
1868 CoreUnloadAndCloseImage (Image, TRUE);
1869 }
1870
1871 Done:
1872 return Status;
1873 }
1874
1875
1876
1877 /**
1878 Unload the specified image.
1879
1880 @param This Indicates the calling context.
1881 @param ImageHandle The specified image handle.
1882
1883 @retval EFI_INVALID_PARAMETER Image handle is NULL.
1884 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.
1885 @retval EFI_SUCCESS Image successfully unloaded.
1886
1887 **/
1888 EFI_STATUS
1889 EFIAPI
1890 CoreUnloadImageEx (
1891 IN EFI_PE32_IMAGE_PROTOCOL *This,
1892 IN EFI_HANDLE ImageHandle
1893 )
1894 {
1895 return CoreUnloadImage (ImageHandle);
1896 }