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