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