]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Dependency/Dependency.c
MdeModulePkg PeiCore: Fix typos
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dependency / Dependency.c
... / ...
CommitLineData
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 for\r
6 schedulability is that the dependency expression is satisfied.\r
7\r
8Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9SPDX-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
30BOOLEAN\r
31IsPpiInstalled (\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
91BOOLEAN\r
92PeimDispatchReadiness (\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\r
107 switch (*(Iterator++)) {\r
108\r
109 //\r
110 // For performance reason we put the frequently used items in front of\r
111 // the rarely used items\r
112 //\r
113\r
114 case (EFI_DEP_PUSH):\r
115 //\r
116 // Check to make sure the dependency grammar doesn't overflow the\r
117 // EvalStack on the push\r
118 //\r
119 if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {\r
120 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
121 return FALSE;\r
122 }\r
123\r
124 //\r
125 // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)\r
126 // We will evaluate if the PPI is installed on the POP operation.\r
127 //\r
128 StackPtr->Operator = (VOID *) Iterator;\r
129 Iterator = Iterator + sizeof (EFI_GUID);\r
130 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = %a\n", StackPtr->Operator, IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r
131 StackPtr++;\r
132 break;\r
133\r
134 case (EFI_DEP_AND):\r
135 case (EFI_DEP_OR):\r
136 if (*(Iterator - 1) == EFI_DEP_AND) {\r
137 DEBUG ((DEBUG_DISPATCH, " AND\n"));\r
138 } else {\r
139 DEBUG ((DEBUG_DISPATCH, " OR\n"));\r
140 }\r
141 //\r
142 // Check to make sure the dependency grammar doesn't underflow the\r
143 // EvalStack on the two POPs for the AND operation. Don't need to\r
144 // check for the overflow on PUSHing the result since we already\r
145 // did two POPs.\r
146 //\r
147 if (StackPtr < &EvalStack[2]) {\r
148 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
149 return FALSE;\r
150 }\r
151\r
152 //\r
153 // Evaluate the first POPed operator only. If the operand is\r
154 // EFI_DEP_AND and the POPed operator evaluates to FALSE, or the\r
155 // operand is EFI_DEP_OR and the POPed operator evaluates to TRUE,\r
156 // we don't need to check the second operator, and the result will be\r
157 // evaluation of the POPed operator. Otherwise, don't POP the second\r
158 // operator since it will now evaluate to the final result on the\r
159 // next operand that causes a POP.\r
160 //\r
161 StackPtr--;\r
162 //\r
163 // Iterator has increased by 1 after we retrieve the operand, so here we\r
164 // should get the value pointed by (Iterator - 1), in order to obtain the\r
165 // same operand.\r
166 //\r
167 if (*(Iterator - 1) == EFI_DEP_AND) {\r
168 if (!(IsPpiInstalled (PeiServices, StackPtr))) {\r
169 (StackPtr-1)->Result = FALSE;\r
170 (StackPtr-1)->Operator = NULL;\r
171 }\r
172 } else {\r
173 if (IsPpiInstalled (PeiServices, StackPtr)) {\r
174 (StackPtr-1)->Result = TRUE;\r
175 (StackPtr-1)->Operator = NULL;\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 DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r
192 return IsPpiInstalled (PeiServices, StackPtr);\r
193\r
194 case (EFI_DEP_NOT):\r
195 DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r
196 //\r
197 // Check to make sure the dependency grammar doesn't underflow the\r
198 // EvalStack on the POP for the NOT operation. Don't need to\r
199 // check for the overflow on PUSHing the result since we already\r
200 // did a POP.\r
201 //\r
202 if (StackPtr < &EvalStack[1]) {\r
203 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
204 return FALSE;\r
205 }\r
206 (StackPtr-1)->Result = (BOOLEAN) !IsPpiInstalled (PeiServices, (StackPtr-1));\r
207 (StackPtr-1)->Operator = NULL;\r
208 break;\r
209\r
210 case (EFI_DEP_TRUE):\r
211 case (EFI_DEP_FALSE):\r
212 if (*(Iterator - 1) == EFI_DEP_TRUE) {\r
213 DEBUG ((DEBUG_DISPATCH, " TRUE\n"));\r
214 } else {\r
215 DEBUG ((DEBUG_DISPATCH, " FALSE\n"));\r
216 }\r
217 //\r
218 // Check to make sure the dependency grammar doesn't overflow the\r
219 // EvalStack on the push\r
220 //\r
221 if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {\r
222 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
223 return FALSE;\r
224 }\r
225 //\r
226 // Iterator has increased by 1 after we retrieve the operand, so here we\r
227 // should get the value pointed by (Iterator - 1), in order to obtain the\r
228 // same operand.\r
229 //\r
230 if (*(Iterator - 1) == EFI_DEP_TRUE) {\r
231 StackPtr->Result = TRUE;\r
232 } else {\r
233 StackPtr->Result = FALSE;\r
234 }\r
235 StackPtr->Operator = NULL;\r
236 StackPtr++;\r
237 break;\r
238\r
239 default:\r
240 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Invalid opcode)\n"));\r
241 //\r
242 // The grammar should never arrive here\r
243 //\r
244 return FALSE;\r
245 }\r
246 }\r
247}\r