3 Responsibility of this module is to load the DXE Core from a Firmware Volume.
5 Copyright (c) 2006 - 2012, 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
);
98 // Get custom extract guided section method guid list
100 ExtractHandlerNumber
= ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable
);
103 // Install custom extraction guid PPI
105 if (ExtractHandlerNumber
> 0) {
106 GuidPpi
= (EFI_PEI_PPI_DESCRIPTOR
*) AllocatePool (ExtractHandlerNumber
* sizeof (EFI_PEI_PPI_DESCRIPTOR
));
107 ASSERT (GuidPpi
!= NULL
);
108 while (ExtractHandlerNumber
-- > 0) {
109 GuidPpi
->Flags
= EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
;
110 GuidPpi
->Ppi
= (VOID
*) &mCustomGuidedSectionExtractionPpi
;
111 GuidPpi
->Guid
= &ExtractHandlerGuidTable
[ExtractHandlerNumber
];
112 Status
= PeiServicesInstallPpi (GuidPpi
++);
113 ASSERT_EFI_ERROR(Status
);
118 // Install DxeIpl and Decompress PPIs.
120 Status
= PeiServicesInstallPpi (mPpiList
);
121 ASSERT_EFI_ERROR(Status
);
127 Validate variable data for the MemoryTypeInformation.
129 @param MemoryData Variable data.
130 @param MemoryDataSize Variable data length.
132 @return TRUE The variable data is valid.
133 @return FALSE The variable data is invalid.
137 ValidateMemoryTypeInfoVariable (
138 IN EFI_MEMORY_TYPE_INFORMATION
*MemoryData
,
139 IN UINTN MemoryDataSize
145 // Check the input parameter.
146 if (MemoryData
== NULL
) {
151 Count
= MemoryDataSize
/ sizeof (*MemoryData
);
154 if (Count
* sizeof(*MemoryData
) != MemoryDataSize
) {
158 // Check last entry type filed.
159 if (MemoryData
[Count
- 1].Type
!= EfiMaxMemoryType
) {
163 // Check the type filed.
164 for (Index
= 0; Index
< Count
- 1; Index
++) {
165 if (MemoryData
[Index
].Type
>= EfiMaxMemoryType
) {
174 Main entry point to last PEIM.
176 This function finds DXE Core in the firmware volume and transfer the control to
179 @param This Entry point for DXE IPL PPI.
180 @param PeiServices General purpose services available to every PEIM.
181 @param HobList Address to the Pei HOB list.
183 @return EFI_SUCCESS DXE core was successfully loaded.
184 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.
190 IN CONST EFI_DXE_IPL_PPI
*This
,
191 IN EFI_PEI_SERVICES
**PeiServices
,
192 IN EFI_PEI_HOB_POINTERS HobList
196 EFI_FV_FILE_INFO DxeCoreFileInfo
;
197 EFI_PHYSICAL_ADDRESS DxeCoreAddress
;
199 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint
;
200 EFI_BOOT_MODE BootMode
;
201 EFI_PEI_FILE_HANDLE FileHandle
;
202 EFI_PEI_READ_ONLY_VARIABLE2_PPI
*Variable
;
203 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
205 UINT32 AuthenticationState
;
207 EFI_PEI_S3_RESUME2_PPI
*S3Resume
;
208 EFI_PEI_RECOVERY_MODULE_PPI
*PeiRecovery
;
209 EFI_MEMORY_TYPE_INFORMATION MemoryData
[EfiMaxMemoryType
+ 1];
212 // if in S3 Resume, restore configure
214 BootMode
= GetBootModeHob ();
216 if (BootMode
== BOOT_ON_S3_RESUME
) {
217 Status
= PeiServicesLocatePpi (
218 &gEfiPeiS3Resume2PpiGuid
,
223 if (EFI_ERROR (Status
)) {
225 // Report Status code that S3Resume PPI can not be found
228 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
229 (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_EC_S3_RESUME_PPI_NOT_FOUND
)
232 ASSERT_EFI_ERROR (Status
);
234 Status
= S3Resume
->S3RestoreConfig2 (S3Resume
);
235 ASSERT_EFI_ERROR (Status
);
236 } else if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
237 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_PC_RECOVERY_BEGIN
));
238 Status
= PeiServicesLocatePpi (
239 &gEfiPeiRecoveryModulePpiGuid
,
242 (VOID
**) &PeiRecovery
245 if (EFI_ERROR (Status
)) {
246 DEBUG ((DEBUG_ERROR
, "Locate Recovery PPI Failed.(Status = %r)\n", Status
));
248 // Report Status code the failure of locating Recovery PPI
251 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
252 (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_EC_RECOVERY_PPI_NOT_FOUND
)
257 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_PC_CAPSULE_LOAD
));
258 Status
= PeiRecovery
->LoadRecoveryCapsule (PeiServices
, PeiRecovery
);
259 if (EFI_ERROR (Status
)) {
260 DEBUG ((DEBUG_ERROR
, "Load Recovery Capsule Failed.(Status = %r)\n", Status
));
262 // Report Status code that recovery image can not be found
265 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
266 (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_EC_NO_RECOVERY_CAPSULE
)
270 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_PC_CAPSULE_START
));
272 // Now should have a HOB with the DXE core
276 Status
= PeiServicesLocatePpi (
277 &gEfiPeiReadOnlyVariable2PpiGuid
,
282 if (!EFI_ERROR (Status
)) {
283 DataSize
= sizeof (MemoryData
);
284 Status
= Variable
->GetVariable (
286 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
287 &gEfiMemoryTypeInformationGuid
,
292 if (!EFI_ERROR (Status
) && ValidateMemoryTypeInfoVariable(MemoryData
, DataSize
)) {
294 // Build the GUID'd HOB for DXE
297 &gEfiMemoryTypeInformationGuid
,
305 // Look in all the FVs present in PEI and find the DXE Core FileHandle
307 FileHandle
= DxeIplFindDxeCore ();
310 // Load the DXE Core from a Firmware Volume.
314 Status
= PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid
, Instance
++, NULL
, (VOID
**) &LoadFile
);
316 // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
318 ASSERT_EFI_ERROR (Status
);
320 Status
= LoadFile
->LoadFile (
328 } while (EFI_ERROR (Status
));
331 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
333 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &DxeCoreFileInfo
);
334 ASSERT_EFI_ERROR (Status
);
337 // Add HOB for the DXE Core
340 &DxeCoreFileInfo
.FileName
,
342 ALIGN_VALUE (DxeCoreSize
, EFI_PAGE_SIZE
),
347 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
349 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
));
351 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID
*)(UINTN
)DxeCoreAddress
, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint
)));
354 // Transfer control to the DXE Core
355 // The hand off state is simply a pointer to the HOB list
357 HandOffToDxeCore (DxeCoreEntryPoint
, HobList
);
359 // If we get here, then the DXE Core returned. This is an error
360 // DxeCore should not return.
365 return EFI_OUT_OF_RESOURCES
;
370 Searches DxeCore in all firmware Volumes and loads the first
371 instance that contains DxeCore.
373 @return FileHandle of DxeCore to load DxeCore.
383 EFI_PEI_FV_HANDLE VolumeHandle
;
384 EFI_PEI_FILE_HANDLE FileHandle
;
389 // Traverse all firmware volume instances
391 Status
= PeiServicesFfsFindNextVolume (Instance
, &VolumeHandle
);
393 // If some error occurs here, then we cannot find any firmware
394 // volume that may contain DxeCore.
396 if (EFI_ERROR (Status
)) {
397 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_CORE_EC_DXE_CORRUPT
));
399 ASSERT_EFI_ERROR (Status
);
402 // Find the DxeCore file type from the beginning in this firmware volume.
405 Status
= PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE
, VolumeHandle
, &FileHandle
);
406 if (!EFI_ERROR (Status
)) {
408 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
409 // return the FileHandle.
414 // We cannot find DxeCore in this firmware volume, then search the next volume.
423 The ExtractSection() function processes the input section and
424 returns a pointer to the section contents. If the section being
425 extracted does not require processing (if the section
426 GuidedSectionHeader.Attributes has the
427 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
428 OutputBuffer is just updated to point to the start of the
429 section's contents. Otherwise, *Buffer must be allocated
430 from PEI permanent memory.
432 @param This Indicates the
433 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
434 Buffer containing the input GUIDed section to be
435 processed. OutputBuffer OutputBuffer is
436 allocated from PEI permanent memory and contains
437 the new section stream.
438 @param InputSection A pointer to the input buffer, which contains
439 the input section to be processed.
440 @param OutputBuffer A pointer to a caller-allocated buffer, whose
441 size is specified by the contents of OutputSize.
442 @param OutputSize A pointer to a caller-allocated
443 UINTN in which the size of *OutputBuffer
444 allocation is stored. If the function
445 returns anything other than EFI_SUCCESS,
446 the value of OutputSize is undefined.
447 @param AuthenticationStatus A pointer to a caller-allocated
448 UINT32 that indicates the
449 authentication status of the
450 output buffer. If the input
451 section's GuidedSectionHeader.
452 Attributes field has the
453 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
455 AuthenticationStatus must return
456 zero. These bits reflect the
457 status of the extraction
458 operation. If the function
459 returns anything other than
460 EFI_SUCCESS, the value of
461 AuthenticationStatus is
464 @retval EFI_SUCCESS The InputSection was
465 successfully processed and the
466 section contents were returned.
468 @retval EFI_OUT_OF_RESOURCES The system has insufficient
469 resources to process the request.
471 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
472 not match this instance of the
473 GUIDed Section Extraction PPI.
478 CustomGuidedSectionExtract (
479 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
480 IN CONST VOID
*InputSection
,
481 OUT VOID
**OutputBuffer
,
482 OUT UINTN
*OutputSize
,
483 OUT UINT32
*AuthenticationStatus
487 UINT8
*ScratchBuffer
;
488 UINT32 ScratchBufferSize
;
489 UINT32 OutputBufferSize
;
490 UINT16 SectionAttribute
;
493 // Init local variable
495 ScratchBuffer
= NULL
;
498 // Call GetInfo to get the size and attribute of input guided section data.
500 Status
= ExtractGuidedSectionGetInfo (
507 if (EFI_ERROR (Status
)) {
508 DEBUG ((DEBUG_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
512 if (ScratchBufferSize
!= 0) {
514 // Allocate scratch buffer
516 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
517 if (ScratchBuffer
== NULL
) {
518 return EFI_OUT_OF_RESOURCES
;
522 if (((SectionAttribute
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) && OutputBufferSize
> 0) {
524 // Allocate output buffer
526 *OutputBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize
) + 1);
527 if (*OutputBuffer
== NULL
) {
528 return EFI_OUT_OF_RESOURCES
;
530 DEBUG ((DEBUG_INFO
, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize
, *OutputBuffer
));
532 // *OutputBuffer still is one section. Adjust *OutputBuffer offset,
533 // skip EFI section header to make section data at page alignment.
535 *OutputBuffer
= (VOID
*)((UINT8
*) *OutputBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
));
538 Status
= ExtractGuidedSectionDecode (
544 if (EFI_ERROR (Status
)) {
548 DEBUG ((DEBUG_ERROR
, "Extract guided section Failed - %r\n", Status
));
552 *OutputSize
= (UINTN
) OutputBufferSize
;
560 Decompresses a section to the output buffer.
562 This function looks up the compression type field in the input section and
563 applies the appropriate compression algorithm to compress the section to a
564 callee allocated buffer.
566 @param This Points to this instance of the
567 EFI_PEI_DECOMPRESS_PEI PPI.
568 @param CompressionSection Points to the compressed section.
569 @param OutputBuffer Holds the returned pointer to the decompressed
571 @param OutputSize Holds the returned size of the decompress
574 @retval EFI_SUCCESS The section was decompressed successfully.
575 OutputBuffer contains the resulting data and
576 OutputSize contains the resulting size.
582 IN CONST EFI_PEI_DECOMPRESS_PPI
*This
,
583 IN CONST EFI_COMPRESSION_SECTION
*CompressionSection
,
584 OUT VOID
**OutputBuffer
,
585 OUT UINTN
*OutputSize
590 UINT8
*ScratchBuffer
;
591 UINT32 DstBufferSize
;
592 UINT32 ScratchBufferSize
;
593 VOID
*CompressionSource
;
594 UINT32 CompressionSourceSize
;
595 UINT32 UncompressedLength
;
596 UINT8 CompressionType
;
598 if (CompressionSection
->CommonHeader
.Type
!= EFI_SECTION_COMPRESSION
) {
600 return EFI_INVALID_PARAMETER
;
603 if (IS_SECTION2 (CompressionSection
)) {
604 CompressionSource
= (VOID
*) ((UINT8
*) CompressionSection
+ sizeof (EFI_COMPRESSION_SECTION2
));
605 CompressionSourceSize
= (UINT32
) (SECTION2_SIZE (CompressionSection
) - sizeof (EFI_COMPRESSION_SECTION2
));
606 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*) CompressionSection
)->UncompressedLength
;
607 CompressionType
= ((EFI_COMPRESSION_SECTION2
*) CompressionSection
)->CompressionType
;
609 CompressionSource
= (VOID
*) ((UINT8
*) CompressionSection
+ sizeof (EFI_COMPRESSION_SECTION
));
610 CompressionSourceSize
= (UINT32
) (SECTION_SIZE (CompressionSection
) - sizeof (EFI_COMPRESSION_SECTION
));
611 UncompressedLength
= CompressionSection
->UncompressedLength
;
612 CompressionType
= CompressionSection
->CompressionType
;
616 // This is a compression set, expand it
618 switch (CompressionType
) {
619 case EFI_STANDARD_COMPRESSION
:
620 if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress
)) {
622 // Load EFI standard compression.
623 // For compressed data, decompress them to destination buffer.
625 Status
= UefiDecompressGetInfo (
627 CompressionSourceSize
,
631 if (EFI_ERROR (Status
)) {
635 DEBUG ((DEBUG_ERROR
, "Decompress GetInfo Failed - %r\n", Status
));
636 return EFI_NOT_FOUND
;
639 // Allocate scratch buffer
641 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
642 if (ScratchBuffer
== NULL
) {
643 return EFI_OUT_OF_RESOURCES
;
646 // Allocate destination buffer, extra one page for adjustment
648 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
649 if (DstBuffer
== NULL
) {
650 return EFI_OUT_OF_RESOURCES
;
653 // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
654 // to make section data at page alignment.
656 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
658 // Call decompress function
660 Status
= UefiDecompress (
665 if (EFI_ERROR (Status
)) {
669 DEBUG ((DEBUG_ERROR
, "Decompress Failed - %r\n", Status
));
670 return EFI_NOT_FOUND
;
675 // PcdDxeIplSupportUefiDecompress is FALSE
676 // Don't support UEFI decompression algorithm.
679 return EFI_NOT_FOUND
;
682 case EFI_NOT_COMPRESSED
:
684 // Allocate destination buffer
686 DstBufferSize
= UncompressedLength
;
687 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
688 if (DstBuffer
== NULL
) {
689 return EFI_OUT_OF_RESOURCES
;
692 // Adjust DstBuffer offset, skip EFI section header
693 // to make section data at page alignment.
695 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
697 // stream is not actually compressed, just encapsulated. So just copy it.
699 CopyMem (DstBuffer
, CompressionSource
, DstBufferSize
);
704 // Don't support other unknown compression type.
707 return EFI_NOT_FOUND
;
710 *OutputSize
= DstBufferSize
;
711 *OutputBuffer
= DstBuffer
;
718 Updates the Stack HOB passed to DXE phase.
720 This function traverses the whole HOB list and update the stack HOB to
721 reflect the real stack that is used by DXE core.
723 @param BaseAddress The lower address of stack used by DxeCore.
724 @param Length The length of stack used by DxeCore.
729 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
733 EFI_PEI_HOB_POINTERS Hob
;
735 Hob
.Raw
= GetHobList ();
736 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
737 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
739 // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to
740 // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some
741 // PEIMs may also keep key information on stack
743 BuildMemoryAllocationHob (
744 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
745 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
749 // Update the BSP Stack Hob to reflect the new stack info.
751 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
752 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
755 Hob
.Raw
= GET_NEXT_HOB (Hob
);