2 PEI Dispatcher Dependency Evaluator
4 This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
5 if a driver can be scheduled for execution. The criteria for
6 schedulability is that the dependency expression is satisfied.
8 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "Dependency.h"
18 This routine determines if a PPI has been installed.
19 The truth value of a GUID is determined by if the PPI has
20 been published and can be queried from the PPI database.
23 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
24 @param Stack Reference to EVAL_STACK_ENTRY that contains PPI GUID to check
26 @retval TRUE if the PPI is already installed.
27 @retval FALSE if the PPI has yet to be installed.
32 IN EFI_PEI_SERVICES
**PeiServices
,
33 IN EVAL_STACK_ENTRY
*Stack
41 // If there is no GUID to evaluate, just return current result on stack.
43 if (Stack
->Operator
== NULL
) {
48 // Copy the Guid into a locale variable so that there are no
49 // possibilities of alignment faults for cross-compilation
50 // environments such as Intel?Itanium(TM).
52 CopyMem(&PpiGuid
, Stack
->Operator
, sizeof(EFI_GUID
));
55 // Check if the PPI is installed.
57 Status
= PeiServicesLocatePpi(
60 NULL
, // EFI_PEI_PPI_DESCRIPTOR
64 if (EFI_ERROR(Status
)) {
73 This is the POSTFIX version of the dependency evaluator. When a
74 PUSH [PPI GUID] is encountered, a pointer to the GUID is stored on
75 the evaluation stack. When that entry is poped from the evaluation
76 stack, the PPI is checked if it is installed. This method allows
77 some time savings as not all PPIs must be checked for certain
78 operation types (AND, OR).
81 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
82 @param DependencyExpression Pointer to a dependency expression. The Grammar adheres to
83 the BNF described above and is stored in postfix notation.
85 @retval TRUE if it is a well-formed Grammar
86 @retval FALSE if the dependency expression overflows the evaluation stack
87 if the dependency expression underflows the evaluation stack
88 if the dependency expression is not a well-formed Grammar.
92 PeimDispatchReadiness (
93 IN EFI_PEI_SERVICES
**PeiServices
,
94 IN VOID
*DependencyExpression
97 DEPENDENCY_EXPRESSION_OPERAND
*Iterator
;
98 EVAL_STACK_ENTRY
*StackPtr
;
99 EVAL_STACK_ENTRY EvalStack
[MAX_GRAMMAR_SIZE
];
101 Iterator
= DependencyExpression
;
103 StackPtr
= EvalStack
;
107 switch (*(Iterator
++)) {
110 // For performance reason we put the frequently used items in front of
111 // the rarely used items
116 // Check to make sure the dependency grammar doesn't overflow the
117 // EvalStack on the push
119 if (StackPtr
> &EvalStack
[MAX_GRAMMAR_SIZE
-1]) {
120 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Underflow Error)\n"));
125 // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)
126 // We will evaluate if the PPI is insalled on the POP operation.
128 StackPtr
->Operator
= (VOID
*) Iterator
;
129 Iterator
= Iterator
+ sizeof (EFI_GUID
);
130 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = %a\n", StackPtr
->Operator
, IsPpiInstalled (PeiServices
, StackPtr
) ? "TRUE" : "FALSE"));
136 if (*(Iterator
- 1) == EFI_DEP_AND
) {
137 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
139 DEBUG ((DEBUG_DISPATCH
, " OR\n"));
142 // Check to make sure the dependency grammar doesn't underflow the
143 // EvalStack on the two POPs for the AND operation. Don't need to
144 // check for the overflow on PUSHing the result since we already
147 if (StackPtr
< &EvalStack
[2]) {
148 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Underflow Error)\n"));
153 // Evaluate the first POPed operator only. If the operand is
154 // EFI_DEP_AND and the POPed operator evaluates to FALSE, or the
155 // operand is EFI_DEP_OR and the POPed operator evaluates to TRUE,
156 // we don't need to check the second operator, and the result will be
157 // evaluation of the POPed operator. Otherwise, don't POP the second
158 // operator since it will now evaluate to the final result on the
159 // next operand that causes a POP.
163 // Iterator has increased by 1 after we retrieve the operand, so here we
164 // should get the value pointed by (Iterator - 1), in order to obtain the
167 if (*(Iterator
- 1) == EFI_DEP_AND
) {
168 if (!(IsPpiInstalled (PeiServices
, StackPtr
))) {
169 (StackPtr
-1)->Result
= FALSE
;
170 (StackPtr
-1)->Operator
= NULL
;
173 if (IsPpiInstalled (PeiServices
, StackPtr
)) {
174 (StackPtr
-1)->Result
= TRUE
;
175 (StackPtr
-1)->Operator
= NULL
;
181 DEBUG ((DEBUG_DISPATCH
, " END\n"));
184 // Check to make sure EvalStack is balanced. If not, then there is
185 // an error in the dependency grammar, so return EFI_INVALID_PARAMETER.
187 if (StackPtr
!= &EvalStack
[0]) {
188 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Underflow Error)\n"));
191 DEBUG ((DEBUG_DISPATCH
, " RESULT = %a\n", IsPpiInstalled (PeiServices
, StackPtr
) ? "TRUE" : "FALSE"));
192 return IsPpiInstalled (PeiServices
, StackPtr
);
195 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
197 // Check to make sure the dependency grammar doesn't underflow the
198 // EvalStack on the POP for the NOT operation. Don't need to
199 // check for the overflow on PUSHing the result since we already
202 if (StackPtr
< &EvalStack
[1]) {
203 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Underflow Error)\n"));
206 (StackPtr
-1)->Result
= (BOOLEAN
) !IsPpiInstalled (PeiServices
, (StackPtr
-1));
207 (StackPtr
-1)->Operator
= NULL
;
211 case (EFI_DEP_FALSE
):
212 if (*(Iterator
- 1) == EFI_DEP_TRUE
) {
213 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
215 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
218 // Check to make sure the dependency grammar doesn't overflow the
219 // EvalStack on the push
221 if (StackPtr
> &EvalStack
[MAX_GRAMMAR_SIZE
-1]) {
222 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Underflow Error)\n"));
226 // Iterator has increased by 1 after we retrieve the operand, so here we
227 // should get the value pointed by (Iterator - 1), in order to obtain the
230 if (*(Iterator
- 1) == EFI_DEP_TRUE
) {
231 StackPtr
->Result
= TRUE
;
233 StackPtr
->Result
= FALSE
;
235 StackPtr
->Operator
= NULL
;
240 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Invalid opcode)\n"));
242 // The grammar should never arrive here