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