summaryrefslogtreecommitdiff
path: root/Core/CORE_DXE/dependency.c
blob: b08c5bfe0f974977348eb10eb9f1964bde890725 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*++ 

Copyright (c) 2004 - 2006, 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:

  DXE 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 "DxeCore.h"
#include "EfiDependency.h"

//
// Global stack used to evaluate dependency expressions
//
BOOLEAN *mDepexEvaluationStack        = NULL;
BOOLEAN *mDepexEvaluationStackEnd     = NULL;
BOOLEAN *mDepexEvaluationStackPointer = NULL;

//
// Worker functions
//

STATIC
EFI_STATUS
GrowDepexStack (
  VOID
  )
/*++

Routine Description:

  Grow size of the Depex stack

Arguments:

  Stack     - Old stack on the way in and new stack on the way out

  StackSize - New size of the stack

Returns:

  EFI_SUCCESS          - Stack successfully growed.
  
  EFI_OUT_OF_RESOURCES - There is not enough system memory to grow the stack.
  
  

--*/
{
  BOOLEAN     *NewStack;
  UINTN       Size;

  Size = DEPEX_STACK_SIZE_INCREMENT;
  if (mDepexEvaluationStack != NULL) {
    Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
  }

  NewStack = CoreAllocateBootServicesPool (Size * sizeof (BOOLEAN));
  if (NewStack == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  if (mDepexEvaluationStack != NULL) {
    //
    // Copy to Old Stack to the New Stack
    //
    EfiCommonLibCopyMem (
      NewStack, 
      mDepexEvaluationStack, 
      (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
      );

    //
    // Free The Old Stack
    //
    CoreFreePool (mDepexEvaluationStack);
  }

  //
  // Make the Stack pointer point to the old data in the new stack
  //
  mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
  mDepexEvaluationStack        = NewStack;
  mDepexEvaluationStackEnd     = NewStack + Size;

  return EFI_SUCCESS;
}


STATIC
EFI_STATUS
PushBool (
  IN BOOLEAN  Value
  )
/*++

Routine Description:

  Push an element onto the Boolean Stack

Arguments:

  Value - BOOLEAN to push.

Returns:

  EFI_SUCCESS          - The value was pushed onto the stack.

  EFI_OUT_OF_RESOURCES - There is not enough system memory to grow the stack.

--*/
{
  EFI_STATUS  Status;

  //
  // Check for a stack overflow condition
  //
  if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
    //
    // Grow the stack
    //
    Status = GrowDepexStack ();
    if (EFI_ERROR (Status)) {
      return Status;
    }
  }

  //
  // Push the item onto the stack
  //
  *mDepexEvaluationStackPointer = Value;
  mDepexEvaluationStackPointer++;

  return EFI_SUCCESS;
}


STATIC
EFI_STATUS 
PopBool (
  OUT BOOLEAN  *Value
  )
/*++

Routine Description:

  Pop an element from the Boolean stack.

Arguments:

  Value - BOOLEAN to pop.

Returns:

  EFI_SUCCESS       - The value was popped onto the stack.

  EFI_ACCESS_DENIED - The pop operation underflowed the stack

--*/
{
  //
  // Check for a stack underflow condition
  //
  if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
    return EFI_ACCESS_DENIED;
  }

  //
  // Pop the item off the stack
  //
  mDepexEvaluationStackPointer--;
  *Value = *mDepexEvaluationStackPointer;
  return EFI_SUCCESS;  
}


EFI_STATUS
CorePreProcessDepex (
  IN  EFI_CORE_DRIVER_ENTRY   *DriverEntry  
  )
/*++

Routine Description:

  Preprocess dependency expression and update DriverEntry to reflect the
  state of  Before, After, and SOR dependencies. If DriverEntry->Before
  or DriverEntry->After is set it will never be cleared. If SOR is set
  it will be cleared by CoreSchedule(), and then the driver can be 
  dispatched.

Arguments:

  DriverEntry - DriverEntry element to update

Returns:

  EFI_SUCCESS - It always works.

--*/
{
  UINT8  *Iterator;
    
  Iterator = DriverEntry->Depex;
  if (*Iterator == EFI_DEP_SOR) {
    DriverEntry->Unrequested = TRUE;
  } else {
    DriverEntry->Dependent = TRUE;
  }
    
  if (*Iterator == EFI_DEP_BEFORE) {
    DriverEntry->Before = TRUE;
  } else if (*Iterator == EFI_DEP_AFTER) {
    DriverEntry->After = TRUE;
  } 

  if (DriverEntry->Before || DriverEntry->After) {
    EfiCommonLibCopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
  }

  return EFI_SUCCESS;
}


BOOLEAN
CoreIsSchedulable (
  IN  EFI_CORE_DRIVER_ENTRY   *DriverEntry  
  )
/*++

Routine Description:

  This is the POSTFIX version of the dependency evaluator.  This code does 
  not need to handle Before or After, as it is not valid to call this 
  routine in this case. The SOR is just ignored and is a nop in the grammer.

  POSTFIX means all the math is done on top of the stack.

Arguments:

  DriverEntry - DriverEntry element to update
  
Returns:

  TRUE  - If driver is ready to run.
  FALSE - If driver is not ready to run or some fatal error was found.

--*/
{
  EFI_STATUS  Status;
  UINT8       *Iterator;
  BOOLEAN     Operator;
  BOOLEAN     Operator2;
  EFI_GUID    DriverGuid;
  VOID        *Interface;

  if (DriverEntry->After || DriverEntry->Before) {
    //
    // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
    // processes them.
    //
    return FALSE;
  }

  if (DriverEntry->Depex == NULL) {
    //
    // A NULL Depex means treat the driver like an EFI 1.0 thing.
    //
    Status = CoreAllEfiServicesAvailable ();
    if (EFI_ERROR (Status)) {
      return FALSE;
    }
    return TRUE;
  }

  //
  // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
  //  incorrectly formed DEPEX expressions
  //
  mDepexEvaluationStackPointer = mDepexEvaluationStack;


  Iterator = DriverEntry->Depex;
  
  while (TRUE) {
    //
    // Check to see if we are attempting to fetch dependency expression instructions
    // past the end of the dependency expression.
    //
    if (((UINTN) Iterator - (UINTN) DriverEntry->Depex) >= DriverEntry->DepexSize) {
      return FALSE;
    }

    //
    // Look at the opcode of the dependency expression instruction.
    //
    switch (*Iterator) {
    case EFI_DEP_BEFORE:
    case EFI_DEP_AFTER:
      //
      // For a well-formed Dependency Expression, the code should never get here.
      // The BEFORE and AFTER are processed prior to this routine's invocation.
      // If the code flow arrives at this point, there was a BEFORE or AFTER
      // that were not the first opcodes.
      //
      ASSERT (FALSE);
    case EFI_DEP_SOR:
      //
      // These opcodes can only appear once as the first opcode.  If it is found 
      // at any other location, then the dependency expression evaluates to FALSE
      //
      if (Iterator != DriverEntry->Depex) {
        return FALSE;
      }
      //
      // Otherwise, it is the first opcode and should be treated as a NOP.
      //
      break;

    case EFI_DEP_PUSH:  
      //
      // Push operator is followed by a GUID. Test to see if the GUID protocol
      // is installed and push the boolean result on the stack.
      //
      EfiCommonLibCopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));

      Status = CoreLocateProtocol (&DriverGuid, NULL, &Interface);

      if (EFI_ERROR (Status)) {
        Status = PushBool (FALSE);
      } else {
        *Iterator = EFI_DEP_REPLACE_TRUE;
        Status = PushBool (TRUE);
      }
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Iterator += sizeof (EFI_GUID);
      break;

    case EFI_DEP_AND:    
      Status = PopBool (&Operator);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Status = PopBool (&Operator2);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Status = PushBool ((BOOLEAN) (Operator && Operator2));
      if (EFI_ERROR (Status)) {
        return FALSE;
      }
      break;

    case EFI_DEP_OR:     
      Status = PopBool (&Operator);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Status = PopBool (&Operator2);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Status = PushBool ((BOOLEAN) (Operator || Operator2));
      if (EFI_ERROR (Status)) {
        return FALSE;
      }
      break;

    case EFI_DEP_NOT:    
      Status = PopBool (&Operator);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Status = PushBool ((BOOLEAN) (!Operator));
      if (EFI_ERROR (Status)) {
        return FALSE;
      }
      break;

    case EFI_DEP_TRUE:   
      Status = PushBool (TRUE);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }
      break;

    case EFI_DEP_FALSE: 
      Status = PushBool (FALSE);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }
      break;

    case EFI_DEP_END:    
      Status = PopBool (&Operator);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }
      return Operator;

    case EFI_DEP_REPLACE_TRUE:
      Status = PushBool (TRUE);
      if (EFI_ERROR (Status)) {
        return FALSE;
      }

      Iterator += sizeof (EFI_GUID);
      break;

    default:      
      return FALSE;
    }
    
    //
    // Skip over the Dependency Op Code we just processed in the switch.
    // The math is done out of order, but it should not matter. That is
    // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
    // This is not an issue, since we just need the correct end result. You
    // need to be careful using Iterator in the loop as it's intermediate value
    // may be strange.
    //
    Iterator++;
  }
  return FALSE;
}