2 Pei Core Main Entry Point
4 Copyright (c) 2006 - 2015, 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
,
71 Shadow PeiCore module from flash to installed memory.
73 @param PrivateData PeiCore's private data structure
75 @return PeiCore function address after shadowing.
77 PEICORE_FUNCTION_POINTER
79 IN PEI_CORE_INSTANCE
*PrivateData
82 EFI_PEI_FILE_HANDLE PeiCoreFileHandle
;
83 EFI_PHYSICAL_ADDRESS EntryPoint
;
85 UINT32 AuthenticationState
;
87 PeiCoreFileHandle
= NULL
;
90 // Find the PEI Core in the BFV
92 Status
= PrivateData
->Fv
[0].FvPpi
->FindFileByType (
93 PrivateData
->Fv
[0].FvPpi
,
94 EFI_FV_FILETYPE_PEI_CORE
,
95 PrivateData
->Fv
[0].FvHandle
,
98 ASSERT_EFI_ERROR (Status
);
101 // Shadow PEI Core into memory so it will run faster
103 Status
= PeiLoadImage (
104 GetPeiServicesTablePointer (),
105 *((EFI_PEI_FILE_HANDLE
*)&PeiCoreFileHandle
),
106 PEIM_STATE_REGISITER_FOR_SHADOW
,
110 ASSERT_EFI_ERROR (Status
);
113 // Compute the PeiCore's function address after shaowed PeiCore.
114 // _ModuleEntryPoint is PeiCore main function entry
116 return (PEICORE_FUNCTION_POINTER
)((UINTN
) EntryPoint
+ (UINTN
) PeiCore
- (UINTN
) _ModuleEntryPoint
);
120 This routine is invoked by main entry of PeiMain module during transition
121 from SEC to PEI. After switching stack in the PEI core, it will restart
122 with the old core data.
124 @param SecCoreDataPtr Points to a data structure containing information about the PEI core's operating
125 environment, such as the size and location of temporary RAM, the stack location and
127 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.
128 An empty PPI list consists of a single descriptor with the end-tag
129 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST. As part of its initialization
130 phase, the PEI Foundation will add these SEC-hosted PPIs to its PPI database such
131 that both the PEI Foundation and any modules can leverage the associated service
132 calls and/or code in these early PPIs
133 @param Data Pointer to old core data that is used to initialize the
135 If NULL, it is first PeiCore entering.
141 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreDataPtr
,
142 IN CONST EFI_PEI_PPI_DESCRIPTOR
*PpiList
,
146 PEI_CORE_INSTANCE PrivateData
;
147 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
148 EFI_SEC_PEI_HAND_OFF NewSecCoreData
;
150 PEI_CORE_TEMP_POINTERS TempPtr
;
151 PEI_CORE_INSTANCE
*OldCoreData
;
152 EFI_PEI_CPU_IO_PPI
*CpuIo
;
153 EFI_PEI_PCI_CFG2_PPI
*PciCfg
;
154 EFI_HOB_HANDOFF_INFO_TABLE
*HandoffInformationTable
;
155 EFI_PEI_TEMPORARY_RAM_DONE_PPI
*TemporaryRamDonePpi
;
159 // Retrieve context passed into PEI Core
161 OldCoreData
= (PEI_CORE_INSTANCE
*) Data
;
162 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*) SecCoreDataPtr
;
165 // Perform PEI Core phase specific actions.
167 if (OldCoreData
== NULL
) {
169 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
171 ZeroMem (&PrivateData
, sizeof (PEI_CORE_INSTANCE
));
172 PrivateData
.Signature
= PEI_CORE_HANDLE_SIGNATURE
;
173 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
176 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
178 if (OldCoreData
->ShadowedPeiCore
== NULL
) {
180 // Fixup the PeiCore's private data
182 OldCoreData
->Ps
= &OldCoreData
->ServiceTableShadow
;
183 OldCoreData
->CpuIo
= &OldCoreData
->ServiceTableShadow
.CpuIo
;
184 if (OldCoreData
->HeapOffsetPositive
) {
185 OldCoreData
->HobList
.Raw
= (VOID
*)(OldCoreData
->HobList
.Raw
+ OldCoreData
->HeapOffset
);
186 OldCoreData
->UnknownFvInfo
= (PEI_CORE_UNKNOW_FORMAT_FV_INFO
*) ((UINT8
*) OldCoreData
->UnknownFvInfo
+ OldCoreData
->HeapOffset
);
187 OldCoreData
->CurrentFvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->CurrentFvFileHandles
+ OldCoreData
->HeapOffset
);
188 OldCoreData
->PpiData
.PpiListPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.PpiListPtrs
+ OldCoreData
->HeapOffset
);
189 OldCoreData
->Fv
= (PEI_CORE_FV_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
+ OldCoreData
->HeapOffset
);
190 for (Index
= 0; Index
< PcdGet32 (PcdPeiCoreMaxFvSupported
); Index
++) {
191 OldCoreData
->Fv
[Index
].PeimState
= (UINT8
*) OldCoreData
->Fv
[Index
].PeimState
+ OldCoreData
->HeapOffset
;
192 OldCoreData
->Fv
[Index
].FvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
[Index
].FvFileHandles
+ OldCoreData
->HeapOffset
);
194 OldCoreData
->FileGuid
= (EFI_GUID
*) ((UINT8
*) OldCoreData
->FileGuid
+ OldCoreData
->HeapOffset
);
195 OldCoreData
->FileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->FileHandles
+ OldCoreData
->HeapOffset
);
197 OldCoreData
->HobList
.Raw
= (VOID
*)(OldCoreData
->HobList
.Raw
- OldCoreData
->HeapOffset
);
198 OldCoreData
->UnknownFvInfo
= (PEI_CORE_UNKNOW_FORMAT_FV_INFO
*) ((UINT8
*) OldCoreData
->UnknownFvInfo
- OldCoreData
->HeapOffset
);
199 OldCoreData
->CurrentFvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->CurrentFvFileHandles
- OldCoreData
->HeapOffset
);
200 OldCoreData
->PpiData
.PpiListPtrs
= (PEI_PPI_LIST_POINTERS
*) ((UINT8
*) OldCoreData
->PpiData
.PpiListPtrs
- OldCoreData
->HeapOffset
);
201 OldCoreData
->Fv
= (PEI_CORE_FV_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
- OldCoreData
->HeapOffset
);
202 for (Index
= 0; Index
< PcdGet32 (PcdPeiCoreMaxFvSupported
); Index
++) {
203 OldCoreData
->Fv
[Index
].PeimState
= (UINT8
*) OldCoreData
->Fv
[Index
].PeimState
- OldCoreData
->HeapOffset
;
204 OldCoreData
->Fv
[Index
].FvFileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->Fv
[Index
].FvFileHandles
- OldCoreData
->HeapOffset
);
206 OldCoreData
->FileGuid
= (EFI_GUID
*) ((UINT8
*) OldCoreData
->FileGuid
- OldCoreData
->HeapOffset
);
207 OldCoreData
->FileHandles
= (EFI_PEI_FILE_HANDLE
*) ((UINT8
*) OldCoreData
->FileHandles
- OldCoreData
->HeapOffset
);
211 // Initialize libraries that the PEI Core is linked against
213 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&OldCoreData
->Ps
);
216 // Fixup for PeiService's address
218 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&OldCoreData
->Ps
);
221 // Update HandOffHob for new installed permenent memory
223 HandoffInformationTable
= OldCoreData
->HobList
.HandoffInformationTable
;
224 if (OldCoreData
->HeapOffsetPositive
) {
225 HandoffInformationTable
->EfiEndOfHobList
= HandoffInformationTable
->EfiEndOfHobList
+ OldCoreData
->HeapOffset
;
227 HandoffInformationTable
->EfiEndOfHobList
= HandoffInformationTable
->EfiEndOfHobList
- OldCoreData
->HeapOffset
;
229 HandoffInformationTable
->EfiMemoryTop
= OldCoreData
->PhysicalMemoryBegin
+ OldCoreData
->PhysicalMemoryLength
;
230 HandoffInformationTable
->EfiMemoryBottom
= OldCoreData
->PhysicalMemoryBegin
;
231 HandoffInformationTable
->EfiFreeMemoryTop
= OldCoreData
->FreePhysicalMemoryTop
;
232 HandoffInformationTable
->EfiFreeMemoryBottom
= HandoffInformationTable
->EfiEndOfHobList
+ sizeof (EFI_HOB_GENERIC_HEADER
);
235 // We need convert the PPI descriptor's pointer
237 ConvertPpiPointers (SecCoreData
, OldCoreData
);
240 // After the whole temporary memory is migrated, then we can allocate page in
243 OldCoreData
->PeiMemoryInstalled
= TRUE
;
246 // Indicate that PeiCore reenter
248 OldCoreData
->PeimDispatcherReenter
= TRUE
;
250 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (OldCoreData
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
252 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
253 // Every bit in the array indicate the status of the corresponding memory page available or not
255 OldCoreData
->PeiCodeMemoryRangeUsageBitMap
= AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber
)>>6) + 1)*sizeof(UINT64
));
259 // Shadow PEI Core. When permanent memory is avaiable, shadow
260 // PEI Core and PEIMs to get high performance.
262 OldCoreData
->ShadowedPeiCore
= ShadowPeiCore (OldCoreData
);
265 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
267 OldCoreData
->ShadowedPeiCore (SecCoreData
, PpiList
, OldCoreData
);
270 // Should never reach here.
277 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
279 CopyMem (&NewSecCoreData
, SecCoreDataPtr
, sizeof (NewSecCoreData
));
280 SecCoreData
= &NewSecCoreData
;
282 CopyMem (&PrivateData
, OldCoreData
, sizeof (PrivateData
));
284 CpuIo
= (VOID
*)PrivateData
.ServiceTableShadow
.CpuIo
;
285 PciCfg
= (VOID
*)PrivateData
.ServiceTableShadow
.PciCfg
;
287 CopyMem (&PrivateData
.ServiceTableShadow
, &gPs
, sizeof (gPs
));
289 PrivateData
.ServiceTableShadow
.CpuIo
= CpuIo
;
290 PrivateData
.ServiceTableShadow
.PciCfg
= PciCfg
;
294 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
296 PrivateData
.Ps
= &PrivateData
.ServiceTableShadow
;
299 // Initialize libraries that the PEI Core is linked against
301 ProcessLibraryConstructorList (NULL
, (CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
304 // Save PeiServicePointer so that it can be retrieved anywhere.
306 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES
**)&PrivateData
.Ps
);
309 // Initialize PEI Core Services
311 InitializeMemoryServices (&PrivateData
, SecCoreData
, OldCoreData
);
312 if (OldCoreData
== NULL
) {
314 // Initialize PEI Core Private Data Buffer
316 PrivateData
.PpiData
.PpiListPtrs
= AllocateZeroPool (sizeof (PEI_PPI_LIST_POINTERS
) * PcdGet32 (PcdPeiCoreMaxPpiSupported
));
317 ASSERT (PrivateData
.PpiData
.PpiListPtrs
!= NULL
);
318 PrivateData
.Fv
= AllocateZeroPool (sizeof (PEI_CORE_FV_HANDLE
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
319 ASSERT (PrivateData
.Fv
!= NULL
);
320 PrivateData
.Fv
[0].PeimState
= AllocateZeroPool (sizeof (UINT8
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
321 ASSERT (PrivateData
.Fv
[0].PeimState
!= NULL
);
322 PrivateData
.Fv
[0].FvFileHandles
= AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
323 ASSERT (PrivateData
.Fv
[0].FvFileHandles
!= NULL
);
324 for (Index
= 1; Index
< PcdGet32 (PcdPeiCoreMaxFvSupported
); Index
++) {
325 PrivateData
.Fv
[Index
].PeimState
= PrivateData
.Fv
[Index
- 1].PeimState
+ PcdGet32 (PcdPeiCoreMaxPeimPerFv
);
326 PrivateData
.Fv
[Index
].FvFileHandles
= PrivateData
.Fv
[Index
- 1].FvFileHandles
+ PcdGet32 (PcdPeiCoreMaxPeimPerFv
);
328 PrivateData
.UnknownFvInfo
= AllocateZeroPool (sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
329 ASSERT (PrivateData
.UnknownFvInfo
!= NULL
);
330 PrivateData
.CurrentFvFileHandles
= AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
331 ASSERT (PrivateData
.CurrentFvFileHandles
!= NULL
);
332 PrivateData
.FileGuid
= AllocatePool (sizeof (EFI_GUID
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
333 ASSERT (PrivateData
.FileGuid
!= NULL
);
334 PrivateData
.FileHandles
= AllocatePool (sizeof (EFI_PEI_FILE_HANDLE
) * (PcdGet32 (PcdPeiCoreMaxPeimPerFv
) + 1));
335 ASSERT (PrivateData
.FileHandles
!= NULL
);
337 InitializePpiServices (&PrivateData
, OldCoreData
);
340 // Update performance measurements
342 if (OldCoreData
== NULL
) {
343 PERF_START (NULL
, "SEC", NULL
, 1);
344 PERF_END (NULL
, "SEC", NULL
, 0);
347 // If first pass, start performance measurement.
349 PERF_START (NULL
,"PEI", NULL
, 0);
350 PERF_START (NULL
,"PreMem", NULL
, 0);
353 PERF_END (NULL
,"PreMem", NULL
, 0);
354 PERF_START (NULL
,"PostMem", NULL
, 0);
358 // Complete PEI Core Service initialization
360 InitializeSecurityServices (&PrivateData
.Ps
, OldCoreData
);
361 InitializeDispatcherData (&PrivateData
, OldCoreData
, SecCoreData
);
362 InitializeImageServices (&PrivateData
, OldCoreData
);
365 // Perform PEI Core Phase specific actions
367 if (OldCoreData
== NULL
) {
369 // Report Status Code EFI_SW_PC_INIT
373 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT
)
377 // If SEC provided any PPI services to PEI, install them.
379 if (PpiList
!= NULL
) {
380 Status
= PeiServicesInstallPpi (PpiList
);
381 ASSERT_EFI_ERROR (Status
);
385 // Try to locate Temporary RAM Done Ppi.
387 Status
= PeiServicesLocatePpi (
388 &gEfiTemporaryRamDonePpiGuid
,
391 (VOID
**)&TemporaryRamDonePpi
393 if (!EFI_ERROR (Status
)) {
395 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.
397 TemporaryRamDonePpi
->TemporaryRamDone ();
401 // Alert any listeners that there is permanent memory available
403 PERF_START (NULL
,"DisMem", NULL
, 0);
404 Status
= PeiServicesInstallPpi (&mMemoryDiscoveredPpi
);
407 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
409 ProcessNotifyList (&PrivateData
);
411 PERF_END (NULL
,"DisMem", NULL
, 0);
415 // Call PEIM dispatcher
417 PeiDispatcher (SecCoreData
, &PrivateData
);
420 // Check if InstallPeiMemory service was called.
422 ASSERT(PrivateData
.PeiMemoryInstalled
== TRUE
);
425 // Measure PEI Core execution time.
427 PERF_END (NULL
, "PostMem", NULL
, 0);
430 // Lookup DXE IPL PPI
432 Status
= PeiServicesLocatePpi (
436 (VOID
**)&TempPtr
.DxeIpl
438 ASSERT_EFI_ERROR (Status
);
440 if (EFI_ERROR (Status
)) {
442 // Report status code to indicate DXE IPL PPI could not be found.
445 EFI_ERROR_CODE
| EFI_ERROR_MAJOR
,
446 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PEI_CORE_EC_DXEIPL_NOT_FOUND
)
452 // Enter DxeIpl to load Dxe core.
454 DEBUG ((EFI_D_INFO
, "DXE IPL Entry\n"));
455 Status
= TempPtr
.DxeIpl
->Entry (
461 // Should never reach here.
463 ASSERT_EFI_ERROR (Status
);