]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Add change log since Edk r5418-5419 is somewhat not backward-compatible.
[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 Offset;\r
278 PEI_CORE_INSTANCE *PrivateInMem;\r
279 UINT64 NewPeiStackSize;\r
280 UINT64 OldPeiStackSize;\r
281 UINT64 StackGap;\r
282 EFI_FV_FILE_INFO FvFileInfo;\r
283 UINTN OldCheckingTop;\r
284 UINTN OldCheckingBottom;\r
285\r
286\r
287 PeiServices = &Private->PS;\r
288 PeimEntryPoint = NULL;\r
289 PeimFileHandle = NULL;\r
290 EntryPoint = 0;\r
291\r
292 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
293 //\r
294 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
295 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
296 //\r
297 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
298 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
299 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
300\r
301 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
302 for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
303 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
304 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
305 Status = PeiLoadImage (\r
306 &Private->PS,\r
307 PeimFileHandle,\r
308 &EntryPoint,\r
309 &AuthenticationState\r
310 );\r
311 if (Status == EFI_SUCCESS) {\r
312 //\r
313 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
314 //\r
315 Private->Fv[Index1].PeimState[Index2]++;\r
316 Private->CurrentFileHandle = PeimFileHandle;\r
317 Private->CurrentPeimFvCount = Index1;\r
318 Private->CurrentPeimCount = Index2;\r
319 //\r
320 // Call the PEIM entry point\r
321 //\r
322 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
323\r
324 PERF_START (0, "PEIM", NULL, 0);\r
325 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->PS);\r
326 PERF_END (0, "PEIM", NULL, 0);\r
327 }\r
328\r
329 //\r
330 // Process the Notify list and dispatch any notifies for\r
331 // newly installed PPIs.\r
332 //\r
333 ProcessNotifyList (Private);\r
334 }\r
335 }\r
336 }\r
337 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
338 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
339 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
340 }\r
341\r
342 //\r
343 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
344 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
345 // pass of the dispatcher, it will start over from the Bfv again to see\r
346 // if any new PEIMs dependencies got satisfied. With a well ordered\r
347 // FV where PEIMs are found in the order their dependencies are also\r
348 // satisfied, this dipatcher should run only once.\r
349 //\r
350 do {\r
351 PeimNeedingDispatch = FALSE;\r
352 PeimDispatchOnThisPass = FALSE;\r
353\r
354 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
355 Private->CurrentPeimFvCount = FvCount;\r
356 VolumeHandle = Private->Fv[FvCount].FvHeader;\r
357\r
358 if (Private->CurrentPeimCount == 0) {\r
359 //\r
360 // When going through each FV, at first, search Apriori file to\r
361 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
362 // dispatch at first.\r
363 //\r
364 DiscoverPeimsAndOrderWithApriori (Private, VolumeHandle);\r
365 }\r
366\r
367 //\r
368 // Start to dispatch all modules within the current Fv.\r
369 //\r
370 for (PeimCount = Private->CurrentPeimCount;\r
371 (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
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
378 PeimNeedingDispatch = TRUE;\r
379 } else {\r
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
386 Status = ProcessFvFile (PeiServices, PeimFileHandle, &AuthenticationState);\r
387 } else {\r
388 //\r
389 // For PEIM driver, Load its entry point\r
390 //\r
391 Status = PeiLoadImage (\r
392 PeiServices,\r
393 PeimFileHandle,\r
394 &EntryPoint,\r
395 &AuthenticationState\r
396 );\r
397 }\r
398\r
399 if ((Status == EFI_SUCCESS)) {\r
400 //\r
401 // The PEIM has its dependencies satisfied, and its entry point\r
402 // has been found, so invoke it.\r
403 //\r
404 PERF_START (0, "PEIM", NULL, 0);\r
405\r
406 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
407\r
408 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
409 EFI_PROGRESS_CODE,\r
410 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),\r
411 (VOID *)(&ExtendedData),\r
412 sizeof (ExtendedData)\r
413 );\r
414\r
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
421\r
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
426 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
427 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
428 }\r
429\r
430 PeimDispatchOnThisPass = TRUE;\r
431 }\r
432\r
433 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
434 EFI_PROGRESS_CODE,\r
435 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),\r
436 (VOID *)(&ExtendedData),\r
437 sizeof (ExtendedData)\r
438 );\r
439 PERF_END (0, "PEIM", NULL, 0);\r
440\r
441 }\r
442\r
443 if (Private->SwitchStackSignal) {\r
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
449 UINT32 *StackPointer;\r
450 \r
451 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
452 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
453 && (*StackPointer == INIT_CAR_VALUE);\r
454 StackPointer ++);\r
455 \r
456 DEBUG ((EFI_D_INFO, "Total Cache as RAM: %d bytes.\n", SecCoreData->TemporaryRamSize));\r
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
466 //\r
467 // Reserve the size of new stack at bottom of physical memory\r
468 //\r
469 OldPeiStackSize = Private->StackSize;\r
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
476\r
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
483 StackGap = 0;\r
484 if (Private->StackSize > OldPeiStackSize) {\r
485 StackGap = Private->StackSize - OldPeiStackSize;\r
486 }\r
487\r
488 //\r
489 // Update HandOffHob for new installed permenent memory\r
490 //\r
491 OldHandOffTable = Private->HobList.HandoffInformationTable;\r
492 OldCheckingBottom = (UINTN)OldHandOffTable;\r
493 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
494\r
495 //\r
496 // The whole temporary memory will be migrated to physical memory.\r
497 // CAUTION: The new base is computed accounding to gap of new stack.\r
498 //\r
499 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
500 Offset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->TemporaryRamBase;\r
501 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + Offset);\r
502 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + Offset);\r
503\r
504 //\r
505 // TemporaryRamSupportPpi is produced by platform's SEC\r
506 //\r
507 Status = PeiLocatePpi (\r
508 (CONST EFI_PEI_SERVICES **) PeiServices,\r
509 &gEfiTemporaryRamSupportPpiGuid,\r
510 0,\r
511 NULL,\r
512 (VOID**)&TemporaryRamSupportPpi\r
513 );\r
514\r
515\r
516 if (!EFI_ERROR (Status)) {\r
517 TemporaryRamSupportPpi->TemporaryRamMigration (\r
518 (CONST EFI_PEI_SERVICES **) PeiServices,\r
519 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
520 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
521 SecCoreData->TemporaryRamSize\r
522 );\r
523\r
524 } else {\r
525 CopyMem (\r
526 (VOID*)(UINTN) NewPermenentMemoryBase,\r
527 SecCoreData->TemporaryRamBase,\r
528 SecCoreData->TemporaryRamSize\r
529 );\r
530 }\r
531\r
532\r
533 //\r
534 //\r
535 // Fixup the PeiCore's private data\r
536 //\r
537 PrivateInMem->PS = &PrivateInMem->ServiceTableShadow;\r
538 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;\r
539 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + Offset);\r
540 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
541\r
542 PeiServices = &PrivateInMem->PS;\r
543\r
544 //\r
545 // Fixup for PeiService's address\r
546 //\r
547 SetPeiServicesTablePointer(PeiServices);\r
548\r
549 //\r
550 // Update HandOffHob for new installed permenent memory\r
551 //\r
552 NewHandOffTable->EfiEndOfHobList =\r
553 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + Offset);\r
554 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +\r
555 PrivateInMem->PhysicalMemoryLength;\r
556 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;\r
557 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;\r
558 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
559 sizeof (EFI_HOB_GENERIC_HEADER);\r
560\r
561 //\r
562 // We need convert the PPI desciptor's pointer\r
563 //\r
564 ConvertPpiPointers ((CONST EFI_PEI_SERVICES **)PeiServices, \r
565 OldCheckingBottom, \r
566 OldCheckingTop, \r
567 NewHandOffTable);\r
568\r
569 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%X Length=0x%X\n",\r
570 (UINTN)PrivateInMem->StackBase,\r
571 PrivateInMem->StackSize));\r
572 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
573\r
574 //\r
575 // After the whole temporary memory is migrated, then we can allocate page in\r
576 // permenent memory.\r
577 //\r
578 PrivateInMem->PeiMemoryInstalled = TRUE;\r
579\r
580 //\r
581 // Restart scan of all PEIMs on next pass\r
582 //\r
583 PrivateInMem->CurrentPeimCount = 0;\r
584\r
585 //\r
586 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
587 // PEI Core and PEIMs to get high performance.\r
588 //\r
589 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
590 PeiServices,\r
591 PrivateInMem\r
592 );\r
593 //\r
594 // Process the Notify list and dispatch any notifies for\r
595 // newly installed PPIs.\r
596 //\r
597 ProcessNotifyList (PrivateInMem);\r
598\r
599 //\r
600 // Entry PEI Phase 2\r
601 //\r
602 PeiCore (SecCoreData, NULL, PrivateInMem);\r
603\r
604 //\r
605 // Code should not come here\r
606 //\r
607 ASSERT_EFI_ERROR(FALSE);\r
608 }\r
609\r
610 //\r
611 // Process the Notify list and dispatch any notifies for\r
612 // newly installed PPIs.\r
613 //\r
614 ProcessNotifyList (Private);\r
615\r
616 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
617 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
618 //\r
619 // If memory is availble we shadow images by default for performance reasons.\r
620 // We call the entry point a 2nd time so the module knows it's shadowed.\r
621 //\r
622 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
623 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
624 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
625\r
626 //\r
627 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
628 //\r
629 Private->Fv[FvCount].PeimState[PeimCount]++;\r
630\r
631 //\r
632 // Process the Notify list and dispatch any notifies for\r
633 // newly installed PPIs.\r
634 //\r
635 ProcessNotifyList (Private);\r
636 }\r
637 }\r
638 }\r
639 }\r
640\r
641 //\r
642 // We set to NULL here to optimize the 2nd entry to this routine after\r
643 // memory is found. This reprevents rescanning of the FV. We set to\r
644 // NULL here so we start at the begining of the next FV\r
645 //\r
646 Private->CurrentFileHandle = NULL;\r
647 Private->CurrentPeimCount = 0;\r
648 //\r
649 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
650 //\r
651 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
652 }\r
653\r
654 //\r
655 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
656 // through all the FV.\r
657 //\r
658 Private->CurrentPeimFvCount = 0;\r
659\r
660 //\r
661 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
662 // dispatched. So we need to make another pass\r
663 //\r
664 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
665 // pass. If we did not dispatch a PEIM there is no point in trying again\r
666 // as it will fail the next time too (nothing has changed).\r
667 //\r
668 } while (PeimNeedingDispatch && PeimDispatchOnThisPass);\r
669\r
670}\r
671\r
672VOID\r
673InitializeDispatcherData (\r
674 IN PEI_CORE_INSTANCE *PrivateData,\r
675 IN PEI_CORE_INSTANCE *OldCoreData,\r
676 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
677 )\r
678/*++\r
679\r
680Routine Description:\r
681\r
682 Initialize the Dispatcher's data members\r
683\r
684Arguments:\r
685\r
686 PeiServices - The PEI core services table.\r
687 OldCoreData - Pointer to old core data (before switching stack).\r
688 NULL if being run in non-permament memory mode.\r
689 SecCoreData - Points to a data structure containing information about the PEI core's operating\r
690 environment, such as the size and location of temporary RAM, the stack location and\r
691 the BFV location.\r
692\r
693Returns:\r
694\r
695 None.\r
696\r
697--*/\r
698{\r
699 if (OldCoreData == NULL) {\r
700 PeiInitializeFv (PrivateData, SecCoreData);\r
701 }\r
702\r
703 return;\r
704}\r
705\r
706\r
707BOOLEAN\r
708DepexSatisfied (\r
709 IN PEI_CORE_INSTANCE *Private,\r
710 IN EFI_PEI_FILE_HANDLE FileHandle,\r
711 IN UINTN PeimCount\r
712 )\r
713/*++\r
714\r
715Routine Description:\r
716\r
717 This routine parses the Dependency Expression, if available, and\r
718 decides if the module can be executed.\r
719\r
720Arguments:\r
721 PeiServices - The PEI Service Table\r
722 CurrentPeimAddress - Address of the PEIM Firmware File under investigation\r
723\r
724Returns:\r
725 TRUE - Can be dispatched\r
726 FALSE - Cannot be dispatched\r
727\r
728--*/\r
729{\r
730 EFI_STATUS Status;\r
731 VOID *DepexData;\r
732\r
733 if (PeimCount < Private->AprioriCount) {\r
734 //\r
735 // If its in the A priori file then we set Depex to TRUE\r
736 //\r
737 return TRUE;\r
738 }\r
739\r
740 //\r
741 // Depex section not in the encapsulated section.\r
742 //\r
743 Status = PeiServicesFfsFindSectionData (\r
744 EFI_SECTION_PEI_DEPEX,\r
745 FileHandle,\r
746 (VOID **)&DepexData\r
747 );\r
748\r
749 if (EFI_ERROR (Status)) {\r
750 //\r
751 // If there is no DEPEX, assume the module can be executed\r
752 //\r
753 return TRUE;\r
754 }\r
755\r
756 //\r
757 // Evaluate a given DEPEX\r
758 //\r
759 return PeimDispatchReadiness (&Private->PS, DepexData);\r
760}\r
761\r
762/**\r
763 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
764 discovery permanent memory.\r
765\r
766 @param FileHandle File handle of a PEIM.\r
767\r
768 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
769 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
770 @retval EFI_SUCCESS Successfully to register itself.\r
771\r
772**/\r
773EFI_STATUS\r
774EFIAPI\r
775PeiRegisterForShadow (\r
776 IN EFI_PEI_FILE_HANDLE FileHandle\r
777 )\r
778{\r
779 PEI_CORE_INSTANCE *Private;\r
780 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
781\r
782 if (Private->CurrentFileHandle != FileHandle) {\r
783 //\r
784 // The FileHandle must be for the current PEIM\r
785 //\r
786 return EFI_NOT_FOUND;\r
787 }\r
788\r
789 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
790 //\r
791 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
792 //\r
793 return EFI_ALREADY_STARTED;\r
794 }\r
795\r
796 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
797\r
798 return EFI_SUCCESS;\r
799}\r
800\r
801\r
802\r
803/**\r
804 Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.\r
805\r
806 @param PeiServices Pointer to the PEI Core Services Table.\r
807 @param FileHandle File handle of a Fv type file.\r
808 @param AuthenticationState Pointer to attestation authentication state of image.\r
809\r
810\r
811 @retval EFI_NOT_FOUND FV image can't be found.\r
812 @retval EFI_SUCCESS Successfully to process it.\r
813\r
814**/\r
815EFI_STATUS\r
816ProcessFvFile (\r
817 IN EFI_PEI_SERVICES **PeiServices,\r
818 IN EFI_PEI_FILE_HANDLE FvFileHandle,\r
819 OUT UINT32 *AuthenticationState\r
820 )\r
821{\r
822 EFI_STATUS Status;\r
823 EFI_PEI_FV_HANDLE FvImageHandle;\r
824 EFI_FV_INFO FvImageInfo;\r
825 UINT32 FvAlignment;\r
826 VOID *FvBuffer;\r
827 EFI_PEI_HOB_POINTERS HobFv2;\r
828\r
829 FvBuffer = NULL;\r
830 *AuthenticationState = 0;\r
831\r
832 //\r
833 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
834 // been extracted.\r
835 //\r
836 HobFv2.Raw = GetHobList ();\r
837 while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {\r
838 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobFv2.FirmwareVolume2->FileName)) {\r
839 //\r
840 // this FILE has been dispatched, it will not be dispatched again.\r
841 //\r
842 return EFI_SUCCESS;\r
843 }\r
844 HobFv2.Raw = GET_NEXT_HOB (HobFv2);\r
845 }\r
846\r
847 //\r
848 // Find FvImage in FvFile\r
849 //\r
850 Status = PeiFfsFindSectionData (\r
851 (CONST EFI_PEI_SERVICES **) PeiServices,\r
852 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
853 FvFileHandle,\r
854 (VOID **)&FvImageHandle\r
855 );\r
856\r
857 if (EFI_ERROR (Status)) {\r
858 return Status;\r
859 }\r
860 //\r
861 // Collect FvImage Info.\r
862 //\r
863 Status = PeiFfsGetVolumeInfo (FvImageHandle, &FvImageInfo);\r
864 ASSERT_EFI_ERROR (Status);\r
865 //\r
866 // FvAlignment must be more than 8 bytes required by FvHeader structure.\r
867 //\r
868 FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16);\r
869 if (FvAlignment < 8) {\r
870 FvAlignment = 8;\r
871 }\r
872 //\r
873 // Check FvImage\r
874 //\r
875 if ((UINTN) FvImageInfo.FvStart % FvAlignment != 0) {\r
876 FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvImageInfo.FvSize), FvAlignment);\r
877 if (FvBuffer == NULL) {\r
878 return EFI_OUT_OF_RESOURCES;\r
879 }\r
880 CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN) FvImageInfo.FvSize);\r
881 //\r
882 // Update FvImageInfo after reload FvImage to new aligned memory\r
883 //\r
884 PeiFfsGetVolumeInfo ((EFI_PEI_FV_HANDLE) FvBuffer, &FvImageInfo);\r
885 }\r
886\r
887 //\r
888 // Install FvPpi and Build FvHob\r
889 //\r
890 PiLibInstallFvInfoPpi (\r
891 NULL,\r
892 FvImageInfo.FvStart,\r
893 (UINT32) FvImageInfo.FvSize,\r
894 &(FvImageInfo.FvName),\r
895 &(((EFI_FFS_FILE_HEADER*)FvFileHandle)->Name)\r
896 );\r
897\r
898 //\r
899 // Inform HOB consumer phase, i.e. DXE core, the existance of this FV\r
900 //\r
901 BuildFvHob (\r
902 (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
903 FvImageInfo.FvSize\r
904 );\r
905 //\r
906 // Makes the encapsulated volume show up in DXE phase to skip processing of\r
907 // encapsulated file again.\r
908 //\r
909 BuildFv2Hob (\r
910 (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
911 FvImageInfo.FvSize,\r
912 &FvImageInfo.FvName,\r
913 &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name)\r
914 );\r
915\r
916 return EFI_SUCCESS;\r
917}\r