]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
6429ad3a218cb533f5ba2d24689197543a8daa87
[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 SecCoreDataPtr 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 *SecCoreDataPtr,
141 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
142 IN VOID *Data
143 )
144 {
145 PEI_CORE_INSTANCE PrivateData;
146 EFI_SEC_PEI_HAND_OFF *SecCoreData;
147 EFI_SEC_PEI_HAND_OFF NewSecCoreData;
148 EFI_STATUS Status;
149 PEI_CORE_TEMP_POINTERS TempPtr;
150 PEI_CORE_INSTANCE *OldCoreData;
151 EFI_PEI_CPU_IO_PPI *CpuIo;
152 EFI_PEI_PCI_CFG2_PPI *PciCfg;
153 EFI_HOB_HANDOFF_INFO_TABLE *HandoffInformationTable;
154 EFI_PEI_TEMPORARY_RAM_DONE_PPI *TemporaryRamDonePpi;
155
156 //
157 // Retrieve context passed into PEI Core
158 //
159 OldCoreData = (PEI_CORE_INSTANCE *) Data;
160 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) SecCoreDataPtr;
161
162 //
163 // Perform PEI Core phase specific actions.
164 //
165 if (OldCoreData == NULL) {
166 //
167 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
168 //
169 ZeroMem (&PrivateData, sizeof (PEI_CORE_INSTANCE));
170 PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;
171 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
172 } else {
173 //
174 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
175 //
176 if (OldCoreData->ShadowedPeiCore == NULL) {
177 //
178 // Fixup the PeiCore's private data
179 //
180 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;
181 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;
182 if (OldCoreData->HeapOffsetPositive) {
183 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);
184 } else {
185 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);
186 }
187
188 //
189 // Initialize libraries that the PEI Core is linked against
190 //
191 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
192
193 //
194 // Fixup for PeiService's address
195 //
196 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
197
198 //
199 // Update HandOffHob for new installed permenent memory
200 //
201 HandoffInformationTable = OldCoreData->HobList.HandoffInformationTable;
202 if (OldCoreData->HeapOffsetPositive) {
203 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList + OldCoreData->HeapOffset;
204 } else {
205 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList - OldCoreData->HeapOffset;
206 }
207 HandoffInformationTable->EfiMemoryTop = OldCoreData->PhysicalMemoryBegin + OldCoreData->PhysicalMemoryLength;
208 HandoffInformationTable->EfiMemoryBottom = OldCoreData->PhysicalMemoryBegin;
209 HandoffInformationTable->EfiFreeMemoryTop = OldCoreData->FreePhysicalMemoryTop;
210 HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);
211
212 //
213 // We need convert the PPI descriptor's pointer
214 //
215 ConvertPpiPointers (SecCoreData, OldCoreData);
216
217 //
218 // After the whole temporary memory is migrated, then we can allocate page in
219 // permenent memory.
220 //
221 OldCoreData->PeiMemoryInstalled = TRUE;
222
223 //
224 // Indicate that PeiCore reenter
225 //
226 OldCoreData->PeimDispatcherReenter = TRUE;
227
228 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (OldCoreData->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
229 //
230 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
231 // Every bit in the array indicate the status of the corresponding memory page available or not
232 //
233 OldCoreData->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));
234 }
235
236 //
237 // Shadow PEI Core. When permanent memory is avaiable, shadow
238 // PEI Core and PEIMs to get high performance.
239 //
240 OldCoreData->ShadowedPeiCore = ShadowPeiCore (OldCoreData);
241
242 //
243 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
244 //
245 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);
246
247 //
248 // Should never reach here.
249 //
250 ASSERT (FALSE);
251 CpuDeadLoop();
252 }
253
254 //
255 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
256 //
257 CopyMem (&NewSecCoreData, SecCoreDataPtr, sizeof (NewSecCoreData));
258 SecCoreData = &NewSecCoreData;
259
260 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));
261
262 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;
263 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;
264
265 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
266
267 PrivateData.ServiceTableShadow.CpuIo = CpuIo;
268 PrivateData.ServiceTableShadow.PciCfg = PciCfg;
269 }
270
271 //
272 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
273 //
274 PrivateData.Ps = &PrivateData.ServiceTableShadow;
275
276 //
277 // Initialize libraries that the PEI Core is linked against
278 //
279 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
280
281 //
282 // Save PeiServicePointer so that it can be retrieved anywhere.
283 //
284 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
285
286 //
287 // Initialize PEI Core Services
288 //
289 InitializeMemoryServices (&PrivateData, SecCoreData, OldCoreData);
290 InitializePpiServices (&PrivateData, OldCoreData);
291
292 //
293 // Update performance measurements
294 //
295 if (OldCoreData == NULL) {
296 PERF_START (NULL, "SEC", NULL, 1);
297 PERF_END (NULL, "SEC", NULL, 0);
298
299 //
300 // If first pass, start performance measurement.
301 //
302 PERF_START (NULL,"PEI", NULL, 0);
303 PERF_START (NULL,"PreMem", NULL, 0);
304
305 } else {
306 PERF_END (NULL,"PreMem", NULL, 0);
307 PERF_START (NULL,"PostMem", NULL, 0);
308 }
309
310 //
311 // Complete PEI Core Service initialization
312 //
313 InitializeSecurityServices (&PrivateData.Ps, OldCoreData);
314 InitializeDispatcherData (&PrivateData, OldCoreData, SecCoreData);
315 InitializeImageServices (&PrivateData, OldCoreData);
316
317 //
318 // Perform PEI Core Phase specific actions
319 //
320 if (OldCoreData == NULL) {
321 //
322 // Report Status Code EFI_SW_PC_INIT
323 //
324 REPORT_STATUS_CODE (
325 EFI_PROGRESS_CODE,
326 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)
327 );
328
329 //
330 // If SEC provided any PPI services to PEI, install them.
331 //
332 if (PpiList != NULL) {
333 Status = PeiServicesInstallPpi (PpiList);
334 ASSERT_EFI_ERROR (Status);
335 }
336 } else {
337 //
338 // Try to locate Temporary RAM Done Ppi.
339 //
340 Status = PeiServicesLocatePpi (
341 &gEfiTemporaryRamDonePpiGuid,
342 0,
343 NULL,
344 (VOID**)&TemporaryRamDonePpi
345 );
346 if (!EFI_ERROR (Status)) {
347 //
348 // Disable the use of Temporary RAM after the transition from Temporary RAM to Permanent RAM is complete.
349 //
350 TemporaryRamDonePpi->TemporaryRamDone ();
351 }
352
353 //
354 // Alert any listeners that there is permanent memory available
355 //
356 PERF_START (NULL,"DisMem", NULL, 0);
357 Status = PeiServicesInstallPpi (&mMemoryDiscoveredPpi);
358
359 //
360 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
361 //
362 ProcessNotifyList (&PrivateData);
363
364 PERF_END (NULL,"DisMem", NULL, 0);
365 }
366
367 //
368 // Call PEIM dispatcher
369 //
370 PeiDispatcher (SecCoreData, &PrivateData);
371
372 //
373 // Check if InstallPeiMemory service was called.
374 //
375 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);
376
377 //
378 // Measure PEI Core execution time.
379 //
380 PERF_END (NULL, "PostMem", NULL, 0);
381
382 //
383 // Lookup DXE IPL PPI
384 //
385 Status = PeiServicesLocatePpi (
386 &gEfiDxeIplPpiGuid,
387 0,
388 NULL,
389 (VOID **)&TempPtr.DxeIpl
390 );
391 ASSERT_EFI_ERROR (Status);
392
393 //
394 // Enter DxeIpl to load Dxe core.
395 //
396 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));
397 Status = TempPtr.DxeIpl->Entry (
398 TempPtr.DxeIpl,
399 &PrivateData.Ps,
400 PrivateData.HobList
401 );
402 //
403 // Should never reach here.
404 //
405 ASSERT_EFI_ERROR (Status);
406 CpuDeadLoop();
407 }