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
;
28 Grow size of the Depex stack
30 @retval EFI_SUCCESS Stack successfully growed.
31 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
42 Size
= DEPEX_STACK_SIZE_INCREMENT
;
43 if (mDepexEvaluationStack
!= NULL
) {
44 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
47 NewStack
= AllocatePool (Size
* sizeof (BOOLEAN
));
48 if (NewStack
== NULL
) {
49 return EFI_OUT_OF_RESOURCES
;
52 if (mDepexEvaluationStack
!= NULL
) {
54 // Copy to Old Stack to the New Stack
58 mDepexEvaluationStack
,
59 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
65 FreePool (mDepexEvaluationStack
);
69 // Make the Stack pointer point to the old data in the new stack
71 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
72 mDepexEvaluationStack
= NewStack
;
73 mDepexEvaluationStackEnd
= NewStack
+ Size
;
81 Push an element onto the Boolean Stack.
83 @param Value BOOLEAN to push.
85 @retval EFI_SUCCESS The value was pushed onto the stack.
86 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
97 // Check for a stack overflow condition
99 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
103 Status
= GrowDepexStack ();
104 if (EFI_ERROR (Status
)) {
110 // Push the item onto the stack
112 *mDepexEvaluationStackPointer
= Value
;
113 mDepexEvaluationStackPointer
++;
121 Pop an element from the Boolean stack.
123 @param Value BOOLEAN to pop.
125 @retval EFI_SUCCESS The value was popped onto the stack.
126 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
135 // Check for a stack underflow condition
137 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
138 return EFI_ACCESS_DENIED
;
142 // Pop the item off the stack
144 mDepexEvaluationStackPointer
--;
145 *Value
= *mDepexEvaluationStackPointer
;
152 Preprocess dependency expression and update DriverEntry to reflect the
153 state of Before, After, and SOR dependencies. If DriverEntry->Before
154 or DriverEntry->After is set it will never be cleared. If SOR is set
155 it will be cleared by CoreSchedule(), and then the driver can be
158 @param DriverEntry DriverEntry element to update .
160 @retval EFI_SUCCESS It always works.
164 CorePreProcessDepex (
165 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
170 Iterator
= DriverEntry
->Depex
;
171 if (*Iterator
== EFI_DEP_SOR
) {
172 DriverEntry
->Unrequested
= TRUE
;
174 DriverEntry
->Dependent
= TRUE
;
177 if (*Iterator
== EFI_DEP_BEFORE
) {
178 DriverEntry
->Before
= TRUE
;
179 } else if (*Iterator
== EFI_DEP_AFTER
) {
180 DriverEntry
->After
= TRUE
;
183 if (DriverEntry
->Before
|| DriverEntry
->After
) {
184 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
193 This is the POSTFIX version of the dependency evaluator. This code does
194 not need to handle Before or After, as it is not valid to call this
195 routine in this case. The SOR is just ignored and is a nop in the grammer.
196 POSTFIX means all the math is done on top of the stack.
198 @param DriverEntry DriverEntry element to update.
200 @retval TRUE If driver is ready to run.
201 @retval FALSE If driver is not ready to run or some fatal error
207 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
220 if (DriverEntry
->After
|| DriverEntry
->Before
) {
222 // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
228 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
230 if (DriverEntry
->Depex
== NULL
) {
232 // A NULL Depex means treat the driver like an UEFI 2.0 thing.
234 Status
= CoreAllEfiServicesAvailable ();
235 DEBUG ((DEBUG_DISPATCH
, " All UEFI Services Available = "));
236 if (EFI_ERROR (Status
)) {
237 DEBUG ((DEBUG_DISPATCH
, "FALSE\n RESULT = FALSE\n"));
240 DEBUG ((DEBUG_DISPATCH
, "TRUE\n RESULT = TRUE\n"));
245 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
246 // incorrectly formed DEPEX expressions
248 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
251 Iterator
= DriverEntry
->Depex
;
255 // Check to see if we are attempting to fetch dependency expression instructions
256 // past the end of the dependency expression.
258 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
259 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
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.
275 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
279 // These opcodes can only appear once as the first opcode. If it is found
280 // at any other location, then the dependency expression evaluates to FALSE
282 if (Iterator
!= DriverEntry
->Depex
) {
283 DEBUG ((DEBUG_DISPATCH
, " SOR\n"));
284 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected SOR opcode)\n"));
287 DEBUG ((DEBUG_DISPATCH
, " SOR = Requested\n"));
289 // Otherwise, it is the first opcode and should be treated as a NOP.
295 // Push operator is followed by a GUID. Test to see if the GUID protocol
296 // is installed and push the boolean result on the stack.
298 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
300 Status
= CoreLocateProtocol (&DriverGuid
, NULL
, &Interface
);
302 if (EFI_ERROR (Status
)) {
303 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = FALSE\n", &DriverGuid
));
304 Status
= PushBool (FALSE
);
306 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
307 *Iterator
= EFI_DEP_REPLACE_TRUE
;
308 Status
= PushBool (TRUE
);
310 if (EFI_ERROR (Status
)) {
311 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
315 Iterator
+= sizeof (EFI_GUID
);
319 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
320 Status
= PopBool (&Operator
);
321 if (EFI_ERROR (Status
)) {
322 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
326 Status
= PopBool (&Operator2
);
327 if (EFI_ERROR (Status
)) {
328 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
332 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
333 if (EFI_ERROR (Status
)) {
334 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
340 DEBUG ((DEBUG_DISPATCH
, " OR\n"));
341 Status
= PopBool (&Operator
);
342 if (EFI_ERROR (Status
)) {
343 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
347 Status
= PopBool (&Operator2
);
348 if (EFI_ERROR (Status
)) {
349 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
353 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
354 if (EFI_ERROR (Status
)) {
355 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
361 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
362 Status
= PopBool (&Operator
);
363 if (EFI_ERROR (Status
)) {
364 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
368 Status
= PushBool ((BOOLEAN
)(!Operator
));
369 if (EFI_ERROR (Status
)) {
370 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
376 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
377 Status
= PushBool (TRUE
);
378 if (EFI_ERROR (Status
)) {
379 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
385 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
386 Status
= PushBool (FALSE
);
387 if (EFI_ERROR (Status
)) {
388 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
394 DEBUG ((DEBUG_DISPATCH
, " END\n"));
395 Status
= PopBool (&Operator
);
396 if (EFI_ERROR (Status
)) {
397 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
400 DEBUG ((DEBUG_DISPATCH
, " RESULT = %a\n", Operator
? "TRUE" : "FALSE"));
403 case EFI_DEP_REPLACE_TRUE
:
404 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
405 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
407 Status
= PushBool (TRUE
);
408 if (EFI_ERROR (Status
)) {
409 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
413 Iterator
+= sizeof (EFI_GUID
);
417 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unknown opcode)\n"));
422 // Skip over the Dependency Op Code we just processed in the switch.
423 // The math is done out of order, but it should not matter. That is
424 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
425 // This is not an issue, since we just need the correct end result. You
426 // need to be careful using Iterator in the loop as it's intermediate value