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