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