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