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