IntelFspWrapperPkg: Fix typos in comments
[mirror_edk2.git] / IntelFspWrapperPkg / FspWrapperSecCore / SecMain.c
CommitLineData
a33a2f62
JY
1/** @file\r
2 C functions in SEC\r
3\r
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
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 | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
21 &gTopOfTemporaryRamPpiGuid,\r
22 NULL // To be patched later.\r
23 },\r
24};\r
25\r
26//\r
27// These are IDT entries pointing to 10:FFFFFFE4h.\r
28//\r
29UINT64 mIdtEntryTemplate = 0xffff8e000010ffe4ULL;\r
30\r
31/**\r
32 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
33\r
34 Entry point to the C language phase of SEC. After the SEC assembly\r
35 code has initialized some temporary memory and set up the stack,\r
36 the control is transferred to this function.\r
37\r
38 @param[in] Context The first input parameter of InitializeDebugAgent().\r
39\r
40**/\r
41VOID\r
42EFIAPI\r
43SecStartupPhase2(\r
44 IN VOID *Context\r
45 );\r
46\r
47\r
48/**\r
49\r
50 Entry point to the C language phase of SEC. After the SEC assembly\r
51 code has initialized some temporary memory and set up the stack,\r
52 the control is transferred to this function.\r
53\r
54 @param[in] SizeOfRam Size of the temporary memory available for use.\r
b9ca25cb 55 @param[in] TempRamBase Base address of temporary ram\r
a33a2f62
JY
56 @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.\r
57**/\r
58VOID\r
59EFIAPI\r
60SecStartup (\r
61 IN UINT32 SizeOfRam,\r
62 IN UINT32 TempRamBase,\r
63 IN VOID *BootFirmwareVolume\r
64 )\r
65{\r
66 EFI_SEC_PEI_HAND_OFF SecCoreData;\r
67 IA32_DESCRIPTOR IdtDescriptor;\r
68 SEC_IDT_TABLE IdtTableInStack;\r
69 UINT32 Index;\r
70 UINT32 PeiStackSize;\r
71\r
72 PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);\r
73 if (PeiStackSize == 0) {\r
74 PeiStackSize = (SizeOfRam >> 1);\r
75 }\r
76\r
77 ASSERT (PeiStackSize < SizeOfRam);\r
78\r
79 //\r
80 // Process all libraries constructor function linked to SecCore.\r
81 //\r
82 ProcessLibraryConstructorList ();\r
83\r
84 DEBUG ((DEBUG_INFO, "SecCore - SecStartup\n"));\r
85\r
86 //\r
87 // Initialize floating point operating environment\r
88 // to be compliant with UEFI spec.\r
89 //\r
90 InitializeFloatingPointUnits ();\r
91\r
92\r
93 // |-------------------|---->\r
94 // |Idt Table |\r
95 // |-------------------|\r
96 // |PeiService Pointer | PeiStackSize\r
97 // |-------------------|\r
98 // | |\r
99 // | Stack |\r
100 // |-------------------|---->\r
101 // | |\r
102 // | |\r
103 // | Heap | PeiTemporayRamSize\r
104 // | |\r
105 // | |\r
106 // |-------------------|----> TempRamBase\r
107\r
108 IdtTableInStack.PeiService = 0;\r
109 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
110 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));\r
111 }\r
112\r
113 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;\r
114 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
115\r
116 AsmWriteIdtr (&IdtDescriptor);\r
117\r
118 //\r
119 // Update the base address and length of Pei temporary memory\r
120 //\r
121 SecCoreData.DataSize = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);\r
122 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;\r
123 SecCoreData.BootFirmwareVolumeSize = (UINTN)(SIZE_4GB - (UINTN) BootFirmwareVolume);\r
124 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;\r
125 SecCoreData.TemporaryRamSize = SizeOfRam;\r
126 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;\r
127 SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;\r
128 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);\r
129 SecCoreData.StackSize = PeiStackSize;\r
130\r
131 DEBUG ((DEBUG_INFO, "BootFirmwareVolumeBase - 0x%x\n", SecCoreData.BootFirmwareVolumeBase));\r
132 DEBUG ((DEBUG_INFO, "BootFirmwareVolumeSize - 0x%x\n", SecCoreData.BootFirmwareVolumeSize));\r
133 DEBUG ((DEBUG_INFO, "TemporaryRamBase - 0x%x\n", SecCoreData.TemporaryRamBase));\r
134 DEBUG ((DEBUG_INFO, "TemporaryRamSize - 0x%x\n", SecCoreData.TemporaryRamSize));\r
135 DEBUG ((DEBUG_INFO, "PeiTemporaryRamBase - 0x%x\n", SecCoreData.PeiTemporaryRamBase));\r
136 DEBUG ((DEBUG_INFO, "PeiTemporaryRamSize - 0x%x\n", SecCoreData.PeiTemporaryRamSize));\r
137 DEBUG ((DEBUG_INFO, "StackBase - 0x%x\n", SecCoreData.StackBase));\r
138 DEBUG ((DEBUG_INFO, "StackSize - 0x%x\n", SecCoreData.StackSize));\r
139\r
140 //\r
141 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.\r
142 //\r
143 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);\r
144\r
145}\r
146\r
147/**\r
148 This API patch the TopOfTemporaryRam value in SecPpiList.\r
149\r
150 @param[in,out] SecPpiList PPI list to be patched.\r
151 @param[in] TopOfTemporaryRam The top of CAR.\r
152\r
153**/\r
154VOID\r
155PatchTopOfTemporaryRamPpi (\r
156 IN OUT EFI_PEI_PPI_DESCRIPTOR *SecPpiList,\r
157 IN VOID *TopOfTemporaryRam\r
158 )\r
159{\r
160 SecPpiList[0].Ppi = TopOfTemporaryRam;\r
161}\r
162\r
163/**\r
164 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
165\r
166 Entry point to the C language phase of SEC. After the SEC assembly\r
167 code has initialized some temporary memory and set up the stack,\r
168 the control is transferred to this function.\r
169\r
170 @param[in] Context The first input parameter of InitializeDebugAgent().\r
171\r
172**/\r
173VOID\r
174EFIAPI\r
175SecStartupPhase2(\r
176 IN VOID *Context\r
177 )\r
178{\r
179 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
180 EFI_PEI_PPI_DESCRIPTOR *PpiList;\r
181 UINT32 Index;\r
182 EFI_PEI_PPI_DESCRIPTOR LocalSecPpiList[sizeof(mPeiSecMainPpi)/sizeof(mPeiSecMainPpi[0])];\r
183 EFI_PEI_PPI_DESCRIPTOR AllSecPpiList[FixedPcdGet32(PcdSecCoreMaxPpiSupported)];\r
184 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;\r
185\r
186 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;\r
187 //\r
188 // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug\r
189 // is enabled.\r
190 //\r
191 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);\r
192 if (PeiCoreEntryPoint == NULL)\r
193 {\r
194 CpuDeadLoop ();\r
195 }\r
196\r
197 CopyMem (LocalSecPpiList, mPeiSecMainPpi, sizeof(mPeiSecMainPpi));\r
198 PatchTopOfTemporaryRamPpi (LocalSecPpiList, (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecCoreData->TemporaryRamSize));\r
199\r
200 //\r
201 // Perform platform specific initialization before entering PeiCore.\r
202 //\r
203 PpiList = SecPlatformMain (SecCoreData);\r
204 if (PpiList != NULL) {\r
205 //\r
206 // Remove the terminal flag from the terminal Ppi\r
207 //\r
208 CopyMem (AllSecPpiList, LocalSecPpiList, sizeof (LocalSecPpiList));\r
209 for (Index = 0; Index < PcdGet32 (PcdSecCoreMaxPpiSupported); Index ++) {\r
210 if ((AllSecPpiList[Index].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) == EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {\r
211 break;\r
212 }\r
213 }\r
214 AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
215\r
216 //\r
217 // Append the platform additional Ppi list\r
218 //\r
219 Index += 1;\r
220 while (Index < PcdGet32 (PcdSecCoreMaxPpiSupported) &&\r
221 ((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {\r
222 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
223 Index++;\r
224 PpiList++;\r
225 }\r
226\r
227 //\r
228 // Check whether the total Ppis exceeds the max supported Ppi.\r
229 //\r
230 if (Index >= PcdGet32 (PcdSecCoreMaxPpiSupported)) {\r
231 //\r
232 // the total Ppi is larger than the supported Max\r
233 // PcdSecCoreMaxPpiSupported can be enlarged to solve it.\r
234 //\r
235 CpuDeadLoop ();\r
236 } else {\r
237 //\r
238 // Add the terminal Ppi\r
239 //\r
240 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
241 }\r
242\r
243 //\r
244 // Set PpiList to the total Ppi\r
245 //\r
246 PpiList = &AllSecPpiList[0];\r
247 } else {\r
248 //\r
249 // No addition Ppi, PpiList directly point to the common Ppi list.\r
250 //\r
251 PpiList = &LocalSecPpiList[0];\r
252 }\r
253\r
254 //\r
255 // Transfer the control to the PEI core\r
256 //\r
257 ASSERT (PeiCoreEntryPoint != NULL);\r
258 (*PeiCoreEntryPoint) (SecCoreData, PpiList);\r
259\r
260 //\r
261 // Should not come here.\r
262 //\r
263 return ;\r
264}\r