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