summaryrefslogtreecommitdiff
path: root/EDK/Foundation/Core/Pei/Dependency/dependency.c
blob: ff3b2e43fa0e345c0de65fa6991d936ff21b19fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*++

Copyright (c) 2004, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  dependency.c

Abstract:

  PEI Dispatcher Dependency Evaluator

  This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
  if a driver can be scheduled for execution.  The criteria for
  schedulability is that the dependency expression is satisfied.
  
--*/

#include "Tiano.h"
#include "PeiCore.h"
#include "PeiLib.h"
#include "EfiDependency.h"
#include "Dependency.h"

STATIC
BOOLEAN
IsPpiInstalled (
  IN EFI_PEI_SERVICES  **PeiServices,
  IN EVAL_STACK_ENTRY  *Stack
  )
/*++

Routine Description:

  This routine determines if a PPI has been installed.
  The truth value of a GUID is determined by if the PPI has
  been published and can be queried from the PPI database.

Arguments:
  PeiServices - The PEI core services table.
  Stack       - Reference to EVAL_STACK_ENTRY that contains PPI GUID to check

Returns:

  True if the PPI is already installed.
  False if the PPI has yet to be installed.

--*/
{
  VOID        *PeiInstance;
  EFI_STATUS  Status;
  EFI_GUID    PpiGuid;
  
  //
  // If there is no GUID to evaluate, just return current result on stack.
  //
  if (Stack->Operator == NULL) {
    return Stack->Result;
  }
  
  //
  // Copy the Guid into a locale variable so that there are no
  // possibilities of alignment faults for cross-compilation 
  // environments such as Intel?Itanium(TM).
  //
  PeiCoreCopyMem(&PpiGuid, Stack->Operator, sizeof(EFI_GUID));

  //
  // Check if the PPI is installed.
  //
  Status = (**PeiServices).LocatePpi(
                             PeiServices,
                             &PpiGuid,        // GUID
                             0,               // INSTANCE
                             NULL,            // EFI_PEI_PPI_DESCRIPTOR
                             &PeiInstance     // PPI
                             );

  if (EFI_ERROR(Status)) {
    return FALSE;
  }
   
  return TRUE;
}


EFI_STATUS
PeimDispatchReadiness (
  IN EFI_PEI_SERVICES   **PeiServices,
  IN VOID               *DependencyExpression,
  OUT BOOLEAN           *Runnable
  )
/*++

Routine Description:

  This is the POSTFIX version of the dependency evaluator.  When a
  PUSH [PPI GUID] is encountered, a pointer to the GUID is stored on
  the evaluation stack.  When that entry is poped from the evaluation
  stack, the PPI is checked if it is installed.  This method allows
  some time savings as not all PPIs must be checked for certain
  operation types (AND, OR).

Arguments:

  PeiServices               - Calling context.

  DependencyExpression      - Pointer to a dependency expression.  The Grammar adheres to 
                              the BNF described above and is stored in postfix notation.
  Runnable                  - is True if the driver can be scheduled and False if the driver 
                              cannot be scheduled.  This is the value that the schedulers 
                              should use for deciding the state of the driver.

Returns:

  Status = EFI_SUCCESS            if it is a well-formed Grammar
           EFI_INVALID_PARAMETER  if the dependency expression overflows
                                  the evaluation stack
           EFI_INVALID_PARAMETER  if the dependency expression underflows
                                  the evaluation stack
           EFI_INVALID_PARAMETER  if the dependency expression is not a
                                  well-formed Grammar.
--*/
{
  EFI_STATUS                     Status;
  DEPENDENCY_EXPRESSION_OPERAND  *Iterator;
  EVAL_STACK_ENTRY               *StackPtr;
  EVAL_STACK_ENTRY               EvalStack[MAX_GRAMMAR_SIZE];

  Status    = EFI_SUCCESS;
  Iterator  = DependencyExpression;
  *Runnable = FALSE;

  StackPtr = &EvalStack[0];

  while (TRUE) {

    switch (*(Iterator++)) {
      
      //
      // For performance reason we put the frequently used items in front of 
      // the rarely used  items
      //
      
      case (EFI_DEP_PUSH):
        //
        // Check to make sure the dependency grammar doesn't overflow the
        // EvalStack on the push
        //
        if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {
          return EFI_INVALID_PARAMETER;
        }

        //
        // Push the pointer to the PUSH opcode operator (pointer to PPI GUID)
        // We will evaluate if the PPI is insalled on the POP operation.
        //
        StackPtr->Operator = (VOID *) Iterator;
        Iterator = Iterator + sizeof (EFI_GUID);
        StackPtr++;
        break;

      case (EFI_DEP_AND):    
      case (EFI_DEP_OR):     
        //
        // Check to make sure the dependency grammar doesn't underflow the
        // EvalStack on the two POPs for the AND operation.  Don't need to
        // check for the overflow on PUSHing the result since we already
        // did two POPs.
        //
        if (StackPtr < &EvalStack[2]) {
          return EFI_INVALID_PARAMETER;
        }

        //
        // Evaluate the first POPed operator only. If the operand is
        // EFI_DEP_AND and the POPed operator evaluates to FALSE, or the
        // operand is EFI_DEP_OR and the POPed operator evaluates to TRUE,
        // we don't need to check the second operator, and the result will be
        // evaluation of the POPed operator. Otherwise, don't POP the second
        // operator since it will now evaluate to the final result on the
        // next operand that causes a POP.
        // 
        StackPtr--;
        //
        // Iterator has increased by 1 after we retrieve the operand, so here we 
        // should get the value pointed by (Iterator - 1), in order to obtain the 
        // same operand.
        //
        if (*(Iterator - 1) == EFI_DEP_AND) {
          if (!(IsPpiInstalled (PeiServices, StackPtr))) {
            (StackPtr-1)->Result = FALSE;
            (StackPtr-1)->Operator = NULL;
          }
        } else {
          if (IsPpiInstalled (PeiServices, StackPtr)) {
            (StackPtr-1)->Result = TRUE;
            (StackPtr-1)->Operator = NULL;
          }
        }
        break;
        
      case (EFI_DEP_END):
        StackPtr--;
        //
        // Check to make sure EvalStack is balanced.  If not, then there is
        // an error in the dependency grammar, so return EFI_INVALID_PARAMETER.
        //
        if (StackPtr != &EvalStack[0]) {
          return EFI_INVALID_PARAMETER;
        }
        *Runnable = IsPpiInstalled (PeiServices, StackPtr);
        return EFI_SUCCESS;
        break;

      case (EFI_DEP_NOT):    
        //
        // Check to make sure the dependency grammar doesn't underflow the
        // EvalStack on the POP for the NOT operation.  Don't need to
        // check for the overflow on PUSHing the result since we already
        // did a POP.
        //
        if (StackPtr < &EvalStack[1]) {
          return EFI_INVALID_PARAMETER;
        }
        (StackPtr-1)->Result = (BOOLEAN) !IsPpiInstalled (PeiServices, (StackPtr-1));
        (StackPtr-1)->Operator = NULL;
        break;

      case (EFI_DEP_TRUE):
      case (EFI_DEP_FALSE):
        //
        // Check to make sure the dependency grammar doesn't overflow the
        // EvalStack on the push
        //
        if (StackPtr > &EvalStack[MAX_GRAMMAR_SIZE-1]) {
          return EFI_INVALID_PARAMETER;
        }
        //
        // Iterator has increased by 1 after we retrieve the operand, so here we 
        // should get the value pointed by (Iterator - 1), in order to obtain the 
        // same operand.
        //
        if (*(Iterator - 1) == EFI_DEP_TRUE) {
          StackPtr->Result = TRUE;
        } else {
          StackPtr->Result = FALSE;
        }
        StackPtr->Operator = NULL;
        StackPtr++;
        break;

      default:
        //
        // The grammar should never arrive here
        //
        return EFI_INVALID_PARAMETER;
        break;
    }
  }
}