]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspPkg/FspSecCore/SecFsp.c
Clean up code.
[mirror_edk2.git] / IntelFspPkg / FspSecCore / SecFsp.c
CommitLineData
c8ec22a2
JY
1/** @file\r
2\r
3 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
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
16UINT32 FspImageSizeOffset = FSP_INFO_HEADER_OFF + OFFSET_IN_FSP_INFO_HEADER(ImageSize);\r
17\r
18/**\r
19\r
20 Calculate the FSP IDT gate descriptor.\r
21\r
22 @param[in] IdtEntryTemplate IDT gate descriptor template.\r
23\r
24 @return FSP specific IDT gate descriptor.\r
25\r
26**/\r
27UINT64\r
28FspGetExceptionHandler(\r
29 IN UINT64 IdtEntryTemplate\r
30 )\r
31{\r
32 UINT32 Entry;\r
33 UINT64 ExceptionHandler;\r
34 IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor;\r
35 FSP_INFO_HEADER *FspInfoHeader;\r
36\r
37 FspInfoHeader = (FSP_INFO_HEADER *)(GetFspBaseAddress() + FSP_INFO_HEADER_OFF);\r
38 ExceptionHandler = IdtEntryTemplate;\r
39 IdtGateDescriptor = (IA32_IDT_GATE_DESCRIPTOR *)&ExceptionHandler;\r
40 Entry = (IdtGateDescriptor->Bits.OffsetHigh << 16) | IdtGateDescriptor->Bits.OffsetLow;\r
41 Entry = FspInfoHeader->ImageBase + FspInfoHeader->ImageSize - (~Entry + 1);\r
42 IdtGateDescriptor->Bits.OffsetHigh = (UINT16)(Entry >> 16);\r
43 IdtGateDescriptor->Bits.OffsetLow = (UINT16)Entry;\r
44\r
45 return ExceptionHandler;\r
46}\r
47\r
48/**\r
49 This function gets the FSP UPD region offset in flash.\r
50\r
51 @return the offset of the UPD region.\r
52\r
53**/\r
54UINT32\r
55EFIAPI\r
56GetFspUpdRegionOffset (\r
57 VOID\r
58 )\r
59{\r
60 FSP_GLOBAL_DATA *FspData;\r
61 UINT32 *Offset;\r
62\r
63 FspData = GetFspGlobalDataPointer ();\r
64\r
65 //\r
66 // It is required to put PcdUpdRegionOffset at offset 0x000C\r
67 // for all FSPs.\r
68 // gPlatformFspPkgTokenSpaceGuid.PcdUpdRegionOffset | 0x000C | 0x12345678\r
69 //\r
70 Offset = (UINT32 *)(FspData->FspInfoHeader->ImageBase + \\r
71 FspData->FspInfoHeader->CfgRegionOffset + 0x0C);\r
72\r
73 return *Offset;\r
74}\r
75\r
76/**\r
77 This interface fills platform specific data.\r
78\r
79 @param[in,out] FspData Pointer to the FSP global data.\r
80\r
81**/\r
82VOID\r
83EFIAPI\r
84SecGetPlatformData (\r
85 IN OUT FSP_GLOBAL_DATA *FspData\r
86 )\r
87{\r
88 FSP_PLAT_DATA *FspPlatformData;\r
89 UINT32 TopOfCar;\r
90 UINT32 *StackPtr;\r
91 UINT32 DwordSize;\r
92\r
93 FspPlatformData = &FspData->PlatformData;\r
94\r
95 //\r
96 // The entries of platform information, together with the number of them,\r
97 // reside in the bottom of stack, left untouched by normal stack operation.\r
98 //\r
99 TopOfCar = PcdGet32 (PcdTemporaryRamBase) + PcdGet32 (PcdTemporaryRamSize);\r
100\r
101 FspPlatformData->DataPtr = NULL;\r
102 FspPlatformData->CodeRegionSize = 0;\r
103 FspPlatformData->CodeRegionBase = 0;\r
104 FspPlatformData->MicorcodeRegionBase = 0;\r
105 FspPlatformData->MicorcodeRegionSize = 0;\r
106\r
107 //\r
108 // Pointer to the size field\r
109 //\r
110 StackPtr = (UINT32 *)(TopOfCar - sizeof(UINT32));\r
111\r
112 while (*StackPtr != 0) {\r
113 if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {\r
114 //\r
115 // This following data was pushed onto stack after TempRamInit API\r
116 //\r
117 DwordSize = 4;\r
118 StackPtr = StackPtr - 1 - DwordSize;\r
119 CopyMem (&(FspPlatformData->CodeRegionBase), StackPtr, (DwordSize << 2));\r
120 StackPtr--;\r
121 } else if (*(StackPtr - 1) == FSP_PER0_SIGNATURE) {\r
122 //\r
123 // This is the performance data for InitTempMemory API entry/exit\r
124 //\r
125 DwordSize = 4;\r
126 StackPtr = StackPtr - 1 - DwordSize;\r
127 CopyMem (FspData->PerfData, StackPtr, (DwordSize << 2));\r
128 ((UINT8 *)(&FspData->PerfData[0]))[7] = FSP_PERF_ID_API_TMPRAMINIT_ENTRY;\r
129 ((UINT8 *)(&FspData->PerfData[1]))[7] = FSP_PERF_ID_API_TMPRAMINIT_EXIT;\r
130 StackPtr--;\r
131 } else {\r
132 StackPtr -= (*StackPtr);\r
133 }\r
134 }\r
135}\r
136\r
137/**\r
138\r
139 Initialize the FSP global data region.\r
140 It needs to be done as soon as possible after the stack is setup.\r
141\r
142 @param[in,out] PeiFspData Pointer of the FSP global data.\r
143 @param[in] BootFirmwareVolume Point to the address of BootFirmwareVolume in stack.\r
144\r
145**/\r
146VOID\r
147FspGlobalDataInit (\r
148 IN OUT FSP_GLOBAL_DATA *PeiFspData,\r
149 IN VOID **BootFirmwareVolume\r
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
165 PeiFspData->CoreStack = *(UINTN *)(BootFirmwareVolume + 2);\r
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
174 PeiFspData->FspInfoHeader = (FSP_INFO_HEADER *)(GetFspBaseAddress() + FSP_INFO_HEADER_OFF);\r
175 SecGetPlatformData (PeiFspData);\r
176\r
177 //\r
178 // Initialize UPD pointer.\r
179 //\r
180 FspInitParams = (FSP_INIT_PARAMS *)GetFspApiParameter ();\r
181 UpdDataRgnPtr = ((FSP_INIT_RT_COMMON_BUFFER *)FspInitParams->RtBufferPtr)->UpdDataRgnPtr;\r
182 if (UpdDataRgnPtr == NULL) {\r
183 UpdDataRgnPtr = (VOID *)(PeiFspData->FspInfoHeader->ImageBase + GetFspUpdRegionOffset());\r
184 }\r
185 SetFspUpdDataPointer (UpdDataRgnPtr);\r
186\r
187 //\r
188 // Initialize serial port\r
189 // It might have been done in ProcessLibraryConstructorList(), however,\r
190 // the FSP global data is not initialized at that time. So do it again\r
191 // for safe.\r
192 //\r
193 SerialPortInitialize ();\r
194\r
195 //\r
196 // Ensure the golbal data pointer is valid\r
197 //\r
198 ASSERT (GetFspGlobalDataPointer () == PeiFspData);\r
199\r
200 for (Idx = 0; Idx < 8; Idx++) {\r
201 ImageId[Idx] = PeiFspData->FspInfoHeader->ImageId[Idx];\r
202 }\r
203 ImageId[Idx] = 0;\r
204\r
205 DEBUG ((DEBUG_INFO | DEBUG_INIT, "\n============= PEIM FSP (%a 0x%08X) =============\n", \\r
206 ImageId, PeiFspData->FspInfoHeader->ImageRevision));\r
207\r
208}\r
209\r
210/**\r
211\r
212 Adjust the FSP data pointers after the stack is migrated to memory.\r
213\r
214 @param[in] OffsetGap The offset gap between the old stack and the new stack.\r
215\r
216**/\r
217VOID\r
218FspDataPointerFixUp (\r
219 IN UINT32 OffsetGap\r
220 )\r
221{\r
222 FSP_GLOBAL_DATA *NewFspData;\r
223\r
224 NewFspData = (FSP_GLOBAL_DATA *)((UINTN)GetFspGlobalDataPointer() + (UINTN)OffsetGap);\r
225 SetFspGlobalDataPointer (NewFspData);\r
226}\r
227\r
228/**\r
229 This function check the FSP API calling condition.\r
230\r
231 @param[in] ApiIdx Internal index of the FSP API.\r
232\r
233**/\r
234EFI_STATUS\r
235EFIAPI\r
236FspApiCallingCheck (\r
237 UINT32 ApiIdx\r
238 )\r
239{\r
240 EFI_STATUS Status;\r
241 FSP_GLOBAL_DATA *FspData;\r
242\r
243 Status = EFI_SUCCESS;\r
244 FspData = GetFspGlobalDataPointer ();\r
245 if (ApiIdx == 1) {\r
246 //\r
247 // FspInit check\r
248 //\r
249 if ((UINT32)FspData != 0xFFFFFFFF) {\r
250 Status = EFI_UNSUPPORTED;\r
251 }\r
252 } else if (ApiIdx == 2) {\r
253 //\r
254 // NotifyPhase check\r
255 //\r
256 if ((FspData == NULL) || ((UINT32)FspData == 0xFFFFFFFF)) {\r
257 Status = EFI_UNSUPPORTED;\r
258 } else {\r
259 if (FspData->Signature != FSP_GLOBAL_DATA_SIGNATURE) {\r
260 Status = EFI_UNSUPPORTED;\r
261 }\r
262 }\r
263 } else {\r
264 Status = EFI_UNSUPPORTED;\r
265 }\r
266\r
267 return Status;\r
268}\r