]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Image/Image.c
add UsbMouseSimulateTouchPad driver to verify the correction of dispatching AbsoluteP...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
... / ...
CommitLineData
1/*++\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 Image.c\r
15\r
16Abstract:\r
17\r
18 Pei Core Load Image Support\r
19\r
20--*/\r
21\r
22#include <PeiMain.h>\r
23\r
24/*++\r
25\r
26Routine Description:\r
27\r
28 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
29\r
30Arguments:\r
31\r
32 FileHandle - The handle to the PE/COFF file\r
33 FileOffset - The offset, in bytes, into the file to read\r
34 ReadSize - The number of bytes to read from the file starting at FileOffset\r
35 Buffer - A pointer to the buffer to read the data into.\r
36\r
37Returns:\r
38\r
39 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
40\r
41--*/ \r
42\r
43EFI_STATUS\r
44PeiLoadImageLoadImage (\r
45 IN EFI_PEI_SERVICES **PeiServices,\r
46 IN EFI_PEI_FILE_HANDLE FileHandle,\r
47 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
48 OUT UINT64 *ImageSizeArg, OPTIONAL\r
49 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
50 OUT UINT32 *AuthenticationState\r
51 )\r
52/*++\r
53\r
54Routine Description:\r
55\r
56 Routine for loading file image.\r
57\r
58Arguments:\r
59\r
60 PeiServices - The PEI core services table.\r
61 FileHandle - Pointer to the FFS file header of the image.\r
62 ImageAddressArg - Pointer to PE/TE image.\r
63 ImageSizeArg - Size of PE/TE image.\r
64 EntryPoint - Pointer to entry point of specified image file for output.\r
65 AuthenticationState - Pointer to attestation authentication state of image.\r
66\r
67Returns:\r
68\r
69 Status - EFI_SUCCESS - Image is successfully loaded.\r
70 EFI_NOT_FOUND - Fail to locate necessary PPI\r
71 Others - Fail to load file.\r
72\r
73--*/\r
74;\r
75\r
76EFI_STATUS\r
77EFIAPI\r
78PeiLoadImageLoadImageWrapper (\r
79 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
80 IN EFI_PEI_FILE_HANDLE FileHandle,\r
81 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
82 OUT UINT64 *ImageSizeArg, OPTIONAL\r
83 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
84 OUT UINT32 *AuthenticationState\r
85 )\r
86/*++\r
87\r
88Routine Description:\r
89\r
90 The wrapper function of PeiLoadImageLoadImage().\r
91\r
92Arguments:\r
93\r
94 This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
95 PeiServices - The PEI core services table.\r
96 FileHandle - Pointer to the FFS file header of the image.\r
97 ImageAddressArg - Pointer to PE/TE image.\r
98 ImageSizeArg - Size of PE/TE image.\r
99 EntryPoint - Pointer to entry point of specified image file for output.\r
100 AuthenticationState - Pointer to attestation authentication state of image.\r
101\r
102Returns:\r
103\r
104 EFI_STATUS.\r
105 \r
106--*/ \r
107;\r
108\r
109STATIC EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {\r
110 PeiLoadImageLoadImageWrapper\r
111};\r
112\r
113\r
114STATIC EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {\r
115 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
116 &gEfiPeiLoadFilePpiGuid,\r
117 &mPeiLoadImagePpi\r
118};\r
119\r
120EFI_STATUS\r
121EFIAPI\r
122PeiImageRead (\r
123 IN VOID *FileHandle,\r
124 IN UINTN FileOffset,\r
125 IN OUT UINTN *ReadSize,\r
126 OUT VOID *Buffer\r
127 )\r
128/*++\r
129\r
130Routine Description:\r
131\r
132 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
133\r
134Arguments:\r
135\r
136 FileHandle - The handle to the PE/COFF file\r
137 FileOffset - The offset, in bytes, into the file to read\r
138 ReadSize - The number of bytes to read from the file starting at FileOffset\r
139 Buffer - A pointer to the buffer to read the data into.\r
140\r
141Returns:\r
142\r
143 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
144\r
145--*/\r
146{\r
147 CHAR8 *Destination8;\r
148 CHAR8 *Source8;\r
149 UINTN Length;\r
150\r
151 Destination8 = Buffer;\r
152 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
153 Length = *ReadSize;\r
154 while (Length--) {\r
155 *(Destination8++) = *(Source8++);\r
156 }\r
157\r
158 return EFI_SUCCESS;\r
159}\r
160\r
161EFI_STATUS\r
162GetImageReadFunction (\r
163 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
164 )\r
165/*++\r
166\r
167Routine Description:\r
168\r
169 Support routine to return the Image Read\r
170\r
171Arguments:\r
172\r
173 PeiServices - PEI Services Table\r
174\r
175 ImageContext - The context of the image being loaded\r
176\r
177Returns:\r
178\r
179 EFI_SUCCESS - If Image function location is found\r
180\r
181--*/\r
182{\r
183 VOID* MemoryBuffer;\r
184\r
185 MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r
186 ASSERT (MemoryBuffer != NULL);\r
187\r
188 CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
189\r
190 ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r
191\r
192 return EFI_SUCCESS;\r
193}\r
194\r
195STATIC\r
196EFI_STATUS\r
197LoadAndRelocatePeCoffImage (\r
198 IN VOID *Pe32Data,\r
199 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
200 OUT UINT64 *ImageSize,\r
201 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
202 )\r
203/*++\r
204\r
205Routine Description:\r
206\r
207 Loads and relocates a PE/COFF image into memory.\r
208\r
209Arguments:\r
210\r
211 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
212\r
213 ImageAddress - The base address of the relocated PE/COFF image\r
214\r
215 ImageSize - The size of the relocated PE/COFF image\r
216\r
217 EntryPoint - The entry point of the relocated PE/COFF image\r
218\r
219Returns:\r
220\r
221 EFI_SUCCESS - The file was loaded and relocated\r
222\r
223 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
224\r
225--*/\r
226{\r
227 EFI_STATUS Status;\r
228 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
229\r
230 ZeroMem (&ImageContext, sizeof (ImageContext));\r
231 ImageContext.Handle = Pe32Data;\r
232 Status = GetImageReadFunction (&ImageContext);\r
233\r
234 ASSERT_EFI_ERROR (Status);\r
235\r
236 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
237 if (EFI_ERROR (Status)) {\r
238 return Status;\r
239 }\r
240 //\r
241 // When Image has no reloc section, it can't be relocated into memory.\r
242 //\r
243 if (ImageContext.RelocationsStripped) {\r
244 DEBUG ((EFI_D_ERROR, "The image at 0x%08x without reloc section can't be loaded into memory", (UINTN) Pe32Data));\r
245 return EFI_INVALID_PARAMETER;\r
246 }\r
247 //\r
248 // Allocate Memory for the image\r
249 //\r
250 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
251 ASSERT (ImageContext.ImageAddress != 0);\r
252 \r
253 //\r
254 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
255 //\r
256 if (ImageContext.IsTeImage) {\r
257 ImageContext.ImageAddress = ImageContext.ImageAddress + \r
258 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
259 sizeof (EFI_TE_IMAGE_HEADER);\r
260 }\r
261\r
262 //\r
263 // Load the image to our new buffer\r
264 //\r
265 Status = PeCoffLoaderLoadImage (&ImageContext);\r
266 if (EFI_ERROR (Status)) {\r
267 return Status;\r
268 }\r
269 //\r
270 // Relocate the image in our new buffer\r
271 //\r
272 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
273 if (EFI_ERROR (Status)) {\r
274 return Status;\r
275 }\r
276\r
277 //\r
278 // Flush the instruction cache so the image data is written before we execute it\r
279 //\r
280 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
281\r
282 *ImageAddress = ImageContext.ImageAddress;\r
283 *ImageSize = ImageContext.ImageSize;\r
284 *EntryPoint = ImageContext.EntryPoint;\r
285\r
286 return EFI_SUCCESS;\r
287}\r
288\r
289EFI_STATUS\r
290PeiLoadImageLoadImage (\r
291 IN EFI_PEI_SERVICES **PeiServices,\r
292 IN EFI_PEI_FILE_HANDLE FileHandle,\r
293 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
294 OUT UINT64 *ImageSizeArg, OPTIONAL\r
295 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
296 OUT UINT32 *AuthenticationState\r
297 )\r
298/*++\r
299\r
300Routine Description:\r
301\r
302 Routine for loading file image.\r
303\r
304Arguments:\r
305\r
306 PeiServices - The PEI core services table.\r
307 FileHandle - Pointer to the FFS file header of the image.\r
308 ImageAddressArg - Pointer to PE/TE image.\r
309 ImageSizeArg - Size of PE/TE image.\r
310 EntryPoint - Pointer to entry point of specified image file for output.\r
311 AuthenticationState - Pointer to attestation authentication state of image.\r
312\r
313Returns:\r
314\r
315 Status - EFI_SUCCESS - Image is successfully loaded.\r
316 EFI_NOT_FOUND - Fail to locate necessary PPI\r
317 Others - Fail to load file.\r
318\r
319--*/\r
320{\r
321 EFI_STATUS Status;\r
322 VOID *Pe32Data;\r
323 EFI_PHYSICAL_ADDRESS ImageAddress;\r
324 UINT64 ImageSize;\r
325 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
326 UINT16 Machine;\r
327 PEI_CORE_INSTANCE *Private;\r
328 VOID *EntryPointArg;\r
329\r
330 *EntryPoint = 0;\r
331 ImageSize = 0;\r
332 *AuthenticationState = 0;\r
333\r
334 //\r
335 // Try to find a TE section.\r
336 //\r
337 Status = PeiServicesFfsFindSectionData (\r
338 EFI_SECTION_TE,\r
339 FileHandle,\r
340 &Pe32Data\r
341 );\r
342 //\r
343 // If we didn't find a TE section, try to find a PE32 section.\r
344 //\r
345 if (EFI_ERROR (Status)) {\r
346 Status = PeiServicesFfsFindSectionData (\r
347 EFI_SECTION_PE32,\r
348 FileHandle,\r
349 &Pe32Data\r
350 );\r
351 if (EFI_ERROR (Status)) {\r
352 //\r
353 // PEI core only carry the loader function fro TE and PE32 executables\r
354 // If this two section does not exist, just return.\r
355 //\r
356 return Status;\r
357 }\r
358 }\r
359 \r
360 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
361\r
362 if (Private->PeiMemoryInstalled && \r
363 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
364 //\r
365 // If memory is installed, perform the shadow operations\r
366 //\r
367 Status = LoadAndRelocatePeCoffImage (\r
368 Pe32Data,\r
369 &ImageAddress,\r
370 &ImageSize,\r
371 &ImageEntryPoint\r
372 );\r
373\r
374 if (EFI_ERROR (Status)) {\r
375 return Status;\r
376 }\r
377\r
378 //\r
379 // Got the entry point from the loaded Pe32Data\r
380 //\r
381 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
382 *EntryPoint = ImageEntryPoint;\r
383 } else {\r
384 //\r
385 // Retrieve the entry point from the PE/COFF or TE image header\r
386 //\r
387 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;\r
388 Status = PeCoffLoaderGetEntryPoint (Pe32Data, &EntryPointArg);\r
389 if (EFI_ERROR (Status)) {\r
390 return Status;\r
391 }\r
392 *EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) EntryPointArg;\r
393 }\r
394 \r
395 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
396 \r
397 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
398 return EFI_UNSUPPORTED; \r
399 }\r
400\r
401 if (ImageAddressArg != NULL) {\r
402 *ImageAddressArg = ImageAddress;\r
403 }\r
404\r
405 if (ImageSizeArg != NULL) {\r
406 *ImageSizeArg = ImageSize;\r
407 }\r
408 \r
409 DEBUG_CODE_BEGIN ();\r
410 CHAR8 *AsciiString;\r
411 CHAR8 AsciiBuffer[512];\r
412 INT32 Index;\r
413 INT32 Index1;\r
414\r
415 //\r
416 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
417 //\r
418 if (Machine != IMAGE_FILE_MACHINE_IA64) {\r
419 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%10p EntryPoint=0x%10p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
420 } else {\r
421 //\r
422 // For IPF Image, the real entry point should be print.\r
423 //\r
424 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%10p EntryPoint=0x%10p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r
425 }\r
426 \r
427 //\r
428 // Print Module Name by PeImage PDB file name.\r
429 //\r
430 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
431 \r
432 if (AsciiString != NULL) {\r
433 for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
434 if (AsciiString[Index] == '\\') {\r
435 break;\r
436 }\r
437 }\r
438\r
439 if (Index != 0) {\r
440 for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
441 AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r
442 }\r
443 AsciiBuffer [Index1] = '\0';\r
444 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
445 }\r
446 }\r
447\r
448 DEBUG_CODE_END ();\r
449\r
450 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
451\r
452 return EFI_SUCCESS;\r
453\r
454}\r
455\r
456\r
457EFI_STATUS\r
458EFIAPI\r
459PeiLoadImageLoadImageWrapper (\r
460 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
461 IN EFI_PEI_FILE_HANDLE FileHandle,\r
462 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
463 OUT UINT64 *ImageSizeArg, OPTIONAL\r
464 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
465 OUT UINT32 *AuthenticationState\r
466 )\r
467/*++\r
468\r
469Routine Description:\r
470\r
471 The wrapper function of PeiLoadImageLoadImage().\r
472\r
473Arguments:\r
474\r
475 This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
476 PeiServices - The PEI core services table.\r
477 FileHandle - Pointer to the FFS file header of the image.\r
478 ImageAddressArg - Pointer to PE/TE image.\r
479 ImageSizeArg - Size of PE/TE image.\r
480 EntryPoint - Pointer to entry point of specified image file for output.\r
481 AuthenticationState - Pointer to attestation authentication state of image.\r
482\r
483Returns:\r
484\r
485 EFI_STATUS.\r
486 \r
487--*/ \r
488{\r
489 return PeiLoadImageLoadImage (\r
490 GetPeiServicesTablePointer (),\r
491 FileHandle,\r
492 ImageAddressArg,\r
493 ImageSizeArg,\r
494 EntryPoint,\r
495 AuthenticationState\r
496 );\r
497}\r
498\r
499EFI_STATUS\r
500PeiLoadImage (\r
501 IN EFI_PEI_SERVICES **PeiServices,\r
502 IN EFI_PEI_FILE_HANDLE FileHandle,\r
503 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
504 OUT UINT32 *AuthenticationState\r
505 )\r
506/*++\r
507\r
508Routine Description:\r
509\r
510 Routine for load image file.\r
511\r
512Arguments:\r
513\r
514 PeiServices - The PEI core services table.\r
515 FileHandle - Pointer to the FFS file header of the image.\r
516 EntryPoint - Pointer to entry point of specified image file for output.\r
517 AuthenticationState - Pointer to attestation authentication state of image.\r
518\r
519Returns:\r
520\r
521 Status - EFI_SUCCESS - Image is successfully loaded.\r
522 EFI_NOT_FOUND - Fail to locate necessary PPI\r
523 Others - Fail to load file.\r
524 \r
525--*/ \r
526{\r
527 EFI_STATUS PpiStatus;\r
528 EFI_STATUS Status;\r
529 UINTN Index;\r
530 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
531 EFI_PHYSICAL_ADDRESS ImageAddress;\r
532 UINT64 ImageSize;\r
533\r
534 //\r
535 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
536 // one at a time, until one reports EFI_SUCCESS.\r
537 //\r
538 Index = 0;\r
539 do {\r
540 PpiStatus = PeiServicesLocatePpi (\r
541 &gEfiPeiLoadFilePpiGuid,\r
542 Index,\r
543 NULL,\r
544 (VOID **)&LoadFile\r
545 );\r
546 if (!EFI_ERROR (PpiStatus)) {\r
547 Status = LoadFile->LoadFile (\r
548 LoadFile, \r
549 FileHandle, \r
550 &ImageAddress, \r
551 &ImageSize,\r
552 EntryPoint,\r
553 AuthenticationState\r
554 );\r
555 if (!EFI_ERROR (Status)) {\r
556 return Status;\r
557 }\r
558 }\r
559 Index++;\r
560 } while (!EFI_ERROR (PpiStatus));\r
561\r
562 //\r
563 // If no instances reports EFI_SUCCESS, then build-in support for\r
564 // the PE32+/TE XIP image format is used.\r
565 //\r
566 Status = PeiLoadImageLoadImage (\r
567 PeiServices, \r
568 FileHandle, \r
569 NULL, \r
570 NULL, \r
571 EntryPoint, \r
572 AuthenticationState\r
573 );\r
574 return Status;\r
575}\r
576\r
577\r
578VOID\r
579InitializeImageServices (\r
580 IN PEI_CORE_INSTANCE *PrivateData,\r
581 IN PEI_CORE_INSTANCE *OldCoreData\r
582 )\r
583/*++\r
584\r
585Routine Description:\r
586\r
587 Install Pei Load File PPI.\r
588\r
589Arguments:\r
590\r
591 PrivateData - Pointer to PEI_CORE_INSTANCE.\r
592 OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
593\r
594Returns:\r
595\r
596 NONE.\r
597 \r
598--*/ \r
599{\r
600 if (OldCoreData == NULL) {\r
601 //\r
602 // The first time we are XIP (running from FLASH). We need to remember the\r
603 // FLASH address so we can reinstall the memory version that runs faster\r
604 //\r
605 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
606 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
607 } else {\r
608 //\r
609 // 2nd time we are running from memory so replace the XIP version with the \r
610 // new memory version. \r
611 //\r
612 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList); \r
613 }\r
614}\r
615\r
616\r
617\r