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 Main entry point to last PEIM.
130 This function finds DXE Core in the firmware volume and transfer the control to
133 @param This Entry point for DXE IPL PPI.
134 @param PeiServices General purpose services available to every PEIM.
135 @param HobList Address to the Pei HOB list.
137 @return EFI_SUCCESS DXE core was successfully loaded.
138 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.
144 IN CONST EFI_DXE_IPL_PPI
*This
,
145 IN EFI_PEI_SERVICES
**PeiServices
,
146 IN EFI_PEI_HOB_POINTERS HobList
150 EFI_FV_FILE_INFO DxeCoreFileInfo
;
151 EFI_PHYSICAL_ADDRESS DxeCoreAddress
;
153 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint
;
154 EFI_BOOT_MODE BootMode
;
155 EFI_PEI_FILE_HANDLE FileHandle
;
156 EFI_PEI_READ_ONLY_VARIABLE2_PPI
*Variable
;
157 EFI_PEI_LOAD_FILE_PPI
*LoadFile
;
159 UINT32 AuthenticationState
;
161 EFI_PEI_S3_RESUME2_PPI
*S3Resume
;
162 EFI_PEI_RECOVERY_MODULE_PPI
*PeiRecovery
;
163 EFI_MEMORY_TYPE_INFORMATION MemoryData
[EfiMaxMemoryType
+ 1];
166 // if in S3 Resume, restore configure
168 BootMode
= GetBootModeHob ();
170 if (BootMode
== BOOT_ON_S3_RESUME
) {
171 Status
= PeiServicesLocatePpi (
172 &gEfiPeiS3Resume2PpiGuid
,
177 ASSERT_EFI_ERROR (Status
);
179 Status
= S3Resume
->S3RestoreConfig2 (S3Resume
);
180 ASSERT_EFI_ERROR (Status
);
181 } else if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
182 Status
= PeiServicesLocatePpi (
183 &gEfiPeiRecoveryModulePpiGuid
,
186 (VOID
**) &PeiRecovery
188 ASSERT_EFI_ERROR (Status
);
190 Status
= PeiRecovery
->LoadRecoveryCapsule (PeiServices
, PeiRecovery
);
191 if (EFI_ERROR (Status
)) {
192 DEBUG ((DEBUG_ERROR
, "Load Recovery Capsule Failed.(Status = %r)\n", Status
));
197 // Now should have a HOB with the DXE core
201 Status
= PeiServicesLocatePpi (
202 &gEfiPeiReadOnlyVariable2PpiGuid
,
207 if (!EFI_ERROR (Status
)) {
208 DataSize
= sizeof (MemoryData
);
209 Status
= Variable
->GetVariable (
211 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
212 &gEfiMemoryTypeInformationGuid
,
217 if (!EFI_ERROR (Status
)) {
219 // Build the GUID'd HOB for DXE
222 &gEfiMemoryTypeInformationGuid
,
230 // Look in all the FVs present in PEI and find the DXE Core FileHandle
232 FileHandle
= DxeIplFindDxeCore ();
235 // Load the DXE Core from a Firmware Volume.
239 Status
= PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid
, Instance
++, NULL
, (VOID
**) &LoadFile
);
241 // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
243 ASSERT_EFI_ERROR (Status
);
245 Status
= LoadFile
->LoadFile (
253 } while (EFI_ERROR (Status
));
256 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
258 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &DxeCoreFileInfo
);
259 ASSERT_EFI_ERROR (Status
);
262 // Add HOB for the DXE Core
265 &DxeCoreFileInfo
.FileName
,
267 ALIGN_VALUE (DxeCoreSize
, EFI_PAGE_SIZE
),
272 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
274 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
));
276 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID
*)(UINTN
)DxeCoreAddress
, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint
)));
279 // Transfer control to the DXE Core
280 // The hand off state is simply a pointer to the HOB list
282 HandOffToDxeCore (DxeCoreEntryPoint
, HobList
);
284 // If we get here, then the DXE Core returned. This is an error
285 // DxeCore should not return.
290 return EFI_OUT_OF_RESOURCES
;
295 Searches DxeCore in all firmware Volumes and loads the first
296 instance that contains DxeCore.
298 @return FileHandle of DxeCore to load DxeCore.
308 EFI_PEI_FV_HANDLE VolumeHandle
;
309 EFI_PEI_FILE_HANDLE FileHandle
;
314 // Traverse all firmware volume instances
316 Status
= PeiServicesFfsFindNextVolume (Instance
, &VolumeHandle
);
318 // If some error occurs here, then we cannot find any firmware
319 // volume that may contain DxeCore.
321 if (EFI_ERROR (Status
)) {
322 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_CORE_EC_DXE_CORRUPT
));
324 ASSERT_EFI_ERROR (Status
);
327 // Find the DxeCore file type from the beginning in this firmware volume.
330 Status
= PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE
, VolumeHandle
, &FileHandle
);
331 if (!EFI_ERROR (Status
)) {
333 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
334 // return the FileHandle.
339 // We cannot find DxeCore in this firmware volume, then search the next volume.
348 The ExtractSection() function processes the input section and
349 returns a pointer to the section contents. If the section being
350 extracted does not require processing (if the section
351 GuidedSectionHeader.Attributes has the
352 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
353 OutputBuffer is just updated to point to the start of the
354 section's contents. Otherwise, *Buffer must be allocated
355 from PEI permanent memory.
357 @param This Indicates the
358 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
359 Buffer containing the input GUIDed section to be
360 processed. OutputBuffer OutputBuffer is
361 allocated from PEI permanent memory and contains
362 the new section stream.
363 @param InputSection A pointer to the input buffer, which contains
364 the input section to be processed.
365 @param OutputBuffer A pointer to a caller-allocated buffer, whose
366 size is specified by the contents of OutputSize.
367 @param OutputSize A pointer to a caller-allocated
368 UINTN in which the size of *OutputBuffer
369 allocation is stored. If the function
370 returns anything other than EFI_SUCCESS,
371 the value of OutputSize is undefined.
372 @param AuthenticationStatus A pointer to a caller-allocated
373 UINT32 that indicates the
374 authentication status of the
375 output buffer. If the input
376 section's GuidedSectionHeader.
377 Attributes field has the
378 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
380 AuthenticationStatus must return
381 zero. These bits reflect the
382 status of the extraction
383 operation. If the function
384 returns anything other than
385 EFI_SUCCESS, the value of
386 AuthenticationStatus is
389 @retval EFI_SUCCESS The InputSection was
390 successfully processed and the
391 section contents were returned.
393 @retval EFI_OUT_OF_RESOURCES The system has insufficient
394 resources to process the request.
396 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
397 not match this instance of the
398 GUIDed Section Extraction PPI.
403 CustomGuidedSectionExtract (
404 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
405 IN CONST VOID
*InputSection
,
406 OUT VOID
**OutputBuffer
,
407 OUT UINTN
*OutputSize
,
408 OUT UINT32
*AuthenticationStatus
412 UINT8
*ScratchBuffer
;
413 UINT32 ScratchBufferSize
;
414 UINT32 OutputBufferSize
;
415 UINT16 SectionAttribute
;
418 // Init local variable
420 ScratchBuffer
= NULL
;
423 // Call GetInfo to get the size and attribute of input guided section data.
425 Status
= ExtractGuidedSectionGetInfo (
432 if (EFI_ERROR (Status
)) {
433 DEBUG ((DEBUG_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
437 if (ScratchBufferSize
!= 0) {
439 // Allocate scratch buffer
441 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
442 if (ScratchBuffer
== NULL
) {
443 return EFI_OUT_OF_RESOURCES
;
447 if (((SectionAttribute
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) && OutputBufferSize
> 0) {
449 // Allocate output buffer
451 *OutputBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize
) + 1);
452 if (*OutputBuffer
== NULL
) {
453 return EFI_OUT_OF_RESOURCES
;
455 DEBUG ((DEBUG_INFO
, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize
, *OutputBuffer
));
457 // *OutputBuffer still is one section. Adjust *OutputBuffer offset,
458 // skip EFI section header to make section data at page alignment.
460 *OutputBuffer
= (VOID
*)((UINT8
*) *OutputBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
));
463 Status
= ExtractGuidedSectionDecode (
469 if (EFI_ERROR (Status
)) {
473 DEBUG ((DEBUG_ERROR
, "Extract guided section Failed - %r\n", Status
));
477 *OutputSize
= (UINTN
) OutputBufferSize
;
485 Decompresses a section to the output buffer.
487 This function looks up the compression type field in the input section and
488 applies the appropriate compression algorithm to compress the section to a
489 callee allocated buffer.
491 @param This Points to this instance of the
492 EFI_PEI_DECOMPRESS_PEI PPI.
493 @param CompressionSection Points to the compressed section.
494 @param OutputBuffer Holds the returned pointer to the decompressed
496 @param OutputSize Holds the returned size of the decompress
499 @retval EFI_SUCCESS The section was decompressed successfully.
500 OutputBuffer contains the resulting data and
501 OutputSize contains the resulting size.
507 IN CONST EFI_PEI_DECOMPRESS_PPI
*This
,
508 IN CONST EFI_COMPRESSION_SECTION
*CompressionSection
,
509 OUT VOID
**OutputBuffer
,
510 OUT UINTN
*OutputSize
515 UINT8
*ScratchBuffer
;
516 UINT32 DstBufferSize
;
517 UINT32 ScratchBufferSize
;
518 VOID
*CompressionSource
;
519 UINT32 CompressionSourceSize
;
520 UINT32 UncompressedLength
;
521 UINT8 CompressionType
;
523 if (CompressionSection
->CommonHeader
.Type
!= EFI_SECTION_COMPRESSION
) {
525 return EFI_INVALID_PARAMETER
;
528 if (IS_SECTION2 (CompressionSection
)) {
529 CompressionSource
= (VOID
*) ((UINT8
*) CompressionSection
+ sizeof (EFI_COMPRESSION_SECTION2
));
530 CompressionSourceSize
= (UINT32
) (SECTION2_SIZE (CompressionSection
) - sizeof (EFI_COMPRESSION_SECTION2
));
531 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*) CompressionSection
)->UncompressedLength
;
532 CompressionType
= ((EFI_COMPRESSION_SECTION2
*) CompressionSection
)->CompressionType
;
534 CompressionSource
= (VOID
*) ((UINT8
*) CompressionSection
+ sizeof (EFI_COMPRESSION_SECTION
));
535 CompressionSourceSize
= (UINT32
) (SECTION_SIZE (CompressionSection
) - sizeof (EFI_COMPRESSION_SECTION
));
536 UncompressedLength
= CompressionSection
->UncompressedLength
;
537 CompressionType
= CompressionSection
->CompressionType
;
541 // This is a compression set, expand it
543 switch (CompressionType
) {
544 case EFI_STANDARD_COMPRESSION
:
545 if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress
)) {
547 // Load EFI standard compression.
548 // For compressed data, decompress them to destination buffer.
550 Status
= UefiDecompressGetInfo (
552 CompressionSourceSize
,
556 if (EFI_ERROR (Status
)) {
560 DEBUG ((DEBUG_ERROR
, "Decompress GetInfo Failed - %r\n", Status
));
561 return EFI_NOT_FOUND
;
564 // Allocate scratch buffer
566 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
567 if (ScratchBuffer
== NULL
) {
568 return EFI_OUT_OF_RESOURCES
;
571 // Allocate destination buffer, extra one page for adjustment
573 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
574 if (DstBuffer
== NULL
) {
575 return EFI_OUT_OF_RESOURCES
;
578 // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
579 // to make section data at page alignment.
581 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
583 // Call decompress function
585 Status
= UefiDecompress (
590 if (EFI_ERROR (Status
)) {
594 DEBUG ((DEBUG_ERROR
, "Decompress Failed - %r\n", Status
));
595 return EFI_NOT_FOUND
;
600 // PcdDxeIplSupportUefiDecompress is FALSE
601 // Don't support UEFI decompression algorithm.
604 return EFI_NOT_FOUND
;
607 case EFI_NOT_COMPRESSED
:
609 // Allocate destination buffer
611 DstBufferSize
= UncompressedLength
;
612 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
613 if (DstBuffer
== NULL
) {
614 return EFI_OUT_OF_RESOURCES
;
617 // Adjust DstBuffer offset, skip EFI section header
618 // to make section data at page alignment.
620 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
622 // stream is not actually compressed, just encapsulated. So just copy it.
624 CopyMem (DstBuffer
, CompressionSource
, DstBufferSize
);
629 // Don't support other unknown compression type.
632 return EFI_NOT_FOUND
;
635 *OutputSize
= DstBufferSize
;
636 *OutputBuffer
= DstBuffer
;
643 Updates the Stack HOB passed to DXE phase.
645 This function traverses the whole HOB list and update the stack HOB to
646 reflect the real stack that is used by DXE core.
648 @param BaseAddress The lower address of stack used by DxeCore.
649 @param Length The length of stack used by DxeCore.
654 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
658 EFI_PEI_HOB_POINTERS Hob
;
660 Hob
.Raw
= GetHobList ();
661 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
662 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
664 // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to
665 // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some
666 // PEIMs may also keep key information on stack
668 BuildMemoryAllocationHob (
669 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
670 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
674 // Update the BSP Stack Hob to reflect the new stack info.
676 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
677 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
680 Hob
.Raw
= GET_NEXT_HOB (Hob
);