2 DXE Dispatcher Dependency Evaluator
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.
8 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
9 All rights reserved. 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
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.
22 // Global stack used to evaluate dependency expressions
24 BOOLEAN
*mDepexEvaluationStack
= NULL
;
25 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
26 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
34 Grow size of the Depex stack
36 @retval EFI_SUCCESS Stack successfully growed.
37 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
49 Size
= DEPEX_STACK_SIZE_INCREMENT
;
50 if (mDepexEvaluationStack
!= NULL
) {
51 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
54 NewStack
= CoreAllocateBootServicesPool (Size
* sizeof (BOOLEAN
));
55 if (NewStack
== NULL
) {
56 return EFI_OUT_OF_RESOURCES
;
59 if (mDepexEvaluationStack
!= NULL
) {
61 // Copy to Old Stack to the New Stack
65 mDepexEvaluationStack
,
66 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
72 CoreFreePool (mDepexEvaluationStack
);
76 // Make the Stack pointer point to the old data in the new stack
78 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
79 mDepexEvaluationStack
= NewStack
;
80 mDepexEvaluationStackEnd
= NewStack
+ Size
;
88 Push an element onto the Boolean Stack
90 @param Value BOOLEAN to push.
92 @retval EFI_SUCCESS The value was pushed onto the stack.
93 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
105 // Check for a stack overflow condition
107 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
111 Status
= GrowDepexStack ();
112 if (EFI_ERROR (Status
)) {
118 // Push the item onto the stack
120 *mDepexEvaluationStackPointer
= Value
;
121 mDepexEvaluationStackPointer
++;
129 Pop an element from the Boolean stack.
131 @param Value BOOLEAN to pop.
133 @retval EFI_SUCCESS The value was popped onto the stack.
134 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack
143 // Check for a stack underflow condition
145 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
146 return EFI_ACCESS_DENIED
;
150 // Pop the item off the stack
152 mDepexEvaluationStackPointer
--;
153 *Value
= *mDepexEvaluationStackPointer
;
160 Preprocess dependency expression and update DriverEntry to reflect the
161 state of Before, After, and SOR dependencies. If DriverEntry->Before
162 or DriverEntry->After is set it will never be cleared. If SOR is set
163 it will be cleared by CoreSchedule(), and then the driver can be
166 @param DriverEntry DriverEntry element to update
168 @retval EFI_SUCCESS It always works.
172 CorePreProcessDepex (
173 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
178 Iterator
= DriverEntry
->Depex
;
179 if (*Iterator
== EFI_DEP_SOR
) {
180 DriverEntry
->Unrequested
= TRUE
;
182 DriverEntry
->Dependent
= TRUE
;
185 if (*Iterator
== EFI_DEP_BEFORE
) {
186 DriverEntry
->Before
= TRUE
;
187 } else if (*Iterator
== EFI_DEP_AFTER
) {
188 DriverEntry
->After
= TRUE
;
191 if (DriverEntry
->Before
|| DriverEntry
->After
) {
192 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
201 This is the POSTFIX version of the dependency evaluator. This code does
202 not need to handle Before or After, as it is not valid to call this
203 routine in this case. The SOR is just ignored and is a nop in the grammer.
204 POSTFIX means all the math is done on top of the stack.
206 @param DriverEntry DriverEntry element to update
208 @retval TRUE If driver is ready to run.
209 @retval FALSE If driver is not ready to run or some fatal error
215 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
225 if (DriverEntry
->After
|| DriverEntry
->Before
) {
227 // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
233 if (DriverEntry
->Depex
== NULL
) {
235 // A NULL Depex means treat the driver like an UEFI 2.0 thing.
237 Status
= CoreAllEfiServicesAvailable ();
238 if (EFI_ERROR (Status
)) {
245 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
246 // incorrectly formed DEPEX expressions
248 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
251 Iterator
= DriverEntry
->Depex
;
255 // Check to see if we are attempting to fetch dependency expression instructions
256 // past the end of the dependency expression.
258 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
263 // Look at the opcode of the dependency expression instruction.
269 // For a well-formed Dependency Expression, the code should never get here.
270 // The BEFORE and AFTER are processed prior to this routine's invocation.
271 // If the code flow arrives at this point, there was a BEFORE or AFTER
272 // that were not the first opcodes.
277 // These opcodes can only appear once as the first opcode. If it is found
278 // at any other location, then the dependency expression evaluates to FALSE
280 if (Iterator
!= DriverEntry
->Depex
) {
284 // Otherwise, it is the first opcode and should be treated as a NOP.
290 // Push operator is followed by a GUID. Test to see if the GUID protocol
291 // is installed and push the boolean result on the stack.
293 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
295 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
297 if (EFI_ERROR (Status
)) {
298 Status
= PushBool (FALSE
);
300 *Iterator
= EFI_DEP_REPLACE_TRUE
;
301 Status
= PushBool (TRUE
);
303 if (EFI_ERROR (Status
)) {
307 Iterator
+= sizeof (EFI_GUID
);
311 Status
= PopBool (&Operator
);
312 if (EFI_ERROR (Status
)) {
316 Status
= PopBool (&Operator2
);
317 if (EFI_ERROR (Status
)) {
321 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
322 if (EFI_ERROR (Status
)) {
328 Status
= PopBool (&Operator
);
329 if (EFI_ERROR (Status
)) {
333 Status
= PopBool (&Operator2
);
334 if (EFI_ERROR (Status
)) {
338 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
339 if (EFI_ERROR (Status
)) {
345 Status
= PopBool (&Operator
);
346 if (EFI_ERROR (Status
)) {
350 Status
= PushBool ((BOOLEAN
)(!Operator
));
351 if (EFI_ERROR (Status
)) {
357 Status
= PushBool (TRUE
);
358 if (EFI_ERROR (Status
)) {
364 Status
= PushBool (FALSE
);
365 if (EFI_ERROR (Status
)) {
371 Status
= PopBool (&Operator
);
372 if (EFI_ERROR (Status
)) {
377 case EFI_DEP_REPLACE_TRUE
:
378 Status
= PushBool (TRUE
);
379 if (EFI_ERROR (Status
)) {
383 Iterator
+= sizeof (EFI_GUID
);
391 // Skip over the Dependency Op Code we just processed in the switch.
392 // The math is done out of order, but it should not matter. That is
393 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
394 // This is not an issue, since we just need the correct end result. You
395 // need to be careful using Iterator in the loop as it's intermediate value