]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
MdeModulePkg PeiCore: Remove the using of PcdPeiCoreMaxPpiSupported
[mirror_edk2.git] / MdeModulePkg / Core / Pei / PeiMain / PeiMain.c
1 /** @file
2 Pei Core Main Entry Point
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PeiMain.h"
16
17 EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi = {
18 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
19 &gEfiPeiMemoryDiscoveredPpiGuid,
20 NULL
21 };
22
23 ///
24 /// Pei service instance
25 ///
26 EFI_PEI_SERVICES gPs = {
27 {
28 PEI_SERVICES_SIGNATURE,
29 PEI_SERVICES_REVISION,
30 sizeof (EFI_PEI_SERVICES),
31 0,
32 0
33 },
34 PeiInstallPpi,
35 PeiReInstallPpi,
36 PeiLocatePpi,
37 PeiNotifyPpi,
38
39 PeiGetBootMode,
40 PeiSetBootMode,
41
42 PeiGetHobList,
43 PeiCreateHob,
44
45 PeiFfsFindNextVolume,
46 PeiFfsFindNextFile,
47 PeiFfsFindSectionData,
48
49 PeiInstallPeiMemory,
50 PeiAllocatePages,
51 PeiAllocatePool,
52 (EFI_PEI_COPY_MEM)CopyMem,
53 (EFI_PEI_SET_MEM)SetMem,
54
55 PeiReportStatusCode,
56 PeiResetSystem,
57
58 &gPeiDefaultCpuIoPpi,
59 &gPeiDefaultPciCfg2Ppi,
60
61 PeiFfsFindFileByName,
62 PeiFfsGetFileInfo,
63 PeiFfsGetVolumeInfo,
64 PeiRegisterForShadow,
65 PeiFfsFindSectionData3,
66 PeiFfsGetFileInfo2,
67 PeiResetSystem2,
68 PeiFreePages,
69 };
70
71 /**
72 Shadow PeiCore module from flash to installed memory.
73
74 @param PrivateData PeiCore's private data structure
75
76 @return PeiCore function address after shadowing.
77 **/
78 PEICORE_FUNCTION_POINTER
79 ShadowPeiCore (
80 IN PEI_CORE_INSTANCE *PrivateData
81 )
82 {
83 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;
84 EFI_PHYSICAL_ADDRESS EntryPoint;
85 EFI_STATUS Status;
86 UINT32 AuthenticationState;
87
88 PeiCoreFileHandle = NULL;
89
90 //
91 // Find the PEI Core in the BFV
92 //
93 Status = PrivateData->Fv[0].FvPpi->FindFileByType (
94 PrivateData->Fv[0].FvPpi,
95 EFI_FV_FILETYPE_PEI_CORE,
96 PrivateData->Fv[0].FvHandle,
97 &PeiCoreFileHandle
98 );
99 ASSERT_EFI_ERROR (Status);
100
101 //
102 // Shadow PEI Core into memory so it will run faster
103 //
104 Status = PeiLoadImage (
105 GetPeiServicesTablePointer (),
106 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),
107 PEIM_STATE_REGISTER_FOR_SHADOW,
108 &EntryPoint,
109 &AuthenticationState
110 );
111 ASSERT_EFI_ERROR (Status);
112
113 //
114 // Compute the PeiCore's function address after shaowed PeiCore.
115 // _ModuleEntryPoint is PeiCore main function entry
116 //
117 return (PEICORE_FUNCTION_POINTER)((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);
118 }
119
120 /**
121 This routine is invoked by main entry of PeiMain module during transition
122 from SEC to PEI. After switching stack in the PEI core, it will restart
123 with the old core data.
124
125 @param SecCoreDataPtr Points to a data structure containing information about the PEI core's operating
126 environment, such as the size and location of temporary RAM, the stack location and
127 the BFV location.
128 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.
129 An empty PPI list consists of a single descriptor with the end-tag
130 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST. As part of its initialization
131 phase, the PEI Foundation will add these SEC-hosted PPIs to its PPI database such
132 that both the PEI Foundation and any modules can leverage the associated service
133 calls and/or code in these early PPIs
134 @param Data Pointer to old core data that is used to initialize the
135 core's data areas.
136 If NULL, it is first PeiCore entering.
137
138 **/
139 VOID
140 EFIAPI
141 PeiCore (
142 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreDataPtr,
143 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
144 IN VOID *Data
145 )
146 {
147 PEI_CORE_INSTANCE PrivateData;
148 EFI_SEC_PEI_HAND_OFF *SecCoreData;
149 EFI_SEC_PEI_HAND_OFF NewSecCoreData;
150 EFI_STATUS Status;
151 PEI_CORE_TEMP_POINTERS TempPtr;
152 PEI_CORE_INSTANCE *OldCoreData;
153 EFI_PEI_CPU_IO_PPI *CpuIo;
154 EFI_PEI_PCI_CFG2_PPI *PciCfg;
155 EFI_HOB_HANDOFF_INFO_TABLE *HandoffInformationTable;
156 EFI_PEI_TEMPORARY_RAM_DONE_PPI *TemporaryRamDonePpi;
157 UINTN Index;
158
159 //
160 // Retrieve context passed into PEI Core
161 //
162 OldCoreData = (PEI_CORE_INSTANCE *) Data;
163 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) SecCoreDataPtr;
164
165 //
166 // Perform PEI Core phase specific actions.
167 //
168 if (OldCoreData == NULL) {
169 //
170 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
171 //
172 ZeroMem (&PrivateData, sizeof (PEI_CORE_INSTANCE));
173 PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;
174 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
175 } else {
176 //
177 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
178 //
179 if (OldCoreData->ShadowedPeiCore == NULL) {
180 //
181 // Fixup the PeiCore's private data
182 //
183 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;
184 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;
185 if (OldCoreData->HeapOffsetPositive) {
186 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);
187 if (OldCoreData->UnknownFvInfo != NULL) {
188 OldCoreData->UnknownFvInfo = (PEI_CORE_UNKNOW_FORMAT_FV_INFO *) ((UINT8 *) OldCoreData->UnknownFvInfo + OldCoreData->HeapOffset);
189 }
190 if (OldCoreData->CurrentFvFileHandles != NULL) {
191 OldCoreData->CurrentFvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->CurrentFvFileHandles + OldCoreData->HeapOffset);
192 }
193 if (OldCoreData->PpiData.PpiList.PpiPtrs != NULL) {
194 OldCoreData->PpiData.PpiList.PpiPtrs = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.PpiList.PpiPtrs + OldCoreData->HeapOffset);
195 }
196 if (OldCoreData->PpiData.CallbackNotifyList.NotifyPtrs != NULL) {
197 OldCoreData->PpiData.CallbackNotifyList.NotifyPtrs = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.CallbackNotifyList.NotifyPtrs + OldCoreData->HeapOffset);
198 }
199 if (OldCoreData->PpiData.DispatchNotifyList.NotifyPtrs != NULL) {
200 OldCoreData->PpiData.DispatchNotifyList.NotifyPtrs = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.DispatchNotifyList.NotifyPtrs + OldCoreData->HeapOffset);
201 }
202 OldCoreData->Fv = (PEI_CORE_FV_HANDLE *) ((UINT8 *) OldCoreData->Fv + OldCoreData->HeapOffset);
203 for (Index = 0; Index < OldCoreData->FvCount; Index ++) {
204 if (OldCoreData->Fv[Index].PeimState != NULL) {
205 OldCoreData->Fv[Index].PeimState = (UINT8 *) OldCoreData->Fv[Index].PeimState + OldCoreData->HeapOffset;
206 }
207 if (OldCoreData->Fv[Index].FvFileHandles != NULL) {
208 OldCoreData->Fv[Index].FvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->Fv[Index].FvFileHandles + OldCoreData->HeapOffset);
209 }
210 }
211 OldCoreData->TempFileGuid = (EFI_GUID *) ((UINT8 *) OldCoreData->TempFileGuid + OldCoreData->HeapOffset);
212 OldCoreData->TempFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->TempFileHandles + OldCoreData->HeapOffset);
213 } else {
214 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);
215 if (OldCoreData->UnknownFvInfo != NULL) {
216 OldCoreData->UnknownFvInfo = (PEI_CORE_UNKNOW_FORMAT_FV_INFO *) ((UINT8 *) OldCoreData->UnknownFvInfo - OldCoreData->HeapOffset);
217 }
218 if (OldCoreData->CurrentFvFileHandles != NULL) {
219 OldCoreData->CurrentFvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->CurrentFvFileHandles - OldCoreData->HeapOffset);
220 }
221 if (OldCoreData->PpiData.PpiList.PpiPtrs != NULL) {
222 OldCoreData->PpiData.PpiList.PpiPtrs = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.PpiList.PpiPtrs - OldCoreData->HeapOffset);
223 }
224 if (OldCoreData->PpiData.CallbackNotifyList.NotifyPtrs != NULL) {
225 OldCoreData->PpiData.CallbackNotifyList.NotifyPtrs = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.CallbackNotifyList.NotifyPtrs - OldCoreData->HeapOffset);
226 }
227 if (OldCoreData->PpiData.DispatchNotifyList.NotifyPtrs != NULL) {
228 OldCoreData->PpiData.DispatchNotifyList.NotifyPtrs = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.DispatchNotifyList.NotifyPtrs - OldCoreData->HeapOffset);
229 }
230 OldCoreData->Fv = (PEI_CORE_FV_HANDLE *) ((UINT8 *) OldCoreData->Fv - OldCoreData->HeapOffset);
231 for (Index = 0; Index < OldCoreData->FvCount; Index ++) {
232 if (OldCoreData->Fv[Index].PeimState != NULL) {
233 OldCoreData->Fv[Index].PeimState = (UINT8 *) OldCoreData->Fv[Index].PeimState - OldCoreData->HeapOffset;
234 }
235 if (OldCoreData->Fv[Index].FvFileHandles != NULL) {
236 OldCoreData->Fv[Index].FvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->Fv[Index].FvFileHandles - OldCoreData->HeapOffset);
237 }
238 }
239 OldCoreData->TempFileGuid = (EFI_GUID *) ((UINT8 *) OldCoreData->TempFileGuid - OldCoreData->HeapOffset);
240 OldCoreData->TempFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->TempFileHandles - OldCoreData->HeapOffset);
241 }
242
243 //
244 // Fixup for PeiService's address
245 //
246 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
247
248 //
249 // Initialize libraries that the PEI Core is linked against
250 //
251 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
252
253 //
254 // Update HandOffHob for new installed permanent memory
255 //
256 HandoffInformationTable = OldCoreData->HobList.HandoffInformationTable;
257 if (OldCoreData->HeapOffsetPositive) {
258 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList + OldCoreData->HeapOffset;
259 } else {
260 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList - OldCoreData->HeapOffset;
261 }
262 HandoffInformationTable->EfiMemoryTop = OldCoreData->PhysicalMemoryBegin + OldCoreData->PhysicalMemoryLength;
263 HandoffInformationTable->EfiMemoryBottom = OldCoreData->PhysicalMemoryBegin;
264 HandoffInformationTable->EfiFreeMemoryTop = OldCoreData->FreePhysicalMemoryTop;
265 HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);
266
267 //
268 // We need convert MemoryBaseAddress in memory allocation HOBs
269 //
270 ConvertMemoryAllocationHobs (OldCoreData);
271
272 //
273 // We need convert the PPI descriptor's pointer
274 //
275 ConvertPpiPointers (SecCoreData, OldCoreData);
276
277 //
278 // After the whole temporary memory is migrated, then we can allocate page in
279 // permanent memory.
280 //
281 OldCoreData->PeiMemoryInstalled = TRUE;
282
283 //
284 // Indicate that PeiCore reenter
285 //
286 OldCoreData->PeimDispatcherReenter = TRUE;
287
288 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (OldCoreData->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
289 //
290 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
291 // Every bit in the array indicate the status of the corresponding memory page available or not
292 //
293 OldCoreData->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));
294 }
295
296 //
297 // Shadow PEI Core. When permanent memory is avaiable, shadow
298 // PEI Core and PEIMs to get high performance.
299 //
300 OldCoreData->ShadowedPeiCore = (PEICORE_FUNCTION_POINTER) (UINTN) PeiCore;
301 if ((HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME && PcdGetBool (PcdShadowPeimOnS3Boot))
302 || (HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME && PcdGetBool (PcdShadowPeimOnBoot))) {
303 OldCoreData->ShadowedPeiCore = ShadowPeiCore (OldCoreData);
304 }
305
306 //
307 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
308 //
309 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);
310
311 //
312 // Should never reach here.
313 //
314 ASSERT (FALSE);
315 CpuDeadLoop();
316
317 UNREACHABLE ();
318 }
319
320 //
321 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
322 //
323 CopyMem (&NewSecCoreData, SecCoreDataPtr, sizeof (NewSecCoreData));
324 SecCoreData = &NewSecCoreData;
325
326 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));
327
328 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;
329 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;
330
331 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
332
333 PrivateData.ServiceTableShadow.CpuIo = CpuIo;
334 PrivateData.ServiceTableShadow.PciCfg = PciCfg;
335 }
336
337 //
338 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
339 //
340 PrivateData.Ps = &PrivateData.ServiceTableShadow;
341
342 //
343 // Save PeiServicePointer so that it can be retrieved anywhere.
344 //
345 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
346
347 //
348 // Initialize libraries that the PEI Core is linked against
349 //
350 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
351
352 //
353 // Initialize PEI Core Services
354 //
355 InitializeMemoryServices (&PrivateData, SecCoreData, OldCoreData);
356
357 //
358 // Update performance measurements
359 //
360 if (OldCoreData == NULL) {
361 PERF_EVENT ("SEC"); // Means the end of SEC phase.
362
363 //
364 // If first pass, start performance measurement.
365 //
366 PERF_CROSSMODULE_BEGIN ("PEI");
367 PERF_INMODULE_BEGIN ("PreMem");
368
369 } else {
370 PERF_INMODULE_END ("PreMem");
371 PERF_INMODULE_BEGIN ("PostMem");
372 }
373
374 //
375 // Complete PEI Core Service initialization
376 //
377 InitializeSecurityServices (&PrivateData.Ps, OldCoreData);
378 InitializeDispatcherData (&PrivateData, OldCoreData, SecCoreData);
379 InitializeImageServices (&PrivateData, OldCoreData);
380
381 //
382 // Perform PEI Core Phase specific actions
383 //
384 if (OldCoreData == NULL) {
385 //
386 // Report Status Code EFI_SW_PC_INIT
387 //
388 REPORT_STATUS_CODE (
389 EFI_PROGRESS_CODE,
390 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)
391 );
392
393 //
394 // If SEC provided the PpiList, process it.
395 //
396 if (PpiList != NULL) {
397 ProcessPpiListFromSec ((CONST EFI_PEI_SERVICES **) &PrivateData.Ps, PpiList);
398 }
399 } else {
400 //
401 // Try to locate Temporary RAM Done Ppi.
402 //
403 Status = PeiServicesLocatePpi (
404 &gEfiTemporaryRamDonePpiGuid,
405 0,
406 NULL,
407 (VOID**)&TemporaryRamDonePpi
408 );
409 if (!EFI_ERROR (Status)) {
410 //
411 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.
412 //
413 TemporaryRamDonePpi->TemporaryRamDone ();
414 }
415
416 //
417 // Alert any listeners that there is permanent memory available
418 //
419 PERF_INMODULE_BEGIN ("DisMem");
420 Status = PeiServicesInstallPpi (&mMemoryDiscoveredPpi);
421
422 //
423 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
424 //
425 ProcessDispatchNotifyList (&PrivateData);
426
427 PERF_INMODULE_END ("DisMem");
428 }
429
430 //
431 // Call PEIM dispatcher
432 //
433 PeiDispatcher (SecCoreData, &PrivateData);
434
435 if (PrivateData.HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) {
436 //
437 // Check if InstallPeiMemory service was called on non-S3 resume boot path.
438 //
439 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);
440 }
441
442 //
443 // Measure PEI Core execution time.
444 //
445 PERF_INMODULE_END ("PostMem");
446
447 //
448 // Lookup DXE IPL PPI
449 //
450 Status = PeiServicesLocatePpi (
451 &gEfiDxeIplPpiGuid,
452 0,
453 NULL,
454 (VOID **)&TempPtr.DxeIpl
455 );
456 ASSERT_EFI_ERROR (Status);
457
458 if (EFI_ERROR (Status)) {
459 //
460 // Report status code to indicate DXE IPL PPI could not be found.
461 //
462 REPORT_STATUS_CODE (
463 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
464 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PEI_CORE_EC_DXEIPL_NOT_FOUND)
465 );
466 CpuDeadLoop ();
467 }
468
469 //
470 // Enter DxeIpl to load Dxe core.
471 //
472 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));
473 Status = TempPtr.DxeIpl->Entry (
474 TempPtr.DxeIpl,
475 &PrivateData.Ps,
476 PrivateData.HobList
477 );
478 //
479 // Should never reach here.
480 //
481 ASSERT_EFI_ERROR (Status);
482 CpuDeadLoop();
483
484 UNREACHABLE ();
485 }