--- /dev/null
+/*++ \r
+\r
+Copyright (c) 2006, Intel Corporation \r
+All rights reserved. This program and the accompanying materials \r
+are licensed and made available under the terms and conditions of the BSD License \r
+which accompanies this distribution. The full text of the license may be found at \r
+http://opensource.org/licenses/bsd-license.php \r
+ \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+ dependency.c\r
+\r
+Abstract:\r
+\r
+ DXE Dispatcher Dependency Evaluator\r
+\r
+ This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine\r
+ if a driver can be scheduled for execution. The criteria for\r
+ schedulability is that the dependency expression is satisfied.\r
+\r
+--*/\r
+\r
+#include <DxeMain.h>\r
+\r
+//\r
+// Global stack used to evaluate dependency expressions\r
+//\r
+BOOLEAN *mDepexEvaluationStack = NULL;\r
+BOOLEAN *mDepexEvaluationStackEnd = NULL;\r
+BOOLEAN *mDepexEvaluationStackPointer = NULL;\r
+\r
+//\r
+// Worker functions\r
+//\r
+\r
+STATIC\r
+EFI_STATUS\r
+GrowDepexStack (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Grow size of the Depex stack\r
+\r
+Arguments:\r
+\r
+ Stack - Old stack on the way in and new stack on the way out\r
+\r
+ StackSize - New size of the stack\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Stack successfully growed.\r
+ \r
+ EFI_OUT_OF_RESOURCES - There is not enough system memory to grow the stack.\r
+ \r
+ \r
+\r
+--*/\r
+{\r
+ BOOLEAN *NewStack;\r
+ UINTN Size;\r
+\r
+ Size = DEPEX_STACK_SIZE_INCREMENT;\r
+ if (mDepexEvaluationStack != NULL) {\r
+ Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);\r
+ }\r
+\r
+ NewStack = CoreAllocateBootServicesPool (Size * sizeof (BOOLEAN));\r
+ if (NewStack == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ if (mDepexEvaluationStack != NULL) {\r
+ //\r
+ // Copy to Old Stack to the New Stack\r
+ //\r
+ CopyMem (\r
+ NewStack, \r
+ mDepexEvaluationStack, \r
+ (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)\r
+ );\r
+\r
+ //\r
+ // Free The Old Stack\r
+ //\r
+ CoreFreePool (mDepexEvaluationStack);\r
+ }\r
+\r
+ //\r
+ // Make the Stack pointer point to the old data in the new stack\r
+ //\r
+ mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);\r
+ mDepexEvaluationStack = NewStack;\r
+ mDepexEvaluationStackEnd = NewStack + Size;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+STATIC\r
+EFI_STATUS\r
+PushBool (\r
+ IN BOOLEAN Value\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Push an element onto the Boolean Stack\r
+\r
+Arguments:\r
+\r
+ Value - BOOLEAN to push.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - The value was pushed onto the stack.\r
+\r
+ EFI_OUT_OF_RESOURCES - There is not enough system memory to grow the stack.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Check for a stack overflow condition\r
+ //\r
+ if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {\r
+ //\r
+ // Grow the stack\r
+ //\r
+ Status = GrowDepexStack ();\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Push the item onto the stack\r
+ //\r
+ *mDepexEvaluationStackPointer = Value;\r
+ mDepexEvaluationStackPointer++;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+STATIC\r
+EFI_STATUS \r
+PopBool (\r
+ OUT BOOLEAN *Value\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Pop an element from the Boolean stack.\r
+\r
+Arguments:\r
+\r
+ Value - BOOLEAN to pop.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - The value was popped onto the stack.\r
+\r
+ EFI_ACCESS_DENIED - The pop operation underflowed the stack\r
+\r
+--*/\r
+{\r
+ //\r
+ // Check for a stack underflow condition\r
+ //\r
+ if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+\r
+ //\r
+ // Pop the item off the stack\r
+ //\r
+ mDepexEvaluationStackPointer--;\r
+ *Value = *mDepexEvaluationStackPointer;\r
+ return EFI_SUCCESS; \r
+}\r
+\r
+\r
+EFI_STATUS\r
+CorePreProcessDepex (\r
+ IN EFI_CORE_DRIVER_ENTRY *DriverEntry \r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Preprocess dependency expression and update DriverEntry to reflect the\r
+ state of Before, After, and SOR dependencies. If DriverEntry->Before\r
+ or DriverEntry->After is set it will never be cleared. If SOR is set\r
+ it will be cleared by CoreSchedule(), and then the driver can be \r
+ dispatched.\r
+\r
+Arguments:\r
+\r
+ DriverEntry - DriverEntry element to update\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - It always works.\r
+\r
+--*/\r
+{\r
+ UINT8 *Iterator;\r
+ \r
+ Iterator = DriverEntry->Depex;\r
+ if (*Iterator == EFI_DEP_SOR) {\r
+ DriverEntry->Unrequested = TRUE;\r
+ } else {\r
+ DriverEntry->Dependent = TRUE;\r
+ }\r
+ \r
+ if (*Iterator == EFI_DEP_BEFORE) {\r
+ DriverEntry->Before = TRUE;\r
+ } else if (*Iterator == EFI_DEP_AFTER) {\r
+ DriverEntry->After = TRUE;\r
+ } \r
+\r
+ if (DriverEntry->Before || DriverEntry->After) {\r
+ CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+BOOLEAN\r
+CoreIsSchedulable (\r
+ IN EFI_CORE_DRIVER_ENTRY *DriverEntry \r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ This is the POSTFIX version of the dependency evaluator. This code does \r
+ not need to handle Before or After, as it is not valid to call this \r
+ routine in this case. The SOR is just ignored and is a nop in the grammer.\r
+\r
+ POSTFIX means all the math is done on top of the stack.\r
+\r
+Arguments:\r
+\r
+ DriverEntry - DriverEntry element to update\r
+ \r
+Returns:\r
+\r
+ TRUE - If driver is ready to run.\r
+\r
+ FALSE - If driver is not ready to run or some fatal error was found.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 *Iterator;\r
+ BOOLEAN Operator;\r
+ BOOLEAN Operator2;\r
+ EFI_GUID DriverGuid;\r
+ VOID *Interface;\r
+\r
+ if (DriverEntry->After || DriverEntry->Before) {\r
+ //\r
+ // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()\r
+ // processes them.\r
+ //\r
+ return FALSE;\r
+ }\r
+\r
+ if (DriverEntry->Depex == NULL) {\r
+ //\r
+ // A NULL Depex means treat the driver like an EFI 1.0 thing.\r
+ //\r
+ Status = CoreAllEfiServicesAvailable ();\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ return TRUE;\r
+ }\r
+\r
+ //\r
+ // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by\r
+ // incorrectly formed DEPEX expressions\r
+ //\r
+ mDepexEvaluationStackPointer = mDepexEvaluationStack;\r
+\r
+\r
+ Iterator = DriverEntry->Depex;\r
+ \r
+ while (TRUE) {\r
+ //\r
+ // Check to see if we are attempting to fetch dependency expression instructions\r
+ // past the end of the dependency expression.\r
+ //\r
+ if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r
+ return FALSE;\r
+ }\r
+\r
+ //\r
+ // Look at the opcode of the dependency expression instruction.\r
+ //\r
+ switch (*Iterator) {\r
+ case EFI_DEP_BEFORE:\r
+ case EFI_DEP_AFTER:\r
+ //\r
+ // For a well-formed Dependency Expression, the code should never get here.\r
+ // The BEFORE and AFTER are processed prior to this routine's invocation.\r
+ // If the code flow arrives at this point, there was a BEFORE or AFTER\r
+ // that were not the first opcodes.\r
+ //\r
+ ASSERT (FALSE);\r
+ case EFI_DEP_SOR:\r
+ //\r
+ // These opcodes can only appear once as the first opcode. If it is found \r
+ // at any other location, then the dependency expression evaluates to FALSE\r
+ //\r
+ if (Iterator != DriverEntry->Depex) {\r
+ return FALSE;\r
+ }\r
+ //\r
+ // Otherwise, it is the first opcode and should be treated as a NOP.\r
+ //\r
+ break;\r
+\r
+ case EFI_DEP_PUSH: \r
+ //\r
+ // Push operator is followed by a GUID. Test to see if the GUID protocol\r
+ // is installed and push the boolean result on the stack.\r
+ //\r
+ CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
+\r
+ Status = CoreLocateProtocol (&DriverGuid, NULL, &Interface);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ Status = PushBool (FALSE);\r
+ } else {\r
+ *Iterator = EFI_DEP_REPLACE_TRUE;\r
+ Status = PushBool (TRUE);\r
+ }\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Iterator += sizeof (EFI_GUID);\r
+ break;\r
+\r
+ case EFI_DEP_AND: \r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PopBool (&Operator2);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PushBool ((BOOLEAN)(Operator && Operator2));\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_OR: \r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PopBool (&Operator2);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_NOT: \r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PushBool ((BOOLEAN)(!Operator));\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_TRUE: \r
+ Status = PushBool (TRUE);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_FALSE: \r
+ Status = PushBool (FALSE);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_END: \r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ return Operator;\r
+\r
+ case EFI_DEP_REPLACE_TRUE:\r
+ Status = PushBool (TRUE);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Iterator += sizeof (EFI_GUID);\r
+ break;\r
+\r
+ default: \r
+ goto Done;\r
+ }\r
+ \r
+ //\r
+ // Skip over the Dependency Op Code we just processed in the switch.\r
+ // The math is done out of order, but it should not matter. That is\r
+ // we may add in the sizeof (EFI_GUID) before we account for the OP Code.\r
+ // This is not an issue, since we just need the correct end result. You\r
+ // need to be careful using Iterator in the loop as it's intermediate value\r
+ // may be strange.\r
+ //\r
+ Iterator++;\r
+ }\r
+\r
+Done:\r
+ return FALSE;\r
+}\r
+\r