3 DXE Dispatcher Dependency Evaluator
5 This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
6 if a driver can be scheduled for execution. The criteria for
7 schedulability is that the dependency expression is satisfied.
9 Copyright (c) 2006 - 2008, Intel Corporation
10 All rights reserved. This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 // Global stack used to evaluate dependency expressions
25 BOOLEAN
*mDepexEvaluationStack
= NULL
;
26 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
27 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
35 Grow size of the Depex stack
37 @retval EFI_SUCCESS Stack successfully growed.
38 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
51 Size
= DEPEX_STACK_SIZE_INCREMENT
;
52 if (mDepexEvaluationStack
!= NULL
) {
53 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
56 NewStack
= CoreAllocateBootServicesPool (Size
* sizeof (BOOLEAN
));
57 if (NewStack
== NULL
) {
58 return EFI_OUT_OF_RESOURCES
;
61 if (mDepexEvaluationStack
!= NULL
) {
63 // Copy to Old Stack to the New Stack
67 mDepexEvaluationStack
,
68 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
74 CoreFreePool (mDepexEvaluationStack
);
78 // Make the Stack pointer point to the old data in the new stack
80 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
81 mDepexEvaluationStack
= NewStack
;
82 mDepexEvaluationStackEnd
= NewStack
+ Size
;
90 Push an element onto the Boolean Stack
92 @param Value BOOLEAN to push.
94 @retval EFI_SUCCESS The value was pushed onto the stack.
95 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
108 // Check for a stack overflow condition
110 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
114 Status
= GrowDepexStack ();
115 if (EFI_ERROR (Status
)) {
121 // Push the item onto the stack
123 *mDepexEvaluationStackPointer
= Value
;
124 mDepexEvaluationStackPointer
++;
132 Pop an element from the Boolean stack.
134 @param Value BOOLEAN to pop.
136 @retval EFI_SUCCESS The value was popped onto the stack.
137 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack
147 // Check for a stack underflow condition
149 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
150 return EFI_ACCESS_DENIED
;
154 // Pop the item off the stack
156 mDepexEvaluationStackPointer
--;
157 *Value
= *mDepexEvaluationStackPointer
;
164 Preprocess dependency expression and update DriverEntry to reflect the
165 state of Before, After, and SOR dependencies. If DriverEntry->Before
166 or DriverEntry->After is set it will never be cleared. If SOR is set
167 it will be cleared by CoreSchedule(), and then the driver can be
170 @param DriverEntry DriverEntry element to update
172 @retval EFI_SUCCESS It always works.
176 CorePreProcessDepex (
177 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
182 Iterator
= DriverEntry
->Depex
;
183 if (*Iterator
== EFI_DEP_SOR
) {
184 DriverEntry
->Unrequested
= TRUE
;
186 DriverEntry
->Dependent
= TRUE
;
189 if (*Iterator
== EFI_DEP_BEFORE
) {
190 DriverEntry
->Before
= TRUE
;
191 } else if (*Iterator
== EFI_DEP_AFTER
) {
192 DriverEntry
->After
= TRUE
;
195 if (DriverEntry
->Before
|| DriverEntry
->After
) {
196 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
205 This is the POSTFIX version of the dependency evaluator. This code does
206 not need to handle Before or After, as it is not valid to call this
207 routine in this case. The SOR is just ignored and is a nop in the grammer.
208 POSTFIX means all the math is done on top of the stack.
210 @param DriverEntry DriverEntry element to update
212 @retval TRUE If driver is ready to run.
213 @retval FALSE If driver is not ready to run or some fatal error
219 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
229 if (DriverEntry
->After
|| DriverEntry
->Before
) {
231 // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
237 if (DriverEntry
->Depex
== NULL
) {
239 // A NULL Depex means treat the driver like an UEFI 2.0 thing.
241 Status
= CoreAllEfiServicesAvailable ();
242 if (EFI_ERROR (Status
)) {
249 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
250 // incorrectly formed DEPEX expressions
252 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
255 Iterator
= DriverEntry
->Depex
;
259 // Check to see if we are attempting to fetch dependency expression instructions
260 // past the end of the dependency expression.
262 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
267 // Look at the opcode of the dependency expression instruction.
273 // For a well-formed Dependency Expression, the code should never get here.
274 // The BEFORE and AFTER are processed prior to this routine's invocation.
275 // If the code flow arrives at this point, there was a BEFORE or AFTER
276 // that were not the first opcodes.
281 // These opcodes can only appear once as the first opcode. If it is found
282 // at any other location, then the dependency expression evaluates to FALSE
284 if (Iterator
!= DriverEntry
->Depex
) {
288 // Otherwise, it is the first opcode and should be treated as a NOP.
294 // Push operator is followed by a GUID. Test to see if the GUID protocol
295 // is installed and push the boolean result on the stack.
297 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
299 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
301 if (EFI_ERROR (Status
)) {
302 Status
= PushBool (FALSE
);
304 *Iterator
= EFI_DEP_REPLACE_TRUE
;
305 Status
= PushBool (TRUE
);
307 if (EFI_ERROR (Status
)) {
311 Iterator
+= sizeof (EFI_GUID
);
315 Status
= PopBool (&Operator
);
316 if (EFI_ERROR (Status
)) {
320 Status
= PopBool (&Operator2
);
321 if (EFI_ERROR (Status
)) {
325 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
326 if (EFI_ERROR (Status
)) {
332 Status
= PopBool (&Operator
);
333 if (EFI_ERROR (Status
)) {
337 Status
= PopBool (&Operator2
);
338 if (EFI_ERROR (Status
)) {
342 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
343 if (EFI_ERROR (Status
)) {
349 Status
= PopBool (&Operator
);
350 if (EFI_ERROR (Status
)) {
354 Status
= PushBool ((BOOLEAN
)(!Operator
));
355 if (EFI_ERROR (Status
)) {
361 Status
= PushBool (TRUE
);
362 if (EFI_ERROR (Status
)) {
368 Status
= PushBool (FALSE
);
369 if (EFI_ERROR (Status
)) {
375 Status
= PopBool (&Operator
);
376 if (EFI_ERROR (Status
)) {
381 case EFI_DEP_REPLACE_TRUE
:
382 Status
= PushBool (TRUE
);
383 if (EFI_ERROR (Status
)) {
387 Iterator
+= sizeof (EFI_GUID
);
395 // Skip over the Dependency Op Code we just processed in the switch.
396 // The math is done out of order, but it should not matter. That is
397 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
398 // This is not an issue, since we just need the correct end result. You
399 // need to be careful using Iterator in the loop as it's intermediate value