]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Core/DxeIplPeim/DxeLoad.c
(1) Using EfiCompress in place of TianoCompress as EFI_STANDARD_COMPRESSION type...
[mirror_edk2.git] / EdkModulePkg / 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 BOOLEAN gInMemory = FALSE;
26
27 //
28 // Module Globals used in the DXE to PEI handoff
29 // These must be module globals, so the stack can be switched
30 //
31 static EFI_DXE_IPL_PPI mDxeIplPpi = {
32 DxeLoadCore
33 };
34
35 static EFI_PEI_FV_FILE_LOADER_PPI mLoadFilePpi = {
36 DxeIplLoadFile
37 };
38
39 static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {
40 {
41 EFI_PEI_PPI_DESCRIPTOR_PPI,
42 &gEfiPeiFvFileLoaderPpiGuid,
43 &mLoadFilePpi
44 },
45 {
46 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
47 &gEfiDxeIplPpiGuid,
48 &mDxeIplPpi
49 }
50 };
51
52 static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = {
53 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
54 &gEfiEndOfPeiSignalPpiGuid,
55 NULL
56 };
57
58 GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gEfiDecompress = {
59 UefiDecompressGetInfo,
60 UefiDecompress
61 };
62
63 GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gTianoDecompress = {
64 TianoDecompressGetInfo,
65 TianoDecompress
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 // Transfer control to the DXE Core
246 // The handoff state is simply a pointer to the HOB list
247 //
248
249 Status = PeiServicesInstallPpi (&mPpiSignal);
250 ASSERT_EFI_ERROR (Status);
251
252 //
253 // Add HOB for the DXE Core
254 //
255 BuildModuleHob (
256 &DxeCoreFileName,
257 DxeCoreAddress,
258 DxeCoreSize,
259 DxeCoreEntryPoint
260 );
261
262 //
263 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
264 //
265 REPORT_STATUS_CODE (
266 EFI_PROGRESS_CODE,
267 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
268 );
269
270 if (FeaturePcdGet (PcdDxeIplBuildShareCodeHobs)) {
271 if (FeaturePcdGet (PcdDxeIplSupportEfiDecompress)) {
272 //
273 // Add HOB for the EFI Decompress Protocol
274 //
275 BuildGuidDataHob (
276 &gEfiDecompressProtocolGuid,
277 (VOID *)&gEfiDecompress,
278 sizeof (gEfiDecompress)
279 );
280 }
281 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {
282 //
283 // Add HOB for the Tiano Decompress Protocol
284 //
285 BuildGuidDataHob (
286 &gEfiTianoDecompressProtocolGuid,
287 (VOID *)&gTianoDecompress,
288 sizeof (gTianoDecompress)
289 );
290 }
291 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {
292 //
293 // Add HOB for the user customized Decompress Protocol
294 //
295 BuildGuidDataHob (
296 &gEfiCustomizedDecompressProtocolGuid,
297 (VOID *)&gCustomDecompress,
298 sizeof (gCustomDecompress)
299 );
300 }
301
302 //
303 // Add HOB for the PE/COFF Loader Protocol
304 //
305 BuildGuidDataHob (
306 &gEfiPeiPeCoffLoaderGuid,
307 (VOID *)&PeiEfiPeiPeCoffLoader,
308 sizeof (VOID *)
309 );
310 }
311
312 DEBUG ((EFI_D_INFO, "DXE Core Entry Point 0x%08x\n", (UINTN) DxeCoreEntryPoint));
313 HandOffToDxeCore (DxeCoreEntryPoint, HobList);
314 //
315 // If we get here, then the DXE Core returned. This is an error
316 // Dxe Core should not return.
317 //
318 ASSERT (FALSE);
319 CpuDeadLoop ();
320
321 return EFI_OUT_OF_RESOURCES;
322 }
323
324 EFI_STATUS
325 PeiFindFile (
326 IN UINT8 Type,
327 IN UINT16 SectionType,
328 OUT EFI_GUID *FileName,
329 OUT VOID **Pe32Data
330 )
331 /*++
332
333 Routine Description:
334
335 Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes
336 described in the HOB list. Able to search in a compression set in a FFS file.
337 But only one level of compression is supported, that is, not able to search
338 in a compression set that is within another compression set.
339
340 Arguments:
341
342 Type - The Type of file to retrieve
343
344 SectionType - The type of section to retrieve from a file
345
346 FileName - The name of the file found in the Firmware Volume
347
348 Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume
349
350 Returns:
351
352 EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to
353 the PE/COFF image is returned in Pe32Data
354
355 EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List
356
357 --*/
358 {
359 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
360 EFI_FFS_FILE_HEADER *FfsFileHeader;
361 VOID *SectionData;
362 EFI_STATUS Status;
363 EFI_PEI_HOB_POINTERS Hob;
364
365
366 FwVolHeader = NULL;
367 FfsFileHeader = NULL;
368 SectionData = NULL;
369 Status = EFI_SUCCESS;
370
371 //
372 // For each Firmware Volume, look for a specified type
373 // of file and break out until no one is found
374 //
375 Hob.Raw = GetHobList ();
376 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) {
377 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress);
378 Status = PeiServicesFfsFindNextFile (
379 Type,
380 FwVolHeader,
381 &FfsFileHeader
382 );
383 if (!EFI_ERROR (Status)) {
384 Status = PeiProcessFile (
385 SectionType,
386 FfsFileHeader,
387 Pe32Data,
388 &Hob
389 );
390 CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID));
391 if (!EFI_ERROR (Status)) {
392 return EFI_SUCCESS;
393 }
394 }
395 Hob.Raw = GET_NEXT_HOB (Hob);
396 }
397 return EFI_NOT_FOUND;
398 }
399
400 EFI_STATUS
401 PeiLoadFile (
402 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader,
403 IN VOID *Pe32Data,
404 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
405 OUT UINT64 *ImageSize,
406 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
407 )
408 /*++
409
410 Routine Description:
411
412 Loads and relocates a PE/COFF image into memory.
413
414 Arguments:
415
416 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol
417
418 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
419
420 ImageAddress - The base address of the relocated PE/COFF image
421
422 ImageSize - The size of the relocated PE/COFF image
423
424 EntryPoint - The entry point of the relocated PE/COFF image
425
426 Returns:
427
428 EFI_SUCCESS - The file was loaded and relocated
429
430 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
431
432 --*/
433 {
434 EFI_STATUS Status;
435 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
436
437 ZeroMem (&ImageContext, sizeof (ImageContext));
438 ImageContext.Handle = Pe32Data;
439 Status = GetImageReadFunction (&ImageContext);
440
441 ASSERT_EFI_ERROR (Status);
442
443 Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext);
444 if (EFI_ERROR (Status)) {
445 return Status;
446 }
447 //
448 // Allocate Memory for the image
449 //
450 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));
451 ASSERT (ImageContext.ImageAddress != 0);
452
453 //
454 // Load the image to our new buffer
455 //
456 Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext);
457 if (EFI_ERROR (Status)) {
458 return Status;
459 }
460 //
461 // Relocate the image in our new buffer
462 //
463 Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext);
464 if (EFI_ERROR (Status)) {
465 return Status;
466 }
467
468 //
469 // Flush the instruction cache so the image data is written before we execute it
470 //
471 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
472
473 *ImageAddress = ImageContext.ImageAddress;
474 *ImageSize = ImageContext.ImageSize;
475 *EntryPoint = ImageContext.EntryPoint;
476
477 return EFI_SUCCESS;
478 }
479
480 EFI_STATUS
481 ShadowDxeIpl (
482 IN EFI_FFS_FILE_HEADER *DxeIplFileHeader,
483 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader
484 )
485 /*++
486
487 Routine Description:
488
489 Shadow the DXE IPL to a different memory location. This occurs after permanent
490 memory has been discovered.
491
492 Arguments:
493
494 DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver
495
496 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol
497
498 Returns:
499
500 EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location.
501
502 EFI_ ERROR - The shadow was unsuccessful.
503
504
505 --*/
506 {
507 UINTN SectionLength;
508 UINTN OccupiedSectionLength;
509 EFI_PHYSICAL_ADDRESS DxeIplAddress;
510 UINT64 DxeIplSize;
511 EFI_PHYSICAL_ADDRESS DxeIplEntryPoint;
512 EFI_STATUS Status;
513 EFI_COMMON_SECTION_HEADER *Section;
514
515 Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1);
516
517 while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) {
518 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;
519 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
520 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);
521 }
522 //
523 // Relocate DxeIpl into memory by using loadfile service
524 //
525 Status = PeiLoadFile (
526 PeiEfiPeiPeCoffLoader,
527 (VOID *) (Section + 1),
528 &DxeIplAddress,
529 &DxeIplSize,
530 &DxeIplEntryPoint
531 );
532
533 if (Status == EFI_SUCCESS) {
534 //
535 // Set gInMemory global variable to TRUE to indicate the dxeipl is shadowed.
536 //
537 *(BOOLEAN *) ((UINTN) &gInMemory + (UINTN) DxeIplEntryPoint - (UINTN) _ModuleEntryPoint) = TRUE;
538 Status = ((EFI_PEIM_ENTRY_POINT) (UINTN) DxeIplEntryPoint) (DxeIplFileHeader, GetPeiServicesTablePointer());
539 }
540
541 return Status;
542 }
543
544 EFI_STATUS
545 EFIAPI
546 DxeIplLoadFile (
547 IN EFI_PEI_FV_FILE_LOADER_PPI *This,
548 IN EFI_FFS_FILE_HEADER *FfsHeader,
549 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
550 OUT UINT64 *ImageSize,
551 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
552 )
553 /*++
554
555 Routine Description:
556
557 Given a pointer to an FFS file containing a PE32 image, get the
558 information on the PE32 image, and then "load" it so that it
559 can be executed.
560
561 Arguments:
562
563 This - pointer to our file loader protocol
564
565 FfsHeader - pointer to the FFS file header of the FFS file that
566 contains the PE32 image we want to load
567
568 ImageAddress - returned address where the PE32 image is loaded
569
570 ImageSize - returned size of the loaded PE32 image
571
572 EntryPoint - entry point to the loaded PE32 image
573
574 Returns:
575
576 EFI_SUCCESS - The FFS file was successfully loaded.
577
578 EFI_ERROR - Unable to load the FFS file.
579
580 --*/
581 {
582 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;
583 EFI_STATUS Status;
584 VOID *Pe32Data;
585
586 Pe32Data = NULL;
587 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();
588
589 //
590 // Preprocess the FFS file to get a pointer to the PE32 information
591 // in the enclosed PE32 image.
592 //
593 Status = PeiProcessFile (
594 EFI_SECTION_PE32,
595 FfsHeader,
596 &Pe32Data,
597 NULL
598 );
599
600 if (EFI_ERROR (Status)) {
601 return Status;
602 }
603 //
604 // Load the PE image from the FFS file
605 //
606 Status = PeiLoadFile (
607 PeiEfiPeiPeCoffLoader,
608 Pe32Data,
609 ImageAddress,
610 ImageSize,
611 EntryPoint
612 );
613
614 return Status;
615 }
616
617 EFI_STATUS
618 PeiProcessFile (
619 IN UINT16 SectionType,
620 IN EFI_FFS_FILE_HEADER *FfsFileHeader,
621 OUT VOID **Pe32Data,
622 IN EFI_PEI_HOB_POINTERS *OrigHob
623 )
624 /*++
625
626 Routine Description:
627
628 Arguments:
629
630 SectionType - The type of section in the FFS file to process.
631
632 FfsFileHeader - Pointer to the FFS file to process, looking for the
633 specified SectionType
634
635 Pe32Data - returned pointer to the start of the PE32 image found
636 in the FFS file.
637
638 Returns:
639
640 EFI_SUCCESS - found the PE32 section in the FFS file
641
642 --*/
643 {
644 EFI_STATUS Status;
645 VOID *SectionData;
646 DECOMPRESS_LIBRARY *DecompressLibrary;
647 UINT8 *DstBuffer;
648 UINT8 *ScratchBuffer;
649 UINT32 DstBufferSize;
650 UINT32 ScratchBufferSize;
651 EFI_COMMON_SECTION_HEADER *CmpSection;
652 UINTN CmpSectionLength;
653 UINTN OccupiedCmpSectionLength;
654 VOID *CmpFileData;
655 UINTN CmpFileSize;
656 EFI_COMMON_SECTION_HEADER *Section;
657 UINTN SectionLength;
658 UINTN OccupiedSectionLength;
659 UINT64 FileSize;
660 EFI_GUID_DEFINED_SECTION *GuidedSectionHeader;
661 UINT32 AuthenticationStatus;
662 EFI_PEI_SECTION_EXTRACTION_PPI *SectionExtract;
663 UINT32 BufferSize;
664 UINT8 *Buffer;
665 EFI_PEI_SECURITY_PPI *Security;
666 BOOLEAN StartCrisisRecovery;
667 EFI_GUID TempGuid;
668 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
669 EFI_COMPRESSION_SECTION *CompressionSection;
670 UINT32 FvAlignment;
671
672 //
673 // Initialize local variables.
674 //
675 DecompressLibrary = NULL;
676 DstBuffer = NULL;
677 DstBufferSize = 0;
678
679 Status = PeiServicesFfsFindSectionData (
680 EFI_SECTION_COMPRESSION,
681 FfsFileHeader,
682 &SectionData
683 );
684
685 //
686 // First process the compression section
687 //
688 if (!EFI_ERROR (Status)) {
689 //
690 // Yes, there is a compression section, so extract the contents
691 // Decompress the image here
692 //
693 Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER));
694
695 do {
696 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;
697 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
698
699 //
700 // Was the DXE Core file encapsulated in a GUID'd section?
701 //
702 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
703 //
704 // Locate the GUID'd Section Extractor
705 //
706 GuidedSectionHeader = (VOID *) (Section + 1);
707
708 //
709 // This following code constitutes the addition of the security model
710 // to the DXE IPL.
711 //
712 //
713 // Set a default authenticatino state
714 //
715 AuthenticationStatus = 0;
716
717 Status = PeiServicesLocatePpi (
718 &gEfiPeiSectionExtractionPpiGuid,
719 0,
720 NULL,
721 (VOID **)&SectionExtract
722 );
723
724 if (EFI_ERROR (Status)) {
725 return Status;
726 }
727 //
728 // Verify Authentication State
729 //
730 CopyMem (&TempGuid, Section + 1, sizeof (EFI_GUID));
731
732 Status = SectionExtract->PeiGetSection (
733 GetPeiServicesTablePointer(),
734 SectionExtract,
735 (EFI_SECTION_TYPE *) &SectionType,
736 &TempGuid,
737 0,
738 (VOID **) &Buffer,
739 &BufferSize,
740 &AuthenticationStatus
741 );
742
743 if (EFI_ERROR (Status)) {
744 return Status;
745 }
746 //
747 // If not ask the Security PPI, if exists, for disposition
748 //
749 //
750 Status = PeiServicesLocatePpi (
751 &gEfiPeiSecurityPpiGuid,
752 0,
753 NULL,
754 (VOID **)&Security
755 );
756 if (EFI_ERROR (Status)) {
757 return Status;
758 }
759
760 Status = Security->AuthenticationState (
761 GetPeiServicesTablePointer(),
762 (struct _EFI_PEI_SECURITY_PPI *) Security,
763 AuthenticationStatus,
764 FfsFileHeader,
765 &StartCrisisRecovery
766 );
767
768 if (EFI_ERROR (Status)) {
769 return Status;
770 }
771 //
772 // If there is a security violation, report to caller and have
773 // the upper-level logic possible engender a crisis recovery
774 //
775 if (StartCrisisRecovery) {
776 return EFI_SECURITY_VIOLATION;
777 }
778 }
779
780 if (Section->Type == EFI_SECTION_PE32) {
781 //
782 // This is what we want
783 //
784 *Pe32Data = (VOID *) (Section + 1);
785 return EFI_SUCCESS;
786 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
787 //
788 // This is a compression set, expand it
789 //
790 CompressionSection = (EFI_COMPRESSION_SECTION *) Section;
791
792 switch (CompressionSection->CompressionType) {
793 case EFI_STANDARD_COMPRESSION:
794 //
795 // Load EFI standard compression.
796 //
797 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {
798 DecompressLibrary = &gEfiDecompress;
799 } else {
800 ASSERT (FALSE);
801 return EFI_NOT_FOUND;
802 }
803 break;
804
805 case EFI_CUSTOMIZED_COMPRESSION:
806 //
807 // Load user customized compression.
808 //
809 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {
810 DecompressLibrary = &gCustomDecompress;
811 } else {
812 ASSERT (FALSE);
813 return EFI_NOT_FOUND;
814 }
815 break;
816
817 case EFI_NOT_COMPRESSED:
818 //
819 // Allocate destination buffer
820 //
821 DstBufferSize = CompressionSection->UncompressedLength;
822 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
823 if (DstBuffer == NULL) {
824 return EFI_OUT_OF_RESOURCES;
825 }
826 //
827 // stream is not actually compressed, just encapsulated. So just copy it.
828 //
829 CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize);
830 break;
831
832 default:
833 //
834 // Don't support other unknown compression type.
835 //
836 ASSERT_EFI_ERROR (Status);
837 return EFI_NOT_FOUND;
838 }
839
840 if (CompressionSection->CompressionType != EFI_NOT_COMPRESSED) {
841 //
842 // For compressed data, decompress them to dstbuffer.
843 //
844 Status = DecompressLibrary->GetInfo (
845 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
846 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),
847 &DstBufferSize,
848 &ScratchBufferSize
849 );
850 if (EFI_ERROR (Status)) {
851 //
852 // GetInfo failed
853 //
854 return EFI_NOT_FOUND;
855 }
856
857 //
858 // Allocate scratch buffer
859 //
860 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
861 if (ScratchBuffer == NULL) {
862 return EFI_OUT_OF_RESOURCES;
863 }
864
865 //
866 // Allocate destination buffer
867 //
868 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
869 if (DstBuffer == NULL) {
870 return EFI_OUT_OF_RESOURCES;
871 }
872
873 //
874 // Call decompress function
875 //
876 Status = DecompressLibrary->Decompress (
877 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
878 DstBuffer,
879 ScratchBuffer
880 );
881 }
882
883 CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer;
884 if (CmpSection->Type == 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 // Adjust Fv Base Address Alignment based on Align Attributes in Fv Header
894 //
895
896 //
897 // When FvImage support Alignment, we need to check whether
898 // its alignment is correct.
899 //
900 if (FvHeader->Attributes | EFI_FVB_ALIGNMENT_CAP) {
901
902 //
903 // Calculate the mini alignment for this FvImage
904 //
905 FvAlignment = 1 << (LowBitSet32 (FvHeader->Attributes >> 16) + 1);
906
907 //
908 // If current FvImage base address doesn't meet the its alignment,
909 // we need to reload this FvImage to another correct memory address.
910 //
911 if (((UINTN) FvHeader % FvAlignment) != 0) {
912 DstBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN) FvHeader->FvLength), FvAlignment);
913 if (DstBuffer == NULL) {
914 return EFI_OUT_OF_RESOURCES;
915 }
916 CopyMem (DstBuffer, FvHeader, (UINTN) FvHeader->FvLength);
917 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer;
918 }
919 }
920 //
921 // Build new FvHob for new decompressed Fv image.
922 //
923 BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength);
924
925 //
926 // Set the original FvHob to unused.
927 //
928 if (OrigHob != NULL) {
929 OrigHob->Header->HobType = EFI_HOB_TYPE_UNUSED;
930 }
931
932 //
933 // when search FvImage Section return true.
934 //
935 if (SectionType == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {
936 *Pe32Data = (VOID *) FvHeader;
937 return EFI_SUCCESS;
938 } else {
939 return EFI_NOT_FOUND;
940 }
941 }
942 }
943 //
944 // Decompress successfully.
945 // Loop the decompressed data searching for expected section.
946 //
947 CmpFileData = (VOID *) DstBuffer;
948 CmpFileSize = DstBufferSize;
949 do {
950 CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff;
951 if (CmpSection->Type == EFI_SECTION_PE32) {
952 //
953 // This is what we want
954 //
955 *Pe32Data = (VOID *) (CmpSection + 1);
956 return EFI_SUCCESS;
957 }
958
959 OccupiedCmpSectionLength = GET_OCCUPIED_SIZE (CmpSectionLength, 4);
960 CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength);
961 } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize);
962 }
963 //
964 // End of the decompression activity
965 //
966
967 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);
968 FileSize = FfsFileHeader->Size[0] & 0xFF;
969 FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00;
970 FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000;
971 FileSize &= 0x00FFFFFF;
972 } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section - (UINT8 *) FfsFileHeader) < FileSize);
973
974 //
975 // search all sections (compression and non compression) in this FFS, don't
976 // find expected section.
977 //
978 return EFI_NOT_FOUND;
979 } else {
980 //
981 // For those FFS that doesn't contain compression section, directly search
982 // PE or TE section in this FFS.
983 //
984
985 Status = PeiServicesFfsFindSectionData (
986 EFI_SECTION_PE32,
987 FfsFileHeader,
988 &SectionData
989 );
990
991 if (EFI_ERROR (Status)) {
992 Status = PeiServicesFfsFindSectionData (
993 EFI_SECTION_TE,
994 FfsFileHeader,
995 &SectionData
996 );
997 if (EFI_ERROR (Status)) {
998 return Status;
999 }
1000 }
1001 }
1002
1003 *Pe32Data = SectionData;
1004
1005 return EFI_SUCCESS;
1006 }
1007