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