]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
Refine code to make it more safely.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / PeiMain / PeiMain.c
... / ...
CommitLineData
1/** @file\r
2 Pei Core Main Entry Point\r
3 \r
4Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
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
13**/\r
14\r
15#include "PeiMain.h"\r
16\r
17EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi = {\r
18 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
19 &gEfiPeiMemoryDiscoveredPpiGuid,\r
20 NULL\r
21};\r
22\r
23///\r
24/// Pei service instance\r
25///\r
26EFI_PEI_SERVICES gPs = {\r
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
45 PeiFfsFindNextVolume,\r
46 PeiFfsFindNextFile,\r
47 PeiFfsFindSectionData,\r
48\r
49 PeiInstallPeiMemory, \r
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
56 PeiResetSystem,\r
57\r
58 &gPeiDefaultCpuIoPpi,\r
59 &gPeiDefaultPciCfg2Ppi,\r
60\r
61 PeiFfsFindFileByName,\r
62 PeiFfsGetFileInfo,\r
63 PeiFfsGetVolumeInfo,\r
64 PeiRegisterForShadow,\r
65 PeiFfsFindSectionData3,\r
66 PeiFfsGetFileInfo2\r
67};\r
68\r
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
118/**\r
119 This routine is invoked by main entry of PeiMain module during transition\r
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
123 @param SecCoreDataPtr Points to a data structure containing information about the PEI core's operating\r
124 environment, such as the size and location of temporary RAM, the stack location and\r
125 the BFV location.\r
126 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.\r
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
132 @param Data Pointer to old core data that is used to initialize the\r
133 core's data areas.\r
134 If NULL, it is first PeiCore entering.\r
135\r
136**/\r
137VOID\r
138EFIAPI\r
139PeiCore (\r
140 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreDataPtr,\r
141 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,\r
142 IN VOID *Data\r
143 )\r
144{\r
145 PEI_CORE_INSTANCE PrivateData;\r
146 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
147 EFI_SEC_PEI_HAND_OFF NewSecCoreData;\r
148 EFI_STATUS Status;\r
149 PEI_CORE_TEMP_POINTERS TempPtr;\r
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
154 EFI_PEI_TEMPORARY_RAM_DONE_PPI *TemporaryRamDonePpi;\r
155 \r
156 //\r
157 // Retrieve context passed into PEI Core\r
158 //\r
159 OldCoreData = (PEI_CORE_INSTANCE *) Data;\r
160 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) SecCoreDataPtr;\r
161\r
162 //\r
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
176 if (OldCoreData->ShadowedPeiCore == NULL) {\r
177 //\r
178 // Fixup the PeiCore's private data\r
179 //\r
180 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;\r
181 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;\r
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
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
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
213 // We need convert the PPI descriptor's pointer\r
214 //\r
215 ConvertPpiPointers (SecCoreData, OldCoreData);\r
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
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
243 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.\r
244 //\r
245 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);\r
246 \r
247 //\r
248 // Should never reach here.\r
249 //\r
250 ASSERT (FALSE);\r
251 CpuDeadLoop();\r
252 }\r
253\r
254 //\r
255 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.\r
256 //\r
257 CopyMem (&NewSecCoreData, SecCoreDataPtr, sizeof (NewSecCoreData));\r
258 SecCoreData = &NewSecCoreData;\r
259\r
260 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));\r
261\r
262 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;\r
263 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;\r
264 \r
265 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));\r
266 \r
267 PrivateData.ServiceTableShadow.CpuIo = CpuIo;\r
268 PrivateData.ServiceTableShadow.PciCfg = PciCfg;\r
269 }\r
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
274 PrivateData.Ps = &PrivateData.ServiceTableShadow;\r
275\r
276 //\r
277 // Initialize libraries that the PEI Core is linked against\r
278 //\r
279 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);\r
280\r
281 //\r
282 // Save PeiServicePointer so that it can be retrieved anywhere.\r
283 //\r
284 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);\r
285\r
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
298\r
299 //\r
300 // If first pass, start performance measurement.\r
301 //\r
302 PERF_START (NULL,"PEI", NULL, 0);\r
303 PERF_START (NULL,"PreMem", NULL, 0);\r
304\r
305 } else {\r
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
316\r
317 //\r
318 // Perform PEI Core Phase specific actions\r
319 // \r
320 if (OldCoreData == NULL) {\r
321 //\r
322 // Report Status Code EFI_SW_PC_INIT\r
323 //\r
324 REPORT_STATUS_CODE (\r
325 EFI_PROGRESS_CODE,\r
326 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)\r
327 );\r
328 \r
329 //\r
330 // If SEC provided any PPI services to PEI, install them.\r
331 //\r
332 if (PpiList != NULL) {\r
333 Status = PeiServicesInstallPpi (PpiList);\r
334 ASSERT_EFI_ERROR (Status);\r
335 }\r
336 } else {\r
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
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
358\r
359 //\r
360 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI\r
361 //\r
362 ProcessNotifyList (&PrivateData);\r
363\r
364 PERF_END (NULL,"DisMem", NULL, 0);\r
365 }\r
366\r
367 //\r
368 // Call PEIM dispatcher\r
369 //\r
370 PeiDispatcher (SecCoreData, &PrivateData);\r
371\r
372 //\r
373 // Check if InstallPeiMemory service was called.\r
374 //\r
375 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);\r
376\r
377 //\r
378 // Measure PEI Core execution time.\r
379 //\r
380 PERF_END (NULL, "PostMem", NULL, 0);\r
381\r
382 //\r
383 // Lookup DXE IPL PPI\r
384 //\r
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
393 //\r
394 // Enter DxeIpl to load Dxe core.\r
395 //\r
396 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));\r
397 Status = TempPtr.DxeIpl->Entry (\r
398 TempPtr.DxeIpl,\r
399 &PrivateData.Ps,\r
400 PrivateData.HobList\r
401 );\r
402 //\r
403 // Should never reach here.\r
404 //\r
405 ASSERT_EFI_ERROR (Status);\r
406 CpuDeadLoop();\r
407}\r