3 Responsibility of this module is to load the DXE Core from a Firmware Volume.
5 Copyright (c) 2006 - 2009, Intel Corporation. <BR>
6 All rights reserved. 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
;
158 EFI_MEMORY_TYPE_INFORMATION MemoryData
[EfiMaxMemoryType
+ 1];
161 // if in S3 Resume, restore configure
163 BootMode
= GetBootModeHob ();
165 if (BootMode
== BOOT_ON_S3_RESUME
) {
166 Status
= AcpiS3ResumeOs();
167 ASSERT_EFI_ERROR (Status
);
168 } else if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
169 Status
= PeiRecoverFirmware ();
170 if (EFI_ERROR (Status
)) {
171 DEBUG ((DEBUG_ERROR
, "Load Recovery Capsule Failed.(Status = %r)\n", Status
));
176 // Now should have a HOB with the DXE core
180 Status
= PeiServicesLocatePpi (
181 &gEfiPeiReadOnlyVariable2PpiGuid
,
186 if (!EFI_ERROR (Status
)) {
187 DataSize
= sizeof (MemoryData
);
188 Status
= Variable
->GetVariable (
190 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
191 &gEfiMemoryTypeInformationGuid
,
196 if (!EFI_ERROR (Status
)) {
198 // Build the GUID'd HOB for DXE
201 &gEfiMemoryTypeInformationGuid
,
209 // Look in all the FVs present in PEI and find the DXE Core FileHandle
211 FileHandle
= DxeIplFindDxeCore ();
214 // Load the DXE Core from a Firmware Volume, may use LoadFile PPI to do this to save code size.
216 Status
= PeiLoadFile (
222 ASSERT_EFI_ERROR (Status
);
225 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
227 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &DxeCoreFileInfo
);
228 ASSERT_EFI_ERROR (Status
);
231 // Add HOB for the DXE Core
234 &DxeCoreFileInfo
.FileName
,
236 ALIGN_VALUE (DxeCoreSize
, EFI_PAGE_SIZE
),
241 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
243 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdStatusCodeValuePeiHandoffToDxe
));
245 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID
*)(UINTN
)DxeCoreAddress
, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint
)));
248 // Transfer control to the DXE Core
249 // The hand off state is simply a pointer to the HOB list
251 HandOffToDxeCore (DxeCoreEntryPoint
, HobList
);
253 // If we get here, then the DXE Core returned. This is an error
254 // DxeCore should not return.
259 return EFI_OUT_OF_RESOURCES
;
264 Searches DxeCore in all firmware Volumes and loads the first
265 instance that contains DxeCore.
267 @return FileHandle of DxeCore to load DxeCore.
277 EFI_PEI_FV_HANDLE VolumeHandle
;
278 EFI_PEI_FILE_HANDLE FileHandle
;
283 // Traverse all firmware volume instances
285 Status
= PeiServicesFfsFindNextVolume (Instance
, &VolumeHandle
);
287 // If some error occurs here, then we cannot find any firmware
288 // volume that may contain DxeCore.
290 ASSERT_EFI_ERROR (Status
);
293 // Find the DxeCore file type from the beginning in this firmware volume.
296 Status
= PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE
, VolumeHandle
, &FileHandle
);
297 if (!EFI_ERROR (Status
)) {
299 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
300 // return the FileHandle.
305 // We cannot find DxeCore in this firmware volume, then search the next volume.
313 Loads and relocates a PE/COFF image into memory.
315 @param FileHandle The image file handle
316 @param ImageAddress The base address of the relocated PE/COFF image
317 @param ImageSize The size of the relocated PE/COFF image
318 @param EntryPoint The entry point of the relocated PE/COFF image
320 @return EFI_SUCCESS The file was loaded and relocated
321 @return EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
326 IN EFI_PEI_FILE_HANDLE FileHandle
,
327 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
328 OUT UINT64
*ImageSize
,
329 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
334 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
338 // First try to find the PE32 section in this ffs file.
340 Status
= PeiServicesFfsFindSectionData (
345 if (EFI_ERROR (Status
)) {
347 // NO image types we support so exit.
352 ZeroMem (&ImageContext
, sizeof (ImageContext
));
353 ImageContext
.Handle
= Pe32Data
;
354 ImageContext
.ImageRead
= PeiImageRead
;
357 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
358 if (EFI_ERROR (Status
)) {
362 // Allocate Memory for the image
364 Status
= PeiServicesAllocatePages (
366 EFI_SIZE_TO_PAGES ((UINT32
) ImageContext
.ImageSize
),
367 &ImageContext
.ImageAddress
369 ASSERT_EFI_ERROR (Status
);
370 ASSERT (ImageContext
.ImageAddress
!= 0);
373 // Load the image to our new buffer
375 Status
= PeCoffLoaderLoadImage (&ImageContext
);
376 if (EFI_ERROR (Status
)) {
380 // Relocate the image in our new buffer
382 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
383 if (EFI_ERROR (Status
)) {
388 // Flush the instruction cache so the image data are written before we execute it
390 InvalidateInstructionCacheRange ((VOID
*)(UINTN
) ImageContext
.ImageAddress
, (UINTN
) ImageContext
.ImageSize
);
392 *ImageAddress
= ImageContext
.ImageAddress
;
393 *ImageSize
= ImageContext
.ImageSize
;
394 *EntryPoint
= ImageContext
.EntryPoint
;
403 The ExtractSection() function processes the input section and
404 returns a pointer to the section contents. If the section being
405 extracted does not require processing (if the section
406 GuidedSectionHeader.Attributes has the
407 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
408 OutputBuffer is just updated to point to the start of the
409 section's contents. Otherwise, *Buffer must be allocated
410 from PEI permanent memory.
412 @param This Indicates the
413 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
414 Buffer containing the input GUIDed section to be
415 processed. OutputBuffer OutputBuffer is
416 allocated from PEI permanent memory and contains
417 the new section stream.
418 @param InputSection A pointer to the input buffer, which contains
419 the input section to be processed.
420 @param OutputBuffer A pointer to a caller-allocated buffer, whose
421 size is specified by the contents of OutputSize.
422 @param OutputSize A pointer to a caller-allocated
423 UINTN in which the size of *OutputBuffer
424 allocation is stored. If the function
425 returns anything other than EFI_SUCCESS,
426 the value of OutputSize is undefined.
427 @param AuthenticationStatus A pointer to a caller-allocated
428 UINT32 that indicates the
429 authentication status of the
430 output buffer. If the input
431 section's GuidedSectionHeader.
432 Attributes field has the
433 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
435 AuthenticationStatus must return
436 zero. These bits reflect the
437 status of the extraction
438 operation. If the function
439 returns anything other than
440 EFI_SUCCESS, the value of
441 AuthenticationStatus is
444 @retval EFI_SUCCESS The InputSection was
445 successfully processed and the
446 section contents were returned.
448 @retval EFI_OUT_OF_RESOURCES The system has insufficient
449 resources to process the request.
451 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
452 not match this instance of the
453 GUIDed Section Extraction PPI.
458 CustomGuidedSectionExtract (
459 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
460 IN CONST VOID
*InputSection
,
461 OUT VOID
**OutputBuffer
,
462 OUT UINTN
*OutputSize
,
463 OUT UINT32
*AuthenticationStatus
467 UINT8
*ScratchBuffer
;
468 UINT32 ScratchBufferSize
;
469 UINT32 OutputBufferSize
;
470 UINT16 SectionAttribute
;
473 // Init local variable
475 ScratchBuffer
= NULL
;
478 // Call GetInfo to get the size and attribute of input guided section data.
480 Status
= ExtractGuidedSectionGetInfo (
487 if (EFI_ERROR (Status
)) {
488 DEBUG ((DEBUG_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
492 if (ScratchBufferSize
!= 0) {
494 // Allocate scratch buffer
496 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
497 if (ScratchBuffer
== NULL
) {
498 return EFI_OUT_OF_RESOURCES
;
502 if (((SectionAttribute
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) && OutputBufferSize
> 0) {
504 // Allocate output buffer
506 *OutputBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize
) + 1);
507 if (*OutputBuffer
== NULL
) {
508 return EFI_OUT_OF_RESOURCES
;
510 DEBUG ((DEBUG_INFO
, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize
, *OutputBuffer
));
512 // *OutputBuffer still is one section. Adjust *OutputBuffer offset,
513 // skip EFI section header to make section data at page alignment.
515 *OutputBuffer
= (VOID
*)((UINT8
*) *OutputBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
));
518 Status
= ExtractGuidedSectionDecode (
524 if (EFI_ERROR (Status
)) {
528 DEBUG ((DEBUG_ERROR
, "Extract guided section Failed - %r\n", Status
));
532 *OutputSize
= (UINTN
) OutputBufferSize
;
540 Decompresses a section to the output buffer.
542 This function looks up the compression type field in the input section and
543 applies the appropriate compression algorithm to compress the section to a
544 callee allocated buffer.
546 @param This Points to this instance of the
547 EFI_PEI_DECOMPRESS_PEI PPI.
548 @param CompressionSection Points to the compressed section.
549 @param OutputBuffer Holds the returned pointer to the decompressed
551 @param OutputSize Holds the returned size of the decompress
554 @retval EFI_SUCCESS The section was decompressed successfully.
555 OutputBuffer contains the resulting data and
556 OutputSize contains the resulting size.
562 IN CONST EFI_PEI_DECOMPRESS_PPI
*This
,
563 IN CONST EFI_COMPRESSION_SECTION
*CompressionSection
,
564 OUT VOID
**OutputBuffer
,
565 OUT UINTN
*OutputSize
570 UINT8
*ScratchBuffer
;
572 UINT32 ScratchBufferSize
;
573 EFI_COMMON_SECTION_HEADER
*Section
;
576 if (CompressionSection
->CommonHeader
.Type
!= EFI_SECTION_COMPRESSION
) {
578 return EFI_INVALID_PARAMETER
;
581 Section
= (EFI_COMMON_SECTION_HEADER
*) CompressionSection
;
582 SectionLength
= *(UINT32
*) (Section
->Size
) & 0x00ffffff;
585 // This is a compression set, expand it
587 switch (CompressionSection
->CompressionType
) {
588 case EFI_STANDARD_COMPRESSION
:
590 // Load EFI standard compression.
591 // For compressed data, decompress them to destination buffer.
593 Status
= UefiDecompressGetInfo (
594 (UINT8
*) ((EFI_COMPRESSION_SECTION
*) Section
+ 1),
595 (UINT32
) SectionLength
- sizeof (EFI_COMPRESSION_SECTION
),
596 (UINT32
*) &DstBufferSize
,
599 if (EFI_ERROR (Status
)) {
603 DEBUG ((DEBUG_ERROR
, "Decompress GetInfo Failed - %r\n", Status
));
604 return EFI_NOT_FOUND
;
607 // Allocate scratch buffer
609 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
610 if (ScratchBuffer
== NULL
) {
611 return EFI_OUT_OF_RESOURCES
;
614 // Allocate destination buffer, extra one page for adjustment
616 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
617 if (DstBuffer
== NULL
) {
618 return EFI_OUT_OF_RESOURCES
;
621 // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
622 // to make section data at page alignment.
624 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
626 // Call decompress function
628 Status
= UefiDecompress (
629 (CHAR8
*) ((EFI_COMPRESSION_SECTION
*) Section
+ 1),
633 if (EFI_ERROR (Status
)) {
637 DEBUG ((DEBUG_ERROR
, "Decompress Failed - %r\n", Status
));
638 return EFI_NOT_FOUND
;
642 case EFI_NOT_COMPRESSED
:
644 // Allocate destination buffer
646 DstBufferSize
= CompressionSection
->UncompressedLength
;
647 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
) + 1);
648 if (DstBuffer
== NULL
) {
649 return EFI_OUT_OF_RESOURCES
;
652 // Adjust DstBuffer offset, skip EFI section header
653 // to make section data at page alignment.
655 DstBuffer
= DstBuffer
+ EFI_PAGE_SIZE
- sizeof (EFI_COMMON_SECTION_HEADER
);
657 // stream is not actually compressed, just encapsulated. So just copy it.
659 CopyMem (DstBuffer
, CompressionSection
+ 1, DstBufferSize
);
664 // Don't support other unknown compression type.
667 return EFI_NOT_FOUND
;
670 *OutputSize
= DstBufferSize
;
671 *OutputBuffer
= DstBuffer
;
678 Updates the Stack HOB passed to DXE phase.
680 This function traverses the whole HOB list and update the stack HOB to
681 reflect the real stack that is used by DXE core.
683 @param BaseAddress The lower address of stack used by DxeCore.
684 @param Length The length of stack used by DxeCore.
689 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
693 EFI_PEI_HOB_POINTERS Hob
;
695 Hob
.Raw
= GetHobList ();
696 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
697 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
699 // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type
700 // to be reclaimed by DXE core.
702 BuildMemoryAllocationHob (
703 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
704 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
705 EfiConventionalMemory
708 // Update the BSP Stack Hob to reflect the new stack info.
710 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
711 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
714 Hob
.Raw
= GET_NEXT_HOB (Hob
);