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