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 - 2012, 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 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 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((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
452 DstBuffer
= (UINTN
)(-1);
454 Status
= SmmAllocatePages (
456 EfiRuntimeServicesCode
,
460 if (EFI_ERROR (Status
)) {
461 if (Buffer
!= NULL
) {
462 gBS
->FreePool (Buffer
);
466 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
469 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
470 DstBuffer
= (UINTN
)(-1);
472 Status
= SmmAllocatePages (
474 EfiRuntimeServicesCode
,
478 if (EFI_ERROR (Status
)) {
479 if (Buffer
!= NULL
) {
480 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 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 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 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 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.
748 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched
749 have been run and the SMM Entry Point has been
751 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point
753 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.
754 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
764 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
766 BOOLEAN PreviousSmmEntryPointRegistered
;
768 if (!gRequestDispatch
) {
769 return EFI_NOT_FOUND
;
772 if (gDispatcherRunning
) {
774 // If the dispatcher is running don't let it be restarted.
776 return EFI_ALREADY_STARTED
;
779 gDispatcherRunning
= TRUE
;
783 // Drain the Scheduled Queue
785 while (!IsListEmpty (&mScheduledQueue
)) {
787 mScheduledQueue
.ForwardLink
,
788 EFI_SMM_DRIVER_ENTRY
,
790 EFI_SMM_DRIVER_ENTRY_SIGNATURE
794 // Load the SMM Driver image into memory. If the Driver was transitioned from
795 // Untrused to Scheduled it would have already been loaded so we may need to
796 // skip the LoadImage
798 if (DriverEntry
->ImageHandle
== NULL
) {
799 Status
= SmmLoadImage (DriverEntry
);
802 // Update the driver state to reflect that it's been loaded
804 if (EFI_ERROR (Status
)) {
806 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
807 // Take driver from Scheduled to Initialized.
809 DriverEntry
->Initialized
= TRUE
;
810 DriverEntry
->Scheduled
= FALSE
;
811 RemoveEntryList (&DriverEntry
->ScheduledLink
);
814 // If it's an error don't try the StartImage
820 DriverEntry
->Scheduled
= FALSE
;
821 DriverEntry
->Initialized
= TRUE
;
822 RemoveEntryList (&DriverEntry
->ScheduledLink
);
824 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
826 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_BEGIN
,
827 &DriverEntry
->ImageHandle
,
828 sizeof (DriverEntry
->ImageHandle
)
832 // Cache state of SmmEntryPointRegistered before calling entry point
834 PreviousSmmEntryPointRegistered
= gSmmCorePrivate
->SmmEntryPointRegistered
;
837 // For each SMM driver, pass NULL as ImageHandle
839 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
)(DriverEntry
->ImageHandle
, gST
);
840 if (EFI_ERROR(Status
)){
841 SmmFreePages(DriverEntry
->ImageBuffer
, DriverEntry
->NumberOfPage
);
844 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
846 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_END
,
847 &DriverEntry
->ImageHandle
,
848 sizeof (DriverEntry
->ImageHandle
)
851 if (!PreviousSmmEntryPointRegistered
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
853 // Return immediately if the SMM Entry Point was registered by the SMM
854 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM
855 // Core Dispatcher. This is required so SMM Mode may be enabled as soon
856 // as all the dependent SMM Drivers for SMM Mode have been dispatched.
857 // Once the SMM Entry Point has been registered, then SMM Mode will be
860 gRequestDispatch
= TRUE
;
861 gDispatcherRunning
= FALSE
;
862 return EFI_NOT_READY
;
867 // Search DriverList for items to place on Scheduled Queue
870 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
871 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
873 if (DriverEntry
->DepexProtocolError
){
875 // If Section Extraction Protocol did not let the Depex be read before retry the read
877 Status
= SmmGetDepexSectionAndPreProccess (DriverEntry
);
880 if (DriverEntry
->Dependent
) {
881 if (SmmIsSchedulable (DriverEntry
)) {
882 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
887 } while (ReadyToRun
);
890 // If there is no more SMM driver to dispatch, stop the dispatch request
892 gRequestDispatch
= FALSE
;
893 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
894 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
896 if (!DriverEntry
->Initialized
){
898 // We have SMM driver pending to dispatch
900 gRequestDispatch
= TRUE
;
905 gDispatcherRunning
= FALSE
;
911 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
912 must add any driver with a before dependency on InsertedDriverEntry first.
913 You do this by recursively calling this routine. After all the Befores are
914 processed you can add InsertedDriverEntry to the mScheduledQueue.
915 Then you can add any driver with an After dependency on InsertedDriverEntry
916 by recursively calling this routine.
918 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
922 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
923 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
927 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
930 // Process Before Dependency
932 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
933 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
934 if (DriverEntry
->Before
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
935 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
936 DEBUG ((DEBUG_DISPATCH
, " BEFORE FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
937 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
939 // Recursively process BEFORE
941 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
942 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
944 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
950 // Convert driver from Dependent to Scheduled state
953 InsertedDriverEntry
->Dependent
= FALSE
;
954 InsertedDriverEntry
->Scheduled
= TRUE
;
955 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
959 // Process After Dependency
961 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
962 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
963 if (DriverEntry
->After
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
964 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
965 DEBUG ((DEBUG_DISPATCH
, " AFTER FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
966 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
968 // Recursively process AFTER
970 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
971 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
973 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
980 Return TRUE if the Fv has been processed, FALSE if not.
982 @param FvHandle The handle of a FV that's being tested
984 @retval TRUE Fv protocol on FvHandle has been processed
985 @retval FALSE Fv protocol on FvHandle has not yet been
991 IN EFI_HANDLE FvHandle
995 KNOWN_HANDLE
*KnownHandle
;
997 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
998 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
999 if (KnownHandle
->Handle
== FvHandle
) {
1007 Remember that Fv protocol on FvHandle has had it's drivers placed on the
1008 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
1009 never removed/freed from the mFvHandleList.
1011 @param FvHandle The handle of a FV that has been processed
1015 FvIsBeingProcesssed (
1016 IN EFI_HANDLE FvHandle
1019 KNOWN_HANDLE
*KnownHandle
;
1021 KnownHandle
= AllocatePool (sizeof (KNOWN_HANDLE
));
1022 ASSERT (KnownHandle
!= NULL
);
1024 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
1025 KnownHandle
->Handle
= FvHandle
;
1026 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
1030 Convert FvHandle and DriverName into an EFI device path
1032 @param Fv Fv protocol, needed to read Depex info out of
1034 @param FvHandle Handle for Fv, needed in the
1035 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1036 read out of the FV at a later time.
1037 @param DriverName Name of driver to add to mDiscoveredList.
1039 @return Pointer to device path constructed from FvHandle and DriverName
1042 EFI_DEVICE_PATH_PROTOCOL
*
1044 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1045 IN EFI_HANDLE FvHandle
,
1046 IN EFI_GUID
*DriverName
1050 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1051 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
1054 // Remember the device path of the FV
1056 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1057 if (EFI_ERROR (Status
)) {
1058 FileNameDevicePath
= NULL
;
1061 // Build a device path to the file in the FV to pass into gBS->LoadImage
1063 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
1064 SetDevicePathEndNode (&mFvDevicePath
.End
);
1067 // Note: FileNameDevicePath is in DXE memory
1069 FileNameDevicePath
= AppendDevicePath (
1071 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
1074 return FileNameDevicePath
;
1078 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1079 and initilize any state variables. Read the Depex from the FV and store it
1080 in DriverEntry. Pre-process the Depex to set the Before and After state.
1081 The Discovered list is never free'ed and contains booleans that represent the
1082 other possible SMM driver states.
1084 @param Fv Fv protocol, needed to read Depex info out of
1086 @param FvHandle Handle for Fv, needed in the
1087 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1088 read out of the FV at a later time.
1089 @param DriverName Name of driver to add to mDiscoveredList.
1091 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1092 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1093 DriverName may be active in the system at any one
1098 SmmAddToDriverList (
1099 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1100 IN EFI_HANDLE FvHandle
,
1101 IN EFI_GUID
*DriverName
1104 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1107 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1110 DriverEntry
= AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY
));
1111 ASSERT (DriverEntry
!= NULL
);
1113 DriverEntry
->Signature
= EFI_SMM_DRIVER_ENTRY_SIGNATURE
;
1114 CopyGuid (&DriverEntry
->FileName
, DriverName
);
1115 DriverEntry
->FvHandle
= FvHandle
;
1116 DriverEntry
->Fv
= Fv
;
1117 DriverEntry
->FvFileDevicePath
= SmmFvToDevicePath (Fv
, FvHandle
, DriverName
);
1119 SmmGetDepexSectionAndPreProccess (DriverEntry
);
1121 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
1122 gRequestDispatch
= TRUE
;
1128 This function is the main entry point for an SMM handler dispatch
1129 or communicate-based callback.
1131 Event notification that is fired every time a FV dispatch protocol is added.
1132 More than one protocol may have been added when this event is fired, so you
1133 must loop on SmmLocateHandle () to see how many protocols were added and
1134 do the following to each FV:
1135 If the Fv has already been processed, skip it. If the Fv has not been
1136 processed then mark it as being processed, as we are about to process it.
1137 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1138 mDiscoveredList is never free'ed and contains variables that define
1139 the other states the SMM driver transitions to..
1140 While you are at it read the A Priori file into memory.
1141 Place drivers in the A Priori list onto the mScheduledQueue.
1143 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1144 @param Context Points to an optional handler context which was specified when the handler was registered.
1145 @param CommBuffer A pointer to a collection of data in memory that will
1146 be conveyed from a non-SMM environment into an SMM environment.
1147 @param CommBufferSize The size of the CommBuffer.
1154 SmmDriverDispatchHandler (
1155 IN EFI_HANDLE DispatchHandle
,
1156 IN CONST VOID
*Context
, OPTIONAL
1157 IN OUT VOID
*CommBuffer
, OPTIONAL
1158 IN OUT UINTN
*CommBufferSize OPTIONAL
1163 EFI_HANDLE
*HandleBuffer
;
1164 EFI_STATUS GetNextFileStatus
;
1165 EFI_STATUS SecurityStatus
;
1166 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1167 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1168 EFI_HANDLE FvHandle
;
1171 EFI_FV_FILETYPE Type
;
1172 EFI_FV_FILE_ATTRIBUTES Attributes
;
1174 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1175 EFI_GUID
*AprioriFile
;
1176 UINTN AprioriEntryCount
;
1179 UINT32 AuthenticationStatus
;
1182 HandleBuffer
= NULL
;
1183 Status
= gBS
->LocateHandleBuffer (
1185 &gEfiFirmwareVolume2ProtocolGuid
,
1190 if (EFI_ERROR (Status
)) {
1191 return EFI_NOT_FOUND
;
1194 for (Index
= 0; Index
< HandleCount
; Index
++) {
1195 FvHandle
= HandleBuffer
[Index
];
1197 if (FvHasBeenProcessed (FvHandle
)) {
1199 // This Fv has already been processed so lets skip it!
1205 // Since we are about to process this Fv mark it as processed.
1207 FvIsBeingProcesssed (FvHandle
);
1209 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1210 if (EFI_ERROR (Status
)) {
1212 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1218 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1219 if (EFI_ERROR (Status
)) {
1221 // The Firmware volume doesn't have device path, can't be dispatched.
1227 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1229 if (mSecurity
== NULL
) {
1230 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
1234 // Evaluate the authentication status of the Firmware Volume through
1235 // Security Architectural Protocol
1237 if (mSecurity
!= NULL
) {
1238 SecurityStatus
= mSecurity
->FileAuthenticationState (
1243 if (SecurityStatus
!= EFI_SUCCESS
) {
1245 // Security check failed. The firmware volume should not be used for any purpose.
1252 // Discover Drivers in FV and add them to the Discovered Driver List.
1253 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1255 for (Index
= 0; Index
< sizeof (mSmmFileTypes
)/sizeof (EFI_FV_FILETYPE
); Index
++) {
1257 // Initialize the search key
1261 Type
= mSmmFileTypes
[Index
];
1262 GetNextFileStatus
= Fv
->GetNextFile (
1270 if (!EFI_ERROR (GetNextFileStatus
)) {
1271 SmmAddToDriverList (Fv
, FvHandle
, &NameGuid
);
1273 } while (!EFI_ERROR (GetNextFileStatus
));
1277 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1278 // (Note: AprioriFile is in DXE memory)
1281 Status
= Fv
->ReadSection (
1286 (VOID
**)&AprioriFile
,
1288 &AuthenticationStatus
1290 if (!EFI_ERROR (Status
)) {
1291 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1293 AprioriEntryCount
= 0;
1297 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1298 // drivers not in the current FV and these must be skipped since the a priori list
1299 // is only valid for the FV that it resided in.
1302 for (Index
= 0; Index
< AprioriEntryCount
; Index
++) {
1303 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1304 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1305 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[Index
]) &&
1306 (FvHandle
== DriverEntry
->FvHandle
)) {
1307 DriverEntry
->Dependent
= FALSE
;
1308 DriverEntry
->Scheduled
= TRUE
;
1309 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1310 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1311 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1318 // Free data allocated by Fv->ReadSection ()
1320 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1321 // used the UEFI Boot Services AllocatePool() function
1323 gBS
->FreePool (AprioriFile
);
1327 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1328 // discovered SMM drivers that have been discovered but not dispatched.
1330 Status
= SmmDispatcher ();
1333 // Check to see if CommBuffer and CommBufferSize are valid
1335 if (CommBuffer
!= NULL
&& CommBufferSize
!= NULL
) {
1336 if (*CommBufferSize
> 0) {
1337 if (Status
== EFI_NOT_READY
) {
1339 // If a the SMM Core Entry Point was just registered, then set flag to
1340 // request the SMM Dispatcher to be restarted.
1342 *(UINT8
*)CommBuffer
= COMM_BUFFER_SMM_DISPATCH_RESTART
;
1343 } else if (!EFI_ERROR (Status
)) {
1345 // Set the flag to show that the SMM Dispatcher executed without errors
1347 *(UINT8
*)CommBuffer
= COMM_BUFFER_SMM_DISPATCH_SUCCESS
;
1350 // Set the flag to show that the SMM Dispatcher encountered an error
1352 *(UINT8
*)CommBuffer
= COMM_BUFFER_SMM_DISPATCH_ERROR
;
1361 Traverse the discovered list for any drivers that were discovered but not loaded
1362 because the dependency experessions evaluated to false.
1366 SmmDisplayDiscoveredNotDispatched (
1371 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1373 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1374 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1375 if (DriverEntry
->Dependent
) {
1376 DEBUG ((DEBUG_LOAD
, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));