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