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