]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
Add code check to avoid access violation.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Dispatcher / Dispatcher.c
... / ...
CommitLineData
1/** @file\r
2 DXE Dispatcher.\r
3\r
4 Step #1 - When a FV protocol is added to the system every driver in the FV\r
5 is added to the mDiscoveredList. The SOR, Before, and After Depex are\r
6 pre-processed as drivers are added to the mDiscoveredList. If an Apriori\r
7 file exists in the FV those drivers are addeded to the\r
8 mScheduledQueue. The mFvHandleList is used to make sure a\r
9 FV is only processed once.\r
10\r
11 Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and\r
12 start it. After mScheduledQueue is drained check the\r
13 mDiscoveredList to see if any item has a Depex that is ready to\r
14 be placed on the mScheduledQueue.\r
15\r
16 Step #3 - Adding to the mScheduledQueue requires that you process Before\r
17 and After dependencies. This is done recursively as the call to add\r
18 to the mScheduledQueue checks for Before and recursively adds\r
19 all Befores. It then addes the item that was passed in and then\r
20 processess the After dependecies by recursively calling the routine.\r
21\r
22 Dispatcher Rules:\r
23 The rules for the dispatcher are in chapter 10 of the DXE CIS. Figure 10-3\r
24 is the state diagram for the DXE dispatcher\r
25\r
26 Depex - Dependency Expresion.\r
27 SOR - Schedule On Request - Don't schedule if this bit is set.\r
28\r
29Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
30This program and the accompanying materials\r
31are licensed and made available under the terms and conditions of the BSD License\r
32which accompanies this distribution. The full text of the license may be found at\r
33http://opensource.org/licenses/bsd-license.php\r
34\r
35THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
36WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
37\r
38**/\r
39\r
40#include "DxeMain.h"\r
41\r
42//\r
43// The Driver List contains one copy of every driver that has been discovered.\r
44// Items are never removed from the driver list. List of EFI_CORE_DRIVER_ENTRY\r
45//\r
46LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);\r
47\r
48//\r
49// Queue of drivers that are ready to dispatch. This queue is a subset of the\r
50// mDiscoveredList.list of EFI_CORE_DRIVER_ENTRY.\r
51//\r
52LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);\r
53\r
54//\r
55// List of handles who's Fv's have been parsed and added to the mFwDriverList.\r
56//\r
57LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList); // list of KNOWN_HANDLE\r
58\r
59//\r
60// Lock for mDiscoveredList, mScheduledQueue, gDispatcherRunning.\r
61//\r
62EFI_LOCK mDispatcherLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL);\r
63\r
64\r
65//\r
66// Flag for the DXE Dispacher. TRUE if dispatcher is execuing.\r
67//\r
68BOOLEAN gDispatcherRunning = FALSE;\r
69\r
70//\r
71// Module globals to manage the FwVol registration notification event\r
72//\r
73EFI_EVENT mFwVolEvent;\r
74VOID *mFwVolEventRegistration;\r
75\r
76//\r
77// List of file types supported by dispatcher\r
78//\r
79EFI_FV_FILETYPE mDxeFileTypes[] = {\r
80 EFI_FV_FILETYPE_DRIVER,\r
81 EFI_FV_FILETYPE_COMBINED_SMM_DXE,\r
82 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER,\r
83 EFI_FV_FILETYPE_DXE_CORE,\r
84 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\r
85};\r
86\r
87typedef struct {\r
88 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File;\r
89 EFI_DEVICE_PATH_PROTOCOL End;\r
90} FV_FILEPATH_DEVICE_PATH;\r
91\r
92FV_FILEPATH_DEVICE_PATH mFvDevicePath;\r
93\r
94\r
95//\r
96// Function Prototypes\r
97//\r
98/**\r
99 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
100 must add any driver with a before dependency on InsertedDriverEntry first.\r
101 You do this by recursively calling this routine. After all the Befores are\r
102 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
103 Then you can add any driver with an After dependency on InsertedDriverEntry\r
104 by recursively calling this routine.\r
105\r
106 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
107\r
108**/\r
109VOID\r
110CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
111 IN EFI_CORE_DRIVER_ENTRY *InsertedDriverEntry\r
112 );\r
113\r
114/**\r
115 Event notification that is fired every time a FV dispatch protocol is added.\r
116 More than one protocol may have been added when this event is fired, so you\r
117 must loop on CoreLocateHandle () to see how many protocols were added and\r
118 do the following to each FV:\r
119 If the Fv has already been processed, skip it. If the Fv has not been\r
120 processed then mark it as being processed, as we are about to process it.\r
121 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
122 mDiscoveredList is never free'ed and contains variables that define\r
123 the other states the DXE driver transitions to..\r
124 While you are at it read the A Priori file into memory.\r
125 Place drivers in the A Priori list onto the mScheduledQueue.\r
126\r
127 @param Event The Event that is being processed, not used.\r
128 @param Context Event Context, not used.\r
129\r
130**/\r
131VOID\r
132EFIAPI\r
133CoreFwVolEventProtocolNotify (\r
134 IN EFI_EVENT Event,\r
135 IN VOID *Context\r
136 );\r
137\r
138/**\r
139 Convert FvHandle and DriverName into an EFI device path\r
140\r
141 @param Fv Fv protocol, needed to read Depex info out of\r
142 FLASH.\r
143 @param FvHandle Handle for Fv, needed in the\r
144 EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
145 read out of the FV at a later time.\r
146 @param DriverName Name of driver to add to mDiscoveredList.\r
147\r
148 @return Pointer to device path constructed from FvHandle and DriverName\r
149\r
150**/\r
151EFI_DEVICE_PATH_PROTOCOL *\r
152CoreFvToDevicePath (\r
153 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
154 IN EFI_HANDLE FvHandle,\r
155 IN EFI_GUID *DriverName\r
156 );\r
157\r
158/**\r
159 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
160 and initilize any state variables. Read the Depex from the FV and store it\r
161 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.\r
162 The Discovered list is never free'ed and contains booleans that represent the\r
163 other possible DXE driver states.\r
164\r
165 @param Fv Fv protocol, needed to read Depex info out of\r
166 FLASH.\r
167 @param FvHandle Handle for Fv, needed in the\r
168 EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
169 read out of the FV at a later time.\r
170 @param DriverName Name of driver to add to mDiscoveredList.\r
171 @param Type Fv File Type of file to add to mDiscoveredList.\r
172\r
173 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
174 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
175 DriverName may be active in the system at any one\r
176 time.\r
177\r
178**/\r
179EFI_STATUS\r
180CoreAddToDriverList (\r
181 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
182 IN EFI_HANDLE FvHandle,\r
183 IN EFI_GUID *DriverName,\r
184 IN EFI_FV_FILETYPE Type\r
185 );\r
186\r
187/**\r
188 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.\r
189\r
190 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.\r
191 @param FvHandle The handle which FVB protocol installed on.\r
192 @param DriverName The driver guid specified.\r
193\r
194 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.\r
195 @retval EFI_VOLUME_CORRUPTED Corrupted volume.\r
196 @retval EFI_SUCCESS Function successfully returned.\r
197\r
198**/\r
199EFI_STATUS\r
200CoreProcessFvImageFile (\r
201 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
202 IN EFI_HANDLE FvHandle,\r
203 IN EFI_GUID *DriverName\r
204 );\r
205\r
206\r
207/**\r
208 Enter critical section by gaining lock on mDispatcherLock.\r
209\r
210**/\r
211VOID\r
212CoreAcquireDispatcherLock (\r
213 VOID\r
214 )\r
215{\r
216 CoreAcquireLock (&mDispatcherLock);\r
217}\r
218\r
219\r
220/**\r
221 Exit critical section by releasing lock on mDispatcherLock.\r
222\r
223**/\r
224VOID\r
225CoreReleaseDispatcherLock (\r
226 VOID\r
227 )\r
228{\r
229 CoreReleaseLock (&mDispatcherLock);\r
230}\r
231\r
232\r
233/**\r
234 Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
235 protocol returns an error via ReadSection defer the reading of the Depex.\r
236\r
237 @param DriverEntry Driver to work on.\r
238\r
239 @retval EFI_SUCCESS Depex read and preprossesed\r
240 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error\r
241 and Depex reading needs to be retried.\r
242 @retval Error DEPEX not found.\r
243\r
244**/\r
245EFI_STATUS\r
246CoreGetDepexSectionAndPreProccess (\r
247 IN EFI_CORE_DRIVER_ENTRY *DriverEntry\r
248 )\r
249{\r
250 EFI_STATUS Status;\r
251 EFI_SECTION_TYPE SectionType;\r
252 UINT32 AuthenticationStatus;\r
253 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
254\r
255\r
256 Fv = DriverEntry->Fv;\r
257\r
258 //\r
259 // Grab Depex info, it will never be free'ed.\r
260 //\r
261 SectionType = EFI_SECTION_DXE_DEPEX;\r
262 Status = Fv->ReadSection (\r
263 DriverEntry->Fv,\r
264 &DriverEntry->FileName,\r
265 SectionType,\r
266 0,\r
267 &DriverEntry->Depex,\r
268 (UINTN *)&DriverEntry->DepexSize,\r
269 &AuthenticationStatus\r
270 );\r
271 if (EFI_ERROR (Status)) {\r
272 if (Status == EFI_PROTOCOL_ERROR) {\r
273 //\r
274 // The section extraction protocol failed so set protocol error flag\r
275 //\r
276 DriverEntry->DepexProtocolError = TRUE;\r
277 } else {\r
278 //\r
279 // If no Depex assume UEFI 2.0 driver model\r
280 //\r
281 DriverEntry->Depex = NULL;\r
282 DriverEntry->Dependent = TRUE;\r
283 DriverEntry->DepexProtocolError = FALSE;\r
284 }\r
285 } else {\r
286 //\r
287 // Set Before, After, and Unrequested state information based on Depex\r
288 // Driver will be put in Dependent or Unrequested state\r
289 //\r
290 CorePreProcessDepex (DriverEntry);\r
291 DriverEntry->DepexProtocolError = FALSE;\r
292 }\r
293\r
294 return Status;\r
295}\r
296\r
297\r
298/**\r
299 Check every driver and locate a matching one. If the driver is found, the Unrequested\r
300 state flag is cleared.\r
301\r
302 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains\r
303 the firmware file specified by DriverName.\r
304 @param DriverName The Driver name to put in the Dependent state.\r
305\r
306 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was\r
307 cleared\r
308 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was\r
309 not set.\r
310\r
311**/\r
312EFI_STATUS\r
313EFIAPI\r
314CoreSchedule (\r
315 IN EFI_HANDLE FirmwareVolumeHandle,\r
316 IN EFI_GUID *DriverName\r
317 )\r
318{\r
319 LIST_ENTRY *Link;\r
320 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
321\r
322 //\r
323 // Check every driver\r
324 //\r
325 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
326 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
327 if (DriverEntry->FvHandle == FirmwareVolumeHandle &&\r
328 DriverEntry->Unrequested &&\r
329 CompareGuid (DriverName, &DriverEntry->FileName)) {\r
330 //\r
331 // Move the driver from the Unrequested to the Dependent state\r
332 //\r
333 CoreAcquireDispatcherLock ();\r
334 DriverEntry->Unrequested = FALSE;\r
335 DriverEntry->Dependent = TRUE;\r
336 CoreReleaseDispatcherLock ();\r
337\r
338 DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_SUCCESS\n", DriverName));\r
339 \r
340 return EFI_SUCCESS;\r
341 }\r
342 }\r
343 \r
344 DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_NOT_FOUND\n", DriverName));\r
345 \r
346 return EFI_NOT_FOUND;\r
347}\r
348\r
349\r
350\r
351/**\r
352 Convert a driver from the Untrused back to the Scheduled state.\r
353\r
354 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains\r
355 the firmware file specified by DriverName.\r
356 @param DriverName The Driver name to put in the Scheduled state\r
357\r
358 @retval EFI_SUCCESS The file was found in the untrusted state, and it\r
359 was promoted to the trusted state.\r
360 @retval EFI_NOT_FOUND The file was not found in the untrusted state.\r
361\r
362**/\r
363EFI_STATUS\r
364EFIAPI\r
365CoreTrust (\r
366 IN EFI_HANDLE FirmwareVolumeHandle,\r
367 IN EFI_GUID *DriverName\r
368 )\r
369{\r
370 LIST_ENTRY *Link;\r
371 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
372\r
373 //\r
374 // Check every driver\r
375 //\r
376 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
377 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
378 if (DriverEntry->FvHandle == FirmwareVolumeHandle &&\r
379 DriverEntry->Untrusted &&\r
380 CompareGuid (DriverName, &DriverEntry->FileName)) {\r
381 //\r
382 // Transition driver from Untrusted to Scheduled state.\r
383 //\r
384 CoreAcquireDispatcherLock ();\r
385 DriverEntry->Untrusted = FALSE;\r
386 DriverEntry->Scheduled = TRUE;\r
387 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
388 CoreReleaseDispatcherLock ();\r
389\r
390 return EFI_SUCCESS;\r
391 }\r
392 }\r
393 return EFI_NOT_FOUND;\r
394}\r
395\r
396\r
397/**\r
398 An empty function to pass error checking of CreateEventEx ().\r
399\r
400 @param Event Event whose notification function is being invoked.\r
401 @param Context Pointer to the notification function's context,\r
402 which is implementation-dependent.\r
403\r
404**/\r
405VOID\r
406EFIAPI\r
407CoreEmptyCallbackFunction (\r
408 IN EFI_EVENT Event,\r
409 IN VOID *Context\r
410 )\r
411{\r
412 return;\r
413}\r
414\r
415/**\r
416 This is the main Dispatcher for DXE and it exits when there are no more\r
417 drivers to run. Drain the mScheduledQueue and load and start a PE\r
418 image for each driver. Search the mDiscoveredList to see if any driver can\r
419 be placed on the mScheduledQueue. If no drivers are placed on the\r
420 mScheduledQueue exit the function. On exit it is assumed the Bds()\r
421 will be called, and when the Bds() exits the Dispatcher will be called\r
422 again.\r
423\r
424 @retval EFI_ALREADY_STARTED The DXE Dispatcher is already running\r
425 @retval EFI_NOT_FOUND No DXE Drivers were dispatched\r
426 @retval EFI_SUCCESS One or more DXE Drivers were dispatched\r
427\r
428**/\r
429EFI_STATUS\r
430EFIAPI\r
431CoreDispatcher (\r
432 VOID\r
433 )\r
434{\r
435 EFI_STATUS Status;\r
436 EFI_STATUS ReturnStatus;\r
437 LIST_ENTRY *Link;\r
438 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
439 BOOLEAN ReadyToRun;\r
440 EFI_EVENT DxeDispatchEvent;\r
441 \r
442\r
443 if (gDispatcherRunning) {\r
444 //\r
445 // If the dispatcher is running don't let it be restarted.\r
446 //\r
447 return EFI_ALREADY_STARTED;\r
448 }\r
449\r
450 gDispatcherRunning = TRUE;\r
451\r
452 Status = CoreCreateEventEx (\r
453 EVT_NOTIFY_SIGNAL,\r
454 TPL_NOTIFY,\r
455 CoreEmptyCallbackFunction,\r
456 NULL,\r
457 &gEfiEventDxeDispatchGuid,\r
458 &DxeDispatchEvent\r
459 );\r
460 if (EFI_ERROR (Status)) {\r
461 return Status;\r
462 }\r
463\r
464 ReturnStatus = EFI_NOT_FOUND;\r
465 do {\r
466 //\r
467 // Drain the Scheduled Queue\r
468 //\r
469 while (!IsListEmpty (&mScheduledQueue)) {\r
470 DriverEntry = CR (\r
471 mScheduledQueue.ForwardLink,\r
472 EFI_CORE_DRIVER_ENTRY,\r
473 ScheduledLink,\r
474 EFI_CORE_DRIVER_ENTRY_SIGNATURE\r
475 );\r
476\r
477 //\r
478 // Load the DXE Driver image into memory. If the Driver was transitioned from\r
479 // Untrused to Scheduled it would have already been loaded so we may need to\r
480 // skip the LoadImage\r
481 //\r
482 if (DriverEntry->ImageHandle == NULL && !DriverEntry->IsFvImage) {\r
483 DEBUG ((DEBUG_INFO, "Loading driver %g\n", &DriverEntry->FileName));\r
484 Status = CoreLoadImage (\r
485 FALSE,\r
486 gDxeCoreImageHandle,\r
487 DriverEntry->FvFileDevicePath,\r
488 NULL,\r
489 0,\r
490 &DriverEntry->ImageHandle\r
491 );\r
492\r
493 //\r
494 // Update the driver state to reflect that it's been loaded\r
495 //\r
496 if (EFI_ERROR (Status)) {\r
497 CoreAcquireDispatcherLock ();\r
498\r
499 if (Status == EFI_SECURITY_VIOLATION) {\r
500 //\r
501 // Take driver from Scheduled to Untrused state\r
502 //\r
503 DriverEntry->Untrusted = TRUE;\r
504 } else {\r
505 //\r
506 // The DXE Driver could not be loaded, and do not attempt to load or start it again.\r
507 // Take driver from Scheduled to Initialized.\r
508 //\r
509 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned\r
510 //\r
511 DriverEntry->Initialized = TRUE;\r
512 }\r
513\r
514 DriverEntry->Scheduled = FALSE;\r
515 RemoveEntryList (&DriverEntry->ScheduledLink);\r
516\r
517 CoreReleaseDispatcherLock ();\r
518\r
519 //\r
520 // If it's an error don't try the StartImage\r
521 //\r
522 continue;\r
523 }\r
524 }\r
525\r
526 CoreAcquireDispatcherLock ();\r
527\r
528 DriverEntry->Scheduled = FALSE;\r
529 DriverEntry->Initialized = TRUE;\r
530 RemoveEntryList (&DriverEntry->ScheduledLink);\r
531\r
532 CoreReleaseDispatcherLock ();\r
533\r
534 \r
535 if (DriverEntry->IsFvImage) {\r
536 //\r
537 // Produce a firmware volume block protocol for FvImage so it gets dispatched from. \r
538 //\r
539 Status = CoreProcessFvImageFile (DriverEntry->Fv, DriverEntry->FvHandle, &DriverEntry->FileName);\r
540 } else {\r
541 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
542 EFI_PROGRESS_CODE,\r
543 (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_BEGIN),\r
544 &DriverEntry->ImageHandle,\r
545 sizeof (DriverEntry->ImageHandle)\r
546 );\r
547 ASSERT (DriverEntry->ImageHandle != NULL);\r
548 \r
549 Status = CoreStartImage (DriverEntry->ImageHandle, NULL, NULL);\r
550 \r
551 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
552 EFI_PROGRESS_CODE,\r
553 (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_END),\r
554 &DriverEntry->ImageHandle,\r
555 sizeof (DriverEntry->ImageHandle)\r
556 );\r
557 }\r
558\r
559 ReturnStatus = EFI_SUCCESS;\r
560 }\r
561\r
562 //\r
563 // Now DXE Dispatcher finished one round of dispatch, signal an event group\r
564 // so that SMM Dispatcher get chance to dispatch SMM Drivers which depend\r
565 // on UEFI protocols\r
566 //\r
567 if (!EFI_ERROR (ReturnStatus)) {\r
568 CoreSignalEvent (DxeDispatchEvent);\r
569 }\r
570\r
571 //\r
572 // Search DriverList for items to place on Scheduled Queue\r
573 //\r
574 ReadyToRun = FALSE;\r
575 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
576 DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
577\r
578 if (DriverEntry->DepexProtocolError){\r
579 //\r
580 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
581 //\r
582 Status = CoreGetDepexSectionAndPreProccess (DriverEntry);\r
583 }\r
584\r
585 if (DriverEntry->Dependent) {\r
586 if (CoreIsSchedulable (DriverEntry)) {\r
587 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
588 ReadyToRun = TRUE;\r
589 }\r
590 } else {\r
591 if (DriverEntry->Unrequested) {\r
592 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
593 DEBUG ((DEBUG_DISPATCH, " SOR = Not Requested\n"));\r
594 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE\n"));\r
595 }\r
596 }\r
597 }\r
598 } while (ReadyToRun);\r
599\r
600 //\r
601 // Close DXE dispatch Event\r
602 //\r
603 CoreCloseEvent (DxeDispatchEvent);\r
604\r
605 gDispatcherRunning = FALSE;\r
606\r
607 return ReturnStatus;\r
608}\r
609\r
610\r
611/**\r
612 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
613 must add any driver with a before dependency on InsertedDriverEntry first.\r
614 You do this by recursively calling this routine. After all the Befores are\r
615 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
616 Then you can add any driver with an After dependency on InsertedDriverEntry\r
617 by recursively calling this routine.\r
618\r
619 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
620\r
621**/\r
622VOID\r
623CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
624 IN EFI_CORE_DRIVER_ENTRY *InsertedDriverEntry\r
625 )\r
626{\r
627 LIST_ENTRY *Link;\r
628 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
629\r
630 //\r
631 // Process Before Dependency\r
632 //\r
633 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
634 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
635 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
636 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
637 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
638 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
639 //\r
640 // Recursively process BEFORE\r
641 //\r
642 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
643 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
644 } else {\r
645 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
646 }\r
647 }\r
648 }\r
649\r
650 //\r
651 // Convert driver from Dependent to Scheduled state\r
652 //\r
653 CoreAcquireDispatcherLock ();\r
654\r
655 InsertedDriverEntry->Dependent = FALSE;\r
656 InsertedDriverEntry->Scheduled = TRUE;\r
657 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
658\r
659 CoreReleaseDispatcherLock ();\r
660\r
661 //\r
662 // Process After Dependency\r
663 //\r
664 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
665 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
666 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
667 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
668 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
669 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
670 //\r
671 // Recursively process AFTER\r
672 //\r
673 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
674 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
675 } else {\r
676 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
677 }\r
678 }\r
679 }\r
680}\r
681\r
682\r
683/**\r
684 Return TRUE if the Fv has been processed, FALSE if not.\r
685\r
686 @param FvHandle The handle of a FV that's being tested\r
687\r
688 @retval TRUE Fv protocol on FvHandle has been processed\r
689 @retval FALSE Fv protocol on FvHandle has not yet been processed\r
690\r
691**/\r
692BOOLEAN\r
693FvHasBeenProcessed (\r
694 IN EFI_HANDLE FvHandle\r
695 )\r
696{\r
697 LIST_ENTRY *Link;\r
698 KNOWN_HANDLE *KnownHandle;\r
699\r
700 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
701 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
702 if (KnownHandle->Handle == FvHandle) {\r
703 return TRUE;\r
704 }\r
705 }\r
706 return FALSE;\r
707}\r
708\r
709\r
710/**\r
711 Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
712 mDiscoveredList. This fucntion adds entries on the mFvHandleList if new \r
713 entry is different from one in mFvHandleList by checking FvImage Guid.\r
714 Items are never removed/freed from the mFvHandleList.\r
715\r
716 @param FvHandle The handle of a FV that has been processed\r
717\r
718 @return A point to new added FvHandle entry. If FvHandle with the same FvImage guid\r
719 has been added, NULL will return. \r
720\r
721**/\r
722KNOWN_HANDLE * \r
723FvIsBeingProcesssed (\r
724 IN EFI_HANDLE FvHandle\r
725 )\r
726{\r
727 EFI_STATUS Status;\r
728 EFI_GUID FvNameGuid;\r
729 BOOLEAN FvNameGuidIsFound;\r
730 UINT32 ExtHeaderOffset;\r
731 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
732 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
733 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;\r
734 UINTN LbaOffset;\r
735 UINTN Index;\r
736 EFI_LBA LbaIndex;\r
737 LIST_ENTRY *Link;\r
738 KNOWN_HANDLE *KnownHandle;\r
739\r
740 //\r
741 // Get the FirmwareVolumeBlock protocol on that handle\r
742 //\r
743 FvNameGuidIsFound = FALSE;\r
744 Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);\r
745 if (!EFI_ERROR (Status)) {\r
746 //\r
747 // Get the full FV header based on FVB protocol.\r
748 //\r
749 ASSERT (Fvb != NULL);\r
750 Status = GetFwVolHeader (Fvb, &FwVolHeader);\r
751 if (!EFI_ERROR (Status)) {\r
752 ASSERT (FwVolHeader != NULL);\r
753 if (VerifyFvHeaderChecksum (FwVolHeader) && FwVolHeader->ExtHeaderOffset != 0) {\r
754 ExtHeaderOffset = (UINT32) FwVolHeader->ExtHeaderOffset;\r
755 BlockMap = FwVolHeader->BlockMap;\r
756 LbaIndex = 0;\r
757 LbaOffset = 0;\r
758 //\r
759 // Find LbaIndex and LbaOffset for FV extension header based on BlockMap.\r
760 //\r
761 while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
762 for (Index = 0; Index < BlockMap->NumBlocks && ExtHeaderOffset >= BlockMap->Length; Index ++) {\r
763 ExtHeaderOffset -= BlockMap->Length;\r
764 LbaIndex ++;\r
765 }\r
766 //\r
767 // Check whether FvExtHeader is crossing the multi block range.\r
768 //\r
769 if (Index < BlockMap->NumBlocks) {\r
770 LbaOffset = ExtHeaderOffset;\r
771 break;\r
772 }\r
773 BlockMap++;\r
774 }\r
775 //\r
776 // Read FvNameGuid from FV extension header.\r
777 //\r
778 Status = ReadFvbData (Fvb, &LbaIndex, &LbaOffset, sizeof (FvNameGuid), (UINT8 *) &FvNameGuid);\r
779 if (!EFI_ERROR (Status)) {\r
780 FvNameGuidIsFound = TRUE;\r
781 }\r
782 }\r
783 CoreFreePool (FwVolHeader);\r
784 }\r
785 }\r
786\r
787 if (FvNameGuidIsFound) {\r
788 //\r
789 // Check whether the FV image with the found FvNameGuid has been processed.\r
790 //\r
791 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
792 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
793 if (CompareGuid (&FvNameGuid, &KnownHandle->FvNameGuid)) {\r
794 DEBUG ((EFI_D_ERROR, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle, KnownHandle->Handle, FvNameGuid));\r
795 return NULL;\r
796 }\r
797 }\r
798 }\r
799\r
800 KnownHandle = AllocateZeroPool (sizeof (KNOWN_HANDLE));\r
801 ASSERT (KnownHandle != NULL);\r
802\r
803 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
804 KnownHandle->Handle = FvHandle;\r
805 if (FvNameGuidIsFound) {\r
806 CopyGuid (&KnownHandle->FvNameGuid, &FvNameGuid);\r
807 }\r
808 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
809 return KnownHandle;\r
810}\r
811\r
812\r
813\r
814\r
815/**\r
816 Convert FvHandle and DriverName into an EFI device path\r
817\r
818 @param Fv Fv protocol, needed to read Depex info out of\r
819 FLASH.\r
820 @param FvHandle Handle for Fv, needed in the\r
821 EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
822 read out of the FV at a later time.\r
823 @param DriverName Name of driver to add to mDiscoveredList.\r
824\r
825 @return Pointer to device path constructed from FvHandle and DriverName\r
826\r
827**/\r
828EFI_DEVICE_PATH_PROTOCOL *\r
829CoreFvToDevicePath (\r
830 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
831 IN EFI_HANDLE FvHandle,\r
832 IN EFI_GUID *DriverName\r
833 )\r
834{\r
835 EFI_STATUS Status;\r
836 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
837 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;\r
838\r
839 //\r
840 // Remember the device path of the FV\r
841 //\r
842 Status = CoreHandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
843 if (EFI_ERROR (Status)) {\r
844 FileNameDevicePath = NULL;\r
845 } else {\r
846 //\r
847 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
848 //\r
849 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
850 SetDevicePathEndNode (&mFvDevicePath.End);\r
851\r
852 FileNameDevicePath = AppendDevicePath (\r
853 FvDevicePath,\r
854 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
855 );\r
856 }\r
857\r
858 return FileNameDevicePath;\r
859}\r
860\r
861\r
862\r
863/**\r
864 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
865 and initilize any state variables. Read the Depex from the FV and store it\r
866 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.\r
867 The Discovered list is never free'ed and contains booleans that represent the\r
868 other possible DXE driver states.\r
869\r
870 @param Fv Fv protocol, needed to read Depex info out of\r
871 FLASH.\r
872 @param FvHandle Handle for Fv, needed in the\r
873 EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
874 read out of the FV at a later time.\r
875 @param DriverName Name of driver to add to mDiscoveredList.\r
876 @param Type Fv File Type of file to add to mDiscoveredList.\r
877\r
878 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
879 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
880 DriverName may be active in the system at any one\r
881 time.\r
882\r
883**/\r
884EFI_STATUS\r
885CoreAddToDriverList (\r
886 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
887 IN EFI_HANDLE FvHandle,\r
888 IN EFI_GUID *DriverName,\r
889 IN EFI_FV_FILETYPE Type\r
890 )\r
891{\r
892 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
893\r
894\r
895 //\r
896 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
897 // NULL or FALSE.\r
898 //\r
899 DriverEntry = AllocateZeroPool (sizeof (EFI_CORE_DRIVER_ENTRY));\r
900 ASSERT (DriverEntry != NULL);\r
901 if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
902 DriverEntry->IsFvImage = TRUE;\r
903 }\r
904\r
905 DriverEntry->Signature = EFI_CORE_DRIVER_ENTRY_SIGNATURE;\r
906 CopyGuid (&DriverEntry->FileName, DriverName);\r
907 DriverEntry->FvHandle = FvHandle;\r
908 DriverEntry->Fv = Fv;\r
909 DriverEntry->FvFileDevicePath = CoreFvToDevicePath (Fv, FvHandle, DriverName);\r
910\r
911 CoreGetDepexSectionAndPreProccess (DriverEntry);\r
912\r
913 CoreAcquireDispatcherLock ();\r
914\r
915 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
916\r
917 CoreReleaseDispatcherLock ();\r
918\r
919 return EFI_SUCCESS;\r
920}\r
921\r
922\r
923/**\r
924 Check if a FV Image type file (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) is\r
925 described by a EFI_HOB_FIRMWARE_VOLUME2 Hob.\r
926\r
927 @param FvNameGuid The FV image guid specified.\r
928 @param DriverName The driver guid specified.\r
929\r
930 @retval TRUE This file is found in a EFI_HOB_FIRMWARE_VOLUME2\r
931 Hob.\r
932 @retval FALSE Not found.\r
933\r
934**/\r
935BOOLEAN\r
936FvFoundInHobFv2 (\r
937 IN CONST EFI_GUID *FvNameGuid,\r
938 IN CONST EFI_GUID *DriverName\r
939 )\r
940{\r
941 EFI_PEI_HOB_POINTERS HobFv2;\r
942\r
943 HobFv2.Raw = GetHobList ();\r
944\r
945 while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {\r
946 //\r
947 // Compare parent FvNameGuid and FileGuid both.\r
948 //\r
949 if (CompareGuid (DriverName, &HobFv2.FirmwareVolume2->FileName) &&\r
950 CompareGuid (FvNameGuid, &HobFv2.FirmwareVolume2->FvName)) {\r
951 return TRUE;\r
952 }\r
953 HobFv2.Raw = GET_NEXT_HOB (HobFv2);\r
954 }\r
955\r
956 return FALSE;\r
957}\r
958\r
959\r
960\r
961/**\r
962 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.\r
963\r
964 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.\r
965 @param FvHandle The handle which FVB protocol installed on.\r
966 @param DriverName The driver guid specified.\r
967\r
968 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.\r
969 @retval EFI_VOLUME_CORRUPTED Corrupted volume.\r
970 @retval EFI_SUCCESS Function successfully returned.\r
971\r
972**/\r
973EFI_STATUS\r
974CoreProcessFvImageFile (\r
975 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
976 IN EFI_HANDLE FvHandle,\r
977 IN EFI_GUID *DriverName\r
978 )\r
979{\r
980 EFI_STATUS Status;\r
981 EFI_SECTION_TYPE SectionType;\r
982 UINT32 AuthenticationStatus;\r
983 VOID *Buffer;\r
984 VOID *AlignedBuffer;\r
985 UINTN BufferSize;\r
986 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
987 UINT32 FvAlignment;\r
988\r
989 //\r
990 // Read the first (and only the first) firmware volume section\r
991 //\r
992 SectionType = EFI_SECTION_FIRMWARE_VOLUME_IMAGE;\r
993 FvHeader = NULL;\r
994 FvAlignment = 0;\r
995 Buffer = NULL;\r
996 BufferSize = 0;\r
997 AlignedBuffer = NULL;\r
998 Status = Fv->ReadSection (\r
999 Fv,\r
1000 DriverName,\r
1001 SectionType,\r
1002 0,\r
1003 &Buffer,\r
1004 &BufferSize,\r
1005 &AuthenticationStatus\r
1006 );\r
1007 if (!EFI_ERROR (Status)) {\r
1008 //\r
1009 // FvImage should be at its required alignment.\r
1010 //\r
1011 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) Buffer;\r
1012 //\r
1013 // Get FvHeader alignment\r
1014 //\r
1015 FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r
1016 //\r
1017 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value. \r
1018 //\r
1019 if (FvAlignment < 8) {\r
1020 FvAlignment = 8;\r
1021 }\r
1022 //\r
1023 // Allocate the aligned buffer for the FvImage.\r
1024 //\r
1025 AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN) FvAlignment);\r
1026 if (AlignedBuffer == NULL) {\r
1027 Status = EFI_OUT_OF_RESOURCES;\r
1028 } else {\r
1029 //\r
1030 // Move FvImage into the aligned buffer and release the original buffer.\r
1031 //\r
1032 CopyMem (AlignedBuffer, Buffer, BufferSize);\r
1033 CoreFreePool (Buffer);\r
1034 Buffer = NULL;\r
1035 //\r
1036 // Produce a FVB protocol for the file\r
1037 //\r
1038 Status = ProduceFVBProtocolOnBuffer (\r
1039 (EFI_PHYSICAL_ADDRESS) (UINTN) AlignedBuffer,\r
1040 (UINT64)BufferSize,\r
1041 FvHandle,\r
1042 NULL\r
1043 );\r
1044 }\r
1045 }\r
1046\r
1047 if (EFI_ERROR (Status)) {\r
1048 //\r
1049 // ReadSection or Produce FVB failed, Free data buffer\r
1050 //\r
1051 if (Buffer != NULL) {\r
1052 FreePool (Buffer);\r
1053 }\r
1054\r
1055 if (AlignedBuffer != NULL) {\r
1056 FreeAlignedPages (AlignedBuffer, EFI_SIZE_TO_PAGES (BufferSize));\r
1057 }\r
1058 }\r
1059\r
1060 return Status;\r
1061}\r
1062\r
1063\r
1064/**\r
1065 Event notification that is fired every time a FV dispatch protocol is added.\r
1066 More than one protocol may have been added when this event is fired, so you\r
1067 must loop on CoreLocateHandle () to see how many protocols were added and\r
1068 do the following to each FV:\r
1069 If the Fv has already been processed, skip it. If the Fv has not been\r
1070 processed then mark it as being processed, as we are about to process it.\r
1071 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
1072 mDiscoveredList is never free'ed and contains variables that define\r
1073 the other states the DXE driver transitions to..\r
1074 While you are at it read the A Priori file into memory.\r
1075 Place drivers in the A Priori list onto the mScheduledQueue.\r
1076\r
1077 @param Event The Event that is being processed, not used.\r
1078 @param Context Event Context, not used.\r
1079\r
1080**/\r
1081VOID\r
1082EFIAPI\r
1083CoreFwVolEventProtocolNotify (\r
1084 IN EFI_EVENT Event,\r
1085 IN VOID *Context\r
1086 )\r
1087{\r
1088 EFI_STATUS Status;\r
1089 EFI_STATUS GetNextFileStatus;\r
1090 EFI_STATUS SecurityStatus;\r
1091 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1092 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1093 EFI_HANDLE FvHandle;\r
1094 UINTN BufferSize;\r
1095 EFI_GUID NameGuid;\r
1096 UINTN Key;\r
1097 EFI_FV_FILETYPE Type;\r
1098 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1099 UINTN Size;\r
1100 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
1101 EFI_GUID *AprioriFile;\r
1102 UINTN AprioriEntryCount;\r
1103 UINTN Index;\r
1104 LIST_ENTRY *Link;\r
1105 UINT32 AuthenticationStatus;\r
1106 UINTN SizeOfBuffer;\r
1107 VOID *DepexBuffer;\r
1108 KNOWN_HANDLE *KnownHandle;\r
1109\r
1110 while (TRUE) {\r
1111 BufferSize = sizeof (EFI_HANDLE);\r
1112 Status = CoreLocateHandle (\r
1113 ByRegisterNotify,\r
1114 NULL,\r
1115 mFwVolEventRegistration,\r
1116 &BufferSize,\r
1117 &FvHandle\r
1118 );\r
1119 if (EFI_ERROR (Status)) {\r
1120 //\r
1121 // If no more notification events exit\r
1122 //\r
1123 return;\r
1124 }\r
1125\r
1126 if (FvHasBeenProcessed (FvHandle)) {\r
1127 //\r
1128 // This Fv has already been processed so lets skip it!\r
1129 //\r
1130 continue;\r
1131 }\r
1132\r
1133 //\r
1134 // Since we are about to process this Fv mark it as processed.\r
1135 //\r
1136 KnownHandle = FvIsBeingProcesssed (FvHandle);\r
1137 if (KnownHandle == NULL) {\r
1138 //\r
1139 // The FV with the same FV name guid has already been processed. \r
1140 // So lets skip it!\r
1141 //\r
1142 continue;\r
1143 }\r
1144\r
1145 Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
1146 if (EFI_ERROR (Status) || Fv == NULL) {\r
1147 //\r
1148 // FvHandle must have Firmware Volume2 protocol thus we should never get here.\r
1149 //\r
1150 ASSERT (FALSE);\r
1151 continue;\r
1152 }\r
1153\r
1154 Status = CoreHandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1155 if (EFI_ERROR (Status)) {\r
1156 //\r
1157 // The Firmware volume doesn't have device path, can't be dispatched.\r
1158 //\r
1159 continue;\r
1160 }\r
1161\r
1162 //\r
1163 // Evaluate the authentication status of the Firmware Volume through\r
1164 // Security Architectural Protocol\r
1165 //\r
1166 if (gSecurity != NULL) {\r
1167 SecurityStatus = gSecurity->FileAuthenticationState (\r
1168 gSecurity,\r
1169 0,\r
1170 FvDevicePath\r
1171 );\r
1172 if (SecurityStatus != EFI_SUCCESS) {\r
1173 //\r
1174 // Security check failed. The firmware volume should not be used for any purpose.\r
1175 //\r
1176 continue;\r
1177 }\r
1178 }\r
1179\r
1180 //\r
1181 // Discover Drivers in FV and add them to the Discovered Driver List.\r
1182 // Process EFI_FV_FILETYPE_DRIVER type and then EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER\r
1183 // EFI_FV_FILETYPE_DXE_CORE is processed to produce a Loaded Image protocol for the core\r
1184 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE is processed to create a Fvb\r
1185 //\r
1186 for (Index = 0; Index < sizeof (mDxeFileTypes) / sizeof (EFI_FV_FILETYPE); Index++) {\r
1187 //\r
1188 // Initialize the search key\r
1189 //\r
1190 Key = 0;\r
1191 do {\r
1192 Type = mDxeFileTypes[Index];\r
1193 GetNextFileStatus = Fv->GetNextFile (\r
1194 Fv,\r
1195 &Key,\r
1196 &Type,\r
1197 &NameGuid,\r
1198 &Attributes,\r
1199 &Size\r
1200 );\r
1201 if (!EFI_ERROR (GetNextFileStatus)) {\r
1202 if (Type == EFI_FV_FILETYPE_DXE_CORE) {\r
1203 //\r
1204 // If this is the DXE core fill in it's DevicePath & DeviceHandle\r
1205 //\r
1206 if (gDxeCoreLoadedImage->FilePath == NULL) {\r
1207 if (CompareGuid (&NameGuid, gDxeCoreFileName)) {\r
1208 //\r
1209 // Maybe One specail Fv cantains only one DXE_CORE module, so its device path must\r
1210 // be initialized completely.\r
1211 //\r
1212 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
1213 SetDevicePathEndNode (&mFvDevicePath.End);\r
1214\r
1215 gDxeCoreLoadedImage->FilePath = DuplicateDevicePath (\r
1216 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
1217 );\r
1218 gDxeCoreLoadedImage->DeviceHandle = FvHandle;\r
1219 }\r
1220 }\r
1221 } else if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
1222 //\r
1223 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
1224 // been extracted.\r
1225 //\r
1226 if (FvFoundInHobFv2 (&KnownHandle->FvNameGuid, &NameGuid)) {\r
1227 continue;\r
1228 }\r
1229\r
1230 //\r
1231 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has PEI depex section.\r
1232 //\r
1233 DepexBuffer = NULL;\r
1234 SizeOfBuffer = 0;\r
1235 Status = Fv->ReadSection (\r
1236 Fv,\r
1237 &NameGuid,\r
1238 EFI_SECTION_PEI_DEPEX,\r
1239 0,\r
1240 &DepexBuffer,\r
1241 &SizeOfBuffer,\r
1242 &AuthenticationStatus\r
1243 );\r
1244 if (!EFI_ERROR (Status)) {\r
1245 //\r
1246 // If PEI depex section is found, this FV image will be ignored in DXE phase.\r
1247 // Now, DxeCore doesn't support FV image with more one type DEPEX section.\r
1248 //\r
1249 FreePool (DepexBuffer);\r
1250 continue;\r
1251 }\r
1252\r
1253 //\r
1254 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has SMM depex section.\r
1255 //\r
1256 DepexBuffer = NULL;\r
1257 SizeOfBuffer = 0;\r
1258 Status = Fv->ReadSection (\r
1259 Fv,\r
1260 &NameGuid,\r
1261 EFI_SECTION_SMM_DEPEX,\r
1262 0,\r
1263 &DepexBuffer,\r
1264 &SizeOfBuffer,\r
1265 &AuthenticationStatus\r
1266 );\r
1267 if (!EFI_ERROR (Status)) {\r
1268 //\r
1269 // If SMM depex section is found, this FV image will be ignored in DXE phase.\r
1270 // Now, DxeCore doesn't support FV image with more one type DEPEX section.\r
1271 //\r
1272 FreePool (DepexBuffer);\r
1273 continue;\r
1274 }\r
1275\r
1276 //\r
1277 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has DXE depex section.\r
1278 //\r
1279 DepexBuffer = NULL;\r
1280 SizeOfBuffer = 0;\r
1281 Status = Fv->ReadSection (\r
1282 Fv,\r
1283 &NameGuid,\r
1284 EFI_SECTION_DXE_DEPEX,\r
1285 0,\r
1286 &DepexBuffer,\r
1287 &SizeOfBuffer,\r
1288 &AuthenticationStatus\r
1289 );\r
1290 if (EFI_ERROR (Status)) {\r
1291 //\r
1292 // If no depex section, produce a firmware volume block protocol for it so it gets dispatched from. \r
1293 //\r
1294 CoreProcessFvImageFile (Fv, FvHandle, &NameGuid);\r
1295 } else {\r
1296 //\r
1297 // If depex section is found, this FV image will be dispatched until its depex is evaluated to TRUE.\r
1298 //\r
1299 FreePool (DepexBuffer);\r
1300 CoreAddToDriverList (Fv, FvHandle, &NameGuid, Type);\r
1301 }\r
1302 } else {\r
1303 //\r
1304 // Transition driver from Undiscovered to Discovered state\r
1305 //\r
1306 CoreAddToDriverList (Fv, FvHandle, &NameGuid, Type);\r
1307 }\r
1308 }\r
1309 } while (!EFI_ERROR (GetNextFileStatus));\r
1310 }\r
1311\r
1312 //\r
1313 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
1314 //\r
1315 AprioriFile = NULL;\r
1316 Status = Fv->ReadSection (\r
1317 Fv,\r
1318 &gAprioriGuid,\r
1319 EFI_SECTION_RAW,\r
1320 0,\r
1321 (VOID **)&AprioriFile,\r
1322 &SizeOfBuffer,\r
1323 &AuthenticationStatus\r
1324 );\r
1325 if (!EFI_ERROR (Status)) {\r
1326 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
1327 } else {\r
1328 AprioriEntryCount = 0;\r
1329 }\r
1330\r
1331 //\r
1332 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes\r
1333 // drivers not in the current FV and these must be skipped since the a priori list\r
1334 // is only valid for the FV that it resided in.\r
1335 //\r
1336\r
1337 for (Index = 0; Index < AprioriEntryCount; Index++) {\r
1338 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1339 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
1340 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&\r
1341 (FvHandle == DriverEntry->FvHandle)) {\r
1342 CoreAcquireDispatcherLock ();\r
1343 DriverEntry->Dependent = FALSE;\r
1344 DriverEntry->Scheduled = TRUE;\r
1345 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
1346 CoreReleaseDispatcherLock ();\r
1347 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1348 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
1349 break;\r
1350 }\r
1351 }\r
1352 }\r
1353\r
1354 //\r
1355 // Free data allocated by Fv->ReadSection ()\r
1356 //\r
1357 CoreFreePool (AprioriFile);\r
1358 }\r
1359}\r
1360\r
1361\r
1362\r
1363/**\r
1364 Initialize the dispatcher. Initialize the notification function that runs when\r
1365 an FV2 protocol is added to the system.\r
1366\r
1367**/\r
1368VOID\r
1369CoreInitializeDispatcher (\r
1370 VOID\r
1371 )\r
1372{\r
1373 mFwVolEvent = EfiCreateProtocolNotifyEvent (\r
1374 &gEfiFirmwareVolume2ProtocolGuid,\r
1375 TPL_CALLBACK,\r
1376 CoreFwVolEventProtocolNotify,\r
1377 NULL,\r
1378 &mFwVolEventRegistration\r
1379 );\r
1380}\r
1381\r
1382//\r
1383// Function only used in debug builds\r
1384//\r
1385\r
1386/**\r
1387 Traverse the discovered list for any drivers that were discovered but not loaded\r
1388 because the dependency experessions evaluated to false.\r
1389\r
1390**/\r
1391VOID\r
1392CoreDisplayDiscoveredNotDispatched (\r
1393 VOID\r
1394 )\r
1395{\r
1396 LIST_ENTRY *Link;\r
1397 EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
1398\r
1399 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
1400 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
1401 if (DriverEntry->Dependent) {\r
1402 DEBUG ((DEBUG_LOAD, "Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
1403 }\r
1404 }\r
1405}\r