]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Dispatcher/Dependency.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Dispatcher / Dependency.c
CommitLineData
504214c4 1/** @file\r
e94a9ff7 2 DXE Dispatcher Dependency Evaluator.\r
504214c4
LG
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
cd5ebaa0
HT
8Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>\r
9This program and the accompanying materials\r
23c98c94 10are licensed and made available under the terms and conditions of the BSD License\r
11which accompanies this distribution. The full text of the license may be found at\r
12http://opensource.org/licenses/bsd-license.php\r
13\r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
28a00297 16\r
504214c4 17**/\r
28a00297 18\r
9c4ac31c 19#include "DxeMain.h"\r
28a00297 20\r
21//\r
22// Global stack used to evaluate dependency expressions\r
23//\r
24BOOLEAN *mDepexEvaluationStack = NULL;\r
25BOOLEAN *mDepexEvaluationStackEnd = NULL;\r
26BOOLEAN *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
022c6d45 36 @retval EFI_SUCCESS Stack successfully growed.\r
e94a9ff7 37 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.\r
162ed594 38\r
39**/\r
28a00297 40EFI_STATUS\r
41GrowDepexStack (\r
42 VOID\r
43 )\r
28a00297 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
9c4ac31c 53 NewStack = AllocatePool (Size * sizeof (BOOLEAN));\r
28a00297 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
022c6d45 63 NewStack,\r
64 mDepexEvaluationStack,\r
28a00297 65 (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)\r
66 );\r
67\r
68 //\r
69 // Free The Old Stack\r
70 //\r
9c4ac31c 71 FreePool (mDepexEvaluationStack);\r
28a00297 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
28a00297 85\r
162ed594 86/**\r
e94a9ff7 87 Push an element onto the Boolean Stack.\r
28a00297 88\r
022c6d45 89 @param Value BOOLEAN to push.\r
28a00297 90\r
022c6d45 91 @retval EFI_SUCCESS The value was pushed onto the stack.\r
e94a9ff7 92 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.\r
28a00297 93\r
162ed594 94**/\r
162ed594 95EFI_STATUS\r
96PushBool (\r
97 IN BOOLEAN Value\r
98 )\r
28a00297 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
28a00297 125\r
162ed594 126/**\r
28a00297 127 Pop an element from the Boolean stack.\r
128\r
022c6d45 129 @param Value BOOLEAN to pop.\r
28a00297 130\r
022c6d45 131 @retval EFI_SUCCESS The value was popped onto the stack.\r
e94a9ff7 132 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.\r
28a00297 133\r
162ed594 134**/\r
022c6d45 135EFI_STATUS\r
162ed594 136PopBool (\r
137 OUT BOOLEAN *Value\r
138 )\r
28a00297 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
022c6d45 152 return EFI_SUCCESS;\r
28a00297 153}\r
154\r
155\r
28a00297 156\r
162ed594 157/**\r
28a00297 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
162ed594 161 it will be cleared by CoreSchedule(), and then the driver can be\r
28a00297 162 dispatched.\r
163\r
e94a9ff7 164 @param DriverEntry DriverEntry element to update .\r
28a00297 165\r
162ed594 166 @retval EFI_SUCCESS It always works.\r
28a00297 167\r
162ed594 168**/\r
169EFI_STATUS\r
170CorePreProcessDepex (\r
022c6d45 171 IN EFI_CORE_DRIVER_ENTRY *DriverEntry\r
162ed594 172 )\r
28a00297 173{\r
174 UINT8 *Iterator;\r
022c6d45 175\r
28a00297 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
022c6d45 182\r
28a00297 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
022c6d45 187 }\r
28a00297 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
28a00297 197\r
162ed594 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
28a00297 201 routine in this case. The SOR is just ignored and is a nop in the grammer.\r
28a00297 202 POSTFIX means all the math is done on top of the stack.\r
203\r
022c6d45 204 @param DriverEntry DriverEntry element to update.\r
28a00297 205\r
022c6d45 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
162ed594 208 was found.\r
28a00297 209\r
162ed594 210**/\r
211BOOLEAN\r
212CoreIsSchedulable (\r
022c6d45 213 IN EFI_CORE_DRIVER_ENTRY *DriverEntry\r
162ed594 214 )\r
28a00297 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
e3d7cceb 223 Operator = FALSE;\r
224 Operator2 = FALSE;\r
225\r
28a00297 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 if (DriverEntry->Depex == NULL) {\r
235 //\r
8a7d75b0 236 // A NULL Depex means treat the driver like an UEFI 2.0 thing.\r
28a00297 237 //\r
238 Status = CoreAllEfiServicesAvailable ();\r
239 if (EFI_ERROR (Status)) {\r
240 return FALSE;\r
241 }\r
242 return TRUE;\r
243 }\r
244\r
245 //\r
246 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by\r
e94a9ff7 247 // incorrectly formed DEPEX expressions\r
28a00297 248 //\r
249 mDepexEvaluationStackPointer = mDepexEvaluationStack;\r
250\r
251\r
252 Iterator = DriverEntry->Depex;\r
022c6d45 253\r
28a00297 254 while (TRUE) {\r
255 //\r
256 // Check to see if we are attempting to fetch dependency expression instructions\r
257 // past the end of the dependency expression.\r
258 //\r
259 if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r
260 return FALSE;\r
261 }\r
262\r
263 //\r
264 // Look at the opcode of the dependency expression instruction.\r
265 //\r
266 switch (*Iterator) {\r
267 case EFI_DEP_BEFORE:\r
268 case EFI_DEP_AFTER:\r
269 //\r
270 // For a well-formed Dependency Expression, the code should never get here.\r
271 // The BEFORE and AFTER are processed prior to this routine's invocation.\r
272 // If the code flow arrives at this point, there was a BEFORE or AFTER\r
273 // that were not the first opcodes.\r
274 //\r
275 ASSERT (FALSE);\r
276 case EFI_DEP_SOR:\r
277 //\r
022c6d45 278 // These opcodes can only appear once as the first opcode. If it is found\r
28a00297 279 // at any other location, then the dependency expression evaluates to FALSE\r
280 //\r
281 if (Iterator != DriverEntry->Depex) {\r
282 return FALSE;\r
283 }\r
284 //\r
285 // Otherwise, it is the first opcode and should be treated as a NOP.\r
286 //\r
287 break;\r
288\r
022c6d45 289 case EFI_DEP_PUSH:\r
28a00297 290 //\r
291 // Push operator is followed by a GUID. Test to see if the GUID protocol\r
292 // is installed and push the boolean result on the stack.\r
293 //\r
294 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
295\r
296 Status = CoreLocateProtocol (&DriverGuid, NULL, &Interface);\r
297\r
298 if (EFI_ERROR (Status)) {\r
299 Status = PushBool (FALSE);\r
300 } else {\r
301 *Iterator = EFI_DEP_REPLACE_TRUE;\r
302 Status = PushBool (TRUE);\r
303 }\r
304 if (EFI_ERROR (Status)) {\r
305 return FALSE;\r
306 }\r
307\r
308 Iterator += sizeof (EFI_GUID);\r
309 break;\r
310\r
022c6d45 311 case EFI_DEP_AND:\r
28a00297 312 Status = PopBool (&Operator);\r
313 if (EFI_ERROR (Status)) {\r
314 return FALSE;\r
315 }\r
316\r
317 Status = PopBool (&Operator2);\r
318 if (EFI_ERROR (Status)) {\r
319 return FALSE;\r
320 }\r
321\r
322 Status = PushBool ((BOOLEAN)(Operator && Operator2));\r
323 if (EFI_ERROR (Status)) {\r
324 return FALSE;\r
325 }\r
326 break;\r
327\r
022c6d45 328 case EFI_DEP_OR:\r
28a00297 329 Status = PopBool (&Operator);\r
330 if (EFI_ERROR (Status)) {\r
331 return FALSE;\r
332 }\r
333\r
334 Status = PopBool (&Operator2);\r
335 if (EFI_ERROR (Status)) {\r
336 return FALSE;\r
337 }\r
338\r
339 Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
340 if (EFI_ERROR (Status)) {\r
341 return FALSE;\r
342 }\r
343 break;\r
344\r
022c6d45 345 case EFI_DEP_NOT:\r
28a00297 346 Status = PopBool (&Operator);\r
347 if (EFI_ERROR (Status)) {\r
348 return FALSE;\r
349 }\r
350\r
351 Status = PushBool ((BOOLEAN)(!Operator));\r
352 if (EFI_ERROR (Status)) {\r
353 return FALSE;\r
354 }\r
355 break;\r
356\r
022c6d45 357 case EFI_DEP_TRUE:\r
28a00297 358 Status = PushBool (TRUE);\r
359 if (EFI_ERROR (Status)) {\r
360 return FALSE;\r
361 }\r
362 break;\r
363\r
022c6d45 364 case EFI_DEP_FALSE:\r
28a00297 365 Status = PushBool (FALSE);\r
366 if (EFI_ERROR (Status)) {\r
367 return FALSE;\r
368 }\r
369 break;\r
370\r
022c6d45 371 case EFI_DEP_END:\r
28a00297 372 Status = PopBool (&Operator);\r
373 if (EFI_ERROR (Status)) {\r
374 return FALSE;\r
375 }\r
376 return Operator;\r
377\r
378 case EFI_DEP_REPLACE_TRUE:\r
379 Status = PushBool (TRUE);\r
380 if (EFI_ERROR (Status)) {\r
381 return FALSE;\r
382 }\r
383\r
384 Iterator += sizeof (EFI_GUID);\r
385 break;\r
386\r
022c6d45 387 default:\r
28a00297 388 goto Done;\r
389 }\r
022c6d45 390\r
28a00297 391 //\r
392 // Skip over the Dependency Op Code we just processed in the switch.\r
393 // The math is done out of order, but it should not matter. That is\r
394 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.\r
395 // This is not an issue, since we just need the correct end result. You\r
396 // need to be careful using Iterator in the loop as it's intermediate value\r
397 // may be strange.\r
398 //\r
399 Iterator++;\r
400 }\r
401\r
402Done:\r
403 return FALSE;\r
404}\r
405\r
162ed594 406\r