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
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.
18 DXE Dispatcher Dependency Evaluator
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.
29 // Global stack used to evaluate dependency expressions
31 BOOLEAN
*mDepexEvaluationStack
= NULL
;
32 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
33 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
48 Grow size of the Depex stack
52 Stack - Old stack on the way in and new stack on the way out
54 StackSize - New size of the stack
58 EFI_SUCCESS - Stack successfully growed.
60 EFI_OUT_OF_RESOURCES - There is not enough system memory to grow the stack.
69 Size
= DEPEX_STACK_SIZE_INCREMENT
;
70 if (mDepexEvaluationStack
!= NULL
) {
71 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
74 NewStack
= CoreAllocateBootServicesPool (Size
* sizeof (BOOLEAN
));
75 if (NewStack
== NULL
) {
76 return EFI_OUT_OF_RESOURCES
;
79 if (mDepexEvaluationStack
!= NULL
) {
81 // Copy to Old Stack to the New Stack
85 mDepexEvaluationStack
,
86 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
92 CoreFreePool (mDepexEvaluationStack
);
96 // Make the Stack pointer point to the old data in the new stack
98 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
99 mDepexEvaluationStack
= NewStack
;
100 mDepexEvaluationStackEnd
= NewStack
+ Size
;
115 Push an element onto the Boolean Stack
119 Value - BOOLEAN to push.
123 EFI_SUCCESS - The value was pushed onto the stack.
125 EFI_OUT_OF_RESOURCES - There is not enough system memory to grow the stack.
132 // Check for a stack overflow condition
134 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
138 Status
= GrowDepexStack ();
139 if (EFI_ERROR (Status
)) {
145 // Push the item onto the stack
147 *mDepexEvaluationStackPointer
= Value
;
148 mDepexEvaluationStackPointer
++;
163 Pop an element from the Boolean stack.
167 Value - BOOLEAN to pop.
171 EFI_SUCCESS - The value was popped onto the stack.
173 EFI_ACCESS_DENIED - The pop operation underflowed the stack
178 // Check for a stack underflow condition
180 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
181 return EFI_ACCESS_DENIED
;
185 // Pop the item off the stack
187 mDepexEvaluationStackPointer
--;
188 *Value
= *mDepexEvaluationStackPointer
;
194 CorePreProcessDepex (
195 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
201 Preprocess dependency expression and update DriverEntry to reflect the
202 state of Before, After, and SOR dependencies. If DriverEntry->Before
203 or DriverEntry->After is set it will never be cleared. If SOR is set
204 it will be cleared by CoreSchedule(), and then the driver can be
209 DriverEntry - DriverEntry element to update
213 EFI_SUCCESS - It always works.
219 Iterator
= DriverEntry
->Depex
;
220 if (*Iterator
== EFI_DEP_SOR
) {
221 DriverEntry
->Unrequested
= TRUE
;
223 DriverEntry
->Dependent
= TRUE
;
226 if (*Iterator
== EFI_DEP_BEFORE
) {
227 DriverEntry
->Before
= TRUE
;
228 } else if (*Iterator
== EFI_DEP_AFTER
) {
229 DriverEntry
->After
= TRUE
;
232 if (DriverEntry
->Before
|| DriverEntry
->After
) {
233 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
242 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
248 This is the POSTFIX version of the dependency evaluator. This code does
249 not need to handle Before or After, as it is not valid to call this
250 routine in this case. The SOR is just ignored and is a nop in the grammer.
252 POSTFIX means all the math is done on top of the stack.
256 DriverEntry - DriverEntry element to update
260 TRUE - If driver is ready to run.
262 FALSE - If driver is not ready to run or some fatal error was found.
273 if (DriverEntry
->After
|| DriverEntry
->Before
) {
275 // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
281 if (DriverEntry
->Depex
== NULL
) {
283 // A NULL Depex means treat the driver like an EFI 1.0 thing.
285 Status
= CoreAllEfiServicesAvailable ();
286 if (EFI_ERROR (Status
)) {
293 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
294 // incorrectly formed DEPEX expressions
296 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
299 Iterator
= DriverEntry
->Depex
;
303 // Check to see if we are attempting to fetch dependency expression instructions
304 // past the end of the dependency expression.
306 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
311 // Look at the opcode of the dependency expression instruction.
317 // For a well-formed Dependency Expression, the code should never get here.
318 // The BEFORE and AFTER are processed prior to this routine's invocation.
319 // If the code flow arrives at this point, there was a BEFORE or AFTER
320 // that were not the first opcodes.
325 // These opcodes can only appear once as the first opcode. If it is found
326 // at any other location, then the dependency expression evaluates to FALSE
328 if (Iterator
!= DriverEntry
->Depex
) {
332 // Otherwise, it is the first opcode and should be treated as a NOP.
338 // Push operator is followed by a GUID. Test to see if the GUID protocol
339 // is installed and push the boolean result on the stack.
341 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
343 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
345 if (EFI_ERROR (Status
)) {
346 Status
= PushBool (FALSE
);
348 *Iterator
= EFI_DEP_REPLACE_TRUE
;
349 Status
= PushBool (TRUE
);
351 if (EFI_ERROR (Status
)) {
355 Iterator
+= sizeof (EFI_GUID
);
359 Status
= PopBool (&Operator
);
360 if (EFI_ERROR (Status
)) {
364 Status
= PopBool (&Operator2
);
365 if (EFI_ERROR (Status
)) {
369 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
370 if (EFI_ERROR (Status
)) {
376 Status
= PopBool (&Operator
);
377 if (EFI_ERROR (Status
)) {
381 Status
= PopBool (&Operator2
);
382 if (EFI_ERROR (Status
)) {
386 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
387 if (EFI_ERROR (Status
)) {
393 Status
= PopBool (&Operator
);
394 if (EFI_ERROR (Status
)) {
398 Status
= PushBool ((BOOLEAN
)(!Operator
));
399 if (EFI_ERROR (Status
)) {
405 Status
= PushBool (TRUE
);
406 if (EFI_ERROR (Status
)) {
412 Status
= PushBool (FALSE
);
413 if (EFI_ERROR (Status
)) {
419 Status
= PopBool (&Operator
);
420 if (EFI_ERROR (Status
)) {
425 case EFI_DEP_REPLACE_TRUE
:
426 Status
= PushBool (TRUE
);
427 if (EFI_ERROR (Status
)) {
431 Iterator
+= sizeof (EFI_GUID
);
439 // Skip over the Dependency Op Code we just processed in the switch.
440 // The math is done out of order, but it should not matter. That is
441 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
442 // This is not an issue, since we just need the correct end result. You
443 // need to be careful using Iterator in the loop as it's intermediate value