]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
Refine code to make it more safely.
[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
0f9ebb32 123 @param SecCoreDataPtr 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
0f9ebb32 140 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreDataPtr,\r
b1f6a7c6 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
0f9ebb32
LG
146 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
147 EFI_SEC_PEI_HAND_OFF NewSecCoreData;\r
ef05e063 148 EFI_STATUS Status;\r
149 PEI_CORE_TEMP_POINTERS TempPtr;\r
ef05e063 150 PEI_CORE_INSTANCE *OldCoreData;\r
151 EFI_PEI_CPU_IO_PPI *CpuIo;\r
152 EFI_PEI_PCI_CFG2_PPI *PciCfg;\r
153 EFI_HOB_HANDOFF_INFO_TABLE *HandoffInformationTable;\r
0f9ebb32
LG
154 EFI_PEI_TEMPORARY_RAM_DONE_PPI *TemporaryRamDonePpi;\r
155 \r
6b22483f 156 //\r
157 // Retrieve context passed into PEI Core\r
158 //\r
0f9ebb32
LG
159 OldCoreData = (PEI_CORE_INSTANCE *) Data;\r
160 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) SecCoreDataPtr;\r
192f6d4c 161\r
40f26b8f 162 //\r
6b22483f 163 // Perform PEI Core phase specific actions.\r
164 //\r
165 if (OldCoreData == NULL) {\r
166 //\r
167 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.\r
168 //\r
169 ZeroMem (&PrivateData, sizeof (PEI_CORE_INSTANCE));\r
170 PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;\r
171 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));\r
172 } else {\r
173 //\r
174 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.\r
175 //\r
ef05e063 176 if (OldCoreData->ShadowedPeiCore == NULL) {\r
ef05e063 177 //\r
178 // Fixup the PeiCore's private data\r
179 //\r
6b22483f 180 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;\r
181 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;\r
ef05e063 182 if (OldCoreData->HeapOffsetPositive) {\r
183 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);\r
184 } else {\r
185 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);\r
186 }\r
187\r
6b22483f 188 //\r
189 // Initialize libraries that the PEI Core is linked against\r
190 //\r
191 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);\r
192 \r
ef05e063 193 //\r
194 // Fixup for PeiService's address\r
195 //\r
196 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);\r
197\r
198 //\r
199 // Update HandOffHob for new installed permenent memory\r
200 //\r
201 HandoffInformationTable = OldCoreData->HobList.HandoffInformationTable;\r
202 if (OldCoreData->HeapOffsetPositive) {\r
203 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList + OldCoreData->HeapOffset;\r
204 } else {\r
205 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList - OldCoreData->HeapOffset;\r
206 }\r
207 HandoffInformationTable->EfiMemoryTop = OldCoreData->PhysicalMemoryBegin + OldCoreData->PhysicalMemoryLength;\r
208 HandoffInformationTable->EfiMemoryBottom = OldCoreData->PhysicalMemoryBegin;\r
209 HandoffInformationTable->EfiFreeMemoryTop = OldCoreData->FreePhysicalMemoryTop;\r
210 HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);\r
211\r
212 //\r
424b7c9f 213 // We need convert the PPI descriptor's pointer\r
ef05e063 214 //\r
424b7c9f 215 ConvertPpiPointers (SecCoreData, OldCoreData);\r
ef05e063 216\r
217 //\r
218 // After the whole temporary memory is migrated, then we can allocate page in\r
219 // permenent memory.\r
220 //\r
221 OldCoreData->PeiMemoryInstalled = TRUE;\r
222\r
223 //\r
224 // Indicate that PeiCore reenter\r
225 //\r
226 OldCoreData->PeimDispatcherReenter = TRUE;\r
227 \r
228 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (OldCoreData->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
229 //\r
230 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.\r
231 // Every bit in the array indicate the status of the corresponding memory page available or not\r
232 //\r
233 OldCoreData->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));\r
234 }\r
235\r
ef05e063 236 //\r
237 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
238 // PEI Core and PEIMs to get high performance.\r
239 //\r
240 OldCoreData->ShadowedPeiCore = ShadowPeiCore (OldCoreData);\r
241 \r
242 //\r
6b22483f 243 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.\r
ef05e063 244 //\r
245 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);\r
6b22483f 246 \r
247 //\r
248 // Should never reach here.\r
249 //\r
250 ASSERT (FALSE);\r
251 CpuDeadLoop();\r
58dcdada 252 }\r
253\r
6b22483f 254 //\r
255 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.\r
256 //\r
0f9ebb32
LG
257 CopyMem (&NewSecCoreData, SecCoreDataPtr, sizeof (NewSecCoreData));\r
258 SecCoreData = &NewSecCoreData;\r
259\r
ef05e063 260 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));\r
0f9ebb32 261\r
b0d803fe 262 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;\r
263 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;\r
264 \r
b1f6a7c6 265 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));\r
b0d803fe 266 \r
267 PrivateData.ServiceTableShadow.CpuIo = CpuIo;\r
268 PrivateData.ServiceTableShadow.PciCfg = PciCfg;\r
192f6d4c 269 }\r
6b22483f 270 \r
271 //\r
272 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory\r
273 //\r
4140a663 274 PrivateData.Ps = &PrivateData.ServiceTableShadow;\r
192f6d4c 275\r
276 //\r
6b22483f 277 // Initialize libraries that the PEI Core is linked against\r
192f6d4c 278 //\r
4140a663 279 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);\r
192f6d4c 280\r
81c7803c 281 //\r
282 // Save PeiServicePointer so that it can be retrieved anywhere.\r
283 //\r
6b22483f 284 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);\r
192f6d4c 285\r
6b22483f 286 //\r
287 // Initialize PEI Core Services\r
288 // \r
289 InitializeMemoryServices (&PrivateData, SecCoreData, OldCoreData);\r
290 InitializePpiServices (&PrivateData, OldCoreData);\r
291 \r
292 //\r
293 // Update performance measurements \r
294 //\r
295 if (OldCoreData == NULL) {\r
296 PERF_START (NULL, "SEC", NULL, 1);\r
297 PERF_END (NULL, "SEC", NULL, 0);\r
192f6d4c 298\r
192f6d4c 299 //\r
6b22483f 300 // If first pass, start performance measurement.\r
192f6d4c 301 //\r
6b22483f 302 PERF_START (NULL,"PEI", NULL, 0);\r
303 PERF_START (NULL,"PreMem", NULL, 0);\r
192f6d4c 304\r
305 } else {\r
6b22483f 306 PERF_END (NULL,"PreMem", NULL, 0);\r
307 PERF_START (NULL,"PostMem", NULL, 0);\r
308 }\r
309\r
310 //\r
311 // Complete PEI Core Service initialization\r
312 // \r
313 InitializeSecurityServices (&PrivateData.Ps, OldCoreData);\r
314 InitializeDispatcherData (&PrivateData, OldCoreData, SecCoreData);\r
315 InitializeImageServices (&PrivateData, OldCoreData);\r
192f6d4c 316\r
6b22483f 317 //\r
318 // Perform PEI Core Phase specific actions\r
319 // \r
320 if (OldCoreData == NULL) {\r
192f6d4c 321 //\r
322 // Report Status Code EFI_SW_PC_INIT\r
323 //\r
324 REPORT_STATUS_CODE (\r
325 EFI_PROGRESS_CODE,\r
f9876ecf 326 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)\r
192f6d4c 327 );\r
008d4018 328 \r
192f6d4c 329 //\r
330 // If SEC provided any PPI services to PEI, install them.\r
331 //\r
5088e385 332 if (PpiList != NULL) {\r
333 Status = PeiServicesInstallPpi (PpiList);\r
192f6d4c 334 ASSERT_EFI_ERROR (Status);\r
335 }\r
6b22483f 336 } else {\r
0f9ebb32
LG
337 //\r
338 // Try to locate Temporary RAM Done Ppi.\r
339 //\r
340 Status = PeiServicesLocatePpi (\r
341 &gEfiTemporaryRamDonePpiGuid,\r
342 0,\r
343 NULL,\r
344 (VOID**)&TemporaryRamDonePpi\r
345 );\r
346 if (!EFI_ERROR (Status)) {\r
347 //\r
348 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.\r
349 //\r
350 TemporaryRamDonePpi->TemporaryRamDone ();\r
351 }\r
352\r
6b22483f 353 //\r
354 // Alert any listeners that there is permanent memory available\r
355 //\r
356 PERF_START (NULL,"DisMem", NULL, 0);\r
357 Status = PeiServicesInstallPpi (&mMemoryDiscoveredPpi);\r
b0d803fe 358\r
6b22483f 359 //\r
360 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI\r
361 //\r
362 ProcessNotifyList (&PrivateData);\r
b0d803fe 363\r
6b22483f 364 PERF_END (NULL,"DisMem", NULL, 0);\r
365 }\r
192f6d4c 366\r
367 //\r
368 // Call PEIM dispatcher\r
369 //\r
b0d803fe 370 PeiDispatcher (SecCoreData, &PrivateData);\r
192f6d4c 371\r
372 //\r
373 // Check if InstallPeiMemory service was called.\r
374 //\r
375 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);\r
376\r
40f26b8f 377 //\r
6b22483f 378 // Measure PEI Core execution time.\r
40f26b8f 379 //\r
192f6d4c 380 PERF_END (NULL, "PostMem", NULL, 0);\r
381\r
6b22483f 382 //\r
383 // Lookup DXE IPL PPI\r
384 //\r
192f6d4c 385 Status = PeiServicesLocatePpi (\r
386 &gEfiDxeIplPpiGuid,\r
387 0,\r
388 NULL,\r
389 (VOID **)&TempPtr.DxeIpl\r
390 );\r
391 ASSERT_EFI_ERROR (Status);\r
392\r
40f26b8f 393 //\r
394 // Enter DxeIpl to load Dxe core.\r
395 //\r
192f6d4c 396 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));\r
397 Status = TempPtr.DxeIpl->Entry (\r
398 TempPtr.DxeIpl,\r
4140a663 399 &PrivateData.Ps,\r
192f6d4c 400 PrivateData.HobList\r
401 );\r
0308e20d 402 //\r
403 // Should never reach here.\r
404 //\r
192f6d4c 405 ASSERT_EFI_ERROR (Status);\r
0308e20d 406 CpuDeadLoop();\r
192f6d4c 407}\r