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