]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/SecCore/SecMain.c
UefiCpuPkg/SecCore: Support EFI_PEI_CORE_FV_LOCATION_PPI
[mirror_edk2.git] / UefiCpuPkg / SecCore / SecMain.c
... / ...
CommitLineData
1/** @file\r
2 C functions in SEC\r
3\r
4 Copyright (c) 2008 - 2019, 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#include "SecMain.h"\r
16\r
17EFI_PEI_TEMPORARY_RAM_DONE_PPI gSecTemporaryRamDonePpi = {\r
18 SecTemporaryRamDone\r
19};\r
20\r
21EFI_SEC_PLATFORM_INFORMATION_PPI mSecPlatformInformationPpi = { SecPlatformInformation };\r
22\r
23EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {\r
24 {\r
25 //\r
26 // SecPerformance PPI notify descriptor.\r
27 //\r
28 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,\r
29 &gPeiSecPerformancePpiGuid,\r
30 (VOID *) (UINTN) SecPerformancePpiCallBack\r
31 },\r
32 {\r
33 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
34 &gEfiTemporaryRamDonePpiGuid,\r
35 &gSecTemporaryRamDonePpi\r
36 },\r
37 {\r
38 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
39 &gEfiSecPlatformInformationPpiGuid,\r
40 &mSecPlatformInformationPpi\r
41 }\r
42};\r
43\r
44//\r
45// These are IDT entries pointing to 10:FFFFFFE4h.\r
46//\r
47UINT64 mIdtEntryTemplate = 0xffff8e000010ffe4ULL;\r
48\r
49/**\r
50 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
51\r
52 Entry point to the C language phase of SEC. After the SEC assembly\r
53 code has initialized some temporary memory and set up the stack,\r
54 the control is transferred to this function.\r
55\r
56 @param[in] Context The first input parameter of InitializeDebugAgent().\r
57\r
58**/\r
59VOID\r
60NORETURN\r
61EFIAPI\r
62SecStartupPhase2(\r
63 IN VOID *Context\r
64 );\r
65\r
66/**\r
67 Entry point of the notification callback function itself within the PEIM.\r
68 It is to get SEC performance data and build HOB to convey the SEC performance\r
69 data to DXE phase.\r
70\r
71 @param PeiServices Indirect reference to the PEI Services Table.\r
72 @param NotifyDescriptor Address of the notification descriptor data structure.\r
73 @param Ppi Address of the PPI that was installed.\r
74\r
75 @return Status of the notification.\r
76 The status code returned from this function is ignored.\r
77**/\r
78EFI_STATUS\r
79EFIAPI\r
80SecPerformancePpiCallBack (\r
81 IN EFI_PEI_SERVICES **PeiServices,\r
82 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
83 IN VOID *Ppi\r
84 )\r
85{\r
86 EFI_STATUS Status;\r
87 PEI_SEC_PERFORMANCE_PPI *SecPerf;\r
88 FIRMWARE_SEC_PERFORMANCE Performance;\r
89\r
90 SecPerf = (PEI_SEC_PERFORMANCE_PPI *) Ppi;\r
91 Status = SecPerf->GetPerformance ((CONST EFI_PEI_SERVICES **) PeiServices, SecPerf, &Performance);\r
92 if (!EFI_ERROR (Status)) {\r
93 BuildGuidDataHob (\r
94 &gEfiFirmwarePerformanceGuid,\r
95 &Performance,\r
96 sizeof (FIRMWARE_SEC_PERFORMANCE)\r
97 );\r
98 DEBUG ((DEBUG_INFO, "FPDT: SEC Performance Hob ResetEnd = %ld\n", Performance.ResetEnd));\r
99 }\r
100\r
101 return Status;\r
102}\r
103\r
104/**\r
105\r
106 Entry point to the C language phase of SEC. After the SEC assembly\r
107 code has initialized some temporary memory and set up the stack,\r
108 the control is transferred to this function.\r
109\r
110\r
111 @param SizeOfRam Size of the temporary memory available for use.\r
112 @param TempRamBase Base address of temporary ram\r
113 @param BootFirmwareVolume Base address of the Boot Firmware Volume.\r
114**/\r
115VOID\r
116NORETURN\r
117EFIAPI\r
118SecStartup (\r
119 IN UINT32 SizeOfRam,\r
120 IN UINT32 TempRamBase,\r
121 IN VOID *BootFirmwareVolume\r
122 )\r
123{\r
124 EFI_SEC_PEI_HAND_OFF SecCoreData;\r
125 IA32_DESCRIPTOR IdtDescriptor;\r
126 SEC_IDT_TABLE IdtTableInStack;\r
127 UINT32 Index;\r
128 UINT32 PeiStackSize;\r
129 EFI_STATUS Status;\r
130\r
131 //\r
132 // Report Status Code to indicate entering SEC core\r
133 //\r
134 REPORT_STATUS_CODE (\r
135 EFI_PROGRESS_CODE,\r
136 EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_ENTRY_POINT\r
137 );\r
138\r
139 PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);\r
140 if (PeiStackSize == 0) {\r
141 PeiStackSize = (SizeOfRam >> 1);\r
142 }\r
143\r
144 ASSERT (PeiStackSize < SizeOfRam);\r
145\r
146 //\r
147 // Process all libraries constructor function linked to SecCore.\r
148 //\r
149 ProcessLibraryConstructorList ();\r
150\r
151 //\r
152 // Initialize floating point operating environment\r
153 // to be compliant with UEFI spec.\r
154 //\r
155 InitializeFloatingPointUnits ();\r
156\r
157 // |-------------------|---->\r
158 // |IDT Table |\r
159 // |-------------------|\r
160 // |PeiService Pointer | PeiStackSize\r
161 // |-------------------|\r
162 // | |\r
163 // | Stack |\r
164 // |-------------------|---->\r
165 // | |\r
166 // | |\r
167 // | Heap | PeiTemporayRamSize\r
168 // | |\r
169 // | |\r
170 // |-------------------|----> TempRamBase\r
171\r
172 IdtTableInStack.PeiService = 0;\r
173 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
174 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));\r
175 }\r
176\r
177 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;\r
178 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
179\r
180 AsmWriteIdtr (&IdtDescriptor);\r
181\r
182 //\r
183 // Setup the default exception handlers\r
184 //\r
185 Status = InitializeCpuExceptionHandlers (NULL);\r
186 ASSERT_EFI_ERROR (Status);\r
187\r
188 //\r
189 // Update the base address and length of Pei temporary memory\r
190 //\r
191 SecCoreData.DataSize = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);\r
192 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;\r
193 SecCoreData.BootFirmwareVolumeSize = (UINTN)((EFI_FIRMWARE_VOLUME_HEADER *) BootFirmwareVolume)->FvLength;\r
194 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;\r
195 SecCoreData.TemporaryRamSize = SizeOfRam;\r
196 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;\r
197 SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;\r
198 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);\r
199 SecCoreData.StackSize = PeiStackSize;\r
200\r
201 //\r
202 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.\r
203 //\r
204 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);\r
205\r
206 //\r
207 // Should not come here.\r
208 //\r
209 UNREACHABLE ();\r
210}\r
211\r
212/**\r
213 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
214\r
215 Entry point to the C language phase of SEC. After the SEC assembly\r
216 code has initialized some temporary memory and set up the stack,\r
217 the control is transferred to this function.\r
218\r
219 @param[in] Context The first input parameter of InitializeDebugAgent().\r
220\r
221**/\r
222VOID\r
223NORETURN\r
224EFIAPI\r
225SecStartupPhase2(\r
226 IN VOID *Context\r
227 )\r
228{\r
229 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
230 EFI_PEI_PPI_DESCRIPTOR *PpiList;\r
231 UINT32 Index;\r
232 EFI_PEI_PPI_DESCRIPTOR *AllSecPpiList;\r
233 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;\r
234\r
235 PeiCoreEntryPoint = NULL;\r
236 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;\r
237 AllSecPpiList = (EFI_PEI_PPI_DESCRIPTOR *) SecCoreData->PeiTemporaryRamBase;\r
238\r
239 //\r
240 // Perform platform specific initialization before entering PeiCore.\r
241 //\r
242 PpiList = SecPlatformMain (SecCoreData);\r
243 //\r
244 // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug\r
245 // is enabled.\r
246 //\r
247 if (PpiList != NULL) {\r
248 for (Index = 0;\r
249 (PpiList[Index].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;\r
250 Index++) {\r
251 if (CompareGuid (PpiList[Index].Guid, &gEfiPeiCoreFvLocationPpiGuid) && (((EFI_PEI_CORE_FV_LOCATION_PPI *) PpiList[Index].Ppi)->PeiCoreFvLocation != 0)) {\r
252 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) ((EFI_PEI_CORE_FV_LOCATION_PPI *) PpiList[Index].Ppi)->PeiCoreFvLocation, &PeiCoreEntryPoint);\r
253 if (PeiCoreEntryPoint != NULL) {\r
254 break;\r
255 } else {\r
256 //\r
257 // PeiCore not found\r
258 //\r
259 CpuDeadLoop ();\r
260 }\r
261 }\r
262 }\r
263 }\r
264 //\r
265 // If EFI_PEI_CORE_FV_LOCATION_PPI not found, try to locate PeiCore from BFV.\r
266 //\r
267 if (PeiCoreEntryPoint == NULL) {\r
268 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);\r
269 if (PeiCoreEntryPoint == NULL) {\r
270 CpuDeadLoop ();\r
271 }\r
272 }\r
273\r
274 if (PpiList != NULL) {\r
275 //\r
276 // Remove the terminal flag from the terminal PPI\r
277 //\r
278 CopyMem (AllSecPpiList, mPeiSecPlatformInformationPpi, sizeof (mPeiSecPlatformInformationPpi));\r
279 Index = sizeof (mPeiSecPlatformInformationPpi) / sizeof (EFI_PEI_PPI_DESCRIPTOR) - 1;\r
280 AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
281\r
282 //\r
283 // Append the platform additional PPI list\r
284 //\r
285 Index += 1;\r
286 while (((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {\r
287 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
288 Index++;\r
289 PpiList++;\r
290 }\r
291\r
292 //\r
293 // Add the terminal PPI\r
294 //\r
295 CopyMem (&AllSecPpiList[Index ++], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
296\r
297 //\r
298 // Set PpiList to the total PPI\r
299 //\r
300 PpiList = AllSecPpiList;\r
301\r
302 //\r
303 // Adjust PEI TEMP RAM Range.\r
304 //\r
305 ASSERT (SecCoreData->PeiTemporaryRamSize > Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
306 SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN) SecCoreData->PeiTemporaryRamBase + Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
307 SecCoreData->PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize - Index * sizeof (EFI_PEI_PPI_DESCRIPTOR);\r
308 //\r
309 // Adjust the Base and Size to be 8-byte aligned as HOB which has 8byte aligned requirement\r
310 // will be built based on them in PEI phase.\r
311 //\r
312 SecCoreData->PeiTemporaryRamBase = (VOID *)(((UINTN)SecCoreData->PeiTemporaryRamBase + 7) & ~0x07);\r
313 SecCoreData->PeiTemporaryRamSize &= ~(UINTN)0x07;\r
314 } else {\r
315 //\r
316 // No addition PPI, PpiList directly point to the common PPI list.\r
317 //\r
318 PpiList = &mPeiSecPlatformInformationPpi[0];\r
319 }\r
320\r
321 DEBUG ((\r
322 DEBUG_INFO,\r
323 "%a() Stack Base: 0x%p, Stack Size: 0x%x\n",\r
324 __FUNCTION__,\r
325 SecCoreData->StackBase,\r
326 (UINT32) SecCoreData->StackSize\r
327 ));\r
328\r
329 //\r
330 // Report Status Code to indicate transferring to PEI core\r
331 //\r
332 REPORT_STATUS_CODE (\r
333 EFI_PROGRESS_CODE,\r
334 EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_HANDOFF_TO_NEXT\r
335 );\r
336\r
337 //\r
338 // Transfer the control to the PEI core\r
339 //\r
340 ASSERT (PeiCoreEntryPoint != NULL);\r
341 (*PeiCoreEntryPoint) (SecCoreData, PpiList);\r
342\r
343 //\r
344 // Should not come here.\r
345 //\r
346 UNREACHABLE ();\r
347}\r
348\r
349/**\r
350 TemporaryRamDone() disables the use of Temporary RAM. If present, this service is invoked\r
351 by the PEI Foundation after the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.\r
352\r
353 @retval EFI_SUCCESS Use of Temporary RAM was disabled.\r
354 @retval EFI_INVALID_PARAMETER Temporary RAM could not be disabled.\r
355\r
356**/\r
357EFI_STATUS\r
358EFIAPI\r
359SecTemporaryRamDone (\r
360 VOID\r
361 )\r
362{\r
363 BOOLEAN State;\r
364\r
365 //\r
366 // Republish Sec Platform Information(2) PPI\r
367 //\r
368 RepublishSecPlatformInformationPpi ();\r
369\r
370 //\r
371 // Migrate DebugAgentContext.\r
372 //\r
373 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);\r
374\r
375 //\r
376 // Disable interrupts and save current interrupt state\r
377 //\r
378 State = SaveAndDisableInterrupts();\r
379\r
380 //\r
381 // Disable Temporary RAM after Stack and Heap have been migrated at this point.\r
382 //\r
383 SecPlatformDisableTemporaryMemory ();\r
384\r
385 //\r
386 // Restore original interrupt state\r
387 //\r
388 SetInterruptState (State);\r
389\r
390 return EFI_SUCCESS;\r
391}\r