]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/SecCore/SecMain.c
StandaloneMmPkg: Apply uncrustify changes
[mirror_edk2.git] / UefiCpuPkg / SecCore / SecMain.c
index 14696c81a5486cee036153e176483416995e6043..2416c4ce56b281ad66626ef95d4bbc6c64d8517e 100644 (file)
@@ -2,13 +2,7 @@
   C functions in SEC\r
 \r
   Copyright (c) 2008 - 2019, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -41,6 +35,43 @@ EFI_PEI_PPI_DESCRIPTOR            mPeiSecPlatformInformationPpi[] = {
   }\r
 };\r
 \r
+/**\r
+  Migrates the Global Descriptor Table (GDT) to permanent memory.\r
+\r
+  @retval   EFI_SUCCESS           The GDT was migrated successfully.\r
+  @retval   EFI_OUT_OF_RESOURCES  The GDT could not be migrated due to lack of available memory.\r
+\r
+**/\r
+EFI_STATUS\r
+MigrateGdt (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS          Status;\r
+  UINTN               GdtBufferSize;\r
+  IA32_DESCRIPTOR     Gdtr;\r
+  VOID                *GdtBuffer;\r
+\r
+  AsmReadGdtr ((IA32_DESCRIPTOR *) &Gdtr);\r
+  GdtBufferSize = sizeof (IA32_SEGMENT_DESCRIPTOR) -1 + Gdtr.Limit + 1;\r
+\r
+  Status =  PeiServicesAllocatePool (\r
+              GdtBufferSize,\r
+              &GdtBuffer\r
+              );\r
+  ASSERT (GdtBuffer != NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  GdtBuffer = ALIGN_POINTER (GdtBuffer, sizeof (IA32_SEGMENT_DESCRIPTOR));\r
+  CopyMem (GdtBuffer, (VOID *) Gdtr.Base, Gdtr.Limit + 1);\r
+  Gdtr.Base = (UINTN) GdtBuffer;\r
+  AsmWriteGdtr (&Gdtr);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 //\r
 // These are IDT entries pointing to 10:FFFFFFE4h.\r
 //\r
@@ -234,7 +265,6 @@ SecStartupPhase2(
 \r
   PeiCoreEntryPoint = NULL;\r
   SecCoreData   = (EFI_SEC_PEI_HAND_OFF *) Context;\r
-  AllSecPpiList = (EFI_PEI_PPI_DESCRIPTOR *) SecCoreData->PeiTemporaryRamBase;\r
 \r
   //\r
   // Perform platform specific initialization before entering PeiCore.\r
@@ -245,9 +275,8 @@ SecStartupPhase2(
   // is enabled.\r
   //\r
   if (PpiList != NULL) {\r
-    for (Index = 0;\r
-      (PpiList[Index].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;\r
-      Index++) {\r
+    Index = 0;\r
+    do {\r
       if (CompareGuid (PpiList[Index].Guid, &gEfiPeiCoreFvLocationPpiGuid) &&\r
           (((EFI_PEI_CORE_FV_LOCATION_PPI *) PpiList[Index].Ppi)->PeiCoreFvLocation != 0)\r
          ) {\r
@@ -263,12 +292,12 @@ SecStartupPhase2(
           break;\r
         } else {\r
           //\r
-          // PeiCore not found\r
+          // Invalid PeiCore FV provided by platform\r
           //\r
           CpuDeadLoop ();\r
         }\r
       }\r
-    }\r
+    } while ((PpiList[Index++].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
   }\r
   //\r
   // If EFI_PEI_CORE_FV_LOCATION_PPI not found, try to locate PeiCore from BFV.\r
@@ -288,6 +317,8 @@ SecStartupPhase2(
   }\r
 \r
   if (PpiList != NULL) {\r
+    AllSecPpiList = (EFI_PEI_PPI_DESCRIPTOR *) SecCoreData->PeiTemporaryRamBase;\r
+\r
     //\r
     // Remove the terminal flag from the terminal PPI\r
     //\r
@@ -376,13 +407,35 @@ SecTemporaryRamDone (
   VOID\r
   )\r
 {\r
-  BOOLEAN  State;\r
+  EFI_STATUS                    Status;\r
+  EFI_STATUS                    Status2;\r
+  UINTN                         Index;\r
+  BOOLEAN                       State;\r
+  EFI_PEI_PPI_DESCRIPTOR        *PeiPpiDescriptor;\r
+  REPUBLISH_SEC_PPI_PPI         *RepublishSecPpiPpi;\r
 \r
   //\r
   // Republish Sec Platform Information(2) PPI\r
   //\r
   RepublishSecPlatformInformationPpi ();\r
 \r
+  //\r
+  // Re-install SEC PPIs using a PEIM produced service if published\r
+  //\r
+  for (Index = 0, Status = EFI_SUCCESS; Status == EFI_SUCCESS; Index++) {\r
+    Status = PeiServicesLocatePpi (\r
+               &gRepublishSecPpiPpiGuid,\r
+               Index,\r
+               &PeiPpiDescriptor,\r
+               (VOID **) &RepublishSecPpiPpi\r
+               );\r
+    if (!EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_INFO, "Calling RepublishSecPpi instance %d.\n", Index));\r
+      Status2 = RepublishSecPpiPpi->RepublishSecPpis ();\r
+      ASSERT_EFI_ERROR (Status2);\r
+    }\r
+  }\r
+\r
   //\r
   // Migrate DebugAgentContext.\r
   //\r
@@ -391,7 +444,15 @@ SecTemporaryRamDone (
   //\r
   // Disable interrupts and save current interrupt state\r
   //\r
-  State = SaveAndDisableInterrupts();\r
+  State = SaveAndDisableInterrupts ();\r
+\r
+  //\r
+  // Migrate GDT before NEM near down\r
+  //\r
+  if (PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes)) {\r
+    Status = MigrateGdt ();\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
 \r
   //\r
   // Disable Temporary RAM after Stack and Heap have been migrated at this point.\r