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