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