]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
Add change log to EdkShellBinPkg.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
CommitLineData
192f6d4c 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
192f6d4c 22#include <PeiMain.h>\r
23\r
b0d803fe 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
192f6d4c 29\r
b0d803fe 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
192f6d4c 42\r
43EFI_STATUS\r
b0d803fe 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
192f6d4c 51 )\r
52/*++\r
53\r
54Routine Description:\r
55\r
56 Routine for loading file image.\r
57\r
58Arguments:\r
59\r
b0d803fe 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
b0d803fe 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
b0d803fe 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
b0d803fe 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
3d7b0992 236 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
b0d803fe 237 if (EFI_ERROR (Status)) {\r
238 return Status;\r
239 }\r
240 //\r
241 // Allocate Memory for the image\r
242 //\r
243 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
244 ASSERT (ImageContext.ImageAddress != 0);\r
4e844595
LG
245 \r
246 //\r
247 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
248 //\r
249 if (ImageContext.IsTeImage) {\r
250 ImageContext.ImageAddress = ImageContext.ImageAddress + \r
251 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
252 sizeof (EFI_TE_IMAGE_HEADER);\r
253 }\r
b0d803fe 254\r
255 //\r
256 // Load the image to our new buffer\r
257 //\r
3d7b0992 258 Status = PeCoffLoaderLoadImage (&ImageContext);\r
b0d803fe 259 if (EFI_ERROR (Status)) {\r
260 return Status;\r
261 }\r
262 //\r
263 // Relocate the image in our new buffer\r
264 //\r
3d7b0992 265 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
b0d803fe 266 if (EFI_ERROR (Status)) {\r
267 return Status;\r
268 }\r
269\r
270 //\r
271 // Flush the instruction cache so the image data is written before we execute it\r
272 //\r
273 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
274\r
275 *ImageAddress = ImageContext.ImageAddress;\r
276 *ImageSize = ImageContext.ImageSize;\r
277 *EntryPoint = ImageContext.EntryPoint;\r
278\r
279 return EFI_SUCCESS;\r
280}\r
281\r
282EFI_STATUS\r
283PeiLoadImageLoadImage (\r
284 IN EFI_PEI_SERVICES **PeiServices,\r
285 IN EFI_PEI_FILE_HANDLE FileHandle,\r
286 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
287 OUT UINT64 *ImageSizeArg, OPTIONAL\r
288 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
289 OUT UINT32 *AuthenticationState\r
290 )\r
291/*++\r
292\r
293Routine Description:\r
294\r
295 Routine for loading file image.\r
296\r
297Arguments:\r
298\r
299 PeiServices - The PEI core services table.\r
300 FileHandle - Pointer to the FFS file header of the image.\r
301 ImageAddressArg - Pointer to PE/TE image.\r
302 ImageSizeArg - Size of PE/TE image.\r
303 EntryPoint - Pointer to entry point of specified image file for output.\r
304 AuthenticationState - Pointer to attestation authentication state of image.\r
192f6d4c 305\r
306Returns:\r
307\r
308 Status - EFI_SUCCESS - Image is successfully loaded.\r
309 EFI_NOT_FOUND - Fail to locate necessary PPI\r
310 Others - Fail to load file.\r
311\r
312--*/\r
313{\r
314 EFI_STATUS Status;\r
315 VOID *Pe32Data;\r
192f6d4c 316 EFI_PHYSICAL_ADDRESS ImageAddress;\r
317 UINT64 ImageSize;\r
318 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
192f6d4c 319 UINT16 Machine;\r
b0d803fe 320 PEI_CORE_INSTANCE *Private;\r
321 VOID *EntryPointArg;\r
192f6d4c 322\r
3d7b0992
LG
323 *EntryPoint = 0;\r
324 ImageSize = 0;\r
b0d803fe 325 *AuthenticationState = 0;\r
192f6d4c 326\r
327 //\r
b0d803fe 328 // Try to find a TE section.\r
192f6d4c 329 //\r
330 Status = PeiServicesFfsFindSectionData (\r
b0d803fe 331 EFI_SECTION_TE,\r
332 FileHandle,\r
192f6d4c 333 &Pe32Data\r
334 );\r
335 //\r
3d7b0992 336 // If we didn't find a TE section, try to find a PE32 section.\r
192f6d4c 337 //\r
338 if (EFI_ERROR (Status)) {\r
339 Status = PeiServicesFfsFindSectionData (\r
b0d803fe 340 EFI_SECTION_PE32,\r
341 FileHandle,\r
342 &Pe32Data\r
192f6d4c 343 );\r
b0d803fe 344 if (EFI_ERROR (Status)) {\r
192f6d4c 345 //\r
b0d803fe 346 // PEI core only carry the loader function fro TE and PE32 executables\r
347 // If this two section does not exist, just return.\r
192f6d4c 348 //\r
b0d803fe 349 return Status;\r
350 }\r
351 }\r
352 \r
353 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
192f6d4c 354\r
b0d803fe 355 if (Private->PeiMemoryInstalled && \r
356 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
3d7b0992
LG
357 //\r
358 // If memory is installed, perform the shadow operations\r
359 //\r
360 Status = LoadAndRelocatePeCoffImage (\r
361 Pe32Data,\r
362 &ImageAddress,\r
363 &ImageSize,\r
364 &ImageEntryPoint\r
365 );\r
192f6d4c 366\r
3d7b0992
LG
367 if (EFI_ERROR (Status)) {\r
368 return Status;\r
b0d803fe 369 }\r
3d7b0992
LG
370\r
371 //\r
372 // Got the entry point from the loaded Pe32Data\r
373 //\r
374 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
375 *EntryPoint = ImageEntryPoint;\r
b0d803fe 376 } else {\r
3d7b0992
LG
377 //\r
378 // Retrieve the entry point from the PE/COFF or TE image header\r
379 //\r
380 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;\r
381 Status = PeCoffLoaderGetEntryPoint (Pe32Data, &EntryPointArg);\r
382 if (EFI_ERROR (Status)) {\r
383 return Status;\r
192f6d4c 384 }\r
3d7b0992 385 *EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) EntryPointArg;\r
192f6d4c 386 }\r
3d7b0992
LG
387 \r
388 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
192f6d4c 389 \r
390 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
391 return EFI_UNSUPPORTED; \r
392 }\r
393\r
b0d803fe 394 if (ImageAddressArg != NULL) {\r
395 *ImageAddressArg = ImageAddress;\r
396 }\r
397\r
398 if (ImageSizeArg != NULL) {\r
399 *ImageSizeArg = ImageSize;\r
400 }\r
401 \r
192f6d4c 402 //\r
403 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
404 //\r
405 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN) ImageAddress, *EntryPoint));\r
406 DEBUG_CODE_BEGIN ();\r
3d7b0992
LG
407 CHAR8 *AsciiString;\r
408 CHAR8 AsciiBuffer[512];\r
409 INT32 Index;\r
410 INT32 Index1;\r
192f6d4c 411 \r
3d7b0992
LG
412 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
413 \r
414 if (AsciiString != NULL) {\r
19ea58a1 415 for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
3d7b0992
LG
416 if (AsciiString[Index] == '\\') {\r
417 break;\r
418 }\r
192f6d4c 419 }\r
192f6d4c 420\r
3d7b0992
LG
421 if (Index != 0) {\r
422 for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
423 AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r
192f6d4c 424 }\r
3d7b0992
LG
425 AsciiBuffer [Index1] = '\0';\r
426 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
192f6d4c 427 }\r
428 }\r
3d7b0992 429\r
192f6d4c 430 DEBUG_CODE_END ();\r
431\r
432 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
433\r
434 return EFI_SUCCESS;\r
b0d803fe 435\r
436}\r
437\r
438\r
439EFI_STATUS\r
440EFIAPI\r
441PeiLoadImageLoadImageWrapper (\r
442 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
443 IN EFI_PEI_FILE_HANDLE FileHandle,\r
444 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
445 OUT UINT64 *ImageSizeArg, OPTIONAL\r
446 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
447 OUT UINT32 *AuthenticationState\r
448 )\r
449/*++\r
450\r
451Routine Description:\r
452\r
453 The wrapper function of PeiLoadImageLoadImage().\r
454\r
455Arguments:\r
456\r
457 This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
458 PeiServices - The PEI core services table.\r
459 FileHandle - Pointer to the FFS file header of the image.\r
460 ImageAddressArg - Pointer to PE/TE image.\r
461 ImageSizeArg - Size of PE/TE image.\r
462 EntryPoint - Pointer to entry point of specified image file for output.\r
463 AuthenticationState - Pointer to attestation authentication state of image.\r
464\r
465Returns:\r
466\r
467 EFI_STATUS.\r
468 \r
469--*/ \r
470{\r
471 return PeiLoadImageLoadImage (\r
472 GetPeiServicesTablePointer (),\r
473 FileHandle,\r
474 ImageAddressArg,\r
475 ImageSizeArg,\r
476 EntryPoint,\r
477 AuthenticationState\r
478 );\r
192f6d4c 479}\r
b0d803fe 480\r
481EFI_STATUS\r
482PeiLoadImage (\r
483 IN EFI_PEI_SERVICES **PeiServices,\r
484 IN EFI_PEI_FILE_HANDLE FileHandle,\r
485 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
486 OUT UINT32 *AuthenticationState\r
487 )\r
488/*++\r
489\r
490Routine Description:\r
491\r
492 Routine for load image file.\r
493\r
494Arguments:\r
495\r
496 PeiServices - The PEI core services table.\r
497 FileHandle - Pointer to the FFS file header of the image.\r
498 EntryPoint - Pointer to entry point of specified image file for output.\r
499 AuthenticationState - Pointer to attestation authentication state of image.\r
500\r
501Returns:\r
502\r
503 Status - EFI_SUCCESS - Image is successfully loaded.\r
504 EFI_NOT_FOUND - Fail to locate necessary PPI\r
505 Others - Fail to load file.\r
506 \r
507--*/ \r
508{\r
509 EFI_STATUS PpiStatus;\r
510 EFI_STATUS Status;\r
511 UINTN Index;\r
512 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
513 EFI_PHYSICAL_ADDRESS ImageAddress;\r
514 UINT64 ImageSize;\r
515\r
516 //\r
517 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
518 // one at a time, until one reports EFI_SUCCESS.\r
519 //\r
520 Index = 0;\r
521 do {\r
522 PpiStatus = PeiServicesLocatePpi (\r
523 &gEfiPeiLoadFilePpiGuid,\r
524 Index,\r
525 NULL,\r
526 (VOID **)&LoadFile\r
527 );\r
528 if (!EFI_ERROR (PpiStatus)) {\r
529 Status = LoadFile->LoadFile (\r
530 LoadFile, \r
531 FileHandle, \r
532 &ImageAddress, \r
533 &ImageSize,\r
534 EntryPoint,\r
535 AuthenticationState\r
536 );\r
537 if (!EFI_ERROR (Status)) {\r
538 return Status;\r
539 }\r
540 }\r
541 Index++;\r
542 } while (!EFI_ERROR (PpiStatus));\r
543\r
544 //\r
545 // If no instances reports EFI_SUCCESS, then build-in support for\r
546 // the PE32+/TE XIP image format is used.\r
547 //\r
548 Status = PeiLoadImageLoadImage (\r
549 PeiServices, \r
550 FileHandle, \r
551 NULL, \r
552 NULL, \r
553 EntryPoint, \r
554 AuthenticationState\r
555 );\r
556 return Status;\r
557}\r
558\r
559\r
560VOID\r
561InitializeImageServices (\r
562 IN PEI_CORE_INSTANCE *PrivateData,\r
563 IN PEI_CORE_INSTANCE *OldCoreData\r
564 )\r
565/*++\r
566\r
567Routine Description:\r
568\r
c58cf83d 569 Install Pei Load File PPI.\r
b0d803fe 570\r
571Arguments:\r
572\r
573 PrivateData - Pointer to PEI_CORE_INSTANCE.\r
574 OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
575\r
576Returns:\r
577\r
578 NONE.\r
579 \r
580--*/ \r
581{\r
b0d803fe 582 if (OldCoreData == NULL) {\r
583 //\r
584 // The first time we are XIP (running from FLASH). We need to remember the\r
585 // FLASH address so we can reinstall the memory version that runs faster\r
586 //\r
587 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
588 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
589 } else {\r
590 //\r
591 // 2nd time we are running from memory so replace the XIP version with the \r
592 // new memory version. \r
593 //\r
594 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList); \r
595 }\r
596}\r
597\r
598\r
599\r