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