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 similar to the DXE dispatcher.
25 The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
26 is the state diagram for the DXE dispatcher
28 Depex - Dependency Expresion.
29 SOR - Schedule On Request - Don't schedule if this bit is set.
31 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
32 This program and the accompanying materials are licensed and made available
33 under the terms and conditions of the BSD License which accompanies this
34 distribution. The full text of the license may be found at
35 http://opensource.org/licenses/bsd-license.php
37 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
42 #include "PiSmmCore.h"
45 // SMM Dispatcher Data structures
47 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
50 LIST_ENTRY Link
; // mFvHandleList
55 // Function Prototypes
59 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
60 must add any driver with a before dependency on InsertedDriverEntry first.
61 You do this by recursively calling this routine. After all the Befores are
62 processed you can add InsertedDriverEntry to the mScheduledQueue.
63 Then you can add any driver with an After dependency on InsertedDriverEntry
64 by recursively calling this routine.
66 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
70 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
71 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
75 // The Driver List contains one copy of every driver that has been discovered.
76 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
78 LIST_ENTRY mDiscoveredList
= INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList
);
81 // Queue of drivers that are ready to dispatch. This queue is a subset of the
82 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
84 LIST_ENTRY mScheduledQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue
);
87 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
89 LIST_ENTRY mFvHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList
);
92 // Flag for the SMM Dispacher. TRUE if dispatcher is execuing.
94 BOOLEAN gDispatcherRunning
= FALSE
;
97 // Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched
99 BOOLEAN gRequestDispatch
= FALSE
;
102 // List of file types supported by dispatcher
104 EFI_FV_FILETYPE mSmmFileTypes
[] = {
106 EFI_FV_FILETYPE_COMBINED_SMM_DXE
108 // Note: DXE core will process the FV image file, so skip it in SMM core
109 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
114 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File
;
115 EFI_DEVICE_PATH_PROTOCOL End
;
116 } FV_FILEPATH_DEVICE_PATH
;
118 FV_FILEPATH_DEVICE_PATH mFvDevicePath
;
121 // DXE Architecture Protocols
123 EFI_SECURITY_ARCH_PROTOCOL
*mSecurity
= NULL
;
126 Loads an EFI image into SMRAM.
128 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
136 IN OUT EFI_SMM_DRIVER_ENTRY
*DriverEntry
139 UINT32 AuthenticationStatus
;
146 EFI_STATUS SecurityStatus
;
147 EFI_HANDLE DeviceHandle
;
148 EFI_PHYSICAL_ADDRESS DstBuffer
;
149 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
150 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
151 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
152 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
153 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
157 Fv
= DriverEntry
->Fv
;
158 NameGuid
= &DriverEntry
->FileName
;
159 FilePath
= DriverEntry
->FvFileDevicePath
;
161 OriginalFilePath
= FilePath
;
162 HandleFilePath
= FilePath
;
164 SecurityStatus
= EFI_SUCCESS
;
165 Status
= EFI_SUCCESS
;
166 AuthenticationStatus
= 0;
169 // Try to get the image device handle by checking the match protocol.
171 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
172 if (EFI_ERROR(Status
)) {
177 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
179 if (mSecurity
== NULL
) {
180 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
184 // Verify the Authentication Status through the Security Architectural Protocol
186 if ((mSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
187 SecurityStatus
= mSecurity
->FileAuthenticationState (
189 AuthenticationStatus
,
192 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
193 Status
= SecurityStatus
;
199 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
201 FilePath
= OriginalFilePath
;
202 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
203 if (!EFI_ERROR (Status
)) {
204 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
205 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
209 // Try reading PE32 section firstly
211 Status
= Fv
->ReadSection (
218 &AuthenticationStatus
221 if (EFI_ERROR (Status
)) {
223 // Try reading TE section secondly
227 Status
= Fv
->ReadSection (
234 &AuthenticationStatus
238 if (EFI_ERROR (Status
)) {
239 if (Buffer
!= NULL
) {
240 Status
= gBS
->FreePool (Buffer
);
246 // Initialize ImageContext
248 ImageContext
.Handle
= Buffer
;
249 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
252 // Get information about the image being loaded
254 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
255 if (EFI_ERROR (Status
)) {
256 if (Buffer
!= NULL
) {
257 Status
= gBS
->FreePool (Buffer
);
262 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
263 DstBuffer
= (UINTN
)(-1);
265 Status
= SmmAllocatePages (
267 EfiRuntimeServicesCode
,
271 if (EFI_ERROR (Status
)) {
272 if (Buffer
!= NULL
) {
273 Status
= gBS
->FreePool (Buffer
);
278 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
280 // Align buffer on section boundry
282 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
283 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
286 // Load the image to our new buffer
288 Status
= PeCoffLoaderLoadImage (&ImageContext
);
289 if (EFI_ERROR (Status
)) {
290 if (Buffer
!= NULL
) {
291 Status
= gBS
->FreePool (Buffer
);
293 SmmFreePages (DstBuffer
, PageCount
);
298 // Relocate the image in our new buffer
300 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
301 if (EFI_ERROR (Status
)) {
302 if (Buffer
!= NULL
) {
303 Status
= gBS
->FreePool (Buffer
);
305 SmmFreePages (DstBuffer
, PageCount
);
310 // Flush the instruction cache so the image data are written before we execute it
312 InvalidateInstructionCacheRange ((VOID
*)(UINTN
) ImageContext
.ImageAddress
, (UINTN
) ImageContext
.ImageSize
);
315 // Save Image EntryPoint in DriverEntry
317 DriverEntry
->ImageEntryPoint
= ImageContext
.EntryPoint
;
318 DriverEntry
->ImageBuffer
= DstBuffer
;
319 DriverEntry
->NumberOfPage
= PageCount
;
322 // Allocate a Loaded Image Protocol in EfiBootServicesData
324 Status
= gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
), (VOID
**)&DriverEntry
->LoadedImage
);
325 if (EFI_ERROR (Status
)) {
326 if (Buffer
!= NULL
) {
327 Status
= gBS
->FreePool (Buffer
);
329 SmmFreePages (DstBuffer
, PageCount
);
334 // Fill in the remaining fields of the Loaded Image Protocol instance.
335 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
337 DriverEntry
->LoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
338 DriverEntry
->LoadedImage
->ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
339 DriverEntry
->LoadedImage
->SystemTable
= gST
;
340 DriverEntry
->LoadedImage
->DeviceHandle
= DeviceHandle
;
343 // Make an EfiBootServicesData buffer copy of FilePath
345 Status
= gBS
->AllocatePool (EfiBootServicesData
, GetDevicePathSize (FilePath
), (VOID
**)&DriverEntry
->LoadedImage
->FilePath
);
346 if (EFI_ERROR (Status
)) {
347 if (Buffer
!= NULL
) {
348 Status
= gBS
->FreePool (Buffer
);
350 SmmFreePages (DstBuffer
, PageCount
);
353 CopyMem (DriverEntry
->LoadedImage
->FilePath
, FilePath
, GetDevicePathSize (FilePath
));
355 DriverEntry
->LoadedImage
->ImageBase
= (VOID
*)(UINTN
)DriverEntry
->ImageBuffer
;
356 DriverEntry
->LoadedImage
->ImageSize
= ImageContext
.ImageSize
;
357 DriverEntry
->LoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
358 DriverEntry
->LoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
361 // Create a new image handle in the UEFI handle database for the SMM Driver
363 DriverEntry
->ImageHandle
= NULL
;
364 Status
= gBS
->InstallMultipleProtocolInterfaces (
365 &DriverEntry
->ImageHandle
,
366 &gEfiLoadedImageProtocolGuid
, DriverEntry
->LoadedImage
,
371 // Print the load address and the PDB file name if it is available
378 CHAR8 EfiFileName
[256];
381 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
382 "Loading driver at 0x%11p EntryPoint=0x%11p ",
383 (VOID
*)(UINTN
) ImageContext
.ImageAddress
,
384 FUNCTION_ENTRY_POINT (ImageContext
.EntryPoint
)));
388 // Print Module Name by Pdb file path.
389 // Windows and Unix style file path are all trimmed correctly.
391 if (ImageContext
.PdbPointer
!= NULL
) {
393 for (Index
= 0; ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
394 if ((ImageContext
.PdbPointer
[Index
] == '\\') || (ImageContext
.PdbPointer
[Index
] == '/')) {
395 StartIndex
= Index
+ 1;
399 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
400 // The PDB file name is limited in the range of 0~255.
401 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
403 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
404 EfiFileName
[Index
] = ImageContext
.PdbPointer
[Index
+ StartIndex
];
405 if (EfiFileName
[Index
] == 0) {
406 EfiFileName
[Index
] = '.';
408 if (EfiFileName
[Index
] == '.') {
409 EfiFileName
[Index
+ 1] = 'e';
410 EfiFileName
[Index
+ 2] = 'f';
411 EfiFileName
[Index
+ 3] = 'i';
412 EfiFileName
[Index
+ 4] = 0;
417 if (Index
== sizeof (EfiFileName
) - 4) {
418 EfiFileName
[Index
] = 0;
420 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
422 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
427 // Free buffer allocated by Fv->ReadSection.
429 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
430 // used the UEFI Boot Services AllocatePool() function
432 Status
= gBS
->FreePool(Buffer
);
437 Preprocess dependency expression and update DriverEntry to reflect the
438 state of Before, After, and SOR dependencies. If DriverEntry->Before
439 or DriverEntry->After is set it will never be cleared. If SOR is set
440 it will be cleared by SmmSchedule(), and then the driver can be
443 @param DriverEntry DriverEntry element to update .
445 @retval EFI_SUCCESS It always works.
450 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
455 Iterator
= DriverEntry
->Depex
;
456 if (*Iterator
== EFI_DEP_SOR
) {
457 DriverEntry
->Unrequested
= TRUE
;
459 DriverEntry
->Dependent
= TRUE
;
462 if (*Iterator
== EFI_DEP_BEFORE
) {
463 DriverEntry
->Before
= TRUE
;
464 } else if (*Iterator
== EFI_DEP_AFTER
) {
465 DriverEntry
->After
= TRUE
;
468 if (DriverEntry
->Before
|| DriverEntry
->After
) {
469 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
476 Read Depex and pre-process the Depex for Before and After. If Section Extraction
477 protocol returns an error via ReadSection defer the reading of the Depex.
479 @param DriverEntry Driver to work on.
481 @retval EFI_SUCCESS Depex read and preprossesed
482 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
483 and Depex reading needs to be retried.
484 @retval Error DEPEX not found.
488 SmmGetDepexSectionAndPreProccess (
489 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
493 EFI_SECTION_TYPE SectionType
;
494 UINT32 AuthenticationStatus
;
495 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
497 Fv
= DriverEntry
->Fv
;
500 // Grab Depex info, it will never be free'ed.
501 // (Note: DriverEntry->Depex is in DXE memory)
503 SectionType
= EFI_SECTION_SMM_DEPEX
;
504 Status
= Fv
->ReadSection (
506 &DriverEntry
->FileName
,
510 (UINTN
*)&DriverEntry
->DepexSize
,
511 &AuthenticationStatus
513 if (EFI_ERROR (Status
)) {
514 if (Status
== EFI_PROTOCOL_ERROR
) {
516 // The section extraction protocol failed so set protocol error flag
518 DriverEntry
->DepexProtocolError
= TRUE
;
521 // If no Depex assume depend on all architectural protocols
523 DriverEntry
->Depex
= NULL
;
524 DriverEntry
->Dependent
= TRUE
;
525 DriverEntry
->DepexProtocolError
= FALSE
;
529 // Set Before, After, and Unrequested state information based on Depex
530 // Driver will be put in Dependent or Unrequested state
532 SmmPreProcessDepex (DriverEntry
);
533 DriverEntry
->DepexProtocolError
= FALSE
;
540 Check every driver and locate a matching one. If the driver is found, the Unrequested
541 state flag is cleared.
543 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
544 the firmware file specified by DriverName.
545 @param DriverName The Driver name to put in the Dependent state.
547 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
549 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
555 IN EFI_HANDLE FirmwareVolumeHandle
,
556 IN EFI_GUID
*DriverName
560 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
563 // Check every driver
565 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
566 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
567 if (DriverEntry
->FvHandle
== FirmwareVolumeHandle
&&
568 DriverEntry
->Unrequested
&&
569 CompareGuid (DriverName
, &DriverEntry
->FileName
)) {
571 // Move the driver from the Unrequested to the Dependent state
573 DriverEntry
->Unrequested
= FALSE
;
574 DriverEntry
->Dependent
= TRUE
;
579 return EFI_NOT_FOUND
;
583 This is the main Dispatcher for SMM and it exits when there are no more
584 drivers to run. Drain the mScheduledQueue and load and start a PE
585 image for each driver. Search the mDiscoveredList to see if any driver can
586 be placed on the mScheduledQueue. If no drivers are placed on the
587 mScheduledQueue exit the function. On exit it is assumed the Bds()
588 will be called, and when the Bds() exits the Dispatcher will be called
591 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
592 @retval EFI_NOT_FOUND No SMM Drivers were dispatched
593 @retval EFI_SUCCESS One or more SMM Drivers were dispatched
602 EFI_STATUS ReturnStatus
;
604 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
607 if (!gRequestDispatch
) {
608 return EFI_NOT_FOUND
;
611 if (gDispatcherRunning
) {
613 // If the dispatcher is running don't let it be restarted.
615 return EFI_ALREADY_STARTED
;
618 gDispatcherRunning
= TRUE
;
620 ReturnStatus
= EFI_NOT_FOUND
;
623 // Drain the Scheduled Queue
625 while (!IsListEmpty (&mScheduledQueue
)) {
627 mScheduledQueue
.ForwardLink
,
628 EFI_SMM_DRIVER_ENTRY
,
630 EFI_SMM_DRIVER_ENTRY_SIGNATURE
634 // Load the SMM Driver image into memory. If the Driver was transitioned from
635 // Untrused to Scheduled it would have already been loaded so we may need to
636 // skip the LoadImage
638 if (DriverEntry
->ImageHandle
== NULL
) {
639 Status
= SmmLoadImage (DriverEntry
);
642 // Update the driver state to reflect that it's been loaded
644 if (EFI_ERROR (Status
)) {
646 if (Status
== EFI_SECURITY_VIOLATION
) {
648 // Take driver from Scheduled to Untrused state
650 DriverEntry
->Untrusted
= TRUE
;
653 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
654 // Take driver from Scheduled to Initialized.
656 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
658 DriverEntry
->Initialized
= TRUE
;
661 DriverEntry
->Scheduled
= FALSE
;
662 RemoveEntryList (&DriverEntry
->ScheduledLink
);
665 // If it's an error don't try the StartImage
671 DriverEntry
->Scheduled
= FALSE
;
672 DriverEntry
->Initialized
= TRUE
;
673 RemoveEntryList (&DriverEntry
->ScheduledLink
);
675 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
677 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_BEGIN
,
678 &DriverEntry
->ImageHandle
,
679 sizeof (DriverEntry
->ImageHandle
)
683 // For each SMM driver, pass NULL as ImageHandle
685 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
)(DriverEntry
->ImageHandle
, gST
);
686 if (EFI_ERROR(Status
)){
687 SmmFreePages(DriverEntry
->ImageBuffer
, DriverEntry
->NumberOfPage
);
690 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
692 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_END
,
693 &DriverEntry
->ImageHandle
,
694 sizeof (DriverEntry
->ImageHandle
)
697 ReturnStatus
= EFI_SUCCESS
;
701 // Search DriverList for items to place on Scheduled Queue
704 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
705 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
707 if (DriverEntry
->DepexProtocolError
){
709 // If Section Extraction Protocol did not let the Depex be read before retry the read
711 Status
= SmmGetDepexSectionAndPreProccess (DriverEntry
);
714 if (DriverEntry
->Dependent
) {
715 if (SmmIsSchedulable (DriverEntry
)) {
716 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
721 } while (ReadyToRun
);
724 // If there is no more SMM driver to dispatch, stop the dispatch request
726 gRequestDispatch
= FALSE
;
727 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
728 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
730 if (!DriverEntry
->Initialized
){
732 // We have SMM driver pending to dispatch
734 gRequestDispatch
= TRUE
;
739 gDispatcherRunning
= FALSE
;
745 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
746 must add any driver with a before dependency on InsertedDriverEntry first.
747 You do this by recursively calling this routine. After all the Befores are
748 processed you can add InsertedDriverEntry to the mScheduledQueue.
749 Then you can add any driver with an After dependency on InsertedDriverEntry
750 by recursively calling this routine.
752 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
756 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
757 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
761 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
764 // Process Before Dependency
766 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
767 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
768 if (DriverEntry
->Before
&& DriverEntry
->Dependent
) {
769 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
771 // Recursively process BEFORE
773 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
779 // Convert driver from Dependent to Scheduled state
782 InsertedDriverEntry
->Dependent
= FALSE
;
783 InsertedDriverEntry
->Scheduled
= TRUE
;
784 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
788 // Process After Dependency
790 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
791 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
792 if (DriverEntry
->After
&& DriverEntry
->Dependent
) {
793 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
795 // Recursively process AFTER
797 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
804 Return TRUE if the Fv has been processed, FALSE if not.
806 @param FvHandle The handle of a FV that's being tested
808 @retval TRUE Fv protocol on FvHandle has been processed
809 @retval FALSE Fv protocol on FvHandle has not yet been
815 IN EFI_HANDLE FvHandle
819 KNOWN_HANDLE
*KnownHandle
;
821 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
822 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
823 if (KnownHandle
->Handle
== FvHandle
) {
831 Remember that Fv protocol on FvHandle has had it's drivers placed on the
832 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
833 never removed/freed from the mFvHandleList.
835 @param FvHandle The handle of a FV that has been processed
839 FvIsBeingProcesssed (
840 IN EFI_HANDLE FvHandle
843 KNOWN_HANDLE
*KnownHandle
;
845 KnownHandle
= AllocatePool (sizeof (KNOWN_HANDLE
));
846 ASSERT (KnownHandle
!= NULL
);
848 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
849 KnownHandle
->Handle
= FvHandle
;
850 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
854 Convert FvHandle and DriverName into an EFI device path
856 @param Fv Fv protocol, needed to read Depex info out of
858 @param FvHandle Handle for Fv, needed in the
859 EFI_SMM_DRIVER_ENTRY so that the PE image can be
860 read out of the FV at a later time.
861 @param DriverName Name of driver to add to mDiscoveredList.
863 @return Pointer to device path constructed from FvHandle and DriverName
866 EFI_DEVICE_PATH_PROTOCOL
*
868 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
869 IN EFI_HANDLE FvHandle
,
870 IN EFI_GUID
*DriverName
874 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
875 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
878 // Remember the device path of the FV
880 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
881 if (EFI_ERROR (Status
)) {
882 FileNameDevicePath
= NULL
;
885 // Build a device path to the file in the FV to pass into gBS->LoadImage
887 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
888 SetDevicePathEndNode (&mFvDevicePath
.End
);
891 // Note: FileNameDevicePath is in DXE memory
893 FileNameDevicePath
= AppendDevicePath (
895 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
898 return FileNameDevicePath
;
902 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
903 and initilize any state variables. Read the Depex from the FV and store it
904 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
905 The Discovered list is never free'ed and contains booleans that represent the
906 other possible SMM driver states.
908 @param Fv Fv protocol, needed to read Depex info out of
910 @param FvHandle Handle for Fv, needed in the
911 EFI_SMM_DRIVER_ENTRY so that the PE image can be
912 read out of the FV at a later time.
913 @param DriverName Name of driver to add to mDiscoveredList.
915 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
916 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
917 DriverName may be active in the system at any one
923 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
924 IN EFI_HANDLE FvHandle
,
925 IN EFI_GUID
*DriverName
928 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
931 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
934 DriverEntry
= AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY
));
935 ASSERT (DriverEntry
!= NULL
);
937 DriverEntry
->Signature
= EFI_SMM_DRIVER_ENTRY_SIGNATURE
;
938 CopyGuid (&DriverEntry
->FileName
, DriverName
);
939 DriverEntry
->FvHandle
= FvHandle
;
940 DriverEntry
->Fv
= Fv
;
941 DriverEntry
->FvFileDevicePath
= SmmFvToDevicePath (Fv
, FvHandle
, DriverName
);
943 SmmGetDepexSectionAndPreProccess (DriverEntry
);
945 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
946 gRequestDispatch
= TRUE
;
952 This function is the main entry point for an SMM handler dispatch
953 or communicate-based callback.
955 Event notification that is fired every time a FV dispatch protocol is added.
956 More than one protocol may have been added when this event is fired, so you
957 must loop on SmmLocateHandle () to see how many protocols were added and
958 do the following to each FV:
959 If the Fv has already been processed, skip it. If the Fv has not been
960 processed then mark it as being processed, as we are about to process it.
961 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
962 mDiscoveredList is never free'ed and contains variables that define
963 the other states the SMM driver transitions to..
964 While you are at it read the A Priori file into memory.
965 Place drivers in the A Priori list onto the mScheduledQueue.
967 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
968 @param Context Points to an optional handler context which was specified when the handler was registered.
969 @param CommBuffer A pointer to a collection of data in memory that will
970 be conveyed from a non-SMM environment into an SMM environment.
971 @param CommBufferSize The size of the CommBuffer.
978 SmmDriverDispatchHandler (
979 IN EFI_HANDLE DispatchHandle
,
980 IN CONST VOID
*Context
, OPTIONAL
981 IN OUT VOID
*CommBuffer
, OPTIONAL
982 IN OUT UINTN
*CommBufferSize OPTIONAL
987 EFI_HANDLE
*HandleBuffer
;
988 EFI_STATUS GetNextFileStatus
;
989 EFI_STATUS SecurityStatus
;
990 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
991 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
995 EFI_FV_FILETYPE Type
;
996 EFI_FV_FILE_ATTRIBUTES Attributes
;
998 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
999 EFI_GUID
*AprioriFile
;
1000 UINTN AprioriEntryCount
;
1003 UINT32 AuthenticationStatus
;
1006 HandleBuffer
= NULL
;
1007 Status
= gBS
->LocateHandleBuffer (
1009 &gEfiFirmwareVolume2ProtocolGuid
,
1014 if (EFI_ERROR (Status
)) {
1015 return EFI_NOT_FOUND
;
1018 for (Index
= 0; Index
< HandleCount
; Index
++) {
1019 FvHandle
= HandleBuffer
[Index
];
1021 if (FvHasBeenProcessed (FvHandle
)) {
1023 // This Fv has already been processed so lets skip it!
1029 // Since we are about to process this Fv mark it as processed.
1031 FvIsBeingProcesssed (FvHandle
);
1033 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1034 if (EFI_ERROR (Status
)) {
1036 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1042 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1043 if (EFI_ERROR (Status
)) {
1045 // The Firmware volume doesn't have device path, can't be dispatched.
1051 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1053 if (mSecurity
== NULL
) {
1054 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
1058 // Evaluate the authentication status of the Firmware Volume through
1059 // Security Architectural Protocol
1061 if (mSecurity
!= NULL
) {
1062 SecurityStatus
= mSecurity
->FileAuthenticationState (
1067 if (SecurityStatus
!= EFI_SUCCESS
) {
1069 // Security check failed. The firmware volume should not be used for any purpose.
1076 // Discover Drivers in FV and add them to the Discovered Driver List.
1077 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1079 for (Index
= 0; Index
< sizeof (mSmmFileTypes
)/sizeof (EFI_FV_FILETYPE
); Index
++) {
1081 // Initialize the search key
1085 Type
= mSmmFileTypes
[Index
];
1086 GetNextFileStatus
= Fv
->GetNextFile (
1094 if (!EFI_ERROR (GetNextFileStatus
)) {
1095 SmmAddToDriverList (Fv
, FvHandle
, &NameGuid
);
1097 } while (!EFI_ERROR (GetNextFileStatus
));
1101 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1102 // (Note: AprioriFile is in DXE memory)
1105 Status
= Fv
->ReadSection (
1110 (VOID
**)&AprioriFile
,
1112 &AuthenticationStatus
1114 if (!EFI_ERROR (Status
)) {
1115 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1117 AprioriEntryCount
= 0;
1121 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1122 // drivers not in the current FV and these must be skipped since the a priori list
1123 // is only valid for the FV that it resided in.
1126 for (Index
= 0; Index
< AprioriEntryCount
; Index
++) {
1127 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1128 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1129 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[Index
]) &&
1130 (FvHandle
== DriverEntry
->FvHandle
)) {
1131 DriverEntry
->Dependent
= FALSE
;
1132 DriverEntry
->Scheduled
= TRUE
;
1133 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1140 // Free data allocated by Fv->ReadSection ()
1142 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1143 // used the UEFI Boot Services AllocatePool() function
1145 gBS
->FreePool (AprioriFile
);
1149 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1150 // discovered SMM drivers that have been discovered but not dispatched.
1152 return SmmDispatcher ();
1156 Traverse the discovered list for any drivers that were discovered but not loaded
1157 because the dependency experessions evaluated to false.
1161 SmmDisplayDiscoveredNotDispatched (
1166 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1168 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1169 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1170 if (DriverEntry
->Dependent
) {
1171 DEBUG ((DEBUG_LOAD
, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));