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 - 2010, 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
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 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
236 if (DriverEntry
->Depex
== NULL
) {
238 // A NULL Depex means treat the driver like an UEFI 2.0 thing.
240 Status
= CoreAllEfiServicesAvailable ();
241 DEBUG ((DEBUG_DISPATCH
, " All UEFI Services Available = "));
242 if (EFI_ERROR (Status
)) {
243 DEBUG ((DEBUG_DISPATCH
, "FALSE\n RESULT = FALSE\n"));
246 DEBUG ((DEBUG_DISPATCH
, "TRUE\n RESULT = TRUE\n"));
251 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
252 // incorrectly formed DEPEX expressions
254 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
257 Iterator
= DriverEntry
->Depex
;
261 // Check to see if we are attempting to fetch dependency expression instructions
262 // past the end of the dependency expression.
264 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
265 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
270 // Look at the opcode of the dependency expression instruction.
276 // For a well-formed Dependency Expression, the code should never get here.
277 // The BEFORE and AFTER are processed prior to this routine's invocation.
278 // If the code flow arrives at this point, there was a BEFORE or AFTER
279 // that were not the first opcodes.
281 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
285 // These opcodes can only appear once as the first opcode. If it is found
286 // at any other location, then the dependency expression evaluates to FALSE
288 if (Iterator
!= DriverEntry
->Depex
) {
289 DEBUG ((DEBUG_DISPATCH
, " SOR\n"));
290 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected SOR opcode)\n"));
293 DEBUG ((DEBUG_DISPATCH
, " SOR = Requested\n"));
295 // Otherwise, it is the first opcode and should be treated as a NOP.
301 // Push operator is followed by a GUID. Test to see if the GUID protocol
302 // is installed and push the boolean result on the stack.
304 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
306 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
308 if (EFI_ERROR (Status
)) {
309 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = FALSE\n", &DriverGuid
));
310 Status
= PushBool (FALSE
);
312 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
313 *Iterator
= EFI_DEP_REPLACE_TRUE
;
314 Status
= PushBool (TRUE
);
316 if (EFI_ERROR (Status
)) {
317 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
321 Iterator
+= sizeof (EFI_GUID
);
325 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
326 Status
= PopBool (&Operator
);
327 if (EFI_ERROR (Status
)) {
328 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
332 Status
= PopBool (&Operator2
);
333 if (EFI_ERROR (Status
)) {
334 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
338 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
339 if (EFI_ERROR (Status
)) {
340 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
346 DEBUG ((DEBUG_DISPATCH
, " OR\n"));
347 Status
= PopBool (&Operator
);
348 if (EFI_ERROR (Status
)) {
349 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
353 Status
= PopBool (&Operator2
);
354 if (EFI_ERROR (Status
)) {
355 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
359 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
360 if (EFI_ERROR (Status
)) {
361 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
367 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
368 Status
= PopBool (&Operator
);
369 if (EFI_ERROR (Status
)) {
370 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
374 Status
= PushBool ((BOOLEAN
)(!Operator
));
375 if (EFI_ERROR (Status
)) {
376 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
382 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
383 Status
= PushBool (TRUE
);
384 if (EFI_ERROR (Status
)) {
385 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
391 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
392 Status
= PushBool (FALSE
);
393 if (EFI_ERROR (Status
)) {
394 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
400 DEBUG ((DEBUG_DISPATCH
, " END\n"));
401 Status
= PopBool (&Operator
);
402 if (EFI_ERROR (Status
)) {
403 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
406 DEBUG ((DEBUG_DISPATCH
, " RESULT = %a\n", Operator
? "TRUE" : "FALSE"));
409 case EFI_DEP_REPLACE_TRUE
:
410 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
411 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
413 Status
= PushBool (TRUE
);
414 if (EFI_ERROR (Status
)) {
415 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
419 Iterator
+= sizeof (EFI_GUID
);
423 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unknown opcode)\n"));
428 // Skip over the Dependency Op Code we just processed in the switch.
429 // The math is done out of order, but it should not matter. That is
430 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
431 // This is not an issue, since we just need the correct end result. You
432 // need to be careful using Iterator in the loop as it's intermediate value