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