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