]>
Commit | Line | Data |
---|---|---|
615c6dd0 | 1 | /** @file\r |
b1f6a7c6 | 2 | PEI Dispatcher Dependency Evaluator\r |
3 | \r | |
4 | This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine\r | |
d3add11e MK |
5 | if a driver can be scheduled for execution. The criteria to be scheduled is\r |
6 | that the dependency expression is satisfied.\r | |
192f6d4c | 7 | \r |
d39d1260 | 8 | Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 9 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
192f6d4c | 10 | \r |
615c6dd0 | 11 | **/\r |
192f6d4c | 12 | \r |
0d516397 | 13 | #include "PeiMain.h"\r |
95770ed8 | 14 | #include "Dependency.h"\r |
192f6d4c | 15 | \r |
b1f6a7c6 | 16 | /**\r |
192f6d4c | 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 | |
192f6d4c | 22 | \r |
ed299e3c | 23 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r |
b1f6a7c6 | 24 | @param Stack Reference to EVAL_STACK_ENTRY that contains PPI GUID to check\r |
192f6d4c | 25 | \r |
b1f6a7c6 | 26 | @retval TRUE if the PPI is already installed.\r |
27 | @retval FALSE if the PPI has yet to be installed.\r | |
192f6d4c | 28 | \r |
b1f6a7c6 | 29 | **/\r |
30 | BOOLEAN\r | |
31 | IsPpiInstalled (\r | |
32 | IN EFI_PEI_SERVICES **PeiServices,\r | |
33 | IN EVAL_STACK_ENTRY *Stack\r | |
34 | )\r | |
192f6d4c | 35 | {\r |
36 | VOID *PeiInstance;\r | |
37 | EFI_STATUS Status;\r | |
38 | EFI_GUID PpiGuid;\r | |
d1102dba | 39 | \r |
192f6d4c | 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 | |
d1102dba | 46 | \r |
192f6d4c | 47 | //\r |
d39d1260 | 48 | // Copy the GUID into a local variable so that there are no\r |
d1102dba | 49 | // possibilities of alignment faults for cross-compilation\r |
192f6d4c | 50 | // environments such as Intel?Itanium(TM).\r |
51 | //\r | |
1436aea4 | 52 | CopyMem (&PpiGuid, Stack->Operator, sizeof (EFI_GUID));\r |
192f6d4c | 53 | \r |
54 | //\r | |
55 | // Check if the PPI is installed.\r | |
56 | //\r | |
1436aea4 | 57 | Status = PeiServicesLocatePpi (\r |
192f6d4c | 58 | &PpiGuid, // GUID\r |
59 | 0, // INSTANCE\r | |
60 | NULL, // EFI_PEI_PPI_DESCRIPTOR\r | |
61 | &PeiInstance // PPI\r | |
62 | );\r | |
63 | \r | |
1436aea4 | 64 | if (EFI_ERROR (Status)) {\r |
192f6d4c | 65 | return FALSE;\r |
66 | }\r | |
d1102dba | 67 | \r |
192f6d4c | 68 | return TRUE;\r |
69 | }\r | |
70 | \r | |
b1f6a7c6 | 71 | /**\r |
192f6d4c | 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 | |
d39d1260 | 75 | the evaluation stack. When that entry is popped from the evaluation\r |
192f6d4c | 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 | |
192f6d4c | 80 | \r |
ed299e3c | 81 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r |
b1f6a7c6 | 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 | |
192f6d4c | 84 | \r |
b1f6a7c6 | 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 | |
192f6d4c | 89 | \r |
b1f6a7c6 | 90 | **/\r |
91 | BOOLEAN\r | |
92 | PeimDispatchReadiness (\r | |
1436aea4 MK |
93 | IN EFI_PEI_SERVICES **PeiServices,\r |
94 | IN VOID *DependencyExpression\r | |
b1f6a7c6 | 95 | )\r |
192f6d4c | 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 | |
1436aea4 | 101 | Iterator = DependencyExpression;\r |
192f6d4c | 102 | \r |
523f48e7 | 103 | StackPtr = EvalStack;\r |
192f6d4c | 104 | \r |
105 | while (TRUE) {\r | |
192f6d4c | 106 | switch (*(Iterator++)) {\r |
192f6d4c | 107 | //\r |
d1102dba | 108 | // For performance reason we put the frequently used items in front of\r |
192f6d4c | 109 | // the rarely used items\r |
110 | //\r | |
d1102dba | 111 | \r |
192f6d4c | 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 | |
6a55eea3 | 118 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r |
b0d803fe | 119 | return FALSE;\r |
192f6d4c | 120 | }\r |
121 | \r | |
122 | //\r | |
123 | // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)\r | |
d39d1260 | 124 | // We will evaluate if the PPI is installed on the POP operation.\r |
192f6d4c | 125 | //\r |
1436aea4 MK |
126 | StackPtr->Operator = (VOID *)Iterator;\r |
127 | Iterator = Iterator + sizeof (EFI_GUID);\r | |
6a55eea3 | 128 | DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = %a\n", StackPtr->Operator, IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r |
192f6d4c | 129 | StackPtr++;\r |
130 | break;\r | |
131 | \r | |
d1102dba LG |
132 | case (EFI_DEP_AND):\r |
133 | case (EFI_DEP_OR):\r | |
6a55eea3 | 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 | |
1436aea4 | 139 | \r |
192f6d4c | 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 | |
6a55eea3 | 147 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r |
b0d803fe | 148 | return FALSE;\r |
192f6d4c | 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 | |
d1102dba | 159 | //\r |
192f6d4c | 160 | StackPtr--;\r |
161 | //\r | |
d1102dba LG |
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 | |
192f6d4c | 164 | // same operand.\r |
165 | //\r | |
166 | if (*(Iterator - 1) == EFI_DEP_AND) {\r | |
167 | if (!(IsPpiInstalled (PeiServices, StackPtr))) {\r | |
1436aea4 | 168 | (StackPtr-1)->Result = FALSE;\r |
192f6d4c | 169 | (StackPtr-1)->Operator = NULL;\r |
170 | }\r | |
171 | } else {\r | |
172 | if (IsPpiInstalled (PeiServices, StackPtr)) {\r | |
1436aea4 | 173 | (StackPtr-1)->Result = TRUE;\r |
192f6d4c | 174 | (StackPtr-1)->Operator = NULL;\r |
175 | }\r | |
176 | }\r | |
1436aea4 | 177 | \r |
192f6d4c | 178 | break;\r |
d1102dba | 179 | \r |
192f6d4c | 180 | case (EFI_DEP_END):\r |
6a55eea3 | 181 | DEBUG ((DEBUG_DISPATCH, " END\n"));\r |
192f6d4c | 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 | |
6a55eea3 | 188 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r |
b0d803fe | 189 | return FALSE;\r |
192f6d4c | 190 | }\r |
1436aea4 | 191 | \r |
6a55eea3 | 192 | DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r |
b0d803fe | 193 | return IsPpiInstalled (PeiServices, StackPtr);\r |
192f6d4c | 194 | \r |
d1102dba | 195 | case (EFI_DEP_NOT):\r |
6a55eea3 | 196 | DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r |
192f6d4c | 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 | |
6a55eea3 | 204 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r |
b0d803fe | 205 | return FALSE;\r |
192f6d4c | 206 | }\r |
1436aea4 MK |
207 | \r |
208 | (StackPtr-1)->Result = (BOOLEAN) !IsPpiInstalled (PeiServices, (StackPtr-1));\r | |
192f6d4c | 209 | (StackPtr-1)->Operator = NULL;\r |
210 | break;\r | |
211 | \r | |
212 | case (EFI_DEP_TRUE):\r | |
213 | case (EFI_DEP_FALSE):\r | |
6a55eea3 | 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 | |
1436aea4 | 219 | \r |
192f6d4c | 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 | |
6a55eea3 | 225 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r |
b0d803fe | 226 | return FALSE;\r |
192f6d4c | 227 | }\r |
1436aea4 | 228 | \r |
192f6d4c | 229 | //\r |
d1102dba LG |
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 | |
192f6d4c | 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 | |
1436aea4 | 239 | \r |
192f6d4c | 240 | StackPtr->Operator = NULL;\r |
241 | StackPtr++;\r | |
242 | break;\r | |
243 | \r | |
244 | default:\r | |
6a55eea3 | 245 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Invalid opcode)\n"));\r |
192f6d4c | 246 | //\r |
247 | // The grammar should never arrive here\r | |
248 | //\r | |
b0d803fe | 249 | return FALSE;\r |
192f6d4c | 250 | }\r |
251 | }\r | |
252 | }\r |