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 - 2018, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
16 // Global stack used to evaluate dependency expressions
18 BOOLEAN
*mDepexEvaluationStack
= NULL
;
19 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
20 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
27 Grow size of the Depex stack
29 @retval EFI_SUCCESS Stack successfully growed.
30 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
41 Size
= DEPEX_STACK_SIZE_INCREMENT
;
42 if (mDepexEvaluationStack
!= NULL
) {
43 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
46 NewStack
= AllocatePool (Size
* sizeof (BOOLEAN
));
47 if (NewStack
== NULL
) {
48 return EFI_OUT_OF_RESOURCES
;
51 if (mDepexEvaluationStack
!= NULL
) {
53 // Copy to Old Stack to the New Stack
57 mDepexEvaluationStack
,
58 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
64 FreePool (mDepexEvaluationStack
);
68 // Make the Stack pointer point to the old data in the new stack
70 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
71 mDepexEvaluationStack
= NewStack
;
72 mDepexEvaluationStackEnd
= NewStack
+ Size
;
78 Push an element onto the Boolean Stack.
80 @param Value BOOLEAN to push.
82 @retval EFI_SUCCESS The value was pushed onto the stack.
83 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
94 // Check for a stack overflow condition
96 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
100 Status
= GrowDepexStack ();
101 if (EFI_ERROR (Status
)) {
107 // Push the item onto the stack
109 *mDepexEvaluationStackPointer
= Value
;
110 mDepexEvaluationStackPointer
++;
116 Pop an element from the Boolean stack.
118 @param Value BOOLEAN to pop.
120 @retval EFI_SUCCESS The value was popped onto the stack.
121 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
130 // Check for a stack underflow condition
132 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
133 return EFI_ACCESS_DENIED
;
137 // Pop the item off the stack
139 mDepexEvaluationStackPointer
--;
140 *Value
= *mDepexEvaluationStackPointer
;
145 Preprocess dependency expression and update DriverEntry to reflect the
146 state of Before, After, and SOR dependencies. If DriverEntry->Before
147 or DriverEntry->After is set it will never be cleared. If SOR is set
148 it will be cleared by CoreSchedule(), and then the driver can be
151 @param DriverEntry DriverEntry element to update .
153 @retval EFI_SUCCESS It always works.
157 CorePreProcessDepex (
158 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
163 Iterator
= DriverEntry
->Depex
;
164 if (*Iterator
== EFI_DEP_SOR
) {
165 DriverEntry
->Unrequested
= TRUE
;
167 DriverEntry
->Dependent
= TRUE
;
170 if (*Iterator
== EFI_DEP_BEFORE
) {
171 DriverEntry
->Before
= TRUE
;
172 } else if (*Iterator
== EFI_DEP_AFTER
) {
173 DriverEntry
->After
= TRUE
;
176 if (DriverEntry
->Before
|| DriverEntry
->After
) {
177 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
184 This is the POSTFIX version of the dependency evaluator. This code does
185 not need to handle Before or After, as it is not valid to call this
186 routine in this case. The SOR is just ignored and is a nop in the grammer.
187 POSTFIX means all the math is done on top of the stack.
189 @param DriverEntry DriverEntry element to update.
191 @retval TRUE If driver is ready to run.
192 @retval FALSE If driver is not ready to run or some fatal error
198 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
211 if (DriverEntry
->After
|| DriverEntry
->Before
) {
213 // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
219 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
221 if (DriverEntry
->Depex
== NULL
) {
223 // A NULL Depex means treat the driver like an UEFI 2.0 thing.
225 Status
= CoreAllEfiServicesAvailable ();
226 DEBUG ((DEBUG_DISPATCH
, " All UEFI Services Available = "));
227 if (EFI_ERROR (Status
)) {
228 DEBUG ((DEBUG_DISPATCH
, "FALSE\n RESULT = FALSE\n"));
232 DEBUG ((DEBUG_DISPATCH
, "TRUE\n RESULT = TRUE\n"));
237 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
238 // incorrectly formed DEPEX expressions
240 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
242 Iterator
= DriverEntry
->Depex
;
246 // Check to see if we are attempting to fetch dependency expression instructions
247 // past the end of the dependency expression.
249 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
250 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
255 // Look at the opcode of the dependency expression instruction.
261 // For a well-formed Dependency Expression, the code should never get here.
262 // The BEFORE and AFTER are processed prior to this routine's invocation.
263 // If the code flow arrives at this point, there was a BEFORE or AFTER
264 // that were not the first opcodes.
266 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
270 // These opcodes can only appear once as the first opcode. If it is found
271 // at any other location, then the dependency expression evaluates to FALSE
273 if (Iterator
!= DriverEntry
->Depex
) {
274 DEBUG ((DEBUG_DISPATCH
, " SOR\n"));
275 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected SOR opcode)\n"));
279 DEBUG ((DEBUG_DISPATCH
, " SOR = Requested\n"));
281 // Otherwise, it is the first opcode and should be treated as a NOP.
287 // Push operator is followed by a GUID. Test to see if the GUID protocol
288 // is installed and push the boolean result on the stack.
290 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
292 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
294 if (EFI_ERROR (Status
)) {
295 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = FALSE\n", &DriverGuid
));
296 Status
= PushBool (FALSE
);
298 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
299 *Iterator
= EFI_DEP_REPLACE_TRUE
;
300 Status
= PushBool (TRUE
);
303 if (EFI_ERROR (Status
)) {
304 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
308 Iterator
+= sizeof (EFI_GUID
);
312 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
313 Status
= PopBool (&Operator
);
314 if (EFI_ERROR (Status
)) {
315 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
319 Status
= PopBool (&Operator2
);
320 if (EFI_ERROR (Status
)) {
321 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
325 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
326 if (EFI_ERROR (Status
)) {
327 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
334 DEBUG ((DEBUG_DISPATCH
, " OR\n"));
335 Status
= PopBool (&Operator
);
336 if (EFI_ERROR (Status
)) {
337 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
341 Status
= PopBool (&Operator2
);
342 if (EFI_ERROR (Status
)) {
343 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
347 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
348 if (EFI_ERROR (Status
)) {
349 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
356 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
357 Status
= PopBool (&Operator
);
358 if (EFI_ERROR (Status
)) {
359 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
363 Status
= PushBool ((BOOLEAN
)(!Operator
));
364 if (EFI_ERROR (Status
)) {
365 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
372 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
373 Status
= PushBool (TRUE
);
374 if (EFI_ERROR (Status
)) {
375 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
382 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
383 Status
= PushBool (FALSE
);
384 if (EFI_ERROR (Status
)) {
385 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
392 DEBUG ((DEBUG_DISPATCH
, " END\n"));
393 Status
= PopBool (&Operator
);
394 if (EFI_ERROR (Status
)) {
395 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
399 DEBUG ((DEBUG_DISPATCH
, " RESULT = %a\n", Operator
? "TRUE" : "FALSE"));
402 case EFI_DEP_REPLACE_TRUE
:
403 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
404 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
406 Status
= PushBool (TRUE
);
407 if (EFI_ERROR (Status
)) {
408 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
412 Iterator
+= sizeof (EFI_GUID
);
416 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unknown opcode)\n"));
421 // Skip over the Dependency Op Code we just processed in the switch.
422 // The math is done out of order, but it should not matter. That is
423 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
424 // This is not an issue, since we just need the correct end result. You
425 // need to be careful using Iterator in the loop as it's intermediate value