]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
1. Enable use-cases in PEI using SecurityPPI co-equal to the use-cases in DXE using...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / PeiMain / PeiMain.c
1 /** @file
2 Pei Core Main Entry Point
3
4 Copyright (c) 2006 - 2013, 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 };
68
69 /**
70 Shadow PeiCore module from flash to installed memory.
71
72 @param PrivateData PeiCore's private data structure
73
74 @return PeiCore function address after shadowing.
75 **/
76 PEICORE_FUNCTION_POINTER
77 ShadowPeiCore (
78 IN PEI_CORE_INSTANCE *PrivateData
79 )
80 {
81 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;
82 EFI_PHYSICAL_ADDRESS EntryPoint;
83 EFI_STATUS Status;
84 UINT32 AuthenticationState;
85
86 PeiCoreFileHandle = NULL;
87
88 //
89 // Find the PEI Core in the BFV
90 //
91 Status = PrivateData->Fv[0].FvPpi->FindFileByType (
92 PrivateData->Fv[0].FvPpi,
93 EFI_FV_FILETYPE_PEI_CORE,
94 PrivateData->Fv[0].FvHandle,
95 &PeiCoreFileHandle
96 );
97 ASSERT_EFI_ERROR (Status);
98
99 //
100 // Shadow PEI Core into memory so it will run faster
101 //
102 Status = PeiLoadImage (
103 GetPeiServicesTablePointer (),
104 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),
105 PEIM_STATE_REGISITER_FOR_SHADOW,
106 &EntryPoint,
107 &AuthenticationState
108 );
109 ASSERT_EFI_ERROR (Status);
110
111 //
112 // Compute the PeiCore's function address after shaowed PeiCore.
113 // _ModuleEntryPoint is PeiCore main function entry
114 //
115 return (PEICORE_FUNCTION_POINTER)((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);
116 }
117
118 /**
119 This routine is invoked by main entry of PeiMain module during transition
120 from SEC to PEI. After switching stack in the PEI core, it will restart
121 with the old core data.
122
123 @param SecCoreData Points to a data structure containing information about the PEI core's operating
124 environment, such as the size and location of temporary RAM, the stack location and
125 the BFV location.
126 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.
127 An empty PPI list consists of a single descriptor with the end-tag
128 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST. As part of its initialization
129 phase, the PEI Foundation will add these SEC-hosted PPIs to its PPI database such
130 that both the PEI Foundation and any modules can leverage the associated service
131 calls and/or code in these early PPIs
132 @param Data Pointer to old core data that is used to initialize the
133 core's data areas.
134 If NULL, it is first PeiCore entering.
135
136 **/
137 VOID
138 EFIAPI
139 PeiCore (
140 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
141 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
142 IN VOID *Data
143 )
144 {
145 PEI_CORE_INSTANCE PrivateData;
146 EFI_STATUS Status;
147 PEI_CORE_TEMP_POINTERS TempPtr;
148 PEI_CORE_INSTANCE *OldCoreData;
149 EFI_PEI_CPU_IO_PPI *CpuIo;
150 EFI_PEI_PCI_CFG2_PPI *PciCfg;
151 EFI_HOB_HANDOFF_INFO_TABLE *HandoffInformationTable;
152
153 //
154 // Retrieve context passed into PEI Core
155 //
156 OldCoreData = (PEI_CORE_INSTANCE *)Data;
157
158 //
159 // Perform PEI Core phase specific actions.
160 //
161 if (OldCoreData == NULL) {
162 //
163 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
164 //
165 ZeroMem (&PrivateData, sizeof (PEI_CORE_INSTANCE));
166 PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;
167 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
168 } else {
169 //
170 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
171 //
172 if (OldCoreData->ShadowedPeiCore == NULL) {
173 //
174 // Fixup the PeiCore's private data
175 //
176 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;
177 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;
178 if (OldCoreData->HeapOffsetPositive) {
179 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);
180 } else {
181 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);
182 }
183
184 //
185 // Initialize libraries that the PEI Core is linked against
186 //
187 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
188
189 //
190 // Fixup for PeiService's address
191 //
192 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
193
194 //
195 // Update HandOffHob for new installed permenent memory
196 //
197 HandoffInformationTable = OldCoreData->HobList.HandoffInformationTable;
198 if (OldCoreData->HeapOffsetPositive) {
199 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList + OldCoreData->HeapOffset;
200 } else {
201 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList - OldCoreData->HeapOffset;
202 }
203 HandoffInformationTable->EfiMemoryTop = OldCoreData->PhysicalMemoryBegin + OldCoreData->PhysicalMemoryLength;
204 HandoffInformationTable->EfiMemoryBottom = OldCoreData->PhysicalMemoryBegin;
205 HandoffInformationTable->EfiFreeMemoryTop = OldCoreData->FreePhysicalMemoryTop;
206 HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);
207
208 //
209 // We need convert the PPI descriptor's pointer
210 //
211 ConvertPpiPointers (SecCoreData, OldCoreData);
212
213 //
214 // After the whole temporary memory is migrated, then we can allocate page in
215 // permenent memory.
216 //
217 OldCoreData->PeiMemoryInstalled = TRUE;
218
219 //
220 // Indicate that PeiCore reenter
221 //
222 OldCoreData->PeimDispatcherReenter = TRUE;
223
224 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (OldCoreData->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
225 //
226 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
227 // Every bit in the array indicate the status of the corresponding memory page available or not
228 //
229 OldCoreData->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));
230 }
231
232 //
233 // Shadow PEI Core. When permanent memory is avaiable, shadow
234 // PEI Core and PEIMs to get high performance.
235 //
236 OldCoreData->ShadowedPeiCore = ShadowPeiCore (OldCoreData);
237
238 //
239 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
240 //
241 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);
242
243 //
244 // Should never reach here.
245 //
246 ASSERT (FALSE);
247 CpuDeadLoop();
248 }
249
250 //
251 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
252 //
253
254 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));
255
256 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;
257 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;
258
259 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
260
261 PrivateData.ServiceTableShadow.CpuIo = CpuIo;
262 PrivateData.ServiceTableShadow.PciCfg = PciCfg;
263 }
264
265 //
266 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
267 //
268 PrivateData.Ps = &PrivateData.ServiceTableShadow;
269
270 //
271 // Initialize libraries that the PEI Core is linked against
272 //
273 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
274
275 //
276 // Save PeiServicePointer so that it can be retrieved anywhere.
277 //
278 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
279
280 //
281 // Initialize PEI Core Services
282 //
283 InitializeMemoryServices (&PrivateData, SecCoreData, OldCoreData);
284 InitializePpiServices (&PrivateData, OldCoreData);
285
286 //
287 // Update performance measurements
288 //
289 if (OldCoreData == NULL) {
290 PERF_START (NULL, "SEC", NULL, 1);
291 PERF_END (NULL, "SEC", NULL, 0);
292
293 //
294 // If first pass, start performance measurement.
295 //
296 PERF_START (NULL,"PEI", NULL, 0);
297 PERF_START (NULL,"PreMem", NULL, 0);
298
299 } else {
300 PERF_END (NULL,"PreMem", NULL, 0);
301 PERF_START (NULL,"PostMem", NULL, 0);
302 }
303
304 //
305 // Complete PEI Core Service initialization
306 //
307 InitializeSecurityServices (&PrivateData.Ps, OldCoreData);
308 InitializeDispatcherData (&PrivateData, OldCoreData, SecCoreData);
309 InitializeImageServices (&PrivateData, OldCoreData);
310
311 //
312 // Perform PEI Core Phase specific actions
313 //
314 if (OldCoreData == NULL) {
315 //
316 // Report Status Code EFI_SW_PC_INIT
317 //
318 REPORT_STATUS_CODE (
319 EFI_PROGRESS_CODE,
320 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)
321 );
322
323 //
324 // If SEC provided any PPI services to PEI, install them.
325 //
326 if (PpiList != NULL) {
327 Status = PeiServicesInstallPpi (PpiList);
328 ASSERT_EFI_ERROR (Status);
329 }
330 } else {
331 //
332 // Alert any listeners that there is permanent memory available
333 //
334 PERF_START (NULL,"DisMem", NULL, 0);
335 Status = PeiServicesInstallPpi (&mMemoryDiscoveredPpi);
336
337 //
338 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
339 //
340 ProcessNotifyList (&PrivateData);
341
342 PERF_END (NULL,"DisMem", NULL, 0);
343 }
344
345 //
346 // Call PEIM dispatcher
347 //
348 PeiDispatcher (SecCoreData, &PrivateData);
349
350 //
351 // Check if InstallPeiMemory service was called.
352 //
353 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);
354
355 //
356 // Measure PEI Core execution time.
357 //
358 PERF_END (NULL, "PostMem", NULL, 0);
359
360 //
361 // Lookup DXE IPL PPI
362 //
363 Status = PeiServicesLocatePpi (
364 &gEfiDxeIplPpiGuid,
365 0,
366 NULL,
367 (VOID **)&TempPtr.DxeIpl
368 );
369 ASSERT_EFI_ERROR (Status);
370
371 //
372 // Enter DxeIpl to load Dxe core.
373 //
374 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));
375 Status = TempPtr.DxeIpl->Entry (
376 TempPtr.DxeIpl,
377 &PrivateData.Ps,
378 PrivateData.HobList
379 );
380 //
381 // Should never reach here.
382 //
383 ASSERT_EFI_ERROR (Status);
384 CpuDeadLoop();
385 }