2 Main SEC phase code. Transitions to PEI.
4 Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/PeimEntryPoint.h>
20 #include <Library/BaseLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/PeiServicesLib.h>
24 #include <Library/PcdLib.h>
25 #include <Library/UefiCpuLib.h>
26 #include <Library/DebugAgentLib.h>
27 #include <Library/IoLib.h>
28 #include <Library/PeCoffLib.h>
29 #include <Library/PeCoffGetEntryPointLib.h>
30 #include <Library/PeCoffExtraActionLib.h>
31 #include <Library/ExtractGuidedSectionLib.h>
32 #include <Library/LocalApicLib.h>
34 #include <Ppi/TemporaryRamSupport.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 The Instance parameter indicates which instance of the section
134 type to return. (0 is first instance, 1 is second...)
136 @param[in] Sections The sections to search
137 @param[in] SizeOfSections Total size of all sections
138 @param[in] SectionType The section type to locate
139 @param[in] Instance The section instance number
140 @param[out] FoundSection The FFS section if found
142 @retval EFI_SUCCESS The file and section was found
143 @retval EFI_NOT_FOUND The file and section was not found
144 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
148 FindFfsSectionInstance (
150 IN UINTN SizeOfSections
,
151 IN EFI_SECTION_TYPE SectionType
,
153 OUT EFI_COMMON_SECTION_HEADER
**FoundSection
156 EFI_PHYSICAL_ADDRESS CurrentAddress
;
158 EFI_PHYSICAL_ADDRESS EndOfSections
;
159 EFI_COMMON_SECTION_HEADER
*Section
;
160 EFI_PHYSICAL_ADDRESS EndOfSection
;
163 // Loop through the FFS file sections within the PEI Core FFS file
165 EndOfSection
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Sections
;
166 EndOfSections
= EndOfSection
+ SizeOfSections
;
168 if (EndOfSection
== EndOfSections
) {
171 CurrentAddress
= (EndOfSection
+ 3) & ~(3ULL);
172 if (CurrentAddress
>= EndOfSections
) {
173 return EFI_VOLUME_CORRUPTED
;
176 Section
= (EFI_COMMON_SECTION_HEADER
*)(UINTN
) CurrentAddress
;
178 Size
= SECTION_SIZE (Section
);
179 if (Size
< sizeof (*Section
)) {
180 return EFI_VOLUME_CORRUPTED
;
183 EndOfSection
= CurrentAddress
+ Size
;
184 if (EndOfSection
> EndOfSections
) {
185 return EFI_VOLUME_CORRUPTED
;
189 // Look for the requested section type
191 if (Section
->Type
== SectionType
) {
193 *FoundSection
= Section
;
201 return EFI_NOT_FOUND
;
205 Locates a section within a series of sections
206 with the specified section type.
208 @param[in] Sections The sections to search
209 @param[in] SizeOfSections Total size of all sections
210 @param[in] SectionType The section type to locate
211 @param[out] FoundSection The FFS section if found
213 @retval EFI_SUCCESS The file and section was found
214 @retval EFI_NOT_FOUND The file and section was not found
215 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
219 FindFfsSectionInSections (
221 IN UINTN SizeOfSections
,
222 IN EFI_SECTION_TYPE SectionType
,
223 OUT EFI_COMMON_SECTION_HEADER
**FoundSection
226 return FindFfsSectionInstance (
236 Locates a FFS file with the specified file type and a section
237 within that file with the specified section type.
239 @param[in] Fv The firmware volume to search
240 @param[in] FileType The file type to locate
241 @param[in] SectionType The section type to locate
242 @param[out] FoundSection The FFS section if found
244 @retval EFI_SUCCESS The file and section was found
245 @retval EFI_NOT_FOUND The file and section was not found
246 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
250 FindFfsFileAndSection (
251 IN EFI_FIRMWARE_VOLUME_HEADER
*Fv
,
252 IN EFI_FV_FILETYPE FileType
,
253 IN EFI_SECTION_TYPE SectionType
,
254 OUT EFI_COMMON_SECTION_HEADER
**FoundSection
258 EFI_PHYSICAL_ADDRESS CurrentAddress
;
259 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume
;
260 EFI_FFS_FILE_HEADER
*File
;
262 EFI_PHYSICAL_ADDRESS EndOfFile
;
264 if (Fv
->Signature
!= EFI_FVH_SIGNATURE
) {
265 DEBUG ((EFI_D_ERROR
, "FV at %p does not have FV header signature\n", Fv
));
266 return EFI_VOLUME_CORRUPTED
;
269 CurrentAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Fv
;
270 EndOfFirmwareVolume
= CurrentAddress
+ Fv
->FvLength
;
273 // Loop through the FFS files in the Boot Firmware Volume
275 for (EndOfFile
= CurrentAddress
+ Fv
->HeaderLength
; ; ) {
277 CurrentAddress
= (EndOfFile
+ 7) & ~(7ULL);
278 if (CurrentAddress
> EndOfFirmwareVolume
) {
279 return EFI_VOLUME_CORRUPTED
;
282 File
= (EFI_FFS_FILE_HEADER
*)(UINTN
) CurrentAddress
;
283 Size
= *(UINT32
*) File
->Size
& 0xffffff;
284 if (Size
< (sizeof (*File
) + sizeof (EFI_COMMON_SECTION_HEADER
))) {
285 return EFI_VOLUME_CORRUPTED
;
288 EndOfFile
= CurrentAddress
+ Size
;
289 if (EndOfFile
> EndOfFirmwareVolume
) {
290 return EFI_VOLUME_CORRUPTED
;
294 // Look for the request file type
296 if (File
->Type
!= FileType
) {
300 Status
= FindFfsSectionInSections (
302 (UINTN
) EndOfFile
- (UINTN
) (File
+ 1),
306 if (!EFI_ERROR (Status
) || (Status
== EFI_VOLUME_CORRUPTED
)) {
313 Locates the compressed main firmware volume and decompresses it.
315 @param[in,out] Fv On input, the firmware volume to search
316 On output, the decompressed BOOT/PEI FV
318 @retval EFI_SUCCESS The file and section was found
319 @retval EFI_NOT_FOUND The file and section was not found
320 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
325 IN OUT EFI_FIRMWARE_VOLUME_HEADER
**Fv
329 EFI_GUID_DEFINED_SECTION
*Section
;
330 UINT32 OutputBufferSize
;
331 UINT32 ScratchBufferSize
;
332 UINT16 SectionAttribute
;
333 UINT32 AuthenticationStatus
;
336 EFI_COMMON_SECTION_HEADER
*FvSection
;
337 EFI_FIRMWARE_VOLUME_HEADER
*PeiMemFv
;
338 EFI_FIRMWARE_VOLUME_HEADER
*DxeMemFv
;
340 UINT32 FvSectionSize
;
342 FvSection
= (EFI_COMMON_SECTION_HEADER
*) NULL
;
344 Status
= FindFfsFileAndSection (
346 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
,
347 EFI_SECTION_GUID_DEFINED
,
348 (EFI_COMMON_SECTION_HEADER
**) &Section
350 if (EFI_ERROR (Status
)) {
351 DEBUG ((EFI_D_ERROR
, "Unable to find GUID defined section\n"));
355 Status
= ExtractGuidedSectionGetInfo (
361 if (EFI_ERROR (Status
)) {
362 DEBUG ((EFI_D_ERROR
, "Unable to GetInfo for GUIDed section\n"));
366 OutputBuffer
= (VOID
*) ((UINT8
*)(UINTN
) PcdGet32 (PcdOvmfDxeMemFvBase
) + SIZE_1MB
);
367 ScratchBuffer
= ALIGN_POINTER ((UINT8
*) OutputBuffer
+ OutputBufferSize
, SIZE_1MB
);
369 DEBUG ((EFI_D_VERBOSE
, "%a: OutputBuffer@%p+0x%x ScratchBuffer@%p+0x%x "
370 "PcdOvmfDecompressionScratchEnd=0x%x\n", __FUNCTION__
, OutputBuffer
,
371 OutputBufferSize
, ScratchBuffer
, ScratchBufferSize
,
372 PcdGet32 (PcdOvmfDecompressionScratchEnd
)));
373 ASSERT ((UINTN
)ScratchBuffer
+ ScratchBufferSize
==
374 PcdGet32 (PcdOvmfDecompressionScratchEnd
));
376 Status
= ExtractGuidedSectionDecode (
380 &AuthenticationStatus
382 if (EFI_ERROR (Status
)) {
383 DEBUG ((EFI_D_ERROR
, "Error during GUID section decode\n"));
387 Status
= FindFfsSectionInstance (
390 EFI_SECTION_FIRMWARE_VOLUME_IMAGE
,
394 if (EFI_ERROR (Status
)) {
395 DEBUG ((EFI_D_ERROR
, "Unable to find PEI FV section\n"));
399 ASSERT (SECTION_SIZE (FvSection
) ==
400 (PcdGet32 (PcdOvmfPeiMemFvSize
) + sizeof (*FvSection
)));
401 ASSERT (FvSection
->Type
== EFI_SECTION_FIRMWARE_VOLUME_IMAGE
);
403 PeiMemFv
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
) PcdGet32 (PcdOvmfPeiMemFvBase
);
404 CopyMem (PeiMemFv
, (VOID
*) (FvSection
+ 1), PcdGet32 (PcdOvmfPeiMemFvSize
));
406 if (PeiMemFv
->Signature
!= EFI_FVH_SIGNATURE
) {
407 DEBUG ((EFI_D_ERROR
, "Extracted FV at %p does not have FV header signature\n", PeiMemFv
));
409 return EFI_VOLUME_CORRUPTED
;
412 Status
= FindFfsSectionInstance (
415 EFI_SECTION_FIRMWARE_VOLUME_IMAGE
,
419 if (EFI_ERROR (Status
)) {
420 DEBUG ((EFI_D_ERROR
, "Unable to find DXE FV section\n"));
424 ASSERT (FvSection
->Type
== EFI_SECTION_FIRMWARE_VOLUME_IMAGE
);
426 if (IS_SECTION2 (FvSection
)) {
427 FvSectionSize
= SECTION2_SIZE (FvSection
);
428 FvHeaderSize
= sizeof (EFI_COMMON_SECTION_HEADER2
);
430 FvSectionSize
= SECTION_SIZE (FvSection
);
431 FvHeaderSize
= sizeof (EFI_COMMON_SECTION_HEADER
);
434 ASSERT (FvSectionSize
== (PcdGet32 (PcdOvmfDxeMemFvSize
) + FvHeaderSize
));
436 DxeMemFv
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
) PcdGet32 (PcdOvmfDxeMemFvBase
);
437 CopyMem (DxeMemFv
, (VOID
*) ((UINTN
)FvSection
+ FvHeaderSize
), PcdGet32 (PcdOvmfDxeMemFvSize
));
439 if (DxeMemFv
->Signature
!= EFI_FVH_SIGNATURE
) {
440 DEBUG ((EFI_D_ERROR
, "Extracted FV at %p does not have FV header signature\n", DxeMemFv
));
442 return EFI_VOLUME_CORRUPTED
;
450 Locates the PEI Core entry point address
452 @param[in] Fv The firmware volume to search
453 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image
455 @retval EFI_SUCCESS The file and section was found
456 @retval EFI_NOT_FOUND The file and section was not found
457 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
461 FindPeiCoreImageBaseInFv (
462 IN EFI_FIRMWARE_VOLUME_HEADER
*Fv
,
463 OUT EFI_PHYSICAL_ADDRESS
*PeiCoreImageBase
467 EFI_COMMON_SECTION_HEADER
*Section
;
469 Status
= FindFfsFileAndSection (
471 EFI_FV_FILETYPE_PEI_CORE
,
475 if (EFI_ERROR (Status
)) {
476 Status
= FindFfsFileAndSection (
478 EFI_FV_FILETYPE_PEI_CORE
,
482 if (EFI_ERROR (Status
)) {
483 DEBUG ((EFI_D_ERROR
, "Unable to find PEI Core image\n"));
488 *PeiCoreImageBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(Section
+ 1);
494 Reads 8-bits of CMOS data.
496 Reads the 8-bits of CMOS data at the location specified by Index.
497 The 8-bit read value is returned.
499 @param Index The CMOS location to read.
501 @return The value read.
510 IoWrite8 (0x70, (UINT8
) Index
);
511 return IoRead8 (0x71);
521 return (CmosRead8 (0xF) == 0xFE);
528 IN OUT EFI_FIRMWARE_VOLUME_HEADER
**PeiFv
531 *PeiFv
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
) PcdGet32 (PcdOvmfPeiMemFvBase
);
537 Locates the PEI Core entry point address
539 @param[in,out] Fv The firmware volume to search
540 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image
542 @retval EFI_SUCCESS The file and section was found
543 @retval EFI_NOT_FOUND The file and section was not found
544 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
548 FindPeiCoreImageBase (
549 IN OUT EFI_FIRMWARE_VOLUME_HEADER
**BootFv
,
550 OUT EFI_PHYSICAL_ADDRESS
*PeiCoreImageBase
555 *PeiCoreImageBase
= 0;
557 S3Resume
= IsS3Resume ();
558 if (S3Resume
&& !FeaturePcdGet (PcdSmmSmramRequire
)) {
560 // A malicious runtime OS may have injected something into our previously
561 // decoded PEI FV, but we don't care about that unless SMM/SMRAM is required.
563 DEBUG ((EFI_D_VERBOSE
, "SEC: S3 resume\n"));
564 GetS3ResumePeiFv (BootFv
);
567 // We're either not resuming, or resuming "securely" -- we'll decompress
568 // both PEI FV and DXE FV from pristine flash.
570 DEBUG ((EFI_D_VERBOSE
, "SEC: %a\n",
571 S3Resume
? "S3 resume (with PEI decompression)" : "Normal boot"));
574 DecompressMemFvs (BootFv
);
577 FindPeiCoreImageBaseInFv (*BootFv
, PeiCoreImageBase
);
581 Find core image base.
586 IN EFI_FIRMWARE_VOLUME_HEADER
*BootFirmwareVolumePtr
,
587 OUT EFI_PHYSICAL_ADDRESS
*SecCoreImageBase
590 EFI_PHYSICAL_ADDRESS CurrentAddress
;
591 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume
;
592 EFI_FFS_FILE_HEADER
*File
;
594 EFI_PHYSICAL_ADDRESS EndOfFile
;
595 EFI_COMMON_SECTION_HEADER
*Section
;
596 EFI_PHYSICAL_ADDRESS EndOfSection
;
598 *SecCoreImageBase
= 0;
600 CurrentAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) BootFirmwareVolumePtr
;
601 EndOfFirmwareVolume
= CurrentAddress
+ BootFirmwareVolumePtr
->FvLength
;
604 // Loop through the FFS files in the Boot Firmware Volume
606 for (EndOfFile
= CurrentAddress
+ BootFirmwareVolumePtr
->HeaderLength
; ; ) {
608 CurrentAddress
= (EndOfFile
+ 7) & 0xfffffffffffffff8ULL
;
609 if (CurrentAddress
> EndOfFirmwareVolume
) {
610 return EFI_NOT_FOUND
;
613 File
= (EFI_FFS_FILE_HEADER
*)(UINTN
) CurrentAddress
;
614 Size
= *(UINT32
*) File
->Size
& 0xffffff;
615 if (Size
< sizeof (*File
)) {
616 return EFI_NOT_FOUND
;
619 EndOfFile
= CurrentAddress
+ Size
;
620 if (EndOfFile
> EndOfFirmwareVolume
) {
621 return EFI_NOT_FOUND
;
627 if (File
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
) {
632 // Loop through the FFS file sections within the FFS file
634 EndOfSection
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (File
+ 1);
636 CurrentAddress
= (EndOfSection
+ 3) & 0xfffffffffffffffcULL
;
637 Section
= (EFI_COMMON_SECTION_HEADER
*)(UINTN
) CurrentAddress
;
639 Size
= *(UINT32
*) Section
->Size
& 0xffffff;
640 if (Size
< sizeof (*Section
)) {
641 return EFI_NOT_FOUND
;
644 EndOfSection
= CurrentAddress
+ Size
;
645 if (EndOfSection
> EndOfFile
) {
646 return EFI_NOT_FOUND
;
650 // Look for executable sections
652 if (Section
->Type
== EFI_SECTION_PE32
|| Section
->Type
== EFI_SECTION_TE
) {
653 if (File
->Type
== EFI_FV_FILETYPE_SECURITY_CORE
) {
654 *SecCoreImageBase
= (PHYSICAL_ADDRESS
) (UINTN
) (Section
+ 1);
661 // SEC Core image found
663 if (*SecCoreImageBase
!= 0) {
670 Find and return Pei Core entry point.
672 It also find SEC and PEI Core file debug inforamtion. It will report them if
673 remote debug is enabled.
677 FindAndReportEntryPoints (
678 IN EFI_FIRMWARE_VOLUME_HEADER
**BootFirmwareVolumePtr
,
679 OUT EFI_PEI_CORE_ENTRY_POINT
*PeiCoreEntryPoint
683 EFI_PHYSICAL_ADDRESS SecCoreImageBase
;
684 EFI_PHYSICAL_ADDRESS PeiCoreImageBase
;
685 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
688 // Find SEC Core and PEI Core image base
690 Status
= FindImageBase (*BootFirmwareVolumePtr
, &SecCoreImageBase
);
691 ASSERT_EFI_ERROR (Status
);
693 FindPeiCoreImageBase (BootFirmwareVolumePtr
, &PeiCoreImageBase
);
695 ZeroMem ((VOID
*) &ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
697 // Report SEC Core debug information when remote debug is enabled
699 ImageContext
.ImageAddress
= SecCoreImageBase
;
700 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
701 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
704 // Report PEI Core debug information when remote debug is enabled
706 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)PeiCoreImageBase
;
707 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
708 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
711 // Find PEI Core entry point
713 Status
= PeCoffLoaderGetEntryPoint ((VOID
*) (UINTN
) PeiCoreImageBase
, (VOID
**) PeiCoreEntryPoint
);
714 if (EFI_ERROR (Status
)) {
715 *PeiCoreEntryPoint
= 0;
723 SecCoreStartupWithStack (
724 IN EFI_FIRMWARE_VOLUME_HEADER
*BootFv
,
725 IN VOID
*TopOfCurrentStack
728 EFI_SEC_PEI_HAND_OFF SecCoreData
;
729 SEC_IDT_TABLE IdtTableInStack
;
730 IA32_DESCRIPTOR IdtDescriptor
;
732 volatile UINT8
*Table
;
735 // To ensure SMM can't be compromised on S3 resume, we must force re-init of
736 // the BaseExtractGuidedSectionLib. Since this is before library contructors
737 // are called, we must use a loop rather than SetMem.
739 Table
= (UINT8
*)(UINTN
)FixedPcdGet64 (PcdGuidedExtractHandlerTableAddress
);
741 Index
< FixedPcdGet32 (PcdGuidedExtractHandlerTableSize
);
746 ProcessLibraryConstructorList (NULL
, NULL
);
749 "SecCoreStartupWithStack(0x%x, 0x%x)\n",
750 (UINT32
)(UINTN
)BootFv
,
751 (UINT32
)(UINTN
)TopOfCurrentStack
755 // Initialize floating point operating environment
756 // to be compliant with UEFI spec.
758 InitializeFloatingPointUnits ();
763 IdtTableInStack
.PeiService
= NULL
;
764 for (Index
= 0; Index
< SEC_IDT_ENTRY_COUNT
; Index
++) {
765 CopyMem (&IdtTableInStack
.IdtTable
[Index
], &mIdtEntryTemplate
, sizeof (mIdtEntryTemplate
));
768 IdtDescriptor
.Base
= (UINTN
)&IdtTableInStack
.IdtTable
;
769 IdtDescriptor
.Limit
= (UINT16
)(sizeof (IdtTableInStack
.IdtTable
) - 1);
771 AsmWriteIdtr (&IdtDescriptor
);
773 #if defined (MDE_CPU_X64)
775 // ASSERT that the Page Tables were set by the reset vector code to
776 // the address we expect.
778 ASSERT (AsmReadCr3 () == (UINTN
) PcdGet32 (PcdOvmfSecPageTablesBase
));
782 // |-------------| <-- TopOfCurrentStack
786 // |-------------| <-- SecCoreData.TemporaryRamBase
789 ASSERT ((UINTN
) (PcdGet32 (PcdOvmfSecPeiTempRamBase
) +
790 PcdGet32 (PcdOvmfSecPeiTempRamSize
)) ==
791 (UINTN
) TopOfCurrentStack
);
794 // Initialize SEC hand-off state
796 SecCoreData
.DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
798 SecCoreData
.TemporaryRamSize
= (UINTN
) PcdGet32 (PcdOvmfSecPeiTempRamSize
);
799 SecCoreData
.TemporaryRamBase
= (VOID
*)((UINT8
*)TopOfCurrentStack
- SecCoreData
.TemporaryRamSize
);
801 SecCoreData
.PeiTemporaryRamBase
= SecCoreData
.TemporaryRamBase
;
802 SecCoreData
.PeiTemporaryRamSize
= SecCoreData
.TemporaryRamSize
>> 1;
804 SecCoreData
.StackBase
= (UINT8
*)SecCoreData
.TemporaryRamBase
+ SecCoreData
.PeiTemporaryRamSize
;
805 SecCoreData
.StackSize
= SecCoreData
.TemporaryRamSize
>> 1;
807 SecCoreData
.BootFirmwareVolumeBase
= BootFv
;
808 SecCoreData
.BootFirmwareVolumeSize
= (UINTN
) BootFv
->FvLength
;
811 // Make sure the 8259 is masked before initializing the Debug Agent and the debug timer is enabled
813 IoWrite8 (0x21, 0xff);
814 IoWrite8 (0xA1, 0xff);
817 // Initialize Local APIC Timer hardware and disable Local APIC Timer
818 // interrupts before initializing the Debug Agent and the debug timer is
821 InitializeApicTimer (0, MAX_UINT32
, TRUE
, 5);
822 DisableApicTimerInterrupt ();
825 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.
827 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC
, &SecCoreData
, SecStartupPhase2
);
831 Caller provided function to be invoked at the end of InitializeDebugAgent().
833 Entry point to the C language phase of SEC. After the SEC assembly
834 code has initialized some temporary memory and set up the stack,
835 the control is transferred to this function.
837 @param[in] Context The first input parameter of InitializeDebugAgent().
846 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
847 EFI_FIRMWARE_VOLUME_HEADER
*BootFv
;
848 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint
;
850 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*) Context
;
853 // Find PEI Core entry point. It will report SEC and Pei Core debug information if remote debug
856 BootFv
= (EFI_FIRMWARE_VOLUME_HEADER
*)SecCoreData
->BootFirmwareVolumeBase
;
857 FindAndReportEntryPoints (&BootFv
, &PeiCoreEntryPoint
);
858 SecCoreData
->BootFirmwareVolumeBase
= BootFv
;
859 SecCoreData
->BootFirmwareVolumeSize
= (UINTN
) BootFv
->FvLength
;
862 // Transfer the control to the PEI core
864 (*PeiCoreEntryPoint
) (SecCoreData
, (EFI_PEI_PPI_DESCRIPTOR
*)&mPrivateDispatchTable
);
867 // If we get here then the PEI Core returned, which is not recoverable.
875 TemporaryRamMigration (
876 IN CONST EFI_PEI_SERVICES
**PeiServices
,
877 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
878 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
882 IA32_DESCRIPTOR IdtDescriptor
;
887 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext
;
889 BASE_LIBRARY_JUMP_BUFFER JumpBuffer
;
892 "TemporaryRamMigration(0x%Lx, 0x%Lx, 0x%Lx)\n",
898 OldHeap
= (VOID
*)(UINTN
)TemporaryMemoryBase
;
899 NewHeap
= (VOID
*)((UINTN
)PermanentMemoryBase
+ (CopySize
>> 1));
901 OldStack
= (VOID
*)((UINTN
)TemporaryMemoryBase
+ (CopySize
>> 1));
902 NewStack
= (VOID
*)(UINTN
)PermanentMemoryBase
;
904 DebugAgentContext
.HeapMigrateOffset
= (UINTN
)NewHeap
- (UINTN
)OldHeap
;
905 DebugAgentContext
.StackMigrateOffset
= (UINTN
)NewStack
- (UINTN
)OldStack
;
907 OldStatus
= SaveAndSetDebugTimerInterrupt (FALSE
);
908 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC
, (VOID
*) &DebugAgentContext
, NULL
);
913 CopyMem (NewHeap
, OldHeap
, CopySize
>> 1);
918 CopyMem (NewStack
, OldStack
, CopySize
>> 1);
921 // Rebase IDT table in permanent memory
923 AsmReadIdtr (&IdtDescriptor
);
924 IdtDescriptor
.Base
= IdtDescriptor
.Base
- (UINTN
)OldStack
+ (UINTN
)NewStack
;
926 AsmWriteIdtr (&IdtDescriptor
);
929 // Use SetJump()/LongJump() to switch to a new stack.
931 if (SetJump (&JumpBuffer
) == 0) {
932 #if defined (MDE_CPU_IA32)
933 JumpBuffer
.Esp
= JumpBuffer
.Esp
+ DebugAgentContext
.StackMigrateOffset
;
935 #if defined (MDE_CPU_X64)
936 JumpBuffer
.Rsp
= JumpBuffer
.Rsp
+ DebugAgentContext
.StackMigrateOffset
;
938 LongJump (&JumpBuffer
, (UINTN
)-1);
941 SaveAndSetDebugTimerInterrupt (OldStatus
);