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