878ddf1f |
1 | /*++\r |
2 | \r |
3 | Copyright (c) 2006, Intel Corporation\r |
4 | All rights reserved. This program and the accompanying materials\r |
5 | are licensed and made available under the terms and conditions of the BSD License\r |
6 | which accompanies this distribution. The full text of the license may be found at\r |
7 | http://opensource.org/licenses/bsd-license.php\r |
8 | \r |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
11 | \r |
12 | Module Name:\r |
13 | \r |
14 | Image.c\r |
15 | \r |
16 | Abstract:\r |
17 | \r |
18 | Core image handling services\r |
19 | \r |
20 | --*/\r |
21 | \r |
22 | #include <DxeMain.h>\r |
23 | //\r |
24 | // Module Globals\r |
25 | //\r |
26 | \r |
27 | //\r |
28 | // LIST of runtime images that need to be relocated.\r |
29 | //\r |
30 | LIST_ENTRY mRuntimeImageList = INITIALIZE_LIST_HEAD_VARIABLE (mRuntimeImageList);\r |
31 | \r |
32 | LOADED_IMAGE_PRIVATE_DATA *mCurrentImage = NULL;\r |
33 | \r |
34 | LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData = {\r |
35 | LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE,\r |
36 | NULL,\r |
37 | {\r |
38 | CoreLoadImageEx,\r |
39 | CoreUnloadImageEx\r |
40 | }\r |
41 | };\r |
42 | \r |
43 | \r |
44 | //\r |
45 | // This code is needed to build the Image handle for the DXE Core\r |
46 | //\r |
47 | LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = {\r |
48 | LOADED_IMAGE_PRIVATE_DATA_SIGNATURE, // Signature\r |
49 | NULL, // Image handle\r |
50 | EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER, // Image type\r |
51 | TRUE, // If entrypoint has been called\r |
52 | NULL, // EntryPoint\r |
53 | {\r |
54 | EFI_LOADED_IMAGE_INFORMATION_REVISION, // Revision\r |
55 | NULL, // Parent handle\r |
56 | NULL, // System handle\r |
57 | \r |
58 | NULL, // Device handle\r |
59 | NULL, // File path\r |
60 | NULL, // Reserved\r |
61 | \r |
62 | 0, // LoadOptionsSize\r |
63 | NULL, // LoadOptions\r |
64 | \r |
65 | NULL, // ImageBase\r |
66 | 0, // ImageSize\r |
67 | EfiBootServicesCode, // ImageCodeType\r |
68 | EfiBootServicesData // ImageDataType\r |
69 | },\r |
70 | (EFI_PHYSICAL_ADDRESS)0, // ImageBasePage\r |
71 | 0, // NumberOfPages\r |
72 | NULL, // FixupData\r |
73 | 0, // Tpl\r |
74 | EFI_SUCCESS, // Status\r |
75 | 0, // ExitDataSize\r |
76 | NULL, // ExitData\r |
77 | NULL, // JumpContext\r |
78 | 0, // Machine\r |
79 | NULL, // Ebc\r |
80 | FALSE, // RuntimeFixupValid\r |
81 | NULL, // RuntimeFixup\r |
82 | { NULL, NULL }, // Link\r |
83 | };\r |
84 | \r |
85 | \r |
86 | EFI_STATUS\r |
87 | CoreInitializeImageServices (\r |
88 | IN VOID *HobStart\r |
89 | )\r |
90 | /*++\r |
91 | \r |
92 | Routine Description:\r |
93 | \r |
94 | Add the Image Services to EFI Boot Services Table and install the protocol\r |
95 | interfaces for this image.\r |
96 | \r |
97 | Arguments:\r |
98 | \r |
99 | HobStart - The HOB to initialize\r |
100 | \r |
101 | Returns:\r |
102 | \r |
103 | Status code.\r |
104 | \r |
105 | --*/\r |
106 | {\r |
107 | EFI_STATUS Status;\r |
108 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
109 | EFI_PHYSICAL_ADDRESS DxeCoreImageBaseAddress;\r |
110 | UINT64 DxeCoreImageLength;\r |
111 | VOID *DxeCoreEntryPoint;\r |
112 | EFI_PEI_HOB_POINTERS DxeCoreHob;\r |
113 | //\r |
114 | // Searching for image hob\r |
115 | //\r |
116 | DxeCoreHob.Raw = HobStart;\r |
117 | while ((DxeCoreHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, DxeCoreHob.Raw)) != NULL) {\r |
118 | if (CompareGuid (&DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.Name, &gEfiHobMemoryAllocModuleGuid)) {\r |
119 | //\r |
120 | // Find Dxe Core HOB\r |
121 | //\r |
122 | break;\r |
123 | }\r |
124 | DxeCoreHob.Raw = GET_NEXT_HOB (DxeCoreHob);\r |
125 | }\r |
126 | ASSERT (DxeCoreHob.Raw != NULL);\r |
127 | \r |
128 | DxeCoreImageBaseAddress = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryBaseAddress;\r |
129 | DxeCoreImageLength = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength;\r |
130 | DxeCoreEntryPoint = (VOID *) (UINTN) DxeCoreHob.MemoryAllocationModule->EntryPoint;\r |
131 | gDxeCoreFileName = &DxeCoreHob.MemoryAllocationModule->ModuleName;\r |
132 | //\r |
133 | // Initialize the fields for an internal driver\r |
134 | //\r |
135 | Image = &mCorePrivateImage;\r |
136 | \r |
137 | Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)DxeCoreEntryPoint;\r |
138 | Image->ImageBasePage = DxeCoreImageBaseAddress;\r |
139 | Image->NumberOfPages = (UINTN)(EFI_SIZE_TO_PAGES((UINTN)(DxeCoreImageLength)));\r |
140 | Image->Tpl = gEfiCurrentTpl;\r |
141 | Image->Info.SystemTable = gST;\r |
142 | Image->Info.ImageBase = (VOID *)(UINTN)DxeCoreImageBaseAddress;\r |
143 | Image->Info.ImageSize = DxeCoreImageLength;\r |
144 | \r |
145 | //\r |
146 | // Install the protocol interfaces for this image\r |
147 | //\r |
148 | Status = CoreInstallProtocolInterface (\r |
149 | &Image->Handle,\r |
150 | &gEfiLoadedImageProtocolGuid,\r |
151 | EFI_NATIVE_INTERFACE,\r |
152 | &Image->Info\r |
153 | );\r |
154 | ASSERT_EFI_ERROR (Status);\r |
155 | \r |
156 | mCurrentImage = Image;\r |
157 | \r |
158 | //\r |
159 | // Fill in DXE globals\r |
160 | //\r |
161 | gDxeCoreImageHandle = Image->Handle;\r |
162 | gDxeCoreLoadedImage = &Image->Info;\r |
163 | \r |
164 | //\r |
165 | // Export DXE Core PE Loader functionality\r |
166 | //\r |
167 | return CoreInstallProtocolInterface (\r |
168 | &mLoadPe32PrivateData.Handle,\r |
169 | &gEfiLoadPeImageProtocolGuid,\r |
170 | EFI_NATIVE_INTERFACE,\r |
171 | &mLoadPe32PrivateData.Pe32Image\r |
172 | );\r |
173 | }\r |
174 | \r |
175 | \r |
176 | EFI_STATUS\r |
177 | CoreShutdownImageServices (\r |
178 | VOID\r |
179 | )\r |
180 | /*++\r |
181 | \r |
182 | Routine Description:\r |
183 | \r |
184 | Transfer control of runtime images to runtime service\r |
185 | \r |
186 | Arguments:\r |
187 | \r |
188 | None\r |
189 | \r |
190 | Returns:\r |
191 | \r |
192 | EFI_SUCCESS - Function successfully returned\r |
193 | \r |
194 | --*/\r |
195 | {\r |
196 | LIST_ENTRY *Link;\r |
197 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
198 | \r |
199 | //\r |
200 | // The Runtime AP is required for the core to function!\r |
201 | //\r |
202 | ASSERT (gRuntime != NULL);\r |
203 | \r |
204 | for (Link = mRuntimeImageList.ForwardLink; Link != &mRuntimeImageList; Link = Link->ForwardLink) {\r |
205 | Image = CR (Link, LOADED_IMAGE_PRIVATE_DATA, Link, LOADED_IMAGE_PRIVATE_DATA_SIGNATURE);\r |
206 | if (Image->RuntimeFixupValid) {\r |
207 | gRuntime->RegisterImage (\r |
208 | gRuntime,\r |
209 | (UINT64)(UINTN)(Image->Info.ImageBase),\r |
210 | (EFI_SIZE_TO_PAGES ((UINTN)Image->Info.ImageSize)),\r |
211 | Image->RuntimeFixup\r |
212 | );\r |
213 | }\r |
214 | }\r |
215 | \r |
216 | return EFI_SUCCESS;\r |
217 | }\r |
218 | \r |
219 | \r |
220 | \r |
221 | EFI_STATUS\r |
222 | CoreLoadPeImage (\r |
223 | IN VOID *Pe32Handle,\r |
224 | IN LOADED_IMAGE_PRIVATE_DATA *Image,\r |
225 | IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,\r |
226 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,\r |
227 | IN UINT32 Attribute\r |
228 | )\r |
229 | /*++\r |
230 | \r |
231 | Routine Description:\r |
232 | \r |
233 | Loads, relocates, and invokes a PE/COFF image\r |
234 | \r |
235 | Arguments:\r |
236 | \r |
237 | Pe32Handle - The handle of PE32 image\r |
238 | Image - PE image to be loaded\r |
239 | DstBuffer - The buffer to store the image\r |
240 | EntryPoint - A pointer to the entry point\r |
241 | Attribute - The bit mask of attributes to set for the load PE image\r |
242 | \r |
243 | Returns:\r |
244 | \r |
245 | EFI_SUCCESS - The file was loaded, relocated, and invoked\r |
246 | \r |
247 | EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r |
248 | \r |
249 | EFI_INVALID_PARAMETER - Invalid parameter\r |
250 | \r |
251 | EFI_BUFFER_TOO_SMALL - Buffer for image is too small\r |
252 | \r |
253 | --*/\r |
254 | {\r |
255 | EFI_STATUS Status;\r |
256 | UINTN Size;\r |
257 | \r |
258 | ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));\r |
259 | \r |
260 | Image->ImageContext.Handle = Pe32Handle;\r |
261 | Image->ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)CoreReadImageFile;\r |
262 | \r |
263 | //\r |
264 | // Get information about the image being loaded\r |
265 | //\r |
266 | Status = gEfiPeiPeCoffLoader->GetImageInfo (gEfiPeiPeCoffLoader, &Image->ImageContext);\r |
267 | if (EFI_ERROR (Status)) {\r |
268 | return Status;\r |
269 | }\r |
270 | \r |
2ce31132 |
271 | if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) {\r |
272 | //\r |
273 | // The PE/COFF loader can support loading image types that can be executed. \r |
274 | // If we loaded an image type that we can not execute return EFI_UNSUPORTED. \r |
275 | //\r |
276 | return EFI_UNSUPPORTED;\r |
277 | }\r |
278 | \r |
279 | \r |
878ddf1f |
280 | //\r |
281 | // Allocate memory of the correct memory type aligned on the required image boundry\r |
282 | //\r |
283 | \r |
284 | if (DstBuffer == 0) {\r |
285 | //\r |
286 | // Allocate Destination Buffer as caller did not pass it in\r |
287 | //\r |
288 | \r |
289 | if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r |
290 | Size = (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment;\r |
291 | } else {\r |
292 | Size = (UINTN)Image->ImageContext.ImageSize;\r |
293 | }\r |
294 | \r |
295 | Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size);\r |
296 | \r |
297 | //\r |
298 | // If the image relocations have not been stripped, then load at any address.\r |
299 | // Otherwise load at the address at which it was linked.\r |
300 | //\r |
301 | Status = CoreAllocatePages (\r |
302 | (Image->ImageContext.RelocationsStripped) ? AllocateAddress : AllocateAnyPages,\r |
303 | Image->ImageContext.ImageCodeMemoryType,\r |
304 | Image->NumberOfPages,\r |
305 | &Image->ImageContext.ImageAddress\r |
306 | );\r |
307 | if (EFI_ERROR (Status)) {\r |
308 | return Status;\r |
309 | }\r |
310 | \r |
311 | Image->ImageBasePage = Image->ImageContext.ImageAddress;\r |
312 | \r |
313 | } else {\r |
314 | //\r |
315 | // Caller provided the destination buffer\r |
316 | //\r |
317 | \r |
318 | if (Image->ImageContext.RelocationsStripped && (Image->ImageContext.ImageAddress != DstBuffer)) {\r |
319 | //\r |
320 | // If the image relocations were stripped, and the caller provided a\r |
321 | // destination buffer address that does not match the address that the\r |
322 | // image is linked at, then the image cannot be loaded.\r |
323 | //\r |
324 | return EFI_INVALID_PARAMETER;\r |
325 | }\r |
326 | \r |
327 | if (Image->NumberOfPages != 0 &&\r |
328 | Image->NumberOfPages <\r |
329 | (EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment))) {\r |
330 | Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);\r |
331 | return EFI_BUFFER_TOO_SMALL;\r |
332 | }\r |
333 | \r |
334 | Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);\r |
335 | Image->ImageContext.ImageAddress = DstBuffer;\r |
336 | Image->ImageBasePage = Image->ImageContext.ImageAddress;\r |
337 | }\r |
338 | \r |
339 | Image->ImageContext.ImageAddress =\r |
340 | (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &\r |
341 | ~((UINTN)Image->ImageContext.SectionAlignment - 1);\r |
342 | \r |
343 | //\r |
344 | // Load the image from the file into the allocated memory\r |
345 | //\r |
346 | Status = gEfiPeiPeCoffLoader->LoadImage (gEfiPeiPeCoffLoader, &Image->ImageContext);\r |
347 | if (EFI_ERROR (Status)) {\r |
348 | return Status;\r |
349 | }\r |
350 | \r |
351 | //\r |
352 | // If this is a Runtime Driver, then allocate memory for the FixupData that\r |
353 | // is used to relocate the image when SetVirtualAddressMap() is called. The\r |
354 | // relocation is done by the Runtime AP.\r |
355 | //\r |
356 | if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) {\r |
357 | if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {\r |
358 | Image->ImageContext.FixupData = CoreAllocateRuntimePool ((UINTN)(Image->ImageContext.FixupDataSize));\r |
359 | if (Image->ImageContext.FixupData == NULL) {\r |
360 | Status = EFI_OUT_OF_RESOURCES;\r |
361 | goto Done;\r |
362 | }\r |
363 | \r |
364 | //\r |
365 | // Make a list off all the RT images so we can let the RT AP know about them\r |
366 | //\r |
367 | Image->RuntimeFixupValid = TRUE;\r |
368 | Image->RuntimeFixup = Image->ImageContext.FixupData;\r |
369 | InsertTailList (&mRuntimeImageList, &Image->Link);\r |
370 | }\r |
371 | }\r |
372 | \r |
373 | //\r |
374 | // Relocate the image in memory\r |
375 | //\r |
376 | Status = gEfiPeiPeCoffLoader->RelocateImage (gEfiPeiPeCoffLoader, &Image->ImageContext);\r |
377 | if (EFI_ERROR (Status)) {\r |
378 | return Status;\r |
379 | }\r |
380 | \r |
381 | //\r |
382 | // Flush the Instruction Cache\r |
383 | //\r |
384 | InvalidateInstructionCacheRange ((VOID *)(UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.ImageSize);\r |
385 | \r |
386 | //\r |
387 | // Copy the machine type from the context to the image private data. This\r |
388 | // is needed during image unload to know if we should call an EBC protocol\r |
389 | // to unload the image.\r |
390 | //\r |
391 | Image->Machine = Image->ImageContext.Machine;\r |
392 | \r |
393 | //\r |
394 | // Get the image entry point. If it's an EBC image, then call into the\r |
395 | // interpreter to create a thunk for the entry point and use the returned\r |
396 | // value for the entry point.\r |
397 | //\r |
398 | Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)Image->ImageContext.EntryPoint;\r |
399 | if (Image->ImageContext.Machine == EFI_IMAGE_MACHINE_EBC) {\r |
400 | //\r |
401 | // Locate the EBC interpreter protocol\r |
402 | //\r |
403 | Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);\r |
404 | if (EFI_ERROR(Status)) {\r |
405 | goto Done;\r |
406 | }\r |
407 | \r |
408 | //\r |
409 | // Register a callback for flushing the instruction cache so that created\r |
410 | // thunks can be flushed.\r |
411 | //\r |
412 | Status = Image->Ebc->RegisterICacheFlush (Image->Ebc, (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange);\r |
413 | if (EFI_ERROR(Status)) {\r |
414 | goto Done;\r |
415 | }\r |
416 | \r |
417 | //\r |
418 | // Create a thunk for the image's entry point. This will be the new\r |
419 | // entry point for the image.\r |
420 | //\r |
421 | Status = Image->Ebc->CreateThunk (\r |
422 | Image->Ebc,\r |
423 | Image->Handle,\r |
424 | (VOID *)(UINTN)Image->ImageContext.EntryPoint,\r |
425 | (VOID **)&Image->EntryPoint\r |
426 | );\r |
427 | if (EFI_ERROR(Status)) {\r |
428 | goto Done;\r |
429 | }\r |
430 | }\r |
431 | \r |
432 | //\r |
433 | // Fill in the image information for the Loaded Image Protocol\r |
434 | //\r |
435 | Image->Type = Image->ImageContext.ImageType;\r |
436 | Image->Info.ImageBase = (VOID *)(UINTN)Image->ImageContext.ImageAddress;\r |
437 | Image->Info.ImageSize = Image->ImageContext.ImageSize;\r |
438 | Image->Info.ImageCodeType = Image->ImageContext.ImageCodeMemoryType;\r |
439 | Image->Info.ImageDataType = Image->ImageContext.ImageDataMemoryType;\r |
440 | \r |
441 | //\r |
442 | // Fill in the entry point of the image if it is available\r |
443 | //\r |
444 | if (EntryPoint != NULL) {\r |
445 | *EntryPoint = Image->ImageContext.EntryPoint;\r |
446 | }\r |
447 | \r |
448 | //\r |
449 | // Print the load address and the PDB file name if it is available\r |
450 | //\r |
451 | \r |
2ce31132 |
452 | DEBUG_CODE_BEGIN ();\r |
453 | \r |
878ddf1f |
454 | UINTN Index;\r |
455 | UINTN StartIndex;\r |
456 | CHAR8 EfiFileName[256];\r |
457 | \r |
458 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading driver at 0x%08x EntryPoint=0x%08x ", (UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.EntryPoint));\r |
459 | if (Image->ImageContext.PdbPointer != NULL) {\r |
460 | StartIndex = 0;\r |
461 | for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) {\r |
462 | if (Image->ImageContext.PdbPointer[Index] == '\\') {\r |
463 | StartIndex = Index + 1;\r |
464 | }\r |
465 | }\r |
466 | //\r |
467 | // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r |
468 | //\r |
469 | for (Index = 0; Index < sizeof (EfiFileName); Index++) {\r |
470 | EfiFileName[Index] = Image->ImageContext.PdbPointer[Index + StartIndex];\r |
471 | if (EfiFileName[Index] == 0) {\r |
472 | EfiFileName[Index] = '.';\r |
473 | }\r |
474 | if (EfiFileName[Index] == '.') {\r |
475 | EfiFileName[Index + 1] = 'e';\r |
476 | EfiFileName[Index + 2] = 'f';\r |
477 | EfiFileName[Index + 3] = 'i';\r |
478 | EfiFileName[Index + 4] = 0;\r |
479 | break;\r |
480 | }\r |
481 | }\r |
482 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r |
483 | }\r |
484 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r |
2ce31132 |
485 | \r |
486 | DEBUG_CODE_END ();\r |
878ddf1f |
487 | \r |
488 | return EFI_SUCCESS;\r |
489 | \r |
490 | Done:\r |
491 | //\r |
492 | // Free memory\r |
493 | //\r |
494 | CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);\r |
495 | return Status;\r |
496 | }\r |
497 | \r |
498 | \r |
499 | LOADED_IMAGE_PRIVATE_DATA *\r |
500 | CoreLoadedImageInfo (\r |
501 | IN EFI_HANDLE ImageHandle\r |
502 | )\r |
503 | /*++\r |
504 | \r |
505 | Routine Description:\r |
506 | \r |
507 | Get the image's private data from its handle.\r |
508 | \r |
509 | Arguments:\r |
510 | \r |
511 | ImageHandle - The image handle\r |
512 | \r |
513 | Returns:\r |
514 | \r |
515 | Return the image private data associated with ImageHandle.\r |
516 | \r |
517 | --*/\r |
518 | {\r |
519 | EFI_STATUS Status;\r |
520 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r |
521 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
522 | \r |
523 | Status = CoreHandleProtocol (\r |
524 | ImageHandle,\r |
525 | &gEfiLoadedImageProtocolGuid,\r |
526 | (VOID **)&LoadedImage\r |
527 | );\r |
528 | if (!EFI_ERROR (Status)) {\r |
529 | Image = LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage);\r |
530 | } else {\r |
531 | DEBUG ((EFI_D_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %x\n", ImageHandle));\r |
532 | Image = NULL;\r |
533 | }\r |
534 | \r |
535 | return Image;\r |
536 | }\r |
537 | \r |
538 | \r |
539 | EFI_STATUS\r |
540 | CoreLoadImageCommon (\r |
541 | IN BOOLEAN BootPolicy,\r |
542 | IN EFI_HANDLE ParentImageHandle,\r |
543 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r |
544 | IN VOID *SourceBuffer OPTIONAL,\r |
545 | IN UINTN SourceSize,\r |
546 | IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,\r |
547 | IN OUT UINTN *NumberOfPages OPTIONAL,\r |
548 | OUT EFI_HANDLE *ImageHandle,\r |
549 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,\r |
550 | IN UINT32 Attribute\r |
551 | )\r |
552 | /*++\r |
553 | \r |
554 | Routine Description:\r |
555 | \r |
556 | Loads an EFI image into memory and returns a handle to the image.\r |
557 | \r |
558 | Arguments:\r |
559 | \r |
560 | BootPolicy - If TRUE, indicates that the request originates from the boot manager,\r |
561 | and that the boot manager is attempting to load FilePath as a boot selection.\r |
562 | ParentImageHandle - The caller's image handle.\r |
563 | FilePath - The specific file path from which the image is loaded.\r |
564 | SourceBuffer - If not NULL, a pointer to the memory location containing a copy of\r |
565 | the image to be loaded.\r |
566 | SourceSize - The size in bytes of SourceBuffer.\r |
567 | DstBuffer - The buffer to store the image\r |
568 | NumberOfPages - If not NULL, a pointer to the image's page number, if this number\r |
569 | is not enough, return EFI_BUFFER_TOO_SMALL and this parameter contain\r |
570 | the required number.\r |
571 | ImageHandle - Pointer to the returned image handle that is created when the image\r |
572 | is successfully loaded.\r |
573 | EntryPoint - A pointer to the entry point\r |
574 | Attribute - The bit mask of attributes to set for the load PE image\r |
575 | \r |
576 | Returns:\r |
577 | \r |
578 | EFI_SUCCESS - The image was loaded into memory.\r |
579 | EFI_NOT_FOUND - The FilePath was not found.\r |
580 | EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r |
581 | EFI_BUFFER_TOO_SMALL - The buffer is too small\r |
582 | EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be\r |
583 | parsed to locate the proper protocol for loading the file.\r |
584 | EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.\r |
585 | --*/\r |
586 | {\r |
587 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
588 | LOADED_IMAGE_PRIVATE_DATA *ParentImage;\r |
589 | IMAGE_FILE_HANDLE FHand;\r |
590 | EFI_STATUS Status;\r |
591 | EFI_STATUS SecurityStatus;\r |
592 | EFI_HANDLE DeviceHandle;\r |
593 | UINT32 AuthenticationStatus;\r |
594 | EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;\r |
595 | EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;\r |
596 | UINTN FilePathSize;\r |
597 | \r |
c91eaa3d |
598 | SecurityStatus = EFI_SUCCESS;\r |
878ddf1f |
599 | \r |
600 | ASSERT (gEfiCurrentTpl < EFI_TPL_NOTIFY);\r |
601 | ParentImage = NULL;\r |
602 | \r |
603 | //\r |
604 | // The caller must pass in a valid ParentImageHandle\r |
605 | //\r |
606 | if (ImageHandle == NULL || ParentImageHandle == NULL) {\r |
607 | return EFI_INVALID_PARAMETER;\r |
608 | }\r |
609 | \r |
610 | ParentImage = CoreLoadedImageInfo (ParentImageHandle);\r |
611 | if (ParentImage == NULL) {\r |
612 | DEBUG((EFI_D_LOAD|EFI_D_ERROR, "LoadImageEx: Parent handle not an image handle\n"));\r |
613 | return EFI_INVALID_PARAMETER;\r |
614 | }\r |
615 | \r |
616 | //\r |
617 | // Get simple read access to the source file\r |
618 | //\r |
619 | OriginalFilePath = FilePath;\r |
620 | Status = CoreOpenImageFile (\r |
621 | BootPolicy,\r |
622 | SourceBuffer,\r |
623 | SourceSize,\r |
624 | FilePath,\r |
625 | &DeviceHandle,\r |
626 | &FHand,\r |
627 | &AuthenticationStatus\r |
628 | );\r |
629 | if (Status == EFI_ALREADY_STARTED) {\r |
630 | Image = NULL;\r |
631 | goto Done;\r |
632 | } else if (EFI_ERROR (Status)) {\r |
633 | return Status;\r |
634 | }\r |
635 | \r |
636 | //\r |
637 | // Verify the Authentication Status through the Security Architectural Protocol\r |
638 | //\r |
639 | if ((gSecurity != NULL) && (OriginalFilePath != NULL)) {\r |
640 | SecurityStatus = gSecurity->FileAuthenticationState (\r |
641 | gSecurity,\r |
642 | AuthenticationStatus,\r |
643 | OriginalFilePath\r |
644 | );\r |
645 | if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {\r |
646 | Status = SecurityStatus;\r |
647 | Image = NULL;\r |
648 | goto Done;\r |
649 | }\r |
650 | }\r |
651 | \r |
652 | \r |
653 | //\r |
654 | // Allocate a new image structure\r |
655 | //\r |
656 | Image = CoreAllocateZeroBootServicesPool (sizeof(LOADED_IMAGE_PRIVATE_DATA));\r |
657 | if (Image == NULL) {\r |
658 | return EFI_OUT_OF_RESOURCES;\r |
659 | }\r |
660 | \r |
661 | //\r |
662 | // Pull out just the file portion of the DevicePath for the LoadedImage FilePath\r |
663 | //\r |
664 | Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r |
665 | if (!EFI_ERROR (Status)) {\r |
666 | FilePathSize = CoreDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r |
667 | FilePath = (EFI_DEVICE_PATH_PROTOCOL *) ( ((UINT8 *)FilePath) + FilePathSize );\r |
668 | }\r |
669 | \r |
670 | //\r |
671 | // Initialize the fields for an internal driver\r |
672 | //\r |
673 | Image->Signature = LOADED_IMAGE_PRIVATE_DATA_SIGNATURE;\r |
674 | Image->Info.SystemTable = gST;\r |
675 | Image->Info.DeviceHandle = DeviceHandle;\r |
676 | Image->Info.Revision = EFI_LOADED_IMAGE_INFORMATION_REVISION;\r |
677 | Image->Info.FilePath = CoreDuplicateDevicePath (FilePath);\r |
678 | Image->Info.ParentHandle = ParentImageHandle;\r |
679 | \r |
680 | if (NumberOfPages != NULL) {\r |
681 | Image->NumberOfPages = *NumberOfPages ;\r |
682 | } else {\r |
683 | Image->NumberOfPages = 0 ;\r |
684 | }\r |
685 | \r |
686 | //\r |
687 | // Install the protocol interfaces for this image\r |
688 | // don't fire notifications yet\r |
689 | //\r |
690 | Status = CoreInstallProtocolInterfaceNotify (\r |
691 | &Image->Handle,\r |
692 | &gEfiLoadedImageProtocolGuid,\r |
693 | EFI_NATIVE_INTERFACE,\r |
694 | &Image->Info,\r |
695 | FALSE\r |
696 | );\r |
697 | if (EFI_ERROR (Status)) {\r |
698 | goto Done;\r |
699 | }\r |
700 | \r |
701 | //\r |
702 | // Load the image. If EntryPoint is Null, it will not be set.\r |
703 | //\r |
704 | Status = CoreLoadPeImage (&FHand, Image, DstBuffer, EntryPoint, Attribute);\r |
705 | if (EFI_ERROR (Status)) {\r |
706 | if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_OUT_OF_RESOURCES)) {\r |
707 | if (NumberOfPages != NULL) {\r |
708 | *NumberOfPages = Image->NumberOfPages;\r |
709 | }\r |
710 | }\r |
711 | goto Done;\r |
712 | }\r |
713 | \r |
714 | //\r |
715 | // Register the image in the Debug Image Info Table if the attribute is set\r |
716 | //\r |
717 | if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) {\r |
718 | CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL, &Image->Info, Image->Handle);\r |
719 | }\r |
720 | \r |
721 | //\r |
722 | //Reinstall loaded image protocol to fire any notifications\r |
723 | //\r |
724 | Status = CoreReinstallProtocolInterface (\r |
725 | Image->Handle,\r |
726 | &gEfiLoadedImageProtocolGuid,\r |
727 | &Image->Info,\r |
728 | &Image->Info\r |
729 | );\r |
730 | if (EFI_ERROR (Status)) {\r |
731 | goto Done;\r |
732 | }\r |
733 | \r |
734 | \r |
735 | //\r |
736 | // Success. Return the image handle\r |
737 | //\r |
738 | *ImageHandle = Image->Handle;\r |
739 | \r |
740 | Done:\r |
741 | //\r |
742 | // All done accessing the source file\r |
743 | // If we allocated the Source buffer, free it\r |
744 | //\r |
745 | if (FHand.FreeBuffer) {\r |
746 | CoreFreePool (FHand.Source);\r |
747 | }\r |
748 | \r |
749 | //\r |
750 | // There was an error. If there's an Image structure, free it\r |
751 | //\r |
752 | if (EFI_ERROR (Status)) {\r |
753 | if (Image != NULL) {\r |
754 | CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer == 0));\r |
755 | *ImageHandle = NULL;\r |
756 | }\r |
c91eaa3d |
757 | } else if (EFI_ERROR (SecurityStatus)) {\r |
758 | Status = SecurityStatus;\r |
878ddf1f |
759 | }\r |
760 | \r |
761 | return Status;\r |
762 | }\r |
763 | \r |
764 | \r |
765 | \r |
766 | EFI_STATUS\r |
767 | EFIAPI\r |
768 | CoreLoadImage (\r |
769 | IN BOOLEAN BootPolicy,\r |
770 | IN EFI_HANDLE ParentImageHandle,\r |
771 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r |
772 | IN VOID *SourceBuffer OPTIONAL,\r |
773 | IN UINTN SourceSize,\r |
774 | OUT EFI_HANDLE *ImageHandle\r |
775 | )\r |
776 | /*++\r |
777 | \r |
778 | Routine Description:\r |
779 | \r |
780 | Loads an EFI image into memory and returns a handle to the image.\r |
781 | \r |
782 | Arguments:\r |
783 | \r |
784 | BootPolicy - If TRUE, indicates that the request originates from the boot manager,\r |
785 | and that the boot manager is attempting to load FilePath as a boot selection.\r |
786 | ParentImageHandle - The caller's image handle.\r |
787 | FilePath - The specific file path from which the image is loaded.\r |
788 | SourceBuffer - If not NULL, a pointer to the memory location containing a copy of\r |
789 | the image to be loaded.\r |
790 | SourceSize - The size in bytes of SourceBuffer.\r |
791 | ImageHandle - Pointer to the returned image handle that is created when the image\r |
792 | is successfully loaded.\r |
793 | \r |
794 | Returns:\r |
795 | \r |
796 | EFI_SUCCESS - The image was loaded into memory.\r |
797 | EFI_NOT_FOUND - The FilePath was not found.\r |
798 | EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r |
799 | EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be\r |
800 | parsed to locate the proper protocol for loading the file.\r |
801 | EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.\r |
802 | --*/\r |
803 | {\r |
804 | EFI_STATUS Status;\r |
805 | \r |
806 | PERF_START (NULL, "LoadImage", NULL, 0);\r |
807 | \r |
808 | Status = CoreLoadImageCommon (\r |
809 | BootPolicy,\r |
810 | ParentImageHandle,\r |
811 | FilePath,\r |
812 | SourceBuffer,\r |
813 | SourceSize,\r |
814 | (EFI_PHYSICAL_ADDRESS)NULL,\r |
815 | NULL,\r |
816 | ImageHandle,\r |
817 | NULL,\r |
818 | EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION\r |
819 | );\r |
820 | \r |
821 | PERF_END (NULL, "LoadImage", NULL, 0);\r |
822 | \r |
823 | return Status;\r |
824 | }\r |
825 | \r |
826 | \r |
827 | EFI_STATUS\r |
828 | EFIAPI\r |
829 | CoreLoadImageEx (\r |
830 | IN EFI_PE32_IMAGE_PROTOCOL *This,\r |
831 | IN EFI_HANDLE ParentImageHandle,\r |
832 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r |
833 | IN VOID *SourceBuffer OPTIONAL,\r |
834 | IN UINTN SourceSize,\r |
835 | IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,\r |
836 | OUT UINTN *NumberOfPages OPTIONAL,\r |
837 | OUT EFI_HANDLE *ImageHandle,\r |
838 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint OPTIONAL,\r |
839 | IN UINT32 Attribute\r |
840 | )\r |
841 | /*++\r |
842 | \r |
843 | Routine Description:\r |
844 | \r |
845 | Loads an EFI image into memory and returns a handle to the image with extended parameters.\r |
846 | \r |
847 | Arguments:\r |
848 | \r |
849 | This - Calling context\r |
850 | ParentImageHandle - The caller's image handle.\r |
851 | FilePath - The specific file path from which the image is loaded.\r |
852 | SourceBuffer - If not NULL, a pointer to the memory location containing a copy of\r |
853 | the image to be loaded.\r |
854 | SourceSize - The size in bytes of SourceBuffer.\r |
855 | DstBuffer - The buffer to store the image.\r |
856 | NumberOfPages - For input, specifies the space size of the image by caller if not NULL.\r |
857 | For output, specifies the actual space size needed.\r |
858 | ImageHandle - Image handle for output.\r |
859 | EntryPoint - Image entry point for output.\r |
860 | Attribute - The bit mask of attributes to set for the load PE image.\r |
861 | \r |
862 | Returns:\r |
863 | \r |
864 | EFI_SUCCESS - The image was loaded into memory.\r |
865 | EFI_NOT_FOUND - The FilePath was not found.\r |
866 | EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r |
867 | EFI_UNSUPPORTED - The image type is not supported, or the device path cannot be\r |
868 | parsed to locate the proper protocol for loading the file.\r |
869 | EFI_OUT_OF_RESOURCES - Image was not loaded due to insufficient resources.\r |
870 | --*/\r |
871 | {\r |
872 | return CoreLoadImageCommon (\r |
873 | TRUE,\r |
874 | ParentImageHandle,\r |
875 | FilePath,\r |
876 | SourceBuffer,\r |
877 | SourceSize,\r |
878 | DstBuffer,\r |
879 | NumberOfPages,\r |
880 | ImageHandle,\r |
881 | EntryPoint,\r |
882 | Attribute\r |
883 | );\r |
884 | }\r |
885 | \r |
886 | \r |
887 | \r |
888 | \r |
889 | EFI_STATUS\r |
890 | EFIAPI\r |
891 | CoreStartImage (\r |
892 | IN EFI_HANDLE ImageHandle,\r |
893 | OUT UINTN *ExitDataSize,\r |
894 | OUT CHAR16 **ExitData OPTIONAL\r |
895 | )\r |
896 | /*++\r |
897 | \r |
898 | Routine Description:\r |
899 | \r |
900 | Transfer control to a loaded image's entry point.\r |
901 | \r |
902 | Arguments:\r |
903 | \r |
904 | ImageHandle - Handle of image to be started.\r |
905 | \r |
906 | ExitDataSize - Pointer of the size to ExitData\r |
907 | \r |
908 | ExitData - Pointer to a pointer to a data buffer that includes a Null-terminated\r |
909 | Unicode string, optionally followed by additional binary data. The string\r |
910 | is a description that the caller may use to further indicate the reason for\r |
511710d6 |
911 | the image's exit.\r |
878ddf1f |
912 | \r |
913 | Returns:\r |
914 | \r |
915 | EFI_INVALID_PARAMETER - Invalid parameter\r |
916 | \r |
917 | EFI_OUT_OF_RESOURCES - No enough buffer to allocate\r |
918 | \r |
919 | EFI_SUCCESS - Successfully transfer control to the image's entry point.\r |
920 | \r |
921 | --*/\r |
922 | {\r |
923 | EFI_STATUS Status;\r |
924 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
925 | LOADED_IMAGE_PRIVATE_DATA *LastImage;\r |
926 | UINT64 HandleDatabaseKey;\r |
927 | UINTN SetJumpFlag;\r |
928 | \r |
929 | Image = CoreLoadedImageInfo (ImageHandle);\r |
930 | if (Image == NULL_HANDLE || Image->Started) {\r |
931 | return EFI_INVALID_PARAMETER;\r |
932 | }\r |
933 | \r |
934 | //\r |
935 | // Don't profile Objects or invalid start requests\r |
936 | //\r |
937 | PERF_START (ImageHandle, START_IMAGE_TOK, NULL, 0);\r |
938 | \r |
878ddf1f |
939 | \r |
940 | //\r |
941 | // Push the current start image context, and\r |
942 | // link the current image to the head. This is the\r |
943 | // only image that can call Exit()\r |
944 | //\r |
2ce31132 |
945 | HandleDatabaseKey = CoreGetHandleDatabaseKey ();\r |
878ddf1f |
946 | LastImage = mCurrentImage;\r |
947 | mCurrentImage = Image;\r |
948 | Image->Tpl = gEfiCurrentTpl;\r |
949 | \r |
950 | //\r |
951 | // Set long jump for Exit() support\r |
952 | //\r |
953 | Image->JumpContext = CoreAllocateBootServicesPool (sizeof (*Image->JumpContext));\r |
954 | if (Image->JumpContext == NULL) {\r |
955 | PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);\r |
956 | return EFI_OUT_OF_RESOURCES;\r |
957 | }\r |
958 | \r |
959 | SetJumpFlag = SetJump (Image->JumpContext);\r |
960 | //\r |
961 | // The initial call to SetJump() must always return 0. \r |
962 | // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump(). \r |
963 | //\r |
964 | if (!SetJumpFlag) {\r |
965 | //\r |
966 | // Call the image's entry point\r |
967 | //\r |
968 | Image->Started = TRUE;\r |
969 | Image->Status = Image->EntryPoint (ImageHandle, Image->Info.SystemTable);\r |
970 | \r |
971 | //\r |
972 | // Add some debug information if the image returned with error.\r |
973 | // This make the user aware and check if the driver image have already released\r |
974 | // all the resource in this situation.\r |
975 | //\r |
2ce31132 |
976 | DEBUG_CODE_BEGIN ();\r |
878ddf1f |
977 | if (EFI_ERROR (Image->Status)) {\r |
978 | DEBUG ((EFI_D_ERROR, "Error: Image at %08X start failed: %x\n", Image->Info.ImageBase, Image->Status));\r |
979 | }\r |
2ce31132 |
980 | DEBUG_CODE_END ();\r |
878ddf1f |
981 | \r |
982 | //\r |
983 | // If the image returns, exit it through Exit()\r |
984 | //\r |
985 | CoreExit (ImageHandle, Image->Status, 0, NULL);\r |
986 | }\r |
987 | \r |
988 | //\r |
989 | // Image has completed. Verify the tpl is the same\r |
990 | //\r |
991 | ASSERT (Image->Tpl == gEfiCurrentTpl);\r |
992 | CoreRestoreTpl (Image->Tpl);\r |
993 | \r |
994 | CoreFreePool (Image->JumpContext);\r |
995 | \r |
996 | //\r |
997 | // Pop the current start image context\r |
998 | //\r |
999 | mCurrentImage = LastImage;\r |
1000 | \r |
1001 | //\r |
1002 | // Go connect any handles that were created or modified while the image executed.\r |
1003 | //\r |
1004 | CoreConnectHandlesByKey (HandleDatabaseKey);\r |
1005 | \r |
1006 | //\r |
1007 | // Handle the image's returned ExitData\r |
1008 | //\r |
2ce31132 |
1009 | DEBUG_CODE_BEGIN ();\r |
878ddf1f |
1010 | if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {\r |
1011 | \r |
1012 | DEBUG (\r |
1013 | (EFI_D_LOAD,\r |
1014 | "StartImage: ExitDataSize %d, ExitData %x",\r |
1015 | Image->ExitDataSize,\r |
1016 | Image->ExitData)\r |
1017 | );\r |
1018 | if (Image->ExitData != NULL) {\r |
1019 | DEBUG ((EFI_D_LOAD, " (%hs)", Image->ExitData));\r |
1020 | }\r |
1021 | DEBUG ((EFI_D_LOAD, "\n"));\r |
1022 | }\r |
2ce31132 |
1023 | DEBUG_CODE_END ();\r |
878ddf1f |
1024 | \r |
1025 | //\r |
1026 | // Return the exit data to the caller\r |
1027 | //\r |
1028 | if (ExitData != NULL && ExitDataSize != NULL) {\r |
1029 | *ExitDataSize = Image->ExitDataSize;\r |
1030 | *ExitData = Image->ExitData;\r |
1031 | } else {\r |
1032 | //\r |
1033 | // Caller doesn't want the exit data, free it\r |
1034 | //\r |
1035 | CoreFreePool (Image->ExitData);\r |
1036 | Image->ExitData = NULL;\r |
1037 | }\r |
1038 | \r |
1039 | //\r |
1040 | // Save the Status because Image will get destroyed if it is unloaded.\r |
1041 | //\r |
1042 | Status = Image->Status;\r |
1043 | \r |
1044 | //\r |
1045 | // If the image returned an error, or if the image is an application\r |
1046 | // unload it\r |
1047 | //\r |
1048 | if (EFI_ERROR (Image->Status) || Image->Type == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r |
1049 | CoreUnloadAndCloseImage (Image, TRUE);\r |
1050 | }\r |
1051 | \r |
1052 | //\r |
1053 | // Done\r |
1054 | //\r |
1055 | PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);\r |
1056 | return Status;\r |
1057 | }\r |
1058 | \r |
1059 | \r |
1060 | VOID\r |
1061 | CoreUnloadAndCloseImage (\r |
1062 | IN LOADED_IMAGE_PRIVATE_DATA *Image,\r |
1063 | IN BOOLEAN FreePage\r |
1064 | )\r |
1065 | /*++\r |
1066 | \r |
1067 | Routine Description:\r |
1068 | \r |
1069 | Unloads EFI image from memory.\r |
1070 | \r |
1071 | Arguments:\r |
1072 | \r |
1073 | Image - EFI image\r |
1074 | FreePage - Free allocated pages\r |
1075 | \r |
1076 | Returns:\r |
1077 | \r |
1078 | None\r |
1079 | \r |
1080 | --*/\r |
1081 | {\r |
1082 | EFI_STATUS Status;\r |
1083 | UINTN HandleCount;\r |
1084 | EFI_HANDLE *HandleBuffer;\r |
1085 | UINTN HandleIndex;\r |
1086 | EFI_GUID **ProtocolGuidArray;\r |
1087 | UINTN ArrayCount;\r |
1088 | UINTN ProtocolIndex;\r |
1089 | EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;\r |
1090 | UINTN OpenInfoCount;\r |
1091 | UINTN OpenInfoIndex;\r |
1092 | \r |
1093 | if (Image->Ebc != NULL) {\r |
1094 | //\r |
1095 | // If EBC protocol exists we must perform cleanups for this image.\r |
1096 | //\r |
1097 | Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);\r |
1098 | }\r |
1099 | \r |
1100 | //\r |
1101 | // Unload image, free Image->ImageContext->ModHandle\r |
1102 | //\r |
1103 | gEfiPeiPeCoffLoader->UnloadImage (gEfiPeiPeCoffLoader, &Image->ImageContext);\r |
1104 | \r |
1105 | //\r |
1106 | // Free our references to the image handle\r |
1107 | //\r |
1108 | if (Image->Handle != NULL_HANDLE) {\r |
1109 | \r |
1110 | Status = CoreLocateHandleBuffer (\r |
1111 | AllHandles,\r |
1112 | NULL,\r |
1113 | NULL,\r |
1114 | &HandleCount,\r |
1115 | &HandleBuffer\r |
1116 | );\r |
1117 | if (!EFI_ERROR (Status)) {\r |
1118 | for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r |
1119 | Status = CoreProtocolsPerHandle (\r |
1120 | HandleBuffer[HandleIndex],\r |
1121 | &ProtocolGuidArray,\r |
1122 | &ArrayCount\r |
1123 | );\r |
1124 | if (!EFI_ERROR (Status)) {\r |
1125 | for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {\r |
1126 | Status = CoreOpenProtocolInformation (\r |
1127 | HandleBuffer[HandleIndex],\r |
1128 | ProtocolGuidArray[ProtocolIndex],\r |
1129 | &OpenInfo,\r |
1130 | &OpenInfoCount\r |
1131 | );\r |
1132 | if (!EFI_ERROR (Status)) {\r |
1133 | for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {\r |
1134 | if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {\r |
1135 | Status = CoreCloseProtocol (\r |
1136 | HandleBuffer[HandleIndex],\r |
1137 | ProtocolGuidArray[ProtocolIndex],\r |
1138 | Image->Handle,\r |
1139 | OpenInfo[OpenInfoIndex].ControllerHandle\r |
1140 | );\r |
1141 | }\r |
1142 | }\r |
1143 | if (OpenInfo != NULL) {\r |
1144 | CoreFreePool(OpenInfo);\r |
1145 | }\r |
1146 | }\r |
1147 | }\r |
1148 | if (ProtocolGuidArray != NULL) {\r |
1149 | CoreFreePool(ProtocolGuidArray);\r |
1150 | }\r |
1151 | }\r |
1152 | }\r |
1153 | if (HandleBuffer != NULL) {\r |
1154 | CoreFreePool (HandleBuffer);\r |
1155 | }\r |
1156 | }\r |
1157 | \r |
1158 | CoreRemoveDebugImageInfoEntry (Image->Handle);\r |
1159 | \r |
1160 | Status = CoreUninstallProtocolInterface (\r |
1161 | Image->Handle,\r |
1162 | &gEfiLoadedImageProtocolGuid,\r |
1163 | &Image->Info\r |
1164 | );\r |
1165 | }\r |
1166 | \r |
1167 | if (Image->RuntimeFixupValid) {\r |
1168 | //\r |
1169 | // Remove the Image from the Runtime Image list as we are about to Free it!\r |
1170 | //\r |
1171 | RemoveEntryList (&Image->Link);\r |
1172 | }\r |
1173 | \r |
1174 | //\r |
1175 | // Free the Image from memory\r |
1176 | //\r |
1177 | if ((Image->ImageBasePage != 0) && FreePage) {\r |
1178 | CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);\r |
1179 | }\r |
1180 | \r |
1181 | //\r |
1182 | // Done with the Image structure\r |
1183 | //\r |
1184 | if (Image->Info.FilePath != NULL) {\r |
1185 | CoreFreePool (Image->Info.FilePath);\r |
1186 | }\r |
1187 | \r |
1188 | if (Image->FixupData != NULL) {\r |
1189 | CoreFreePool (Image->FixupData);\r |
1190 | }\r |
1191 | \r |
1192 | CoreFreePool (Image);\r |
1193 | }\r |
1194 | \r |
1195 | \r |
1196 | \r |
1197 | EFI_STATUS\r |
1198 | EFIAPI\r |
1199 | CoreExit (\r |
1200 | IN EFI_HANDLE ImageHandle,\r |
1201 | IN EFI_STATUS Status,\r |
1202 | IN UINTN ExitDataSize,\r |
1203 | IN CHAR16 *ExitData OPTIONAL\r |
1204 | )\r |
1205 | /*++\r |
1206 | \r |
1207 | Routine Description:\r |
1208 | \r |
1209 | Terminates the currently loaded EFI image and returns control to boot services.\r |
1210 | \r |
1211 | Arguments:\r |
1212 | \r |
1213 | ImageHandle - Handle that identifies the image. This parameter is passed to the image\r |
1214 | on entry.\r |
511710d6 |
1215 | Status - The image's exit code.\r |
878ddf1f |
1216 | ExitDataSize - The size, in bytes, of ExitData. Ignored if ExitStatus is\r |
1217 | EFI_SUCCESS.\r |
1218 | ExitData - Pointer to a data buffer that includes a Null-terminated Unicode string,\r |
1219 | optionally followed by additional binary data. The string is a\r |
1220 | description that the caller may use to further indicate the reason for\r |
511710d6 |
1221 | the image's exit.\r |
878ddf1f |
1222 | \r |
1223 | Returns:\r |
1224 | \r |
1225 | EFI_INVALID_PARAMETER - Image handle is NULL or it is not current image.\r |
1226 | \r |
1227 | EFI_SUCCESS - Successfully terminates the currently loaded EFI image.\r |
1228 | \r |
1229 | EFI_ACCESS_DENIED - Should never reach there.\r |
1230 | \r |
1231 | EFI_OUT_OF_RESOURCES - Could not allocate pool\r |
1232 | \r |
1233 | --*/\r |
1234 | {\r |
1235 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
1236 | \r |
1237 | Image = CoreLoadedImageInfo (ImageHandle);\r |
1238 | if (Image == NULL_HANDLE) {\r |
1239 | return EFI_INVALID_PARAMETER;\r |
1240 | }\r |
1241 | \r |
1242 | if (!Image->Started) {\r |
1243 | //\r |
1244 | // The image has not been started so just free its resources\r |
1245 | //\r |
1246 | CoreUnloadAndCloseImage (Image, TRUE);\r |
1247 | return EFI_SUCCESS;\r |
1248 | }\r |
1249 | \r |
1250 | //\r |
1251 | // Image has been started, verify this image can exit\r |
1252 | //\r |
1253 | if (Image != mCurrentImage) {\r |
1254 | DEBUG ((EFI_D_LOAD|EFI_D_ERROR, "Exit: Image is not exitable image\n"));\r |
1255 | return EFI_INVALID_PARAMETER;\r |
1256 | }\r |
1257 | \r |
1258 | //\r |
1259 | // Set status\r |
1260 | //\r |
1261 | Image->Status = Status;\r |
1262 | \r |
1263 | //\r |
1264 | // If there's ExitData info, move it\r |
1265 | //\r |
1266 | if (ExitData != NULL) {\r |
1267 | Image->ExitDataSize = ExitDataSize;\r |
1268 | Image->ExitData = CoreAllocateBootServicesPool (Image->ExitDataSize);\r |
1269 | if (Image->ExitData == NULL) {\r |
1270 | return EFI_OUT_OF_RESOURCES;\r |
1271 | }\r |
1272 | CopyMem (Image->ExitData, ExitData, Image->ExitDataSize);\r |
1273 | }\r |
1274 | \r |
1275 | //\r |
1276 | // return to StartImage\r |
1277 | //\r |
1278 | LongJump (Image->JumpContext, (UINTN)-1);\r |
1279 | \r |
1280 | //\r |
1281 | // If we return from LongJump, then it is an error\r |
1282 | //\r |
1283 | ASSERT (FALSE);\r |
1284 | return EFI_ACCESS_DENIED;\r |
1285 | }\r |
1286 | \r |
1287 | \r |
1288 | \r |
1289 | EFI_STATUS\r |
1290 | EFIAPI\r |
1291 | CoreUnloadImage (\r |
1292 | IN EFI_HANDLE ImageHandle\r |
1293 | )\r |
1294 | /*++\r |
1295 | \r |
1296 | Routine Description:\r |
1297 | \r |
1298 | Unloads an image.\r |
1299 | \r |
1300 | Arguments:\r |
1301 | \r |
1302 | ImageHandle - Handle that identifies the image to be unloaded.\r |
1303 | \r |
1304 | Returns:\r |
1305 | \r |
1306 | EFI_SUCCESS - The image has been unloaded.\r |
1307 | EFI_UNSUPPORTED - The image has been sarted, and does not support unload.\r |
1308 | EFI_INVALID_PARAMPETER - ImageHandle is not a valid image handle.\r |
1309 | \r |
1310 | --*/\r |
1311 | {\r |
1312 | EFI_STATUS Status;\r |
1313 | LOADED_IMAGE_PRIVATE_DATA *Image;\r |
1314 | \r |
1315 | Image = CoreLoadedImageInfo (ImageHandle);\r |
1316 | if (Image == NULL ) {\r |
1317 | //\r |
1318 | // The image handle is not valid\r |
1319 | //\r |
1320 | return EFI_INVALID_PARAMETER;\r |
1321 | }\r |
1322 | \r |
1323 | if (Image->Started) {\r |
1324 | //\r |
1325 | // The image has been started, request it to unload.\r |
1326 | //\r |
1327 | Status = EFI_UNSUPPORTED;\r |
1328 | if (Image->Info.Unload != NULL) {\r |
1329 | Status = Image->Info.Unload (ImageHandle);\r |
1330 | }\r |
1331 | \r |
1332 | } else {\r |
1333 | //\r |
1334 | // This Image hasn't been started, thus it can be unloaded\r |
1335 | //\r |
1336 | Status = EFI_SUCCESS;\r |
1337 | }\r |
1338 | \r |
1339 | \r |
1340 | if (!EFI_ERROR (Status)) {\r |
1341 | //\r |
1342 | // if the Image was not started or Unloaded O.K. then clean up\r |
1343 | //\r |
1344 | CoreUnloadAndCloseImage (Image, TRUE);\r |
1345 | }\r |
1346 | \r |
1347 | return Status;\r |
1348 | }\r |
1349 | \r |
1350 | \r |
1351 | EFI_STATUS\r |
1352 | EFIAPI\r |
1353 | CoreUnloadImageEx (\r |
1354 | IN EFI_PE32_IMAGE_PROTOCOL *This,\r |
1355 | IN EFI_HANDLE ImageHandle\r |
1356 | )\r |
1357 | /*++\r |
1358 | \r |
1359 | Routine Description:\r |
1360 | \r |
1361 | Unload the specified image.\r |
1362 | \r |
1363 | Arguments:\r |
1364 | \r |
1365 | This - Indicates the calling context.\r |
1366 | \r |
1367 | ImageHandle - The specified image handle.\r |
1368 | \r |
1369 | Returns:\r |
1370 | \r |
1371 | EFI_INVALID_PARAMETER - Image handle is NULL.\r |
1372 | \r |
1373 | EFI_UNSUPPORTED - Attempt to unload an unsupported image.\r |
1374 | \r |
1375 | EFI_SUCCESS - Image successfully unloaded.\r |
1376 | \r |
1377 | --*/\r |
1378 | {\r |
1379 | return CoreUnloadImage (ImageHandle);\r |
1380 | }\r |