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