Update IntelFspWrapperPkg according to FSP1.1.
[mirror_edk2.git] / IntelFspWrapperPkg / FspInitPei / SecMain.c
CommitLineData
a33a2f62
JY
1/** @file\r
2 C functions in SEC\r
3\r
d8043ce9 4 Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r
a33a2f62
JY
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15\r
16#include "SecMain.h"\r
17\r
18EFI_PEI_PPI_DESCRIPTOR mPeiSecMainPpi[] = {\r
19 {\r
20 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
21 &gTopOfTemporaryRamPpiGuid,\r
22 NULL // To be patched later.\r
23 },\r
24 {\r
25 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
26 &gFspInitDonePpiGuid,\r
27 &gFspInitDonePpi\r
28 },\r
29};\r
30\r
d8043ce9
JY
31FSP_INIT_DONE_PPI gFspInitDonePpi = {\r
32 FspInitDoneGetFspHobList\r
33};\r
34\r
a33a2f62
JY
35//\r
36// These are IDT entries pointing to 10:FFFFFFE4h.\r
37//\r
38UINT64 mIdtEntryTemplate = 0xffff8e000010ffe4ULL;\r
39\r
40/**\r
41 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
42\r
43 Entry point to the C language phase of SEC. After the SEC assembly\r
44 code has initialized some temporary memory and set up the stack,\r
45 the control is transferred to this function.\r
46\r
47 @param[in] Context The first input parameter of InitializeDebugAgent().\r
48\r
49**/\r
50VOID\r
51EFIAPI\r
52SecStartupPhase2(\r
53 IN VOID *Context\r
54 );\r
55\r
56\r
57/**\r
58\r
59 Entry point to the C language phase of SEC. After the SEC assembly\r
60 code has initialized some temporary memory and set up the stack,\r
61 the control is transferred to this function.\r
62\r
63 @param[in] SizeOfRam Size of the temporary memory available for use.\r
64 @param[in] TempRamBase Base address of tempory ram\r
65 @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.\r
66**/\r
67VOID\r
68EFIAPI\r
69SecStartup (\r
70 IN UINT32 SizeOfRam,\r
71 IN UINT32 TempRamBase,\r
72 IN VOID *BootFirmwareVolume\r
73 )\r
74{\r
75 EFI_SEC_PEI_HAND_OFF SecCoreData;\r
76 IA32_DESCRIPTOR IdtDescriptor;\r
77 SEC_IDT_TABLE IdtTableInStack;\r
78 UINT32 Index;\r
79 UINT32 PeiStackSize;\r
80\r
81 PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);\r
82 if (PeiStackSize == 0) {\r
83 PeiStackSize = (SizeOfRam >> 1);\r
84 }\r
85\r
86 ASSERT (PeiStackSize < SizeOfRam);\r
87\r
88 //\r
89 // Process all libraries constructor function linked to SecCore.\r
90 //\r
91 ProcessLibraryConstructorList ();\r
92\r
93 DEBUG ((DEBUG_INFO, "FspPei - SecStartup\n"));\r
94\r
95 //\r
96 // Initialize floating point operating environment\r
97 // to be compliant with UEFI spec.\r
98 //\r
99 InitializeFloatingPointUnits ();\r
100\r
101\r
102 // |-------------------|---->\r
103 // |Idt Table |\r
104 // |-------------------|\r
105 // |PeiService Pointer | PeiStackSize\r
106 // |-------------------|\r
107 // | |\r
108 // | Stack |\r
109 // |-------------------|---->\r
110 // | |\r
111 // | |\r
112 // | Heap | PeiTemporayRamSize\r
113 // | |\r
114 // | |\r
115 // |-------------------|----> TempRamBase\r
116\r
117 IdtTableInStack.PeiService = 0;\r
118 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
119 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));\r
120 }\r
121\r
122 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;\r
123 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
124\r
125 AsmWriteIdtr (&IdtDescriptor);\r
126\r
127 //\r
128 // Update the base address and length of Pei temporary memory\r
129 //\r
130 SecCoreData.DataSize = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);\r
131 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;\r
132 SecCoreData.BootFirmwareVolumeSize = (UINTN)(SIZE_4GB - (UINTN) BootFirmwareVolume);\r
133 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;\r
134 SecCoreData.TemporaryRamSize = SizeOfRam;\r
135 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;\r
136 SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;\r
137 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);\r
138 SecCoreData.StackSize = PeiStackSize;\r
139\r
140 DEBUG ((DEBUG_INFO, "BootFirmwareVolumeBase - 0x%x\n", SecCoreData.BootFirmwareVolumeBase));\r
141 DEBUG ((DEBUG_INFO, "BootFirmwareVolumeSize - 0x%x\n", SecCoreData.BootFirmwareVolumeSize));\r
142 DEBUG ((DEBUG_INFO, "TemporaryRamBase - 0x%x\n", SecCoreData.TemporaryRamBase));\r
143 DEBUG ((DEBUG_INFO, "TemporaryRamSize - 0x%x\n", SecCoreData.TemporaryRamSize));\r
144 DEBUG ((DEBUG_INFO, "PeiTemporaryRamBase - 0x%x\n", SecCoreData.PeiTemporaryRamBase));\r
145 DEBUG ((DEBUG_INFO, "PeiTemporaryRamSize - 0x%x\n", SecCoreData.PeiTemporaryRamSize));\r
146 DEBUG ((DEBUG_INFO, "StackBase - 0x%x\n", SecCoreData.StackBase));\r
147 DEBUG ((DEBUG_INFO, "StackSize - 0x%x\n", SecCoreData.StackSize));\r
148\r
149 //\r
150 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.\r
151 //\r
152 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);\r
153\r
154}\r
155\r
156/**\r
157 This API patch the TopOfTemporaryRam value in SecPpiList.\r
158\r
159 @param[in,out] SecPpiList PPI list to be patched.\r
160 @param[in] TopOfTemporaryRam The top of Temporary Ram.\r
161\r
162**/\r
163VOID\r
164PatchTopOfTemporaryRamPpi (\r
165 IN OUT EFI_PEI_PPI_DESCRIPTOR *SecPpiList,\r
166 IN VOID *TopOfTemporaryRam\r
167 )\r
168{\r
169 SecPpiList[0].Ppi = TopOfTemporaryRam;\r
170}\r
171\r
172/**\r
173 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
174\r
175 Entry point to the C language phase of SEC. After the SEC assembly\r
176 code has initialized some temporary memory and set up the stack,\r
177 the control is transferred to this function.\r
178\r
179 @param[in] Context The first input parameter of InitializeDebugAgent().\r
180\r
181**/\r
182VOID\r
183EFIAPI\r
184SecStartupPhase2(\r
185 IN VOID *Context\r
186 )\r
187{\r
188 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
189 EFI_PEI_PPI_DESCRIPTOR *PpiList;\r
190 UINT32 Index;\r
191 EFI_PEI_PPI_DESCRIPTOR LocalSecPpiList[sizeof(mPeiSecMainPpi)/sizeof(mPeiSecMainPpi[0])];\r
192 EFI_PEI_PPI_DESCRIPTOR AllSecPpiList[FixedPcdGet32(PcdSecCoreMaxPpiSupported)];\r
193 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;\r
194\r
195 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;\r
196 //\r
197 // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug\r
198 // is enabled.\r
199 //\r
200 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);\r
201 if (PeiCoreEntryPoint == NULL)\r
202 {\r
203 CpuDeadLoop ();\r
204 }\r
205\r
206 CopyMem (LocalSecPpiList, mPeiSecMainPpi, sizeof(mPeiSecMainPpi));\r
207 PatchTopOfTemporaryRamPpi (LocalSecPpiList, (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecCoreData->TemporaryRamSize));\r
208\r
209 //\r
210 // Perform platform specific initialization before entering PeiCore.\r
211 //\r
212 PpiList = SecPlatformMain (SecCoreData);\r
213 if (PpiList != NULL) {\r
214 //\r
215 // Remove the terminal flag from the terminal Ppi\r
216 //\r
217 CopyMem (AllSecPpiList, LocalSecPpiList, sizeof (LocalSecPpiList));\r
218 for (Index = 0; Index < PcdGet32 (PcdSecCoreMaxPpiSupported); Index ++) {\r
219 if ((AllSecPpiList[Index].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) == EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {\r
220 break;\r
221 }\r
222 }\r
223 AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
224\r
225 //\r
226 // Append the platform additional Ppi list\r
227 //\r
228 Index += 1;\r
229 while (Index < PcdGet32 (PcdSecCoreMaxPpiSupported) &&\r
230 ((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {\r
231 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
232 Index++;\r
233 PpiList++;\r
234 }\r
235\r
236 //\r
237 // Check whether the total Ppis exceeds the max supported Ppi.\r
238 //\r
239 if (Index >= PcdGet32 (PcdSecCoreMaxPpiSupported)) {\r
240 //\r
241 // the total Ppi is larger than the supported Max\r
242 // PcdSecCoreMaxPpiSupported can be enlarged to solve it.\r
243 //\r
244 CpuDeadLoop ();\r
245 } else {\r
246 //\r
247 // Add the terminal Ppi\r
248 //\r
249 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
250 }\r
251\r
252 //\r
253 // Set PpiList to the total Ppi\r
254 //\r
255 PpiList = &AllSecPpiList[0];\r
256 } else {\r
257 //\r
258 // No addition Ppi, PpiList directly point to the common Ppi list.\r
259 //\r
260 PpiList = &LocalSecPpiList[0];\r
261 }\r
262\r
263 //\r
264 // Transfer the control to the PEI core\r
265 //\r
266 ASSERT (PeiCoreEntryPoint != NULL);\r
267 (*PeiCoreEntryPoint) (SecCoreData, PpiList);\r
268\r
269 //\r
270 // Should not come here.\r
271 //\r
272 return ;\r
273}\r
d8043ce9
JY
274\r
275/**\r
276 Return Hob list produced by FSP.\r
277\r
278 @param[in] PeiServices The pointer to the PEI Services Table.\r
279 @param[in] This The pointer to this instance of this PPI.\r
280 @param[out] FspHobList The pointer to Hob list produced by FSP.\r
281\r
282 @return EFI_SUCCESS FReturn Hob list produced by FSP successfully.\r
283**/\r
284EFI_STATUS\r
285EFIAPI\r
286FspInitDoneGetFspHobList (\r
287 IN CONST EFI_PEI_SERVICES **PeiServices,\r
288 IN FSP_INIT_DONE_PPI *This,\r
289 OUT VOID **FspHobList\r
290 )\r
291{\r
292 VOID *TopOfTemporaryRamPpi;\r
293 EFI_STATUS Status;\r
294\r
295 Status = (*PeiServices)->LocatePpi (\r
296 PeiServices,\r
297 &gTopOfTemporaryRamPpiGuid,\r
298 0,\r
299 NULL,\r
300 (VOID **) &TopOfTemporaryRamPpi\r
301 );\r
302 if (EFI_ERROR (Status)) {\r
303 return EFI_NOT_FOUND;\r
304 }\r
305\r
306 *FspHobList = (VOID *)(UINTN)(*(UINT32 *)((UINTN)TopOfTemporaryRamPpi - sizeof(UINT32)));\r
307\r
308 return EFI_SUCCESS;\r
309}\r
310\r