]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
1. Enable use-cases in PEI using SecurityPPI co-equal to the use-cases in DXE using...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / PeiMain / PeiMain.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 Pei Core Main Entry Point\r
3 \r
c7935105 4Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 5This program and the accompanying materials\r
192f6d4c 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
615c6dd0 13**/\r
192f6d4c 14\r
0d516397 15#include "PeiMain.h"\r
192f6d4c 16\r
fe1e36e5 17EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi = {\r
192f6d4c 18 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
19 &gEfiPeiMemoryDiscoveredPpiGuid,\r
20 NULL\r
21};\r
22\r
40f26b8f 23///\r
82b8c8df 24/// Pei service instance\r
40f26b8f 25///\r
fe1e36e5 26EFI_PEI_SERVICES gPs = {\r
192f6d4c 27 {\r
28 PEI_SERVICES_SIGNATURE,\r
29 PEI_SERVICES_REVISION,\r
30 sizeof (EFI_PEI_SERVICES),\r
31 0,\r
32 0\r
33 },\r
34 PeiInstallPpi,\r
35 PeiReInstallPpi,\r
36 PeiLocatePpi,\r
37 PeiNotifyPpi,\r
38\r
39 PeiGetBootMode,\r
40 PeiSetBootMode,\r
41\r
42 PeiGetHobList,\r
43 PeiCreateHob,\r
44\r
3b428ade 45 PeiFfsFindNextVolume,\r
192f6d4c 46 PeiFfsFindNextFile,\r
47 PeiFfsFindSectionData,\r
48\r
b0d803fe 49 PeiInstallPeiMemory, \r
192f6d4c 50 PeiAllocatePages,\r
51 PeiAllocatePool,\r
52 (EFI_PEI_COPY_MEM)CopyMem,\r
53 (EFI_PEI_SET_MEM)SetMem,\r
54\r
55 PeiReportStatusCode,\r
14e8823a 56 PeiResetSystem,\r
b0d803fe 57\r
8d415937 58 &gPeiDefaultCpuIoPpi,\r
59 &gPeiDefaultPciCfg2Ppi,\r
b0d803fe 60\r
61 PeiFfsFindFileByName,\r
62 PeiFfsGetFileInfo,\r
63 PeiFfsGetVolumeInfo,\r
c7935105
SZ
64 PeiRegisterForShadow,\r
65 PeiFfsFindSectionData3,\r
66 PeiFfsGetFileInfo2\r
192f6d4c 67};\r
68\r
ef05e063 69/**\r
70 Shadow PeiCore module from flash to installed memory.\r
71 \r
72 @param PrivateData PeiCore's private data structure\r
73\r
74 @return PeiCore function address after shadowing.\r
75**/\r
76PEICORE_FUNCTION_POINTER\r
77ShadowPeiCore (\r
78 IN PEI_CORE_INSTANCE *PrivateData\r
79 )\r
80{\r
81 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;\r
82 EFI_PHYSICAL_ADDRESS EntryPoint;\r
83 EFI_STATUS Status;\r
84 UINT32 AuthenticationState;\r
85\r
86 PeiCoreFileHandle = NULL;\r
87\r
88 //\r
89 // Find the PEI Core in the BFV\r
90 //\r
91 Status = PrivateData->Fv[0].FvPpi->FindFileByType (\r
92 PrivateData->Fv[0].FvPpi,\r
93 EFI_FV_FILETYPE_PEI_CORE,\r
94 PrivateData->Fv[0].FvHandle,\r
95 &PeiCoreFileHandle\r
96 );\r
97 ASSERT_EFI_ERROR (Status);\r
98\r
99 //\r
100 // Shadow PEI Core into memory so it will run faster\r
101 //\r
102 Status = PeiLoadImage (\r
103 GetPeiServicesTablePointer (),\r
104 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),\r
105 PEIM_STATE_REGISITER_FOR_SHADOW,\r
106 &EntryPoint,\r
107 &AuthenticationState\r
108 );\r
109 ASSERT_EFI_ERROR (Status);\r
110\r
111 //\r
112 // Compute the PeiCore's function address after shaowed PeiCore.\r
113 // _ModuleEntryPoint is PeiCore main function entry\r
114 //\r
115 return (PEICORE_FUNCTION_POINTER)((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
116}\r
117\r
b1f6a7c6 118/**\r
82b8c8df 119 This routine is invoked by main entry of PeiMain module during transition\r
192f6d4c 120 from SEC to PEI. After switching stack in the PEI core, it will restart\r
121 with the old core data.\r
122\r
b1f6a7c6 123 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 124 environment, such as the size and location of temporary RAM, the stack location and\r
125 the BFV location.\r
b1f6a7c6 126 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.\r
5aae0aa7 127 An empty PPI list consists of a single descriptor with the end-tag\r
128 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST. As part of its initialization\r
129 phase, the PEI Foundation will add these SEC-hosted PPIs to its PPI database such\r
130 that both the PEI Foundation and any modules can leverage the associated service\r
131 calls and/or code in these early PPIs\r
b1f6a7c6 132 @param Data Pointer to old core data that is used to initialize the\r
192f6d4c 133 core's data areas.\r
82b8c8df 134 If NULL, it is first PeiCore entering.\r
192f6d4c 135\r
b1f6a7c6 136**/\r
0308e20d 137VOID\r
b1f6a7c6 138EFIAPI\r
139PeiCore (\r
140 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
141 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,\r
142 IN VOID *Data\r
143 )\r
192f6d4c 144{\r
ef05e063 145 PEI_CORE_INSTANCE PrivateData;\r
146 EFI_STATUS Status;\r
147 PEI_CORE_TEMP_POINTERS TempPtr;\r
ef05e063 148 PEI_CORE_INSTANCE *OldCoreData;\r
149 EFI_PEI_CPU_IO_PPI *CpuIo;\r
150 EFI_PEI_PCI_CFG2_PPI *PciCfg;\r
151 EFI_HOB_HANDOFF_INFO_TABLE *HandoffInformationTable;\r
192f6d4c 152\r
6b22483f 153 //\r
154 // Retrieve context passed into PEI Core\r
155 //\r
ef05e063 156 OldCoreData = (PEI_CORE_INSTANCE *)Data;\r
192f6d4c 157\r
40f26b8f 158 //\r
6b22483f 159 // Perform PEI Core phase specific actions.\r
160 //\r
161 if (OldCoreData == NULL) {\r
162 //\r
163 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.\r
164 //\r
165 ZeroMem (&PrivateData, sizeof (PEI_CORE_INSTANCE));\r
166 PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;\r
167 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));\r
168 } else {\r
169 //\r
170 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.\r
171 //\r
ef05e063 172 if (OldCoreData->ShadowedPeiCore == NULL) {\r
ef05e063 173 //\r
174 // Fixup the PeiCore's private data\r
175 //\r
6b22483f 176 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;\r
177 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;\r
ef05e063 178 if (OldCoreData->HeapOffsetPositive) {\r
179 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);\r
180 } else {\r
181 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);\r
182 }\r
183\r
6b22483f 184 //\r
185 // Initialize libraries that the PEI Core is linked against\r
186 //\r
187 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);\r
188 \r
ef05e063 189 //\r
190 // Fixup for PeiService's address\r
191 //\r
192 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);\r
193\r
194 //\r
195 // Update HandOffHob for new installed permenent memory\r
196 //\r
197 HandoffInformationTable = OldCoreData->HobList.HandoffInformationTable;\r
198 if (OldCoreData->HeapOffsetPositive) {\r
199 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList + OldCoreData->HeapOffset;\r
200 } else {\r
201 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList - OldCoreData->HeapOffset;\r
202 }\r
203 HandoffInformationTable->EfiMemoryTop = OldCoreData->PhysicalMemoryBegin + OldCoreData->PhysicalMemoryLength;\r
204 HandoffInformationTable->EfiMemoryBottom = OldCoreData->PhysicalMemoryBegin;\r
205 HandoffInformationTable->EfiFreeMemoryTop = OldCoreData->FreePhysicalMemoryTop;\r
206 HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);\r
207\r
208 //\r
424b7c9f 209 // We need convert the PPI descriptor's pointer\r
ef05e063 210 //\r
424b7c9f 211 ConvertPpiPointers (SecCoreData, OldCoreData);\r
ef05e063 212\r
213 //\r
214 // After the whole temporary memory is migrated, then we can allocate page in\r
215 // permenent memory.\r
216 //\r
217 OldCoreData->PeiMemoryInstalled = TRUE;\r
218\r
219 //\r
220 // Indicate that PeiCore reenter\r
221 //\r
222 OldCoreData->PeimDispatcherReenter = TRUE;\r
223 \r
224 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (OldCoreData->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
225 //\r
226 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.\r
227 // Every bit in the array indicate the status of the corresponding memory page available or not\r
228 //\r
229 OldCoreData->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));\r
230 }\r
231\r
ef05e063 232 //\r
233 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
234 // PEI Core and PEIMs to get high performance.\r
235 //\r
236 OldCoreData->ShadowedPeiCore = ShadowPeiCore (OldCoreData);\r
237 \r
238 //\r
6b22483f 239 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.\r
ef05e063 240 //\r
241 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);\r
6b22483f 242 \r
243 //\r
244 // Should never reach here.\r
245 //\r
246 ASSERT (FALSE);\r
247 CpuDeadLoop();\r
58dcdada 248 }\r
249\r
6b22483f 250 //\r
251 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.\r
252 //\r
253 \r
ef05e063 254 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));\r
b0d803fe 255 \r
256 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;\r
257 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;\r
258 \r
b1f6a7c6 259 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));\r
b0d803fe 260 \r
261 PrivateData.ServiceTableShadow.CpuIo = CpuIo;\r
262 PrivateData.ServiceTableShadow.PciCfg = PciCfg;\r
192f6d4c 263 }\r
6b22483f 264 \r
265 //\r
266 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory\r
267 //\r
4140a663 268 PrivateData.Ps = &PrivateData.ServiceTableShadow;\r
192f6d4c 269\r
270 //\r
6b22483f 271 // Initialize libraries that the PEI Core is linked against\r
192f6d4c 272 //\r
4140a663 273 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);\r
192f6d4c 274\r
81c7803c 275 //\r
276 // Save PeiServicePointer so that it can be retrieved anywhere.\r
277 //\r
6b22483f 278 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);\r
192f6d4c 279\r
6b22483f 280 //\r
281 // Initialize PEI Core Services\r
282 // \r
283 InitializeMemoryServices (&PrivateData, SecCoreData, OldCoreData);\r
284 InitializePpiServices (&PrivateData, OldCoreData);\r
285 \r
286 //\r
287 // Update performance measurements \r
288 //\r
289 if (OldCoreData == NULL) {\r
290 PERF_START (NULL, "SEC", NULL, 1);\r
291 PERF_END (NULL, "SEC", NULL, 0);\r
192f6d4c 292\r
192f6d4c 293 //\r
6b22483f 294 // If first pass, start performance measurement.\r
192f6d4c 295 //\r
6b22483f 296 PERF_START (NULL,"PEI", NULL, 0);\r
297 PERF_START (NULL,"PreMem", NULL, 0);\r
192f6d4c 298\r
299 } else {\r
6b22483f 300 PERF_END (NULL,"PreMem", NULL, 0);\r
301 PERF_START (NULL,"PostMem", NULL, 0);\r
302 }\r
303\r
304 //\r
305 // Complete PEI Core Service initialization\r
306 // \r
307 InitializeSecurityServices (&PrivateData.Ps, OldCoreData);\r
308 InitializeDispatcherData (&PrivateData, OldCoreData, SecCoreData);\r
309 InitializeImageServices (&PrivateData, OldCoreData);\r
192f6d4c 310\r
6b22483f 311 //\r
312 // Perform PEI Core Phase specific actions\r
313 // \r
314 if (OldCoreData == NULL) {\r
192f6d4c 315 //\r
316 // Report Status Code EFI_SW_PC_INIT\r
317 //\r
318 REPORT_STATUS_CODE (\r
319 EFI_PROGRESS_CODE,\r
f9876ecf 320 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)\r
192f6d4c 321 );\r
008d4018 322 \r
192f6d4c 323 //\r
324 // If SEC provided any PPI services to PEI, install them.\r
325 //\r
5088e385 326 if (PpiList != NULL) {\r
327 Status = PeiServicesInstallPpi (PpiList);\r
192f6d4c 328 ASSERT_EFI_ERROR (Status);\r
329 }\r
6b22483f 330 } else {\r
331 //\r
332 // Alert any listeners that there is permanent memory available\r
333 //\r
334 PERF_START (NULL,"DisMem", NULL, 0);\r
335 Status = PeiServicesInstallPpi (&mMemoryDiscoveredPpi);\r
b0d803fe 336\r
6b22483f 337 //\r
338 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI\r
339 //\r
340 ProcessNotifyList (&PrivateData);\r
b0d803fe 341\r
6b22483f 342 PERF_END (NULL,"DisMem", NULL, 0);\r
343 }\r
192f6d4c 344\r
345 //\r
346 // Call PEIM dispatcher\r
347 //\r
b0d803fe 348 PeiDispatcher (SecCoreData, &PrivateData);\r
192f6d4c 349\r
350 //\r
351 // Check if InstallPeiMemory service was called.\r
352 //\r
353 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);\r
354\r
40f26b8f 355 //\r
6b22483f 356 // Measure PEI Core execution time.\r
40f26b8f 357 //\r
192f6d4c 358 PERF_END (NULL, "PostMem", NULL, 0);\r
359\r
6b22483f 360 //\r
361 // Lookup DXE IPL PPI\r
362 //\r
192f6d4c 363 Status = PeiServicesLocatePpi (\r
364 &gEfiDxeIplPpiGuid,\r
365 0,\r
366 NULL,\r
367 (VOID **)&TempPtr.DxeIpl\r
368 );\r
369 ASSERT_EFI_ERROR (Status);\r
370\r
40f26b8f 371 //\r
372 // Enter DxeIpl to load Dxe core.\r
373 //\r
192f6d4c 374 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));\r
375 Status = TempPtr.DxeIpl->Entry (\r
376 TempPtr.DxeIpl,\r
4140a663 377 &PrivateData.Ps,\r
192f6d4c 378 PrivateData.HobList\r
379 );\r
0308e20d 380 //\r
381 // Should never reach here.\r
382 //\r
192f6d4c 383 ASSERT_EFI_ERROR (Status);\r
0308e20d 384 CpuDeadLoop();\r
192f6d4c 385}\r