TianoCore EDK2 master
Loading...
Searching...
No Matches
Dependency.c
Go to the documentation of this file.
1
13#include "PiSmmCore.h"
14
23#define EFI_DEP_REPLACE_TRUE 0xff
24
28#define DEPEX_STACK_SIZE_INCREMENT 0x1000
29
30//
31// Global stack used to evaluate dependency expressions
32//
33BOOLEAN *mDepexEvaluationStack = NULL;
34BOOLEAN *mDepexEvaluationStackEnd = NULL;
35BOOLEAN *mDepexEvaluationStackPointer = NULL;
36
46 VOID
47 )
48{
49 BOOLEAN *NewStack;
50 UINTN Size;
51
52 Size = DEPEX_STACK_SIZE_INCREMENT;
53 if (mDepexEvaluationStack != NULL) {
54 Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
55 }
56
57 NewStack = AllocatePool (Size * sizeof (BOOLEAN));
58 if (NewStack == NULL) {
59 return EFI_OUT_OF_RESOURCES;
60 }
61
62 if (mDepexEvaluationStack != NULL) {
63 //
64 // Copy to Old Stack to the New Stack
65 //
66 CopyMem (
67 NewStack,
68 mDepexEvaluationStack,
69 (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
70 );
71
72 //
73 // Free The Old Stack
74 //
75 FreePool (mDepexEvaluationStack);
76 }
77
78 //
79 // Make the Stack pointer point to the old data in the new stack
80 //
81 mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
82 mDepexEvaluationStack = NewStack;
83 mDepexEvaluationStackEnd = NewStack + Size;
84
85 return EFI_SUCCESS;
86}
87
99 IN BOOLEAN Value
100 )
101{
102 EFI_STATUS Status;
103
104 //
105 // Check for a stack overflow condition
106 //
107 if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
108 //
109 // Grow the stack
110 //
111 Status = GrowDepexStack ();
112 if (EFI_ERROR (Status)) {
113 return Status;
114 }
115 }
116
117 //
118 // Push the item onto the stack
119 //
120 *mDepexEvaluationStackPointer = Value;
121 mDepexEvaluationStackPointer++;
122
123 return EFI_SUCCESS;
124}
125
137 OUT BOOLEAN *Value
138 )
139{
140 //
141 // Check for a stack underflow condition
142 //
143 if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
144 return EFI_ACCESS_DENIED;
145 }
146
147 //
148 // Pop the item off the stack
149 //
150 mDepexEvaluationStackPointer--;
151 *Value = *mDepexEvaluationStackPointer;
152 return EFI_SUCCESS;
153}
154
167BOOLEAN
170 )
171{
172 EFI_STATUS Status;
173 UINT8 *Iterator;
174 BOOLEAN Operator;
175 BOOLEAN Operator2;
176 EFI_GUID DriverGuid;
177 VOID *Interface;
178
179 Operator = FALSE;
180 Operator2 = FALSE;
181
182 if (DriverEntry->After || DriverEntry->Before) {
183 //
184 // If Before or After Depex skip as SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
185 // processes them.
186 //
187 return FALSE;
188 }
189
190 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
191
192 if (DriverEntry->Depex == NULL) {
193 //
194 // A NULL Depex means that the SMM driver is not built correctly.
195 // All SMM drivers must have a valid depex expression.
196 //
197 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Depex is empty)\n"));
198 ASSERT (FALSE);
199 return FALSE;
200 }
201
202 //
203 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
204 // incorrectly formed DEPEX expressions
205 //
206 mDepexEvaluationStackPointer = mDepexEvaluationStack;
207
208 Iterator = DriverEntry->Depex;
209
210 while (TRUE) {
211 //
212 // Check to see if we are attempting to fetch dependency expression instructions
213 // past the end of the dependency expression.
214 //
215 if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
216 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
217 return FALSE;
218 }
219
220 //
221 // Look at the opcode of the dependency expression instruction.
222 //
223 switch (*Iterator) {
224 case EFI_DEP_BEFORE:
225 case EFI_DEP_AFTER:
226 //
227 // For a well-formed Dependency Expression, the code should never get here.
228 // The BEFORE and AFTER are processed prior to this routine's invocation.
229 // If the code flow arrives at this point, there was a BEFORE or AFTER
230 // that were not the first opcodes.
231 //
232 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
233 ASSERT (FALSE);
234
235 case EFI_DEP_PUSH:
236 //
237 // Push operator is followed by a GUID. Test to see if the GUID protocol
238 // is installed and push the boolean result on the stack.
239 //
240 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
241
242 Status = SmmLocateProtocol (&DriverGuid, NULL, &Interface);
243 if (EFI_ERROR (Status)) {
244 //
245 // For SMM Driver, it may depend on uefi protocols
246 //
247 Status = gBS->LocateProtocol (&DriverGuid, NULL, &Interface);
248 }
249
250 if (EFI_ERROR (Status)) {
251 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = FALSE\n", &DriverGuid));
252 Status = PushBool (FALSE);
253 } else {
254 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));
255 *Iterator = EFI_DEP_REPLACE_TRUE;
256 Status = PushBool (TRUE);
257 }
258
259 if (EFI_ERROR (Status)) {
260 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
261 return FALSE;
262 }
263
264 Iterator += sizeof (EFI_GUID);
265 break;
266
267 case EFI_DEP_AND:
268 DEBUG ((DEBUG_DISPATCH, " AND\n"));
269 Status = PopBool (&Operator);
270 if (EFI_ERROR (Status)) {
271 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
272 return FALSE;
273 }
274
275 Status = PopBool (&Operator2);
276 if (EFI_ERROR (Status)) {
277 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
278 return FALSE;
279 }
280
281 Status = PushBool ((BOOLEAN)(Operator && Operator2));
282 if (EFI_ERROR (Status)) {
283 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
284 return FALSE;
285 }
286
287 break;
288
289 case EFI_DEP_OR:
290 DEBUG ((DEBUG_DISPATCH, " OR\n"));
291 Status = PopBool (&Operator);
292 if (EFI_ERROR (Status)) {
293 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
294 return FALSE;
295 }
296
297 Status = PopBool (&Operator2);
298 if (EFI_ERROR (Status)) {
299 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
300 return FALSE;
301 }
302
303 Status = PushBool ((BOOLEAN)(Operator || Operator2));
304 if (EFI_ERROR (Status)) {
305 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
306 return FALSE;
307 }
308
309 break;
310
311 case EFI_DEP_NOT:
312 DEBUG ((DEBUG_DISPATCH, " NOT\n"));
313 Status = PopBool (&Operator);
314 if (EFI_ERROR (Status)) {
315 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
316 return FALSE;
317 }
318
319 Status = PushBool ((BOOLEAN)(!Operator));
320 if (EFI_ERROR (Status)) {
321 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
322 return FALSE;
323 }
324
325 break;
326
327 case EFI_DEP_TRUE:
328 DEBUG ((DEBUG_DISPATCH, " TRUE\n"));
329 Status = PushBool (TRUE);
330 if (EFI_ERROR (Status)) {
331 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
332 return FALSE;
333 }
334
335 break;
336
337 case EFI_DEP_FALSE:
338 DEBUG ((DEBUG_DISPATCH, " FALSE\n"));
339 Status = PushBool (FALSE);
340 if (EFI_ERROR (Status)) {
341 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
342 return FALSE;
343 }
344
345 break;
346
347 case EFI_DEP_END:
348 DEBUG ((DEBUG_DISPATCH, " END\n"));
349 Status = PopBool (&Operator);
350 if (EFI_ERROR (Status)) {
351 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
352 return FALSE;
353 }
354
355 DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
356 return Operator;
357
359 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
360 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));
361 Status = PushBool (TRUE);
362 if (EFI_ERROR (Status)) {
363 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
364 return FALSE;
365 }
366
367 Iterator += sizeof (EFI_GUID);
368 break;
369
370 default:
371 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unknown opcode)\n"));
372 goto Done;
373 }
374
375 //
376 // Skip over the Dependency Op Code we just processed in the switch.
377 // The math is done out of order, but it should not matter. That is
378 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
379 // This is not an issue, since we just need the correct end result. You
380 // need to be careful using Iterator in the loop as its intermediate value
381 // may be strange.
382 //
383 Iterator++;
384 }
385
386Done:
387 return FALSE;
388}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define EFI_DEP_REPLACE_TRUE
Definition: DxeMain.h:110
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS PopBool(OUT BOOLEAN *Value)
Definition: Dependency.c:125
EFI_STATUS GrowDepexStack(VOID)
Definition: Dependency.c:34
EFI_STATUS PushBool(IN BOOLEAN Value)
Definition: Dependency.c:87
BOOLEAN SmmIsSchedulable(IN EFI_SMM_DRIVER_ENTRY *DriverEntry)
Definition: Dependency.c:168
EFI_STATUS EFIAPI SmmLocateProtocol(IN EFI_GUID *Protocol, IN VOID *Registration OPTIONAL, OUT VOID **Interface)
Definition: Locate.c:194
#define NULL
Definition: Base.h:319
#define VOID
Definition: Base.h:269
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:435
#define EFI_DEP_BEFORE
Definition: PiDependency.h:19
#define EFI_DEP_AFTER
Definition: PiDependency.h:25
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
EFI_STATUS EFIAPI DriverEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: TdTcg2Dxe.c:2387
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
GUID EFI_GUID
Definition: UefiBaseType.h:25
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
Definition: Base.h:213