SecMain.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /** @file
  2. Copyright (c) 2014 - 2022, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "SecMain.h"
  6. #include "SecFsp.h"
  7. EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI gSecTemporaryRamSupportPpi = {
  8. SecTemporaryRamSupport
  9. };
  10. EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {
  11. {
  12. EFI_PEI_PPI_DESCRIPTOR_PPI,
  13. &gFspInApiModePpiGuid,
  14. NULL
  15. },
  16. {
  17. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  18. &gEfiTemporaryRamSupportPpiGuid,
  19. &gSecTemporaryRamSupportPpi
  20. }
  21. };
  22. //
  23. // These are IDT entries pointing to 08:FFFFFFE4h.
  24. //
  25. UINT64 mIdtEntryTemplate = 0xffff8e000008ffe4ULL;
  26. /**
  27. Entry point to the C language phase of SEC. After the SEC assembly
  28. code has initialized some temporary memory and set up the stack,
  29. the control is transferred to this function.
  30. @param[in] SizeOfRam Size of the temporary memory available for use.
  31. @param[in] TempRamBase Base address of temporary ram
  32. @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.
  33. @param[in] PeiCore PeiCore entry point.
  34. @param[in] BootLoaderStack BootLoader stack.
  35. @param[in] ApiIdx the index of API.
  36. @return This function never returns.
  37. **/
  38. VOID
  39. EFIAPI
  40. SecStartup (
  41. IN UINT32 SizeOfRam,
  42. IN UINT32 TempRamBase,
  43. IN VOID *BootFirmwareVolume,
  44. IN PEI_CORE_ENTRY PeiCore,
  45. IN UINTN BootLoaderStack,
  46. IN UINT32 ApiIdx
  47. )
  48. {
  49. EFI_SEC_PEI_HAND_OFF SecCoreData;
  50. IA32_DESCRIPTOR IdtDescriptor;
  51. SEC_IDT_TABLE IdtTableInStack;
  52. UINT32 Index;
  53. FSP_GLOBAL_DATA PeiFspData;
  54. IA32_IDT_GATE_DESCRIPTOR ExceptionHandler;
  55. UINTN IdtSize;
  56. //
  57. // Process all libraries constructor function linked to SecCore.
  58. //
  59. ProcessLibraryConstructorList ();
  60. //
  61. // Initialize floating point operating environment
  62. // to be compliant with UEFI spec.
  63. //
  64. InitializeFloatingPointUnits ();
  65. //
  66. // Scenario 1 memory map when running on bootloader stack
  67. //
  68. // |-------------------|---->
  69. // |Idt Table |
  70. // |-------------------|
  71. // |PeiService Pointer |
  72. // |-------------------|
  73. // | |
  74. // | |
  75. // | Heap |
  76. // | |
  77. // | |
  78. // |-------------------|----> TempRamBase
  79. //
  80. //
  81. // |-------------------|
  82. // |Bootloader stack |----> somewhere in memory, FSP will share this stack.
  83. // |-------------------|
  84. //
  85. // Scenario 2 memory map when running FSP on a separate stack
  86. //
  87. // |-------------------|---->
  88. // |Idt Table |
  89. // |-------------------|
  90. // |PeiService Pointer | PeiStackSize
  91. // |-------------------|
  92. // | |
  93. // | Stack |
  94. // |-------------------|---->
  95. // | |
  96. // | |
  97. // | Heap | PeiTemporaryRamSize
  98. // | |
  99. // | |
  100. // |-------------------|----> TempRamBase
  101. IdtTableInStack.PeiService = 0;
  102. AsmReadIdtr (&IdtDescriptor);
  103. if (IdtDescriptor.Base == 0) {
  104. ExceptionHandler = FspGetExceptionHandler (mIdtEntryTemplate);
  105. for (Index = 0; Index < FixedPcdGet8 (PcdFspMaxInterruptSupported); Index++) {
  106. CopyMem ((VOID *)&IdtTableInStack.IdtTable[Index], (VOID *)&ExceptionHandler, sizeof (IA32_IDT_GATE_DESCRIPTOR));
  107. }
  108. IdtSize = sizeof (IdtTableInStack.IdtTable);
  109. } else {
  110. IdtSize = IdtDescriptor.Limit + 1;
  111. if (IdtSize > sizeof (IdtTableInStack.IdtTable)) {
  112. //
  113. // ERROR: IDT table size from boot loader is larger than FSP can support, DeadLoop here!
  114. //
  115. CpuDeadLoop ();
  116. } else {
  117. CopyMem ((VOID *)(UINTN)&IdtTableInStack.IdtTable, (VOID *)IdtDescriptor.Base, IdtSize);
  118. }
  119. }
  120. IdtDescriptor.Base = (UINTN)&IdtTableInStack.IdtTable;
  121. IdtDescriptor.Limit = (UINT16)(IdtSize - 1);
  122. AsmWriteIdtr (&IdtDescriptor);
  123. //
  124. // Initialize the global FSP data region
  125. //
  126. FspGlobalDataInit (&PeiFspData, BootLoaderStack, (UINT8)ApiIdx);
  127. //
  128. // Update the base address and length of Pei temporary memory
  129. //
  130. SecCoreData.DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);
  131. SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;
  132. SecCoreData.BootFirmwareVolumeSize = (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)BootFirmwareVolume)->FvLength;
  133. //
  134. // Support FSP reserved temporary memory from the whole temporary memory provided by bootloader.
  135. // FSP reserved temporary memory will not be given to PeiCore.
  136. //
  137. SecCoreData.TemporaryRamBase = (UINT8 *)(UINTN)TempRamBase + PcdGet32 (PcdFspPrivateTemporaryRamSize);
  138. SecCoreData.TemporaryRamSize = SizeOfRam - PcdGet32 (PcdFspPrivateTemporaryRamSize);
  139. if (PcdGet8 (PcdFspHeapSizePercentage) == 0) {
  140. SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
  141. SecCoreData.PeiTemporaryRamSize = SecCoreData.TemporaryRamSize;
  142. SecCoreData.StackBase = (VOID *)GetFspEntryStack (); // Share the same boot loader stack
  143. SecCoreData.StackSize = 0;
  144. } else {
  145. SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
  146. SecCoreData.PeiTemporaryRamSize = SecCoreData.TemporaryRamSize * PcdGet8 (PcdFspHeapSizePercentage) / 100;
  147. SecCoreData.StackBase = (VOID *)(UINTN)((UINTN)SecCoreData.TemporaryRamBase + SecCoreData.PeiTemporaryRamSize);
  148. SecCoreData.StackSize = SecCoreData.TemporaryRamSize - SecCoreData.PeiTemporaryRamSize;
  149. }
  150. DEBUG ((DEBUG_INFO, "Fsp BootFirmwareVolumeBase - 0x%x\n", SecCoreData.BootFirmwareVolumeBase));
  151. DEBUG ((DEBUG_INFO, "Fsp BootFirmwareVolumeSize - 0x%x\n", SecCoreData.BootFirmwareVolumeSize));
  152. DEBUG ((DEBUG_INFO, "Fsp TemporaryRamBase - 0x%x\n", SecCoreData.TemporaryRamBase));
  153. DEBUG ((DEBUG_INFO, "Fsp TemporaryRamSize - 0x%x\n", SecCoreData.TemporaryRamSize));
  154. DEBUG ((DEBUG_INFO, "Fsp PeiTemporaryRamBase - 0x%x\n", SecCoreData.PeiTemporaryRamBase));
  155. DEBUG ((DEBUG_INFO, "Fsp PeiTemporaryRamSize - 0x%x\n", SecCoreData.PeiTemporaryRamSize));
  156. DEBUG ((DEBUG_INFO, "Fsp StackBase - 0x%x\n", SecCoreData.StackBase));
  157. DEBUG ((DEBUG_INFO, "Fsp StackSize - 0x%x\n", SecCoreData.StackSize));
  158. //
  159. // Call PeiCore Entry
  160. //
  161. PeiCore (&SecCoreData, mPeiSecPlatformInformationPpi);
  162. //
  163. // Should never be here
  164. //
  165. CpuDeadLoop ();
  166. }
  167. /**
  168. This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
  169. permanent memory.
  170. @param[in] PeiServices Pointer to the PEI Services Table.
  171. @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
  172. Temporary RAM contents.
  173. @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
  174. Temporary RAM contents.
  175. @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.
  176. @retval EFI_SUCCESS The data was successfully returned.
  177. @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
  178. TemporaryMemoryBase > PermanentMemoryBase.
  179. **/
  180. EFI_STATUS
  181. EFIAPI
  182. SecTemporaryRamSupport (
  183. IN CONST EFI_PEI_SERVICES **PeiServices,
  184. IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
  185. IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
  186. IN UINTN CopySize
  187. )
  188. {
  189. IA32_DESCRIPTOR IdtDescriptor;
  190. VOID *OldHeap;
  191. VOID *NewHeap;
  192. VOID *OldStack;
  193. VOID *NewStack;
  194. UINTN HeapSize;
  195. UINTN StackSize;
  196. UINTN CurrentStack;
  197. UINTN FspStackBase;
  198. //
  199. // Override OnSeparateStack to 1 because this function will switch stack to permanent memory
  200. // which makes FSP running on different stack from bootloader temporary ram stack.
  201. //
  202. GetFspGlobalDataPointer ()->OnSeparateStack = 1;
  203. if (PcdGet8 (PcdFspHeapSizePercentage) == 0) {
  204. CurrentStack = AsmReadStackPointer ();
  205. FspStackBase = (UINTN)GetFspEntryStack ();
  206. StackSize = FspStackBase - CurrentStack;
  207. HeapSize = CopySize;
  208. OldHeap = (VOID *)(UINTN)TemporaryMemoryBase;
  209. NewHeap = (VOID *)((UINTN)PermanentMemoryBase);
  210. OldStack = (VOID *)CurrentStack;
  211. //
  212. // The old stack is copied at the end of the stack region because stack grows down.
  213. //
  214. NewStack = (VOID *)((UINTN)PermanentMemoryBase - StackSize);
  215. } else {
  216. HeapSize = CopySize * PcdGet8 (PcdFspHeapSizePercentage) / 100;
  217. StackSize = CopySize - HeapSize;
  218. OldHeap = (VOID *)(UINTN)TemporaryMemoryBase;
  219. NewHeap = (VOID *)((UINTN)PermanentMemoryBase + StackSize);
  220. OldStack = (VOID *)((UINTN)TemporaryMemoryBase + HeapSize);
  221. NewStack = (VOID *)(UINTN)PermanentMemoryBase;
  222. }
  223. //
  224. // Migrate Heap
  225. //
  226. CopyMem (NewHeap, OldHeap, HeapSize);
  227. //
  228. // Migrate Stack
  229. //
  230. CopyMem (NewStack, OldStack, StackSize);
  231. //
  232. // We need *not* fix the return address because currently,
  233. // The PeiCore is executed in flash.
  234. //
  235. //
  236. // Rebase IDT table in permanent memory
  237. //
  238. AsmReadIdtr (&IdtDescriptor);
  239. IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
  240. AsmWriteIdtr (&IdtDescriptor);
  241. //
  242. // Fixed the FSP data pointer
  243. //
  244. FspDataPointerFixUp ((UINTN)NewStack - (UINTN)OldStack);
  245. //
  246. // SecSwitchStack function must be invoked after the memory migration
  247. // immediately, also we need fixup the stack change caused by new call into
  248. // permanent memory.
  249. //
  250. SecSwitchStack (
  251. (UINTN)OldStack,
  252. (UINTN)NewStack
  253. );
  254. return EFI_SUCCESS;
  255. }