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 stack.
48 Size
= DEPEX_STACK_SIZE_INCREMENT
;
49 if (mDepexEvaluationStack
!= NULL
) {
50 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
53 NewStack
= AllocatePool (Size
* sizeof (BOOLEAN
));
54 if (NewStack
== NULL
) {
55 return EFI_OUT_OF_RESOURCES
;
58 if (mDepexEvaluationStack
!= NULL
) {
60 // Copy to Old Stack to the New Stack
64 mDepexEvaluationStack
,
65 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
71 FreePool (mDepexEvaluationStack
);
75 // Make the Stack pointer point to the old data in the new stack
77 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
78 mDepexEvaluationStack
= NewStack
;
79 mDepexEvaluationStackEnd
= NewStack
+ Size
;
87 Push an element onto the Boolean Stack.
89 @param Value BOOLEAN to push.
91 @retval EFI_SUCCESS The value was pushed onto the stack.
92 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
103 // Check for a stack overflow condition
105 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
109 Status
= GrowDepexStack ();
110 if (EFI_ERROR (Status
)) {
116 // Push the item onto the stack
118 *mDepexEvaluationStackPointer
= Value
;
119 mDepexEvaluationStackPointer
++;
127 Pop an element from the Boolean stack.
129 @param Value BOOLEAN to pop.
131 @retval EFI_SUCCESS The value was popped onto the stack.
132 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
141 // Check for a stack underflow condition
143 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
144 return EFI_ACCESS_DENIED
;
148 // Pop the item off the stack
150 mDepexEvaluationStackPointer
--;
151 *Value
= *mDepexEvaluationStackPointer
;
158 Preprocess dependency expression and update DriverEntry to reflect the
159 state of Before, After, and SOR dependencies. If DriverEntry->Before
160 or DriverEntry->After is set it will never be cleared. If SOR is set
161 it will be cleared by CoreSchedule(), and then the driver can be
164 @param DriverEntry DriverEntry element to update .
166 @retval EFI_SUCCESS It always works.
170 CorePreProcessDepex (
171 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
176 Iterator
= DriverEntry
->Depex
;
177 if (*Iterator
== EFI_DEP_SOR
) {
178 DriverEntry
->Unrequested
= TRUE
;
180 DriverEntry
->Dependent
= TRUE
;
183 if (*Iterator
== EFI_DEP_BEFORE
) {
184 DriverEntry
->Before
= TRUE
;
185 } else if (*Iterator
== EFI_DEP_AFTER
) {
186 DriverEntry
->After
= TRUE
;
189 if (DriverEntry
->Before
|| DriverEntry
->After
) {
190 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
199 This is the POSTFIX version of the dependency evaluator. This code does
200 not need to handle Before or After, as it is not valid to call this
201 routine in this case. The SOR is just ignored and is a nop in the grammer.
202 POSTFIX means all the math is done on top of the stack.
204 @param DriverEntry DriverEntry element to update.
206 @retval TRUE If driver is ready to run.
207 @retval FALSE If driver is not ready to run or some fatal error
213 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
226 if (DriverEntry
->After
|| DriverEntry
->Before
) {
228 // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
234 if (DriverEntry
->Depex
== NULL
) {
236 // A NULL Depex means treat the driver like an UEFI 2.0 thing.
238 Status
= CoreAllEfiServicesAvailable ();
239 if (EFI_ERROR (Status
)) {
246 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
247 // incorrectly formed DEPEX expressions
249 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
252 Iterator
= DriverEntry
->Depex
;
256 // Check to see if we are attempting to fetch dependency expression instructions
257 // past the end of the dependency expression.
259 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
264 // Look at the opcode of the dependency expression instruction.
270 // For a well-formed Dependency Expression, the code should never get here.
271 // The BEFORE and AFTER are processed prior to this routine's invocation.
272 // If the code flow arrives at this point, there was a BEFORE or AFTER
273 // that were not the first opcodes.
278 // These opcodes can only appear once as the first opcode. If it is found
279 // at any other location, then the dependency expression evaluates to FALSE
281 if (Iterator
!= DriverEntry
->Depex
) {
285 // Otherwise, it is the first opcode and should be treated as a NOP.
291 // Push operator is followed by a GUID. Test to see if the GUID protocol
292 // is installed and push the boolean result on the stack.
294 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
296 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
298 if (EFI_ERROR (Status
)) {
299 Status
= PushBool (FALSE
);
301 *Iterator
= EFI_DEP_REPLACE_TRUE
;
302 Status
= PushBool (TRUE
);
304 if (EFI_ERROR (Status
)) {
308 Iterator
+= sizeof (EFI_GUID
);
312 Status
= PopBool (&Operator
);
313 if (EFI_ERROR (Status
)) {
317 Status
= PopBool (&Operator2
);
318 if (EFI_ERROR (Status
)) {
322 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
323 if (EFI_ERROR (Status
)) {
329 Status
= PopBool (&Operator
);
330 if (EFI_ERROR (Status
)) {
334 Status
= PopBool (&Operator2
);
335 if (EFI_ERROR (Status
)) {
339 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
340 if (EFI_ERROR (Status
)) {
346 Status
= PopBool (&Operator
);
347 if (EFI_ERROR (Status
)) {
351 Status
= PushBool ((BOOLEAN
)(!Operator
));
352 if (EFI_ERROR (Status
)) {
358 Status
= PushBool (TRUE
);
359 if (EFI_ERROR (Status
)) {
365 Status
= PushBool (FALSE
);
366 if (EFI_ERROR (Status
)) {
372 Status
= PopBool (&Operator
);
373 if (EFI_ERROR (Status
)) {
378 case EFI_DEP_REPLACE_TRUE
:
379 Status
= PushBool (TRUE
);
380 if (EFI_ERROR (Status
)) {
384 Iterator
+= sizeof (EFI_GUID
);
392 // Skip over the Dependency Op Code we just processed in the switch.
393 // The math is done out of order, but it should not matter. That is
394 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
395 // This is not an issue, since we just need the correct end result. You
396 // need to be careful using Iterator in the loop as it's intermediate value