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