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