SecFsp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /** @file
  2. Copyright (c) 2014 - 2022, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "SecFsp.h"
  6. /**
  7. Calculate the FSP IDT gate descriptor.
  8. @param[in] IdtEntryTemplate IDT gate descriptor template.
  9. @return FSP specific IDT gate descriptor.
  10. **/
  11. IA32_IDT_GATE_DESCRIPTOR
  12. FspGetExceptionHandler (
  13. IN UINT64 IdtEntryTemplate
  14. )
  15. {
  16. UINT32 Entry;
  17. IA32_IDT_GATE_DESCRIPTOR ExceptionHandler;
  18. IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor;
  19. FSP_INFO_HEADER *FspInfoHeader;
  20. ZeroMem ((VOID *)&ExceptionHandler, sizeof (IA32_IDT_GATE_DESCRIPTOR));
  21. FspInfoHeader = (FSP_INFO_HEADER *)(UINTN)AsmGetFspInfoHeader ();
  22. *(UINT64 *) &ExceptionHandler = IdtEntryTemplate;
  23. IdtGateDescriptor = &ExceptionHandler;
  24. Entry = (IdtGateDescriptor->Bits.OffsetHigh << 16) | IdtGateDescriptor->Bits.OffsetLow;
  25. Entry = FspInfoHeader->ImageBase + FspInfoHeader->ImageSize - (~Entry + 1);
  26. IdtGateDescriptor->Bits.OffsetHigh = (UINT16)(Entry >> 16);
  27. IdtGateDescriptor->Bits.OffsetLow = (UINT16)Entry;
  28. return ExceptionHandler;
  29. }
  30. /**
  31. This interface fills platform specific data.
  32. @param[in,out] FspData Pointer to the FSP global data.
  33. **/
  34. VOID
  35. EFIAPI
  36. SecGetPlatformData (
  37. IN OUT FSP_GLOBAL_DATA *FspData
  38. )
  39. {
  40. FSP_PLAT_DATA *FspPlatformData;
  41. UINT32 TopOfCar;
  42. UINT32 *StackPtr;
  43. UINT32 DwordSize;
  44. FspPlatformData = &FspData->PlatformData;
  45. //
  46. // The entries of platform information, together with the number of them,
  47. // reside in the bottom of stack, left untouched by normal stack operation.
  48. //
  49. FspPlatformData->DataPtr = NULL;
  50. FspPlatformData->MicrocodeRegionBase = 0;
  51. FspPlatformData->MicrocodeRegionSize = 0;
  52. FspPlatformData->CodeRegionBase = 0;
  53. FspPlatformData->CodeRegionSize = 0;
  54. //
  55. // Pointer to the size field
  56. //
  57. TopOfCar = PcdGet32 (PcdTemporaryRamBase) + PcdGet32 (PcdTemporaryRamSize);
  58. StackPtr = (UINT32 *)(TopOfCar - sizeof (UINT32));
  59. if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {
  60. while (*StackPtr != 0) {
  61. if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {
  62. //
  63. // This following data was pushed onto stack after TempRamInit API
  64. //
  65. DwordSize = 4;
  66. StackPtr = StackPtr - 1 - DwordSize;
  67. CopyMem (&(FspPlatformData->MicrocodeRegionBase), StackPtr, (DwordSize << 2));
  68. StackPtr--;
  69. } else if (*(StackPtr - 1) == FSP_PER0_SIGNATURE) {
  70. //
  71. // This is the performance data for InitTempMemory API entry/exit
  72. //
  73. DwordSize = 4;
  74. StackPtr = StackPtr - 1 - DwordSize;
  75. CopyMem (FspData->PerfData, StackPtr, (DwordSize << 2));
  76. ((UINT8 *)(&FspData->PerfData[0]))[7] = FSP_PERF_ID_API_TEMP_RAM_INIT_ENTRY;
  77. ((UINT8 *)(&FspData->PerfData[1]))[7] = FSP_PERF_ID_API_TEMP_RAM_INIT_EXIT;
  78. StackPtr--;
  79. } else {
  80. StackPtr -= (*StackPtr);
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. Initialize the FSP global data region.
  87. It needs to be done as soon as possible after the stack is setup.
  88. @param[in,out] PeiFspData Pointer of the FSP global data.
  89. @param[in] BootLoaderStack BootLoader stack.
  90. @param[in] ApiIdx The index of the FSP API.
  91. **/
  92. VOID
  93. FspGlobalDataInit (
  94. IN OUT FSP_GLOBAL_DATA *PeiFspData,
  95. IN UINTN BootLoaderStack,
  96. IN UINT8 ApiIdx
  97. )
  98. {
  99. VOID *FspmUpdDataPtr;
  100. CHAR8 ImageId[9];
  101. UINTN Idx;
  102. //
  103. // Set FSP Global Data pointer
  104. //
  105. SetFspGlobalDataPointer (PeiFspData);
  106. ZeroMem ((VOID *)PeiFspData, sizeof (FSP_GLOBAL_DATA));
  107. PeiFspData->Signature = FSP_GLOBAL_DATA_SIGNATURE;
  108. PeiFspData->Version = FSP_GLOBAL_DATA_VERSION;
  109. PeiFspData->CoreStack = BootLoaderStack;
  110. PeiFspData->PerfIdx = 2;
  111. PeiFspData->PerfSig = FSP_PERFORMANCE_DATA_SIGNATURE;
  112. //
  113. // Cache FspHobList pointer passed by bootloader via ApiParameter2
  114. //
  115. PeiFspData->FspHobListPtr = (VOID **)GetFspApiParameter2 ();
  116. SetFspMeasurePoint (FSP_PERF_ID_API_FSP_MEMORY_INIT_ENTRY);
  117. //
  118. // Get FSP Header offset
  119. // It may have multiple FVs, so look into the last one for FSP header
  120. //
  121. PeiFspData->FspInfoHeader = (FSP_INFO_HEADER *)(UINTN)AsmGetFspInfoHeader ();
  122. SecGetPlatformData (PeiFspData);
  123. //
  124. // Set API calling mode
  125. //
  126. SetFspApiCallingIndex (ApiIdx);
  127. //
  128. // Set UPD pointer
  129. //
  130. FspmUpdDataPtr = (VOID *)GetFspApiParameter ();
  131. if (FspmUpdDataPtr == NULL) {
  132. FspmUpdDataPtr = (VOID *)(UINTN)(PeiFspData->FspInfoHeader->ImageBase + PeiFspData->FspInfoHeader->CfgRegionOffset);
  133. }
  134. SetFspUpdDataPointer (FspmUpdDataPtr);
  135. SetFspMemoryInitUpdDataPointer (FspmUpdDataPtr);
  136. SetFspSiliconInitUpdDataPointer (NULL);
  137. //
  138. // Initialize OnSeparateStack value.
  139. //
  140. if (PcdGet8 (PcdFspHeapSizePercentage) != 0) {
  141. //
  142. // FSP is running on its own stack and may need switching stack when calling bootloader functions.
  143. //
  144. GetFspGlobalDataPointer ()->OnSeparateStack = 1;
  145. }
  146. //
  147. // Initialize serial port
  148. // It might have been done in ProcessLibraryConstructorList(), however,
  149. // the FSP global data is not initialized at that time. So do it again
  150. // for safe.
  151. //
  152. SerialPortInitialize ();
  153. //
  154. // Ensure the global data pointer is valid
  155. //
  156. ASSERT (GetFspGlobalDataPointer () == PeiFspData);
  157. for (Idx = 0; Idx < 8; Idx++) {
  158. ImageId[Idx] = PeiFspData->FspInfoHeader->ImageId[Idx];
  159. }
  160. ImageId[Idx] = 0;
  161. DEBUG ((
  162. DEBUG_INFO | DEBUG_INIT,
  163. "\n============= FSP Spec v%d.%d Header Revision v%x (%a v%x.%x.%x.%x) =============\n", \
  164. (PeiFspData->FspInfoHeader->SpecVersion >> 4) & 0xF, \
  165. PeiFspData->FspInfoHeader->SpecVersion & 0xF, \
  166. PeiFspData->FspInfoHeader->HeaderRevision, \
  167. ImageId, \
  168. (PeiFspData->FspInfoHeader->ImageRevision >> 24) & 0xFF, \
  169. (PeiFspData->FspInfoHeader->ImageRevision >> 16) & 0xFF, \
  170. (PeiFspData->FspInfoHeader->HeaderRevision >= 6) ? \
  171. (((PeiFspData->FspInfoHeader->ImageRevision >> 8) & 0xFF) | (PeiFspData->FspInfoHeader->ExtendedImageRevision & 0xFF00)) : \
  172. ((PeiFspData->FspInfoHeader->ImageRevision >> 8) & 0xFF), \
  173. (PeiFspData->FspInfoHeader->HeaderRevision >= 6) ? \
  174. ((PeiFspData->FspInfoHeader->ImageRevision & 0xFF) | ((PeiFspData->FspInfoHeader->ExtendedImageRevision & 0xFF) << 8)) : \
  175. (PeiFspData->FspInfoHeader->ImageRevision & 0xFF)
  176. ));
  177. }
  178. /**
  179. Adjust the FSP data pointers after the stack is migrated to memory.
  180. @param[in] OffsetGap The offset gap between the old stack and the new stack.
  181. **/
  182. VOID
  183. FspDataPointerFixUp (
  184. IN UINTN OffsetGap
  185. )
  186. {
  187. FSP_GLOBAL_DATA *NewFspData;
  188. NewFspData = (FSP_GLOBAL_DATA *)((UINTN)GetFspGlobalDataPointer () + (UINTN)OffsetGap);
  189. SetFspGlobalDataPointer (NewFspData);
  190. }