]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | PEI Dispatcher Dependency Evaluator\r | |
3 | \r | |
4 | This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine\r | |
5 | if a driver can be scheduled for execution. The criteria to be scheduled is\r | |
6 | that the dependency expression is satisfied.\r | |
7 | \r | |
8 | Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r | |
9 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
10 | \r | |
11 | **/\r | |
12 | \r | |
13 | #include "PeiMain.h"\r | |
14 | #include "Dependency.h"\r | |
15 | \r | |
16 | /**\r | |
17 | \r | |
18 | This routine determines if a PPI has been installed.\r | |
19 | The truth value of a GUID is determined by if the PPI has\r | |
20 | been published and can be queried from the PPI database.\r | |
21 | \r | |
22 | \r | |
23 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r | |
24 | @param Stack Reference to EVAL_STACK_ENTRY that contains PPI GUID to check\r | |
25 | \r | |
26 | @retval TRUE if the PPI is already installed.\r | |
27 | @retval FALSE if the PPI has yet to be installed.\r | |
28 | \r | |
29 | **/\r | |
30 | BOOLEAN\r | |
31 | IsPpiInstalled (\r | |
32 | IN EFI_PEI_SERVICES **PeiServices,\r | |
33 | IN EVAL_STACK_ENTRY *Stack\r | |
34 | )\r | |
35 | {\r | |
36 | VOID *PeiInstance;\r | |
37 | EFI_STATUS Status;\r | |
38 | EFI_GUID PpiGuid;\r | |
39 | \r | |
40 | //\r | |
41 | // If there is no GUID to evaluate, just return current result on stack.\r | |
42 | //\r | |
43 | if (Stack->Operator == NULL) {\r | |
44 | return Stack->Result;\r | |
45 | }\r | |
46 | \r | |
47 | //\r | |
48 | // Copy the GUID into a local variable so that there are no\r | |
49 | // possibilities of alignment faults for cross-compilation\r | |
50 | // environments such as Intel?Itanium(TM).\r | |
51 | //\r | |
52 | CopyMem (&PpiGuid, Stack->Operator, sizeof (EFI_GUID));\r | |
53 | \r | |
54 | //\r | |
55 | // Check if the PPI is installed.\r | |
56 | //\r | |
57 | Status = PeiServicesLocatePpi (\r | |
58 | &PpiGuid, // GUID\r | |
59 | 0, // INSTANCE\r | |
60 | NULL, // EFI_PEI_PPI_DESCRIPTOR\r | |
61 | &PeiInstance // PPI\r | |
62 | );\r | |
63 | \r | |
64 | if (EFI_ERROR (Status)) {\r | |
65 | return FALSE;\r | |
66 | }\r | |
67 | \r | |
68 | return TRUE;\r | |
69 | }\r | |
70 | \r | |
71 | /**\r | |
72 | \r | |
73 | This is the POSTFIX version of the dependency evaluator. When a\r | |
74 | PUSH [PPI GUID] is encountered, a pointer to the GUID is stored on\r | |
75 | the evaluation stack. When that entry is popped from the evaluation\r | |
76 | stack, the PPI is checked if it is installed. This method allows\r | |
77 | some time savings as not all PPIs must be checked for certain\r | |
78 | operation types (AND, OR).\r | |
79 | \r | |
80 | \r | |
81 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r | |
82 | @param DependencyExpression Pointer to a dependency expression. The Grammar adheres to\r | |
83 | the BNF described above and is stored in postfix notation.\r | |
84 | \r | |
85 | @retval TRUE if it is a well-formed Grammar\r | |
86 | @retval FALSE if the dependency expression overflows the evaluation stack\r | |
87 | if the dependency expression underflows the evaluation stack\r | |
88 | if the dependency expression is not a well-formed Grammar.\r | |
89 | \r | |
90 | **/\r | |
91 | BOOLEAN\r | |
92 | PeimDispatchReadiness (\r | |
93 | IN EFI_PEI_SERVICES **PeiServices,\r | |
94 | IN VOID *DependencyExpression\r | |
95 | )\r | |
96 | {\r | |
97 | DEPENDENCY_EXPRESSION_OPERAND *Iterator;\r | |
98 | EVAL_STACK_ENTRY *StackPtr;\r | |
99 | EVAL_STACK_ENTRY EvalStack[MAX_GRAMMAR_SIZE];\r | |
100 | \r | |
101 | Iterator = DependencyExpression;\r | |
102 | \r | |
103 | StackPtr = EvalStack;\r | |
104 | \r | |
105 | while (TRUE) {\r | |
106 | switch (*(Iterator++)) {\r | |
107 | //\r | |
108 | // For performance reason we put the frequently used items in front of\r | |
109 | // the rarely used items\r | |
110 | //\r | |
111 | \r | |
112 | case (EFI_DEP_PUSH):\r | |
113 | //\r | |
114 | // Check to make sure the dependency grammar doesn't overflow the\r | |
115 | // EvalStack on the push\r | |
116 | //\r | |
117 | if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {\r | |
118 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r | |
119 | return FALSE;\r | |
120 | }\r | |
121 | \r | |
122 | //\r | |
123 | // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)\r | |
124 | // We will evaluate if the PPI is installed on the POP operation.\r | |
125 | //\r | |
126 | StackPtr->Operator = (VOID *)Iterator;\r | |
127 | Iterator = Iterator + sizeof (EFI_GUID);\r | |
128 | DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = %a\n", StackPtr->Operator, IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r | |
129 | StackPtr++;\r | |
130 | break;\r | |
131 | \r | |
132 | case (EFI_DEP_AND):\r | |
133 | case (EFI_DEP_OR):\r | |
134 | if (*(Iterator - 1) == EFI_DEP_AND) {\r | |
135 | DEBUG ((DEBUG_DISPATCH, " AND\n"));\r | |
136 | } else {\r | |
137 | DEBUG ((DEBUG_DISPATCH, " OR\n"));\r | |
138 | }\r | |
139 | \r | |
140 | //\r | |
141 | // Check to make sure the dependency grammar doesn't underflow the\r | |
142 | // EvalStack on the two POPs for the AND operation. Don't need to\r | |
143 | // check for the overflow on PUSHing the result since we already\r | |
144 | // did two POPs.\r | |
145 | //\r | |
146 | if (StackPtr < &EvalStack[2]) {\r | |
147 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r | |
148 | return FALSE;\r | |
149 | }\r | |
150 | \r | |
151 | //\r | |
152 | // Evaluate the first POPed operator only. If the operand is\r | |
153 | // EFI_DEP_AND and the POPed operator evaluates to FALSE, or the\r | |
154 | // operand is EFI_DEP_OR and the POPed operator evaluates to TRUE,\r | |
155 | // we don't need to check the second operator, and the result will be\r | |
156 | // evaluation of the POPed operator. Otherwise, don't POP the second\r | |
157 | // operator since it will now evaluate to the final result on the\r | |
158 | // next operand that causes a POP.\r | |
159 | //\r | |
160 | StackPtr--;\r | |
161 | //\r | |
162 | // Iterator has increased by 1 after we retrieve the operand, so here we\r | |
163 | // should get the value pointed by (Iterator - 1), in order to obtain the\r | |
164 | // same operand.\r | |
165 | //\r | |
166 | if (*(Iterator - 1) == EFI_DEP_AND) {\r | |
167 | if (!(IsPpiInstalled (PeiServices, StackPtr))) {\r | |
168 | (StackPtr-1)->Result = FALSE;\r | |
169 | (StackPtr-1)->Operator = NULL;\r | |
170 | }\r | |
171 | } else {\r | |
172 | if (IsPpiInstalled (PeiServices, StackPtr)) {\r | |
173 | (StackPtr-1)->Result = TRUE;\r | |
174 | (StackPtr-1)->Operator = NULL;\r | |
175 | }\r | |
176 | }\r | |
177 | \r | |
178 | break;\r | |
179 | \r | |
180 | case (EFI_DEP_END):\r | |
181 | DEBUG ((DEBUG_DISPATCH, " END\n"));\r | |
182 | StackPtr--;\r | |
183 | //\r | |
184 | // Check to make sure EvalStack is balanced. If not, then there is\r | |
185 | // an error in the dependency grammar, so return EFI_INVALID_PARAMETER.\r | |
186 | //\r | |
187 | if (StackPtr != &EvalStack[0]) {\r | |
188 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r | |
189 | return FALSE;\r | |
190 | }\r | |
191 | \r | |
192 | DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r | |
193 | return IsPpiInstalled (PeiServices, StackPtr);\r | |
194 | \r | |
195 | case (EFI_DEP_NOT):\r | |
196 | DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r | |
197 | //\r | |
198 | // Check to make sure the dependency grammar doesn't underflow the\r | |
199 | // EvalStack on the POP for the NOT operation. Don't need to\r | |
200 | // check for the overflow on PUSHing the result since we already\r | |
201 | // did a POP.\r | |
202 | //\r | |
203 | if (StackPtr < &EvalStack[1]) {\r | |
204 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r | |
205 | return FALSE;\r | |
206 | }\r | |
207 | \r | |
208 | (StackPtr-1)->Result = (BOOLEAN) !IsPpiInstalled (PeiServices, (StackPtr-1));\r | |
209 | (StackPtr-1)->Operator = NULL;\r | |
210 | break;\r | |
211 | \r | |
212 | case (EFI_DEP_TRUE):\r | |
213 | case (EFI_DEP_FALSE):\r | |
214 | if (*(Iterator - 1) == EFI_DEP_TRUE) {\r | |
215 | DEBUG ((DEBUG_DISPATCH, " TRUE\n"));\r | |
216 | } else {\r | |
217 | DEBUG ((DEBUG_DISPATCH, " FALSE\n"));\r | |
218 | }\r | |
219 | \r | |
220 | //\r | |
221 | // Check to make sure the dependency grammar doesn't overflow the\r | |
222 | // EvalStack on the push\r | |
223 | //\r | |
224 | if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {\r | |
225 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r | |
226 | return FALSE;\r | |
227 | }\r | |
228 | \r | |
229 | //\r | |
230 | // Iterator has increased by 1 after we retrieve the operand, so here we\r | |
231 | // should get the value pointed by (Iterator - 1), in order to obtain the\r | |
232 | // same operand.\r | |
233 | //\r | |
234 | if (*(Iterator - 1) == EFI_DEP_TRUE) {\r | |
235 | StackPtr->Result = TRUE;\r | |
236 | } else {\r | |
237 | StackPtr->Result = FALSE;\r | |
238 | }\r | |
239 | \r | |
240 | StackPtr->Operator = NULL;\r | |
241 | StackPtr++;\r | |
242 | break;\r | |
243 | \r | |
244 | default:\r | |
245 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Invalid opcode)\n"));\r | |
246 | //\r | |
247 | // The grammar should never arrive here\r | |
248 | //\r | |
249 | return FALSE;\r | |
250 | }\r | |
251 | }\r | |
252 | }\r |