36#define SEC_IDT_ENTRY_COUNT 34
40 IA32_IDT_GATE_DESCRIPTOR IdtTable[SEC_IDT_ENTRY_COUNT];
67 (EFI_PEI_PPI_DESCRIPTOR_PPI),
68 &gEfiTemporaryRamSupportPpiGuid,
69 &mTemporaryRamSupportPpi
72 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
73 &gEfiPeiMpInitLibMpDepPpiGuid,
80 (EFI_PEI_PPI_DESCRIPTOR_PPI),
81 &gEfiTemporaryRamSupportPpiGuid,
82 &mTemporaryRamSupportPpi
85 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
86 &gEfiPeiMpInitLibUpDepPpiGuid,
94IA32_IDT_GATE_DESCRIPTOR mIdtEntryTemplate = {
99 IA32_IDT_GATE_TYPE_INTERRUPT_32,
122 ASSERT (((
UINTN)*BootFv & EFI_PAGE_MASK) == 0);
128 Distance += EFI_PAGE_SIZE;
129 if (Distance > SIZE_32MB) {
130 return EFI_NOT_FOUND;
133 if (Fv->
Signature != EFI_FVH_SIGNATURE) {
168 IN EFI_SECTION_TYPE SectionType,
183 EndOfSections = EndOfSection + SizeOfSections;
185 if (EndOfSection == EndOfSections) {
189 CurrentAddress = (EndOfSection + 3) & ~(3ULL);
190 if (CurrentAddress >= EndOfSections) {
191 return EFI_VOLUME_CORRUPTED;
197 if (Size <
sizeof (*Section)) {
198 return EFI_VOLUME_CORRUPTED;
201 EndOfSection = CurrentAddress + Size;
202 if (EndOfSection > EndOfSections) {
203 return EFI_VOLUME_CORRUPTED;
209 if (Section->Type == SectionType) {
211 *FoundSection = Section;
219 return EFI_NOT_FOUND;
240 IN EFI_SECTION_TYPE SectionType,
270 IN EFI_FV_FILETYPE FileType,
271 IN EFI_SECTION_TYPE SectionType,
282 if (Fv->Signature != EFI_FVH_SIGNATURE) {
283 DEBUG ((DEBUG_ERROR,
"FV at %p does not have FV header signature\n", Fv));
284 return EFI_VOLUME_CORRUPTED;
288 EndOfFirmwareVolume = CurrentAddress + Fv->FvLength;
293 for (EndOfFile = CurrentAddress + Fv->HeaderLength; ; ) {
294 CurrentAddress = (EndOfFile + 7) & ~(7ULL);
295 if (CurrentAddress > EndOfFirmwareVolume) {
296 return EFI_VOLUME_CORRUPTED;
302 return EFI_VOLUME_CORRUPTED;
305 EndOfFile = CurrentAddress + Size;
306 if (EndOfFile > EndOfFirmwareVolume) {
307 return EFI_VOLUME_CORRUPTED;
313 if (File->
Type != FileType) {
323 if (!EFI_ERROR (Status) || (Status == EFI_VOLUME_CORRUPTED)) {
347 UINT32 OutputBufferSize;
348 UINT32 ScratchBufferSize;
349 UINT16 SectionAttribute;
350 UINT32 AuthenticationStatus;
357 UINT32 FvSectionSize;
363 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
364 EFI_SECTION_GUID_DEFINED,
367 if (EFI_ERROR (Status)) {
368 DEBUG ((DEBUG_ERROR,
"Unable to find GUID defined section\n"));
378 if (EFI_ERROR (Status)) {
379 DEBUG ((DEBUG_ERROR,
"Unable to GetInfo for GUIDed section\n"));
383 OutputBuffer = (VOID *)((UINT8 *)(
UINTN)
PcdGet32 (PcdOvmfDxeMemFvBase) + SIZE_1MB);
384 ScratchBuffer =
ALIGN_POINTER ((UINT8 *)OutputBuffer + OutputBufferSize, SIZE_1MB);
388 "%a: OutputBuffer@%p+0x%x ScratchBuffer@%p+0x%x "
389 "PcdOvmfDecompressionScratchEnd=0x%x\n",
395 PcdGet32 (PcdOvmfDecompressionScratchEnd)
398 (
UINTN)ScratchBuffer + ScratchBufferSize ==
399 PcdGet32 (PcdOvmfDecompressionScratchEnd)
402 Status = ExtractGuidedSectionDecode (
406 &AuthenticationStatus
408 if (EFI_ERROR (Status)) {
409 DEBUG ((DEBUG_ERROR,
"Error during GUID section decode\n"));
416 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
420 if (EFI_ERROR (Status)) {
421 DEBUG ((DEBUG_ERROR,
"Unable to find PEI FV section\n"));
427 (
PcdGet32 (PcdOvmfPeiMemFvSize) +
sizeof (*FvSection))
429 ASSERT (FvSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE);
432 CopyMem (PeiMemFv, (VOID *)(FvSection + 1),
PcdGet32 (PcdOvmfPeiMemFvSize));
434 if (PeiMemFv->
Signature != EFI_FVH_SIGNATURE) {
435 DEBUG ((DEBUG_ERROR,
"Extracted FV at %p does not have FV header signature\n", PeiMemFv));
437 return EFI_VOLUME_CORRUPTED;
443 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
447 if (EFI_ERROR (Status)) {
448 DEBUG ((DEBUG_ERROR,
"Unable to find DXE FV section\n"));
452 ASSERT (FvSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE);
454 if (IS_SECTION2 (FvSection)) {
455 FvSectionSize = SECTION2_SIZE (FvSection);
462 ASSERT (FvSectionSize == (
PcdGet32 (PcdOvmfDxeMemFvSize) + FvHeaderSize));
467 if (DxeMemFv->
Signature != EFI_FVH_SIGNATURE) {
468 DEBUG ((DEBUG_ERROR,
"Extracted FV at %p does not have FV header signature\n", DxeMemFv));
470 return EFI_VOLUME_CORRUPTED;
499 EFI_FV_FILETYPE_PEI_CORE,
503 if (EFI_ERROR (Status)) {
506 EFI_FV_FILETYPE_PEI_CORE,
510 if (EFI_ERROR (Status)) {
511 DEBUG ((DEBUG_ERROR,
"Unable to find PEI Core image\n"));
579 *PeiCoreImageBase = 0;
581 S3Resume = IsS3Resume ();
587 DEBUG ((DEBUG_VERBOSE,
"SEC: S3 resume\n"));
588 GetS3ResumePeiFv (BootFv);
597 S3Resume ?
"S3 resume (with PEI decompression)" :
"Normal boot"
625 *SecCoreImageBase = 0;
628 EndOfFirmwareVolume = CurrentAddress + BootFirmwareVolumePtr->FvLength;
633 for (EndOfFile = CurrentAddress + BootFirmwareVolumePtr->HeaderLength; ; ) {
634 CurrentAddress = (EndOfFile + 7) & 0xfffffffffffffff8ULL;
635 if (CurrentAddress > EndOfFirmwareVolume) {
636 return EFI_NOT_FOUND;
641 if (Size <
sizeof (*File)) {
642 return EFI_NOT_FOUND;
645 EndOfFile = CurrentAddress + Size;
646 if (EndOfFile > EndOfFirmwareVolume) {
647 return EFI_NOT_FOUND;
653 if (File->
Type != EFI_FV_FILETYPE_SECURITY_CORE) {
662 CurrentAddress = (EndOfSection + 3) & 0xfffffffffffffffcULL;
666 if (Size <
sizeof (*Section)) {
667 return EFI_NOT_FOUND;
670 EndOfSection = CurrentAddress + Size;
671 if (EndOfSection > EndOfFile) {
672 return EFI_NOT_FOUND;
678 if ((Section->Type ==
EFI_SECTION_PE32) || (Section->Type == EFI_SECTION_TE)) {
679 if (File->
Type == EFI_FV_FILETYPE_SECURITY_CORE) {
680 *SecCoreImageBase = (PHYSICAL_ADDRESS)(
UINTN)(Section + 1);
690 if (*SecCoreImageBase != 0) {
717 Status =
FindImageBase (*BootFirmwareVolumePtr, &SecCoreImageBase);
726 ImageContext.ImageAddress = SecCoreImageBase;
727 ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)(
UINTN)ImageContext.ImageAddress);
734 ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)(
UINTN)ImageContext.ImageAddress);
740 Status = PeCoffLoaderGetEntryPoint ((VOID *)(
UINTN)PeiCoreImageBase, (VOID **)PeiCoreEntryPoint);
741 if (EFI_ERROR (Status)) {
742 *PeiCoreEntryPoint = 0;
763 if (!Edx.Bits.MTRR) {
767 #if defined (TDX_GUEST_SUPPORTED)
768 if (
CcProbe () == CcGuestTypeIntelTdx) {
776 DEBUG ((DEBUG_INFO,
"%a: Skip TD-Guest\n", __func__));
783 DefType.
Bits.
Type = MSR_IA32_MTRR_CACHE_WRITE_BACK;
790SecCoreStartupWithStack (
792 IN VOID *TopOfCurrentStack
797 IA32_DESCRIPTOR IdtDescriptor;
799 volatile UINT8 *Table;
801 #if defined (TDX_GUEST_SUPPORTED)
802 if (
CcProbe () == CcGuestTypeIntelTdx) {
845 IdtTableInStack.PeiService =
NULL;
847 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index++) {
857 Src = (
CONST UINT8 *)&mIdtEntryTemplate;
858 Dst = (
volatile UINT8 *)&IdtTableInStack.IdtTable[Index];
859 for (Byte = 0; Byte <
sizeof (mIdtEntryTemplate); Byte++) {
860 Dst[Byte] = Src[Byte];
864 IdtDescriptor.Base = (
UINTN)&IdtTableInStack.IdtTable;
865 IdtDescriptor.Limit = (UINT16)(
sizeof (IdtTableInStack.IdtTable) - 1);
885 InitializeCpuExceptionHandlers (
NULL);
888 ProcessLibraryConstructorList ();
904 #if defined (TDX_GUEST_SUPPORTED)
905 if (
CcProbe () == CcGuestTypeIntelTdx) {
910 InitializeCpuExceptionHandlers (
NULL);
917 "SecCoreStartupWithStack(0x%x, 0x%x)\n",
918 (UINT32)(
UINTN)BootFv,
919 (UINT32)(
UINTN)TopOfCurrentStack
928 #if defined (MDE_CPU_X64)
946 PcdGet32 (PcdOvmfSecPeiTempRamSize)) ==
947 (
UINTN)TopOfCurrentStack
1034 if (
CcProbe () == CcGuestTypeIntelTdx) {
1043 (*PeiCoreEntryPoint)(SecCoreData, EfiPeiPpiDescriptor);
1061 IA32_DESCRIPTOR IdtDescriptor;
1068 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
1072 "TemporaryRamMigration(0x%Lx, 0x%Lx, 0x%Lx)\n",
1073 TemporaryMemoryBase,
1074 PermanentMemoryBase,
1078 OldHeap = (VOID *)(
UINTN)TemporaryMemoryBase;
1079 NewHeap = (VOID *)((
UINTN)PermanentMemoryBase + (CopySize >> 1));
1081 OldStack = (VOID *)((
UINTN)TemporaryMemoryBase + (CopySize >> 1));
1082 NewStack = (VOID *)(
UINTN)PermanentMemoryBase;
1084 DebugAgentContext.HeapMigrateOffset = (
UINTN)NewHeap - (
UINTN)OldHeap;
1085 DebugAgentContext.StackMigrateOffset = (
UINTN)NewStack - (
UINTN)OldStack;
1093 CopyMem (NewHeap, OldHeap, CopySize >> 1);
1098 CopyMem (NewStack, OldStack, CopySize >> 1);
1104 IdtDescriptor.Base = IdtDescriptor.Base - (
UINTN)OldStack + (
UINTN)NewStack;
1111 if (
SetJump (&JumpBuffer) == 0) {
1112 #if defined (MDE_CPU_IA32)
1113 JumpBuffer.Esp = JumpBuffer.Esp + DebugAgentContext.StackMigrateOffset;
1114 JumpBuffer.Ebp = JumpBuffer.Ebp + DebugAgentContext.StackMigrateOffset;
1116 #if defined (MDE_CPU_X64)
1117 JumpBuffer.Rsp = JumpBuffer.Rsp + DebugAgentContext.StackMigrateOffset;
1118 JumpBuffer.Rbp = JumpBuffer.Rbp + DebugAgentContext.StackMigrateOffset;
RETURNS_TWICE UINTN EFIAPI SetJump(OUT BASE_LIBRARY_JUMP_BUFFER *JumpBuffer)
VOID EFIAPI CpuDeadLoop(VOID)
VOID EFIAPI LongJump(IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer, IN UINTN Value)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
UINT8 EFIAPI CcProbe(VOID)
BOOLEAN EFIAPI SaveAndSetDebugTimerInterrupt(IN BOOLEAN EnableStatus)
VOID EFIAPI InitializeDebugAgent(IN UINT32 InitFlag, IN VOID *Context OPTIONAL, IN DEBUG_AGENT_CONTINUE Function OPTIONAL)
VOID EFIAPI AsmEnableCache(VOID)
UINTN EFIAPI AsmReadCr3(VOID)
UINT64 EFIAPI AsmReadMsr64(IN UINT32 Index)
UINT64 EFIAPI AsmWriteMsr64(IN UINT32 Index, IN UINT64 Value)
UINT8 EFIAPI IoWrite8(IN UINTN Port, IN UINT8 Value)
UINT8 EFIAPI IoRead8(IN UINTN Port)
VOID EFIAPI InitializeApicTimer(IN UINTN DivideValue, IN UINT32 InitCount, IN BOOLEAN PeriodicMode, IN UINT8 Vector)
VOID EFIAPI DisableApicTimerInterrupt(VOID)
#define ALIGN_POINTER(Pointer, Alignment)
#define ASSERT_EFI_ERROR(StatusParameter)
#define DEBUG(Expression)
#define MSR_IA32_MTRR_DEF_TYPE
#define CPUID_VERSION_INFO
UINT32 EFIAPI AsmCpuid(IN UINT32 Index, OUT UINT32 *RegisterEax OPTIONAL, OUT UINT32 *RegisterEbx OPTIONAL, OUT UINT32 *RegisterEcx OPTIONAL, OUT UINT32 *RegisterEdx OPTIONAL)
STATIC VOID EFIAPI SecStartupPhase2(IN VOID *Context)
STATIC EFI_STATUS FindPeiCoreImageBaseInFv(IN EFI_FIRMWARE_VOLUME_HEADER *Fv, OUT EFI_PHYSICAL_ADDRESS *PeiCoreImageBase)
STATIC EFI_STATUS FindFfsSectionInstance(IN VOID *Sections, IN UINTN SizeOfSections, IN EFI_SECTION_TYPE SectionType, IN UINTN Instance, OUT EFI_COMMON_SECTION_HEADER **FoundSection)
STATIC VOID FindAndReportEntryPoints(IN EFI_FIRMWARE_VOLUME_HEADER **BootFirmwareVolumePtr, OUT EFI_PEI_CORE_ENTRY_POINT *PeiCoreEntryPoint)
STATIC EFI_STATUS EFIAPI TemporaryRamMigration(IN CONST EFI_PEI_SERVICES **PeiServices, IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase, IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase, IN UINTN CopySize)
STATIC EFI_STATUS FindFfsFileAndSection(IN EFI_FIRMWARE_VOLUME_HEADER *Fv, IN EFI_FV_FILETYPE FileType, IN EFI_SECTION_TYPE SectionType, OUT EFI_COMMON_SECTION_HEADER **FoundSection)
STATIC EFI_STATUS FindFfsSectionInSections(IN VOID *Sections, IN UINTN SizeOfSections, IN EFI_SECTION_TYPE SectionType, OUT EFI_COMMON_SECTION_HEADER **FoundSection)
VOID SecValidateSystemRam(VOID)
VOID SecMapApicBaseUnencrypted(VOID)
VOID SevEsProtocolCheck(VOID)
BOOLEAN SevEsIsEnabled(VOID)
VOID FindPeiCoreImageBase(IN OUT EFI_FIRMWARE_VOLUME_HEADER **BootFv, OUT EFI_PHYSICAL_ADDRESS *PeiCoreImageBase)
EFI_STATUS FindImageBase(IN EFI_FIRMWARE_VOLUME_HEADER *BootFirmwareVolumePtr, OUT EFI_PHYSICAL_ADDRESS *SecCoreImageBase)
STATIC UINT8 CmosRead8(IN UINTN Index)
EFI_STATUS DecompressMemFvs(IN OUT EFI_FIRMWARE_VOLUME_HEADER **Fv)
EFI_STATUS FindMainFv(IN OUT EFI_FIRMWARE_VOLUME_HEADER **BootFv)
#define FixedPcdGet32(TokenName)
#define FixedPcdGet64(TokenName)
#define PcdGet32(TokenName)
#define FeaturePcdGet(TokenName)
#define FFS_FILE_SIZE(FfsFileHeaderPtr)
#define SECTION_SIZE(SectionHeaderPtr)
VOID(EFIAPI * EFI_PEI_CORE_ENTRY_POINT)(IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData, IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList)
struct _EFI_SEC_PEI_HAND_OFF EFI_SEC_PEI_HAND_OFF
EFI_STATUS EFIAPI TdxHelperProcessTdHob(VOID)
EFI_STATUS EFIAPI TdxHelperMeasureTdHob(VOID)
EFI_STATUS EFIAPI TdxHelperMeasureCfvImage(VOID)
UINT64 EFI_PHYSICAL_ADDRESS
VOID EFIAPI InitializeFloatingPointUnits(VOID)
VOID EFIAPI AsmReadIdtr(OUT IA32_DESCRIPTOR *Idtr)
VOID EFIAPI AsmWriteIdtr(IN CONST IA32_DESCRIPTOR *Idtr)
UINTN PeiTemporaryRamSize
VOID * BootFirmwareVolumeBase
UINTN BootFirmwareVolumeSize
VOID * PeiTemporaryRamBase
struct MSR_IA32_MTRR_DEF_TYPE_REGISTER::@653 Bits