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