2 Pei Core Main Entry Point
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi
= {
12 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
13 &gEfiPeiMemoryDiscoveredPpiGuid
,
18 /// Pei service instance
20 EFI_PEI_SERVICES gPs
= {
22 PEI_SERVICES_SIGNATURE
,
23 PEI_SERVICES_REVISION
,
24 sizeof (EFI_PEI_SERVICES
),
41 PeiFfsFindSectionData
,
46 (EFI_PEI_COPY_MEM
)CopyMem
,
47 (EFI_PEI_SET_MEM
)SetMem
,
53 &gPeiDefaultPciCfg2Ppi
,
59 PeiFfsFindSectionData3
,
66 Shadow PeiCore module from flash to installed memory.
68 @param PrivateData PeiCore's private data structure
70 @return PeiCore function address after shadowing.
72 PEICORE_FUNCTION_POINTER
74 IN PEI_CORE_INSTANCE
*PrivateData
77 EFI_PEI_FILE_HANDLE PeiCoreFileHandle
;
78 EFI_PHYSICAL_ADDRESS EntryPoint
;
80 UINT32 AuthenticationState
;
82 EFI_PEI_CORE_FV_LOCATION_PPI
*PeiCoreFvLocationPpi
;
85 PeiCoreFileHandle
= NULL
;
87 // Default PeiCore is in BFV
91 // Find the PEI Core either from EFI_PEI_CORE_FV_LOCATION_PPI indicated FV or BFV
93 Status
= PeiServicesLocatePpi (
94 &gEfiPeiCoreFvLocationPpiGuid
,
97 (VOID
**) &PeiCoreFvLocationPpi
99 if (!EFI_ERROR (Status
) && (PeiCoreFvLocationPpi
->PeiCoreFvLocation
!= NULL
)) {
101 // If PeiCoreFvLocation present, the PEI Core should be found from indicated FV
103 for (Index
= 0; Index
< PrivateData
->FvCount
; Index
++) {
104 if (PrivateData
->Fv
[Index
].FvHandle
== PeiCoreFvLocationPpi
->PeiCoreFvLocation
) {
105 PeiCoreFvIndex
= Index
;
109 ASSERT (Index
< PrivateData
->FvCount
);
112 // Find PEI Core from the given FV index
114 Status
= PrivateData
->Fv
[PeiCoreFvIndex
].FvPpi
->FindFileByType (
115 PrivateData
->Fv
[PeiCoreFvIndex
].FvPpi
,
116 EFI_FV_FILETYPE_PEI_CORE
,
117 PrivateData
->Fv
[PeiCoreFvIndex
].FvHandle
,
120 ASSERT_EFI_ERROR (Status
);
123 // Shadow PEI Core into memory so it will run faster
125 Status
= PeiLoadImage (
126 GetPeiServicesTablePointer (),
127 *((EFI_PEI_FILE_HANDLE
*)&PeiCoreFileHandle
),
128 PEIM_STATE_REGISTER_FOR_SHADOW
,
132 ASSERT_EFI_ERROR (Status
);
135 // Compute the PeiCore's function address after shadowed PeiCore.
136 // _ModuleEntryPoint is PeiCore main function entry
138 return (PEICORE_FUNCTION_POINTER
)((UINTN
) EntryPoint
+ (UINTN
) PeiCore
- (UINTN
) _ModuleEntryPoint
);
142 This routine is invoked by main entry of PeiMain module during transition
143 from SEC to PEI. After switching stack in the PEI core, it will restart
144 with the old core data.
146 @param SecCoreDataPtr Points to a data structure containing information about the PEI core's operating
147 environment, such as the size and location of temporary RAM, the stack location and
149 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.
150 An empty PPI list consists of a single descriptor with the end-tag
151 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST. As part of its initialization
152 phase, the PEI Foundation will add these SEC-hosted PPIs to its PPI database such
153 that both the PEI Foundation and any modules can leverage the associated service
154 calls and/or code in these early PPIs
155 @param Data Pointer to old core data that is used to initialize the
157 If NULL, it is first PeiCore entering.
163 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreDataPtr
,
164 IN CONST EFI_PEI_PPI_DESCRIPTOR
*PpiList
,
168 PEI_CORE_INSTANCE PrivateData
;
169 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
170 EFI_SEC_PEI_HAND_OFF NewSecCoreData
;
172 PEI_CORE_TEMP_POINTERS TempPtr
;
173 PEI_CORE_INSTANCE
*OldCoreData
;
174 EFI_PEI_CPU_IO_PPI
*CpuIo
;
175 EFI_PEI_PCI_CFG2_PPI
*PciCfg
;
176 EFI_HOB_HANDOFF_INFO_TABLE
*HandoffInformationTable
;
177 EFI_PEI_TEMPORARY_RAM_DONE_PPI
*TemporaryRamDonePpi
;
181 // Retrieve context passed into PEI Core
183 OldCoreData
= (PEI_CORE_INSTANCE
*) Data
;
184 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*) SecCoreDataPtr
;
187 // Perform PEI Core phase specific actions.
189 if (OldCoreData
== NULL
) {
191 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
193 ZeroMem (&PrivateData
, sizeof (PEI_CORE_INSTANCE
));
194 PrivateData
.Signature
= PEI_CORE_HANDLE_SIGNATURE
;
195 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
198 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
200 if (OldCoreData
->ShadowedPeiCore
== NULL
) {
202 // Fixup the PeiCore's private data
204 OldCoreData
->Ps
= &OldCoreData
->ServiceTableShadow
;
205 OldCoreData
->CpuIo
= &OldCoreData
->ServiceTableShadow
.CpuIo
;
206 if (OldCoreData
->HeapOffsetPositive
) {
207 OldCoreData
->HobList
.Raw
= (VOID
*)(OldCoreData
->HobList
.Raw
+ OldCoreData
->HeapOffset
);
208 if (OldCoreData
->UnknownFvInfo
!= NULL
) {
209 OldCoreData
->UnknownFvInfo
= (PEI_CORE_UNKNOW_FORMAT_FV_INFO
*) ((UINT8
*) OldCoreData
->UnknownFvInfo
+ OldCoreData
->HeapOffset
);
211 if (OldCoreData
->CurrentFvFileHandles
!= NULL
) {
212 OldCoreData
->CurrentFvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->CurrentFvFileHandles
+ OldCoreData
->HeapOffset
);
214 if (OldCoreData
->PpiData
.PpiList
.PpiPtrs
!= NULL
) {
215 OldCoreData
->PpiData
.PpiList
.PpiPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.PpiList
.PpiPtrs
+ OldCoreData
->HeapOffset
);
217 if (OldCoreData
->PpiData
.CallbackNotifyList
.NotifyPtrs
!= NULL
) {
218 OldCoreData
->PpiData
.CallbackNotifyList
.NotifyPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.CallbackNotifyList
.NotifyPtrs
+ OldCoreData
->HeapOffset
);
220 if (OldCoreData
->PpiData
.DispatchNotifyList
.NotifyPtrs
!= NULL
) {
221 OldCoreData
->PpiData
.DispatchNotifyList
.NotifyPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.DispatchNotifyList
.NotifyPtrs
+ OldCoreData
->HeapOffset
);
223 OldCoreData
->Fv
= (PEI_CORE_FV_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
+ OldCoreData
->HeapOffset
);
224 for (Index
= 0; Index
< OldCoreData
->FvCount
; Index
++) {
225 if (OldCoreData
->Fv
[Index
].PeimState
!= NULL
) {
226 OldCoreData
->Fv
[Index
].PeimState
= (UINT8
*) OldCoreData
->Fv
[Index
].PeimState
+ OldCoreData
->HeapOffset
;
228 if (OldCoreData
->Fv
[Index
].FvFileHandles
!= NULL
) {
229 OldCoreData
->Fv
[Index
].FvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
[Index
].FvFileHandles
+ OldCoreData
->HeapOffset
);
232 OldCoreData
->TempFileGuid
= (EFI_GUID
*) ((UINT8
*) OldCoreData
->TempFileGuid
+ OldCoreData
->HeapOffset
);
233 OldCoreData
->TempFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->TempFileHandles
+ OldCoreData
->HeapOffset
);
235 OldCoreData
->HobList
.Raw
= (VOID
*)(OldCoreData
->HobList
.Raw
- OldCoreData
->HeapOffset
);
236 if (OldCoreData
->UnknownFvInfo
!= NULL
) {
237 OldCoreData
->UnknownFvInfo
= (PEI_CORE_UNKNOW_FORMAT_FV_INFO
*) ((UINT8
*) OldCoreData
->UnknownFvInfo
- OldCoreData
->HeapOffset
);
239 if (OldCoreData
->CurrentFvFileHandles
!= NULL
) {
240 OldCoreData
->CurrentFvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->CurrentFvFileHandles
- OldCoreData
->HeapOffset
);
242 if (OldCoreData
->PpiData
.PpiList
.PpiPtrs
!= NULL
) {
243 OldCoreData
->PpiData
.PpiList
.PpiPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.PpiList
.PpiPtrs
- OldCoreData
->HeapOffset
);
245 if (OldCoreData
->PpiData
.CallbackNotifyList
.NotifyPtrs
!= NULL
) {
246 OldCoreData
->PpiData
.CallbackNotifyList
.NotifyPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.CallbackNotifyList
.NotifyPtrs
- OldCoreData
->HeapOffset
);
248 if (OldCoreData
->PpiData
.DispatchNotifyList
.NotifyPtrs
!= NULL
) {
249 OldCoreData
->PpiData
.DispatchNotifyList
.NotifyPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.DispatchNotifyList
.NotifyPtrs
- OldCoreData
->HeapOffset
);
251 OldCoreData
->Fv
= (PEI_CORE_FV_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
- OldCoreData
->HeapOffset
);
252 for (Index
= 0; Index
< OldCoreData
->FvCount
; Index
++) {
253 if (OldCoreData
->Fv
[Index
].PeimState
!= NULL
) {
254 OldCoreData
->Fv
[Index
].PeimState
= (UINT8
*) OldCoreData
->Fv
[Index
].PeimState
- OldCoreData
->HeapOffset
;
256 if (OldCoreData
->Fv
[Index
].FvFileHandles
!= NULL
) {
257 OldCoreData
->Fv
[Index
].FvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
[Index
].FvFileHandles
- OldCoreData
->HeapOffset
);
260 OldCoreData
->TempFileGuid
= (EFI_GUID
*) ((UINT8
*) OldCoreData
->TempFileGuid
- OldCoreData
->HeapOffset
);
261 OldCoreData
->TempFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->TempFileHandles
- OldCoreData
->HeapOffset
);
265 // Fixup for PeiService's address
267 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&OldCoreData
->Ps
);
270 // Initialize libraries that the PEI Core is linked against
272 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&OldCoreData
->Ps
);
275 // Update HandOffHob for new installed permanent memory
277 HandoffInformationTable
= OldCoreData
->HobList
.HandoffInformationTable
;
278 if (OldCoreData
->HeapOffsetPositive
) {
279 HandoffInformationTable
->EfiEndOfHobList
= HandoffInformationTable
->EfiEndOfHobList
+ OldCoreData
->HeapOffset
;
281 HandoffInformationTable
->EfiEndOfHobList
= HandoffInformationTable
->EfiEndOfHobList
- OldCoreData
->HeapOffset
;
283 HandoffInformationTable
->EfiMemoryTop
= OldCoreData
->PhysicalMemoryBegin
+ OldCoreData
->PhysicalMemoryLength
;
284 HandoffInformationTable
->EfiMemoryBottom
= OldCoreData
->PhysicalMemoryBegin
;
285 HandoffInformationTable
->EfiFreeMemoryTop
= OldCoreData
->FreePhysicalMemoryTop
;
286 HandoffInformationTable
->EfiFreeMemoryBottom
= HandoffInformationTable
->EfiEndOfHobList
+ sizeof (EFI_HOB_GENERIC_HEADER
);
289 // We need convert MemoryBaseAddress in memory allocation HOBs
291 ConvertMemoryAllocationHobs (OldCoreData
);
294 // We need convert the PPI descriptor's pointer
296 ConvertPpiPointers (SecCoreData
, OldCoreData
);
299 // After the whole temporary memory is migrated, then we can allocate page in
302 OldCoreData
->PeiMemoryInstalled
= TRUE
;
305 // Indicate that PeiCore reenter
307 OldCoreData
->PeimDispatcherReenter
= TRUE
;
309 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (OldCoreData
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
311 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
312 // Every bit in the array indicate the status of the corresponding memory page available or not
314 OldCoreData
->PeiCodeMemoryRangeUsageBitMap
= AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber
)>>6) + 1)*sizeof(UINT64
));
318 // Shadow PEI Core. When permanent memory is available, shadow
319 // PEI Core and PEIMs to get high performance.
321 OldCoreData
->ShadowedPeiCore
= (PEICORE_FUNCTION_POINTER
) (UINTN
) PeiCore
;
322 if (PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes
) ||
323 (HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
&& PcdGetBool (PcdShadowPeimOnS3Boot
)) ||
324 (HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
&& PcdGetBool (PcdShadowPeimOnBoot
))) {
325 OldCoreData
->ShadowedPeiCore
= ShadowPeiCore (OldCoreData
);
329 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
331 OldCoreData
->ShadowedPeiCore (SecCoreData
, PpiList
, OldCoreData
);
334 // Should never reach here.
343 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
345 CopyMem (&NewSecCoreData
, SecCoreDataPtr
, sizeof (NewSecCoreData
));
346 SecCoreData
= &NewSecCoreData
;
348 CopyMem (&PrivateData
, OldCoreData
, sizeof (PrivateData
));
350 CpuIo
= (VOID
*)PrivateData
.ServiceTableShadow
.CpuIo
;
351 PciCfg
= (VOID
*)PrivateData
.ServiceTableShadow
.PciCfg
;
353 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
355 PrivateData
.ServiceTableShadow
.CpuIo
= CpuIo
;
356 PrivateData
.ServiceTableShadow
.PciCfg
= PciCfg
;
360 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
362 PrivateData
.Ps
= &PrivateData
.ServiceTableShadow
;
365 // Save PeiServicePointer so that it can be retrieved anywhere.
367 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
370 // Initialize libraries that the PEI Core is linked against
372 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
375 // Initialize PEI Core Services
377 InitializeMemoryServices (&PrivateData
, SecCoreData
, OldCoreData
);
380 // Update performance measurements
382 if (OldCoreData
== NULL
) {
383 PERF_EVENT ("SEC"); // Means the end of SEC phase.
386 // If first pass, start performance measurement.
388 PERF_CROSSMODULE_BEGIN ("PEI");
389 PERF_INMODULE_BEGIN ("PreMem");
392 PERF_INMODULE_END ("PreMem");
393 PERF_INMODULE_BEGIN ("PostMem");
397 // Complete PEI Core Service initialization
399 InitializeSecurityServices (&PrivateData
.Ps
, OldCoreData
);
400 InitializeDispatcherData (&PrivateData
, OldCoreData
, SecCoreData
);
401 InitializeImageServices (&PrivateData
, OldCoreData
);
404 // Perform PEI Core Phase specific actions
406 if (OldCoreData
== NULL
) {
408 // Report Status Code EFI_SW_PC_INIT
412 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT
)
416 // If SEC provided the PpiList, process it.
418 if (PpiList
!= NULL
) {
419 ProcessPpiListFromSec ((CONST EFI_PEI_SERVICES
**) &PrivateData
.Ps
, PpiList
);
422 if (PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes
)) {
424 // When PcdMigrateTemporaryRamFirmwareVolumes is TRUE, alway shadow all
425 // PEIMs no matter the condition of PcdShadowPeimOnBoot and PcdShadowPeimOnS3Boot
427 DEBUG ((DEBUG_VERBOSE
, "PPI lists before temporary RAM evacuation:\n"));
428 DumpPpiList (&PrivateData
);
431 // Migrate installed content from Temporary RAM to Permanent RAM
433 EvacuateTempRam (&PrivateData
, SecCoreData
);
435 DEBUG ((DEBUG_VERBOSE
, "PPI lists after temporary RAM evacuation:\n"));
436 DumpPpiList (&PrivateData
);
440 // Try to locate Temporary RAM Done Ppi.
442 Status
= PeiServicesLocatePpi (
443 &gEfiTemporaryRamDonePpiGuid
,
446 (VOID
**)&TemporaryRamDonePpi
448 if (!EFI_ERROR (Status
)) {
450 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.
452 TemporaryRamDonePpi
->TemporaryRamDone ();
456 // Alert any listeners that there is permanent memory available
458 PERF_INMODULE_BEGIN ("DisMem");
459 Status
= PeiServicesInstallPpi (&mMemoryDiscoveredPpi
);
462 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
464 ProcessDispatchNotifyList (&PrivateData
);
466 PERF_INMODULE_END ("DisMem");
470 // Call PEIM dispatcher
472 PeiDispatcher (SecCoreData
, &PrivateData
);
474 if (PrivateData
.HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
) {
476 // Check if InstallPeiMemory service was called on non-S3 resume boot path.
478 ASSERT(PrivateData
.PeiMemoryInstalled
== TRUE
);
482 // Measure PEI Core execution time.
484 PERF_INMODULE_END ("PostMem");
487 // Lookup DXE IPL PPI
489 Status
= PeiServicesLocatePpi (
493 (VOID
**)&TempPtr
.DxeIpl
495 ASSERT_EFI_ERROR (Status
);
497 if (EFI_ERROR (Status
)) {
499 // Report status code to indicate DXE IPL PPI could not be found.
502 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
503 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_EC_DXEIPL_NOT_FOUND
)
509 // Enter DxeIpl to load Dxe core.
511 DEBUG ((EFI_D_INFO
, "DXE IPL Entry\n"));
512 Status
= TempPtr
.DxeIpl
->Entry (
518 // Should never reach here.
520 ASSERT_EFI_ERROR (Status
);