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