2 Main SEC phase code. Transitions to PEI.
4 Copyright (c) 2008 - 2013, 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.
18 #include <Library/PeimEntryPoint.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/PeiServicesLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/UefiCpuLib.h>
25 #include <Library/DebugAgentLib.h>
26 #include <Library/IoLib.h>
27 #include <Library/PeCoffLib.h>
28 #include <Library/PeCoffGetEntryPointLib.h>
29 #include <Library/PeCoffExtraActionLib.h>
30 #include <Library/ExtractGuidedSectionLib.h>
32 #include <Ppi/TemporaryRamSupport.h>
34 #include <IndustryStandard/X64Paging.h>
36 #define SEC_IDT_ENTRY_COUNT 34
38 typedef struct _SEC_IDT_TABLE
{
39 EFI_PEI_SERVICES
*PeiService
;
40 IA32_IDT_GATE_DESCRIPTOR IdtTable
[SEC_IDT_ENTRY_COUNT
];
51 TemporaryRamMigration (
52 IN CONST EFI_PEI_SERVICES
**PeiServices
,
53 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
54 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
61 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mTemporaryRamSupportPpi
= {
65 EFI_PEI_PPI_DESCRIPTOR mPrivateDispatchTable
[] = {
67 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
68 &gEfiTemporaryRamSupportPpiGuid
,
69 &mTemporaryRamSupportPpi
74 // Template of an IDT entry pointing to 10:FFFFFFE4h.
76 IA32_IDT_GATE_DESCRIPTOR mIdtEntryTemplate
= {
81 IA32_IDT_GATE_TYPE_INTERRUPT_32
, // GateType
87 Locates the main boot firmware volume.
89 @param[in,out] BootFv On input, the base of the BootFv
90 On output, the decompressed main firmware volume
92 @retval EFI_SUCCESS The main firmware volume was located and decompressed
93 @retval EFI_NOT_FOUND The main firmware volume was not found
98 IN OUT EFI_FIRMWARE_VOLUME_HEADER
**BootFv
101 EFI_FIRMWARE_VOLUME_HEADER
*Fv
;
104 ASSERT (((UINTN
) *BootFv
& EFI_PAGE_MASK
) == 0);
107 Distance
= (UINTN
) (*BootFv
)->FvLength
;
109 Fv
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) Fv
- EFI_PAGE_SIZE
);
110 Distance
+= EFI_PAGE_SIZE
;
111 if (Distance
> SIZE_32MB
) {
112 return EFI_NOT_FOUND
;
115 if (Fv
->Signature
!= EFI_FVH_SIGNATURE
) {
119 if ((UINTN
) Fv
->FvLength
> Distance
) {
130 Locates a section within a series of sections
131 with the specified section type.
133 @param[in] Sections The sections to search
134 @param[in] SizeOfSections Total size of all sections
135 @param[in] SectionType The section type to locate
136 @param[out] FoundSection The FFS section if found
138 @retval EFI_SUCCESS The file and section was found
139 @retval EFI_NOT_FOUND The file and section was not found
140 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
144 FindFfsSectionInSections (
146 IN UINTN SizeOfSections
,
147 IN EFI_SECTION_TYPE SectionType
,
148 OUT EFI_COMMON_SECTION_HEADER
**FoundSection
151 EFI_PHYSICAL_ADDRESS CurrentAddress
;
153 EFI_PHYSICAL_ADDRESS EndOfSections
;
154 EFI_COMMON_SECTION_HEADER
*Section
;
155 EFI_PHYSICAL_ADDRESS EndOfSection
;
158 // Loop through the FFS file sections within the PEI Core FFS file
160 EndOfSection
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Sections
;
161 EndOfSections
= EndOfSection
+ SizeOfSections
;
163 if (EndOfSection
== EndOfSections
) {
166 CurrentAddress
= (EndOfSection
+ 3) & ~(3ULL);
167 if (CurrentAddress
>= EndOfSections
) {
168 return EFI_VOLUME_CORRUPTED
;
171 Section
= (EFI_COMMON_SECTION_HEADER
*)(UINTN
) CurrentAddress
;
172 DEBUG ((EFI_D_INFO
, "Section->Type: 0x%x\n", Section
->Type
));
174 Size
= SECTION_SIZE (Section
);
175 if (Size
< sizeof (*Section
)) {
176 return EFI_VOLUME_CORRUPTED
;
179 EndOfSection
= CurrentAddress
+ Size
;
180 if (EndOfSection
> EndOfSections
) {
181 return EFI_VOLUME_CORRUPTED
;
185 // Look for the requested section type
187 if (Section
->Type
== SectionType
) {
188 *FoundSection
= Section
;
191 DEBUG ((EFI_D_INFO
, "Section->Type (0x%x) != SectionType (0x%x)\n", Section
->Type
, SectionType
));
194 return EFI_NOT_FOUND
;
198 Locates a FFS file with the specified file type and a section
199 within that file with the specified section type.
201 @param[in] Fv The firmware volume to search
202 @param[in] FileType The file type to locate
203 @param[in] SectionType The section type to locate
204 @param[out] FoundSection The FFS section if found
206 @retval EFI_SUCCESS The file and section was found
207 @retval EFI_NOT_FOUND The file and section was not found
208 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
213 FindFfsFileAndSection (
214 IN EFI_FIRMWARE_VOLUME_HEADER
*Fv
,
215 IN EFI_FV_FILETYPE FileType
,
216 IN EFI_SECTION_TYPE SectionType
,
217 OUT EFI_COMMON_SECTION_HEADER
**FoundSection
221 EFI_PHYSICAL_ADDRESS CurrentAddress
;
222 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume
;
223 EFI_FFS_FILE_HEADER
*File
;
225 EFI_PHYSICAL_ADDRESS EndOfFile
;
227 if (Fv
->Signature
!= EFI_FVH_SIGNATURE
) {
228 DEBUG ((EFI_D_INFO
, "FV at %p does not have FV header signature\n", Fv
));
229 return EFI_VOLUME_CORRUPTED
;
232 CurrentAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Fv
;
233 EndOfFirmwareVolume
= CurrentAddress
+ Fv
->FvLength
;
236 // Loop through the FFS files in the Boot Firmware Volume
238 for (EndOfFile
= CurrentAddress
+ Fv
->HeaderLength
; ; ) {
240 CurrentAddress
= (EndOfFile
+ 7) & ~(7ULL);
241 if (CurrentAddress
> EndOfFirmwareVolume
) {
242 return EFI_VOLUME_CORRUPTED
;
245 File
= (EFI_FFS_FILE_HEADER
*)(UINTN
) CurrentAddress
;
246 Size
= *(UINT32
*) File
->Size
& 0xffffff;
247 if (Size
< (sizeof (*File
) + sizeof (EFI_COMMON_SECTION_HEADER
))) {
248 return EFI_VOLUME_CORRUPTED
;
250 DEBUG ((EFI_D_INFO
, "File->Type: 0x%x\n", File
->Type
));
252 EndOfFile
= CurrentAddress
+ Size
;
253 if (EndOfFile
> EndOfFirmwareVolume
) {
254 return EFI_VOLUME_CORRUPTED
;
258 // Look for the request file type
260 if (File
->Type
!= FileType
) {
261 DEBUG ((EFI_D_INFO
, "File->Type (0x%x) != FileType (0x%x)\n", File
->Type
, FileType
));
265 Status
= FindFfsSectionInSections (
267 (UINTN
) EndOfFile
- (UINTN
) (File
+ 1),
271 if (!EFI_ERROR (Status
) || (Status
== EFI_VOLUME_CORRUPTED
)) {
278 Locates the compressed main firmware volume and decompresses it.
280 @param[in,out] Fv On input, the firmware volume to search
281 On output, the decompressed main FV
283 @retval EFI_SUCCESS The file and section was found
284 @retval EFI_NOT_FOUND The file and section was not found
285 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
291 IN OUT EFI_FIRMWARE_VOLUME_HEADER
**Fv
295 EFI_GUID_DEFINED_SECTION
*Section
;
296 UINT32 OutputBufferSize
;
297 UINT32 ScratchBufferSize
;
298 UINT16 SectionAttribute
;
299 UINT32 AuthenticationStatus
;
302 EFI_FIRMWARE_VOLUME_IMAGE_SECTION
*NewFvSection
;
303 EFI_FIRMWARE_VOLUME_HEADER
*NewFv
;
305 NewFvSection
= (EFI_FIRMWARE_VOLUME_IMAGE_SECTION
*) NULL
;
307 Status
= FindFfsFileAndSection (
309 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
,
310 EFI_SECTION_GUID_DEFINED
,
311 (EFI_COMMON_SECTION_HEADER
**) &Section
313 if (EFI_ERROR (Status
)) {
314 DEBUG ((EFI_D_ERROR
, "Unable to find GUID defined section\n"));
318 Status
= ExtractGuidedSectionGetInfo (
324 if (EFI_ERROR (Status
)) {
325 DEBUG ((EFI_D_ERROR
, "Unable to GetInfo for GUIDed section\n"));
329 //PcdGet32 (PcdOvmfMemFvBase), PcdGet32 (PcdOvmfMemFvSize)
330 OutputBuffer
= (VOID
*) ((UINT8
*)(UINTN
) PcdGet32 (PcdOvmfMemFvBase
) + SIZE_1MB
);
331 ScratchBuffer
= ALIGN_POINTER ((UINT8
*) OutputBuffer
+ OutputBufferSize
, SIZE_1MB
);
332 Status
= ExtractGuidedSectionDecode (
336 &AuthenticationStatus
338 if (EFI_ERROR (Status
)) {
339 DEBUG ((EFI_D_ERROR
, "Error during GUID section decode\n"));
343 Status
= FindFfsSectionInSections (
346 EFI_SECTION_FIRMWARE_VOLUME_IMAGE
,
347 (EFI_COMMON_SECTION_HEADER
**) &NewFvSection
349 if (EFI_ERROR (Status
)) {
350 DEBUG ((EFI_D_ERROR
, "Unable to find FV image in extracted data\n"));
354 NewFv
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
) PcdGet32 (PcdOvmfMemFvBase
);
355 CopyMem (NewFv
, (VOID
*) (NewFvSection
+ 1), PcdGet32 (PcdOvmfMemFvSize
));
357 if (NewFv
->Signature
!= EFI_FVH_SIGNATURE
) {
358 DEBUG ((EFI_D_ERROR
, "Extracted FV at %p does not have FV header signature\n", NewFv
));
360 return EFI_VOLUME_CORRUPTED
;
368 Locates the PEI Core entry point address
370 @param[in] Fv The firmware volume to search
371 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image
373 @retval EFI_SUCCESS The file and section was found
374 @retval EFI_NOT_FOUND The file and section was not found
375 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
380 FindPeiCoreImageBaseInFv (
381 IN EFI_FIRMWARE_VOLUME_HEADER
*Fv
,
382 OUT EFI_PHYSICAL_ADDRESS
*PeiCoreImageBase
386 EFI_COMMON_SECTION_HEADER
*Section
;
388 Status
= FindFfsFileAndSection (
390 EFI_FV_FILETYPE_PEI_CORE
,
394 if (EFI_ERROR (Status
)) {
395 Status
= FindFfsFileAndSection (
397 EFI_FV_FILETYPE_PEI_CORE
,
401 if (EFI_ERROR (Status
)) {
402 DEBUG ((EFI_D_ERROR
, "Unable to find PEI Core image\n"));
407 *PeiCoreImageBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(Section
+ 1);
412 Locates the PEI Core entry point address
414 @param[in,out] Fv The firmware volume to search
415 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image
417 @retval EFI_SUCCESS The file and section was found
418 @retval EFI_NOT_FOUND The file and section was not found
419 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
424 FindPeiCoreImageBase (
425 IN OUT EFI_FIRMWARE_VOLUME_HEADER
**BootFv
,
426 OUT EFI_PHYSICAL_ADDRESS
*PeiCoreImageBase
429 *PeiCoreImageBase
= 0;
433 DecompressGuidedFv (BootFv
);
435 FindPeiCoreImageBaseInFv (*BootFv
, PeiCoreImageBase
);
439 Find core image base.
445 IN EFI_FIRMWARE_VOLUME_HEADER
*BootFirmwareVolumePtr
,
446 OUT EFI_PHYSICAL_ADDRESS
*SecCoreImageBase
449 EFI_PHYSICAL_ADDRESS CurrentAddress
;
450 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume
;
451 EFI_FFS_FILE_HEADER
*File
;
453 EFI_PHYSICAL_ADDRESS EndOfFile
;
454 EFI_COMMON_SECTION_HEADER
*Section
;
455 EFI_PHYSICAL_ADDRESS EndOfSection
;
457 *SecCoreImageBase
= 0;
459 CurrentAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) BootFirmwareVolumePtr
;
460 EndOfFirmwareVolume
= CurrentAddress
+ BootFirmwareVolumePtr
->FvLength
;
463 // Loop through the FFS files in the Boot Firmware Volume
465 for (EndOfFile
= CurrentAddress
+ BootFirmwareVolumePtr
->HeaderLength
; ; ) {
467 CurrentAddress
= (EndOfFile
+ 7) & 0xfffffffffffffff8ULL
;
468 if (CurrentAddress
> EndOfFirmwareVolume
) {
469 return EFI_NOT_FOUND
;
472 File
= (EFI_FFS_FILE_HEADER
*)(UINTN
) CurrentAddress
;
473 Size
= *(UINT32
*) File
->Size
& 0xffffff;
474 if (Size
< sizeof (*File
)) {
475 return EFI_NOT_FOUND
;
478 EndOfFile
= CurrentAddress
+ Size
;
479 if (EndOfFile
> EndOfFirmwareVolume
) {
480 return EFI_NOT_FOUND
;
486 if (File
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
) {
491 // Loop through the FFS file sections within the FFS file
493 EndOfSection
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (File
+ 1);
495 CurrentAddress
= (EndOfSection
+ 3) & 0xfffffffffffffffcULL
;
496 Section
= (EFI_COMMON_SECTION_HEADER
*)(UINTN
) CurrentAddress
;
498 Size
= *(UINT32
*) Section
->Size
& 0xffffff;
499 if (Size
< sizeof (*Section
)) {
500 return EFI_NOT_FOUND
;
503 EndOfSection
= CurrentAddress
+ Size
;
504 if (EndOfSection
> EndOfFile
) {
505 return EFI_NOT_FOUND
;
509 // Look for executable sections
511 if (Section
->Type
== EFI_SECTION_PE32
|| Section
->Type
== EFI_SECTION_TE
) {
512 if (File
->Type
== EFI_FV_FILETYPE_SECURITY_CORE
) {
513 *SecCoreImageBase
= (PHYSICAL_ADDRESS
) (UINTN
) (Section
+ 1);
520 // SEC Core image found
522 if (*SecCoreImageBase
!= 0) {
528 #if defined (MDE_CPU_X64)
530 Allocates and fills in the Page Directory and Page Table Entries to
531 establish a 1:1 Virtual to Physical mapping.
533 @param Location Memory to build the page tables in
537 Create4GbIdentityMappingPageTables (
543 EFI_PHYSICAL_ADDRESS PageAddress
;
544 UINTN IndexOfPml4Entries
;
545 UINTN IndexOfPdpEntries
;
546 UINTN IndexOfPageDirectoryEntries
;
547 UINT32 NumberOfPml4EntriesNeeded
;
548 UINT32 NumberOfPdpEntriesNeeded
;
549 X64_PAGE_MAP_AND_DIRECTORY_POINTER
*PageMapLevel4Entry
;
550 X64_PAGE_MAP_AND_DIRECTORY_POINTER
*PageMap
;
551 X64_PAGE_MAP_AND_DIRECTORY_POINTER
*PageDirectoryPointerEntry
;
552 X64_PAGE_TABLE_ENTRY
*PageDirectoryEntry
;
553 UINTN NextAllocAddress
;
554 BOOLEAN Page1GSupport
;
555 X64_PAGE_TABLE_1G_ENTRY
*PageDirectory1GEntry
;
557 Page1GSupport
= FALSE
;
558 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
559 if (RegEax
>= 0x80000001) {
560 AsmCpuid (0x80000001, NULL
, NULL
, NULL
, &RegEdx
);
561 if ((RegEdx
& BIT26
) != 0) {
562 Page1GSupport
= TRUE
;
567 // Only build entries for the first 4GB at this stage.
569 NumberOfPml4EntriesNeeded
= 1;
570 NumberOfPdpEntriesNeeded
= 4;
572 NextAllocAddress
= (UINTN
) Location
;
575 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.
577 PageMap
= (VOID
*) NextAllocAddress
;
578 NextAllocAddress
+= SIZE_4KB
;
580 PageMapLevel4Entry
= PageMap
;
582 for (IndexOfPml4Entries
= 0; IndexOfPml4Entries
< NumberOfPml4EntriesNeeded
; IndexOfPml4Entries
++, PageMapLevel4Entry
++) {
584 // Each PML4 entry points to a page of Page Directory Pointer entires.
585 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
587 PageDirectoryPointerEntry
= (VOID
*) NextAllocAddress
;
588 NextAllocAddress
+= SIZE_4KB
;
593 PageMapLevel4Entry
->Uint64
= (UINT64
)(UINTN
)PageDirectoryPointerEntry
;
594 PageMapLevel4Entry
->Bits
.ReadWrite
= 1;
595 PageMapLevel4Entry
->Bits
.Present
= 1;
598 PageDirectory1GEntry
= (VOID
*) PageDirectoryPointerEntry
;
600 for (IndexOfPageDirectoryEntries
= 0; IndexOfPageDirectoryEntries
< 512; IndexOfPageDirectoryEntries
++, PageDirectory1GEntry
++, PageAddress
+= SIZE_1GB
) {
602 // Fill in the Page Directory entries
604 PageDirectory1GEntry
->Uint64
= (UINT64
)PageAddress
;
605 PageDirectory1GEntry
->Bits
.ReadWrite
= 1;
606 PageDirectory1GEntry
->Bits
.Present
= 1;
607 PageDirectory1GEntry
->Bits
.MustBe1
= 1;
610 for (IndexOfPdpEntries
= 0; IndexOfPdpEntries
< NumberOfPdpEntriesNeeded
; IndexOfPdpEntries
++, PageDirectoryPointerEntry
++) {
612 // Each Directory Pointer entries points to a page of Page Directory entires.
613 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
615 PageDirectoryEntry
= (VOID
*) NextAllocAddress
;
616 NextAllocAddress
+= SIZE_4KB
;
619 // Fill in a Page Directory Pointer Entries
621 PageDirectoryPointerEntry
->Uint64
= (UINT64
)(UINTN
)PageDirectoryEntry
;
622 PageDirectoryPointerEntry
->Bits
.ReadWrite
= 1;
623 PageDirectoryPointerEntry
->Bits
.Present
= 1;
625 for (IndexOfPageDirectoryEntries
= 0; IndexOfPageDirectoryEntries
< 512; IndexOfPageDirectoryEntries
++, PageDirectoryEntry
++, PageAddress
+= SIZE_2MB
) {
627 // Fill in the Page Directory entries
629 PageDirectoryEntry
->Uint64
= (UINT64
)PageAddress
;
630 PageDirectoryEntry
->Bits
.ReadWrite
= 1;
631 PageDirectoryEntry
->Bits
.Present
= 1;
632 PageDirectoryEntry
->Bits
.MustBe1
= 1;
636 for (; IndexOfPdpEntries
< 512; IndexOfPdpEntries
++, PageDirectoryPointerEntry
++) {
638 PageDirectoryPointerEntry
,
639 sizeof(X64_PAGE_MAP_AND_DIRECTORY_POINTER
)
646 // For the PML4 entries we are not using fill in a null entry.
648 for (; IndexOfPml4Entries
< 512; IndexOfPml4Entries
++, PageMapLevel4Entry
++) {
651 sizeof (X64_PAGE_MAP_AND_DIRECTORY_POINTER
)
655 AsmWriteCr3 ((UINTN
) PageMap
);
660 Find and return Pei Core entry point.
662 It also find SEC and PEI Core file debug inforamtion. It will report them if
663 remote debug is enabled.
668 FindAndReportEntryPoints (
669 IN EFI_FIRMWARE_VOLUME_HEADER
**BootFirmwareVolumePtr
,
670 OUT EFI_PEI_CORE_ENTRY_POINT
*PeiCoreEntryPoint
674 EFI_PHYSICAL_ADDRESS SecCoreImageBase
;
675 EFI_PHYSICAL_ADDRESS PeiCoreImageBase
;
676 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
679 // Find SEC Core and PEI Core image base
681 Status
= FindImageBase (*BootFirmwareVolumePtr
, &SecCoreImageBase
);
682 ASSERT_EFI_ERROR (Status
);
684 FindPeiCoreImageBase (BootFirmwareVolumePtr
, &PeiCoreImageBase
);
686 ZeroMem ((VOID
*) &ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
688 // Report SEC Core debug information when remote debug is enabled
690 ImageContext
.ImageAddress
= SecCoreImageBase
;
691 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
692 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
695 // Report PEI Core debug information when remote debug is enabled
697 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)PeiCoreImageBase
;
698 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
699 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
702 // Find PEI Core entry point
704 Status
= PeCoffLoaderGetEntryPoint ((VOID
*) (UINTN
) PeiCoreImageBase
, (VOID
**) PeiCoreEntryPoint
);
705 if (EFI_ERROR (Status
)) {
706 *PeiCoreEntryPoint
= 0;
714 SecCoreStartupWithStack (
715 IN EFI_FIRMWARE_VOLUME_HEADER
*BootFv
,
716 IN VOID
*TopOfCurrentStack
719 EFI_SEC_PEI_HAND_OFF SecCoreData
;
720 SEC_IDT_TABLE IdtTableInStack
;
721 IA32_DESCRIPTOR IdtDescriptor
;
724 ProcessLibraryConstructorList (NULL
, NULL
);
727 "SecCoreStartupWithStack(0x%x, 0x%x)\n",
728 (UINT32
)(UINTN
)BootFv
,
729 (UINT32
)(UINTN
)TopOfCurrentStack
733 // Initialize floating point operating environment
734 // to be compliant with UEFI spec.
736 InitializeFloatingPointUnits ();
741 IdtTableInStack
.PeiService
= NULL
;
742 for (Index
= 0; Index
< SEC_IDT_ENTRY_COUNT
; Index
++) {
743 CopyMem (&IdtTableInStack
.IdtTable
[Index
], &mIdtEntryTemplate
, sizeof (mIdtEntryTemplate
));
746 IdtDescriptor
.Base
= (UINTN
)&IdtTableInStack
.IdtTable
;
747 IdtDescriptor
.Limit
= (UINT16
)(sizeof (IdtTableInStack
.IdtTable
) - 1);
749 AsmWriteIdtr (&IdtDescriptor
);
752 // |-------------| <-- TopOfCurrentStack
756 // |-------------| <-- SecCoreData.TemporaryRamBase
760 // Initialize SEC hand-off state
762 SecCoreData
.DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
764 SecCoreData
.TemporaryRamSize
= SIZE_64KB
;
765 SecCoreData
.TemporaryRamBase
= (VOID
*)((UINT8
*)TopOfCurrentStack
- SecCoreData
.TemporaryRamSize
);
767 SecCoreData
.PeiTemporaryRamBase
= SecCoreData
.TemporaryRamBase
;
768 SecCoreData
.PeiTemporaryRamSize
= SecCoreData
.TemporaryRamSize
>> 1;
770 SecCoreData
.StackBase
= (UINT8
*)SecCoreData
.TemporaryRamBase
+ SecCoreData
.PeiTemporaryRamSize
;
771 SecCoreData
.StackSize
= SecCoreData
.TemporaryRamSize
>> 1;
773 SecCoreData
.BootFirmwareVolumeBase
= BootFv
;
774 SecCoreData
.BootFirmwareVolumeSize
= (UINTN
) BootFv
->FvLength
;
777 // Make sure the 8259 is masked before initializing the Debug Agent and the debug timer is enabled
779 IoWrite8 (0x21, 0xff);
780 IoWrite8 (0xA1, 0xff);
782 #if defined (MDE_CPU_X64)
784 // Create Identity Mapped Pages in RAM
786 Create4GbIdentityMappingPageTables (TopOfCurrentStack
);
790 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.
792 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC
, &SecCoreData
, SecStartupPhase2
);
796 Caller provided function to be invoked at the end of InitializeDebugAgent().
798 Entry point to the C language phase of SEC. After the SEC assembly
799 code has initialized some temporary memory and set up the stack,
800 the control is transferred to this function.
802 @param[in] Context The first input parameter of InitializeDebugAgent().
811 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
812 EFI_FIRMWARE_VOLUME_HEADER
*BootFv
;
813 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint
;
815 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*) Context
;
818 // Find PEI Core entry point. It will report SEC and Pei Core debug information if remote debug
821 BootFv
= (EFI_FIRMWARE_VOLUME_HEADER
*)SecCoreData
->BootFirmwareVolumeBase
;
822 FindAndReportEntryPoints (&BootFv
, &PeiCoreEntryPoint
);
823 SecCoreData
->BootFirmwareVolumeBase
= BootFv
;
824 SecCoreData
->BootFirmwareVolumeSize
= (UINTN
) BootFv
->FvLength
;
827 // Transfer the control to the PEI core
829 (*PeiCoreEntryPoint
) (SecCoreData
, (EFI_PEI_PPI_DESCRIPTOR
*)&mPrivateDispatchTable
);
832 // If we get here then the PEI Core returned, which is not recoverable.
840 TemporaryRamMigration (
841 IN CONST EFI_PEI_SERVICES
**PeiServices
,
842 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
843 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
847 IA32_DESCRIPTOR IdtDescriptor
;
852 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext
;
854 BASE_LIBRARY_JUMP_BUFFER JumpBuffer
;
856 DEBUG ((EFI_D_ERROR
, "TemporaryRamMigration(0x%x, 0x%x, 0x%x)\n", (UINTN
)TemporaryMemoryBase
, (UINTN
)PermanentMemoryBase
, CopySize
));
858 OldHeap
= (VOID
*)(UINTN
)TemporaryMemoryBase
;
859 NewHeap
= (VOID
*)((UINTN
)PermanentMemoryBase
+ (CopySize
>> 1));
861 OldStack
= (VOID
*)((UINTN
)TemporaryMemoryBase
+ (CopySize
>> 1));
862 NewStack
= (VOID
*)(UINTN
)PermanentMemoryBase
;
864 DebugAgentContext
.HeapMigrateOffset
= (UINTN
)NewHeap
- (UINTN
)OldHeap
;
865 DebugAgentContext
.StackMigrateOffset
= (UINTN
)NewStack
- (UINTN
)OldStack
;
867 OldStatus
= SaveAndSetDebugTimerInterrupt (FALSE
);
868 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC
, (VOID
*) &DebugAgentContext
, NULL
);
873 CopyMem (NewHeap
, OldHeap
, CopySize
>> 1);
878 CopyMem (NewStack
, OldStack
, CopySize
>> 1);
881 // Rebase IDT table in permanent memory
883 AsmReadIdtr (&IdtDescriptor
);
884 IdtDescriptor
.Base
= IdtDescriptor
.Base
- (UINTN
)OldStack
+ (UINTN
)NewStack
;
886 AsmWriteIdtr (&IdtDescriptor
);
889 // Use SetJump()/LongJump() to switch to a new stack.
891 if (SetJump (&JumpBuffer
) == 0) {
892 #if defined (MDE_CPU_IA32)
893 JumpBuffer
.Esp
= JumpBuffer
.Esp
+ DebugAgentContext
.StackMigrateOffset
;
895 #if defined (MDE_CPU_X64)
896 JumpBuffer
.Rsp
= JumpBuffer
.Rsp
+ DebugAgentContext
.StackMigrateOffset
;
898 LongJump (&JumpBuffer
, (UINTN
)-1);
901 SaveAndSetDebugTimerInterrupt (OldStatus
);