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