4 Step #1 - When a FV protocol is added to the system every driver in the FV
5 is added to the mDiscoveredList. The 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.
30 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
31 This program and the accompanying materials are licensed and made available
32 under the terms and conditions of the BSD License which accompanies this
33 distribution. The full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
41 #include "PiSmmCore.h"
44 // SMM Dispatcher Data structures
46 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
49 LIST_ENTRY Link
; // mFvHandleList
54 // Function Prototypes
58 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
59 must add any driver with a before dependency on InsertedDriverEntry first.
60 You do this by recursively calling this routine. After all the Befores are
61 processed you can add InsertedDriverEntry to the mScheduledQueue.
62 Then you can add any driver with an After dependency on InsertedDriverEntry
63 by recursively calling this routine.
65 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
69 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
70 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
74 // The Driver List contains one copy of every driver that has been discovered.
75 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
77 LIST_ENTRY mDiscoveredList
= INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList
);
80 // Queue of drivers that are ready to dispatch. This queue is a subset of the
81 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
83 LIST_ENTRY mScheduledQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue
);
86 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
88 LIST_ENTRY mFvHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList
);
91 // Flag for the SMM Dispacher. TRUE if dispatcher is execuing.
93 BOOLEAN gDispatcherRunning
= FALSE
;
96 // Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched
98 BOOLEAN gRequestDispatch
= FALSE
;
101 // List of file types supported by dispatcher
103 EFI_FV_FILETYPE mSmmFileTypes
[] = {
105 EFI_FV_FILETYPE_COMBINED_SMM_DXE
107 // Note: DXE core will process the FV image file, so skip it in SMM core
108 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
113 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File
;
114 EFI_DEVICE_PATH_PROTOCOL End
;
115 } FV_FILEPATH_DEVICE_PATH
;
117 FV_FILEPATH_DEVICE_PATH mFvDevicePath
;
120 // DXE Architecture Protocols
122 EFI_SECURITY_ARCH_PROTOCOL
*mSecurity
= NULL
;
125 // The global variable is defined for Loading modules at fixed address feature to track the SMM code
126 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding
127 // memory page available or not.
129 GLOBAL_REMOVE_IF_UNREFERENCED UINT64
*mSmmCodeMemoryRangeUsageBitMap
=NULL
;
132 To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
133 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
134 The function is only invoked when load modules at fixed address feature is enabled.
136 @param ImageBase The base addres the image will be loaded at.
137 @param ImageSize The size of the image
139 @retval EFI_SUCCESS The memory range the image will be loaded in is available
140 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
143 CheckAndMarkFixLoadingMemoryUsageBitMap (
144 IN EFI_PHYSICAL_ADDRESS ImageBase
,
148 UINT32 SmmCodePageNumber
;
150 EFI_PHYSICAL_ADDRESS SmmCodeBase
;
151 UINTN BaseOffsetPageNumber
;
152 UINTN TopOffsetPageNumber
;
155 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
157 SmmCodePageNumber
= PcdGet32(PcdLoadFixAddressSmmCodePageNumber
);
158 SmmCodeSize
= EFI_PAGES_TO_SIZE (SmmCodePageNumber
);
159 SmmCodeBase
= gLoadModuleAtFixAddressSmramBase
;
162 // If the memory usage bit map is not initialized, do it. Every bit in the array
163 // indicate the status of the corresponding memory page, available or not
165 if (mSmmCodeMemoryRangeUsageBitMap
== NULL
) {
166 mSmmCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((SmmCodePageNumber
/ 64) + 1)*sizeof(UINT64
));
169 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
171 if (mSmmCodeMemoryRangeUsageBitMap
== NULL
) {
172 return EFI_NOT_FOUND
;
175 // see if the memory range for loading the image is in the SMM code range.
177 if (SmmCodeBase
+ SmmCodeSize
< ImageBase
+ ImageSize
|| SmmCodeBase
> ImageBase
) {
178 return EFI_NOT_FOUND
;
181 // Test if the memory is avalaible or not.
183 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- SmmCodeBase
));
184 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- SmmCodeBase
));
185 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
186 if ((mSmmCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
188 // This page is already used.
190 return EFI_NOT_FOUND
;
195 // Being here means the memory range is available. So mark the bits for the memory range
197 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
198 mSmmCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
203 Get the fixed loadding address from image header assigned by build tool. This function only be called
204 when Loading module at Fixed address feature enabled.
206 @param ImageContext Pointer to the image context structure that describes the PE/COFF
207 image that needs to be examined by this function.
208 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
209 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
213 GetPeCoffImageFixLoadingAssignedAddress(
214 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
217 UINTN SectionHeaderOffset
;
219 EFI_IMAGE_SECTION_HEADER SectionHeader
;
220 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
221 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
224 UINT16 NumberOfSections
;
225 UINT64 ValueInSectionHeader
;
227 FixLoaddingAddress
= 0;
228 Status
= EFI_NOT_FOUND
;
231 // Get PeHeader pointer
233 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
234 SectionHeaderOffset
= (UINTN
)(
235 ImageContext
->PeCoffHeaderOffset
+
237 sizeof (EFI_IMAGE_FILE_HEADER
) +
238 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
240 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
243 // Get base address from the first section header that doesn't point to code section.
245 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
247 // Read section header from file
249 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
250 Status
= ImageContext
->ImageRead (
251 ImageContext
->Handle
,
256 if (EFI_ERROR (Status
)) {
260 Status
= EFI_NOT_FOUND
;
262 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
264 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
265 // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
266 // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
267 // should not be Zero, or else, these 2 fileds should be set to Zero
269 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
270 if (ValueInSectionHeader
!= 0) {
272 // Found first section header that doesn't point to code section in which uild tool saves the
273 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
275 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressSmramBase
+ (INT64
)ValueInSectionHeader
);
277 // Check if the memory range is avaliable.
279 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoaddingAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
280 if (!EFI_ERROR(Status
)) {
282 // The assigned address is valid. Return the specified loadding address
284 ImageContext
->ImageAddress
= FixLoaddingAddress
;
289 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
291 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoaddingAddress
, Status
));
295 Loads an EFI image into SMRAM.
297 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
305 IN OUT EFI_SMM_DRIVER_ENTRY
*DriverEntry
308 UINT32 AuthenticationStatus
;
315 EFI_STATUS SecurityStatus
;
316 EFI_HANDLE DeviceHandle
;
317 EFI_PHYSICAL_ADDRESS DstBuffer
;
318 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
319 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
320 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
321 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
322 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
326 Fv
= DriverEntry
->Fv
;
327 NameGuid
= &DriverEntry
->FileName
;
328 FilePath
= DriverEntry
->FvFileDevicePath
;
330 OriginalFilePath
= FilePath
;
331 HandleFilePath
= FilePath
;
333 SecurityStatus
= EFI_SUCCESS
;
334 Status
= EFI_SUCCESS
;
335 AuthenticationStatus
= 0;
338 // Try to get the image device handle by checking the match protocol.
340 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
341 if (EFI_ERROR(Status
)) {
346 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
348 if (mSecurity
== NULL
) {
349 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
353 // Verify the Authentication Status through the Security Architectural Protocol
355 if ((mSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
356 SecurityStatus
= mSecurity
->FileAuthenticationState (
358 AuthenticationStatus
,
361 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
362 Status
= SecurityStatus
;
368 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
370 FilePath
= OriginalFilePath
;
371 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
372 if (!EFI_ERROR (Status
)) {
373 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
374 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
378 // Try reading PE32 section firstly
380 Status
= Fv
->ReadSection (
387 &AuthenticationStatus
390 if (EFI_ERROR (Status
)) {
392 // Try reading TE section secondly
396 Status
= Fv
->ReadSection (
403 &AuthenticationStatus
407 if (EFI_ERROR (Status
)) {
408 if (Buffer
!= NULL
) {
409 Status
= gBS
->FreePool (Buffer
);
415 // Initialize ImageContext
417 ImageContext
.Handle
= Buffer
;
418 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
421 // Get information about the image being loaded
423 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
424 if (EFI_ERROR (Status
)) {
425 if (Buffer
!= NULL
) {
426 Status
= gBS
->FreePool (Buffer
);
431 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE
432 // to hold the Smm driver code
434 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
436 // Get the fixed loading address assigned by Build tool
438 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
439 if (!EFI_ERROR (Status
)) {
441 // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range
442 // following statements is to bypass SmmFreePages
445 DstBuffer
= (UINTN
)gLoadModuleAtFixAddressSmramBase
;
447 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
449 // allocate the memory to load the SMM driver
451 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
452 DstBuffer
= (UINTN
)(-1);
454 Status
= SmmAllocatePages (
456 EfiRuntimeServicesCode
,
460 if (EFI_ERROR (Status
)) {
461 if (Buffer
!= NULL
) {
462 Status
= gBS
->FreePool (Buffer
);
466 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
469 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
470 DstBuffer
= (UINTN
)(-1);
472 Status
= SmmAllocatePages (
474 EfiRuntimeServicesCode
,
478 if (EFI_ERROR (Status
)) {
479 if (Buffer
!= NULL
) {
480 Status
= gBS
->FreePool (Buffer
);
485 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
488 // Align buffer on section boundry
490 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
491 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
494 // Load the image to our new buffer
496 Status
= PeCoffLoaderLoadImage (&ImageContext
);
497 if (EFI_ERROR (Status
)) {
498 if (Buffer
!= NULL
) {
499 Status
= gBS
->FreePool (Buffer
);
501 SmmFreePages (DstBuffer
, PageCount
);
506 // Relocate the image in our new buffer
508 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
509 if (EFI_ERROR (Status
)) {
510 if (Buffer
!= NULL
) {
511 Status
= gBS
->FreePool (Buffer
);
513 SmmFreePages (DstBuffer
, PageCount
);
518 // Flush the instruction cache so the image data are written before we execute it
520 InvalidateInstructionCacheRange ((VOID
*)(UINTN
) ImageContext
.ImageAddress
, (UINTN
) ImageContext
.ImageSize
);
523 // Save Image EntryPoint in DriverEntry
525 DriverEntry
->ImageEntryPoint
= ImageContext
.EntryPoint
;
526 DriverEntry
->ImageBuffer
= DstBuffer
;
527 DriverEntry
->NumberOfPage
= PageCount
;
530 // Allocate a Loaded Image Protocol in EfiBootServicesData
532 Status
= gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
), (VOID
**)&DriverEntry
->LoadedImage
);
533 if (EFI_ERROR (Status
)) {
534 if (Buffer
!= NULL
) {
535 Status
= gBS
->FreePool (Buffer
);
537 SmmFreePages (DstBuffer
, PageCount
);
542 // Fill in the remaining fields of the Loaded Image Protocol instance.
543 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
545 DriverEntry
->LoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
546 DriverEntry
->LoadedImage
->ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
547 DriverEntry
->LoadedImage
->SystemTable
= gST
;
548 DriverEntry
->LoadedImage
->DeviceHandle
= DeviceHandle
;
551 // Make an EfiBootServicesData buffer copy of FilePath
553 Status
= gBS
->AllocatePool (EfiBootServicesData
, GetDevicePathSize (FilePath
), (VOID
**)&DriverEntry
->LoadedImage
->FilePath
);
554 if (EFI_ERROR (Status
)) {
555 if (Buffer
!= NULL
) {
556 Status
= gBS
->FreePool (Buffer
);
558 SmmFreePages (DstBuffer
, PageCount
);
561 CopyMem (DriverEntry
->LoadedImage
->FilePath
, FilePath
, GetDevicePathSize (FilePath
));
563 DriverEntry
->LoadedImage
->ImageBase
= (VOID
*)(UINTN
)DriverEntry
->ImageBuffer
;
564 DriverEntry
->LoadedImage
->ImageSize
= ImageContext
.ImageSize
;
565 DriverEntry
->LoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
566 DriverEntry
->LoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
569 // Create a new image handle in the UEFI handle database for the SMM Driver
571 DriverEntry
->ImageHandle
= NULL
;
572 Status
= gBS
->InstallMultipleProtocolInterfaces (
573 &DriverEntry
->ImageHandle
,
574 &gEfiLoadedImageProtocolGuid
, DriverEntry
->LoadedImage
,
579 // Print the load address and the PDB file name if it is available
586 CHAR8 EfiFileName
[256];
589 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
590 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",
591 (VOID
*)(UINTN
) ImageContext
.ImageAddress
,
592 FUNCTION_ENTRY_POINT (ImageContext
.EntryPoint
)));
596 // Print Module Name by Pdb file path.
597 // Windows and Unix style file path are all trimmed correctly.
599 if (ImageContext
.PdbPointer
!= NULL
) {
601 for (Index
= 0; ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
602 if ((ImageContext
.PdbPointer
[Index
] == '\\') || (ImageContext
.PdbPointer
[Index
] == '/')) {
603 StartIndex
= Index
+ 1;
607 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
608 // The PDB file name is limited in the range of 0~255.
609 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
611 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
612 EfiFileName
[Index
] = ImageContext
.PdbPointer
[Index
+ StartIndex
];
613 if (EfiFileName
[Index
] == 0) {
614 EfiFileName
[Index
] = '.';
616 if (EfiFileName
[Index
] == '.') {
617 EfiFileName
[Index
+ 1] = 'e';
618 EfiFileName
[Index
+ 2] = 'f';
619 EfiFileName
[Index
+ 3] = 'i';
620 EfiFileName
[Index
+ 4] = 0;
625 if (Index
== sizeof (EfiFileName
) - 4) {
626 EfiFileName
[Index
] = 0;
628 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
630 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
635 // Free buffer allocated by Fv->ReadSection.
637 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
638 // used the UEFI Boot Services AllocatePool() function
640 Status
= gBS
->FreePool(Buffer
);
645 Preprocess dependency expression and update DriverEntry to reflect the
646 state of Before and After dependencies. If DriverEntry->Before
647 or DriverEntry->After is set it will never be cleared.
649 @param DriverEntry DriverEntry element to update .
651 @retval EFI_SUCCESS It always works.
656 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
661 Iterator
= DriverEntry
->Depex
;
662 DriverEntry
->Dependent
= TRUE
;
664 if (*Iterator
== EFI_DEP_BEFORE
) {
665 DriverEntry
->Before
= TRUE
;
666 } else if (*Iterator
== EFI_DEP_AFTER
) {
667 DriverEntry
->After
= TRUE
;
670 if (DriverEntry
->Before
|| DriverEntry
->After
) {
671 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
678 Read Depex and pre-process the Depex for Before and After. If Section Extraction
679 protocol returns an error via ReadSection defer the reading of the Depex.
681 @param DriverEntry Driver to work on.
683 @retval EFI_SUCCESS Depex read and preprossesed
684 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
685 and Depex reading needs to be retried.
686 @retval Error DEPEX not found.
690 SmmGetDepexSectionAndPreProccess (
691 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
695 EFI_SECTION_TYPE SectionType
;
696 UINT32 AuthenticationStatus
;
697 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
699 Fv
= DriverEntry
->Fv
;
702 // Grab Depex info, it will never be free'ed.
703 // (Note: DriverEntry->Depex is in DXE memory)
705 SectionType
= EFI_SECTION_SMM_DEPEX
;
706 Status
= Fv
->ReadSection (
708 &DriverEntry
->FileName
,
712 (UINTN
*)&DriverEntry
->DepexSize
,
713 &AuthenticationStatus
715 if (EFI_ERROR (Status
)) {
716 if (Status
== EFI_PROTOCOL_ERROR
) {
718 // The section extraction protocol failed so set protocol error flag
720 DriverEntry
->DepexProtocolError
= TRUE
;
723 // If no Depex assume depend on all architectural protocols
725 DriverEntry
->Depex
= NULL
;
726 DriverEntry
->Dependent
= TRUE
;
727 DriverEntry
->DepexProtocolError
= FALSE
;
731 // Set Before and After state information based on Depex
732 // Driver will be put in Dependent state
734 SmmPreProcessDepex (DriverEntry
);
735 DriverEntry
->DepexProtocolError
= FALSE
;
742 This is the main Dispatcher for SMM and it exits when there are no more
743 drivers to run. Drain the mScheduledQueue and load and start a PE
744 image for each driver. Search the mDiscoveredList to see if any driver can
745 be placed on the mScheduledQueue. If no drivers are placed on the
746 mScheduledQueue exit the function. On exit it is assumed the Bds()
747 will be called, and when the Bds() exits the Dispatcher will be called
750 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
751 @retval EFI_NOT_FOUND No SMM Drivers were dispatched
752 @retval EFI_SUCCESS One or more SMM Drivers were dispatched
761 EFI_STATUS ReturnStatus
;
763 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
766 if (!gRequestDispatch
) {
767 return EFI_NOT_FOUND
;
770 if (gDispatcherRunning
) {
772 // If the dispatcher is running don't let it be restarted.
774 return EFI_ALREADY_STARTED
;
777 gDispatcherRunning
= TRUE
;
779 ReturnStatus
= EFI_NOT_FOUND
;
782 // Drain the Scheduled Queue
784 while (!IsListEmpty (&mScheduledQueue
)) {
786 mScheduledQueue
.ForwardLink
,
787 EFI_SMM_DRIVER_ENTRY
,
789 EFI_SMM_DRIVER_ENTRY_SIGNATURE
793 // Load the SMM Driver image into memory. If the Driver was transitioned from
794 // Untrused to Scheduled it would have already been loaded so we may need to
795 // skip the LoadImage
797 if (DriverEntry
->ImageHandle
== NULL
) {
798 Status
= SmmLoadImage (DriverEntry
);
801 // Update the driver state to reflect that it's been loaded
803 if (EFI_ERROR (Status
)) {
805 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
806 // Take driver from Scheduled to Initialized.
808 DriverEntry
->Initialized
= TRUE
;
809 DriverEntry
->Scheduled
= FALSE
;
810 RemoveEntryList (&DriverEntry
->ScheduledLink
);
813 // If it's an error don't try the StartImage
819 DriverEntry
->Scheduled
= FALSE
;
820 DriverEntry
->Initialized
= TRUE
;
821 RemoveEntryList (&DriverEntry
->ScheduledLink
);
823 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
825 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_BEGIN
,
826 &DriverEntry
->ImageHandle
,
827 sizeof (DriverEntry
->ImageHandle
)
831 // For each SMM driver, pass NULL as ImageHandle
833 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
)(DriverEntry
->ImageHandle
, gST
);
834 if (EFI_ERROR(Status
)){
835 SmmFreePages(DriverEntry
->ImageBuffer
, DriverEntry
->NumberOfPage
);
838 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
840 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_END
,
841 &DriverEntry
->ImageHandle
,
842 sizeof (DriverEntry
->ImageHandle
)
845 ReturnStatus
= EFI_SUCCESS
;
849 // Search DriverList for items to place on Scheduled Queue
852 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
853 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
855 if (DriverEntry
->DepexProtocolError
){
857 // If Section Extraction Protocol did not let the Depex be read before retry the read
859 Status
= SmmGetDepexSectionAndPreProccess (DriverEntry
);
862 if (DriverEntry
->Dependent
) {
863 if (SmmIsSchedulable (DriverEntry
)) {
864 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
869 } while (ReadyToRun
);
872 // If there is no more SMM driver to dispatch, stop the dispatch request
874 gRequestDispatch
= FALSE
;
875 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
876 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
878 if (!DriverEntry
->Initialized
){
880 // We have SMM driver pending to dispatch
882 gRequestDispatch
= TRUE
;
887 gDispatcherRunning
= FALSE
;
893 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
894 must add any driver with a before dependency on InsertedDriverEntry first.
895 You do this by recursively calling this routine. After all the Befores are
896 processed you can add InsertedDriverEntry to the mScheduledQueue.
897 Then you can add any driver with an After dependency on InsertedDriverEntry
898 by recursively calling this routine.
900 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
904 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
905 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
909 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
912 // Process Before Dependency
914 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
915 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
916 if (DriverEntry
->Before
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
917 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
918 DEBUG ((DEBUG_DISPATCH
, " BEFORE FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
919 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
921 // Recursively process BEFORE
923 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
924 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
926 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
932 // Convert driver from Dependent to Scheduled state
935 InsertedDriverEntry
->Dependent
= FALSE
;
936 InsertedDriverEntry
->Scheduled
= TRUE
;
937 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
941 // Process After Dependency
943 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
944 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
945 if (DriverEntry
->After
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
946 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
947 DEBUG ((DEBUG_DISPATCH
, " AFTER FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
948 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
950 // Recursively process AFTER
952 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
953 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
955 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
962 Return TRUE if the Fv has been processed, FALSE if not.
964 @param FvHandle The handle of a FV that's being tested
966 @retval TRUE Fv protocol on FvHandle has been processed
967 @retval FALSE Fv protocol on FvHandle has not yet been
973 IN EFI_HANDLE FvHandle
977 KNOWN_HANDLE
*KnownHandle
;
979 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
980 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
981 if (KnownHandle
->Handle
== FvHandle
) {
989 Remember that Fv protocol on FvHandle has had it's drivers placed on the
990 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
991 never removed/freed from the mFvHandleList.
993 @param FvHandle The handle of a FV that has been processed
997 FvIsBeingProcesssed (
998 IN EFI_HANDLE FvHandle
1001 KNOWN_HANDLE
*KnownHandle
;
1003 KnownHandle
= AllocatePool (sizeof (KNOWN_HANDLE
));
1004 ASSERT (KnownHandle
!= NULL
);
1006 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
1007 KnownHandle
->Handle
= FvHandle
;
1008 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
1012 Convert FvHandle and DriverName into an EFI device path
1014 @param Fv Fv protocol, needed to read Depex info out of
1016 @param FvHandle Handle for Fv, needed in the
1017 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1018 read out of the FV at a later time.
1019 @param DriverName Name of driver to add to mDiscoveredList.
1021 @return Pointer to device path constructed from FvHandle and DriverName
1024 EFI_DEVICE_PATH_PROTOCOL
*
1026 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1027 IN EFI_HANDLE FvHandle
,
1028 IN EFI_GUID
*DriverName
1032 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1033 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
1036 // Remember the device path of the FV
1038 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1039 if (EFI_ERROR (Status
)) {
1040 FileNameDevicePath
= NULL
;
1043 // Build a device path to the file in the FV to pass into gBS->LoadImage
1045 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
1046 SetDevicePathEndNode (&mFvDevicePath
.End
);
1049 // Note: FileNameDevicePath is in DXE memory
1051 FileNameDevicePath
= AppendDevicePath (
1053 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
1056 return FileNameDevicePath
;
1060 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1061 and initilize any state variables. Read the Depex from the FV and store it
1062 in DriverEntry. Pre-process the Depex to set the Before and After state.
1063 The Discovered list is never free'ed and contains booleans that represent the
1064 other possible SMM driver states.
1066 @param Fv Fv protocol, needed to read Depex info out of
1068 @param FvHandle Handle for Fv, needed in the
1069 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1070 read out of the FV at a later time.
1071 @param DriverName Name of driver to add to mDiscoveredList.
1073 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1074 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1075 DriverName may be active in the system at any one
1080 SmmAddToDriverList (
1081 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1082 IN EFI_HANDLE FvHandle
,
1083 IN EFI_GUID
*DriverName
1086 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1089 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1092 DriverEntry
= AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY
));
1093 ASSERT (DriverEntry
!= NULL
);
1095 DriverEntry
->Signature
= EFI_SMM_DRIVER_ENTRY_SIGNATURE
;
1096 CopyGuid (&DriverEntry
->FileName
, DriverName
);
1097 DriverEntry
->FvHandle
= FvHandle
;
1098 DriverEntry
->Fv
= Fv
;
1099 DriverEntry
->FvFileDevicePath
= SmmFvToDevicePath (Fv
, FvHandle
, DriverName
);
1101 SmmGetDepexSectionAndPreProccess (DriverEntry
);
1103 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
1104 gRequestDispatch
= TRUE
;
1110 This function is the main entry point for an SMM handler dispatch
1111 or communicate-based callback.
1113 Event notification that is fired every time a FV dispatch protocol is added.
1114 More than one protocol may have been added when this event is fired, so you
1115 must loop on SmmLocateHandle () to see how many protocols were added and
1116 do the following to each FV:
1117 If the Fv has already been processed, skip it. If the Fv has not been
1118 processed then mark it as being processed, as we are about to process it.
1119 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1120 mDiscoveredList is never free'ed and contains variables that define
1121 the other states the SMM driver transitions to..
1122 While you are at it read the A Priori file into memory.
1123 Place drivers in the A Priori list onto the mScheduledQueue.
1125 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1126 @param Context Points to an optional handler context which was specified when the handler was registered.
1127 @param CommBuffer A pointer to a collection of data in memory that will
1128 be conveyed from a non-SMM environment into an SMM environment.
1129 @param CommBufferSize The size of the CommBuffer.
1136 SmmDriverDispatchHandler (
1137 IN EFI_HANDLE DispatchHandle
,
1138 IN CONST VOID
*Context
, OPTIONAL
1139 IN OUT VOID
*CommBuffer
, OPTIONAL
1140 IN OUT UINTN
*CommBufferSize OPTIONAL
1145 EFI_HANDLE
*HandleBuffer
;
1146 EFI_STATUS GetNextFileStatus
;
1147 EFI_STATUS SecurityStatus
;
1148 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1149 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1150 EFI_HANDLE FvHandle
;
1153 EFI_FV_FILETYPE Type
;
1154 EFI_FV_FILE_ATTRIBUTES Attributes
;
1156 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1157 EFI_GUID
*AprioriFile
;
1158 UINTN AprioriEntryCount
;
1161 UINT32 AuthenticationStatus
;
1164 HandleBuffer
= NULL
;
1165 Status
= gBS
->LocateHandleBuffer (
1167 &gEfiFirmwareVolume2ProtocolGuid
,
1172 if (EFI_ERROR (Status
)) {
1173 return EFI_NOT_FOUND
;
1176 for (Index
= 0; Index
< HandleCount
; Index
++) {
1177 FvHandle
= HandleBuffer
[Index
];
1179 if (FvHasBeenProcessed (FvHandle
)) {
1181 // This Fv has already been processed so lets skip it!
1187 // Since we are about to process this Fv mark it as processed.
1189 FvIsBeingProcesssed (FvHandle
);
1191 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1192 if (EFI_ERROR (Status
)) {
1194 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1200 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1201 if (EFI_ERROR (Status
)) {
1203 // The Firmware volume doesn't have device path, can't be dispatched.
1209 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1211 if (mSecurity
== NULL
) {
1212 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
1216 // Evaluate the authentication status of the Firmware Volume through
1217 // Security Architectural Protocol
1219 if (mSecurity
!= NULL
) {
1220 SecurityStatus
= mSecurity
->FileAuthenticationState (
1225 if (SecurityStatus
!= EFI_SUCCESS
) {
1227 // Security check failed. The firmware volume should not be used for any purpose.
1234 // Discover Drivers in FV and add them to the Discovered Driver List.
1235 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1237 for (Index
= 0; Index
< sizeof (mSmmFileTypes
)/sizeof (EFI_FV_FILETYPE
); Index
++) {
1239 // Initialize the search key
1243 Type
= mSmmFileTypes
[Index
];
1244 GetNextFileStatus
= Fv
->GetNextFile (
1252 if (!EFI_ERROR (GetNextFileStatus
)) {
1253 SmmAddToDriverList (Fv
, FvHandle
, &NameGuid
);
1255 } while (!EFI_ERROR (GetNextFileStatus
));
1259 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1260 // (Note: AprioriFile is in DXE memory)
1263 Status
= Fv
->ReadSection (
1268 (VOID
**)&AprioriFile
,
1270 &AuthenticationStatus
1272 if (!EFI_ERROR (Status
)) {
1273 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1275 AprioriEntryCount
= 0;
1279 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1280 // drivers not in the current FV and these must be skipped since the a priori list
1281 // is only valid for the FV that it resided in.
1284 for (Index
= 0; Index
< AprioriEntryCount
; Index
++) {
1285 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1286 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1287 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[Index
]) &&
1288 (FvHandle
== DriverEntry
->FvHandle
)) {
1289 DriverEntry
->Dependent
= FALSE
;
1290 DriverEntry
->Scheduled
= TRUE
;
1291 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1292 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1293 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1300 // Free data allocated by Fv->ReadSection ()
1302 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1303 // used the UEFI Boot Services AllocatePool() function
1305 gBS
->FreePool (AprioriFile
);
1309 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1310 // discovered SMM drivers that have been discovered but not dispatched.
1312 return SmmDispatcher ();
1316 Traverse the discovered list for any drivers that were discovered but not loaded
1317 because the dependency experessions evaluated to false.
1321 SmmDisplayDiscoveredNotDispatched (
1326 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1328 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1329 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1330 if (DriverEntry
->Dependent
) {
1331 DEBUG ((DEBUG_LOAD
, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));