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