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