]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFspWrapperPkg/FspInitPei/SecMain.c
Add IntelFspWrapper to support boot EDKII on FSP bin.
[mirror_edk2.git] / IntelFspWrapperPkg / FspInitPei / SecMain.c
diff --git a/IntelFspWrapperPkg/FspInitPei/SecMain.c b/IntelFspWrapperPkg/FspInitPei/SecMain.c
new file mode 100644 (file)
index 0000000..10550e7
--- /dev/null
@@ -0,0 +1,269 @@
+/** @file\r
+  C functions in SEC\r
+\r
+  Copyright (c) 2014, 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
+\r
+**/\r
+\r
+\r
+#include "SecMain.h"\r
+\r
+EFI_PEI_PPI_DESCRIPTOR            mPeiSecMainPpi[] = {\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_PPI,\r
+    &gTopOfTemporaryRamPpiGuid,\r
+    NULL // To be patched later.\r
+  },\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
+    &gFspInitDonePpiGuid,\r
+    &gFspInitDonePpi\r
+  },\r
+};\r
+\r
+//\r
+// These are IDT entries pointing to 10:FFFFFFE4h.\r
+//\r
+UINT64  mIdtEntryTemplate = 0xffff8e000010ffe4ULL;\r
+\r
+/**\r
+  Caller provided function to be invoked at the end of InitializeDebugAgent().\r
+\r
+  Entry point to the C language phase of SEC. After the SEC assembly\r
+  code has initialized some temporary memory and set up the stack,\r
+  the control is transferred to this function.\r
+\r
+  @param[in] Context    The first input parameter of InitializeDebugAgent().\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SecStartupPhase2(\r
+  IN VOID                     *Context\r
+  );\r
+\r
+\r
+/**\r
+\r
+  Entry point to the C language phase of SEC. After the SEC assembly\r
+  code has initialized some temporary memory and set up the stack,\r
+  the control is transferred to this function.\r
+\r
+  @param[in] SizeOfRam           Size of the temporary memory available for use.\r
+  @param[in] TempRamBase         Base address of tempory ram\r
+  @param[in] BootFirmwareVolume  Base address of the Boot Firmware Volume.\r
+**/\r
+VOID\r
+EFIAPI\r
+SecStartup (\r
+  IN UINT32                   SizeOfRam,\r
+  IN UINT32                   TempRamBase,\r
+  IN VOID                     *BootFirmwareVolume\r
+  )\r
+{\r
+  EFI_SEC_PEI_HAND_OFF        SecCoreData;\r
+  IA32_DESCRIPTOR             IdtDescriptor;\r
+  SEC_IDT_TABLE               IdtTableInStack;\r
+  UINT32                      Index;\r
+  UINT32                      PeiStackSize;\r
+\r
+  PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);\r
+  if (PeiStackSize == 0) {\r
+    PeiStackSize = (SizeOfRam >> 1);\r
+  }\r
+\r
+  ASSERT (PeiStackSize < SizeOfRam);\r
+\r
+  //\r
+  // Process all libraries constructor function linked to SecCore.\r
+  //\r
+  ProcessLibraryConstructorList ();\r
+\r
+  DEBUG ((DEBUG_INFO, "FspPei - SecStartup\n"));\r
+\r
+  //\r
+  // Initialize floating point operating environment\r
+  // to be compliant with UEFI spec.\r
+  //\r
+  InitializeFloatingPointUnits ();\r
+\r
+\r
+  // |-------------------|---->\r
+  // |Idt Table          |\r
+  // |-------------------|\r
+  // |PeiService Pointer |    PeiStackSize\r
+  // |-------------------|\r
+  // |                   |\r
+  // |      Stack        |\r
+  // |-------------------|---->\r
+  // |                   |\r
+  // |                   |\r
+  // |      Heap         |    PeiTemporayRamSize\r
+  // |                   |\r
+  // |                   |\r
+  // |-------------------|---->  TempRamBase\r
+\r
+  IdtTableInStack.PeiService = 0;\r
+  for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
+    CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));\r
+  }\r
+\r
+  IdtDescriptor.Base  = (UINTN) &IdtTableInStack.IdtTable;\r
+  IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
+\r
+  AsmWriteIdtr (&IdtDescriptor);\r
+\r
+  //\r
+  // Update the base address and length of Pei temporary memory\r
+  //\r
+  SecCoreData.DataSize               = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);\r
+  SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;\r
+  SecCoreData.BootFirmwareVolumeSize = (UINTN)(SIZE_4GB - (UINTN) BootFirmwareVolume);\r
+  SecCoreData.TemporaryRamBase       = (VOID*)(UINTN) TempRamBase;\r
+  SecCoreData.TemporaryRamSize       = SizeOfRam;\r
+  SecCoreData.PeiTemporaryRamBase    = SecCoreData.TemporaryRamBase;\r
+  SecCoreData.PeiTemporaryRamSize    = SizeOfRam - PeiStackSize;\r
+  SecCoreData.StackBase              = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);\r
+  SecCoreData.StackSize              = PeiStackSize;\r
+\r
+  DEBUG ((DEBUG_INFO, "BootFirmwareVolumeBase - 0x%x\n", SecCoreData.BootFirmwareVolumeBase));\r
+  DEBUG ((DEBUG_INFO, "BootFirmwareVolumeSize - 0x%x\n", SecCoreData.BootFirmwareVolumeSize));\r
+  DEBUG ((DEBUG_INFO, "TemporaryRamBase       - 0x%x\n", SecCoreData.TemporaryRamBase));\r
+  DEBUG ((DEBUG_INFO, "TemporaryRamSize       - 0x%x\n", SecCoreData.TemporaryRamSize));\r
+  DEBUG ((DEBUG_INFO, "PeiTemporaryRamBase    - 0x%x\n", SecCoreData.PeiTemporaryRamBase));\r
+  DEBUG ((DEBUG_INFO, "PeiTemporaryRamSize    - 0x%x\n", SecCoreData.PeiTemporaryRamSize));\r
+  DEBUG ((DEBUG_INFO, "StackBase              - 0x%x\n", SecCoreData.StackBase));\r
+  DEBUG ((DEBUG_INFO, "StackSize              - 0x%x\n", SecCoreData.StackSize));\r
+\r
+  //\r
+  // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.\r
+  //\r
+  InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);\r
+\r
+}\r
+\r
+/**\r
+  This API patch the TopOfTemporaryRam value in SecPpiList.\r
+\r
+  @param[in,out] SecPpiList           PPI list to be patched.\r
+  @param[in]     TopOfTemporaryRam    The top of Temporary Ram.\r
+\r
+**/\r
+VOID\r
+PatchTopOfTemporaryRamPpi (\r
+  IN OUT EFI_PEI_PPI_DESCRIPTOR *SecPpiList,\r
+  IN VOID                       *TopOfTemporaryRam\r
+  )\r
+{\r
+  SecPpiList[0].Ppi = TopOfTemporaryRam;\r
+}\r
+\r
+/**\r
+  Caller provided function to be invoked at the end of InitializeDebugAgent().\r
+\r
+  Entry point to the C language phase of SEC. After the SEC assembly\r
+  code has initialized some temporary memory and set up the stack,\r
+  the control is transferred to this function.\r
+\r
+  @param[in] Context    The first input parameter of InitializeDebugAgent().\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SecStartupPhase2(\r
+  IN VOID                     *Context\r
+  )\r
+{\r
+  EFI_SEC_PEI_HAND_OFF        *SecCoreData;\r
+  EFI_PEI_PPI_DESCRIPTOR      *PpiList;\r
+  UINT32                      Index;\r
+  EFI_PEI_PPI_DESCRIPTOR      LocalSecPpiList[sizeof(mPeiSecMainPpi)/sizeof(mPeiSecMainPpi[0])];\r
+  EFI_PEI_PPI_DESCRIPTOR      AllSecPpiList[FixedPcdGet32(PcdSecCoreMaxPpiSupported)];\r
+  EFI_PEI_CORE_ENTRY_POINT    PeiCoreEntryPoint;\r
+\r
+  SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;\r
+  //\r
+  // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug\r
+  // is enabled.\r
+  //\r
+  FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);\r
+  if (PeiCoreEntryPoint == NULL)\r
+  {\r
+    CpuDeadLoop ();\r
+  }\r
+\r
+  CopyMem (LocalSecPpiList, mPeiSecMainPpi, sizeof(mPeiSecMainPpi));\r
+  PatchTopOfTemporaryRamPpi (LocalSecPpiList, (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecCoreData->TemporaryRamSize));\r
+\r
+  //\r
+  // Perform platform specific initialization before entering PeiCore.\r
+  //\r
+  PpiList = SecPlatformMain (SecCoreData);\r
+  if (PpiList != NULL) {\r
+    //\r
+    // Remove the terminal flag from the terminal Ppi\r
+    //\r
+    CopyMem (AllSecPpiList, LocalSecPpiList, sizeof (LocalSecPpiList));\r
+    for (Index = 0; Index < PcdGet32 (PcdSecCoreMaxPpiSupported); Index ++) {\r
+      if ((AllSecPpiList[Index].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) == EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {\r
+        break;\r
+      }\r
+    }\r
+    AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
+\r
+    //\r
+    // Append the platform additional Ppi list\r
+    //\r
+    Index += 1;\r
+    while (Index < PcdGet32 (PcdSecCoreMaxPpiSupported) &&\r
+           ((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {\r
+      CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
+      Index++;\r
+      PpiList++;\r
+    }\r
+\r
+    //\r
+    // Check whether the total Ppis exceeds the max supported Ppi.\r
+    //\r
+    if (Index >= PcdGet32 (PcdSecCoreMaxPpiSupported)) {\r
+      //\r
+      // the total Ppi is larger than the supported Max\r
+      // PcdSecCoreMaxPpiSupported can be enlarged to solve it.\r
+      //\r
+      CpuDeadLoop ();\r
+    } else {\r
+      //\r
+      // Add the terminal Ppi\r
+      //\r
+      CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
+    }\r
+\r
+    //\r
+    // Set PpiList to the total Ppi\r
+    //\r
+    PpiList = &AllSecPpiList[0];\r
+  } else {\r
+    //\r
+    // No addition Ppi, PpiList directly point to the common Ppi list.\r
+    //\r
+    PpiList = &LocalSecPpiList[0];\r
+  }\r
+\r
+  //\r
+  // Transfer the control to the PEI core\r
+  //\r
+  ASSERT (PeiCoreEntryPoint != NULL);\r
+  (*PeiCoreEntryPoint) (SecCoreData, PpiList);\r
+\r
+  //\r
+  // Should not come here.\r
+  //\r
+  return ;\r
+}\r