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