]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Image/Image.c
EdkCompatibilityPkg MpServicesOnFrameworkMpServicesThunk: Check for x2APIC mode befor...
[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
31222776
RN
1089 if (SecurityStatus == EFI_ACCESS_DENIED) {\r
1090 //\r
1091 // Image was not loaded because the platform policy prohibits the image from being loaded.\r
1092 // It's the only place we could meet EFI_ACCESS_DENIED.\r
1093 //\r
1094 *ImageHandle = NULL;\r
1095 }\r
28a00297 1096 Status = SecurityStatus;\r
1097 Image = NULL;\r
1098 goto Done;\r
1099 }\r
1100 }\r
1101\r
1102\r
1103 //\r
1104 // Allocate a new image structure\r
1105 //\r
9c4ac31c 1106 Image = AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA));\r
28a00297 1107 if (Image == NULL) {\r
1108 return EFI_OUT_OF_RESOURCES;\r
1109 }\r
1110\r
1111 //\r
1112 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath\r
1113 //\r
cfe9de52 1114 FilePath = OriginalFilePath;\r
d2fbaaab 1115 if (DeviceHandle != NULL) {\r
1116 Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
1117 if (!EFI_ERROR (Status)) {\r
1118 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
1119 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
1120 }\r
28a00297 1121 }\r
28a00297 1122 //\r
1123 // Initialize the fields for an internal driver\r
1124 //\r
1125 Image->Signature = LOADED_IMAGE_PRIVATE_DATA_SIGNATURE;\r
1126 Image->Info.SystemTable = gDxeCoreST;\r
1127 Image->Info.DeviceHandle = DeviceHandle;\r
162ed594 1128 Image->Info.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
9c4ac31c 1129 Image->Info.FilePath = DuplicateDevicePath (FilePath);\r
28a00297 1130 Image->Info.ParentHandle = ParentImageHandle;\r
1131\r
85658066 1132\r
28a00297 1133 if (NumberOfPages != NULL) {\r
1134 Image->NumberOfPages = *NumberOfPages ;\r
1135 } else {\r
1136 Image->NumberOfPages = 0 ;\r
1137 }\r
1138\r
1139 //\r
1140 // Install the protocol interfaces for this image\r
1141 // don't fire notifications yet\r
1142 //\r
1143 Status = CoreInstallProtocolInterfaceNotify (\r
1144 &Image->Handle,\r
1145 &gEfiLoadedImageProtocolGuid,\r
1146 EFI_NATIVE_INTERFACE,\r
1147 &Image->Info,\r
1148 FALSE\r
1149 );\r
1150 if (EFI_ERROR (Status)) {\r
1151 goto Done;\r
1152 }\r
1153\r
1154 //\r
1155 // Load the image. If EntryPoint is Null, it will not be set.\r
1156 //\r
822360ee 1157 Status = CoreLoadPeImage (BootPolicy, &FHand, Image, DstBuffer, EntryPoint, Attribute);\r
28a00297 1158 if (EFI_ERROR (Status)) {\r
1159 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_OUT_OF_RESOURCES)) {\r
1160 if (NumberOfPages != NULL) {\r
1161 *NumberOfPages = Image->NumberOfPages;\r
1162 }\r
1163 }\r
1164 goto Done;\r
1165 }\r
1166\r
152af594 1167 if (NumberOfPages != NULL) {\r
1168 *NumberOfPages = Image->NumberOfPages;\r
57d6f36d 1169 }\r
152af594 1170\r
28a00297 1171 //\r
1172 // Register the image in the Debug Image Info Table if the attribute is set\r
1173 //\r
71f68914 1174 if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) != 0) {\r
28a00297 1175 CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL, &Image->Info, Image->Handle);\r
1176 }\r
1177\r
1178 //\r
1179 //Reinstall loaded image protocol to fire any notifications\r
1180 //\r
1181 Status = CoreReinstallProtocolInterface (\r
1182 Image->Handle,\r
1183 &gEfiLoadedImageProtocolGuid,\r
1184 &Image->Info,\r
1185 &Image->Info\r
1186 );\r
1187 if (EFI_ERROR (Status)) {\r
1188 goto Done;\r
1189 }\r
1190\r
ba39e316 1191 //\r
1192 // If DevicePath parameter to the LoadImage() is not NULL, then make a copy of DevicePath,\r
1193 // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer.\r
1194 //\r
1195 if (OriginalFilePath != NULL) {\r
9c4ac31c 1196 Image->LoadedImageDevicePath = DuplicateDevicePath (OriginalFilePath);\r
ba39e316 1197 }\r
1198\r
1199 //\r
1200 // Install Loaded Image Device Path Protocol onto the image handle of a PE/COFE image\r
1201 //\r
1202 Status = CoreInstallProtocolInterface (\r
1203 &Image->Handle,\r
1204 &gEfiLoadedImageDevicePathProtocolGuid,\r
1205 EFI_NATIVE_INTERFACE,\r
1206 Image->LoadedImageDevicePath\r
1207 );\r
1208 if (EFI_ERROR (Status)) {\r
1209 goto Done;\r
1210 }\r
28a00297 1211\r
7547649f 1212 //\r
1213 // Install HII Package List Protocol onto the image handle\r
1214 //\r
1215 if (Image->ImageContext.HiiResourceData != 0) {\r
1216 Status = CoreInstallProtocolInterface (\r
1217 &Image->Handle,\r
1218 &gEfiHiiPackageListProtocolGuid,\r
1219 EFI_NATIVE_INTERFACE,\r
1220 (VOID *) (UINTN) Image->ImageContext.HiiResourceData\r
1221 );\r
1222 if (EFI_ERROR (Status)) {\r
1223 goto Done;\r
1224 }\r
1225 }\r
1226\r
28a00297 1227 //\r
1228 // Success. Return the image handle\r
1229 //\r
1230 *ImageHandle = Image->Handle;\r
1231\r
1232Done:\r
1233 //\r
1234 // All done accessing the source file\r
1235 // If we allocated the Source buffer, free it\r
1236 //\r
1237 if (FHand.FreeBuffer) {\r
1238 CoreFreePool (FHand.Source);\r
1239 }\r
1240\r
1241 //\r
1242 // There was an error. If there's an Image structure, free it\r
1243 //\r
1244 if (EFI_ERROR (Status)) {\r
1245 if (Image != NULL) {\r
1246 CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer == 0));\r
28a00297 1247 }\r
1248 } else if (EFI_ERROR (SecurityStatus)) {\r
1249 Status = SecurityStatus;\r
1250 }\r
1251\r
1252 return Status;\r
1253}\r
1254\r
1255\r
1256\r
162ed594 1257\r
1258/**\r
1259 Loads an EFI image into memory and returns a handle to the image.\r
1260\r
57d6f36d 1261 @param BootPolicy If TRUE, indicates that the request originates\r
1262 from the boot manager, and that the boot\r
1263 manager is attempting to load FilePath as a\r
1264 boot selection.\r
1265 @param ParentImageHandle The caller's image handle.\r
1266 @param FilePath The specific file path from which the image is\r
1267 loaded.\r
1268 @param SourceBuffer If not NULL, a pointer to the memory location\r
1269 containing a copy of the image to be loaded.\r
1270 @param SourceSize The size in bytes of SourceBuffer.\r
1271 @param ImageHandle Pointer to the returned image handle that is\r
1272 created when the image is successfully loaded.\r
1273\r
1274 @retval EFI_SUCCESS The image was loaded into memory.\r
1275 @retval EFI_NOT_FOUND The FilePath was not found.\r
1276 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1277 @retval EFI_UNSUPPORTED The image type is not supported, or the device\r
1278 path cannot be parsed to locate the proper\r
1279 protocol for loading the file.\r
1280 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient\r
162ed594 1281 resources.\r
b695e7ff
LG
1282 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not\r
1283 understood.\r
1284 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.\r
1285 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the \r
1286 image from being loaded. NULL is returned in *ImageHandle.\r
1287 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a \r
1288 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current \r
1289 platform policy specifies that the image should not be started.\r
162ed594 1290\r
1291**/\r
28a00297 1292EFI_STATUS\r
1293EFIAPI\r
1294CoreLoadImage (\r
1295 IN BOOLEAN BootPolicy,\r
1296 IN EFI_HANDLE ParentImageHandle,\r
1297 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
1298 IN VOID *SourceBuffer OPTIONAL,\r
1299 IN UINTN SourceSize,\r
1300 OUT EFI_HANDLE *ImageHandle\r
1301 )\r
28a00297 1302{\r
1303 EFI_STATUS Status;\r
7cff25d6 1304 UINT64 Tick;\r
28a00297 1305\r
7cff25d6 1306 Tick = 0;\r
1307 PERF_CODE (\r
1308 Tick = GetPerformanceCounter ();\r
1309 );\r
28a00297 1310\r
1311 Status = CoreLoadImageCommon (\r
1312 BootPolicy,\r
1313 ParentImageHandle,\r
1314 FilePath,\r
1315 SourceBuffer,\r
1316 SourceSize,\r
1be0dda6 1317 (EFI_PHYSICAL_ADDRESS) (UINTN) NULL,\r
28a00297 1318 NULL,\r
1319 ImageHandle,\r
1320 NULL,\r
1321 EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION\r
1322 );\r
1323\r
bc6b5892 1324 PERF_START (*ImageHandle, "LoadImage:", NULL, Tick);\r
1325 PERF_END (*ImageHandle, "LoadImage:", NULL, 0);\r
28a00297 1326\r
1327 return Status;\r
1328}\r
1329\r
1330\r
023c0fec 1331\r
1332/**\r
1333 Loads an EFI image into memory and returns a handle to the image with extended parameters.\r
1334\r
1335 @param This Calling context\r
1336 @param ParentImageHandle The caller's image handle.\r
1337 @param FilePath The specific file path from which the image is\r
1338 loaded.\r
1339 @param SourceBuffer If not NULL, a pointer to the memory location\r
1340 containing a copy of the image to be loaded.\r
1341 @param SourceSize The size in bytes of SourceBuffer.\r
1342 @param DstBuffer The buffer to store the image.\r
1343 @param NumberOfPages For input, specifies the space size of the\r
1344 image by caller if not NULL. For output,\r
1345 specifies the actual space size needed.\r
1346 @param ImageHandle Image handle for output.\r
1347 @param EntryPoint Image entry point for output.\r
1348 @param Attribute The bit mask of attributes to set for the load\r
1349 PE image.\r
1350\r
1351 @retval EFI_SUCCESS The image was loaded into memory.\r
1352 @retval EFI_NOT_FOUND The FilePath was not found.\r
1353 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1354 @retval EFI_UNSUPPORTED The image type is not supported, or the device\r
1355 path cannot be parsed to locate the proper\r
1356 protocol for loading the file.\r
1357 @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient\r
1358 resources.\r
b695e7ff
LG
1359 @retval EFI_LOAD_ERROR Image was not loaded because the image format was corrupt or not\r
1360 understood.\r
1361 @retval EFI_DEVICE_ERROR Image was not loaded because the device returned a read error.\r
1362 @retval EFI_ACCESS_DENIED Image was not loaded because the platform policy prohibits the \r
1363 image from being loaded. NULL is returned in *ImageHandle.\r
1364 @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle was created with a \r
1365 valid EFI_LOADED_IMAGE_PROTOCOL. However, the current \r
1366 platform policy specifies that the image should not be started.\r
023c0fec 1367\r
1368**/\r
1369EFI_STATUS\r
1370EFIAPI\r
1371CoreLoadImageEx (\r
1372 IN EFI_PE32_IMAGE_PROTOCOL *This,\r
1373 IN EFI_HANDLE ParentImageHandle,\r
1374 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
1375 IN VOID *SourceBuffer OPTIONAL,\r
1376 IN UINTN SourceSize,\r
1377 IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,\r
1378 OUT UINTN *NumberOfPages OPTIONAL,\r
1379 OUT EFI_HANDLE *ImageHandle,\r
1380 OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,\r
1381 IN UINT32 Attribute\r
1382 )\r
1383{\r
1384 return CoreLoadImageCommon (\r
1385 TRUE,\r
1386 ParentImageHandle,\r
1387 FilePath,\r
1388 SourceBuffer,\r
1389 SourceSize,\r
1390 DstBuffer,\r
1391 NumberOfPages,\r
1392 ImageHandle,\r
1393 EntryPoint,\r
1394 Attribute\r
1395 );\r
1396}\r
1397\r
1398\r
162ed594 1399/**\r
1400 Transfer control to a loaded image's entry point.\r
1401\r
57d6f36d 1402 @param ImageHandle Handle of image to be started.\r
1403 @param ExitDataSize Pointer of the size to ExitData\r
1404 @param ExitData Pointer to a pointer to a data buffer that\r
b695e7ff 1405 includes a Null-terminated string,\r
57d6f36d 1406 optionally followed by additional binary data.\r
1407 The string is a description that the caller may\r
1408 use to further indicate the reason for the\r
1409 image's exit.\r
1410\r
1411 @retval EFI_INVALID_PARAMETER Invalid parameter\r
1412 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
1413 @retval EFI_SUCCESS Successfully transfer control to the image's\r
162ed594 1414 entry point.\r
1415\r
1416**/\r
28a00297 1417EFI_STATUS\r
1418EFIAPI\r
1419CoreStartImage (\r
1420 IN EFI_HANDLE ImageHandle,\r
1421 OUT UINTN *ExitDataSize,\r
1422 OUT CHAR16 **ExitData OPTIONAL\r
1423 )\r
28a00297 1424{\r
1425 EFI_STATUS Status;\r
1426 LOADED_IMAGE_PRIVATE_DATA *Image;\r
1427 LOADED_IMAGE_PRIVATE_DATA *LastImage;\r
1428 UINT64 HandleDatabaseKey;\r
1429 UINTN SetJumpFlag;\r
1430\r
1431 Image = CoreLoadedImageInfo (ImageHandle);\r
4008328a 1432 if (Image == NULL || Image->Started) {\r
28a00297 1433 return EFI_INVALID_PARAMETER;\r
1434 }\r
1435\r
db0b7ad5
LG
1436 //\r
1437 // The image to be started must have the machine type supported by DxeCore.\r
1438 //\r
1439 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine));\r
919df8e6 1440 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) {\r
919df8e6
LG
1441 return EFI_UNSUPPORTED;\r
1442 }\r
1443\r
28a00297 1444 //\r
1445 // Don't profile Objects or invalid start requests\r
1446 //\r
bc6b5892 1447 PERF_START (ImageHandle, "StartImage:", NULL, 0);\r
28a00297 1448\r
1449\r
1450 //\r
1451 // Push the current start image context, and\r
1452 // link the current image to the head. This is the\r
1453 // only image that can call Exit()\r
1454 //\r
1455 HandleDatabaseKey = CoreGetHandleDatabaseKey ();\r
1456 LastImage = mCurrentImage;\r
1457 mCurrentImage = Image;\r
1458 Image->Tpl = gEfiCurrentTpl;\r
1459\r
1460 //\r
1461 // Set long jump for Exit() support\r
1462 // JumpContext must be aligned on a CPU specific boundary.\r
1463 // Overallocate the buffer and force the required alignment\r
1464 //\r
9c4ac31c 1465 Image->JumpBuffer = AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
28a00297 1466 if (Image->JumpBuffer == NULL) {\r
bc6b5892 1467 PERF_END (ImageHandle, "StartImage:", NULL, 0);\r
28a00297 1468 return EFI_OUT_OF_RESOURCES;\r
1469 }\r
1470 Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
1471\r
1472 SetJumpFlag = SetJump (Image->JumpContext);\r
1473 //\r
1474 // The initial call to SetJump() must always return 0.\r
1475 // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().\r
1476 //\r
71f68914 1477 if (SetJumpFlag == 0) {\r
28a00297 1478 //\r
1479 // Call the image's entry point\r
1480 //\r
1481 Image->Started = TRUE;\r
1482 Image->Status = Image->EntryPoint (ImageHandle, Image->Info.SystemTable);\r
1483\r
1484 //\r
1485 // Add some debug information if the image returned with error.\r
1486 // This make the user aware and check if the driver image have already released\r
1487 // all the resource in this situation.\r
1488 //\r
1489 DEBUG_CODE_BEGIN ();\r
1490 if (EFI_ERROR (Image->Status)) {\r
91136124 1491 DEBUG ((DEBUG_ERROR, "Error: Image at %11p start failed: %r\n", Image->Info.ImageBase, Image->Status));\r
28a00297 1492 }\r
1493 DEBUG_CODE_END ();\r
1494\r
1495 //\r
1496 // If the image returns, exit it through Exit()\r
1497 //\r
1498 CoreExit (ImageHandle, Image->Status, 0, NULL);\r
1499 }\r
1500\r
1501 //\r
1502 // Image has completed. Verify the tpl is the same\r
1503 //\r
1504 ASSERT (Image->Tpl == gEfiCurrentTpl);\r
1505 CoreRestoreTpl (Image->Tpl);\r
1506\r
1507 CoreFreePool (Image->JumpBuffer);\r
1508\r
1509 //\r
1510 // Pop the current start image context\r
1511 //\r
1512 mCurrentImage = LastImage;\r
1513\r
1514 //\r
1515 // Go connect any handles that were created or modified while the image executed.\r
1516 //\r
1517 CoreConnectHandlesByKey (HandleDatabaseKey);\r
1518\r
1519 //\r
1520 // Handle the image's returned ExitData\r
1521 //\r
1522 DEBUG_CODE_BEGIN ();\r
1523 if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {\r
1524\r
7df7393f 1525 DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %p", (UINT32)Image->ExitDataSize, Image->ExitData));\r
28a00297 1526 if (Image->ExitData != NULL) {\r
162ed594 1527 DEBUG ((DEBUG_LOAD, " (%hs)", Image->ExitData));\r
28a00297 1528 }\r
162ed594 1529 DEBUG ((DEBUG_LOAD, "\n"));\r
28a00297 1530 }\r
1531 DEBUG_CODE_END ();\r
1532\r
1533 //\r
1534 // Return the exit data to the caller\r
1535 //\r
1536 if (ExitData != NULL && ExitDataSize != NULL) {\r
1537 *ExitDataSize = Image->ExitDataSize;\r
1538 *ExitData = Image->ExitData;\r
1539 } else {\r
1540 //\r
1541 // Caller doesn't want the exit data, free it\r
1542 //\r
1543 CoreFreePool (Image->ExitData);\r
1544 Image->ExitData = NULL;\r
1545 }\r
1546\r
1547 //\r
1548 // Save the Status because Image will get destroyed if it is unloaded.\r
1549 //\r
1550 Status = Image->Status;\r
1551\r
1552 //\r
1553 // If the image returned an error, or if the image is an application\r
1554 // unload it\r
1555 //\r
1556 if (EFI_ERROR (Image->Status) || Image->Type == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1557 CoreUnloadAndCloseImage (Image, TRUE);\r
1558 }\r
1559\r
1560 //\r
1561 // Done\r
1562 //\r
bc6b5892 1563 PERF_END (ImageHandle, "StartImage:", NULL, 0);\r
28a00297 1564 return Status;\r
1565}\r
1566\r
162ed594 1567/**\r
1568 Terminates the currently loaded EFI image and returns control to boot services.\r
1569\r
57d6f36d 1570 @param ImageHandle Handle that identifies the image. This\r
1571 parameter is passed to the image on entry.\r
1572 @param Status The image's exit code.\r
1573 @param ExitDataSize The size, in bytes, of ExitData. Ignored if\r
1574 ExitStatus is EFI_SUCCESS.\r
1575 @param ExitData Pointer to a data buffer that includes a\r
1576 Null-terminated Unicode string, optionally\r
1577 followed by additional binary data. The string\r
1578 is a description that the caller may use to\r
1579 further indicate the reason for the image's\r
1580 exit.\r
1581\r
1582 @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current\r
1583 image.\r
1584 @retval EFI_SUCCESS Successfully terminates the currently loaded\r
1585 EFI image.\r
1586 @retval EFI_ACCESS_DENIED Should never reach there.\r
162ed594 1587 @retval EFI_OUT_OF_RESOURCES Could not allocate pool\r
1588\r
1589**/\r
28a00297 1590EFI_STATUS\r
1591EFIAPI\r
1592CoreExit (\r
1593 IN EFI_HANDLE ImageHandle,\r
1594 IN EFI_STATUS Status,\r
1595 IN UINTN ExitDataSize,\r
1596 IN CHAR16 *ExitData OPTIONAL\r
1597 )\r
28a00297 1598{\r
1599 LOADED_IMAGE_PRIVATE_DATA *Image;\r
1600 EFI_TPL OldTpl;\r
1601\r
1602 //\r
1603 // Prevent possible reentrance to this function\r
1604 // for the same ImageHandle\r
57d6f36d 1605 //\r
1606 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1607\r
28a00297 1608 Image = CoreLoadedImageInfo (ImageHandle);\r
4008328a 1609 if (Image == NULL) {\r
28a00297 1610 Status = EFI_INVALID_PARAMETER;\r
1611 goto Done;\r
1612 }\r
1613\r
1614 if (!Image->Started) {\r
1615 //\r
1616 // The image has not been started so just free its resources\r
1617 //\r
1618 CoreUnloadAndCloseImage (Image, TRUE);\r
1619 Status = EFI_SUCCESS;\r
1620 goto Done;\r
1621 }\r
1622\r
1623 //\r
1624 // Image has been started, verify this image can exit\r
1625 //\r
1626 if (Image != mCurrentImage) {\r
162ed594 1627 DEBUG ((DEBUG_LOAD|DEBUG_ERROR, "Exit: Image is not exitable image\n"));\r
28a00297 1628 Status = EFI_INVALID_PARAMETER;\r
1629 goto Done;\r
1630 }\r
1631\r
1632 //\r
1633 // Set status\r
1634 //\r
1635 Image->Status = Status;\r
1636\r
1637 //\r
1638 // If there's ExitData info, move it\r
1639 //\r
1640 if (ExitData != NULL) {\r
1641 Image->ExitDataSize = ExitDataSize;\r
9c4ac31c 1642 Image->ExitData = AllocatePool (Image->ExitDataSize);\r
28a00297 1643 if (Image->ExitData == NULL) {\r
1644 Status = EFI_OUT_OF_RESOURCES;\r
1645 goto Done;\r
1646 }\r
1647 CopyMem (Image->ExitData, ExitData, Image->ExitDataSize);\r
1648 }\r
1649\r
1650 CoreRestoreTpl (OldTpl);\r
1651 //\r
1652 // return to StartImage\r
1653 //\r
1654 LongJump (Image->JumpContext, (UINTN)-1);\r
1655\r
1656 //\r
1657 // If we return from LongJump, then it is an error\r
1658 //\r
1659 ASSERT (FALSE);\r
1660 Status = EFI_ACCESS_DENIED;\r
1661Done:\r
1662 CoreRestoreTpl (OldTpl);\r
1663 return Status;\r
1664}\r
1665\r
1666\r
1667\r
28a00297 1668\r
162ed594 1669/**\r
28a00297 1670 Unloads an image.\r
1671\r
57d6f36d 1672 @param ImageHandle Handle that identifies the image to be\r
1673 unloaded.\r
28a00297 1674\r
57d6f36d 1675 @retval EFI_SUCCESS The image has been unloaded.\r
1676 @retval EFI_UNSUPPORTED The image has been sarted, and does not support\r
1677 unload.\r
162ed594 1678 @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.\r
28a00297 1679\r
162ed594 1680**/\r
1681EFI_STATUS\r
1682EFIAPI\r
1683CoreUnloadImage (\r
1684 IN EFI_HANDLE ImageHandle\r
1685 )\r
28a00297 1686{\r
1687 EFI_STATUS Status;\r
1688 LOADED_IMAGE_PRIVATE_DATA *Image;\r
28a00297 1689\r
28a00297 1690 Image = CoreLoadedImageInfo (ImageHandle);\r
1691 if (Image == NULL ) {\r
1692 //\r
1693 // The image handle is not valid\r
1694 //\r
1695 Status = EFI_INVALID_PARAMETER;\r
1696 goto Done;\r
1697 }\r
1698\r
1699 if (Image->Started) {\r
1700 //\r
1701 // The image has been started, request it to unload.\r
1702 //\r
1703 Status = EFI_UNSUPPORTED;\r
1704 if (Image->Info.Unload != NULL) {\r
1705 Status = Image->Info.Unload (ImageHandle);\r
1706 }\r
1707\r
1708 } else {\r
1709 //\r
1710 // This Image hasn't been started, thus it can be unloaded\r
1711 //\r
1712 Status = EFI_SUCCESS;\r
1713 }\r
1714\r
1715\r
1716 if (!EFI_ERROR (Status)) {\r
1717 //\r
1718 // if the Image was not started or Unloaded O.K. then clean up\r
1719 //\r
1720 CoreUnloadAndCloseImage (Image, TRUE);\r
1721 }\r
1722\r
1723Done:\r
28a00297 1724 return Status;\r
1725}\r
1726\r
023c0fec 1727\r
1728\r
1729/**\r
1730 Unload the specified image.\r
1731\r
1732 @param This Indicates the calling context.\r
1733 @param ImageHandle The specified image handle.\r
1734\r
1735 @retval EFI_INVALID_PARAMETER Image handle is NULL.\r
1736 @retval EFI_UNSUPPORTED Attempt to unload an unsupported image.\r
1737 @retval EFI_SUCCESS Image successfully unloaded.\r
1738\r
1739**/\r
1740EFI_STATUS\r
1741EFIAPI\r
1742CoreUnloadImageEx (\r
1743 IN EFI_PE32_IMAGE_PROTOCOL *This,\r
1744 IN EFI_HANDLE ImageHandle\r
1745 )\r
1746{\r
1747 return CoreUnloadImage (ImageHandle);\r
1748}\r