2 Pei Core Main Entry Point
4 Copyright (c) 2006 - 2017, 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
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.
17 EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi
= {
18 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
19 &gEfiPeiMemoryDiscoveredPpiGuid
,
24 /// Pei service instance
26 EFI_PEI_SERVICES gPs
= {
28 PEI_SERVICES_SIGNATURE
,
29 PEI_SERVICES_REVISION
,
30 sizeof (EFI_PEI_SERVICES
),
47 PeiFfsFindSectionData
,
52 (EFI_PEI_COPY_MEM
)CopyMem
,
53 (EFI_PEI_SET_MEM
)SetMem
,
59 &gPeiDefaultPciCfg2Ppi
,
65 PeiFfsFindSectionData3
,
72 Shadow PeiCore module from flash to installed memory.
74 @param PrivateData PeiCore's private data structure
76 @return PeiCore function address after shadowing.
78 PEICORE_FUNCTION_POINTER
80 IN PEI_CORE_INSTANCE
*PrivateData
83 EFI_PEI_FILE_HANDLE PeiCoreFileHandle
;
84 EFI_PHYSICAL_ADDRESS EntryPoint
;
86 UINT32 AuthenticationState
;
88 PeiCoreFileHandle
= NULL
;
91 // Find the PEI Core in the BFV
93 Status
= PrivateData
->Fv
[0].FvPpi
->FindFileByType (
94 PrivateData
->Fv
[0].FvPpi
,
95 EFI_FV_FILETYPE_PEI_CORE
,
96 PrivateData
->Fv
[0].FvHandle
,
99 ASSERT_EFI_ERROR (Status
);
102 // Shadow PEI Core into memory so it will run faster
104 Status
= PeiLoadImage (
105 GetPeiServicesTablePointer (),
106 *((EFI_PEI_FILE_HANDLE
*)&PeiCoreFileHandle
),
107 PEIM_STATE_REGISITER_FOR_SHADOW
,
111 ASSERT_EFI_ERROR (Status
);
114 // Compute the PeiCore's function address after shaowed PeiCore.
115 // _ModuleEntryPoint is PeiCore main function entry
117 return (PEICORE_FUNCTION_POINTER
)((UINTN
) EntryPoint
+ (UINTN
) PeiCore
- (UINTN
) _ModuleEntryPoint
);
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.
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
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
136 If NULL, it is first PeiCore entering.
142 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreDataPtr
,
143 IN CONST EFI_PEI_PPI_DESCRIPTOR
*PpiList
,
147 PEI_CORE_INSTANCE PrivateData
;
148 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
149 EFI_SEC_PEI_HAND_OFF NewSecCoreData
;
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
;
160 // Retrieve context passed into PEI Core
162 OldCoreData
= (PEI_CORE_INSTANCE
*) Data
;
163 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*) SecCoreDataPtr
;
166 // Perform PEI Core phase specific actions.
168 if (OldCoreData
== NULL
) {
170 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
172 ZeroMem (&PrivateData
, sizeof (PEI_CORE_INSTANCE
));
173 PrivateData
.Signature
= PEI_CORE_HANDLE_SIGNATURE
;
174 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
177 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
179 if (OldCoreData
->ShadowedPeiCore
== NULL
) {
181 // Fixup the PeiCore's private data
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 OldCoreData
->UnknownFvInfo
= (PEI_CORE_UNKNOW_FORMAT_FV_INFO
*) ((UINT8
*) OldCoreData
->UnknownFvInfo
+ OldCoreData
->HeapOffset
);
188 OldCoreData
->CurrentFvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->CurrentFvFileHandles
+ OldCoreData
->HeapOffset
);
189 OldCoreData
->PpiData
.PpiListPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.PpiListPtrs
+ OldCoreData
->HeapOffset
);
190 OldCoreData
->Fv
= (PEI_CORE_FV_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
+ OldCoreData
->HeapOffset
);
191 for (Index
= 0; Index
< PcdGet32 (PcdPeiCoreMaxFvSupported
); Index
++) {
192 OldCoreData
->Fv
[Index
].PeimState
= (UINT8
*) OldCoreData
->Fv
[Index
].PeimState
+ OldCoreData
->HeapOffset
;
193 OldCoreData
->Fv
[Index
].FvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
[Index
].FvFileHandles
+ OldCoreData
->HeapOffset
);
195 OldCoreData
->FileGuid
= (EFI_GUID
*) ((UINT8
*) OldCoreData
->FileGuid
+ OldCoreData
->HeapOffset
);
196 OldCoreData
->FileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->FileHandles
+ OldCoreData
->HeapOffset
);
198 OldCoreData
->HobList
.Raw
= (VOID
*)(OldCoreData
->HobList
.Raw
- OldCoreData
->HeapOffset
);
199 OldCoreData
->UnknownFvInfo
= (PEI_CORE_UNKNOW_FORMAT_FV_INFO
*) ((UINT8
*) OldCoreData
->UnknownFvInfo
- OldCoreData
->HeapOffset
);
200 OldCoreData
->CurrentFvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->CurrentFvFileHandles
- OldCoreData
->HeapOffset
);
201 OldCoreData
->PpiData
.PpiListPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.PpiListPtrs
- OldCoreData
->HeapOffset
);
202 OldCoreData
->Fv
= (PEI_CORE_FV_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
- OldCoreData
->HeapOffset
);
203 for (Index
= 0; Index
< PcdGet32 (PcdPeiCoreMaxFvSupported
); Index
++) {
204 OldCoreData
->Fv
[Index
].PeimState
= (UINT8
*) OldCoreData
->Fv
[Index
].PeimState
- OldCoreData
->HeapOffset
;
205 OldCoreData
->Fv
[Index
].FvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
[Index
].FvFileHandles
- OldCoreData
->HeapOffset
);
207 OldCoreData
->FileGuid
= (EFI_GUID
*) ((UINT8
*) OldCoreData
->FileGuid
- OldCoreData
->HeapOffset
);
208 OldCoreData
->FileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->FileHandles
- OldCoreData
->HeapOffset
);
212 // Fixup for PeiService's address
214 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&OldCoreData
->Ps
);
217 // Initialize libraries that the PEI Core is linked against
219 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&OldCoreData
->Ps
);
222 // Update HandOffHob for new installed permanent memory
224 HandoffInformationTable
= OldCoreData
->HobList
.HandoffInformationTable
;
225 if (OldCoreData
->HeapOffsetPositive
) {
226 HandoffInformationTable
->EfiEndOfHobList
= HandoffInformationTable
->EfiEndOfHobList
+ OldCoreData
->HeapOffset
;
228 HandoffInformationTable
->EfiEndOfHobList
= HandoffInformationTable
->EfiEndOfHobList
- OldCoreData
->HeapOffset
;
230 HandoffInformationTable
->EfiMemoryTop
= OldCoreData
->PhysicalMemoryBegin
+ OldCoreData
->PhysicalMemoryLength
;
231 HandoffInformationTable
->EfiMemoryBottom
= OldCoreData
->PhysicalMemoryBegin
;
232 HandoffInformationTable
->EfiFreeMemoryTop
= OldCoreData
->FreePhysicalMemoryTop
;
233 HandoffInformationTable
->EfiFreeMemoryBottom
= HandoffInformationTable
->EfiEndOfHobList
+ sizeof (EFI_HOB_GENERIC_HEADER
);
236 // We need convert MemoryBaseAddress in memory allocation HOBs
238 ConvertMemoryAllocationHobs (OldCoreData
);
241 // We need convert the PPI descriptor's pointer
243 ConvertPpiPointers (SecCoreData
, OldCoreData
);
246 // After the whole temporary memory is migrated, then we can allocate page in
249 OldCoreData
->PeiMemoryInstalled
= TRUE
;
252 // Indicate that PeiCore reenter
254 OldCoreData
->PeimDispatcherReenter
= TRUE
;
256 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (OldCoreData
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
258 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
259 // Every bit in the array indicate the status of the corresponding memory page available or not
261 OldCoreData
->PeiCodeMemoryRangeUsageBitMap
= AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber
)>>6) + 1)*sizeof(UINT64
));
265 // Shadow PEI Core. When permanent memory is avaiable, shadow
266 // PEI Core and PEIMs to get high performance.
268 OldCoreData
->ShadowedPeiCore
= (PEICORE_FUNCTION_POINTER
) (UINTN
) PeiCore
;
269 if ((HandoffInformationTable
->BootMode
== BOOT_ON_S3_RESUME
&& PcdGetBool (PcdShadowPeimOnS3Boot
))
270 || (HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
&& PcdGetBool (PcdShadowPeimOnBoot
))) {
271 OldCoreData
->ShadowedPeiCore
= ShadowPeiCore (OldCoreData
);
275 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
277 OldCoreData
->ShadowedPeiCore (SecCoreData
, PpiList
, OldCoreData
);
280 // Should never reach here.
289 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
291 CopyMem (&NewSecCoreData
, SecCoreDataPtr
, sizeof (NewSecCoreData
));
292 SecCoreData
= &NewSecCoreData
;
294 CopyMem (&PrivateData
, OldCoreData
, sizeof (PrivateData
));
296 CpuIo
= (VOID
*)PrivateData
.ServiceTableShadow
.CpuIo
;
297 PciCfg
= (VOID
*)PrivateData
.ServiceTableShadow
.PciCfg
;
299 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
301 PrivateData
.ServiceTableShadow
.CpuIo
= CpuIo
;
302 PrivateData
.ServiceTableShadow
.PciCfg
= PciCfg
;
306 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
308 PrivateData
.Ps
= &PrivateData
.ServiceTableShadow
;
311 // Save PeiServicePointer so that it can be retrieved anywhere.
313 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
316 // Initialize libraries that the PEI Core is linked against
318 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
321 // Initialize PEI Core Services
323 InitializeMemoryServices (&PrivateData
, SecCoreData
, OldCoreData
);
324 if (OldCoreData
== NULL
) {
326 // Initialize PEI Core Private Data Buffer
328 PrivateData
.PpiData
.PpiListPtrs
= AllocateZeroPool (sizeof (PEI_PPI_LIST_POINTERS
) * PcdGet32 (PcdPeiCoreMaxPpiSupported
));
329 ASSERT (PrivateData
.PpiData
.PpiListPtrs
!= NULL
);
330 PrivateData
.Fv
= AllocateZeroPool (sizeof (PEI_CORE_FV_HANDLE
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
331 ASSERT (PrivateData
.Fv
!= NULL
);
332 PrivateData
.Fv
[0].PeimState
= AllocateZeroPool (sizeof (UINT8
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
333 ASSERT (PrivateData
.Fv
[0].PeimState
!= NULL
);
334 PrivateData
.Fv
[0].FvFileHandles
= AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
335 ASSERT (PrivateData
.Fv
[0].FvFileHandles
!= NULL
);
336 for (Index
= 1; Index
< PcdGet32 (PcdPeiCoreMaxFvSupported
); Index
++) {
337 PrivateData
.Fv
[Index
].PeimState
= PrivateData
.Fv
[Index
- 1].PeimState
+ PcdGet32 (PcdPeiCoreMaxPeimPerFv
);
338 PrivateData
.Fv
[Index
].FvFileHandles
= PrivateData
.Fv
[Index
- 1].FvFileHandles
+ PcdGet32 (PcdPeiCoreMaxPeimPerFv
);
340 PrivateData
.UnknownFvInfo
= AllocateZeroPool (sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
341 ASSERT (PrivateData
.UnknownFvInfo
!= NULL
);
342 PrivateData
.CurrentFvFileHandles
= AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
343 ASSERT (PrivateData
.CurrentFvFileHandles
!= NULL
);
344 PrivateData
.FileGuid
= AllocatePool (sizeof (EFI_GUID
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
345 ASSERT (PrivateData
.FileGuid
!= NULL
);
346 PrivateData
.FileHandles
= AllocatePool (sizeof (EFI_PEI_FILE_HANDLE
) * (PcdGet32 (PcdPeiCoreMaxPeimPerFv
) + 1));
347 ASSERT (PrivateData
.FileHandles
!= NULL
);
349 InitializePpiServices (&PrivateData
, OldCoreData
);
352 // Update performance measurements
354 if (OldCoreData
== NULL
) {
355 PERF_START (NULL
, "SEC", NULL
, 1);
356 PERF_END (NULL
, "SEC", NULL
, 0);
359 // If first pass, start performance measurement.
361 PERF_START (NULL
,"PEI", NULL
, 0);
362 PERF_START (NULL
,"PreMem", NULL
, 0);
365 PERF_END (NULL
,"PreMem", NULL
, 0);
366 PERF_START (NULL
,"PostMem", NULL
, 0);
370 // Complete PEI Core Service initialization
372 InitializeSecurityServices (&PrivateData
.Ps
, OldCoreData
);
373 InitializeDispatcherData (&PrivateData
, OldCoreData
, SecCoreData
);
374 InitializeImageServices (&PrivateData
, OldCoreData
);
377 // Perform PEI Core Phase specific actions
379 if (OldCoreData
== NULL
) {
381 // Report Status Code EFI_SW_PC_INIT
385 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT
)
389 // If SEC provided the PpiList, process it.
391 if (PpiList
!= NULL
) {
392 ProcessPpiListFromSec ((CONST EFI_PEI_SERVICES
**) &PrivateData
.Ps
, PpiList
);
396 // Try to locate Temporary RAM Done Ppi.
398 Status
= PeiServicesLocatePpi (
399 &gEfiTemporaryRamDonePpiGuid
,
402 (VOID
**)&TemporaryRamDonePpi
404 if (!EFI_ERROR (Status
)) {
406 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.
408 TemporaryRamDonePpi
->TemporaryRamDone ();
412 // Alert any listeners that there is permanent memory available
414 PERF_START (NULL
,"DisMem", NULL
, 0);
415 Status
= PeiServicesInstallPpi (&mMemoryDiscoveredPpi
);
418 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
420 ProcessNotifyList (&PrivateData
);
422 PERF_END (NULL
,"DisMem", NULL
, 0);
426 // Call PEIM dispatcher
428 PeiDispatcher (SecCoreData
, &PrivateData
);
430 if (PrivateData
.HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
) {
432 // Check if InstallPeiMemory service was called on non-S3 resume boot path.
434 ASSERT(PrivateData
.PeiMemoryInstalled
== TRUE
);
438 // Measure PEI Core execution time.
440 PERF_END (NULL
, "PostMem", NULL
, 0);
443 // Lookup DXE IPL PPI
445 Status
= PeiServicesLocatePpi (
449 (VOID
**)&TempPtr
.DxeIpl
451 ASSERT_EFI_ERROR (Status
);
453 if (EFI_ERROR (Status
)) {
455 // Report status code to indicate DXE IPL PPI could not be found.
458 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
459 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_EC_DXEIPL_NOT_FOUND
)
465 // Enter DxeIpl to load Dxe core.
467 DEBUG ((EFI_D_INFO
, "DXE IPL Entry\n"));
468 Status
= TempPtr
.DxeIpl
->Entry (
474 // Should never reach here.
476 ASSERT_EFI_ERROR (Status
);