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 - 2013, 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
;
95 // Function Prototypes
98 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
99 must add any driver with a before dependency on InsertedDriverEntry first.
100 You do this by recursively calling this routine. After all the Befores are
101 processed you can add InsertedDriverEntry to the mScheduledQueue.
102 Then you can add any driver with an After dependency on InsertedDriverEntry
103 by recursively calling this routine.
105 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
109 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
110 IN EFI_CORE_DRIVER_ENTRY
*InsertedDriverEntry
114 Event notification that is fired every time a FV dispatch protocol is added.
115 More than one protocol may have been added when this event is fired, so you
116 must loop on CoreLocateHandle () to see how many protocols were added and
117 do the following to each FV:
118 If the Fv has already been processed, skip it. If the Fv has not been
119 processed then mark it as being processed, as we are about to process it.
120 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
121 mDiscoveredList is never free'ed and contains variables that define
122 the other states the DXE driver transitions to..
123 While you are at it read the A Priori file into memory.
124 Place drivers in the A Priori list onto the mScheduledQueue.
126 @param Event The Event that is being processed, not used.
127 @param Context Event Context, not used.
132 CoreFwVolEventProtocolNotify (
138 Convert FvHandle and DriverName into an EFI device path
140 @param Fv Fv protocol, needed to read Depex info out of
142 @param FvHandle Handle for Fv, needed in the
143 EFI_CORE_DRIVER_ENTRY so that the PE image can be
144 read out of the FV at a later time.
145 @param DriverName Name of driver to add to mDiscoveredList.
147 @return Pointer to device path constructed from FvHandle and DriverName
150 EFI_DEVICE_PATH_PROTOCOL
*
152 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
153 IN EFI_HANDLE FvHandle
,
154 IN EFI_GUID
*DriverName
158 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
159 and initilize any state variables. Read the Depex from the FV and store it
160 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
161 The Discovered list is never free'ed and contains booleans that represent the
162 other possible DXE driver states.
164 @param Fv Fv protocol, needed to read Depex info out of
166 @param FvHandle Handle for Fv, needed in the
167 EFI_CORE_DRIVER_ENTRY so that the PE image can be
168 read out of the FV at a later time.
169 @param DriverName Name of driver to add to mDiscoveredList.
170 @param Type Fv File Type of file to add to mDiscoveredList.
172 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
173 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
174 DriverName may be active in the system at any one
179 CoreAddToDriverList (
180 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
181 IN EFI_HANDLE FvHandle
,
182 IN EFI_GUID
*DriverName
,
183 IN EFI_FV_FILETYPE Type
187 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
189 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.
190 @param FvHandle The handle which FVB protocol installed on.
191 @param DriverName The driver guid specified.
193 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.
194 @retval EFI_VOLUME_CORRUPTED Corrupted volume.
195 @retval EFI_SUCCESS Function successfully returned.
199 CoreProcessFvImageFile (
200 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
201 IN EFI_HANDLE FvHandle
,
202 IN EFI_GUID
*DriverName
207 Enter critical section by gaining lock on mDispatcherLock.
211 CoreAcquireDispatcherLock (
215 CoreAcquireLock (&mDispatcherLock
);
220 Exit critical section by releasing lock on mDispatcherLock.
224 CoreReleaseDispatcherLock (
228 CoreReleaseLock (&mDispatcherLock
);
233 Read Depex and pre-process the Depex for Before and After. If Section Extraction
234 protocol returns an error via ReadSection defer the reading of the Depex.
236 @param DriverEntry Driver to work on.
238 @retval EFI_SUCCESS Depex read and preprossesed
239 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
240 and Depex reading needs to be retried.
241 @retval Error DEPEX not found.
245 CoreGetDepexSectionAndPreProccess (
246 IN EFI_CORE_DRIVER_ENTRY
*DriverEntry
250 EFI_SECTION_TYPE SectionType
;
251 UINT32 AuthenticationStatus
;
252 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
255 Fv
= DriverEntry
->Fv
;
258 // Grab Depex info, it will never be free'ed.
260 SectionType
= EFI_SECTION_DXE_DEPEX
;
261 Status
= Fv
->ReadSection (
263 &DriverEntry
->FileName
,
267 (UINTN
*)&DriverEntry
->DepexSize
,
268 &AuthenticationStatus
270 if (EFI_ERROR (Status
)) {
271 if (Status
== EFI_PROTOCOL_ERROR
) {
273 // The section extraction protocol failed so set protocol error flag
275 DriverEntry
->DepexProtocolError
= TRUE
;
278 // If no Depex assume UEFI 2.0 driver model
280 DriverEntry
->Depex
= NULL
;
281 DriverEntry
->Dependent
= TRUE
;
282 DriverEntry
->DepexProtocolError
= FALSE
;
286 // Set Before, After, and Unrequested state information based on Depex
287 // Driver will be put in Dependent or Unrequested state
289 CorePreProcessDepex (DriverEntry
);
290 DriverEntry
->DepexProtocolError
= FALSE
;
298 Check every driver and locate a matching one. If the driver is found, the Unrequested
299 state flag is cleared.
301 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
302 the firmware file specified by DriverName.
303 @param DriverName The Driver name to put in the Dependent state.
305 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
307 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
314 IN EFI_HANDLE FirmwareVolumeHandle
,
315 IN EFI_GUID
*DriverName
319 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
322 // Check every driver
324 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
325 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
326 if (DriverEntry
->FvHandle
== FirmwareVolumeHandle
&&
327 DriverEntry
->Unrequested
&&
328 CompareGuid (DriverName
, &DriverEntry
->FileName
)) {
330 // Move the driver from the Unrequested to the Dependent state
332 CoreAcquireDispatcherLock ();
333 DriverEntry
->Unrequested
= FALSE
;
334 DriverEntry
->Dependent
= TRUE
;
335 CoreReleaseDispatcherLock ();
337 DEBUG ((DEBUG_DISPATCH
, "Schedule FFS(%g) - EFI_SUCCESS\n", DriverName
));
343 DEBUG ((DEBUG_DISPATCH
, "Schedule FFS(%g) - EFI_NOT_FOUND\n", DriverName
));
345 return EFI_NOT_FOUND
;
351 Convert a driver from the Untrused back to the Scheduled state.
353 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
354 the firmware file specified by DriverName.
355 @param DriverName The Driver name to put in the Scheduled state
357 @retval EFI_SUCCESS The file was found in the untrusted state, and it
358 was promoted to the trusted state.
359 @retval EFI_NOT_FOUND The file was not found in the untrusted state.
365 IN EFI_HANDLE FirmwareVolumeHandle
,
366 IN EFI_GUID
*DriverName
370 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
373 // Check every driver
375 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
376 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
377 if (DriverEntry
->FvHandle
== FirmwareVolumeHandle
&&
378 DriverEntry
->Untrusted
&&
379 CompareGuid (DriverName
, &DriverEntry
->FileName
)) {
381 // Transition driver from Untrusted to Scheduled state.
383 CoreAcquireDispatcherLock ();
384 DriverEntry
->Untrusted
= FALSE
;
385 DriverEntry
->Scheduled
= TRUE
;
386 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
387 CoreReleaseDispatcherLock ();
392 return EFI_NOT_FOUND
;
397 An empty function to pass error checking of CreateEventEx ().
399 @param Event Event whose notification function is being invoked.
400 @param Context Pointer to the notification function's context,
401 which is implementation-dependent.
406 CoreEmptyCallbackFunction (
415 This is the main Dispatcher for DXE and it exits when there are no more
416 drivers to run. Drain the mScheduledQueue and load and start a PE
417 image for each driver. Search the mDiscoveredList to see if any driver can
418 be placed on the mScheduledQueue. If no drivers are placed on the
419 mScheduledQueue exit the function. On exit it is assumed the Bds()
420 will be called, and when the Bds() exits the Dispatcher will be called
423 @retval EFI_ALREADY_STARTED The DXE Dispatcher is already running
424 @retval EFI_NOT_FOUND No DXE Drivers were dispatched
425 @retval EFI_SUCCESS One or more DXE Drivers were dispatched
435 EFI_STATUS ReturnStatus
;
437 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
439 EFI_EVENT DxeDispatchEvent
;
442 if (gDispatcherRunning
) {
444 // If the dispatcher is running don't let it be restarted.
446 return EFI_ALREADY_STARTED
;
449 gDispatcherRunning
= TRUE
;
451 Status
= CoreCreateEventEx (
454 CoreEmptyCallbackFunction
,
456 &gEfiEventDxeDispatchGuid
,
459 if (EFI_ERROR (Status
)) {
463 ReturnStatus
= EFI_NOT_FOUND
;
466 // Drain the Scheduled Queue
468 while (!IsListEmpty (&mScheduledQueue
)) {
470 mScheduledQueue
.ForwardLink
,
471 EFI_CORE_DRIVER_ENTRY
,
473 EFI_CORE_DRIVER_ENTRY_SIGNATURE
477 // Load the DXE Driver image into memory. If the Driver was transitioned from
478 // Untrused to Scheduled it would have already been loaded so we may need to
479 // skip the LoadImage
481 if (DriverEntry
->ImageHandle
== NULL
&& !DriverEntry
->IsFvImage
) {
482 DEBUG ((DEBUG_INFO
, "Loading driver %g\n", &DriverEntry
->FileName
));
483 Status
= CoreLoadImage (
486 DriverEntry
->FvFileDevicePath
,
489 &DriverEntry
->ImageHandle
493 // Update the driver state to reflect that it's been loaded
495 if (EFI_ERROR (Status
)) {
496 CoreAcquireDispatcherLock ();
498 if (Status
== EFI_SECURITY_VIOLATION
) {
500 // Take driver from Scheduled to Untrused state
502 DriverEntry
->Untrusted
= TRUE
;
505 // The DXE Driver could not be loaded, and do not attempt to load or start it again.
506 // Take driver from Scheduled to Initialized.
508 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
510 DriverEntry
->Initialized
= TRUE
;
513 DriverEntry
->Scheduled
= FALSE
;
514 RemoveEntryList (&DriverEntry
->ScheduledLink
);
516 CoreReleaseDispatcherLock ();
519 // If it's an error don't try the StartImage
525 CoreAcquireDispatcherLock ();
527 DriverEntry
->Scheduled
= FALSE
;
528 DriverEntry
->Initialized
= TRUE
;
529 RemoveEntryList (&DriverEntry
->ScheduledLink
);
531 CoreReleaseDispatcherLock ();
534 if (DriverEntry
->IsFvImage
) {
536 // Produce a firmware volume block protocol for FvImage so it gets dispatched from.
538 Status
= CoreProcessFvImageFile (DriverEntry
->Fv
, DriverEntry
->FvHandle
, &DriverEntry
->FileName
);
540 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
542 (EFI_SOFTWARE_DXE_CORE
| EFI_SW_PC_INIT_BEGIN
),
543 &DriverEntry
->ImageHandle
,
544 sizeof (DriverEntry
->ImageHandle
)
546 ASSERT (DriverEntry
->ImageHandle
!= NULL
);
548 Status
= CoreStartImage (DriverEntry
->ImageHandle
, NULL
, NULL
);
550 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
552 (EFI_SOFTWARE_DXE_CORE
| EFI_SW_PC_INIT_END
),
553 &DriverEntry
->ImageHandle
,
554 sizeof (DriverEntry
->ImageHandle
)
558 ReturnStatus
= EFI_SUCCESS
;
562 // Now DXE Dispatcher finished one round of dispatch, signal an event group
563 // so that SMM Dispatcher get chance to dispatch SMM Drivers which depend
566 if (!EFI_ERROR (ReturnStatus
)) {
567 CoreSignalEvent (DxeDispatchEvent
);
571 // Search DriverList for items to place on Scheduled Queue
574 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
575 DriverEntry
= CR (Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
577 if (DriverEntry
->DepexProtocolError
){
579 // If Section Extraction Protocol did not let the Depex be read before retry the read
581 Status
= CoreGetDepexSectionAndPreProccess (DriverEntry
);
584 if (DriverEntry
->Dependent
) {
585 if (CoreIsSchedulable (DriverEntry
)) {
586 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
590 if (DriverEntry
->Unrequested
) {
591 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
592 DEBUG ((DEBUG_DISPATCH
, " SOR = Not Requested\n"));
593 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE\n"));
597 } while (ReadyToRun
);
600 // Close DXE dispatch Event
602 CoreCloseEvent (DxeDispatchEvent
);
604 gDispatcherRunning
= FALSE
;
611 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
612 must add any driver with a before dependency on InsertedDriverEntry first.
613 You do this by recursively calling this routine. After all the Befores are
614 processed you can add InsertedDriverEntry to the mScheduledQueue.
615 Then you can add any driver with an After dependency on InsertedDriverEntry
616 by recursively calling this routine.
618 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
622 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
623 IN EFI_CORE_DRIVER_ENTRY
*InsertedDriverEntry
627 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
630 // Process Before Dependency
632 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
633 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
634 if (DriverEntry
->Before
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
635 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
636 DEBUG ((DEBUG_DISPATCH
, " BEFORE FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
637 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
639 // Recursively process BEFORE
641 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
642 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
644 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
650 // Convert driver from Dependent to Scheduled state
652 CoreAcquireDispatcherLock ();
654 InsertedDriverEntry
->Dependent
= FALSE
;
655 InsertedDriverEntry
->Scheduled
= TRUE
;
656 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
658 CoreReleaseDispatcherLock ();
661 // Process After Dependency
663 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
664 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
665 if (DriverEntry
->After
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
666 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
667 DEBUG ((DEBUG_DISPATCH
, " AFTER FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
668 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
670 // Recursively process AFTER
672 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
673 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
675 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
683 Return TRUE if the Fv has been processed, FALSE if not.
685 @param FvHandle The handle of a FV that's being tested
687 @retval TRUE Fv protocol on FvHandle has been processed
688 @retval FALSE Fv protocol on FvHandle has not yet been processed
693 IN EFI_HANDLE FvHandle
697 KNOWN_HANDLE
*KnownHandle
;
699 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
700 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
701 if (KnownHandle
->Handle
== FvHandle
) {
710 Remember that Fv protocol on FvHandle has had it's drivers placed on the
711 mDiscoveredList. This fucntion adds entries on the mFvHandleList if new
712 entry is different from one in mFvHandleList by checking FvImage Guid.
713 Items are never removed/freed from the mFvHandleList.
715 @param FvHandle The handle of a FV that has been processed
717 @return A point to new added FvHandle entry. If FvHandle with the same FvImage guid
718 has been added, NULL will return.
722 FvIsBeingProcesssed (
723 IN EFI_HANDLE FvHandle
728 BOOLEAN FvNameGuidIsFound
;
729 UINT32 ExtHeaderOffset
;
730 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
731 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
732 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
737 KNOWN_HANDLE
*KnownHandle
;
740 // Get the FirmwareVolumeBlock protocol on that handle
742 FvNameGuidIsFound
= FALSE
;
743 Status
= CoreHandleProtocol (FvHandle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
744 if (!EFI_ERROR (Status
)) {
746 // Get the full FV header based on FVB protocol.
748 ASSERT (Fvb
!= NULL
);
749 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
750 if (!EFI_ERROR (Status
)) {
751 ASSERT (FwVolHeader
!= NULL
);
752 if (VerifyFvHeaderChecksum (FwVolHeader
) && FwVolHeader
->ExtHeaderOffset
!= 0) {
753 ExtHeaderOffset
= (UINT32
) FwVolHeader
->ExtHeaderOffset
;
754 BlockMap
= FwVolHeader
->BlockMap
;
758 // Find LbaIndex and LbaOffset for FV extension header based on BlockMap.
760 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
761 for (Index
= 0; Index
< BlockMap
->NumBlocks
&& ExtHeaderOffset
>= BlockMap
->Length
; Index
++) {
762 ExtHeaderOffset
-= BlockMap
->Length
;
766 // Check whether FvExtHeader is crossing the multi block range.
768 if (Index
< BlockMap
->NumBlocks
) {
769 LbaOffset
= ExtHeaderOffset
;
775 // Read FvNameGuid from FV extension header.
777 Status
= ReadFvbData (Fvb
, &LbaIndex
, &LbaOffset
, sizeof (FvNameGuid
), (UINT8
*) &FvNameGuid
);
778 if (!EFI_ERROR (Status
)) {
779 FvNameGuidIsFound
= TRUE
;
782 CoreFreePool (FwVolHeader
);
786 if (FvNameGuidIsFound
) {
788 // Check whether the FV image with the found FvNameGuid has been processed.
790 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
791 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
792 if (CompareGuid (&FvNameGuid
, &KnownHandle
->FvNameGuid
)) {
793 DEBUG ((EFI_D_ERROR
, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle
, KnownHandle
->Handle
, FvNameGuid
));
799 KnownHandle
= AllocateZeroPool (sizeof (KNOWN_HANDLE
));
800 ASSERT (KnownHandle
!= NULL
);
802 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
803 KnownHandle
->Handle
= FvHandle
;
804 if (FvNameGuidIsFound
) {
805 CopyGuid (&KnownHandle
->FvNameGuid
, &FvNameGuid
);
807 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
815 Convert FvHandle and DriverName into an EFI device path
817 @param Fv Fv protocol, needed to read Depex info out of
819 @param FvHandle Handle for Fv, needed in the
820 EFI_CORE_DRIVER_ENTRY so that the PE image can be
821 read out of the FV at a later time.
822 @param DriverName Name of driver to add to mDiscoveredList.
824 @return Pointer to device path constructed from FvHandle and DriverName
827 EFI_DEVICE_PATH_PROTOCOL
*
829 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
830 IN EFI_HANDLE FvHandle
,
831 IN EFI_GUID
*DriverName
835 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
836 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
839 // Remember the device path of the FV
841 Status
= CoreHandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
842 if (EFI_ERROR (Status
)) {
843 FileNameDevicePath
= NULL
;
846 // Build a device path to the file in the FV to pass into gBS->LoadImage
848 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
849 SetDevicePathEndNode (&mFvDevicePath
.End
);
851 FileNameDevicePath
= AppendDevicePath (
853 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
857 return FileNameDevicePath
;
863 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
864 and initilize any state variables. Read the Depex from the FV and store it
865 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
866 The Discovered list is never free'ed and contains booleans that represent the
867 other possible DXE driver states.
869 @param Fv Fv protocol, needed to read Depex info out of
871 @param FvHandle Handle for Fv, needed in the
872 EFI_CORE_DRIVER_ENTRY so that the PE image can be
873 read out of the FV at a later time.
874 @param DriverName Name of driver to add to mDiscoveredList.
875 @param Type Fv File Type of file to add to mDiscoveredList.
877 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
878 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
879 DriverName may be active in the system at any one
884 CoreAddToDriverList (
885 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
886 IN EFI_HANDLE FvHandle
,
887 IN EFI_GUID
*DriverName
,
888 IN EFI_FV_FILETYPE Type
891 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
895 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
898 DriverEntry
= AllocateZeroPool (sizeof (EFI_CORE_DRIVER_ENTRY
));
899 ASSERT (DriverEntry
!= NULL
);
900 if (Type
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
901 DriverEntry
->IsFvImage
= TRUE
;
904 DriverEntry
->Signature
= EFI_CORE_DRIVER_ENTRY_SIGNATURE
;
905 CopyGuid (&DriverEntry
->FileName
, DriverName
);
906 DriverEntry
->FvHandle
= FvHandle
;
907 DriverEntry
->Fv
= Fv
;
908 DriverEntry
->FvFileDevicePath
= CoreFvToDevicePath (Fv
, FvHandle
, DriverName
);
910 CoreGetDepexSectionAndPreProccess (DriverEntry
);
912 CoreAcquireDispatcherLock ();
914 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
916 CoreReleaseDispatcherLock ();
923 Check if a FV Image type file (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) is
924 described by a EFI_HOB_FIRMWARE_VOLUME2 Hob.
926 @param FvNameGuid The FV image guid specified.
927 @param DriverName The driver guid specified.
929 @retval TRUE This file is found in a EFI_HOB_FIRMWARE_VOLUME2
931 @retval FALSE Not found.
936 IN CONST EFI_GUID
*FvNameGuid
,
937 IN CONST EFI_GUID
*DriverName
940 EFI_PEI_HOB_POINTERS HobFv2
;
942 HobFv2
.Raw
= GetHobList ();
944 while ((HobFv2
.Raw
= GetNextHob (EFI_HOB_TYPE_FV2
, HobFv2
.Raw
)) != NULL
) {
946 // Compare parent FvNameGuid and FileGuid both.
948 if (CompareGuid (DriverName
, &HobFv2
.FirmwareVolume2
->FileName
) &&
949 CompareGuid (FvNameGuid
, &HobFv2
.FirmwareVolume2
->FvName
)) {
952 HobFv2
.Raw
= GET_NEXT_HOB (HobFv2
);
961 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
963 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.
964 @param FvHandle The handle which FVB protocol installed on.
965 @param DriverName The driver guid specified.
967 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.
968 @retval EFI_VOLUME_CORRUPTED Corrupted volume.
969 @retval EFI_SUCCESS Function successfully returned.
973 CoreProcessFvImageFile (
974 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
975 IN EFI_HANDLE FvHandle
,
976 IN EFI_GUID
*DriverName
980 EFI_SECTION_TYPE SectionType
;
981 UINT32 AuthenticationStatus
;
985 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
987 EFI_DEVICE_PATH_PROTOCOL
*FvFileDevicePath
;
990 // Read the first (and only the first) firmware volume section
992 SectionType
= EFI_SECTION_FIRMWARE_VOLUME_IMAGE
;
997 AlignedBuffer
= NULL
;
998 Status
= Fv
->ReadSection (
1005 &AuthenticationStatus
1007 if (!EFI_ERROR (Status
)) {
1009 // Evaluate the authentication status of the Firmware Volume through
1010 // Security Architectural Protocol
1012 if (gSecurity
!= NULL
) {
1013 FvFileDevicePath
= CoreFvToDevicePath (Fv
, FvHandle
, DriverName
);
1014 Status
= gSecurity
->FileAuthenticationState (
1016 AuthenticationStatus
,
1019 if (FvFileDevicePath
!= NULL
) {
1020 FreePool (FvFileDevicePath
);
1023 if (Status
!= EFI_SUCCESS
) {
1025 // Security check failed. The firmware volume should not be used for any purpose.
1027 if (Buffer
!= NULL
) {
1035 // FvImage should be at its required alignment.
1037 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) Buffer
;
1039 // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume
1040 // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from
1041 // its initial linked location and maintain its alignment.
1043 if ((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) {
1045 // Get FvHeader alignment
1047 FvAlignment
= 1 << ((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
1049 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.
1051 if (FvAlignment
< 8) {
1055 // Allocate the aligned buffer for the FvImage.
1057 AlignedBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), (UINTN
) FvAlignment
);
1058 if (AlignedBuffer
== NULL
) {
1060 return EFI_OUT_OF_RESOURCES
;
1063 // Move FvImage into the aligned buffer and release the original buffer.
1065 CopyMem (AlignedBuffer
, Buffer
, BufferSize
);
1066 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) AlignedBuffer
;
1067 CoreFreePool (Buffer
);
1072 // Produce a FVB protocol for the file
1074 Status
= ProduceFVBProtocolOnBuffer (
1075 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FvHeader
,
1078 AuthenticationStatus
,
1083 if (EFI_ERROR (Status
)) {
1085 // ReadSection or Produce FVB failed, Free data buffer
1087 if (Buffer
!= NULL
) {
1091 if (AlignedBuffer
!= NULL
) {
1092 FreeAlignedPages (AlignedBuffer
, EFI_SIZE_TO_PAGES (BufferSize
));
1101 Event notification that is fired every time a FV dispatch protocol is added.
1102 More than one protocol may have been added when this event is fired, so you
1103 must loop on CoreLocateHandle () to see how many protocols were added and
1104 do the following to each FV:
1105 If the Fv has already been processed, skip it. If the Fv has not been
1106 processed then mark it as being processed, as we are about to process it.
1107 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1108 mDiscoveredList is never free'ed and contains variables that define
1109 the other states the DXE driver transitions to..
1110 While you are at it read the A Priori file into memory.
1111 Place drivers in the A Priori list onto the mScheduledQueue.
1113 @param Event The Event that is being processed, not used.
1114 @param Context Event Context, not used.
1119 CoreFwVolEventProtocolNotify (
1125 EFI_STATUS GetNextFileStatus
;
1126 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1127 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1128 EFI_HANDLE FvHandle
;
1132 EFI_FV_FILETYPE Type
;
1133 EFI_FV_FILE_ATTRIBUTES Attributes
;
1135 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
1136 EFI_GUID
*AprioriFile
;
1137 UINTN AprioriEntryCount
;
1140 UINT32 AuthenticationStatus
;
1143 KNOWN_HANDLE
*KnownHandle
;
1146 BufferSize
= sizeof (EFI_HANDLE
);
1147 Status
= CoreLocateHandle (
1150 mFwVolEventRegistration
,
1154 if (EFI_ERROR (Status
)) {
1156 // If no more notification events exit
1161 if (FvHasBeenProcessed (FvHandle
)) {
1163 // This Fv has already been processed so lets skip it!
1169 // Since we are about to process this Fv mark it as processed.
1171 KnownHandle
= FvIsBeingProcesssed (FvHandle
);
1172 if (KnownHandle
== NULL
) {
1174 // The FV with the same FV name guid has already been processed.
1180 Status
= CoreHandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1181 if (EFI_ERROR (Status
) || Fv
== NULL
) {
1183 // FvHandle must have Firmware Volume2 protocol thus we should never get here.
1189 Status
= CoreHandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1190 if (EFI_ERROR (Status
)) {
1192 // The Firmware volume doesn't have device path, can't be dispatched.
1198 // Discover Drivers in FV and add them to the Discovered Driver List.
1199 // Process EFI_FV_FILETYPE_DRIVER type and then EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
1200 // EFI_FV_FILETYPE_DXE_CORE is processed to produce a Loaded Image protocol for the core
1201 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE is processed to create a Fvb
1203 for (Index
= 0; Index
< sizeof (mDxeFileTypes
) / sizeof (EFI_FV_FILETYPE
); Index
++) {
1205 // Initialize the search key
1209 Type
= mDxeFileTypes
[Index
];
1210 GetNextFileStatus
= Fv
->GetNextFile (
1218 if (!EFI_ERROR (GetNextFileStatus
)) {
1219 if (Type
== EFI_FV_FILETYPE_DXE_CORE
) {
1221 // If this is the DXE core fill in it's DevicePath & DeviceHandle
1223 if (gDxeCoreLoadedImage
->FilePath
== NULL
) {
1224 if (CompareGuid (&NameGuid
, gDxeCoreFileName
)) {
1226 // Maybe One specail Fv cantains only one DXE_CORE module, so its device path must
1227 // be initialized completely.
1229 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, &NameGuid
);
1230 SetDevicePathEndNode (&mFvDevicePath
.End
);
1232 gDxeCoreLoadedImage
->FilePath
= DuplicateDevicePath (
1233 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
1235 gDxeCoreLoadedImage
->DeviceHandle
= FvHandle
;
1238 } else if (Type
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
1240 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
1243 if (FvFoundInHobFv2 (&KnownHandle
->FvNameGuid
, &NameGuid
)) {
1248 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has PEI depex section.
1252 Status
= Fv
->ReadSection (
1255 EFI_SECTION_PEI_DEPEX
,
1259 &AuthenticationStatus
1261 if (!EFI_ERROR (Status
)) {
1263 // If PEI depex section is found, this FV image will be ignored in DXE phase.
1264 // Now, DxeCore doesn't support FV image with more one type DEPEX section.
1266 FreePool (DepexBuffer
);
1271 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has SMM depex section.
1275 Status
= Fv
->ReadSection (
1278 EFI_SECTION_SMM_DEPEX
,
1282 &AuthenticationStatus
1284 if (!EFI_ERROR (Status
)) {
1286 // If SMM depex section is found, this FV image will be ignored in DXE phase.
1287 // Now, DxeCore doesn't support FV image with more one type DEPEX section.
1289 FreePool (DepexBuffer
);
1294 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has DXE depex section.
1298 Status
= Fv
->ReadSection (
1301 EFI_SECTION_DXE_DEPEX
,
1305 &AuthenticationStatus
1307 if (EFI_ERROR (Status
)) {
1309 // If no depex section, produce a firmware volume block protocol for it so it gets dispatched from.
1311 CoreProcessFvImageFile (Fv
, FvHandle
, &NameGuid
);
1314 // If depex section is found, this FV image will be dispatched until its depex is evaluated to TRUE.
1316 FreePool (DepexBuffer
);
1317 CoreAddToDriverList (Fv
, FvHandle
, &NameGuid
, Type
);
1321 // Transition driver from Undiscovered to Discovered state
1323 CoreAddToDriverList (Fv
, FvHandle
, &NameGuid
, Type
);
1326 } while (!EFI_ERROR (GetNextFileStatus
));
1330 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1333 Status
= Fv
->ReadSection (
1338 (VOID
**)&AprioriFile
,
1340 &AuthenticationStatus
1342 if (!EFI_ERROR (Status
)) {
1343 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1345 AprioriEntryCount
= 0;
1349 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1350 // drivers not in the current FV and these must be skipped since the a priori list
1351 // is only valid for the FV that it resided in.
1354 for (Index
= 0; Index
< AprioriEntryCount
; Index
++) {
1355 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1356 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
1357 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[Index
]) &&
1358 (FvHandle
== DriverEntry
->FvHandle
)) {
1359 CoreAcquireDispatcherLock ();
1360 DriverEntry
->Dependent
= FALSE
;
1361 DriverEntry
->Scheduled
= TRUE
;
1362 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1363 CoreReleaseDispatcherLock ();
1364 DEBUG ((DEBUG_DISPATCH
, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1365 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1372 // Free data allocated by Fv->ReadSection ()
1374 CoreFreePool (AprioriFile
);
1381 Initialize the dispatcher. Initialize the notification function that runs when
1382 an FV2 protocol is added to the system.
1386 CoreInitializeDispatcher (
1390 mFwVolEvent
= EfiCreateProtocolNotifyEvent (
1391 &gEfiFirmwareVolume2ProtocolGuid
,
1393 CoreFwVolEventProtocolNotify
,
1395 &mFwVolEventRegistration
1400 // Function only used in debug builds
1404 Traverse the discovered list for any drivers that were discovered but not loaded
1405 because the dependency experessions evaluated to false.
1409 CoreDisplayDiscoveredNotDispatched (
1414 EFI_CORE_DRIVER_ENTRY
*DriverEntry
;
1416 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1417 DriverEntry
= CR(Link
, EFI_CORE_DRIVER_ENTRY
, Link
, EFI_CORE_DRIVER_ENTRY_SIGNATURE
);
1418 if (DriverEntry
->Dependent
) {
1419 DEBUG ((DEBUG_LOAD
, "Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));