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