]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dependency/Dependency.c
Fixed a bug for OPTION_NII(62) defined in RFC 5970.
[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
5 if a driver can be scheduled for execution. The criteria for\r
6 schedulability is that the dependency expression is satisfied.\r
192f6d4c 7\r
cd5ebaa0
HT
8Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>\r
9This program and the accompanying materials \r
192f6d4c 10are licensed and made available under the terms and conditions of the BSD License \r
11which accompanies this distribution. The full text of the license may be found at \r
12http://opensource.org/licenses/bsd-license.php \r
13 \r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
16\r
615c6dd0 17**/\r
192f6d4c 18\r
0d516397 19#include "PeiMain.h"\r
95770ed8 20#include "Dependency.h"\r
192f6d4c 21\r
b1f6a7c6 22/**\r
192f6d4c 23\r
24 This routine determines if a PPI has been installed.\r
25 The truth value of a GUID is determined by if the PPI has\r
26 been published and can be queried from the PPI database.\r
27\r
192f6d4c 28\r
ed299e3c 29 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 30 @param Stack Reference to EVAL_STACK_ENTRY that contains PPI GUID to check\r
192f6d4c 31\r
b1f6a7c6 32 @retval TRUE if the PPI is already installed.\r
33 @retval FALSE if the PPI has yet to be installed.\r
192f6d4c 34\r
b1f6a7c6 35**/\r
36BOOLEAN\r
37IsPpiInstalled (\r
38 IN EFI_PEI_SERVICES **PeiServices,\r
39 IN EVAL_STACK_ENTRY *Stack\r
40 )\r
192f6d4c 41{\r
42 VOID *PeiInstance;\r
43 EFI_STATUS Status;\r
44 EFI_GUID PpiGuid;\r
45 \r
46 //\r
47 // If there is no GUID to evaluate, just return current result on stack.\r
48 //\r
49 if (Stack->Operator == NULL) {\r
50 return Stack->Result;\r
51 }\r
52 \r
53 //\r
54 // Copy the Guid into a locale variable so that there are no\r
55 // possibilities of alignment faults for cross-compilation \r
56 // environments such as Intel?Itanium(TM).\r
57 //\r
58 CopyMem(&PpiGuid, Stack->Operator, sizeof(EFI_GUID));\r
59\r
60 //\r
61 // Check if the PPI is installed.\r
62 //\r
63 Status = PeiServicesLocatePpi(\r
64 &PpiGuid, // GUID\r
65 0, // INSTANCE\r
66 NULL, // EFI_PEI_PPI_DESCRIPTOR\r
67 &PeiInstance // PPI\r
68 );\r
69\r
70 if (EFI_ERROR(Status)) {\r
71 return FALSE;\r
72 }\r
73 \r
74 return TRUE;\r
75}\r
76\r
b1f6a7c6 77/**\r
192f6d4c 78\r
79 This is the POSTFIX version of the dependency evaluator. When a\r
80 PUSH [PPI GUID] is encountered, a pointer to the GUID is stored on\r
81 the evaluation stack. When that entry is poped from the evaluation\r
82 stack, the PPI is checked if it is installed. This method allows\r
83 some time savings as not all PPIs must be checked for certain\r
84 operation types (AND, OR).\r
85\r
192f6d4c 86\r
ed299e3c 87 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 88 @param DependencyExpression Pointer to a dependency expression. The Grammar adheres to\r
89 the BNF described above and is stored in postfix notation.\r
192f6d4c 90\r
b1f6a7c6 91 @retval TRUE if it is a well-formed Grammar\r
92 @retval FALSE if the dependency expression overflows the evaluation stack\r
93 if the dependency expression underflows the evaluation stack\r
94 if the dependency expression is not a well-formed Grammar.\r
192f6d4c 95\r
b1f6a7c6 96**/\r
97BOOLEAN\r
98PeimDispatchReadiness (\r
99 IN EFI_PEI_SERVICES **PeiServices,\r
100 IN VOID *DependencyExpression\r
101 )\r
192f6d4c 102{\r
103 DEPENDENCY_EXPRESSION_OPERAND *Iterator;\r
104 EVAL_STACK_ENTRY *StackPtr;\r
105 EVAL_STACK_ENTRY EvalStack[MAX_GRAMMAR_SIZE];\r
106\r
107 Iterator = DependencyExpression;\r
192f6d4c 108\r
109 StackPtr = &EvalStack[0];\r
110\r
111 while (TRUE) {\r
112\r
113 switch (*(Iterator++)) {\r
114 \r
115 //\r
116 // For performance reason we put the frequently used items in front of \r
117 // the rarely used items\r
118 //\r
119 \r
120 case (EFI_DEP_PUSH):\r
121 //\r
122 // Check to make sure the dependency grammar doesn't overflow the\r
123 // EvalStack on the push\r
124 //\r
125 if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {\r
b0d803fe 126 return FALSE;\r
192f6d4c 127 }\r
128\r
129 //\r
130 // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)\r
131 // We will evaluate if the PPI is insalled on the POP operation.\r
132 //\r
133 StackPtr->Operator = (VOID *) Iterator;\r
134 Iterator = Iterator + sizeof (EFI_GUID);\r
135 StackPtr++;\r
136 break;\r
137\r
138 case (EFI_DEP_AND): \r
139 case (EFI_DEP_OR): \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
b0d803fe 147 return FALSE;\r
192f6d4c 148 }\r
149\r
150 //\r
151 // Evaluate the first POPed operator only. If the operand is\r
152 // EFI_DEP_AND and the POPed operator evaluates to FALSE, or the\r
153 // operand is EFI_DEP_OR and the POPed operator evaluates to TRUE,\r
154 // we don't need to check the second operator, and the result will be\r
155 // evaluation of the POPed operator. Otherwise, don't POP the second\r
156 // operator since it will now evaluate to the final result on the\r
157 // next operand that causes a POP.\r
158 // \r
159 StackPtr--;\r
160 //\r
161 // Iterator has increased by 1 after we retrieve the operand, so here we \r
162 // should get the value pointed by (Iterator - 1), in order to obtain the \r
163 // same operand.\r
164 //\r
165 if (*(Iterator - 1) == EFI_DEP_AND) {\r
166 if (!(IsPpiInstalled (PeiServices, StackPtr))) {\r
167 (StackPtr-1)->Result = FALSE;\r
168 (StackPtr-1)->Operator = NULL;\r
169 }\r
170 } else {\r
171 if (IsPpiInstalled (PeiServices, StackPtr)) {\r
172 (StackPtr-1)->Result = TRUE;\r
173 (StackPtr-1)->Operator = NULL;\r
174 }\r
175 }\r
176 break;\r
177 \r
178 case (EFI_DEP_END):\r
179 StackPtr--;\r
180 //\r
181 // Check to make sure EvalStack is balanced. If not, then there is\r
182 // an error in the dependency grammar, so return EFI_INVALID_PARAMETER.\r
183 //\r
184 if (StackPtr != &EvalStack[0]) {\r
b0d803fe 185 return FALSE;\r
192f6d4c 186 }\r
b0d803fe 187 return IsPpiInstalled (PeiServices, StackPtr);\r
192f6d4c 188 break;\r
189\r
190 case (EFI_DEP_NOT): \r
191 //\r
192 // Check to make sure the dependency grammar doesn't underflow the\r
193 // EvalStack on the POP for the NOT operation. Don't need to\r
194 // check for the overflow on PUSHing the result since we already\r
195 // did a POP.\r
196 //\r
197 if (StackPtr < &EvalStack[1]) {\r
b0d803fe 198 return FALSE;\r
192f6d4c 199 }\r
200 (StackPtr-1)->Result = (BOOLEAN) !IsPpiInstalled (PeiServices, (StackPtr-1));\r
201 (StackPtr-1)->Operator = NULL;\r
202 break;\r
203\r
204 case (EFI_DEP_TRUE):\r
205 case (EFI_DEP_FALSE):\r
206 //\r
207 // Check to make sure the dependency grammar doesn't overflow the\r
208 // EvalStack on the push\r
209 //\r
210 if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {\r
b0d803fe 211 return FALSE;\r
192f6d4c 212 }\r
213 //\r
214 // Iterator has increased by 1 after we retrieve the operand, so here we \r
215 // should get the value pointed by (Iterator - 1), in order to obtain the \r
216 // same operand.\r
217 //\r
218 if (*(Iterator - 1) == EFI_DEP_TRUE) {\r
219 StackPtr->Result = TRUE;\r
220 } else {\r
221 StackPtr->Result = FALSE;\r
222 }\r
223 StackPtr->Operator = NULL;\r
224 StackPtr++;\r
225 break;\r
226\r
227 default:\r
228 //\r
229 // The grammar should never arrive here\r
230 //\r
b0d803fe 231 return FALSE;\r
192f6d4c 232 }\r
233 }\r
234}\r