]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dependency/Dependency.c
MdeModulePkg PeiCore: Improve comment semantics
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dependency / Dependency.c
CommitLineData
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 8Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9d510e61 9SPDX-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
30BOOLEAN\r
31IsPpiInstalled (\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
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
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
91BOOLEAN\r
92PeimDispatchReadiness (\r
93 IN EFI_PEI_SERVICES **PeiServices,\r
94 IN VOID *DependencyExpression\r
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
101 Iterator = DependencyExpression;\r
192f6d4c 102\r
523f48e7 103 StackPtr = EvalStack;\r
192f6d4c 104\r
105 while (TRUE) {\r
106\r
107 switch (*(Iterator++)) {\r
d1102dba 108\r
192f6d4c 109 //\r
d1102dba 110 // For performance reason we put the frequently used items in front of\r
192f6d4c 111 // the rarely used items\r
112 //\r
d1102dba 113\r
192f6d4c 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
6a55eea3 120 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
b0d803fe 121 return FALSE;\r
192f6d4c 122 }\r
123\r
124 //\r
125 // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)\r
d39d1260 126 // We will evaluate if the PPI is installed on the POP operation.\r
192f6d4c 127 //\r
128 StackPtr->Operator = (VOID *) Iterator;\r
129 Iterator = Iterator + sizeof (EFI_GUID);\r
6a55eea3 130 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = %a\n", StackPtr->Operator, IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r
192f6d4c 131 StackPtr++;\r
132 break;\r
133\r
d1102dba
LG
134 case (EFI_DEP_AND):\r
135 case (EFI_DEP_OR):\r
6a55eea3 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
192f6d4c 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
6a55eea3 148 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
b0d803fe 149 return FALSE;\r
192f6d4c 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
d1102dba 160 //\r
192f6d4c 161 StackPtr--;\r
162 //\r
d1102dba
LG
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
192f6d4c 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
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
6a55eea3 191 DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", IsPpiInstalled (PeiServices, StackPtr) ? "TRUE" : "FALSE"));\r
b0d803fe 192 return IsPpiInstalled (PeiServices, StackPtr);\r
192f6d4c 193\r
d1102dba 194 case (EFI_DEP_NOT):\r
6a55eea3 195 DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r
192f6d4c 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
6a55eea3 203 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
b0d803fe 204 return FALSE;\r
192f6d4c 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
6a55eea3 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
192f6d4c 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
6a55eea3 222 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Underflow Error)\n"));\r
b0d803fe 223 return FALSE;\r
192f6d4c 224 }\r
225 //\r
d1102dba
LG
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
192f6d4c 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
6a55eea3 240 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Invalid opcode)\n"));\r
192f6d4c 241 //\r
242 // The grammar should never arrive here\r
243 //\r
b0d803fe 244 return FALSE;\r
192f6d4c 245 }\r
246 }\r
247}\r