]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Sec/SecMain.c
OvmfPkg/SecMain: Fix stack switching to permanent memory
[mirror_edk2.git] / OvmfPkg / Sec / SecMain.c
index 93e3594e29aa6d0c39eefea3165b38c698e2ca65..f7fec3d8c03b93d73042e480e4400be41f22ce32 100644 (file)
@@ -2,6 +2,7 @@
   Main SEC phase code.  Transitions to PEI.\r
 \r
   Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
   Main SEC phase code.  Transitions to PEI.\r
 \r
   Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -332,11 +333,13 @@ DecompressMemFvs (
   UINT32                            AuthenticationStatus;\r
   VOID                              *OutputBuffer;\r
   VOID                              *ScratchBuffer;\r
   UINT32                            AuthenticationStatus;\r
   VOID                              *OutputBuffer;\r
   VOID                              *ScratchBuffer;\r
-  EFI_FIRMWARE_VOLUME_IMAGE_SECTION *FvSection;\r
+  EFI_COMMON_SECTION_HEADER         *FvSection;\r
   EFI_FIRMWARE_VOLUME_HEADER        *PeiMemFv;\r
   EFI_FIRMWARE_VOLUME_HEADER        *DxeMemFv;\r
   EFI_FIRMWARE_VOLUME_HEADER        *PeiMemFv;\r
   EFI_FIRMWARE_VOLUME_HEADER        *DxeMemFv;\r
+  UINT32                            FvHeaderSize;\r
+  UINT32                            FvSectionSize;\r
 \r
 \r
-  FvSection = (EFI_FIRMWARE_VOLUME_IMAGE_SECTION*) NULL;\r
+  FvSection = (EFI_COMMON_SECTION_HEADER*) NULL;\r
 \r
   Status = FindFfsFileAndSection (\r
              *Fv,\r
 \r
   Status = FindFfsFileAndSection (\r
              *Fv,\r
@@ -386,7 +389,7 @@ DecompressMemFvs (
              OutputBufferSize,\r
              EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
              0,\r
              OutputBufferSize,\r
              EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
              0,\r
-             (EFI_COMMON_SECTION_HEADER**) &FvSection\r
+             &FvSection\r
              );\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "Unable to find PEI FV section\n"));\r
              );\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "Unable to find PEI FV section\n"));\r
@@ -411,7 +414,7 @@ DecompressMemFvs (
              OutputBufferSize,\r
              EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
              1,\r
              OutputBufferSize,\r
              EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
              1,\r
-             (EFI_COMMON_SECTION_HEADER**) &FvSection\r
+             &FvSection\r
              );\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "Unable to find DXE FV section\n"));\r
              );\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "Unable to find DXE FV section\n"));\r
@@ -419,11 +422,19 @@ DecompressMemFvs (
   }\r
 \r
   ASSERT (FvSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE);\r
   }\r
 \r
   ASSERT (FvSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE);\r
-  ASSERT (SECTION_SIZE (FvSection) ==\r
-          (PcdGet32 (PcdOvmfDxeMemFvSize) + sizeof (*FvSection)));\r
+\r
+  if (IS_SECTION2 (FvSection)) {\r
+    FvSectionSize = SECTION2_SIZE (FvSection);\r
+    FvHeaderSize = sizeof (EFI_COMMON_SECTION_HEADER2);\r
+  } else {\r
+    FvSectionSize = SECTION_SIZE (FvSection);\r
+    FvHeaderSize = sizeof (EFI_COMMON_SECTION_HEADER);\r
+  }\r
+\r
+  ASSERT (FvSectionSize == (PcdGet32 (PcdOvmfDxeMemFvSize) + FvHeaderSize));\r
 \r
   DxeMemFv = (EFI_FIRMWARE_VOLUME_HEADER*)(UINTN) PcdGet32 (PcdOvmfDxeMemFvBase);\r
 \r
   DxeMemFv = (EFI_FIRMWARE_VOLUME_HEADER*)(UINTN) PcdGet32 (PcdOvmfDxeMemFvBase);\r
-  CopyMem (DxeMemFv, (VOID*) (FvSection + 1), PcdGet32 (PcdOvmfDxeMemFvSize));\r
+  CopyMem (DxeMemFv, (VOID*) ((UINTN)FvSection + FvHeaderSize), PcdGet32 (PcdOvmfDxeMemFvSize));\r
 \r
   if (DxeMemFv->Signature != EFI_FVH_SIGNATURE) {\r
     DEBUG ((EFI_D_ERROR, "Extracted FV at %p does not have FV header signature\n", DxeMemFv));\r
 \r
   if (DxeMemFv->Signature != EFI_FVH_SIGNATURE) {\r
     DEBUG ((EFI_D_ERROR, "Extracted FV at %p does not have FV header signature\n", DxeMemFv));\r
@@ -539,13 +550,25 @@ FindPeiCoreImageBase (
      OUT  EFI_PHYSICAL_ADDRESS             *PeiCoreImageBase\r
   )\r
 {\r
      OUT  EFI_PHYSICAL_ADDRESS             *PeiCoreImageBase\r
   )\r
 {\r
+  BOOLEAN S3Resume;\r
+\r
   *PeiCoreImageBase = 0;\r
 \r
   *PeiCoreImageBase = 0;\r
 \r
-  if (IsS3Resume ()) {\r
+  S3Resume = IsS3Resume ();\r
+  if (S3Resume && !FeaturePcdGet (PcdSmmSmramRequire)) {\r
+    //\r
+    // A malicious runtime OS may have injected something into our previously\r
+    // decoded PEI FV, but we don't care about that unless SMM/SMRAM is required.\r
+    //\r
     DEBUG ((EFI_D_VERBOSE, "SEC: S3 resume\n"));\r
     GetS3ResumePeiFv (BootFv);\r
   } else {\r
     DEBUG ((EFI_D_VERBOSE, "SEC: S3 resume\n"));\r
     GetS3ResumePeiFv (BootFv);\r
   } else {\r
-    DEBUG ((EFI_D_VERBOSE, "SEC: Normal boot\n"));\r
+    //\r
+    // We're either not resuming, or resuming "securely" -- we'll decompress\r
+    // both PEI FV and DXE FV from pristine flash.\r
+    //\r
+    DEBUG ((EFI_D_VERBOSE, "SEC: %a\n",\r
+      S3Resume ? "S3 resume (with PEI decompression)" : "Normal boot"));\r
     FindMainFv (BootFv);\r
 \r
     DecompressMemFvs (BootFv);\r
     FindMainFv (BootFv);\r
 \r
     DecompressMemFvs (BootFv);\r
@@ -646,7 +669,7 @@ FindImageBase (
 /*\r
   Find and return Pei Core entry point.\r
 \r
 /*\r
   Find and return Pei Core entry point.\r
 \r
-  It also find SEC and PEI Core file debug inforamtion. It will report them if\r
+  It also find SEC and PEI Core file debug information. It will report them if\r
   remote debug is enabled.\r
 \r
 **/\r
   remote debug is enabled.\r
 \r
 **/\r
@@ -908,9 +931,11 @@ TemporaryRamMigration (
   if (SetJump (&JumpBuffer) == 0) {\r
 #if defined (MDE_CPU_IA32)\r
     JumpBuffer.Esp = JumpBuffer.Esp + DebugAgentContext.StackMigrateOffset;\r
   if (SetJump (&JumpBuffer) == 0) {\r
 #if defined (MDE_CPU_IA32)\r
     JumpBuffer.Esp = JumpBuffer.Esp + DebugAgentContext.StackMigrateOffset;\r
+    JumpBuffer.Ebp = JumpBuffer.Ebp + DebugAgentContext.StackMigrateOffset;\r
 #endif    \r
 #if defined (MDE_CPU_X64)\r
     JumpBuffer.Rsp = JumpBuffer.Rsp + DebugAgentContext.StackMigrateOffset;\r
 #endif    \r
 #if defined (MDE_CPU_X64)\r
     JumpBuffer.Rsp = JumpBuffer.Rsp + DebugAgentContext.StackMigrateOffset;\r
+    JumpBuffer.Rbp = JumpBuffer.Rbp + DebugAgentContext.StackMigrateOffset;\r
 #endif    \r
     LongJump (&JumpBuffer, (UINTN)-1);\r
   }\r
 #endif    \r
     LongJump (&JumpBuffer, (UINTN)-1);\r
   }\r