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