]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | DXE Dispatcher Dependency Evaluator.\r | |
3 | \r | |
4 | This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine\r | |
5 | if a driver can be scheduled for execution. The criteria for\r | |
6 | schedulability is that the dependency expression is satisfied.\r | |
7 | \r | |
8 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
9 | This program and the accompanying materials\r | |
10 | are licensed and made available under the terms and conditions of the BSD License\r | |
11 | which accompanies this distribution. The full text of the license may be found at\r | |
12 | http://opensource.org/licenses/bsd-license.php\r | |
13 | \r | |
14 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
15 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
16 | \r | |
17 | **/\r | |
18 | \r | |
19 | #include "DxeMain.h"\r | |
20 | \r | |
21 | //\r | |
22 | // Global stack used to evaluate dependency expressions\r | |
23 | //\r | |
24 | BOOLEAN *mDepexEvaluationStack = NULL;\r | |
25 | BOOLEAN *mDepexEvaluationStackEnd = NULL;\r | |
26 | BOOLEAN *mDepexEvaluationStackPointer = NULL;\r | |
27 | \r | |
28 | //\r | |
29 | // Worker functions\r | |
30 | //\r | |
31 | \r | |
32 | \r | |
33 | /**\r | |
34 | Grow size of the Depex stack\r | |
35 | \r | |
36 | @retval EFI_SUCCESS Stack successfully growed.\r | |
37 | @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.\r | |
38 | \r | |
39 | **/\r | |
40 | EFI_STATUS\r | |
41 | GrowDepexStack (\r | |
42 | VOID\r | |
43 | )\r | |
44 | {\r | |
45 | BOOLEAN *NewStack;\r | |
46 | UINTN Size;\r | |
47 | \r | |
48 | Size = DEPEX_STACK_SIZE_INCREMENT;\r | |
49 | if (mDepexEvaluationStack != NULL) {\r | |
50 | Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);\r | |
51 | }\r | |
52 | \r | |
53 | NewStack = AllocatePool (Size * sizeof (BOOLEAN));\r | |
54 | if (NewStack == NULL) {\r | |
55 | return EFI_OUT_OF_RESOURCES;\r | |
56 | }\r | |
57 | \r | |
58 | if (mDepexEvaluationStack != NULL) {\r | |
59 | //\r | |
60 | // Copy to Old Stack to the New Stack\r | |
61 | //\r | |
62 | CopyMem (\r | |
63 | NewStack,\r | |
64 | mDepexEvaluationStack,\r | |
65 | (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)\r | |
66 | );\r | |
67 | \r | |
68 | //\r | |
69 | // Free The Old Stack\r | |
70 | //\r | |
71 | FreePool (mDepexEvaluationStack);\r | |
72 | }\r | |
73 | \r | |
74 | //\r | |
75 | // Make the Stack pointer point to the old data in the new stack\r | |
76 | //\r | |
77 | mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);\r | |
78 | mDepexEvaluationStack = NewStack;\r | |
79 | mDepexEvaluationStackEnd = NewStack + Size;\r | |
80 | \r | |
81 | return EFI_SUCCESS;\r | |
82 | }\r | |
83 | \r | |
84 | \r | |
85 | \r | |
86 | /**\r | |
87 | Push an element onto the Boolean Stack.\r | |
88 | \r | |
89 | @param Value BOOLEAN to push.\r | |
90 | \r | |
91 | @retval EFI_SUCCESS The value was pushed onto the stack.\r | |
92 | @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.\r | |
93 | \r | |
94 | **/\r | |
95 | EFI_STATUS\r | |
96 | PushBool (\r | |
97 | IN BOOLEAN Value\r | |
98 | )\r | |
99 | {\r | |
100 | EFI_STATUS Status;\r | |
101 | \r | |
102 | //\r | |
103 | // Check for a stack overflow condition\r | |
104 | //\r | |
105 | if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {\r | |
106 | //\r | |
107 | // Grow the stack\r | |
108 | //\r | |
109 | Status = GrowDepexStack ();\r | |
110 | if (EFI_ERROR (Status)) {\r | |
111 | return Status;\r | |
112 | }\r | |
113 | }\r | |
114 | \r | |
115 | //\r | |
116 | // Push the item onto the stack\r | |
117 | //\r | |
118 | *mDepexEvaluationStackPointer = Value;\r | |
119 | mDepexEvaluationStackPointer++;\r | |
120 | \r | |
121 | return EFI_SUCCESS;\r | |
122 | }\r | |
123 | \r | |
124 | \r | |
125 | \r | |
126 | /**\r | |
127 | Pop an element from the Boolean stack.\r | |
128 | \r | |
129 | @param Value BOOLEAN to pop.\r | |
130 | \r | |
131 | @retval EFI_SUCCESS The value was popped onto the stack.\r | |
132 | @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.\r | |
133 | \r | |
134 | **/\r | |
135 | EFI_STATUS\r | |
136 | PopBool (\r | |
137 | OUT BOOLEAN *Value\r | |
138 | )\r | |
139 | {\r | |
140 | //\r | |
141 | // Check for a stack underflow condition\r | |
142 | //\r | |
143 | if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {\r | |
144 | return EFI_ACCESS_DENIED;\r | |
145 | }\r | |
146 | \r | |
147 | //\r | |
148 | // Pop the item off the stack\r | |
149 | //\r | |
150 | mDepexEvaluationStackPointer--;\r | |
151 | *Value = *mDepexEvaluationStackPointer;\r | |
152 | return EFI_SUCCESS;\r | |
153 | }\r | |
154 | \r | |
155 | \r | |
156 | \r | |
157 | /**\r | |
158 | Preprocess dependency expression and update DriverEntry to reflect the\r | |
159 | state of Before, After, and SOR dependencies. If DriverEntry->Before\r | |
160 | or DriverEntry->After is set it will never be cleared. If SOR is set\r | |
161 | it will be cleared by CoreSchedule(), and then the driver can be\r | |
162 | dispatched.\r | |
163 | \r | |
164 | @param DriverEntry DriverEntry element to update .\r | |
165 | \r | |
166 | @retval EFI_SUCCESS It always works.\r | |
167 | \r | |
168 | **/\r | |
169 | EFI_STATUS\r | |
170 | CorePreProcessDepex (\r | |
171 | IN EFI_CORE_DRIVER_ENTRY *DriverEntry\r | |
172 | )\r | |
173 | {\r | |
174 | UINT8 *Iterator;\r | |
175 | \r | |
176 | Iterator = DriverEntry->Depex;\r | |
177 | if (*Iterator == EFI_DEP_SOR) {\r | |
178 | DriverEntry->Unrequested = TRUE;\r | |
179 | } else {\r | |
180 | DriverEntry->Dependent = TRUE;\r | |
181 | }\r | |
182 | \r | |
183 | if (*Iterator == EFI_DEP_BEFORE) {\r | |
184 | DriverEntry->Before = TRUE;\r | |
185 | } else if (*Iterator == EFI_DEP_AFTER) {\r | |
186 | DriverEntry->After = TRUE;\r | |
187 | }\r | |
188 | \r | |
189 | if (DriverEntry->Before || DriverEntry->After) {\r | |
190 | CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r | |
191 | }\r | |
192 | \r | |
193 | return EFI_SUCCESS;\r | |
194 | }\r | |
195 | \r | |
196 | \r | |
197 | \r | |
198 | /**\r | |
199 | This is the POSTFIX version of the dependency evaluator. This code does\r | |
200 | not need to handle Before or After, as it is not valid to call this\r | |
201 | routine in this case. The SOR is just ignored and is a nop in the grammer.\r | |
202 | POSTFIX means all the math is done on top of the stack.\r | |
203 | \r | |
204 | @param DriverEntry DriverEntry element to update.\r | |
205 | \r | |
206 | @retval TRUE If driver is ready to run.\r | |
207 | @retval FALSE If driver is not ready to run or some fatal error\r | |
208 | was found.\r | |
209 | \r | |
210 | **/\r | |
211 | BOOLEAN\r | |
212 | CoreIsSchedulable (\r | |
213 | IN EFI_CORE_DRIVER_ENTRY *DriverEntry\r | |
214 | )\r | |
215 | {\r | |
216 | EFI_STATUS Status;\r | |
217 | UINT8 *Iterator;\r | |
218 | BOOLEAN Operator;\r | |
219 | BOOLEAN Operator2;\r | |
220 | EFI_GUID DriverGuid;\r | |
221 | VOID *Interface;\r | |
222 | \r | |
223 | Operator = FALSE;\r | |
224 | Operator2 = FALSE;\r | |
225 | \r | |
226 | if (DriverEntry->After || DriverEntry->Before) {\r | |
227 | //\r | |
228 | // If Before or After Depex skip as CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()\r | |
229 | // processes them.\r | |
230 | //\r | |
231 | return FALSE;\r | |
232 | }\r | |
233 | \r | |
234 | DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r | |
235 | \r | |
236 | if (DriverEntry->Depex == NULL) {\r | |
237 | //\r | |
238 | // A NULL Depex means treat the driver like an UEFI 2.0 thing.\r | |
239 | //\r | |
240 | Status = CoreAllEfiServicesAvailable ();\r | |
241 | DEBUG ((DEBUG_DISPATCH, " All UEFI Services Available = "));\r | |
242 | if (EFI_ERROR (Status)) {\r | |
243 | DEBUG ((DEBUG_DISPATCH, "FALSE\n RESULT = FALSE\n"));\r | |
244 | return FALSE;\r | |
245 | }\r | |
246 | DEBUG ((DEBUG_DISPATCH, "TRUE\n RESULT = TRUE\n"));\r | |
247 | return TRUE;\r | |
248 | }\r | |
249 | \r | |
250 | //\r | |
251 | // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by\r | |
252 | // incorrectly formed DEPEX expressions\r | |
253 | //\r | |
254 | mDepexEvaluationStackPointer = mDepexEvaluationStack;\r | |
255 | \r | |
256 | \r | |
257 | Iterator = DriverEntry->Depex;\r | |
258 | \r | |
259 | while (TRUE) {\r | |
260 | //\r | |
261 | // Check to see if we are attempting to fetch dependency expression instructions\r | |
262 | // past the end of the dependency expression.\r | |
263 | //\r | |
264 | if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r | |
265 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));\r | |
266 | return FALSE;\r | |
267 | }\r | |
268 | \r | |
269 | //\r | |
270 | // Look at the opcode of the dependency expression instruction.\r | |
271 | //\r | |
272 | switch (*Iterator) {\r | |
273 | case EFI_DEP_BEFORE:\r | |
274 | case EFI_DEP_AFTER:\r | |
275 | //\r | |
276 | // For a well-formed Dependency Expression, the code should never get here.\r | |
277 | // The BEFORE and AFTER are processed prior to this routine's invocation.\r | |
278 | // If the code flow arrives at this point, there was a BEFORE or AFTER\r | |
279 | // that were not the first opcodes.\r | |
280 | //\r | |
281 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));\r | |
282 | ASSERT (FALSE);\r | |
283 | case EFI_DEP_SOR:\r | |
284 | //\r | |
285 | // These opcodes can only appear once as the first opcode. If it is found\r | |
286 | // at any other location, then the dependency expression evaluates to FALSE\r | |
287 | //\r | |
288 | if (Iterator != DriverEntry->Depex) {\r | |
289 | DEBUG ((DEBUG_DISPATCH, " SOR\n"));\r | |
290 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected SOR opcode)\n"));\r | |
291 | return FALSE;\r | |
292 | }\r | |
293 | DEBUG ((DEBUG_DISPATCH, " SOR = Requested\n"));\r | |
294 | //\r | |
295 | // Otherwise, it is the first opcode and should be treated as a NOP.\r | |
296 | //\r | |
297 | break;\r | |
298 | \r | |
299 | case EFI_DEP_PUSH:\r | |
300 | //\r | |
301 | // Push operator is followed by a GUID. Test to see if the GUID protocol\r | |
302 | // is installed and push the boolean result on the stack.\r | |
303 | //\r | |
304 | CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r | |
305 | \r | |
306 | Status = CoreLocateProtocol (&DriverGuid, NULL, &Interface);\r | |
307 | \r | |
308 | if (EFI_ERROR (Status)) {\r | |
309 | DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = FALSE\n", &DriverGuid));\r | |
310 | Status = PushBool (FALSE);\r | |
311 | } else {\r | |
312 | DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r | |
313 | *Iterator = EFI_DEP_REPLACE_TRUE;\r | |
314 | Status = PushBool (TRUE);\r | |
315 | }\r | |
316 | if (EFI_ERROR (Status)) {\r | |
317 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
318 | return FALSE;\r | |
319 | }\r | |
320 | \r | |
321 | Iterator += sizeof (EFI_GUID);\r | |
322 | break;\r | |
323 | \r | |
324 | case EFI_DEP_AND:\r | |
325 | DEBUG ((DEBUG_DISPATCH, " AND\n"));\r | |
326 | Status = PopBool (&Operator);\r | |
327 | if (EFI_ERROR (Status)) {\r | |
328 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
329 | return FALSE;\r | |
330 | }\r | |
331 | \r | |
332 | Status = PopBool (&Operator2);\r | |
333 | if (EFI_ERROR (Status)) {\r | |
334 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
335 | return FALSE;\r | |
336 | }\r | |
337 | \r | |
338 | Status = PushBool ((BOOLEAN)(Operator && Operator2));\r | |
339 | if (EFI_ERROR (Status)) {\r | |
340 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
341 | return FALSE;\r | |
342 | }\r | |
343 | break;\r | |
344 | \r | |
345 | case EFI_DEP_OR:\r | |
346 | DEBUG ((DEBUG_DISPATCH, " OR\n"));\r | |
347 | Status = PopBool (&Operator);\r | |
348 | if (EFI_ERROR (Status)) {\r | |
349 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
350 | return FALSE;\r | |
351 | }\r | |
352 | \r | |
353 | Status = PopBool (&Operator2);\r | |
354 | if (EFI_ERROR (Status)) {\r | |
355 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
356 | return FALSE;\r | |
357 | }\r | |
358 | \r | |
359 | Status = PushBool ((BOOLEAN)(Operator || Operator2));\r | |
360 | if (EFI_ERROR (Status)) {\r | |
361 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
362 | return FALSE;\r | |
363 | }\r | |
364 | break;\r | |
365 | \r | |
366 | case EFI_DEP_NOT:\r | |
367 | DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r | |
368 | Status = PopBool (&Operator);\r | |
369 | if (EFI_ERROR (Status)) {\r | |
370 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
371 | return FALSE;\r | |
372 | }\r | |
373 | \r | |
374 | Status = PushBool ((BOOLEAN)(!Operator));\r | |
375 | if (EFI_ERROR (Status)) {\r | |
376 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
377 | return FALSE;\r | |
378 | }\r | |
379 | break;\r | |
380 | \r | |
381 | case EFI_DEP_TRUE:\r | |
382 | DEBUG ((DEBUG_DISPATCH, " TRUE\n"));\r | |
383 | Status = PushBool (TRUE);\r | |
384 | if (EFI_ERROR (Status)) {\r | |
385 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
386 | return FALSE;\r | |
387 | }\r | |
388 | break;\r | |
389 | \r | |
390 | case EFI_DEP_FALSE:\r | |
391 | DEBUG ((DEBUG_DISPATCH, " FALSE\n"));\r | |
392 | Status = PushBool (FALSE);\r | |
393 | if (EFI_ERROR (Status)) {\r | |
394 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
395 | return FALSE;\r | |
396 | }\r | |
397 | break;\r | |
398 | \r | |
399 | case EFI_DEP_END:\r | |
400 | DEBUG ((DEBUG_DISPATCH, " END\n"));\r | |
401 | Status = PopBool (&Operator);\r | |
402 | if (EFI_ERROR (Status)) {\r | |
403 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
404 | return FALSE;\r | |
405 | }\r | |
406 | DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", Operator ? "TRUE" : "FALSE"));\r | |
407 | return Operator;\r | |
408 | \r | |
409 | case EFI_DEP_REPLACE_TRUE:\r | |
410 | CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r | |
411 | DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r | |
412 | \r | |
413 | Status = PushBool (TRUE);\r | |
414 | if (EFI_ERROR (Status)) {\r | |
415 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r | |
416 | return FALSE;\r | |
417 | }\r | |
418 | \r | |
419 | Iterator += sizeof (EFI_GUID);\r | |
420 | break;\r | |
421 | \r | |
422 | default:\r | |
423 | DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unknown opcode)\n"));\r | |
424 | goto Done;\r | |
425 | }\r | |
426 | \r | |
427 | //\r | |
428 | // Skip over the Dependency Op Code we just processed in the switch.\r | |
429 | // The math is done out of order, but it should not matter. That is\r | |
430 | // we may add in the sizeof (EFI_GUID) before we account for the OP Code.\r | |
431 | // This is not an issue, since we just need the correct end result. You\r | |
432 | // need to be careful using Iterator in the loop as it's intermediate value\r | |
433 | // may be strange.\r | |
434 | //\r | |
435 | Iterator++;\r | |
436 | }\r | |
437 | \r | |
438 | Done:\r | |
439 | return FALSE;\r | |
440 | }\r | |
441 | \r | |
442 | \r |