]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Replace BufToHexString by UnicodeValueToString
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core dispatch services\r
3 \r
90e128e2 4Copyright (c) 2006 - 2009, Intel Corporation\r
192f6d4c 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
615c6dd0 13**/\r
192f6d4c 14\r
0d516397 15#include "PeiMain.h"\r
192f6d4c 16\r
b1f6a7c6 17///\r
18/// CAR is filled with this initial value during SEC phase\r
19///\r
a7715e73 20#define INIT_CAR_VALUE 0x5AA55AA5\r
21\r
797a9d67 22typedef struct {\r
23 EFI_STATUS_CODE_DATA DataHeader;\r
24 EFI_HANDLE Handle;\r
25} PEIM_FILE_HANDLE_EXTENDED_DATA;\r
26\r
b1f6a7c6 27/**\r
b0d803fe 28\r
29 Discover all Peims and optional Apriori file in one FV. There is at most one\r
30 Apriori file in one FV.\r
31\r
b0d803fe 32\r
b1f6a7c6 33 @param Private - Pointer to the private data passed in from caller\r
34 @param VolumeHandle - Fv handle.\r
b0d803fe 35\r
b1f6a7c6 36**/\r
37VOID\r
38DiscoverPeimsAndOrderWithApriori (\r
39 IN PEI_CORE_INSTANCE *Private,\r
40 IN EFI_PEI_FV_HANDLE VolumeHandle\r
41 )\r
b0d803fe 42{\r
43 EFI_STATUS Status;\r
44 EFI_PEI_FV_HANDLE FileHandle;\r
177aabe6 45 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
b0d803fe 46 EFI_GUID *Apriori;\r
47 UINTN Index;\r
48 UINTN Index2;\r
49 UINTN PeimIndex;\r
50 UINTN PeimCount;\r
51 EFI_GUID *Guid;\r
177aabe6 52 EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
53 EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
b0d803fe 54\r
55 //\r
56 // Walk the FV and find all the PEIMs and the Apriori file.\r
57 //\r
58 AprioriFileHandle = NULL;\r
59 Private->CurrentFvFileHandles[0] = NULL;\r
60 Guid = NULL;\r
61 FileHandle = NULL;\r
62\r
63 //\r
64 // If the current Fv has been scanned, directly get its cachable record.\r
65 //\r
66 if (Private->Fv[Private->CurrentPeimFvCount].ScanFv) {\r
67 CopyMem (Private->CurrentFvFileHandles, Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
68 return;\r
69 }\r
70\r
71 //\r
72 // Go ahead to scan this Fv, and cache FileHandles within it.\r
73 //\r
177aabe6 74 for (PeimCount = 0; PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {\r
b0d803fe 75 Status = PeiFindFileEx (\r
58dcdada 76 VolumeHandle,\r
77 NULL,\r
78 PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE,\r
b0d803fe 79 &FileHandle,\r
80 &AprioriFileHandle\r
81 );\r
82 if (Status != EFI_SUCCESS) {\r
83 break;\r
84 }\r
58dcdada 85\r
b0d803fe 86 Private->CurrentFvFileHandles[PeimCount] = FileHandle;\r
87 }\r
97b2c9b5
LG
88 \r
89 //\r
90 // Check whether the count of Peims exceeds the max support PEIMs in a FV image\r
91 // If more Peims are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.\r
92 //\r
93 ASSERT (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
b0d803fe 94\r
58dcdada 95 Private->AprioriCount = 0;\r
b0d803fe 96 if (AprioriFileHandle != NULL) {\r
97 //\r
98 // Read the Apriori file\r
99 //\r
177aabe6 100 Status = PeiServicesFfsFindSectionData (EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
b0d803fe 101 if (!EFI_ERROR (Status)) {\r
102 //\r
103 // Calculate the number of PEIMs in the A Priori list\r
104 //\r
105 Private->AprioriCount = *(UINT32 *)(((EFI_FFS_FILE_HEADER *)AprioriFileHandle)->Size) & 0x00FFFFFF;\r
58dcdada 106 Private->AprioriCount -= sizeof (EFI_FFS_FILE_HEADER) - sizeof (EFI_COMMON_SECTION_HEADER);\r
b0d803fe 107 Private->AprioriCount /= sizeof (EFI_GUID);\r
58dcdada 108\r
82b8c8df 109 ZeroMem (FileGuid, sizeof (FileGuid));\r
b0d803fe 110 for (Index = 0; Index < PeimCount; Index++) {\r
111 //\r
112 // Make an array of file name guids that matches the FileHandle array so we can convert\r
113 // quickly from file name to file handle\r
114 //\r
58dcdada 115 CopyMem (&FileGuid[Index], &((EFI_FFS_FILE_HEADER *)Private->CurrentFvFileHandles[Index])->Name,sizeof(EFI_GUID));\r
b0d803fe 116 }\r
117\r
118 //\r
119 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.\r
58dcdada 120 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.\r
b0d803fe 121 //\r
122 Index2 = 0;\r
123 for (Index = 0; Index2 < Private->AprioriCount; Index++) {\r
124 while (Index2 < Private->AprioriCount) {\r
125 Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);\r
126 if (Guid != NULL) {\r
127 break;\r
128 }\r
129 }\r
130 if (Guid == NULL) {\r
58dcdada 131 break;\r
b0d803fe 132 }\r
133 PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);\r
134 TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];\r
135\r
136 //\r
137 // Since we have copied the file handle we can remove it from this list.\r
138 //\r
139 Private->CurrentFvFileHandles[PeimIndex] = NULL;\r
140 }\r
141\r
142 //\r
143 // Update valid Aprioricount\r
144 //\r
145 Private->AprioriCount = Index;\r
58dcdada 146\r
b0d803fe 147 //\r
148 // Add in any PEIMs not in the Apriori file\r
149 //\r
150 for (;Index < PeimCount; Index++) {\r
151 for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
152 if (Private->CurrentFvFileHandles[Index2] != NULL) {\r
153 TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];\r
154 Private->CurrentFvFileHandles[Index2] = NULL;\r
155 break;\r
156 }\r
157 }\r
158 }\r
159 //\r
160 //Index the end of array contains re-range Pei moudle.\r
161 //\r
162 TempFileHandles[Index] = NULL;\r
58dcdada 163\r
b0d803fe 164 //\r
165 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.\r
58dcdada 166 // We need to update it to start with files in the A Priori list and\r
167 // then the remaining files in PEIM order.\r
b0d803fe 168 //\r
169 CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles));\r
170 }\r
171 }\r
172 //\r
173 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.\r
174 // Instead, we can retrieve the file handles within this Fv from cachable data.\r
175 //\r
176 Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;\r
177 CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
58dcdada 178\r
179}\r
180\r
b1f6a7c6 181/**\r
182 Shadow PeiCore module from flash to installed memory.\r
183 \r
d73d93c3 184 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
b1f6a7c6 185 @param PrivateInMem PeiCore's private data structure\r
186\r
82b8c8df 187 @return PeiCore function address after shadowing.\r
b1f6a7c6 188**/\r
58dcdada 189VOID*\r
190ShadowPeiCore(\r
6c7a807a 191 IN CONST EFI_PEI_SERVICES **PeiServices,\r
192 IN PEI_CORE_INSTANCE *PrivateInMem\r
58dcdada 193 )\r
194{\r
195 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;\r
196 EFI_PHYSICAL_ADDRESS EntryPoint;\r
197 EFI_STATUS Status;\r
198 UINT32 AuthenticationState;\r
199\r
200 PeiCoreFileHandle = NULL;\r
201\r
202 //\r
203 // Find the PEI Core in the BFV\r
204 //\r
205 Status = PeiFindFileEx (\r
206 (EFI_PEI_FV_HANDLE)PrivateInMem->Fv[0].FvHeader,\r
207 NULL,\r
208 EFI_FV_FILETYPE_PEI_CORE,\r
209 &PeiCoreFileHandle,\r
210 NULL\r
211 );\r
212 ASSERT_EFI_ERROR (Status);\r
213\r
214 //\r
215 // Shadow PEI Core into memory so it will run faster\r
216 //\r
217 Status = PeiLoadImage (\r
218 PeiServices,\r
219 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),\r
220 &EntryPoint,\r
221 &AuthenticationState\r
222 );\r
223 ASSERT_EFI_ERROR (Status);\r
224\r
82b8c8df 225 //\r
226 // Compute the PeiCore's function address after shaowed PeiCore.\r
227 // _ModuleEntryPoint is PeiCore main function entry\r
228 //\r
58dcdada 229 return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
b0d803fe 230}\r
231\r
b1f6a7c6 232/**\r
192f6d4c 233 Conduct PEIM dispatch.\r
234\r
b1f6a7c6 235 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 236 environment, such as the size and location of temporary RAM, the stack location and\r
237 the BFV location.\r
b1f6a7c6 238 @param Private Pointer to the private data passed in from caller\r
192f6d4c 239\r
b1f6a7c6 240**/\r
241VOID\r
242PeiDispatcher (\r
243 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
244 IN PEI_CORE_INSTANCE *Private\r
245 )\r
192f6d4c 246{\r
b0d803fe 247 EFI_STATUS Status;\r
248 UINT32 Index1;\r
249 UINT32 Index2;\r
6c7a807a 250 CONST EFI_PEI_SERVICES **PeiServices;\r
58dcdada 251 EFI_PEI_FV_HANDLE VolumeHandle;\r
b0d803fe 252 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
253 UINTN FvCount;\r
254 UINTN PeimCount;\r
255 UINT32 AuthenticationState;\r
256 EFI_PHYSICAL_ADDRESS EntryPoint;\r
797a9d67 257 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
b0d803fe 258 UINTN SaveCurrentPeimCount;\r
1053e0c5 259 UINTN SaveCurrentFvCount;\r
b0d803fe 260 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
797a9d67 261 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;\r
58dcdada 262 EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase;\r
263 TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
264 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable;\r
265 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable;\r
66c69dea 266 INTN StackOffset;\r
267 INTN HeapOffset;\r
58dcdada 268 PEI_CORE_INSTANCE *PrivateInMem;\r
269 UINT64 NewPeiStackSize;\r
270 UINT64 OldPeiStackSize;\r
271 UINT64 StackGap;\r
288f9b38 272 EFI_FV_FILE_INFO FvFileInfo;\r
58dcdada 273 UINTN OldCheckingTop;\r
274 UINTN OldCheckingBottom;\r
b0d803fe 275\r
276\r
6c7a807a 277 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->PS;\r
b0d803fe 278 PeimEntryPoint = NULL;\r
279 PeimFileHandle = NULL;\r
288f9b38 280 EntryPoint = 0;\r
b0d803fe 281\r
282 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
283 //\r
284 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
285 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
286 //\r
287 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
1053e0c5 288 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
b0d803fe 289 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
290\r
1053e0c5 291 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
177aabe6 292 for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
b0d803fe 293 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
58dcdada 294 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
b0d803fe 295 Status = PeiLoadImage (\r
6c7a807a 296 (CONST EFI_PEI_SERVICES **) &Private->PS,\r
58dcdada 297 PeimFileHandle,\r
298 &EntryPoint,\r
b0d803fe 299 &AuthenticationState\r
300 );\r
301 if (Status == EFI_SUCCESS) {\r
302 //\r
303 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
304 //\r
305 Private->Fv[Index1].PeimState[Index2]++;\r
1053e0c5 306 Private->CurrentFileHandle = PeimFileHandle;\r
58dcdada 307 Private->CurrentPeimFvCount = Index1;\r
308 Private->CurrentPeimCount = Index2;\r
b0d803fe 309 //\r
310 // Call the PEIM entry point\r
311 //\r
797a9d67 312 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
58dcdada 313\r
b0d803fe 314 PERF_START (0, "PEIM", NULL, 0);\r
797a9d67 315 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->PS);\r
b0d803fe 316 PERF_END (0, "PEIM", NULL, 0);\r
58dcdada 317 }\r
318\r
b0d803fe 319 //\r
320 // Process the Notify list and dispatch any notifies for\r
321 // newly installed PPIs.\r
322 //\r
323 ProcessNotifyList (Private);\r
324 }\r
325 }\r
326 }\r
58dcdada 327 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
328 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
329 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
b0d803fe 330 }\r
192f6d4c 331\r
332 //\r
333 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
334 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
335 // pass of the dispatcher, it will start over from the Bfv again to see\r
336 // if any new PEIMs dependencies got satisfied. With a well ordered\r
337 // FV where PEIMs are found in the order their dependencies are also\r
338 // satisfied, this dipatcher should run only once.\r
339 //\r
b0d803fe 340 do {\r
82b8c8df 341 //\r
342 // In case that reenter PeiCore happens, the last pass record is still available. \r
343 //\r
344 if (!Private->PeimDispatcherReenter) {\r
345 Private->PeimNeedingDispatch = FALSE;\r
346 Private->PeimDispatchOnThisPass = FALSE;\r
347 } else {\r
348 Private->PeimDispatcherReenter = FALSE;\r
349 }\r
350 \r
b0d803fe 351 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
352 Private->CurrentPeimFvCount = FvCount;\r
2a00326e
LG
353 //\r
354 // Get this Fv Handle by PeiService FvFindNextVolume.\r
355 //\r
6c7a807a 356 PeiFvFindNextVolume (PeiServices, FvCount, &VolumeHandle);\r
192f6d4c 357\r
b0d803fe 358 if (Private->CurrentPeimCount == 0) {\r
359 //\r
360 // When going through each FV, at first, search Apriori file to\r
58dcdada 361 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
b0d803fe 362 // dispatch at first.\r
363 //\r
364 DiscoverPeimsAndOrderWithApriori (Private, VolumeHandle);\r
365 }\r
192f6d4c 366\r
367 //\r
b0d803fe 368 // Start to dispatch all modules within the current Fv.\r
192f6d4c 369 //\r
58dcdada 370 for (PeimCount = Private->CurrentPeimCount;\r
371 (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
b0d803fe 372 PeimCount++) {\r
373 Private->CurrentPeimCount = PeimCount;\r
374 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
375\r
376 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
377 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
82b8c8df 378 Private->PeimNeedingDispatch = TRUE;\r
b0d803fe 379 } else {\r
288f9b38
LG
380 Status = PeiFfsGetFileInfo (PeimFileHandle, &FvFileInfo);\r
381 ASSERT_EFI_ERROR (Status);\r
382 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
383 //\r
384 // For Fv type file, Produce new FV PPI and FV hob\r
385 //\r
97b2c9b5 386 Status = ProcessFvFile (PeiServices, VolumeHandle, PeimFileHandle, &AuthenticationState);\r
288f9b38
LG
387 } else {\r
388 //\r
389 // For PEIM driver, Load its entry point\r
390 //\r
391 Status = PeiLoadImage (\r
58dcdada 392 PeiServices,\r
393 PeimFileHandle,\r
394 &EntryPoint,\r
288f9b38
LG
395 &AuthenticationState\r
396 );\r
397 }\r
398\r
b0d803fe 399 if ((Status == EFI_SUCCESS)) {\r
192f6d4c 400 //\r
58dcdada 401 // The PEIM has its dependencies satisfied, and its entry point\r
402 // has been found, so invoke it.\r
192f6d4c 403 //\r
b0d803fe 404 PERF_START (0, "PEIM", NULL, 0);\r
192f6d4c 405\r
b0d803fe 406 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
192f6d4c 407\r
408 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
409 EFI_PROGRESS_CODE,\r
797a9d67 410 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),\r
192f6d4c 411 (VOID *)(&ExtendedData),\r
412 sizeof (ExtendedData)\r
413 );\r
414\r
b0d803fe 415 Status = VerifyPeim (Private, VolumeHandle, PeimFileHandle);\r
416 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {\r
417 //\r
418 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
419 //\r
420 Private->Fv[FvCount].PeimState[PeimCount]++;\r
58dcdada 421\r
288f9b38
LG
422 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
423 //\r
424 // Call the PEIM entry point for PEIM driver\r
425 //\r
797a9d67 426 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
427 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
288f9b38 428 }\r
797a9d67 429\r
82b8c8df 430 Private->PeimDispatchOnThisPass = TRUE;\r
192f6d4c 431 }\r
432\r
433 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
434 EFI_PROGRESS_CODE,\r
797a9d67 435 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),\r
192f6d4c 436 (VOID *)(&ExtendedData),\r
437 sizeof (ExtendedData)\r
438 );\r
b0d803fe 439 PERF_END (0, "PEIM", NULL, 0);\r
440\r
58dcdada 441 }\r
442\r
443 if (Private->SwitchStackSignal) {\r
a7715e73 444 //\r
445 // Before switch stack from CAR to permenent memory, caculate the heap and stack\r
446 // usage in temporary memory for debuging.\r
447 //\r
448 DEBUG_CODE_BEGIN ();\r
96317468 449 UINT32 *StackPointer;\r
a7715e73 450 \r
96317468 451 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
452 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
a7715e73 453 && (*StackPointer == INIT_CAR_VALUE);\r
454 StackPointer ++);\r
455 \r
7df7393f 456 DEBUG ((EFI_D_INFO, "Total Cache as RAM: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
a7715e73 457 DEBUG ((EFI_D_INFO, " CAR stack ever used: %d bytes.\n",\r
458 (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
459 ));\r
460 DEBUG ((EFI_D_INFO, " CAR heap used: %d bytes.\n",\r
461 ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -\r
462 (UINTN) Private->HobList.Raw)\r
463 ));\r
464 DEBUG_CODE_END ();\r
465 \r
a3a15d21 466 //\r
58dcdada 467 // Reserve the size of new stack at bottom of physical memory\r
a3a15d21 468 //\r
63b62331 469 OldPeiStackSize = (UINT64) SecCoreData->StackSize;\r
58dcdada 470 NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
471 if (FixedPcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {\r
472 Private->StackSize = NewPeiStackSize;\r
473 } else {\r
474 Private->StackSize = FixedPcdGet32(PcdPeiCoreMaxPeiStackSize);\r
475 }\r
192f6d4c 476\r
58dcdada 477 //\r
478 // In theory, the size of new stack in permenent memory should large than\r
479 // size of old stack in temporary memory.\r
480 // But if new stack is smaller than the size of old stack, we also reserve\r
481 // the size of old stack at bottom of permenent memory.\r
482 //\r
90e128e2 483 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize));\r
28127cc7
LG
484 ASSERT (Private->StackSize >= OldPeiStackSize);\r
485 StackGap = Private->StackSize - OldPeiStackSize;\r
d74eeda8 486\r
58dcdada 487 //\r
488 // Update HandOffHob for new installed permenent memory\r
489 //\r
490 OldHandOffTable = Private->HobList.HandoffInformationTable;\r
5c5a0601 491 OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);\r
58dcdada 492 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
192f6d4c 493\r
494 //\r
58dcdada 495 // The whole temporary memory will be migrated to physical memory.\r
496 // CAUTION: The new base is computed accounding to gap of new stack.\r
192f6d4c 497 //\r
58dcdada 498 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
40f26b8f 499 \r
500 //\r
501 // Caculate stack offset and heap offset between CAR and new permement \r
502 // memory seperately.\r
503 //\r
d74eeda8 504 StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;\r
66c69dea 505 HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \\r
506 (UINTN) SecCoreData->PeiTemporaryRamBase);\r
7df7393f 507 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset));\r
66c69dea 508 \r
40f26b8f 509 //\r
510 // Caculate new HandOffTable and PrivateData address in permenet memory's stack\r
511 //\r
66c69dea 512 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);\r
513 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);\r
192f6d4c 514\r
515 //\r
58dcdada 516 // TemporaryRamSupportPpi is produced by platform's SEC\r
192f6d4c 517 //\r
58dcdada 518 Status = PeiLocatePpi (\r
519 (CONST EFI_PEI_SERVICES **) PeiServices,\r
520 &gEfiTemporaryRamSupportPpiGuid,\r
521 0,\r
522 NULL,\r
523 (VOID**)&TemporaryRamSupportPpi\r
524 );\r
525\r
81c7803c 526\r
58dcdada 527 if (!EFI_ERROR (Status)) {\r
40f26b8f 528 //\r
529 // Temporary Ram support Ppi is provided by platform, it will copy \r
530 // temporary memory to permenent memory and do stack switching.\r
531 // After invoken temporary Ram support, following code's stack is in \r
532 // memory but not in CAR.\r
533 //\r
58dcdada 534 TemporaryRamSupportPpi->TemporaryRamMigration (\r
535 (CONST EFI_PEI_SERVICES **) PeiServices,\r
536 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
537 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
538 SecCoreData->TemporaryRamSize\r
539 );\r
540\r
541 } else {\r
b414ea4b 542 //\r
543 // In IA32/x64/Itanium architecture, we need platform provide\r
544 // TEMPORAY_RAM_MIGRATION_PPI.\r
545 //\r
546 ASSERT (FALSE);\r
58dcdada 547 }\r
548\r
549\r
550 //\r
b0d803fe 551 //\r
58dcdada 552 // Fixup the PeiCore's private data\r
b0d803fe 553 //\r
58dcdada 554 PrivateInMem->PS = &PrivateInMem->ServiceTableShadow;\r
555 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;\r
66c69dea 556 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);\r
58dcdada 557 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
558\r
6c7a807a 559 PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->PS;\r
58dcdada 560\r
561 //\r
562 // Fixup for PeiService's address\r
563 //\r
564 SetPeiServicesTablePointer(PeiServices);\r
565\r
566 //\r
567 // Update HandOffHob for new installed permenent memory\r
568 //\r
569 NewHandOffTable->EfiEndOfHobList =\r
66c69dea 570 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);\r
58dcdada 571 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +\r
572 PrivateInMem->PhysicalMemoryLength;\r
573 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;\r
574 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;\r
575 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
576 sizeof (EFI_HOB_GENERIC_HEADER);\r
577\r
578 //\r
579 // We need convert the PPI desciptor's pointer\r
580 //\r
40f26b8f 581 ConvertPpiPointers (PrivateInMem, \r
58dcdada 582 OldCheckingBottom, \r
583 OldCheckingTop, \r
5c5a0601 584 HeapOffset\r
585 );\r
58dcdada 586\r
7df7393f 587 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n",\r
588 PrivateInMem->StackBase,\r
58dcdada 589 PrivateInMem->StackSize));\r
590 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
591\r
592 //\r
593 // After the whole temporary memory is migrated, then we can allocate page in\r
594 // permenent memory.\r
595 //\r
596 PrivateInMem->PeiMemoryInstalled = TRUE;\r
597\r
1bd90a4c 598 //\r
82b8c8df 599 // Indicate that PeiCore reenter\r
1bd90a4c 600 //\r
82b8c8df 601 PrivateInMem->PeimDispatcherReenter = TRUE;\r
602 \r
192f6d4c 603 //\r
b0d803fe 604 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
605 // PEI Core and PEIMs to get high performance.\r
192f6d4c 606 //\r
58dcdada 607 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
608 PeiServices,\r
609 PrivateInMem\r
610 );\r
b0d803fe 611 //\r
58dcdada 612 // Process the Notify list and dispatch any notifies for\r
613 // newly installed PPIs.\r
b0d803fe 614 //\r
58dcdada 615 ProcessNotifyList (PrivateInMem);\r
616\r
b0d803fe 617 //\r
58dcdada 618 // Entry PEI Phase 2\r
b0d803fe 619 //\r
58dcdada 620 PeiCore (SecCoreData, NULL, PrivateInMem);\r
b0d803fe 621\r
58dcdada 622 //\r
623 // Code should not come here\r
624 //\r
625 ASSERT_EFI_ERROR(FALSE);\r
192f6d4c 626 }\r
192f6d4c 627\r
58dcdada 628 //\r
629 // Process the Notify list and dispatch any notifies for\r
630 // newly installed PPIs.\r
631 //\r
632 ProcessNotifyList (Private);\r
633\r
b0d803fe 634 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
635 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
636 //\r
58dcdada 637 // If memory is availble we shadow images by default for performance reasons.\r
638 // We call the entry point a 2nd time so the module knows it's shadowed.\r
b0d803fe 639 //\r
640 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
e67ca95c 641 ASSERT (PeimEntryPoint != NULL);\r
797a9d67 642 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
b0d803fe 643 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
58dcdada 644\r
b0d803fe 645 //\r
646 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
647 //\r
648 Private->Fv[FvCount].PeimState[PeimCount]++;\r
192f6d4c 649\r
192f6d4c 650 //\r
b0d803fe 651 // Process the Notify list and dispatch any notifies for\r
652 // newly installed PPIs.\r
192f6d4c 653 //\r
b0d803fe 654 ProcessNotifyList (Private);\r
192f6d4c 655 }\r
656 }\r
657 }\r
192f6d4c 658 }\r
192f6d4c 659\r
b0d803fe 660 //\r
661 // We set to NULL here to optimize the 2nd entry to this routine after\r
662 // memory is found. This reprevents rescanning of the FV. We set to\r
663 // NULL here so we start at the begining of the next FV\r
664 //\r
665 Private->CurrentFileHandle = NULL;\r
666 Private->CurrentPeimCount = 0;\r
667 //\r
668 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
669 //\r
670 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
192f6d4c 671 }\r
672\r
673 //\r
58dcdada 674 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
b0d803fe 675 // through all the FV.\r
192f6d4c 676 //\r
b0d803fe 677 Private->CurrentPeimFvCount = 0;\r
192f6d4c 678\r
679 //\r
58dcdada 680 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
b0d803fe 681 // dispatched. So we need to make another pass\r
192f6d4c 682 //\r
58dcdada 683 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
b0d803fe 684 // pass. If we did not dispatch a PEIM there is no point in trying again\r
685 // as it will fail the next time too (nothing has changed).\r
192f6d4c 686 //\r
82b8c8df 687 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
192f6d4c 688\r
192f6d4c 689}\r
690\r
b1f6a7c6 691/**\r
192f6d4c 692 Initialize the Dispatcher's data members\r
693\r
b1f6a7c6 694 @param PrivateData PeiCore's private data structure\r
695 @param OldCoreData Old data from SecCore\r
192f6d4c 696 NULL if being run in non-permament memory mode.\r
b1f6a7c6 697 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 698 environment, such as the size and location of temporary RAM, the stack location and\r
699 the BFV location.\r
192f6d4c 700\r
b1f6a7c6 701 @return None.\r
192f6d4c 702\r
b1f6a7c6 703**/\r
704VOID\r
705InitializeDispatcherData (\r
706 IN PEI_CORE_INSTANCE *PrivateData,\r
707 IN PEI_CORE_INSTANCE *OldCoreData,\r
708 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
709 )\r
192f6d4c 710{\r
192f6d4c 711 if (OldCoreData == NULL) {\r
82b8c8df 712 PrivateData->PeimDispatcherReenter = FALSE;\r
b0d803fe 713 PeiInitializeFv (PrivateData, SecCoreData);\r
192f6d4c 714 }\r
715\r
716 return;\r
717}\r
718\r
b1f6a7c6 719/**\r
720 This routine parses the Dependency Expression, if available, and\r
721 decides if the module can be executed.\r
722\r
723\r
724 @param Private PeiCore's private data structure\r
725 @param FileHandle PEIM's file handle\r
726 @param PeimCount Peim count in all dispatched PEIMs.\r
192f6d4c 727\r
b1f6a7c6 728 @retval TRUE Can be dispatched\r
729 @retval FALSE Cannot be dispatched\r
730\r
731**/\r
192f6d4c 732BOOLEAN\r
733DepexSatisfied (\r
b0d803fe 734 IN PEI_CORE_INSTANCE *Private,\r
735 IN EFI_PEI_FILE_HANDLE FileHandle,\r
736 IN UINTN PeimCount\r
192f6d4c 737 )\r
192f6d4c 738{\r
288f9b38
LG
739 EFI_STATUS Status;\r
740 VOID *DepexData;\r
b0d803fe 741\r
742 if (PeimCount < Private->AprioriCount) {\r
743 //\r
744 // If its in the A priori file then we set Depex to TRUE\r
745 //\r
746 return TRUE;\r
747 }\r
58dcdada 748\r
288f9b38 749 //\r
58dcdada 750 // Depex section not in the encapsulated section.\r
288f9b38
LG
751 //\r
752 Status = PeiServicesFfsFindSectionData (\r
753 EFI_SECTION_PEI_DEPEX,\r
58dcdada 754 FileHandle,\r
288f9b38
LG
755 (VOID **)&DepexData\r
756 );\r
b0d803fe 757\r
192f6d4c 758 if (EFI_ERROR (Status)) {\r
b0d803fe 759 //\r
760 // If there is no DEPEX, assume the module can be executed\r
761 //\r
192f6d4c 762 return TRUE;\r
763 }\r
764\r
765 //\r
766 // Evaluate a given DEPEX\r
767 //\r
b0d803fe 768 return PeimDispatchReadiness (&Private->PS, DepexData);\r
192f6d4c 769}\r
770\r
14e8823a 771/**\r
772 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
773 discovery permanent memory.\r
774\r
b1f6a7c6 775 @param FileHandle File handle of a PEIM.\r
58dcdada 776\r
b1f6a7c6 777 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
778 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
779 @retval EFI_SUCCESS Successfully to register itself.\r
14e8823a 780\r
58dcdada 781**/\r
14e8823a 782EFI_STATUS\r
783EFIAPI\r
784PeiRegisterForShadow (\r
785 IN EFI_PEI_FILE_HANDLE FileHandle\r
786 )\r
787{\r
788 PEI_CORE_INSTANCE *Private;\r
789 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
790\r
791 if (Private->CurrentFileHandle != FileHandle) {\r
792 //\r
793 // The FileHandle must be for the current PEIM\r
794 //\r
795 return EFI_NOT_FOUND;\r
796 }\r
797\r
798 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
799 //\r
800 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
801 //\r
802 return EFI_ALREADY_STARTED;\r
803 }\r
58dcdada 804\r
14e8823a 805 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
806\r
807 return EFI_SUCCESS;\r
808}\r
809\r