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 shaowed 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 avaiable, shadow
319 // PEI Core and PEIMs to get high performance.
321 OldCoreData
->ShadowedPeiCore
= (PEICORE_FUNCTION_POINTER
) (UINTN
) PeiCore
;
322 if ((HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
&& PcdGetBool (PcdShadowPeimOnS3Boot
))
323 || (HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
&& PcdGetBool (PcdShadowPeimOnBoot
))) {
324 OldCoreData
->ShadowedPeiCore
= ShadowPeiCore (OldCoreData
);
328 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
330 OldCoreData
->ShadowedPeiCore (SecCoreData
, PpiList
, OldCoreData
);
333 // Should never reach here.
342 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
344 CopyMem (&NewSecCoreData
, SecCoreDataPtr
, sizeof (NewSecCoreData
));
345 SecCoreData
= &NewSecCoreData
;
347 CopyMem (&PrivateData
, OldCoreData
, sizeof (PrivateData
));
349 CpuIo
= (VOID
*)PrivateData
.ServiceTableShadow
.CpuIo
;
350 PciCfg
= (VOID
*)PrivateData
.ServiceTableShadow
.PciCfg
;
352 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
354 PrivateData
.ServiceTableShadow
.CpuIo
= CpuIo
;
355 PrivateData
.ServiceTableShadow
.PciCfg
= PciCfg
;
359 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
361 PrivateData
.Ps
= &PrivateData
.ServiceTableShadow
;
364 // Save PeiServicePointer so that it can be retrieved anywhere.
366 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
369 // Initialize libraries that the PEI Core is linked against
371 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
374 // Initialize PEI Core Services
376 InitializeMemoryServices (&PrivateData
, SecCoreData
, OldCoreData
);
379 // Update performance measurements
381 if (OldCoreData
== NULL
) {
382 PERF_EVENT ("SEC"); // Means the end of SEC phase.
385 // If first pass, start performance measurement.
387 PERF_CROSSMODULE_BEGIN ("PEI");
388 PERF_INMODULE_BEGIN ("PreMem");
391 PERF_INMODULE_END ("PreMem");
392 PERF_INMODULE_BEGIN ("PostMem");
396 // Complete PEI Core Service initialization
398 InitializeSecurityServices (&PrivateData
.Ps
, OldCoreData
);
399 InitializeDispatcherData (&PrivateData
, OldCoreData
, SecCoreData
);
400 InitializeImageServices (&PrivateData
, OldCoreData
);
403 // Perform PEI Core Phase specific actions
405 if (OldCoreData
== NULL
) {
407 // Report Status Code EFI_SW_PC_INIT
411 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT
)
415 // If SEC provided the PpiList, process it.
417 if (PpiList
!= NULL
) {
418 ProcessPpiListFromSec ((CONST EFI_PEI_SERVICES
**) &PrivateData
.Ps
, PpiList
);
422 // Try to locate Temporary RAM Done Ppi.
424 Status
= PeiServicesLocatePpi (
425 &gEfiTemporaryRamDonePpiGuid
,
428 (VOID
**)&TemporaryRamDonePpi
430 if (!EFI_ERROR (Status
)) {
432 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.
434 TemporaryRamDonePpi
->TemporaryRamDone ();
438 // Alert any listeners that there is permanent memory available
440 PERF_INMODULE_BEGIN ("DisMem");
441 Status
= PeiServicesInstallPpi (&mMemoryDiscoveredPpi
);
444 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
446 ProcessDispatchNotifyList (&PrivateData
);
448 PERF_INMODULE_END ("DisMem");
452 // Call PEIM dispatcher
454 PeiDispatcher (SecCoreData
, &PrivateData
);
456 if (PrivateData
.HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
) {
458 // Check if InstallPeiMemory service was called on non-S3 resume boot path.
460 ASSERT(PrivateData
.PeiMemoryInstalled
== TRUE
);
464 // Measure PEI Core execution time.
466 PERF_INMODULE_END ("PostMem");
469 // Lookup DXE IPL PPI
471 Status
= PeiServicesLocatePpi (
475 (VOID
**)&TempPtr
.DxeIpl
477 ASSERT_EFI_ERROR (Status
);
479 if (EFI_ERROR (Status
)) {
481 // Report status code to indicate DXE IPL PPI could not be found.
484 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
485 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_EC_DXEIPL_NOT_FOUND
)
491 // Enter DxeIpl to load Dxe core.
493 DEBUG ((EFI_D_INFO
, "DXE IPL Entry\n"));
494 Status
= TempPtr
.DxeIpl
->Entry (
500 // Should never reach here.
502 ASSERT_EFI_ERROR (Status
);