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