]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspPkg/FspSecCore/SecFsp.c
MdeModulePkg/Partition: Fix media probe
[mirror_edk2.git] / IntelFspPkg / FspSecCore / SecFsp.c
CommitLineData
c8ec22a2
JY
1/** @file\r
2\r
d5fb1edf 3 Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r
c8ec22a2
JY
4 This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php.\r
8\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12**/\r
13\r
14#include "SecFsp.h"\r
15\r
c8ec22a2
JY
16/**\r
17\r
18 Calculate the FSP IDT gate descriptor.\r
19\r
20 @param[in] IdtEntryTemplate IDT gate descriptor template.\r
21\r
22 @return FSP specific IDT gate descriptor.\r
23\r
24**/\r
25UINT64\r
26FspGetExceptionHandler(\r
27 IN UINT64 IdtEntryTemplate\r
28 )\r
29{\r
30 UINT32 Entry;\r
31 UINT64 ExceptionHandler;\r
32 IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor;\r
33 FSP_INFO_HEADER *FspInfoHeader;\r
34\r
16b7e82c 35 FspInfoHeader = (FSP_INFO_HEADER *)AsmGetFspInfoHeader();\r
c8ec22a2
JY
36 ExceptionHandler = IdtEntryTemplate;\r
37 IdtGateDescriptor = (IA32_IDT_GATE_DESCRIPTOR *)&ExceptionHandler;\r
38 Entry = (IdtGateDescriptor->Bits.OffsetHigh << 16) | IdtGateDescriptor->Bits.OffsetLow;\r
39 Entry = FspInfoHeader->ImageBase + FspInfoHeader->ImageSize - (~Entry + 1);\r
40 IdtGateDescriptor->Bits.OffsetHigh = (UINT16)(Entry >> 16);\r
41 IdtGateDescriptor->Bits.OffsetLow = (UINT16)Entry;\r
42\r
43 return ExceptionHandler;\r
44}\r
45\r
46/**\r
47 This function gets the FSP UPD region offset in flash.\r
48\r
49 @return the offset of the UPD region.\r
50\r
51**/\r
52UINT32\r
53EFIAPI\r
54GetFspUpdRegionOffset (\r
55 VOID\r
56 )\r
57{\r
58 FSP_GLOBAL_DATA *FspData;\r
59 UINT32 *Offset;\r
60\r
61 FspData = GetFspGlobalDataPointer ();\r
62\r
63 //\r
64 // It is required to put PcdUpdRegionOffset at offset 0x000C\r
65 // for all FSPs.\r
66 // gPlatformFspPkgTokenSpaceGuid.PcdUpdRegionOffset | 0x000C | 0x12345678\r
67 //\r
68 Offset = (UINT32 *)(FspData->FspInfoHeader->ImageBase + \\r
69 FspData->FspInfoHeader->CfgRegionOffset + 0x0C);\r
70\r
71 return *Offset;\r
72}\r
73\r
74/**\r
75 This interface fills platform specific data.\r
76\r
77 @param[in,out] FspData Pointer to the FSP global data.\r
78\r
79**/\r
80VOID\r
81EFIAPI\r
82SecGetPlatformData (\r
83 IN OUT FSP_GLOBAL_DATA *FspData\r
84 )\r
85{\r
86 FSP_PLAT_DATA *FspPlatformData;\r
87 UINT32 TopOfCar;\r
88 UINT32 *StackPtr;\r
89 UINT32 DwordSize;\r
90\r
91 FspPlatformData = &FspData->PlatformData;\r
92\r
93 //\r
94 // The entries of platform information, together with the number of them,\r
95 // reside in the bottom of stack, left untouched by normal stack operation.\r
96 //\r
97 TopOfCar = PcdGet32 (PcdTemporaryRamBase) + PcdGet32 (PcdTemporaryRamSize);\r
98\r
99 FspPlatformData->DataPtr = NULL;\r
9da59186
JY
100 FspPlatformData->MicrocodeRegionBase = 0;\r
101 FspPlatformData->MicrocodeRegionSize = 0;\r
c8ec22a2 102 FspPlatformData->CodeRegionBase = 0;\r
9da59186 103 FspPlatformData->CodeRegionSize = 0;\r
c8ec22a2
JY
104\r
105 //\r
106 // Pointer to the size field\r
107 //\r
108 StackPtr = (UINT32 *)(TopOfCar - sizeof(UINT32));\r
109\r
110 while (*StackPtr != 0) {\r
111 if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {\r
112 //\r
113 // This following data was pushed onto stack after TempRamInit API\r
114 //\r
115 DwordSize = 4;\r
116 StackPtr = StackPtr - 1 - DwordSize;\r
9da59186 117 CopyMem (&(FspPlatformData->MicrocodeRegionBase), StackPtr, (DwordSize << 2));\r
c8ec22a2
JY
118 StackPtr--;\r
119 } else if (*(StackPtr - 1) == FSP_PER0_SIGNATURE) {\r
120 //\r
121 // This is the performance data for InitTempMemory API entry/exit\r
122 //\r
123 DwordSize = 4;\r
124 StackPtr = StackPtr - 1 - DwordSize;\r
125 CopyMem (FspData->PerfData, StackPtr, (DwordSize << 2));\r
126 ((UINT8 *)(&FspData->PerfData[0]))[7] = FSP_PERF_ID_API_TMPRAMINIT_ENTRY;\r
127 ((UINT8 *)(&FspData->PerfData[1]))[7] = FSP_PERF_ID_API_TMPRAMINIT_EXIT;\r
128 StackPtr--;\r
129 } else {\r
130 StackPtr -= (*StackPtr);\r
131 }\r
132 }\r
133}\r
134\r
135/**\r
136\r
137 Initialize the FSP global data region.\r
138 It needs to be done as soon as possible after the stack is setup.\r
139\r
140 @param[in,out] PeiFspData Pointer of the FSP global data.\r
9da59186 141 @param[in] BootLoaderStack BootLoader stack.\r
d5fb1edf 142 @param[in] ApiIdx The index of the FSP API.\r
c8ec22a2
JY
143\r
144**/\r
145VOID\r
146FspGlobalDataInit (\r
147 IN OUT FSP_GLOBAL_DATA *PeiFspData,\r
9da59186 148 IN UINT32 BootLoaderStack,\r
d5fb1edf 149 IN UINT8 ApiIdx\r
c8ec22a2
JY
150 )\r
151{\r
152 VOID *UpdDataRgnPtr;\r
153 FSP_INIT_PARAMS *FspInitParams;\r
154 CHAR8 ImageId[9];\r
155 UINTN Idx;\r
156\r
157 //\r
158 // Init PCIE_BAR with value and set global FSP data pointer.\r
159 // PciExpress Base should have been programmed by platform already.\r
160 //\r
161 SetFspGlobalDataPointer (PeiFspData);\r
162 ZeroMem ((VOID *)PeiFspData, sizeof(FSP_GLOBAL_DATA));\r
163\r
164 PeiFspData->Signature = FSP_GLOBAL_DATA_SIGNATURE;\r
9da59186 165 PeiFspData->CoreStack = BootLoaderStack;\r
c8ec22a2
JY
166 PeiFspData->PerfIdx = 2;\r
167\r
168 SetFspMeasurePoint (FSP_PERF_ID_API_FSPINIT_ENTRY);\r
169\r
170 //\r
171 // Get FSP Header offset\r
172 // It may have multiple FVs, so look into the last one for FSP header\r
173 //\r
16b7e82c 174 PeiFspData->FspInfoHeader = (FSP_INFO_HEADER *)AsmGetFspInfoHeader();\r
c8ec22a2
JY
175 SecGetPlatformData (PeiFspData);\r
176\r
d5fb1edf
JY
177 //\r
178 // Set API calling mode\r
179 //\r
180 SetFspApiCallingMode (ApiIdx == 1 ? 0 : 1);\r
181\r
c8ec22a2
JY
182 //\r
183 // Initialize UPD pointer.\r
184 //\r
185 FspInitParams = (FSP_INIT_PARAMS *)GetFspApiParameter ();\r
186 UpdDataRgnPtr = ((FSP_INIT_RT_COMMON_BUFFER *)FspInitParams->RtBufferPtr)->UpdDataRgnPtr;\r
187 if (UpdDataRgnPtr == NULL) {\r
188 UpdDataRgnPtr = (VOID *)(PeiFspData->FspInfoHeader->ImageBase + GetFspUpdRegionOffset());\r
189 }\r
190 SetFspUpdDataPointer (UpdDataRgnPtr);\r
191\r
192 //\r
193 // Initialize serial port\r
194 // It might have been done in ProcessLibraryConstructorList(), however,\r
195 // the FSP global data is not initialized at that time. So do it again\r
196 // for safe.\r
197 //\r
198 SerialPortInitialize ();\r
199\r
200 //\r
201 // Ensure the golbal data pointer is valid\r
202 //\r
203 ASSERT (GetFspGlobalDataPointer () == PeiFspData);\r
204\r
205 for (Idx = 0; Idx < 8; Idx++) {\r
206 ImageId[Idx] = PeiFspData->FspInfoHeader->ImageId[Idx];\r
207 }\r
208 ImageId[Idx] = 0;\r
209\r
d5fb1edf
JY
210 DEBUG ((DEBUG_INFO | DEBUG_INIT, "\n============= PEIM FSP v1.%x (%a v%x.%x.%x.%x) =============\n", \\r
211 PeiFspData->FspInfoHeader->HeaderRevision - 1, \\r
212 ImageId, \\r
213 (PeiFspData->FspInfoHeader->ImageRevision >> 24) & 0xff, \\r
214 (PeiFspData->FspInfoHeader->ImageRevision >> 16) & 0xff, \\r
215 (PeiFspData->FspInfoHeader->ImageRevision >> 8) & 0xff, \\r
216 (PeiFspData->FspInfoHeader->ImageRevision >> 0) & 0xff));\r
c8ec22a2
JY
217\r
218}\r
219\r
220/**\r
221\r
222 Adjust the FSP data pointers after the stack is migrated to memory.\r
223\r
224 @param[in] OffsetGap The offset gap between the old stack and the new stack.\r
225\r
226**/\r
227VOID\r
228FspDataPointerFixUp (\r
229 IN UINT32 OffsetGap\r
230 )\r
231{\r
232 FSP_GLOBAL_DATA *NewFspData;\r
233\r
234 NewFspData = (FSP_GLOBAL_DATA *)((UINTN)GetFspGlobalDataPointer() + (UINTN)OffsetGap);\r
235 SetFspGlobalDataPointer (NewFspData);\r
236}\r
237\r
238/**\r
239 This function check the FSP API calling condition.\r
240\r
241 @param[in] ApiIdx Internal index of the FSP API.\r
9da59186 242 @param[in] ApiParam Parameter of the FSP API.\r
c8ec22a2
JY
243\r
244**/\r
245EFI_STATUS\r
246EFIAPI\r
247FspApiCallingCheck (\r
9da59186
JY
248 IN UINT32 ApiIdx,\r
249 IN VOID *ApiParam\r
c8ec22a2
JY
250 )\r
251{\r
9da59186
JY
252 EFI_STATUS Status;\r
253 FSP_GLOBAL_DATA *FspData;\r
254 FSP_INIT_PARAMS *FspInitParams;\r
255 FSP_INIT_RT_COMMON_BUFFER *FspRtBuffer;\r
256\r
257 FspInitParams = (FSP_INIT_PARAMS *) ApiParam;\r
258 FspRtBuffer = ((FSP_INIT_RT_COMMON_BUFFER *)FspInitParams->RtBufferPtr);\r
c8ec22a2
JY
259\r
260 Status = EFI_SUCCESS;\r
9da59186 261 FspData = GetFspGlobalDataPointer ();\r
c8ec22a2
JY
262 if (ApiIdx == 1) {\r
263 //\r
264 // FspInit check\r
265 //\r
266 if ((UINT32)FspData != 0xFFFFFFFF) {\r
267 Status = EFI_UNSUPPORTED;\r
b2344187 268 } else if ((FspRtBuffer == NULL) || ((FspRtBuffer->BootLoaderTolumSize % EFI_PAGE_SIZE) != 0) || (EFI_ERROR(FspUpdSignatureCheck(ApiIdx, ApiParam)))) {\r
9da59186 269 Status = EFI_INVALID_PARAMETER;\r
c8ec22a2
JY
270 }\r
271 } else if (ApiIdx == 2) {\r
272 //\r
273 // NotifyPhase check\r
274 //\r
275 if ((FspData == NULL) || ((UINT32)FspData == 0xFFFFFFFF)) {\r
276 Status = EFI_UNSUPPORTED;\r
277 } else {\r
278 if (FspData->Signature != FSP_GLOBAL_DATA_SIGNATURE) {\r
279 Status = EFI_UNSUPPORTED;\r
280 }\r
281 }\r
d5fb1edf
JY
282 } else if (ApiIdx == 3) {\r
283 //\r
284 // FspMemoryInit check\r
285 //\r
286 if ((UINT32)FspData != 0xFFFFFFFF) {\r
287 Status = EFI_UNSUPPORTED;\r
b2344187 288 } else if ((FspRtBuffer == NULL) || ((FspRtBuffer->BootLoaderTolumSize % EFI_PAGE_SIZE) != 0) || (EFI_ERROR(FspUpdSignatureCheck(ApiIdx, ApiParam)))) {\r
9da59186 289 Status = EFI_INVALID_PARAMETER;\r
d5fb1edf
JY
290 }\r
291 } else if (ApiIdx == 4) {\r
292 //\r
293 // TempRamExit check\r
294 //\r
295 if ((FspData == NULL) || ((UINT32)FspData == 0xFFFFFFFF)) {\r
296 Status = EFI_UNSUPPORTED;\r
297 } else {\r
298 if (FspData->Signature != FSP_GLOBAL_DATA_SIGNATURE) {\r
299 Status = EFI_UNSUPPORTED;\r
300 }\r
301 }\r
302 } else if (ApiIdx == 5) {\r
303 //\r
304 // FspSiliconInit check\r
305 //\r
306 if ((FspData == NULL) || ((UINT32)FspData == 0xFFFFFFFF)) {\r
307 Status = EFI_UNSUPPORTED;\r
308 } else {\r
309 if (FspData->Signature != FSP_GLOBAL_DATA_SIGNATURE) {\r
310 Status = EFI_UNSUPPORTED;\r
b2344187
JY
311 } else if (EFI_ERROR(FspUpdSignatureCheck(ApiIdx, ApiParam))) {\r
312 Status = EFI_INVALID_PARAMETER;\r
d5fb1edf
JY
313 }\r
314 }\r
c8ec22a2
JY
315 } else {\r
316 Status = EFI_UNSUPPORTED;\r
317 }\r
318\r
319 return Status;\r
320}\r
9da59186
JY
321\r
322/**\r
323 This function gets the boot FV offset in FSP.\r
324 @return the boot firmware volumen offset inside FSP binary\r
325\r
326**/\r
327UINT32\r
328EFIAPI\r
329GetBootFirmwareVolumeOffset (\r
330 VOID\r
331 )\r
332{ \r
333 return PcdGet32 (PcdFspBootFirmwareVolumeBase) - PcdGet32 (PcdFspAreaBaseAddress);\r
334}\r