4 Step #1 - When a FV protocol is added to the system every driver in the FV
5 is added to the mDiscoveredList. The SOR, Before, and After Depex are
6 pre-processed as drivers are added to the mDiscoveredList. If an Apriori
7 file exists in the FV those drivers are addeded to the
8 mScheduledQueue. The mFvHandleList is used to make sure a
9 FV is only processed once.
11 Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
12 start it. After mScheduledQueue is drained check the
13 mDiscoveredList to see if any item has a Depex that is ready to
14 be placed on the mScheduledQueue.
16 Step #3 - Adding to the mScheduledQueue requires that you process Before
17 and After dependencies. This is done recursively as the call to add
18 to the mScheduledQueue checks for Before and recursively adds
19 all Befores. It then addes the item that was passed in and then
20 processess the After dependecies by recursively calling the routine.
23 The rules for the dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
24 is the state diagram for the DXE dispatcher
26 Depex - Dependency Expresion.
27 SOR - Schedule On Request - Don't schedule if this bit is set.
29 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
30 This program and the accompanying materials
31 are licensed and made available under the terms and conditions of the BSD License
32 which accompanies this distribution. The full text of the license may be found at
33 http://opensource.org/licenses/bsd-license.php
35 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
36 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
43 // The Driver List contains one copy of every driver that has been discovered.
44 // Items are never removed from the driver list. List of EFI_CORE_DRIVER_ENTRY
46 LIST_ENTRY mDiscoveredList
= INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList
);
49 // Queue of drivers that are ready to dispatch. This queue is a subset of the
50 // mDiscoveredList.list of EFI_CORE_DRIVER_ENTRY.
52 LIST_ENTRY mScheduledQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue
);
55 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
57 LIST_ENTRY mFvHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList
); // list of KNOWN_HANDLE
60 // Lock for mDiscoveredList, mScheduledQueue, gDispatcherRunning.
62 EFI_LOCK mDispatcherLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
);
66 // Flag for the DXE Dispacher. TRUE if dispatcher is execuing.
68 BOOLEAN gDispatcherRunning
= FALSE
;
71 // Module globals to manage the FwVol registration notification event
73 EFI_EVENT mFwVolEvent
;
74 VOID
*mFwVolEventRegistration
;
77 // List of file types supported by dispatcher
79 EFI_FV_FILETYPE mDxeFileTypes
[] = {
80 EFI_FV_FILETYPE_DRIVER
,
81 EFI_FV_FILETYPE_COMBINED_SMM_DXE
,
82 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
,
83 EFI_FV_FILETYPE_DXE_CORE
,
84 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
88 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File
;
89 EFI_DEVICE_PATH_PROTOCOL End
;
90 } FV_FILEPATH_DEVICE_PATH
;
92 FV_FILEPATH_DEVICE_PATH mFvDevicePath
= {
98 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
100 END_DEVICE_PATH_LENGTH
105 // Function Prototypes
108 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
109 must add any driver with a before dependency on InsertedDriverEntry first.
110 You do this by recursively calling this routine. After all the Befores are
111 processed you can add InsertedDriverEntry to the mScheduledQueue.
112 Then you can add any driver with an After dependency on InsertedDriverEntry
113 by recursively calling this routine.
115 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
119 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
120 IN EFI_CORE_DRIVER_ENTRY
*InsertedDriverEntry
124 Event notification that is fired every time a FV dispatch protocol is added.
125 More than one protocol may have been added when this event is fired, so you
126 must loop on CoreLocateHandle () to see how many protocols were added and
127 do the following to each FV:
128 If the Fv has already been processed, skip it. If the Fv has not been
129 processed then mark it as being processed, as we are about to process it.
130 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
131 mDiscoveredList is never free'ed and contains variables that define
132 the other states the DXE driver transitions to..
133 While you are at it read the A Priori file into memory.
134 Place drivers in the A Priori list onto the mScheduledQueue.
136 @param Event The Event that is being processed, not used.
137 @param Context Event Context, not used.
142 CoreFwVolEventProtocolNotify (
148 Convert FvHandle and DriverName into an EFI device path
150 @param Fv Fv protocol, needed to read Depex info out of
152 @param FvHandle Handle for Fv, needed in the
153 EFI_CORE_DRIVER_ENTRY so that the PE image can be
154 read out of the FV at a later time.
155 @param DriverName Name of driver to add to mDiscoveredList.
157 @return Pointer to device path constructed from FvHandle and DriverName
160 EFI_DEVICE_PATH_PROTOCOL
*
162 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
163 IN EFI_HANDLE FvHandle
,
164 IN EFI_GUID
*DriverName
168 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
169 and initilize any state variables. Read the Depex from the FV and store it
170 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
171 The Discovered list is never free'ed and contains booleans that represent the
172 other possible DXE driver states.
174 @param Fv Fv protocol, needed to read Depex info out of
176 @param FvHandle Handle for Fv, needed in the
177 EFI_CORE_DRIVER_ENTRY so that the PE image can be
178 read out of the FV at a later time.
179 @param DriverName Name of driver to add to mDiscoveredList.
180 @param Type Fv File Type of file to add to mDiscoveredList.
182 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
183 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
184 DriverName may be active in the system at any one
189 CoreAddToDriverList (
190 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
191 IN EFI_HANDLE FvHandle
,
192 IN EFI_GUID
*DriverName
,
193 IN EFI_FV_FILETYPE Type
197 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
199 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.
200 @param FvHandle The handle which FVB protocol installed on.
201 @param DriverName The driver guid specified.
203 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.
204 @retval EFI_VOLUME_CORRUPTED Corrupted volume.
205 @retval EFI_SUCCESS Function successfully returned.
209 CoreProcessFvImageFile (
210 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
211 IN EFI_HANDLE FvHandle
,
212 IN EFI_GUID
*DriverName
217 Enter critical section by gaining lock on mDispatcherLock.
221 CoreAcquireDispatcherLock (
225 CoreAcquireLock (&mDispatcherLock
);
230 Exit critical section by releasing lock on mDispatcherLock.
234 CoreReleaseDispatcherLock (
238 CoreReleaseLock (&mDispatcherLock
);
243 Read Depex and pre-process the Depex for Before and After. If Section Extraction
244 protocol returns an error via ReadSection defer the reading of the Depex.
246 @param DriverEntry Driver to work on.
248 @retval EFI_SUCCESS Depex read and preprossesed
249 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
250 and Depex reading needs to be retried.
251 @retval Error DEPEX not found.
255 CoreGetDepexSectionAndPreProccess (
256 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
260 EFI_SECTION_TYPE SectionType
;
261 UINT32 AuthenticationStatus
;
262 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
265 Fv
= DriverEntry
->Fv
;
268 // Grab Depex info, it will never be free'ed.
270 SectionType
= EFI_SECTION_DXE_DEPEX
;
271 Status
= Fv
->ReadSection (
273 &DriverEntry
->FileName
,
277 (UINTN
*)&DriverEntry
->DepexSize
,
278 &AuthenticationStatus
280 if (EFI_ERROR (Status
)) {
281 if (Status
== EFI_PROTOCOL_ERROR
) {
283 // The section extraction protocol failed so set protocol error flag
285 DriverEntry
->DepexProtocolError
= TRUE
;
288 // If no Depex assume UEFI 2.0 driver model
290 DriverEntry
->Depex
= NULL
;
291 DriverEntry
->Dependent
= TRUE
;
292 DriverEntry
->DepexProtocolError
= FALSE
;
296 // Set Before, After, and Unrequested state information based on Depex
297 // Driver will be put in Dependent or Unrequested state
299 CorePreProcessDepex (DriverEntry
);
300 DriverEntry
->DepexProtocolError
= FALSE
;
308 Check every driver and locate a matching one. If the driver is found, the Unrequested
309 state flag is cleared.
311 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
312 the firmware file specified by DriverName.
313 @param DriverName The Driver name to put in the Dependent state.
315 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
317 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
324 IN EFI_HANDLE FirmwareVolumeHandle
,
325 IN EFI_GUID
*DriverName
329 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
332 // Check every driver
334 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
335 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
336 if (DriverEntry
->FvHandle
== FirmwareVolumeHandle
&&
337 DriverEntry
->Unrequested
&&
338 CompareGuid (DriverName
, &DriverEntry
->FileName
)) {
340 // Move the driver from the Unrequested to the Dependent state
342 CoreAcquireDispatcherLock ();
343 DriverEntry
->Unrequested
= FALSE
;
344 DriverEntry
->Dependent
= TRUE
;
345 CoreReleaseDispatcherLock ();
347 DEBUG ((DEBUG_DISPATCH
, "Schedule FFS(%g) - EFI_SUCCESS\n", DriverName
));
353 DEBUG ((DEBUG_DISPATCH
, "Schedule FFS(%g) - EFI_NOT_FOUND\n", DriverName
));
355 return EFI_NOT_FOUND
;
361 Convert a driver from the Untrused back to the Scheduled state.
363 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
364 the firmware file specified by DriverName.
365 @param DriverName The Driver name to put in the Scheduled state
367 @retval EFI_SUCCESS The file was found in the untrusted state, and it
368 was promoted to the trusted state.
369 @retval EFI_NOT_FOUND The file was not found in the untrusted state.
375 IN EFI_HANDLE FirmwareVolumeHandle
,
376 IN EFI_GUID
*DriverName
380 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
383 // Check every driver
385 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
386 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
387 if (DriverEntry
->FvHandle
== FirmwareVolumeHandle
&&
388 DriverEntry
->Untrusted
&&
389 CompareGuid (DriverName
, &DriverEntry
->FileName
)) {
391 // Transition driver from Untrusted to Scheduled state.
393 CoreAcquireDispatcherLock ();
394 DriverEntry
->Untrusted
= FALSE
;
395 DriverEntry
->Scheduled
= TRUE
;
396 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
397 CoreReleaseDispatcherLock ();
402 return EFI_NOT_FOUND
;
407 An empty function to pass error checking of CreateEventEx ().
409 @param Event Event whose notification function is being invoked.
410 @param Context Pointer to the notification function's context,
411 which is implementation-dependent.
416 CoreEmptyCallbackFunction (
425 This is the main Dispatcher for DXE and it exits when there are no more
426 drivers to run. Drain the mScheduledQueue and load and start a PE
427 image for each driver. Search the mDiscoveredList to see if any driver can
428 be placed on the mScheduledQueue. If no drivers are placed on the
429 mScheduledQueue exit the function. On exit it is assumed the Bds()
430 will be called, and when the Bds() exits the Dispatcher will be called
433 @retval EFI_ALREADY_STARTED The DXE Dispatcher is already running
434 @retval EFI_NOT_FOUND No DXE Drivers were dispatched
435 @retval EFI_SUCCESS One or more DXE Drivers were dispatched
445 EFI_STATUS ReturnStatus
;
447 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
449 EFI_EVENT DxeDispatchEvent
;
452 if (gDispatcherRunning
) {
454 // If the dispatcher is running don't let it be restarted.
456 return EFI_ALREADY_STARTED
;
459 gDispatcherRunning
= TRUE
;
461 Status
= CoreCreateEventEx (
464 CoreEmptyCallbackFunction
,
466 &gEfiEventDxeDispatchGuid
,
469 if (EFI_ERROR (Status
)) {
473 ReturnStatus
= EFI_NOT_FOUND
;
476 // Drain the Scheduled Queue
478 while (!IsListEmpty (&mScheduledQueue
)) {
480 mScheduledQueue
.ForwardLink
,
481 EFI_CORE_DRIVER_ENTRY
,
483 EFI_CORE_DRIVER_ENTRY_SIGNATURE
487 // Load the DXE Driver image into memory. If the Driver was transitioned from
488 // Untrused to Scheduled it would have already been loaded so we may need to
489 // skip the LoadImage
491 if (DriverEntry
->ImageHandle
== NULL
&& !DriverEntry
->IsFvImage
) {
492 DEBUG ((DEBUG_INFO
, "Loading driver %g\n", &DriverEntry
->FileName
));
493 Status
= CoreLoadImage (
496 DriverEntry
->FvFileDevicePath
,
499 &DriverEntry
->ImageHandle
503 // Update the driver state to reflect that it's been loaded
505 if (EFI_ERROR (Status
)) {
506 CoreAcquireDispatcherLock ();
508 if (Status
== EFI_SECURITY_VIOLATION
) {
510 // Take driver from Scheduled to Untrused state
512 DriverEntry
->Untrusted
= TRUE
;
515 // The DXE Driver could not be loaded, and do not attempt to load or start it again.
516 // Take driver from Scheduled to Initialized.
518 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
520 DriverEntry
->Initialized
= TRUE
;
523 DriverEntry
->Scheduled
= FALSE
;
524 RemoveEntryList (&DriverEntry
->ScheduledLink
);
526 CoreReleaseDispatcherLock ();
529 // If it's an error don't try the StartImage
535 CoreAcquireDispatcherLock ();
537 DriverEntry
->Scheduled
= FALSE
;
538 DriverEntry
->Initialized
= TRUE
;
539 RemoveEntryList (&DriverEntry
->ScheduledLink
);
541 CoreReleaseDispatcherLock ();
544 if (DriverEntry
->IsFvImage
) {
546 // Produce a firmware volume block protocol for FvImage so it gets dispatched from.
548 Status
= CoreProcessFvImageFile (DriverEntry
->Fv
, DriverEntry
->FvHandle
, &DriverEntry
->FileName
);
550 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
552 (EFI_SOFTWARE_DXE_CORE
| EFI_SW_PC_INIT_BEGIN
),
553 &DriverEntry
->ImageHandle
,
554 sizeof (DriverEntry
->ImageHandle
)
556 ASSERT (DriverEntry
->ImageHandle
!= NULL
);
558 Status
= CoreStartImage (DriverEntry
->ImageHandle
, NULL
, NULL
);
560 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
562 (EFI_SOFTWARE_DXE_CORE
| EFI_SW_PC_INIT_END
),
563 &DriverEntry
->ImageHandle
,
564 sizeof (DriverEntry
->ImageHandle
)
568 ReturnStatus
= EFI_SUCCESS
;
572 // Now DXE Dispatcher finished one round of dispatch, signal an event group
573 // so that SMM Dispatcher get chance to dispatch SMM Drivers which depend
576 if (!EFI_ERROR (ReturnStatus
)) {
577 CoreSignalEvent (DxeDispatchEvent
);
581 // Search DriverList for items to place on Scheduled Queue
584 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
585 DriverEntry
= CR (Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
587 if (DriverEntry
->DepexProtocolError
){
589 // If Section Extraction Protocol did not let the Depex be read before retry the read
591 Status
= CoreGetDepexSectionAndPreProccess (DriverEntry
);
594 if (DriverEntry
->Dependent
) {
595 if (CoreIsSchedulable (DriverEntry
)) {
596 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
600 if (DriverEntry
->Unrequested
) {
601 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
602 DEBUG ((DEBUG_DISPATCH
, " SOR = Not Requested\n"));
603 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE\n"));
607 } while (ReadyToRun
);
610 // Close DXE dispatch Event
612 CoreCloseEvent (DxeDispatchEvent
);
614 gDispatcherRunning
= FALSE
;
621 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
622 must add any driver with a before dependency on InsertedDriverEntry first.
623 You do this by recursively calling this routine. After all the Befores are
624 processed you can add InsertedDriverEntry to the mScheduledQueue.
625 Then you can add any driver with an After dependency on InsertedDriverEntry
626 by recursively calling this routine.
628 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
632 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
633 IN EFI_CORE_DRIVER_ENTRY
*InsertedDriverEntry
637 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
640 // Process Before Dependency
642 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
643 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
644 if (DriverEntry
->Before
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
645 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
646 DEBUG ((DEBUG_DISPATCH
, " BEFORE FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
647 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
649 // Recursively process BEFORE
651 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
652 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
654 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
660 // Convert driver from Dependent to Scheduled state
662 CoreAcquireDispatcherLock ();
664 InsertedDriverEntry
->Dependent
= FALSE
;
665 InsertedDriverEntry
->Scheduled
= TRUE
;
666 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
668 CoreReleaseDispatcherLock ();
671 // Process After Dependency
673 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
674 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
675 if (DriverEntry
->After
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
676 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
677 DEBUG ((DEBUG_DISPATCH
, " AFTER FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
678 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
680 // Recursively process AFTER
682 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
683 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
685 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
693 Return TRUE if the Fv has been processed, FALSE if not.
695 @param FvHandle The handle of a FV that's being tested
697 @retval TRUE Fv protocol on FvHandle has been processed
698 @retval FALSE Fv protocol on FvHandle has not yet been processed
703 IN EFI_HANDLE FvHandle
707 KNOWN_HANDLE
*KnownHandle
;
709 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
710 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
711 if (KnownHandle
->Handle
== FvHandle
) {
720 Remember that Fv protocol on FvHandle has had it's drivers placed on the
721 mDiscoveredList. This fucntion adds entries on the mFvHandleList if new
722 entry is different from one in mFvHandleList by checking FvImage Guid.
723 Items are never removed/freed from the mFvHandleList.
725 @param FvHandle The handle of a FV that has been processed
727 @return A point to new added FvHandle entry. If FvHandle with the same FvImage guid
728 has been added, NULL will return.
732 FvIsBeingProcesssed (
733 IN EFI_HANDLE FvHandle
738 BOOLEAN FvNameGuidIsFound
;
739 UINT32 ExtHeaderOffset
;
740 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
741 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
742 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
747 KNOWN_HANDLE
*KnownHandle
;
750 // Get the FirmwareVolumeBlock protocol on that handle
752 FvNameGuidIsFound
= FALSE
;
753 Status
= CoreHandleProtocol (FvHandle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
754 if (!EFI_ERROR (Status
)) {
756 // Get the full FV header based on FVB protocol.
758 ASSERT (Fvb
!= NULL
);
759 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
760 if (!EFI_ERROR (Status
)) {
761 ASSERT (FwVolHeader
!= NULL
);
762 if (VerifyFvHeaderChecksum (FwVolHeader
) && FwVolHeader
->ExtHeaderOffset
!= 0) {
763 ExtHeaderOffset
= (UINT32
) FwVolHeader
->ExtHeaderOffset
;
764 BlockMap
= FwVolHeader
->BlockMap
;
768 // Find LbaIndex and LbaOffset for FV extension header based on BlockMap.
770 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
771 for (Index
= 0; Index
< BlockMap
->NumBlocks
&& ExtHeaderOffset
>= BlockMap
->Length
; Index
++) {
772 ExtHeaderOffset
-= BlockMap
->Length
;
776 // Check whether FvExtHeader is crossing the multi block range.
778 if (Index
< BlockMap
->NumBlocks
) {
779 LbaOffset
= ExtHeaderOffset
;
785 // Read FvNameGuid from FV extension header.
787 Status
= ReadFvbData (Fvb
, &LbaIndex
, &LbaOffset
, sizeof (FvNameGuid
), (UINT8
*) &FvNameGuid
);
788 if (!EFI_ERROR (Status
)) {
789 FvNameGuidIsFound
= TRUE
;
792 CoreFreePool (FwVolHeader
);
796 if (FvNameGuidIsFound
) {
798 // Check whether the FV image with the found FvNameGuid has been processed.
800 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
801 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
802 if (CompareGuid (&FvNameGuid
, &KnownHandle
->FvNameGuid
)) {
803 DEBUG ((EFI_D_ERROR
, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle
, KnownHandle
->Handle
, FvNameGuid
));
809 KnownHandle
= AllocateZeroPool (sizeof (KNOWN_HANDLE
));
810 ASSERT (KnownHandle
!= NULL
);
812 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
813 KnownHandle
->Handle
= FvHandle
;
814 if (FvNameGuidIsFound
) {
815 CopyGuid (&KnownHandle
->FvNameGuid
, &FvNameGuid
);
817 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
825 Convert FvHandle and DriverName into an EFI device path
827 @param Fv Fv protocol, needed to read Depex info out of
829 @param FvHandle Handle for Fv, needed in the
830 EFI_CORE_DRIVER_ENTRY so that the PE image can be
831 read out of the FV at a later time.
832 @param DriverName Name of driver to add to mDiscoveredList.
834 @return Pointer to device path constructed from FvHandle and DriverName
837 EFI_DEVICE_PATH_PROTOCOL
*
839 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
840 IN EFI_HANDLE FvHandle
,
841 IN EFI_GUID
*DriverName
845 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
846 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
849 // Remember the device path of the FV
851 Status
= CoreHandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
852 if (EFI_ERROR (Status
)) {
853 FileNameDevicePath
= NULL
;
856 // Build a device path to the file in the FV to pass into gBS->LoadImage
858 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
859 SetDevicePathEndNode (&mFvDevicePath
.End
);
861 FileNameDevicePath
= AppendDevicePath (
863 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
867 return FileNameDevicePath
;
873 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
874 and initilize any state variables. Read the Depex from the FV and store it
875 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
876 The Discovered list is never free'ed and contains booleans that represent the
877 other possible DXE driver states.
879 @param Fv Fv protocol, needed to read Depex info out of
881 @param FvHandle Handle for Fv, needed in the
882 EFI_CORE_DRIVER_ENTRY so that the PE image can be
883 read out of the FV at a later time.
884 @param DriverName Name of driver to add to mDiscoveredList.
885 @param Type Fv File Type of file to add to mDiscoveredList.
887 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
888 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
889 DriverName may be active in the system at any one
894 CoreAddToDriverList (
895 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
896 IN EFI_HANDLE FvHandle
,
897 IN EFI_GUID
*DriverName
,
898 IN EFI_FV_FILETYPE Type
901 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
905 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
908 DriverEntry
= AllocateZeroPool (sizeof (EFI_CORE_DRIVER_ENTRY
));
909 ASSERT (DriverEntry
!= NULL
);
910 if (Type
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
911 DriverEntry
->IsFvImage
= TRUE
;
914 DriverEntry
->Signature
= EFI_CORE_DRIVER_ENTRY_SIGNATURE
;
915 CopyGuid (&DriverEntry
->FileName
, DriverName
);
916 DriverEntry
->FvHandle
= FvHandle
;
917 DriverEntry
->Fv
= Fv
;
918 DriverEntry
->FvFileDevicePath
= CoreFvToDevicePath (Fv
, FvHandle
, DriverName
);
920 CoreGetDepexSectionAndPreProccess (DriverEntry
);
922 CoreAcquireDispatcherLock ();
924 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
926 CoreReleaseDispatcherLock ();
933 Check if a FV Image type file (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) is
934 described by a EFI_HOB_FIRMWARE_VOLUME2 Hob.
936 @param FvNameGuid The FV image guid specified.
937 @param DriverName The driver guid specified.
939 @retval TRUE This file is found in a EFI_HOB_FIRMWARE_VOLUME2
941 @retval FALSE Not found.
946 IN CONST EFI_GUID
*FvNameGuid
,
947 IN CONST EFI_GUID
*DriverName
950 EFI_PEI_HOB_POINTERS HobFv2
;
952 HobFv2
.Raw
= GetHobList ();
954 while ((HobFv2
.Raw
= GetNextHob (EFI_HOB_TYPE_FV2
, HobFv2
.Raw
)) != NULL
) {
956 // Compare parent FvNameGuid and FileGuid both.
958 if (CompareGuid (DriverName
, &HobFv2
.FirmwareVolume2
->FileName
) &&
959 CompareGuid (FvNameGuid
, &HobFv2
.FirmwareVolume2
->FvName
)) {
962 HobFv2
.Raw
= GET_NEXT_HOB (HobFv2
);
971 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
973 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.
974 @param FvHandle The handle which FVB protocol installed on.
975 @param DriverName The driver guid specified.
977 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.
978 @retval EFI_VOLUME_CORRUPTED Corrupted volume.
979 @retval EFI_SUCCESS Function successfully returned.
983 CoreProcessFvImageFile (
984 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
985 IN EFI_HANDLE FvHandle
,
986 IN EFI_GUID
*DriverName
990 EFI_SECTION_TYPE SectionType
;
991 UINT32 AuthenticationStatus
;
995 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
999 // Read the first (and only the first) firmware volume section
1001 SectionType
= EFI_SECTION_FIRMWARE_VOLUME_IMAGE
;
1006 AlignedBuffer
= NULL
;
1007 Status
= Fv
->ReadSection (
1014 &AuthenticationStatus
1016 if (!EFI_ERROR (Status
)) {
1018 // FvImage should be at its required alignment.
1020 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) Buffer
;
1022 // Get FvHeader alignment
1024 FvAlignment
= 1 << ((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
1026 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.
1028 if (FvAlignment
< 8) {
1032 // Allocate the aligned buffer for the FvImage.
1034 AlignedBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), (UINTN
) FvAlignment
);
1035 if (AlignedBuffer
== NULL
) {
1036 Status
= EFI_OUT_OF_RESOURCES
;
1039 // Move FvImage into the aligned buffer and release the original buffer.
1041 CopyMem (AlignedBuffer
, Buffer
, BufferSize
);
1042 CoreFreePool (Buffer
);
1045 // Produce a FVB protocol for the file
1047 Status
= ProduceFVBProtocolOnBuffer (
1048 (EFI_PHYSICAL_ADDRESS
) (UINTN
) AlignedBuffer
,
1051 AuthenticationStatus
,
1057 if (EFI_ERROR (Status
)) {
1059 // ReadSection or Produce FVB failed, Free data buffer
1061 if (Buffer
!= NULL
) {
1065 if (AlignedBuffer
!= NULL
) {
1066 FreeAlignedPages (AlignedBuffer
, EFI_SIZE_TO_PAGES (BufferSize
));
1075 Event notification that is fired every time a FV dispatch protocol is added.
1076 More than one protocol may have been added when this event is fired, so you
1077 must loop on CoreLocateHandle () to see how many protocols were added and
1078 do the following to each FV:
1079 If the Fv has already been processed, skip it. If the Fv has not been
1080 processed then mark it as being processed, as we are about to process it.
1081 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1082 mDiscoveredList is never free'ed and contains variables that define
1083 the other states the DXE driver transitions to..
1084 While you are at it read the A Priori file into memory.
1085 Place drivers in the A Priori list onto the mScheduledQueue.
1087 @param Event The Event that is being processed, not used.
1088 @param Context Event Context, not used.
1093 CoreFwVolEventProtocolNotify (
1099 EFI_STATUS GetNextFileStatus
;
1100 EFI_STATUS SecurityStatus
;
1101 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1102 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1103 EFI_HANDLE FvHandle
;
1107 EFI_FV_FILETYPE Type
;
1108 EFI_FV_FILE_ATTRIBUTES Attributes
;
1110 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
1111 EFI_GUID
*AprioriFile
;
1112 UINTN AprioriEntryCount
;
1115 UINT32 AuthenticationStatus
;
1118 KNOWN_HANDLE
*KnownHandle
;
1121 BufferSize
= sizeof (EFI_HANDLE
);
1122 Status
= CoreLocateHandle (
1125 mFwVolEventRegistration
,
1129 if (EFI_ERROR (Status
)) {
1131 // If no more notification events exit
1136 if (FvHasBeenProcessed (FvHandle
)) {
1138 // This Fv has already been processed so lets skip it!
1144 // Since we are about to process this Fv mark it as processed.
1146 KnownHandle
= FvIsBeingProcesssed (FvHandle
);
1147 if (KnownHandle
== NULL
) {
1149 // The FV with the same FV name guid has already been processed.
1155 Status
= CoreHandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1156 if (EFI_ERROR (Status
) || Fv
== NULL
) {
1158 // FvHandle must have Firmware Volume2 protocol thus we should never get here.
1164 Status
= CoreHandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1165 if (EFI_ERROR (Status
)) {
1167 // The Firmware volume doesn't have device path, can't be dispatched.
1173 // Evaluate the authentication status of the Firmware Volume through
1174 // Security Architectural Protocol
1176 if (gSecurity
!= NULL
) {
1177 SecurityStatus
= gSecurity
->FileAuthenticationState (
1182 if (SecurityStatus
!= EFI_SUCCESS
) {
1184 // Security check failed. The firmware volume should not be used for any purpose.
1191 // Discover Drivers in FV and add them to the Discovered Driver List.
1192 // Process EFI_FV_FILETYPE_DRIVER type and then EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
1193 // EFI_FV_FILETYPE_DXE_CORE is processed to produce a Loaded Image protocol for the core
1194 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE is processed to create a Fvb
1196 for (Index
= 0; Index
< sizeof (mDxeFileTypes
) / sizeof (EFI_FV_FILETYPE
); Index
++) {
1198 // Initialize the search key
1202 Type
= mDxeFileTypes
[Index
];
1203 GetNextFileStatus
= Fv
->GetNextFile (
1211 if (!EFI_ERROR (GetNextFileStatus
)) {
1212 if (Type
== EFI_FV_FILETYPE_DXE_CORE
) {
1214 // If this is the DXE core fill in it's DevicePath & DeviceHandle
1216 if (gDxeCoreLoadedImage
->FilePath
== NULL
) {
1217 if (CompareGuid (&NameGuid
, gDxeCoreFileName
)) {
1219 // Maybe One specail Fv cantains only one DXE_CORE module, so its device path must
1220 // be initialized completely.
1222 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, &NameGuid
);
1223 SetDevicePathEndNode (&mFvDevicePath
.End
);
1225 gDxeCoreLoadedImage
->FilePath
= DuplicateDevicePath (
1226 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
1228 gDxeCoreLoadedImage
->DeviceHandle
= FvHandle
;
1231 } else if (Type
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
1233 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
1236 if (FvFoundInHobFv2 (&KnownHandle
->FvNameGuid
, &NameGuid
)) {
1241 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has PEI depex section.
1245 Status
= Fv
->ReadSection (
1248 EFI_SECTION_PEI_DEPEX
,
1252 &AuthenticationStatus
1254 if (!EFI_ERROR (Status
)) {
1256 // If PEI depex section is found, this FV image will be ignored in DXE phase.
1257 // Now, DxeCore doesn't support FV image with more one type DEPEX section.
1259 FreePool (DepexBuffer
);
1264 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has SMM depex section.
1268 Status
= Fv
->ReadSection (
1271 EFI_SECTION_SMM_DEPEX
,
1275 &AuthenticationStatus
1277 if (!EFI_ERROR (Status
)) {
1279 // If SMM depex section is found, this FV image will be ignored in DXE phase.
1280 // Now, DxeCore doesn't support FV image with more one type DEPEX section.
1282 FreePool (DepexBuffer
);
1287 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has DXE depex section.
1291 Status
= Fv
->ReadSection (
1294 EFI_SECTION_DXE_DEPEX
,
1298 &AuthenticationStatus
1300 if (EFI_ERROR (Status
)) {
1302 // If no depex section, produce a firmware volume block protocol for it so it gets dispatched from.
1304 CoreProcessFvImageFile (Fv
, FvHandle
, &NameGuid
);
1307 // If depex section is found, this FV image will be dispatched until its depex is evaluated to TRUE.
1309 FreePool (DepexBuffer
);
1310 CoreAddToDriverList (Fv
, FvHandle
, &NameGuid
, Type
);
1314 // Transition driver from Undiscovered to Discovered state
1316 CoreAddToDriverList (Fv
, FvHandle
, &NameGuid
, Type
);
1319 } while (!EFI_ERROR (GetNextFileStatus
));
1323 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1326 Status
= Fv
->ReadSection (
1331 (VOID
**)&AprioriFile
,
1333 &AuthenticationStatus
1335 if (!EFI_ERROR (Status
)) {
1336 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1338 AprioriEntryCount
= 0;
1342 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1343 // drivers not in the current FV and these must be skipped since the a priori list
1344 // is only valid for the FV that it resided in.
1347 for (Index
= 0; Index
< AprioriEntryCount
; Index
++) {
1348 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1349 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
1350 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[Index
]) &&
1351 (FvHandle
== DriverEntry
->FvHandle
)) {
1352 CoreAcquireDispatcherLock ();
1353 DriverEntry
->Dependent
= FALSE
;
1354 DriverEntry
->Scheduled
= TRUE
;
1355 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1356 CoreReleaseDispatcherLock ();
1357 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1358 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1365 // Free data allocated by Fv->ReadSection ()
1367 CoreFreePool (AprioriFile
);
1374 Initialize the dispatcher. Initialize the notification function that runs when
1375 an FV2 protocol is added to the system.
1379 CoreInitializeDispatcher (
1383 mFwVolEvent
= EfiCreateProtocolNotifyEvent (
1384 &gEfiFirmwareVolume2ProtocolGuid
,
1386 CoreFwVolEventProtocolNotify
,
1388 &mFwVolEventRegistration
1393 // Function only used in debug builds
1397 Traverse the discovered list for any drivers that were discovered but not loaded
1398 because the dependency experessions evaluated to false.
1402 CoreDisplayDiscoveredNotDispatched (
1407 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
1409 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1410 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
1411 if (DriverEntry
->Dependent
) {
1412 DEBUG ((DEBUG_LOAD
, "Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));