3 Responsibility of this module is to load the DXE Core from a Firmware Volume.
5 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // Module Globals used in the DXE to PEI hand off
21 // These must be module globals, so the stack can be switched
23 CONST EFI_DXE_IPL_PPI mDxeIplPpi
= {
27 CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi
= {
28 CustomGuidedSectionExtract
31 CONST EFI_PEI_DECOMPRESS_PPI mDecompressPpi
= {
35 CONST EFI_PEI_PPI_DESCRIPTOR mPpiList
[] = {
37 EFI_PEI_PPI_DESCRIPTOR_PPI
,
42 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
43 &gEfiPeiDecompressPpiGuid
,
44 (VOID
*) &mDecompressPpi
48 CONST EFI_PEI_PPI_DESCRIPTOR gEndOfPeiSignalPpi
= {
49 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
50 &gEfiEndOfPeiSignalPpiGuid
,
55 Entry point of DXE IPL PEIM.
57 This function installs DXE IPL PPI and Decompress PPI. It also reloads
58 itself to memory on non-S3 resume boot path.
60 @param FileHandle Handle of the file being invoked.
61 @param PeiServices Describes the list of possible PEI Services.
63 @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully.
64 @retval Others Some error occurs during the execution of this function.
69 PeimInitializeDxeIpl (
70 IN EFI_PEI_FILE_HANDLE FileHandle
,
71 IN CONST EFI_PEI_SERVICES
**PeiServices
75 EFI_BOOT_MODE BootMode
;
76 EFI_GUID
*ExtractHandlerGuidTable
;
77 UINTN ExtractHandlerNumber
;
78 EFI_PEI_PPI_DESCRIPTOR
*GuidPpi
;
80 BootMode
= GetBootModeHob ();
82 if (BootMode
!= BOOT_ON_S3_RESUME
) {
83 Status
= PeiServicesRegisterForShadow (FileHandle
);
84 if (Status
== EFI_SUCCESS
) {
86 // EFI_SUCESS means it is the first time to call register for shadow.
92 // Ensure that DXE IPL is shadowed to permanent memory.
94 ASSERT (Status
== EFI_ALREADY_STARTED
);
97 // Get custom extract guided section method guid list
99 ExtractHandlerNumber
= ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable
);
102 // Install custom extraction guid PPI
104 if (ExtractHandlerNumber
> 0) {
105 GuidPpi
= (EFI_PEI_PPI_DESCRIPTOR
*) AllocatePool (ExtractHandlerNumber
* sizeof (EFI_PEI_PPI_DESCRIPTOR
));
106 ASSERT (GuidPpi
!= NULL
);
107 while (ExtractHandlerNumber
-- > 0) {
108 GuidPpi
->Flags
= EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
;
109 GuidPpi
->Ppi
= (VOID
*) &mCustomGuidedSectionExtractionPpi
;
110 GuidPpi
->Guid
= &ExtractHandlerGuidTable
[ExtractHandlerNumber
];
111 Status
= PeiServicesInstallPpi (GuidPpi
++);
112 ASSERT_EFI_ERROR(Status
);
119 // Install DxeIpl and Decompress PPIs.
121 Status
= PeiServicesInstallPpi (mPpiList
);
122 ASSERT_EFI_ERROR(Status
);
128 Validate variable data for the MemoryTypeInformation.
130 @param MemoryData Variable data.
131 @param MemoryDataSize Variable data length.
133 @return TRUE The variable data is valid.
134 @return FALSE The variable data is invalid.
138 ValidateMemoryTypeInfoVariable (
139 IN EFI_MEMORY_TYPE_INFORMATION
*MemoryData
,
140 IN UINTN MemoryDataSize
146 // Check the input parameter.
147 if (MemoryData
== NULL
) {
152 Count
= MemoryDataSize
/ sizeof (*MemoryData
);
155 if (Count
* sizeof(*MemoryData
) != MemoryDataSize
) {
159 // Check last entry type filed.
160 if (MemoryData
[Count
- 1].Type
!= EfiMaxMemoryType
) {
164 // Check the type filed.
165 for (Index
= 0; Index
< Count
- 1; Index
++) {
166 if (MemoryData
[Index
].Type
>= EfiMaxMemoryType
) {
175 Main entry point to last PEIM.
177 This function finds DXE Core in the firmware volume and transfer the control to
180 @param This Entry point for DXE IPL PPI.
181 @param PeiServices General purpose services available to every PEIM.
182 @param HobList Address to the Pei HOB list.
184 @return EFI_SUCCESS DXE core was successfully loaded.
185 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.
191 IN CONST EFI_DXE_IPL_PPI
*This
,
192 IN EFI_PEI_SERVICES
**PeiServices
,
193 IN EFI_PEI_HOB_POINTERS HobList
197 EFI_FV_FILE_INFO DxeCoreFileInfo
;
198 EFI_PHYSICAL_ADDRESS DxeCoreAddress
;
200 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint
;
201 EFI_BOOT_MODE BootMode
;
202 EFI_PEI_FILE_HANDLE FileHandle
;
203 EFI_PEI_READ_ONLY_VARIABLE2_PPI
*Variable
;
204 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
206 UINT32 AuthenticationState
;
208 EFI_PEI_S3_RESUME2_PPI
*S3Resume
;
209 EFI_PEI_RECOVERY_MODULE_PPI
*PeiRecovery
;
210 EFI_MEMORY_TYPE_INFORMATION MemoryData
[EfiMaxMemoryType
+ 1];
213 // if in S3 Resume, restore configure
215 BootMode
= GetBootModeHob ();
217 if (BootMode
== BOOT_ON_S3_RESUME
) {
218 Status
= PeiServicesLocatePpi (
219 &gEfiPeiS3Resume2PpiGuid
,
224 ASSERT_EFI_ERROR (Status
);
226 Status
= S3Resume
->S3RestoreConfig2 (S3Resume
);
227 ASSERT_EFI_ERROR (Status
);
228 } else if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
229 Status
= PeiServicesLocatePpi (
230 &gEfiPeiRecoveryModulePpiGuid
,
233 (VOID
**) &PeiRecovery
235 ASSERT_EFI_ERROR (Status
);
237 Status
= PeiRecovery
->LoadRecoveryCapsule (PeiServices
, PeiRecovery
);
238 if (EFI_ERROR (Status
)) {
239 DEBUG ((DEBUG_ERROR
, "Load Recovery Capsule Failed.(Status = %r)\n", Status
));
244 // Now should have a HOB with the DXE core
248 Status
= PeiServicesLocatePpi (
249 &gEfiPeiReadOnlyVariable2PpiGuid
,
254 if (!EFI_ERROR (Status
)) {
255 DataSize
= sizeof (MemoryData
);
256 Status
= Variable
->GetVariable (
258 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
259 &gEfiMemoryTypeInformationGuid
,
264 if (!EFI_ERROR (Status
) && ValidateMemoryTypeInfoVariable(MemoryData
, DataSize
)) {
266 // Build the GUID'd HOB for DXE
269 &gEfiMemoryTypeInformationGuid
,
277 // Look in all the FVs present in PEI and find the DXE Core FileHandle
279 FileHandle
= DxeIplFindDxeCore ();
282 // Load the DXE Core from a Firmware Volume.
286 Status
= PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid
, Instance
++, NULL
, (VOID
**) &LoadFile
);
288 // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
290 ASSERT_EFI_ERROR (Status
);
292 Status
= LoadFile
->LoadFile (
300 } while (EFI_ERROR (Status
));
303 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
305 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &DxeCoreFileInfo
);
306 ASSERT_EFI_ERROR (Status
);
309 // Add HOB for the DXE Core
312 &DxeCoreFileInfo
.FileName
,
314 ALIGN_VALUE (DxeCoreSize
, EFI_PAGE_SIZE
),
319 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
321 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
));
323 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID
*)(UINTN
)DxeCoreAddress
, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint
)));
326 // Transfer control to the DXE Core
327 // The hand off state is simply a pointer to the HOB list
329 HandOffToDxeCore (DxeCoreEntryPoint
, HobList
);
331 // If we get here, then the DXE Core returned. This is an error
332 // DxeCore should not return.
337 return EFI_OUT_OF_RESOURCES
;
342 Searches DxeCore in all firmware Volumes and loads the first
343 instance that contains DxeCore.
345 @return FileHandle of DxeCore to load DxeCore.
355 EFI_PEI_FV_HANDLE VolumeHandle
;
356 EFI_PEI_FILE_HANDLE FileHandle
;
361 // Traverse all firmware volume instances
363 Status
= PeiServicesFfsFindNextVolume (Instance
, &VolumeHandle
);
365 // If some error occurs here, then we cannot find any firmware
366 // volume that may contain DxeCore.
368 if (EFI_ERROR (Status
)) {
369 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_CORE_EC_DXE_CORRUPT
));
371 ASSERT_EFI_ERROR (Status
);
374 // Find the DxeCore file type from the beginning in this firmware volume.
377 Status
= PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE
, VolumeHandle
, &FileHandle
);
378 if (!EFI_ERROR (Status
)) {
380 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
381 // return the FileHandle.
386 // We cannot find DxeCore in this firmware volume, then search the next volume.
395 The ExtractSection() function processes the input section and
396 returns a pointer to the section contents. If the section being
397 extracted does not require processing (if the section
398 GuidedSectionHeader.Attributes has the
399 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
400 OutputBuffer is just updated to point to the start of the
401 section's contents. Otherwise, *Buffer must be allocated
402 from PEI permanent memory.
404 @param This Indicates the
405 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
406 Buffer containing the input GUIDed section to be
407 processed. OutputBuffer OutputBuffer is
408 allocated from PEI permanent memory and contains
409 the new section stream.
410 @param InputSection A pointer to the input buffer, which contains
411 the input section to be processed.
412 @param OutputBuffer A pointer to a caller-allocated buffer, whose
413 size is specified by the contents of OutputSize.
414 @param OutputSize A pointer to a caller-allocated
415 UINTN in which the size of *OutputBuffer
416 allocation is stored. If the function
417 returns anything other than EFI_SUCCESS,
418 the value of OutputSize is undefined.
419 @param AuthenticationStatus A pointer to a caller-allocated
420 UINT32 that indicates the
421 authentication status of the
422 output buffer. If the input
423 section's GuidedSectionHeader.
424 Attributes field has the
425 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
427 AuthenticationStatus must return
428 zero. These bits reflect the
429 status of the extraction
430 operation. If the function
431 returns anything other than
432 EFI_SUCCESS, the value of
433 AuthenticationStatus is
436 @retval EFI_SUCCESS The InputSection was
437 successfully processed and the
438 section contents were returned.
440 @retval EFI_OUT_OF_RESOURCES The system has insufficient
441 resources to process the request.
443 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
444 not match this instance of the
445 GUIDed Section Extraction PPI.
450 CustomGuidedSectionExtract (
451 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
452 IN CONST VOID
*InputSection
,
453 OUT VOID
**OutputBuffer
,
454 OUT UINTN
*OutputSize
,
455 OUT UINT32
*AuthenticationStatus
459 UINT8
*ScratchBuffer
;
460 UINT32 ScratchBufferSize
;
461 UINT32 OutputBufferSize
;
462 UINT16 SectionAttribute
;
465 // Init local variable
467 ScratchBuffer
= NULL
;
470 // Call GetInfo to get the size and attribute of input guided section data.
472 Status
= ExtractGuidedSectionGetInfo (
479 if (EFI_ERROR (Status
)) {
480 DEBUG ((DEBUG_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
484 if (ScratchBufferSize
!= 0) {
486 // Allocate scratch buffer
488 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
489 if (ScratchBuffer
== NULL
) {
490 return EFI_OUT_OF_RESOURCES
;
494 if (((SectionAttribute
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) && OutputBufferSize
> 0) {
496 // Allocate output buffer
498 *OutputBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize
) + 1);
499 if (*OutputBuffer
== NULL
) {
500 return EFI_OUT_OF_RESOURCES
;
502 DEBUG ((DEBUG_INFO
, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize
, *OutputBuffer
));
504 // *OutputBuffer still is one section. Adjust *OutputBuffer offset,
505 // skip EFI section header to make section data at page alignment.
507 *OutputBuffer
= (VOID
*)((UINT8
*) *OutputBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
));
510 Status
= ExtractGuidedSectionDecode (
516 if (EFI_ERROR (Status
)) {
520 DEBUG ((DEBUG_ERROR
, "Extract guided section Failed - %r\n", Status
));
524 *OutputSize
= (UINTN
) OutputBufferSize
;
532 Decompresses a section to the output buffer.
534 This function looks up the compression type field in the input section and
535 applies the appropriate compression algorithm to compress the section to a
536 callee allocated buffer.
538 @param This Points to this instance of the
539 EFI_PEI_DECOMPRESS_PEI PPI.
540 @param CompressionSection Points to the compressed section.
541 @param OutputBuffer Holds the returned pointer to the decompressed
543 @param OutputSize Holds the returned size of the decompress
546 @retval EFI_SUCCESS The section was decompressed successfully.
547 OutputBuffer contains the resulting data and
548 OutputSize contains the resulting size.
554 IN CONST EFI_PEI_DECOMPRESS_PPI
*This
,
555 IN CONST EFI_COMPRESSION_SECTION
*CompressionSection
,
556 OUT VOID
**OutputBuffer
,
557 OUT UINTN
*OutputSize
562 UINT8
*ScratchBuffer
;
563 UINT32 DstBufferSize
;
564 UINT32 ScratchBufferSize
;
565 VOID
*CompressionSource
;
566 UINT32 CompressionSourceSize
;
567 UINT32 UncompressedLength
;
568 UINT8 CompressionType
;
570 if (CompressionSection
->CommonHeader
.Type
!= EFI_SECTION_COMPRESSION
) {
572 return EFI_INVALID_PARAMETER
;
575 if (IS_SECTION2 (CompressionSection
)) {
576 CompressionSource
= (VOID
*) ((UINT8
*) CompressionSection
+ sizeof (EFI_COMPRESSION_SECTION2
));
577 CompressionSourceSize
= (UINT32
) (SECTION2_SIZE (CompressionSection
) - sizeof (EFI_COMPRESSION_SECTION2
));
578 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*) CompressionSection
)->UncompressedLength
;
579 CompressionType
= ((EFI_COMPRESSION_SECTION2
*) CompressionSection
)->CompressionType
;
581 CompressionSource
= (VOID
*) ((UINT8
*) CompressionSection
+ sizeof (EFI_COMPRESSION_SECTION
));
582 CompressionSourceSize
= (UINT32
) (SECTION_SIZE (CompressionSection
) - sizeof (EFI_COMPRESSION_SECTION
));
583 UncompressedLength
= CompressionSection
->UncompressedLength
;
584 CompressionType
= CompressionSection
->CompressionType
;
588 // This is a compression set, expand it
590 switch (CompressionType
) {
591 case EFI_STANDARD_COMPRESSION
:
592 if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress
)) {
594 // Load EFI standard compression.
595 // For compressed data, decompress them to destination buffer.
597 Status
= UefiDecompressGetInfo (
599 CompressionSourceSize
,
603 if (EFI_ERROR (Status
)) {
607 DEBUG ((DEBUG_ERROR
, "Decompress GetInfo Failed - %r\n", Status
));
608 return EFI_NOT_FOUND
;
611 // Allocate scratch buffer
613 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
614 if (ScratchBuffer
== NULL
) {
615 return EFI_OUT_OF_RESOURCES
;
618 // Allocate destination buffer, extra one page for adjustment
620 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
621 if (DstBuffer
== NULL
) {
622 return EFI_OUT_OF_RESOURCES
;
625 // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
626 // to make section data at page alignment.
628 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
630 // Call decompress function
632 Status
= UefiDecompress (
637 if (EFI_ERROR (Status
)) {
641 DEBUG ((DEBUG_ERROR
, "Decompress Failed - %r\n", Status
));
642 return EFI_NOT_FOUND
;
647 // PcdDxeIplSupportUefiDecompress is FALSE
648 // Don't support UEFI decompression algorithm.
651 return EFI_NOT_FOUND
;
654 case EFI_NOT_COMPRESSED
:
656 // Allocate destination buffer
658 DstBufferSize
= UncompressedLength
;
659 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
660 if (DstBuffer
== NULL
) {
661 return EFI_OUT_OF_RESOURCES
;
664 // Adjust DstBuffer offset, skip EFI section header
665 // to make section data at page alignment.
667 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
669 // stream is not actually compressed, just encapsulated. So just copy it.
671 CopyMem (DstBuffer
, CompressionSource
, DstBufferSize
);
676 // Don't support other unknown compression type.
679 return EFI_NOT_FOUND
;
682 *OutputSize
= DstBufferSize
;
683 *OutputBuffer
= DstBuffer
;
690 Updates the Stack HOB passed to DXE phase.
692 This function traverses the whole HOB list and update the stack HOB to
693 reflect the real stack that is used by DXE core.
695 @param BaseAddress The lower address of stack used by DxeCore.
696 @param Length The length of stack used by DxeCore.
701 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
705 EFI_PEI_HOB_POINTERS Hob
;
707 Hob
.Raw
= GetHobList ();
708 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
709 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
711 // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to
712 // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some
713 // PEIMs may also keep key information on stack
715 BuildMemoryAllocationHob (
716 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
717 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
721 // Update the BSP Stack Hob to reflect the new stack info.
723 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
724 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
727 Hob
.Raw
= GET_NEXT_HOB (Hob
);