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