]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Enhance PeiCore's dispatcher, move PeimDispatchOnThisPass and PeimNeedingDispatch...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core dispatch services\r
3 \r
192f6d4c 4Copyright (c) 2006, Intel Corporation\r
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
192f6d4c 15#include <PeiMain.h>\r
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
88\r
58dcdada 89 Private->AprioriCount = 0;\r
b0d803fe 90 if (AprioriFileHandle != NULL) {\r
91 //\r
92 // Read the Apriori file\r
93 //\r
177aabe6 94 Status = PeiServicesFfsFindSectionData (EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
b0d803fe 95 if (!EFI_ERROR (Status)) {\r
96 //\r
97 // Calculate the number of PEIMs in the A Priori list\r
98 //\r
99 Private->AprioriCount = *(UINT32 *)(((EFI_FFS_FILE_HEADER *)AprioriFileHandle)->Size) & 0x00FFFFFF;\r
58dcdada 100 Private->AprioriCount -= sizeof (EFI_FFS_FILE_HEADER) - sizeof (EFI_COMMON_SECTION_HEADER);\r
b0d803fe 101 Private->AprioriCount /= sizeof (EFI_GUID);\r
58dcdada 102\r
b0d803fe 103 SetMem (FileGuid, sizeof (FileGuid), 0);\r
104 for (Index = 0; Index < PeimCount; Index++) {\r
105 //\r
106 // Make an array of file name guids that matches the FileHandle array so we can convert\r
107 // quickly from file name to file handle\r
108 //\r
58dcdada 109 CopyMem (&FileGuid[Index], &((EFI_FFS_FILE_HEADER *)Private->CurrentFvFileHandles[Index])->Name,sizeof(EFI_GUID));\r
b0d803fe 110 }\r
111\r
112 //\r
113 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.\r
58dcdada 114 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.\r
b0d803fe 115 //\r
116 Index2 = 0;\r
117 for (Index = 0; Index2 < Private->AprioriCount; Index++) {\r
118 while (Index2 < Private->AprioriCount) {\r
119 Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);\r
120 if (Guid != NULL) {\r
121 break;\r
122 }\r
123 }\r
124 if (Guid == NULL) {\r
58dcdada 125 break;\r
b0d803fe 126 }\r
127 PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);\r
128 TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];\r
129\r
130 //\r
131 // Since we have copied the file handle we can remove it from this list.\r
132 //\r
133 Private->CurrentFvFileHandles[PeimIndex] = NULL;\r
134 }\r
135\r
136 //\r
137 // Update valid Aprioricount\r
138 //\r
139 Private->AprioriCount = Index;\r
58dcdada 140\r
b0d803fe 141 //\r
142 // Add in any PEIMs not in the Apriori file\r
143 //\r
144 for (;Index < PeimCount; Index++) {\r
145 for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
146 if (Private->CurrentFvFileHandles[Index2] != NULL) {\r
147 TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];\r
148 Private->CurrentFvFileHandles[Index2] = NULL;\r
149 break;\r
150 }\r
151 }\r
152 }\r
153 //\r
154 //Index the end of array contains re-range Pei moudle.\r
155 //\r
156 TempFileHandles[Index] = NULL;\r
58dcdada 157\r
b0d803fe 158 //\r
159 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.\r
58dcdada 160 // We need to update it to start with files in the A Priori list and\r
161 // then the remaining files in PEIM order.\r
b0d803fe 162 //\r
163 CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles));\r
164 }\r
165 }\r
166 //\r
167 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.\r
168 // Instead, we can retrieve the file handles within this Fv from cachable data.\r
169 //\r
170 Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;\r
171 CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
58dcdada 172\r
173}\r
174\r
b1f6a7c6 175/**\r
176 Shadow PeiCore module from flash to installed memory.\r
177 \r
d73d93c3 178 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
b1f6a7c6 179 @param PrivateInMem PeiCore's private data structure\r
180\r
181**/\r
58dcdada 182VOID*\r
183ShadowPeiCore(\r
184 EFI_PEI_SERVICES **PeiServices,\r
185 PEI_CORE_INSTANCE *PrivateInMem\r
186 )\r
187{\r
188 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;\r
189 EFI_PHYSICAL_ADDRESS EntryPoint;\r
190 EFI_STATUS Status;\r
191 UINT32 AuthenticationState;\r
192\r
193 PeiCoreFileHandle = NULL;\r
194\r
195 //\r
196 // Find the PEI Core in the BFV\r
197 //\r
198 Status = PeiFindFileEx (\r
199 (EFI_PEI_FV_HANDLE)PrivateInMem->Fv[0].FvHeader,\r
200 NULL,\r
201 EFI_FV_FILETYPE_PEI_CORE,\r
202 &PeiCoreFileHandle,\r
203 NULL\r
204 );\r
205 ASSERT_EFI_ERROR (Status);\r
206\r
207 //\r
208 // Shadow PEI Core into memory so it will run faster\r
209 //\r
210 Status = PeiLoadImage (\r
211 PeiServices,\r
212 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),\r
213 &EntryPoint,\r
214 &AuthenticationState\r
215 );\r
216 ASSERT_EFI_ERROR (Status);\r
217\r
218 return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
b0d803fe 219}\r
220\r
b1f6a7c6 221/**\r
192f6d4c 222 Conduct PEIM dispatch.\r
223\r
b1f6a7c6 224 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 225 environment, such as the size and location of temporary RAM, the stack location and\r
226 the BFV location.\r
b1f6a7c6 227 @param Private Pointer to the private data passed in from caller\r
192f6d4c 228\r
b1f6a7c6 229 @retval EFI_SUCCESS - Successfully dispatched PEIM.\r
230 @retval EFI_NOT_FOUND - The dispatch failed.\r
192f6d4c 231\r
b1f6a7c6 232**/\r
233VOID\r
234PeiDispatcher (\r
235 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
236 IN PEI_CORE_INSTANCE *Private\r
237 )\r
192f6d4c 238{\r
b0d803fe 239 EFI_STATUS Status;\r
240 UINT32 Index1;\r
241 UINT32 Index2;\r
242 EFI_PEI_SERVICES **PeiServices;\r
58dcdada 243 EFI_PEI_FV_HANDLE VolumeHandle;\r
b0d803fe 244 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
245 UINTN FvCount;\r
246 UINTN PeimCount;\r
247 UINT32 AuthenticationState;\r
248 EFI_PHYSICAL_ADDRESS EntryPoint;\r
797a9d67 249 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
b0d803fe 250 UINTN SaveCurrentPeimCount;\r
1053e0c5 251 UINTN SaveCurrentFvCount;\r
b0d803fe 252 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
797a9d67 253 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;\r
58dcdada 254 EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase;\r
255 TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
256 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable;\r
257 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable;\r
66c69dea 258 INTN StackOffset;\r
259 INTN HeapOffset;\r
58dcdada 260 PEI_CORE_INSTANCE *PrivateInMem;\r
261 UINT64 NewPeiStackSize;\r
262 UINT64 OldPeiStackSize;\r
263 UINT64 StackGap;\r
288f9b38 264 EFI_FV_FILE_INFO FvFileInfo;\r
58dcdada 265 UINTN OldCheckingTop;\r
266 UINTN OldCheckingBottom;\r
b0d803fe 267\r
268\r
269 PeiServices = &Private->PS;\r
270 PeimEntryPoint = NULL;\r
271 PeimFileHandle = NULL;\r
288f9b38 272 EntryPoint = 0;\r
b0d803fe 273\r
274 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
275 //\r
276 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
277 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
278 //\r
279 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
1053e0c5 280 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
b0d803fe 281 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
282\r
1053e0c5 283 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
177aabe6 284 for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
b0d803fe 285 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
58dcdada 286 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
b0d803fe 287 Status = PeiLoadImage (\r
58dcdada 288 &Private->PS,\r
289 PeimFileHandle,\r
290 &EntryPoint,\r
b0d803fe 291 &AuthenticationState\r
292 );\r
293 if (Status == EFI_SUCCESS) {\r
294 //\r
295 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
296 //\r
297 Private->Fv[Index1].PeimState[Index2]++;\r
1053e0c5 298 Private->CurrentFileHandle = PeimFileHandle;\r
58dcdada 299 Private->CurrentPeimFvCount = Index1;\r
300 Private->CurrentPeimCount = Index2;\r
b0d803fe 301 //\r
302 // Call the PEIM entry point\r
303 //\r
797a9d67 304 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
58dcdada 305\r
b0d803fe 306 PERF_START (0, "PEIM", NULL, 0);\r
797a9d67 307 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->PS);\r
b0d803fe 308 PERF_END (0, "PEIM", NULL, 0);\r
58dcdada 309 }\r
310\r
b0d803fe 311 //\r
312 // Process the Notify list and dispatch any notifies for\r
313 // newly installed PPIs.\r
314 //\r
315 ProcessNotifyList (Private);\r
316 }\r
317 }\r
318 }\r
58dcdada 319 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
320 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
321 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
b0d803fe 322 }\r
192f6d4c 323\r
324 //\r
325 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
326 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
327 // pass of the dispatcher, it will start over from the Bfv again to see\r
328 // if any new PEIMs dependencies got satisfied. With a well ordered\r
329 // FV where PEIMs are found in the order their dependencies are also\r
330 // satisfied, this dipatcher should run only once.\r
331 //\r
b0d803fe 332 do {\r
b0d803fe 333\r
334 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
335 Private->CurrentPeimFvCount = FvCount;\r
336 VolumeHandle = Private->Fv[FvCount].FvHeader;\r
192f6d4c 337\r
b0d803fe 338 if (Private->CurrentPeimCount == 0) {\r
339 //\r
340 // When going through each FV, at first, search Apriori file to\r
58dcdada 341 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
b0d803fe 342 // dispatch at first.\r
343 //\r
344 DiscoverPeimsAndOrderWithApriori (Private, VolumeHandle);\r
345 }\r
192f6d4c 346\r
347 //\r
b0d803fe 348 // Start to dispatch all modules within the current Fv.\r
192f6d4c 349 //\r
58dcdada 350 for (PeimCount = Private->CurrentPeimCount;\r
351 (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
b0d803fe 352 PeimCount++) {\r
353 Private->CurrentPeimCount = PeimCount;\r
354 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
355\r
356 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
357 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
1762b989 358 Private->PeimNeedingDispatch = TRUE;\r
b0d803fe 359 } else {\r
288f9b38
LG
360 Status = PeiFfsGetFileInfo (PeimFileHandle, &FvFileInfo);\r
361 ASSERT_EFI_ERROR (Status);\r
362 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
363 //\r
364 // For Fv type file, Produce new FV PPI and FV hob\r
365 //\r
366 Status = ProcessFvFile (PeiServices, PeimFileHandle, &AuthenticationState);\r
367 } else {\r
368 //\r
369 // For PEIM driver, Load its entry point\r
370 //\r
371 Status = PeiLoadImage (\r
58dcdada 372 PeiServices,\r
373 PeimFileHandle,\r
374 &EntryPoint,\r
288f9b38
LG
375 &AuthenticationState\r
376 );\r
377 }\r
378\r
b0d803fe 379 if ((Status == EFI_SUCCESS)) {\r
192f6d4c 380 //\r
58dcdada 381 // The PEIM has its dependencies satisfied, and its entry point\r
382 // has been found, so invoke it.\r
192f6d4c 383 //\r
b0d803fe 384 PERF_START (0, "PEIM", NULL, 0);\r
192f6d4c 385\r
b0d803fe 386 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
192f6d4c 387\r
388 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
389 EFI_PROGRESS_CODE,\r
797a9d67 390 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),\r
192f6d4c 391 (VOID *)(&ExtendedData),\r
392 sizeof (ExtendedData)\r
393 );\r
394\r
b0d803fe 395 Status = VerifyPeim (Private, VolumeHandle, PeimFileHandle);\r
396 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {\r
397 //\r
398 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
399 //\r
400 Private->Fv[FvCount].PeimState[PeimCount]++;\r
58dcdada 401\r
288f9b38
LG
402 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
403 //\r
404 // Call the PEIM entry point for PEIM driver\r
405 //\r
797a9d67 406 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
407 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
288f9b38 408 }\r
797a9d67 409\r
1762b989 410 Private->PeimDispatchOnThisPass = TRUE;\r
192f6d4c 411 }\r
412\r
413 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
414 EFI_PROGRESS_CODE,\r
797a9d67 415 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),\r
192f6d4c 416 (VOID *)(&ExtendedData),\r
417 sizeof (ExtendedData)\r
418 );\r
b0d803fe 419 PERF_END (0, "PEIM", NULL, 0);\r
420\r
58dcdada 421 }\r
422\r
423 if (Private->SwitchStackSignal) {\r
a7715e73 424 //\r
425 // Before switch stack from CAR to permenent memory, caculate the heap and stack\r
426 // usage in temporary memory for debuging.\r
427 //\r
428 DEBUG_CODE_BEGIN ();\r
96317468 429 UINT32 *StackPointer;\r
a7715e73 430 \r
96317468 431 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
432 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
a7715e73 433 && (*StackPointer == INIT_CAR_VALUE);\r
434 StackPointer ++);\r
435 \r
436 DEBUG ((EFI_D_INFO, "Total Cache as RAM: %d bytes.\n", SecCoreData->TemporaryRamSize));\r
437 DEBUG ((EFI_D_INFO, " CAR stack ever used: %d bytes.\n",\r
438 (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
439 ));\r
440 DEBUG ((EFI_D_INFO, " CAR heap used: %d bytes.\n",\r
441 ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -\r
442 (UINTN) Private->HobList.Raw)\r
443 ));\r
444 DEBUG_CODE_END ();\r
445 \r
a3a15d21 446 //\r
58dcdada 447 // Reserve the size of new stack at bottom of physical memory\r
a3a15d21 448 //\r
58dcdada 449 OldPeiStackSize = Private->StackSize;\r
450 NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
451 if (FixedPcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {\r
452 Private->StackSize = NewPeiStackSize;\r
453 } else {\r
454 Private->StackSize = FixedPcdGet32(PcdPeiCoreMaxPeiStackSize);\r
455 }\r
192f6d4c 456\r
58dcdada 457 //\r
458 // In theory, the size of new stack in permenent memory should large than\r
459 // size of old stack in temporary memory.\r
460 // But if new stack is smaller than the size of old stack, we also reserve\r
461 // the size of old stack at bottom of permenent memory.\r
462 //\r
463 StackGap = 0;\r
464 if (Private->StackSize > OldPeiStackSize) {\r
465 StackGap = Private->StackSize - OldPeiStackSize;\r
466 }\r
d74eeda8 467\r
58dcdada 468 //\r
469 // Update HandOffHob for new installed permenent memory\r
470 //\r
471 OldHandOffTable = Private->HobList.HandoffInformationTable;\r
5c5a0601 472 OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);\r
58dcdada 473 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
192f6d4c 474\r
475 //\r
58dcdada 476 // The whole temporary memory will be migrated to physical memory.\r
477 // CAUTION: The new base is computed accounding to gap of new stack.\r
192f6d4c 478 //\r
58dcdada 479 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
40f26b8f 480 \r
481 //\r
482 // Caculate stack offset and heap offset between CAR and new permement \r
483 // memory seperately.\r
484 //\r
d74eeda8 485 StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;\r
66c69dea 486 HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \\r
487 (UINTN) SecCoreData->PeiTemporaryRamBase);\r
488 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%X Stack Offset = 0x%X\n", HeapOffset, StackOffset));\r
489 \r
40f26b8f 490 //\r
491 // Caculate new HandOffTable and PrivateData address in permenet memory's stack\r
492 //\r
66c69dea 493 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);\r
494 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);\r
192f6d4c 495\r
496 //\r
58dcdada 497 // TemporaryRamSupportPpi is produced by platform's SEC\r
192f6d4c 498 //\r
58dcdada 499 Status = PeiLocatePpi (\r
500 (CONST EFI_PEI_SERVICES **) PeiServices,\r
501 &gEfiTemporaryRamSupportPpiGuid,\r
502 0,\r
503 NULL,\r
504 (VOID**)&TemporaryRamSupportPpi\r
505 );\r
506\r
81c7803c 507\r
58dcdada 508 if (!EFI_ERROR (Status)) {\r
40f26b8f 509 //\r
510 // Temporary Ram support Ppi is provided by platform, it will copy \r
511 // temporary memory to permenent memory and do stack switching.\r
512 // After invoken temporary Ram support, following code's stack is in \r
513 // memory but not in CAR.\r
514 //\r
58dcdada 515 TemporaryRamSupportPpi->TemporaryRamMigration (\r
516 (CONST EFI_PEI_SERVICES **) PeiServices,\r
517 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
518 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
519 SecCoreData->TemporaryRamSize\r
520 );\r
521\r
522 } else {\r
b414ea4b 523 //\r
524 // In IA32/x64/Itanium architecture, we need platform provide\r
525 // TEMPORAY_RAM_MIGRATION_PPI.\r
526 //\r
527 ASSERT (FALSE);\r
58dcdada 528 }\r
529\r
530\r
531 //\r
b0d803fe 532 //\r
58dcdada 533 // Fixup the PeiCore's private data\r
b0d803fe 534 //\r
58dcdada 535 PrivateInMem->PS = &PrivateInMem->ServiceTableShadow;\r
536 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;\r
66c69dea 537 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);\r
58dcdada 538 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
539\r
540 PeiServices = &PrivateInMem->PS;\r
541\r
542 //\r
543 // Fixup for PeiService's address\r
544 //\r
545 SetPeiServicesTablePointer(PeiServices);\r
546\r
547 //\r
548 // Update HandOffHob for new installed permenent memory\r
549 //\r
550 NewHandOffTable->EfiEndOfHobList =\r
66c69dea 551 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);\r
58dcdada 552 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +\r
553 PrivateInMem->PhysicalMemoryLength;\r
554 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;\r
555 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;\r
556 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
557 sizeof (EFI_HOB_GENERIC_HEADER);\r
558\r
559 //\r
560 // We need convert the PPI desciptor's pointer\r
561 //\r
40f26b8f 562 ConvertPpiPointers (PrivateInMem, \r
58dcdada 563 OldCheckingBottom, \r
564 OldCheckingTop, \r
5c5a0601 565 HeapOffset\r
566 );\r
58dcdada 567\r
568 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%X Length=0x%X\n",\r
569 (UINTN)PrivateInMem->StackBase,\r
570 PrivateInMem->StackSize));\r
571 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
572\r
573 //\r
574 // After the whole temporary memory is migrated, then we can allocate page in\r
575 // permenent memory.\r
576 //\r
577 PrivateInMem->PeiMemoryInstalled = TRUE;\r
578\r
192f6d4c 579 //\r
b0d803fe 580 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
581 // PEI Core and PEIMs to get high performance.\r
192f6d4c 582 //\r
58dcdada 583 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
584 PeiServices,\r
585 PrivateInMem\r
586 );\r
b0d803fe 587 //\r
58dcdada 588 // Process the Notify list and dispatch any notifies for\r
589 // newly installed PPIs.\r
b0d803fe 590 //\r
58dcdada 591 ProcessNotifyList (PrivateInMem);\r
592\r
b0d803fe 593 //\r
58dcdada 594 // Entry PEI Phase 2\r
b0d803fe 595 //\r
58dcdada 596 PeiCore (SecCoreData, NULL, PrivateInMem);\r
b0d803fe 597\r
58dcdada 598 //\r
599 // Code should not come here\r
600 //\r
601 ASSERT_EFI_ERROR(FALSE);\r
192f6d4c 602 }\r
192f6d4c 603\r
58dcdada 604 //\r
605 // Process the Notify list and dispatch any notifies for\r
606 // newly installed PPIs.\r
607 //\r
608 ProcessNotifyList (Private);\r
609\r
b0d803fe 610 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
611 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
612 //\r
58dcdada 613 // If memory is availble we shadow images by default for performance reasons.\r
614 // We call the entry point a 2nd time so the module knows it's shadowed.\r
b0d803fe 615 //\r
616 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
797a9d67 617 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
b0d803fe 618 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
58dcdada 619\r
b0d803fe 620 //\r
621 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
622 //\r
623 Private->Fv[FvCount].PeimState[PeimCount]++;\r
192f6d4c 624\r
192f6d4c 625 //\r
b0d803fe 626 // Process the Notify list and dispatch any notifies for\r
627 // newly installed PPIs.\r
192f6d4c 628 //\r
b0d803fe 629 ProcessNotifyList (Private);\r
192f6d4c 630 }\r
631 }\r
632 }\r
192f6d4c 633 }\r
192f6d4c 634\r
b0d803fe 635 //\r
636 // We set to NULL here to optimize the 2nd entry to this routine after\r
637 // memory is found. This reprevents rescanning of the FV. We set to\r
638 // NULL here so we start at the begining of the next FV\r
639 //\r
640 Private->CurrentFileHandle = NULL;\r
641 Private->CurrentPeimCount = 0;\r
642 //\r
643 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
644 //\r
645 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
192f6d4c 646 }\r
647\r
648 //\r
58dcdada 649 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
b0d803fe 650 // through all the FV.\r
192f6d4c 651 //\r
b0d803fe 652 Private->CurrentPeimFvCount = 0;\r
192f6d4c 653\r
654 //\r
58dcdada 655 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
b0d803fe 656 // dispatched. So we need to make another pass\r
192f6d4c 657 //\r
58dcdada 658 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
b0d803fe 659 // pass. If we did not dispatch a PEIM there is no point in trying again\r
660 // as it will fail the next time too (nothing has changed).\r
192f6d4c 661 //\r
1762b989 662 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
192f6d4c 663\r
192f6d4c 664}\r
665\r
b1f6a7c6 666/**\r
192f6d4c 667 Initialize the Dispatcher's data members\r
668\r
b1f6a7c6 669 @param PrivateData PeiCore's private data structure\r
670 @param OldCoreData Old data from SecCore\r
192f6d4c 671 NULL if being run in non-permament memory mode.\r
b1f6a7c6 672 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 673 environment, such as the size and location of temporary RAM, the stack location and\r
674 the BFV location.\r
192f6d4c 675\r
b1f6a7c6 676 @return None.\r
192f6d4c 677\r
b1f6a7c6 678**/\r
679VOID\r
680InitializeDispatcherData (\r
681 IN PEI_CORE_INSTANCE *PrivateData,\r
682 IN PEI_CORE_INSTANCE *OldCoreData,\r
683 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
684 )\r
192f6d4c 685{\r
192f6d4c 686 if (OldCoreData == NULL) {\r
1762b989 687 PrivateData->PeimNeedingDispatch = FALSE;\r
688 PrivateData->PeimDispatchOnThisPass = FALSE;\r
b0d803fe 689 PeiInitializeFv (PrivateData, SecCoreData);\r
192f6d4c 690 }\r
691\r
692 return;\r
693}\r
694\r
b1f6a7c6 695/**\r
696 This routine parses the Dependency Expression, if available, and\r
697 decides if the module can be executed.\r
698\r
699\r
700 @param Private PeiCore's private data structure\r
701 @param FileHandle PEIM's file handle\r
702 @param PeimCount Peim count in all dispatched PEIMs.\r
192f6d4c 703\r
b1f6a7c6 704 @retval TRUE Can be dispatched\r
705 @retval FALSE Cannot be dispatched\r
706\r
707**/\r
192f6d4c 708BOOLEAN\r
709DepexSatisfied (\r
b0d803fe 710 IN PEI_CORE_INSTANCE *Private,\r
711 IN EFI_PEI_FILE_HANDLE FileHandle,\r
712 IN UINTN PeimCount\r
192f6d4c 713 )\r
192f6d4c 714{\r
288f9b38
LG
715 EFI_STATUS Status;\r
716 VOID *DepexData;\r
b0d803fe 717\r
718 if (PeimCount < Private->AprioriCount) {\r
719 //\r
720 // If its in the A priori file then we set Depex to TRUE\r
721 //\r
722 return TRUE;\r
723 }\r
58dcdada 724\r
288f9b38 725 //\r
58dcdada 726 // Depex section not in the encapsulated section.\r
288f9b38
LG
727 //\r
728 Status = PeiServicesFfsFindSectionData (\r
729 EFI_SECTION_PEI_DEPEX,\r
58dcdada 730 FileHandle,\r
288f9b38
LG
731 (VOID **)&DepexData\r
732 );\r
b0d803fe 733\r
192f6d4c 734 if (EFI_ERROR (Status)) {\r
b0d803fe 735 //\r
736 // If there is no DEPEX, assume the module can be executed\r
737 //\r
192f6d4c 738 return TRUE;\r
739 }\r
740\r
741 //\r
742 // Evaluate a given DEPEX\r
743 //\r
b0d803fe 744 return PeimDispatchReadiness (&Private->PS, DepexData);\r
192f6d4c 745}\r
746\r
14e8823a 747/**\r
748 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
749 discovery permanent memory.\r
750\r
b1f6a7c6 751 @param FileHandle File handle of a PEIM.\r
58dcdada 752\r
b1f6a7c6 753 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
754 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
755 @retval EFI_SUCCESS Successfully to register itself.\r
14e8823a 756\r
58dcdada 757**/\r
14e8823a 758EFI_STATUS\r
759EFIAPI\r
760PeiRegisterForShadow (\r
761 IN EFI_PEI_FILE_HANDLE FileHandle\r
762 )\r
763{\r
764 PEI_CORE_INSTANCE *Private;\r
765 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
766\r
767 if (Private->CurrentFileHandle != FileHandle) {\r
768 //\r
769 // The FileHandle must be for the current PEIM\r
770 //\r
771 return EFI_NOT_FOUND;\r
772 }\r
773\r
774 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
775 //\r
776 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
777 //\r
778 return EFI_ALREADY_STARTED;\r
779 }\r
58dcdada 780\r
14e8823a 781 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
782\r
783 return EFI_SUCCESS;\r
784}\r
785\r
288f9b38
LG
786/**\r
787 Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.\r
788\r
d73d93c3 789 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
b1f6a7c6 790 @param FvFileHandle File handle of a Fv type file.\r
288f9b38
LG
791 @param AuthenticationState Pointer to attestation authentication state of image.\r
792\r
58dcdada 793\r
b1f6a7c6 794 @retval EFI_NOT_FOUND FV image can't be found.\r
795 @retval EFI_SUCCESS Successfully to process it.\r
288f9b38
LG
796\r
797**/\r
798EFI_STATUS\r
799ProcessFvFile (\r
800 IN EFI_PEI_SERVICES **PeiServices,\r
801 IN EFI_PEI_FILE_HANDLE FvFileHandle,\r
802 OUT UINT32 *AuthenticationState\r
803 )\r
804{\r
805 EFI_STATUS Status;\r
806 EFI_PEI_FV_HANDLE FvImageHandle;\r
807 EFI_FV_INFO FvImageInfo;\r
808 UINT32 FvAlignment;\r
809 VOID *FvBuffer;\r
b2821d6b 810 EFI_PEI_HOB_POINTERS HobPtr;\r
58dcdada 811\r
288f9b38
LG
812 FvBuffer = NULL;\r
813 *AuthenticationState = 0;\r
814\r
815 //\r
58dcdada 816 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
288f9b38
LG
817 // been extracted.\r
818 //\r
b2821d6b
LG
819 HobPtr.Raw = GetHobList ();\r
820 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {\r
821 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {\r
288f9b38
LG
822 //\r
823 // this FILE has been dispatched, it will not be dispatched again.\r
824 //\r
825 return EFI_SUCCESS;\r
826 }\r
b2821d6b 827 HobPtr.Raw = GET_NEXT_HOB (HobPtr);\r
288f9b38 828 }\r
58dcdada 829\r
288f9b38
LG
830 //\r
831 // Find FvImage in FvFile\r
832 //\r
833 Status = PeiFfsFindSectionData (\r
834 (CONST EFI_PEI_SERVICES **) PeiServices,\r
835 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
836 FvFileHandle,\r
837 (VOID **)&FvImageHandle\r
838 );\r
58dcdada 839\r
288f9b38
LG
840 if (EFI_ERROR (Status)) {\r
841 return Status;\r
842 }\r
843 //\r
844 // Collect FvImage Info.\r
845 //\r
846 Status = PeiFfsGetVolumeInfo (FvImageHandle, &FvImageInfo);\r
847 ASSERT_EFI_ERROR (Status);\r
848 //\r
849 // FvAlignment must be more than 8 bytes required by FvHeader structure.\r
850 //\r
851 FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16);\r
852 if (FvAlignment < 8) {\r
853 FvAlignment = 8;\r
854 }\r
58dcdada 855 //\r
288f9b38
LG
856 // Check FvImage\r
857 //\r
858 if ((UINTN) FvImageInfo.FvStart % FvAlignment != 0) {\r
859 FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvImageInfo.FvSize), FvAlignment);\r
860 if (FvBuffer == NULL) {\r
861 return EFI_OUT_OF_RESOURCES;\r
862 }\r
863 CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN) FvImageInfo.FvSize);\r
864 //\r
865 // Update FvImageInfo after reload FvImage to new aligned memory\r
866 //\r
867 PeiFfsGetVolumeInfo ((EFI_PEI_FV_HANDLE) FvBuffer, &FvImageInfo);\r
868 }\r
58dcdada 869\r
288f9b38
LG
870 //\r
871 // Install FvPpi and Build FvHob\r
872 //\r
873 PiLibInstallFvInfoPpi (\r
874 NULL,\r
875 FvImageInfo.FvStart,\r
876 (UINT32) FvImageInfo.FvSize,\r
877 &(FvImageInfo.FvName),\r
878 &(((EFI_FFS_FILE_HEADER*)FvFileHandle)->Name)\r
879 );\r
880\r
881 //\r
b2821d6b
LG
882 // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase\r
883 // based on its parent Fvimage is informed or not.\r
884 // If FvHob of its parent fvimage is built, the extracted FvImage will be built also. \r
885 // Or, the extracted FvImage will not be built.\r
288f9b38 886 //\r
b2821d6b
LG
887 HobPtr.Raw = GetHobList ();\r
888 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV, HobPtr.Raw)) != NULL) {\r
889 if (((EFI_PHYSICAL_ADDRESS) (UINTN)FvFileHandle > HobPtr.FirmwareVolume->BaseAddress) && \r
890 ((EFI_PHYSICAL_ADDRESS) (UINTN)FvFileHandle < HobPtr.FirmwareVolume->BaseAddress + HobPtr.FirmwareVolume->Length)) {\r
891 BuildFvHob (\r
892 (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
893 FvImageInfo.FvSize\r
894 );\r
895 break;\r
896 }\r
897 HobPtr.Raw = GET_NEXT_HOB (HobPtr);\r
898 }\r
899\r
288f9b38
LG
900 //\r
901 // Makes the encapsulated volume show up in DXE phase to skip processing of\r
902 // encapsulated file again.\r
903 //\r
904 BuildFv2Hob (\r
905 (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
906 FvImageInfo.FvSize,\r
907 &FvImageInfo.FvName,\r
908 &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name)\r
909 );\r
58dcdada 910\r
288f9b38
LG
911 return EFI_SUCCESS;\r
912}\r