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