Make DxeIpl loop find the required section type in peiprocessfile functions.
[mirror_edk2.git] / EdkModulePkg / Core / DxeIplPeim / DxeLoad.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2006, 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 DxeLoad.c\r
15\r
16Abstract:\r
17\r
18 Last PEIM.\r
19 Responsibility of this module is to load the DXE Core from a Firmware Volume.\r
20\r
21--*/\r
22\r
abb26634 23#include "DxeIpl.h"\r
24\r
878ddf1f 25BOOLEAN gInMemory = FALSE;\r
26\r
27//\r
28// Module Globals used in the DXE to PEI handoff\r
29// These must be module globals, so the stack can be switched\r
30//\r
31static EFI_DXE_IPL_PPI mDxeIplPpi = {\r
32 DxeLoadCore\r
33};\r
34\r
35static EFI_PEI_FV_FILE_LOADER_PPI mLoadFilePpi = {\r
36 DxeIplLoadFile\r
37};\r
38\r
abb26634 39static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {\r
40 {\r
41 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
878ddf1f 42 &gEfiPeiFvFileLoaderPpiGuid,\r
43 &mLoadFilePpi\r
abb26634 44 },\r
45 {\r
878ddf1f 46 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
47 &gEfiDxeIplPpiGuid,\r
48 &mDxeIplPpi\r
abb26634 49 }\r
878ddf1f 50};\r
51\r
52static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = {\r
53 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
54 &gEfiEndOfPeiSignalPpiGuid,\r
55 NULL\r
56};\r
57\r
abb26634 58GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gEfiDecompress = {\r
878ddf1f 59 UefiDecompressGetInfo,\r
60 UefiDecompress\r
61};\r
62\r
abb26634 63GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gTianoDecompress = {\r
878ddf1f 64 TianoDecompressGetInfo,\r
65 TianoDecompress\r
66};\r
67\r
abb26634 68GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gCustomDecompress = {\r
878ddf1f 69 CustomDecompressGetInfo,\r
70 CustomDecompress\r
71};\r
72\r
878ddf1f 73EFI_STATUS\r
74EFIAPI\r
75PeimInitializeDxeIpl (\r
76 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
77 IN EFI_PEI_SERVICES **PeiServices\r
78 )\r
79/*++\r
80\r
81Routine Description:\r
82\r
83 Initializes the Dxe Ipl PPI\r
84\r
85Arguments:\r
86\r
87 FfsHeader - Pointer to FFS file header\r
88 PeiServices - General purpose services available to every PEIM.\r
89\r
90Returns:\r
91\r
92 EFI_SUCCESS\r
93\r
94--*/\r
95{\r
96 EFI_STATUS Status;\r
97 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
98 EFI_BOOT_MODE BootMode;\r
99\r
84a99d48 100 Status = PeiServicesGetBootMode (&BootMode);\r
878ddf1f 101 ASSERT_EFI_ERROR (Status);\r
102\r
abb26634 103 if (!gInMemory && (BootMode != BOOT_ON_S3_RESUME)) { \r
878ddf1f 104 //\r
105 // The DxeIpl has not yet been shadowed\r
106 //\r
107 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
108\r
109 //\r
110 // Shadow DxeIpl and then re-run its entry point\r
111 //\r
112 Status = ShadowDxeIpl (FfsHeader, PeiEfiPeiPeCoffLoader);\r
878ddf1f 113 } else {\r
878ddf1f 114 //\r
abb26634 115 // Install FvFileLoader and DxeIpl PPIs.\r
878ddf1f 116 //\r
abb26634 117 Status = PeiServicesInstallPpi (mPpiList);\r
118 ASSERT_EFI_ERROR(Status);\r
878ddf1f 119 }\r
abb26634 120 \r
121 return Status;\r
878ddf1f 122}\r
123\r
124EFI_STATUS\r
125EFIAPI\r
126DxeLoadCore (\r
127 IN EFI_DXE_IPL_PPI *This,\r
128 IN EFI_PEI_SERVICES **PeiServices,\r
129 IN EFI_PEI_HOB_POINTERS HobList\r
130 )\r
131/*++\r
132\r
133Routine Description:\r
134\r
135 Main entry point to last PEIM\r
136\r
137Arguments:\r
138 This - Entry point for DXE IPL PPI\r
139 PeiServices - General purpose services available to every PEIM.\r
140 HobList - Address to the Pei HOB list\r
141\r
142Returns:\r
143\r
144 EFI_SUCCESS - DEX core was successfully loaded.\r
145 EFI_OUT_OF_RESOURCES - There are not enough resources to load DXE core.\r
146\r
147--*/\r
148{\r
149 EFI_STATUS Status;\r
878ddf1f 150 EFI_GUID DxeCoreFileName;\r
151 EFI_GUID FirmwareFileName;\r
152 VOID *Pe32Data;\r
abb26634 153 VOID *FvImageData; \r
878ddf1f 154 EFI_PHYSICAL_ADDRESS DxeCoreAddress;\r
155 UINT64 DxeCoreSize;\r
156 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;\r
157 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
158 EFI_BOOT_MODE BootMode;\r
159 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;\r
160 EFI_PEI_S3_RESUME_PPI *S3Resume;\r
161\r
162// PERF_START (PeiServices, L"DxeIpl", NULL, 0);\r
878ddf1f 163\r
164 //\r
165 // if in S3 Resume, restore configure\r
166 //\r
84a99d48 167 Status = PeiServicesGetBootMode (&BootMode);\r
abb26634 168 ASSERT_EFI_ERROR(Status);\r
878ddf1f 169\r
abb26634 170 if (BootMode == BOOT_ON_S3_RESUME) {\r
84a99d48 171 Status = PeiServicesLocatePpi (\r
878ddf1f 172 &gEfiPeiS3ResumePpiGuid,\r
173 0,\r
174 NULL,\r
175 (VOID **)&S3Resume\r
176 );\r
878ddf1f 177 ASSERT_EFI_ERROR (Status);\r
178\r
179 Status = S3Resume->S3RestoreConfig (PeiServices);\r
878ddf1f 180 ASSERT_EFI_ERROR (Status);\r
abb26634 181 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
878ddf1f 182\r
84a99d48 183 Status = PeiServicesLocatePpi (\r
878ddf1f 184 &gEfiPeiRecoveryModulePpiGuid,\r
185 0,\r
186 NULL,\r
187 (VOID **)&PeiRecovery\r
188 );\r
878ddf1f 189 ASSERT_EFI_ERROR (Status);\r
abb26634 190\r
878ddf1f 191 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);\r
f78797d5
LG
192 if (EFI_ERROR (Status)) {\r
193 DEBUG ((EFI_D_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));\r
194 CpuDeadLoop ();\r
195 }\r
878ddf1f 196\r
197 //\r
198 // Now should have a HOB with the DXE core w/ the old HOB destroyed\r
199 //\r
200 }\r
201\r
202 //\r
abb26634 203 // Install the PEI Protocols that are shared between PEI and DXE\r
204 //\r
205 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
206 ASSERT (PeiEfiPeiPeCoffLoader != NULL);\r
207\r
abb26634 208\r
209 //\r
210 // Find the EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE type compressed Firmware Volume file\r
878ddf1f 211 // The file found will be processed by PeiProcessFile: It will first be decompressed to\r
abb26634 212 // a normal FV, then a corresponding FV type hob will be built. \r
878ddf1f 213 //\r
214 Status = PeiFindFile (\r
abb26634 215 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
216 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
217 &FirmwareFileName,\r
218 &FvImageData\r
219 );\r
878ddf1f 220\r
221 //\r
222 // Find the DXE Core in a Firmware Volume\r
223 //\r
224 Status = PeiFindFile (\r
225 EFI_FV_FILETYPE_DXE_CORE,\r
226 EFI_SECTION_PE32,\r
227 &DxeCoreFileName,\r
228 &Pe32Data\r
229 );\r
878ddf1f 230 ASSERT_EFI_ERROR (Status);\r
231\r
232 //\r
233 // Load the DXE Core from a Firmware Volume\r
234 //\r
235 Status = PeiLoadFile (\r
abb26634 236 PeiEfiPeiPeCoffLoader,\r
237 Pe32Data,\r
238 &DxeCoreAddress,\r
239 &DxeCoreSize,\r
240 &DxeCoreEntryPoint\r
241 );\r
878ddf1f 242 ASSERT_EFI_ERROR (Status);\r
243\r
878ddf1f 244 //\r
245 // Add HOB for the DXE Core\r
246 //\r
247 BuildModuleHob (\r
248 &DxeCoreFileName,\r
249 DxeCoreAddress,\r
250 DxeCoreSize,\r
251 DxeCoreEntryPoint\r
252 );\r
253\r
254 //\r
255 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT\r
256 //\r
257 REPORT_STATUS_CODE (\r
258 EFI_PROGRESS_CODE,\r
259 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT\r
260 );\r
261\r
657d3918 262 if (FeaturePcdGet (PcdDxeIplBuildShareCodeHobs)) {\r
263 if (FeaturePcdGet (PcdDxeIplSupportEfiDecompress)) {\r
264 //\r
265 // Add HOB for the EFI Decompress Protocol\r
266 //\r
267 BuildGuidDataHob (\r
268 &gEfiDecompressProtocolGuid,\r
269 (VOID *)&gEfiDecompress,\r
270 sizeof (gEfiDecompress)\r
271 );\r
272 }\r
273 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {\r
274 //\r
275 // Add HOB for the Tiano Decompress Protocol\r
276 //\r
277 BuildGuidDataHob (\r
278 &gEfiTianoDecompressProtocolGuid,\r
279 (VOID *)&gTianoDecompress,\r
280 sizeof (gTianoDecompress)\r
281 );\r
282 }\r
283 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {\r
284 //\r
285 // Add HOB for the user customized Decompress Protocol\r
286 //\r
287 BuildGuidDataHob (\r
288 &gEfiCustomizedDecompressProtocolGuid,\r
289 (VOID *)&gCustomDecompress,\r
290 sizeof (gCustomDecompress)\r
291 );\r
292 }\r
abb26634 293\r
294 //\r
295 // Add HOB for the PE/COFF Loader Protocol\r
296 //\r
297 BuildGuidDataHob (\r
298 &gEfiPeiPeCoffLoaderGuid,\r
299 (VOID *)&PeiEfiPeiPeCoffLoader,\r
300 sizeof (VOID *)\r
301 );\r
657d3918 302 }\r
303\r
3d109491 304 //\r
305 // Transfer control to the DXE Core\r
306 // The handoff state is simply a pointer to the HOB list\r
307 //\r
308\r
b32a39b3 309 DEBUG ((EFI_D_INFO, "DXE Core Entry Point 0x%08x\n", (UINTN) DxeCoreEntryPoint));\r
3d109491 310 HandOffToDxeCore (DxeCoreEntryPoint, HobList, &mPpiSignal);\r
878ddf1f 311 //\r
312 // If we get here, then the DXE Core returned. This is an error\r
f78797d5 313 // Dxe Core should not return.\r
878ddf1f 314 //\r
f78797d5
LG
315 ASSERT (FALSE);\r
316 CpuDeadLoop ();\r
878ddf1f 317\r
318 return EFI_OUT_OF_RESOURCES;\r
319}\r
320\r
321EFI_STATUS\r
322PeiFindFile (\r
323 IN UINT8 Type,\r
324 IN UINT16 SectionType,\r
325 OUT EFI_GUID *FileName,\r
326 OUT VOID **Pe32Data\r
327 )\r
328/*++\r
329\r
330Routine Description:\r
331\r
332 Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes\r
333 described in the HOB list. Able to search in a compression set in a FFS file.\r
334 But only one level of compression is supported, that is, not able to search\r
335 in a compression set that is within another compression set.\r
336\r
337Arguments:\r
338\r
339 Type - The Type of file to retrieve\r
340\r
341 SectionType - The type of section to retrieve from a file\r
342\r
343 FileName - The name of the file found in the Firmware Volume\r
344\r
345 Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume\r
346\r
347Returns:\r
348\r
349 EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to\r
350 the PE/COFF image is returned in Pe32Data\r
351\r
352 EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List\r
353\r
354--*/\r
355{\r
356 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
357 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
878ddf1f 358 EFI_STATUS Status;\r
359 EFI_PEI_HOB_POINTERS Hob;\r
360\r
361\r
362 FwVolHeader = NULL;\r
363 FfsFileHeader = NULL;\r
abb26634 364 Status = EFI_SUCCESS;\r
878ddf1f 365\r
366 //\r
abb26634 367 // For each Firmware Volume, look for a specified type\r
368 // of file and break out until no one is found \r
878ddf1f 369 //\r
370 Hob.Raw = GetHobList ();\r
371 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) {\r
372 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress);\r
84a99d48 373 Status = PeiServicesFfsFindNextFile (\r
878ddf1f 374 Type,\r
375 FwVolHeader,\r
376 &FfsFileHeader\r
377 );\r
378 if (!EFI_ERROR (Status)) {\r
878ddf1f 379 Status = PeiProcessFile (\r
380 SectionType,\r
abb26634 381 FfsFileHeader,\r
382 Pe32Data,\r
383 &Hob\r
878ddf1f 384 );\r
6326ee4e 385 CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID));\r
d6992908
LG
386 //\r
387 // Find all Fv type ffs to get all FvImage and add them into FvHob\r
388 //\r
389 if (!EFI_ERROR (Status) && (Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) {\r
abb26634 390 return EFI_SUCCESS;\r
391 }\r
878ddf1f 392 }\r
393 Hob.Raw = GET_NEXT_HOB (Hob);\r
394 }\r
395 return EFI_NOT_FOUND;\r
396}\r
397\r
398EFI_STATUS\r
399PeiLoadFile (\r
400 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader,\r
401 IN VOID *Pe32Data,\r
402 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
403 OUT UINT64 *ImageSize,\r
404 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
405 )\r
406/*++\r
407\r
408Routine Description:\r
409\r
410 Loads and relocates a PE/COFF image into memory.\r
411\r
412Arguments:\r
413\r
414 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol\r
415\r
416 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
417\r
418 ImageAddress - The base address of the relocated PE/COFF image\r
419\r
420 ImageSize - The size of the relocated PE/COFF image\r
421\r
422 EntryPoint - The entry point of the relocated PE/COFF image\r
423\r
424Returns:\r
425\r
426 EFI_SUCCESS - The file was loaded and relocated\r
427\r
428 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
429\r
430--*/\r
431{\r
432 EFI_STATUS Status;\r
433 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
434\r
435 ZeroMem (&ImageContext, sizeof (ImageContext));\r
436 ImageContext.Handle = Pe32Data;\r
437 Status = GetImageReadFunction (&ImageContext);\r
438\r
439 ASSERT_EFI_ERROR (Status);\r
440\r
441 Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext);\r
442 if (EFI_ERROR (Status)) {\r
443 return Status;\r
444 }\r
445 //\r
446 // Allocate Memory for the image\r
447 //\r
448 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
449 ASSERT (ImageContext.ImageAddress != 0);\r
450\r
451 //\r
452 // Load the image to our new buffer\r
453 //\r
454 Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext);\r
455 if (EFI_ERROR (Status)) {\r
456 return Status;\r
457 }\r
458 //\r
459 // Relocate the image in our new buffer\r
460 //\r
461 Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext);\r
462 if (EFI_ERROR (Status)) {\r
463 return Status;\r
464 }\r
465\r
466 //\r
467 // Flush the instruction cache so the image data is written before we execute it\r
468 //\r
469 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
470\r
471 *ImageAddress = ImageContext.ImageAddress;\r
472 *ImageSize = ImageContext.ImageSize;\r
473 *EntryPoint = ImageContext.EntryPoint;\r
474\r
475 return EFI_SUCCESS;\r
476}\r
477\r
478EFI_STATUS\r
479ShadowDxeIpl (\r
480 IN EFI_FFS_FILE_HEADER *DxeIplFileHeader,\r
481 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader\r
482 )\r
483/*++\r
484\r
485Routine Description:\r
486\r
487 Shadow the DXE IPL to a different memory location. This occurs after permanent\r
488 memory has been discovered.\r
489\r
490Arguments:\r
491\r
492 DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver\r
493\r
494 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol\r
495\r
496Returns:\r
497\r
498 EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location.\r
499\r
500 EFI_ ERROR - The shadow was unsuccessful.\r
501\r
502\r
503--*/\r
504{\r
505 UINTN SectionLength;\r
506 UINTN OccupiedSectionLength;\r
507 EFI_PHYSICAL_ADDRESS DxeIplAddress;\r
508 UINT64 DxeIplSize;\r
509 EFI_PHYSICAL_ADDRESS DxeIplEntryPoint;\r
510 EFI_STATUS Status;\r
511 EFI_COMMON_SECTION_HEADER *Section;\r
512\r
513 Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1);\r
514\r
515 while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) {\r
516 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
abb26634 517 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
878ddf1f 518 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);\r
519 }\r
520 //\r
521 // Relocate DxeIpl into memory by using loadfile service\r
522 //\r
523 Status = PeiLoadFile (\r
524 PeiEfiPeiPeCoffLoader,\r
525 (VOID *) (Section + 1),\r
526 &DxeIplAddress,\r
527 &DxeIplSize,\r
528 &DxeIplEntryPoint\r
529 );\r
530\r
531 if (Status == EFI_SUCCESS) {\r
532 //\r
abb26634 533 // Set gInMemory global variable to TRUE to indicate the dxeipl is shadowed.\r
878ddf1f 534 //\r
abb26634 535 *(BOOLEAN *) ((UINTN) &gInMemory + (UINTN) DxeIplEntryPoint - (UINTN) _ModuleEntryPoint) = TRUE;\r
878ddf1f 536 Status = ((EFI_PEIM_ENTRY_POINT) (UINTN) DxeIplEntryPoint) (DxeIplFileHeader, GetPeiServicesTablePointer());\r
537 }\r
538\r
539 return Status;\r
540}\r
541\r
542EFI_STATUS\r
543EFIAPI\r
544DxeIplLoadFile (\r
545 IN EFI_PEI_FV_FILE_LOADER_PPI *This,\r
546 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
547 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
548 OUT UINT64 *ImageSize,\r
549 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
550 )\r
551/*++\r
552\r
553Routine Description:\r
554\r
555 Given a pointer to an FFS file containing a PE32 image, get the\r
556 information on the PE32 image, and then "load" it so that it\r
557 can be executed.\r
558\r
559Arguments:\r
560\r
561 This - pointer to our file loader protocol\r
562\r
563 FfsHeader - pointer to the FFS file header of the FFS file that\r
564 contains the PE32 image we want to load\r
565\r
566 ImageAddress - returned address where the PE32 image is loaded\r
567\r
568 ImageSize - returned size of the loaded PE32 image\r
569\r
570 EntryPoint - entry point to the loaded PE32 image\r
571\r
572Returns:\r
573\r
574 EFI_SUCCESS - The FFS file was successfully loaded.\r
575\r
576 EFI_ERROR - Unable to load the FFS file.\r
577\r
578--*/\r
579{\r
580 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
581 EFI_STATUS Status;\r
582 VOID *Pe32Data;\r
583\r
584 Pe32Data = NULL;\r
585 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
586\r
587 //\r
588 // Preprocess the FFS file to get a pointer to the PE32 information\r
589 // in the enclosed PE32 image.\r
590 //\r
591 Status = PeiProcessFile (\r
592 EFI_SECTION_PE32,\r
abb26634 593 FfsHeader,\r
594 &Pe32Data,\r
595 NULL\r
878ddf1f 596 );\r
597\r
598 if (EFI_ERROR (Status)) {\r
599 return Status;\r
600 }\r
601 //\r
602 // Load the PE image from the FFS file\r
603 //\r
604 Status = PeiLoadFile (\r
605 PeiEfiPeiPeCoffLoader,\r
606 Pe32Data,\r
607 ImageAddress,\r
608 ImageSize,\r
609 EntryPoint\r
610 );\r
611\r
612 return Status;\r
613}\r
614\r
615EFI_STATUS\r
616PeiProcessFile (\r
6326ee4e 617 IN UINT16 SectionType,\r
abb26634 618 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
619 OUT VOID **Pe32Data,\r
620 IN EFI_PEI_HOB_POINTERS *OrigHob\r
878ddf1f 621 )\r
622/*++\r
623\r
624Routine Description:\r
625\r
626Arguments:\r
627\r
628 SectionType - The type of section in the FFS file to process.\r
629\r
630 FfsFileHeader - Pointer to the FFS file to process, looking for the\r
631 specified SectionType\r
632\r
633 Pe32Data - returned pointer to the start of the PE32 image found\r
634 in the FFS file.\r
635\r
636Returns:\r
637\r
638 EFI_SUCCESS - found the PE32 section in the FFS file\r
639\r
640--*/\r
641{\r
642 EFI_STATUS Status;\r
643 VOID *SectionData;\r
644 DECOMPRESS_LIBRARY *DecompressLibrary;\r
645 UINT8 *DstBuffer;\r
646 UINT8 *ScratchBuffer;\r
647 UINT32 DstBufferSize;\r
648 UINT32 ScratchBufferSize;\r
649 EFI_COMMON_SECTION_HEADER *CmpSection;\r
650 UINTN CmpSectionLength;\r
651 UINTN OccupiedCmpSectionLength;\r
652 VOID *CmpFileData;\r
653 UINTN CmpFileSize;\r
654 EFI_COMMON_SECTION_HEADER *Section;\r
655 UINTN SectionLength;\r
656 UINTN OccupiedSectionLength;\r
657 UINT64 FileSize;\r
878ddf1f 658 UINT32 AuthenticationStatus;\r
659 EFI_PEI_SECTION_EXTRACTION_PPI *SectionExtract;\r
660 UINT32 BufferSize;\r
661 UINT8 *Buffer;\r
662 EFI_PEI_SECURITY_PPI *Security;\r
663 BOOLEAN StartCrisisRecovery;\r
664 EFI_GUID TempGuid;\r
665 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
666 EFI_COMPRESSION_SECTION *CompressionSection;\r
667\r
4afc6a7b
LG
668 //\r
669 // Initialize local variables.\r
670 //\r
671 DecompressLibrary = NULL;\r
672 DstBuffer = NULL;\r
673 DstBufferSize = 0;\r
674\r
84a99d48 675 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 676 EFI_SECTION_COMPRESSION,\r
677 FfsFileHeader,\r
678 &SectionData\r
679 );\r
680\r
681 //\r
abb26634 682 // First process the compression section\r
878ddf1f 683 //\r
684 if (!EFI_ERROR (Status)) {\r
685 //\r
686 // Yes, there is a compression section, so extract the contents\r
687 // Decompress the image here\r
688 //\r
689 Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER));\r
690\r
691 do {\r
692 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
abb26634 693 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
878ddf1f 694\r
695 //\r
696 // Was the DXE Core file encapsulated in a GUID'd section?\r
697 //\r
698 if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
878ddf1f 699\r
700 //\r
701 // This following code constitutes the addition of the security model\r
702 // to the DXE IPL.\r
703 //\r
704 //\r
705 // Set a default authenticatino state\r
706 //\r
707 AuthenticationStatus = 0;\r
708\r
84a99d48 709 Status = PeiServicesLocatePpi (\r
878ddf1f 710 &gEfiPeiSectionExtractionPpiGuid,\r
711 0,\r
712 NULL,\r
713 (VOID **)&SectionExtract\r
714 );\r
715\r
716 if (EFI_ERROR (Status)) {\r
717 return Status;\r
718 }\r
719 //\r
720 // Verify Authentication State\r
721 //\r
722 CopyMem (&TempGuid, Section + 1, sizeof (EFI_GUID));\r
723\r
724 Status = SectionExtract->PeiGetSection (\r
725 GetPeiServicesTablePointer(),\r
726 SectionExtract,\r
727 (EFI_SECTION_TYPE *) &SectionType,\r
728 &TempGuid,\r
729 0,\r
730 (VOID **) &Buffer,\r
731 &BufferSize,\r
732 &AuthenticationStatus\r
733 );\r
734\r
735 if (EFI_ERROR (Status)) {\r
736 return Status;\r
737 }\r
738 //\r
739 // If not ask the Security PPI, if exists, for disposition\r
740 //\r
741 //\r
84a99d48 742 Status = PeiServicesLocatePpi (\r
878ddf1f 743 &gEfiPeiSecurityPpiGuid,\r
744 0,\r
745 NULL,\r
746 (VOID **)&Security\r
747 );\r
748 if (EFI_ERROR (Status)) {\r
749 return Status;\r
750 }\r
751\r
752 Status = Security->AuthenticationState (\r
753 GetPeiServicesTablePointer(),\r
754 (struct _EFI_PEI_SECURITY_PPI *) Security,\r
755 AuthenticationStatus,\r
756 FfsFileHeader,\r
757 &StartCrisisRecovery\r
758 );\r
759\r
760 if (EFI_ERROR (Status)) {\r
761 return Status;\r
762 }\r
763 //\r
764 // If there is a security violation, report to caller and have\r
765 // the upper-level logic possible engender a crisis recovery\r
766 //\r
767 if (StartCrisisRecovery) {\r
768 return EFI_SECURITY_VIOLATION;\r
769 }\r
770 }\r
771\r
772 if (Section->Type == EFI_SECTION_PE32) {\r
773 //\r
774 // This is what we want\r
775 //\r
776 *Pe32Data = (VOID *) (Section + 1);\r
777 return EFI_SUCCESS;\r
778 } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
779 //\r
780 // This is a compression set, expand it\r
781 //\r
782 CompressionSection = (EFI_COMPRESSION_SECTION *) Section;\r
783\r
784 switch (CompressionSection->CompressionType) {\r
785 case EFI_STANDARD_COMPRESSION:\r
4afc6a7b
LG
786 //\r
787 // Load EFI standard compression.\r
788 //\r
abb26634 789 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {\r
4afc6a7b 790 DecompressLibrary = &gEfiDecompress;\r
abb26634 791 } else {\r
792 ASSERT (FALSE);\r
793 return EFI_NOT_FOUND;\r
794 }\r
878ddf1f 795 break;\r
796\r
797 case EFI_CUSTOMIZED_COMPRESSION:\r
798 //\r
4afc6a7b 799 // Load user customized compression.\r
878ddf1f 800 //\r
abb26634 801 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {\r
802 DecompressLibrary = &gCustomDecompress;\r
803 } else {\r
804 ASSERT (FALSE);\r
805 return EFI_NOT_FOUND;\r
806 }\r
878ddf1f 807 break;\r
808\r
809 case EFI_NOT_COMPRESSED:\r
4afc6a7b
LG
810 //\r
811 // Allocate destination buffer\r
812 //\r
813 DstBufferSize = CompressionSection->UncompressedLength;\r
814 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
815 if (DstBuffer == NULL) {\r
816 return EFI_OUT_OF_RESOURCES;\r
817 }\r
818 //\r
819 // stream is not actually compressed, just encapsulated. So just copy it.\r
820 //\r
821 CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize);\r
822 break;\r
823\r
878ddf1f 824 default:\r
825 //\r
4afc6a7b 826 // Don't support other unknown compression type.\r
878ddf1f 827 //\r
828 ASSERT_EFI_ERROR (Status);\r
829 return EFI_NOT_FOUND;\r
830 }\r
4afc6a7b
LG
831 \r
832 if (CompressionSection->CompressionType != EFI_NOT_COMPRESSED) {\r
878ddf1f 833 //\r
4afc6a7b 834 // For compressed data, decompress them to dstbuffer.\r
878ddf1f 835 //\r
4afc6a7b
LG
836 Status = DecompressLibrary->GetInfo (\r
837 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
838 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),\r
839 &DstBufferSize,\r
840 &ScratchBufferSize\r
841 );\r
842 if (EFI_ERROR (Status)) {\r
843 //\r
844 // GetInfo failed\r
845 //\r
e75ac0cc 846 DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status));\r
4afc6a7b
LG
847 return EFI_NOT_FOUND;\r
848 }\r
849 \r
850 //\r
851 // Allocate scratch buffer\r
852 //\r
853 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
854 if (ScratchBuffer == NULL) {\r
855 return EFI_OUT_OF_RESOURCES;\r
856 }\r
857 \r
858 //\r
859 // Allocate destination buffer\r
860 //\r
861 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
862 if (DstBuffer == NULL) {\r
863 return EFI_OUT_OF_RESOURCES;\r
864 }\r
865 \r
866 //\r
867 // Call decompress function\r
868 //\r
869 Status = DecompressLibrary->Decompress (\r
870 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
871 DstBuffer,\r
872 ScratchBuffer\r
873 );\r
e75ac0cc
LG
874 if (EFI_ERROR (Status)) {\r
875 //\r
876 // Decompress failed\r
877 //\r
878 DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status));\r
879 return EFI_NOT_FOUND;\r
880 }\r
878ddf1f 881 }\r
878ddf1f 882\r
878ddf1f 883 //\r
884 // Decompress successfully.\r
885 // Loop the decompressed data searching for expected section.\r
886 //\r
d6992908 887 CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer;\r
878ddf1f 888 CmpFileData = (VOID *) DstBuffer;\r
889 CmpFileSize = DstBufferSize;\r
890 do {\r
891 CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff;\r
d6992908 892 if (CmpSection->Type == SectionType) {\r
878ddf1f 893 //\r
894 // This is what we want\r
895 //\r
d6992908
LG
896 if (SectionType == EFI_SECTION_PE32) {\r
897 *Pe32Data = (VOID *) (CmpSection + 1);\r
898 return EFI_SUCCESS;\r
899 } else if (SectionType == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {\r
900 // \r
901 // Firmware Volume Image in this Section\r
902 // Skip the section header to get FvHeader\r
903 //\r
904 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (CmpSection + 1);\r
905 \r
906 if (FvHeader->Signature == EFI_FVH_SIGNATURE) {\r
907 //\r
908 // Because FvLength in FvHeader is UINT64 type, \r
909 // so FvHeader must meed at least 8 bytes alignment.\r
910 // If current FvImage base address doesn't meet its alignment,\r
911 // we need to reload this FvImage to another correct memory address.\r
912 //\r
913 if (((UINTN) FvHeader % sizeof (UINT64)) != 0) {\r
914 DstBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN) CmpSectionLength - sizeof (EFI_COMMON_SECTION_HEADER)), sizeof (UINT64));\r
915 if (DstBuffer == NULL) {\r
916 return EFI_OUT_OF_RESOURCES;\r
917 }\r
918 CopyMem (DstBuffer, FvHeader, (UINTN) CmpSectionLength - sizeof (EFI_COMMON_SECTION_HEADER));\r
919 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer; \r
920 }\r
878ddf1f 921\r
d6992908
LG
922 //\r
923 // Build new FvHob for new decompressed Fv image.\r
924 //\r
925 BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength);\r
926 \r
927 //\r
928 // Set the original FvHob to unused.\r
929 //\r
930 if (OrigHob != NULL) {\r
931 OrigHob->Header->HobType = EFI_HOB_TYPE_UNUSED;\r
932 }\r
933 \r
934 //\r
935 // return found FvImage data.\r
936 //\r
937 *Pe32Data = (VOID *) FvHeader;\r
938 return EFI_SUCCESS;\r
939 }\r
940 }\r
941 }\r
abb26634 942 OccupiedCmpSectionLength = GET_OCCUPIED_SIZE (CmpSectionLength, 4);\r
878ddf1f 943 CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength);\r
944 } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize);\r
945 }\r
abb26634 946 //\r
947 // End of the decompression activity\r
948 //\r
878ddf1f 949\r
950 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);\r
951 FileSize = FfsFileHeader->Size[0] & 0xFF;\r
952 FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00;\r
953 FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000;\r
954 FileSize &= 0x00FFFFFF;\r
955 } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section - (UINT8 *) FfsFileHeader) < FileSize);\r
abb26634 956 \r
878ddf1f 957 //\r
abb26634 958 // search all sections (compression and non compression) in this FFS, don't \r
959 // find expected section.\r
878ddf1f 960 //\r
abb26634 961 return EFI_NOT_FOUND;\r
878ddf1f 962 } else {\r
abb26634 963 //\r
964 // For those FFS that doesn't contain compression section, directly search \r
965 // PE or TE section in this FFS.\r
966 //\r
878ddf1f 967\r
84a99d48 968 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 969 EFI_SECTION_PE32,\r
970 FfsFileHeader,\r
971 &SectionData\r
972 );\r
973\r
974 if (EFI_ERROR (Status)) {\r
84a99d48 975 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 976 EFI_SECTION_TE,\r
977 FfsFileHeader,\r
978 &SectionData\r
979 );\r
980 if (EFI_ERROR (Status)) {\r
981 return Status;\r
982 }\r
983 }\r
984 }\r
985\r
986 *Pe32Data = SectionData;\r
987\r
988 return EFI_SUCCESS;\r
989}\r
abb26634 990\r