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
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.
18 EFI PEI Core dispatch services
35 DiscoverPeimsAndOrderWithApriori (
36 IN PEI_CORE_INSTANCE
*Private
,
37 IN EFI_PEI_FV_HANDLE VolumeHandle
43 Discover all Peims and optional Apriori file in one FV. There is at most one
44 Apriori file in one FV.
48 Private - Pointer to the private data passed in from caller
49 VolumeHandle - Fv handle.
57 EFI_PEI_FV_HANDLE FileHandle
;
58 EFI_PEI_FV_HANDLE AprioriFileHandle
;
65 EFI_PEI_FV_HANDLE TempFileHandles
[PEI_CORE_MAX_PEIM_PER_FV
];
66 EFI_GUID FileGuid
[PEI_CORE_MAX_PEIM_PER_FV
];
69 // Walk the FV and find all the PEIMs and the Apriori file.
71 AprioriFileHandle
= NULL
;
72 Private
->CurrentFvFileHandles
[0] = NULL
;
77 // If the current Fv has been scanned, directly get its cachable record.
79 if (Private
->Fv
[Private
->CurrentPeimFvCount
].ScanFv
) {
80 CopyMem (Private
->CurrentFvFileHandles
, Private
->Fv
[Private
->CurrentPeimFvCount
].FvFileHandles
, sizeof (Private
->CurrentFvFileHandles
));
85 // Go ahead to scan this Fv, and cache FileHandles within it.
87 for (PeimCount
= 0; PeimCount
< PEI_CORE_MAX_PEIM_PER_FV
; PeimCount
++) {
88 Status
= PeiFindFileEx (
91 PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE
,
95 if (Status
!= EFI_SUCCESS
) {
99 Private
->CurrentFvFileHandles
[PeimCount
] = FileHandle
;
102 Private
->AprioriCount
= 0;
103 if (AprioriFileHandle
!= NULL
) {
105 // Read the Apriori file
107 Status
= PeiServicesFfsFindSectionData (EFI_SECTION_RAW
, &AprioriFileHandle
, (VOID
**) &Apriori
);
108 if (!EFI_ERROR (Status
)) {
110 // Calculate the number of PEIMs in the A Priori list
112 Private
->AprioriCount
= *(UINT32
*)(((EFI_FFS_FILE_HEADER
*)AprioriFileHandle
)->Size
) & 0x00FFFFFF;
113 Private
->AprioriCount
-= sizeof (EFI_FFS_FILE_HEADER
) - sizeof (EFI_COMMON_SECTION_HEADER
);
114 Private
->AprioriCount
/= sizeof (EFI_GUID
);
116 SetMem (FileGuid
, sizeof (FileGuid
), 0);
117 for (Index
= 0; Index
< PeimCount
; Index
++) {
119 // Make an array of file name guids that matches the FileHandle array so we can convert
120 // quickly from file name to file handle
122 CopyMem (&FileGuid
[Index
], &((EFI_FFS_FILE_HEADER
*)Private
->CurrentFvFileHandles
[Index
])->Name
,sizeof(EFI_GUID
));
126 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.
127 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.
130 for (Index
= 0; Index2
< Private
->AprioriCount
; Index
++) {
131 while (Index2
< Private
->AprioriCount
) {
132 Guid
= ScanGuid (FileGuid
, PeimCount
* sizeof (EFI_GUID
), &Apriori
[Index2
++]);
140 PeimIndex
= ((UINTN
)Guid
- (UINTN
)&FileGuid
[0])/sizeof (EFI_GUID
);
141 TempFileHandles
[Index
] = Private
->CurrentFvFileHandles
[PeimIndex
];
144 // Since we have copied the file handle we can remove it from this list.
146 Private
->CurrentFvFileHandles
[PeimIndex
] = NULL
;
150 // Update valid Aprioricount
152 Private
->AprioriCount
= Index
;
155 // Add in any PEIMs not in the Apriori file
157 for (;Index
< PeimCount
; Index
++) {
158 for (Index2
= 0; Index2
< PeimCount
; Index2
++) {
159 if (Private
->CurrentFvFileHandles
[Index2
] != NULL
) {
160 TempFileHandles
[Index
] = Private
->CurrentFvFileHandles
[Index2
];
161 Private
->CurrentFvFileHandles
[Index2
] = NULL
;
167 //Index the end of array contains re-range Pei moudle.
169 TempFileHandles
[Index
] = NULL
;
172 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.
173 // We need to update it to start with files in the A Priori list and
174 // then the remaining files in PEIM order.
176 CopyMem (Private
->CurrentFvFileHandles
, TempFileHandles
, sizeof (Private
->CurrentFvFileHandles
));
180 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.
181 // Instead, we can retrieve the file handles within this Fv from cachable data.
183 Private
->Fv
[Private
->CurrentPeimFvCount
].ScanFv
= TRUE
;
184 CopyMem (Private
->Fv
[Private
->CurrentPeimFvCount
].FvFileHandles
, Private
->CurrentFvFileHandles
, sizeof (Private
->CurrentFvFileHandles
));
190 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
191 IN PEI_CORE_INSTANCE
*Private
198 Conduct PEIM dispatch.
202 SecCoreData - Points to a data structure containing information about the PEI core's operating
203 environment, such as the size and location of temporary RAM, the stack location and
205 PrivateData - Pointer to the private data passed in from caller
206 DispatchData - Pointer to PEI_CORE_DISPATCH_DATA data.
210 EFI_SUCCESS - Successfully dispatched PEIM.
211 EFI_NOT_FOUND - The dispatch failed.
218 EFI_PEI_SERVICES
**PeiServices
;
220 EFI_PEI_FV_HANDLE VolumeHandle
;
221 EFI_PEI_FILE_HANDLE PeiCoreFileHandle
;
222 EFI_PEI_FILE_HANDLE PeimFileHandle
;
225 UINT32 AuthenticationState
;
226 EFI_PHYSICAL_ADDRESS EntryPoint
;
227 EFI_PEIM_ENTRY_POINT PeimEntryPoint
;
228 BOOLEAN PeimNeedingDispatch
;
229 BOOLEAN PeimDispatchOnThisPass
;
230 UINTN SaveCurrentPeimCount
;
231 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle
;
233 PEI_CORE_PARAMETERS PeiCoreParameters
;
234 EFI_DEVICE_HANDLE_EXTENDED_DATA ExtendedData
;
237 PeiServices
= &Private
->PS
;
238 PeimEntryPoint
= NULL
;
239 PeimFileHandle
= NULL
;
241 if ((Private
->PeiMemoryInstalled
) && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
243 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile
244 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.
246 SaveCurrentPeimCount
= Private
->CurrentPeimCount
;
247 SaveCurrentFileHandle
= Private
->CurrentFileHandle
;
249 for (Index1
= 0;Index1
<= Private
->CurrentPeimFvCount
; Index1
++) {
250 for (Index2
= 0; (Index2
< PEI_CORE_MAX_PEIM_PER_FV
) && (Private
->Fv
[Index1
].FvFileHandles
[Index2
] != NULL
); Index2
++) {
251 if (Private
->Fv
[Index1
].PeimState
[Index2
] == PEIM_STATE_REGISITER_FOR_SHADOW
) {
252 PeimFileHandle
= Private
->Fv
[Index1
].FvFileHandles
[Index2
];
253 Status
= PeiLoadImage (
259 if (Status
== EFI_SUCCESS
) {
261 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
263 Private
->Fv
[Index1
].PeimState
[Index2
]++;
264 Private
->CurrentFileHandle
= PeimFileHandle
;
265 Private
->CurrentPeimCount
= Index2
;
267 // Call the PEIM entry point
269 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT
)(UINTN
)EntryPoint
;
271 PERF_START (0, "PEIM", NULL
, 0);
272 PeimEntryPoint(PeimFileHandle
, &Private
->PS
);
273 PERF_END (0, "PEIM", NULL
, 0);
277 // Process the Notify list and dispatch any notifies for
278 // newly installed PPIs.
280 ProcessNotifyList (Private
);
284 Private
->CurrentFileHandle
= SaveCurrentFileHandle
;
285 Private
->CurrentPeimCount
= SaveCurrentPeimCount
;
289 // This is the main dispatch loop. It will search known FVs for PEIMs and
290 // attempt to dispatch them. If any PEIM gets dispatched through a single
291 // pass of the dispatcher, it will start over from the Bfv again to see
292 // if any new PEIMs dependencies got satisfied. With a well ordered
293 // FV where PEIMs are found in the order their dependencies are also
294 // satisfied, this dipatcher should run only once.
297 PeimNeedingDispatch
= FALSE
;
298 PeimDispatchOnThisPass
= FALSE
;
300 for (FvCount
= Private
->CurrentPeimFvCount
; FvCount
< Private
->FvCount
; FvCount
++) {
301 Private
->CurrentPeimFvCount
= FvCount
;
302 VolumeHandle
= Private
->Fv
[FvCount
].FvHeader
;
304 if (Private
->CurrentPeimCount
== 0) {
306 // When going through each FV, at first, search Apriori file to
307 // reorder all PEIMs to ensure the PEIMs in Apriori file to get
308 // dispatch at first.
310 DiscoverPeimsAndOrderWithApriori (Private
, VolumeHandle
);
314 // Start to dispatch all modules within the current Fv.
316 for (PeimCount
= Private
->CurrentPeimCount
;
317 (PeimCount
< PEI_CORE_MAX_PEIM_PER_FV
) && (Private
->CurrentFvFileHandles
[PeimCount
] != NULL
);
319 Private
->CurrentPeimCount
= PeimCount
;
320 PeimFileHandle
= Private
->CurrentFileHandle
= Private
->CurrentFvFileHandles
[PeimCount
];
322 if (Private
->Fv
[FvCount
].PeimState
[PeimCount
] == PEIM_STATE_NOT_DISPATCHED
) {
323 if (!DepexSatisfied (Private
, PeimFileHandle
, PeimCount
)) {
324 PeimNeedingDispatch
= TRUE
;
326 Status
= PeiLoadImage (
332 if ((Status
== EFI_SUCCESS
)) {
334 // The PEIM has its dependencies satisfied, and its entry point
335 // has been found, so invoke it.
337 PERF_START (0, "PEIM", NULL
, 0);
339 ExtendedData
.Handle
= (EFI_HANDLE
)PeimFileHandle
;
341 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
343 EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT_BEGIN
,
344 (VOID
*)(&ExtendedData
),
345 sizeof (ExtendedData
)
348 Status
= VerifyPeim (Private
, VolumeHandle
, PeimFileHandle
);
349 if (Status
!= EFI_SECURITY_VIOLATION
&& (AuthenticationState
== 0)) {
351 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
353 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
356 // Call the PEIM entry point
358 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT
)(UINTN
)EntryPoint
;
359 PeimEntryPoint (PeimFileHandle
, PeiServices
);
360 PeimDispatchOnThisPass
= TRUE
;
363 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
365 EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT_END
,
366 (VOID
*)(&ExtendedData
),
367 sizeof (ExtendedData
)
369 PERF_END (0, "PEIM", NULL
, 0);
374 // Process the Notify list and dispatch any notifies for
375 // newly installed PPIs.
377 ProcessNotifyList (Private
);
380 // If permanent memory was discovered and installed by this
381 // PEIM, shadow PEI Core and switch the stacks to the new memory.
383 if (Private
->SwitchStackSignal
) {
386 // Make sure we don't retry the same PEIM that added memory
388 Private
->CurrentPeimCount
++;
391 // Migrate IDT from CAR into real memory, so after stack switches to
392 // the new memory, the caller can get memory version PeiServiceTable.
394 //MigrateIdtTable (PeiServices);
396 // Since we are at dispatch level, only the Core's private data
397 // is preserved, nobody else should have any data on the stack.
398 // So we need to copy PEI core instance data to memory.
400 PrivateInMem
= AllocateCopyPool (sizeof (PEI_CORE_INSTANCE
), Private
);
401 ASSERT (PrivateInMem
!= NULL
);
404 // Shadow PEI Core. When permanent memory is avaiable, shadow
405 // PEI Core and PEIMs to get high performance.
407 PeiCoreFileHandle
= NULL
;
409 // Find the PEI Core in the BFV
411 Status
= PeiFindFileEx (
412 (EFI_PEI_FV_HANDLE
)Private
->Fv
[0].FvHeader
,
414 EFI_FV_FILETYPE_PEI_CORE
,
418 ASSERT_EFI_ERROR (Status
);
421 // Shadow PEI Core into memory so it will run faster
423 Status
= PeiLoadImage (PeiServices
, PeiCoreFileHandle
, &EntryPoint
, &AuthenticationState
);
424 ASSERT_EFI_ERROR (Status
);
427 // Switch to memory based stack and reenter PEI Core that has been
428 // shadowed to memory.
431 // Adjust the top of stack to be aligned at CPU_STACK_ALIGNMENT
433 TopOfStack
= (VOID
*)((UINTN
)Private
->StackBase
+ (UINTN
)Private
->StackSize
- CPU_STACK_ALIGNMENT
);
434 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
436 PeiCoreParameters
.SecCoreData
= SecCoreData
;
437 PeiCoreParameters
.PpiList
= NULL
;
438 PeiCoreParameters
.Data
= PrivateInMem
;
439 ASSERT (PeiCoreParameters
.Data
!= 0);
443 (VOID
*) (UINTN
) PeiCore
,
444 (VOID
*) &PeiCoreParameters
,
446 (VOID
*)(UINTN
)Private
->StackBase
450 if ((Private
->PeiMemoryInstalled
) && (Private
->Fv
[FvCount
].PeimState
[PeimCount
] == PEIM_STATE_REGISITER_FOR_SHADOW
) && \
451 (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
453 // If memory is availble we shadow images by default for performance reasons.
454 // We call the entry point a 2nd time so the module knows it's shadowed.
456 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
457 PeimEntryPoint (PeimFileHandle
, PeiServices
);
458 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
461 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
463 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
466 // Process the Notify list and dispatch any notifies for
467 // newly installed PPIs.
469 ProcessNotifyList (Private
);
476 // We set to NULL here to optimize the 2nd entry to this routine after
477 // memory is found. This reprevents rescanning of the FV. We set to
478 // NULL here so we start at the begining of the next FV
480 Private
->CurrentFileHandle
= NULL
;
481 Private
->CurrentPeimCount
= 0;
483 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL
485 SetMem (Private
->CurrentFvFileHandles
, sizeof (Private
->CurrentFvFileHandles
), 0);
489 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go
490 // through all the FV.
492 Private
->CurrentPeimFvCount
= 0;
495 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get
496 // dispatched. So we need to make another pass
498 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this
499 // pass. If we did not dispatch a PEIM there is no point in trying again
500 // as it will fail the next time too (nothing has changed).
502 } while (PeimNeedingDispatch
&& PeimDispatchOnThisPass
);
507 InitializeDispatcherData (
508 IN PEI_CORE_INSTANCE
*PrivateData
,
509 IN PEI_CORE_INSTANCE
*OldCoreData
,
510 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
516 Initialize the Dispatcher's data members
520 PeiServices - The PEI core services table.
521 OldCoreData - Pointer to old core data (before switching stack).
522 NULL if being run in non-permament memory mode.
523 SecCoreData - Points to a data structure containing information about the PEI core's operating
524 environment, such as the size and location of temporary RAM, the stack location and
533 if (OldCoreData
== NULL
) {
534 PeiInitializeFv (PrivateData
, SecCoreData
);
543 IN PEI_CORE_INSTANCE
*Private
,
544 IN EFI_PEI_FILE_HANDLE FileHandle
,
551 This routine parses the Dependency Expression, if available, and
552 decides if the module can be executed.
555 PeiServices - The PEI Service Table
556 CurrentPeimAddress - Address of the PEIM Firmware File under investigation
559 TRUE - Can be dispatched
560 FALSE - Cannot be dispatched
567 if (PeimCount
< Private
->AprioriCount
) {
569 // If its in the A priori file then we set Depex to TRUE
574 Status
= PeiServicesFfsFindSectionData (EFI_SECTION_PEI_DEPEX
, FileHandle
, (VOID
**) &DepexData
);
575 if (EFI_ERROR (Status
)) {
577 // If there is no DEPEX, assume the module can be executed
583 // Evaluate a given DEPEX
585 return PeimDispatchReadiness (&Private
->PS
, DepexData
);
589 This routine enable a PEIM to register itself to shadow when PEI Foundation
590 discovery permanent memory.
592 @param FileHandle File handle of a PEIM.
594 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.
595 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.
596 @retval EFI_SUCCESS Successfully to register itself.
601 PeiRegisterForShadow (
602 IN EFI_PEI_FILE_HANDLE FileHandle
605 PEI_CORE_INSTANCE
*Private
;
606 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
608 if (Private
->CurrentFileHandle
!= FileHandle
) {
610 // The FileHandle must be for the current PEIM
612 return EFI_NOT_FOUND
;
615 if (Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] >= PEIM_STATE_REGISITER_FOR_SHADOW
) {
617 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started
619 return EFI_ALREADY_STARTED
;
622 Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] = PEIM_STATE_REGISITER_FOR_SHADOW
;
629 This routine invoke the PeiCore's entry in new stack environment.
631 @param Context1 The first context parameter is entry of PeiCore
632 @param Context2 The second context parameter is parameter structure point for PeiCore
642 PEI_CORE_ENTRY_POINT PeiCoreEntryPoint
;
643 PEI_CORE_PARAMETERS
*PeiCoreParameters
;
646 // Running on new stack in SEC Core
649 PeiCoreEntryPoint
= (PEI_CORE_ENTRY_POINT
) (UINTN
) Context1
;
650 PeiCoreParameters
= (PEI_CORE_PARAMETERS
*)Context2
;
653 // Call PEI Core using new stack
656 PeiCoreParameters
->SecCoreData
,
657 PeiCoreParameters
->PpiList
,
658 PeiCoreParameters
->Data
664 ASSERT_EFI_ERROR (FALSE
);