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