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 processes the After dependencies 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 Expression.
30 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
31 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
32 SPDX-License-Identifier: BSD-2-Clause-Patent
36 #include "PiSmmCore.h"
39 // SMM Dispatcher Data structures
41 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
44 LIST_ENTRY Link
; // mFvHandleList
49 // Function Prototypes
53 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
54 must add any driver with a before dependency on InsertedDriverEntry first.
55 You do this by recursively calling this routine. After all the Befores are
56 processed you can add InsertedDriverEntry to the mScheduledQueue.
57 Then you can add any driver with an After dependency on InsertedDriverEntry
58 by recursively calling this routine.
60 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
64 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
65 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
69 // The Driver List contains one copy of every driver that has been discovered.
70 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
72 LIST_ENTRY mDiscoveredList
= INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList
);
75 // Queue of drivers that are ready to dispatch. This queue is a subset of the
76 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
78 LIST_ENTRY mScheduledQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue
);
81 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
83 LIST_ENTRY mFvHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList
);
86 // Flag for the SMM Dispatcher. TRUE if dispatcher is executing.
88 BOOLEAN gDispatcherRunning
= FALSE
;
91 // Flag for the SMM Dispatcher. TRUE if there is one or more SMM drivers ready to be dispatched
93 BOOLEAN gRequestDispatch
= FALSE
;
96 // List of file types supported by dispatcher
98 EFI_FV_FILETYPE mSmmFileTypes
[] = {
100 EFI_FV_FILETYPE_COMBINED_SMM_DXE
,
101 EFI_FV_FILETYPE_SMM_CORE
,
103 // Note: DXE core will process the FV image file, so skip it in SMM core
104 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
109 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File
;
110 EFI_DEVICE_PATH_PROTOCOL End
;
111 } FV_FILEPATH_DEVICE_PATH
;
113 FV_FILEPATH_DEVICE_PATH mFvDevicePath
;
116 // DXE Architecture Protocols
118 EFI_SECURITY_ARCH_PROTOCOL
*mSecurity
= NULL
;
119 EFI_SECURITY2_ARCH_PROTOCOL
*mSecurity2
= NULL
;
122 // The global variable is defined for Loading modules at fixed address feature to track the SMM code
123 // memory range usage. It is a bit mapped array in which every bit indicates the corresponding
124 // memory page available or not.
126 GLOBAL_REMOVE_IF_UNREFERENCED UINT64
*mSmmCodeMemoryRangeUsageBitMap
=NULL
;
129 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
130 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.
131 The function is only invoked when load modules at fixed address feature is enabled.
133 @param ImageBase The base address the image will be loaded at.
134 @param ImageSize The size of the image
136 @retval EFI_SUCCESS The memory range the image will be loaded in is available
137 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
140 CheckAndMarkFixLoadingMemoryUsageBitMap (
141 IN EFI_PHYSICAL_ADDRESS ImageBase
,
145 UINT32 SmmCodePageNumber
;
147 EFI_PHYSICAL_ADDRESS SmmCodeBase
;
148 UINTN BaseOffsetPageNumber
;
149 UINTN TopOffsetPageNumber
;
152 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
154 SmmCodePageNumber
= PcdGet32(PcdLoadFixAddressSmmCodePageNumber
);
155 SmmCodeSize
= EFI_PAGES_TO_SIZE (SmmCodePageNumber
);
156 SmmCodeBase
= gLoadModuleAtFixAddressSmramBase
;
159 // If the memory usage bit map is not initialized, do it. Every bit in the array
160 // indicate the status of the corresponding memory page, available or not
162 if (mSmmCodeMemoryRangeUsageBitMap
== NULL
) {
163 mSmmCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((SmmCodePageNumber
/ 64) + 1)*sizeof(UINT64
));
166 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
168 if (mSmmCodeMemoryRangeUsageBitMap
== NULL
) {
169 return EFI_NOT_FOUND
;
172 // see if the memory range for loading the image is in the SMM code range.
174 if (SmmCodeBase
+ SmmCodeSize
< ImageBase
+ ImageSize
|| SmmCodeBase
> ImageBase
) {
175 return EFI_NOT_FOUND
;
178 // Test if the memory is available or not.
180 BaseOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- SmmCodeBase
));
181 TopOffsetPageNumber
= EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- SmmCodeBase
));
182 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
183 if ((mSmmCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
185 // This page is already used.
187 return EFI_NOT_FOUND
;
192 // Being here means the memory range is available. So mark the bits for the memory range
194 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
195 mSmmCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
200 Get the fixed loading address from image header assigned by build tool. This function only be called
201 when Loading module at Fixed address feature enabled.
203 @param ImageContext Pointer to the image context structure that describes the PE/COFF
204 image that needs to be examined by this function.
205 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
206 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
210 GetPeCoffImageFixLoadingAssignedAddress(
211 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
214 UINTN SectionHeaderOffset
;
216 EFI_IMAGE_SECTION_HEADER SectionHeader
;
217 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
218 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
221 UINT16 NumberOfSections
;
222 UINT64 ValueInSectionHeader
;
224 FixLoadingAddress
= 0;
225 Status
= EFI_NOT_FOUND
;
228 // Get PeHeader pointer
230 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
231 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
233 sizeof (EFI_IMAGE_FILE_HEADER
) +
234 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
235 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
238 // Get base address from the first section header that doesn't point to code section.
240 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
242 // Read section header from file
244 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
245 Status
= ImageContext
->ImageRead (
246 ImageContext
->Handle
,
251 if (EFI_ERROR (Status
)) {
255 Status
= EFI_NOT_FOUND
;
257 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
259 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
260 // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
261 // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
262 // should not be Zero, or else, these 2 fields should be set to Zero
264 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
265 if (ValueInSectionHeader
!= 0) {
267 // Found first section header that doesn't point to code section in which build tool saves the
268 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
270 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressSmramBase
+ (INT64
)ValueInSectionHeader
);
272 // Check if the memory range is available.
274 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
275 if (!EFI_ERROR(Status
)) {
277 // The assigned address is valid. Return the specified loading address
279 ImageContext
->ImageAddress
= FixLoadingAddress
;
284 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
286 DEBUG ((DEBUG_INFO
|DEBUG_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress
, Status
));
290 Loads an EFI image into SMRAM.
292 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
300 IN OUT EFI_SMM_DRIVER_ENTRY
*DriverEntry
303 UINT32 AuthenticationStatus
;
310 EFI_STATUS SecurityStatus
;
311 EFI_HANDLE DeviceHandle
;
312 EFI_PHYSICAL_ADDRESS DstBuffer
;
313 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
314 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
315 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
316 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
317 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
319 PERF_LOAD_IMAGE_BEGIN (DriverEntry
->ImageHandle
);
323 Fv
= DriverEntry
->Fv
;
324 NameGuid
= &DriverEntry
->FileName
;
325 FilePath
= DriverEntry
->FvFileDevicePath
;
327 OriginalFilePath
= FilePath
;
328 HandleFilePath
= FilePath
;
330 SecurityStatus
= EFI_SUCCESS
;
331 Status
= EFI_SUCCESS
;
332 AuthenticationStatus
= 0;
335 // Try to get the image device handle by checking the match protocol.
337 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
338 if (EFI_ERROR(Status
)) {
343 // If the Security2 and Security Architectural Protocol has not been located yet, then attempt to locate it
345 if (mSecurity2
== NULL
) {
346 gBS
->LocateProtocol (&gEfiSecurity2ArchProtocolGuid
, NULL
, (VOID
**)&mSecurity2
);
348 if (mSecurity
== NULL
) {
349 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
352 // When Security2 is installed, Security Architectural Protocol must be published.
354 ASSERT (mSecurity2
== NULL
|| mSecurity
!= NULL
);
357 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
359 FilePath
= OriginalFilePath
;
360 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
361 if (!EFI_ERROR (Status
)) {
362 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
363 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
367 // Try reading PE32 section firstly
369 Status
= Fv
->ReadSection (
376 &AuthenticationStatus
379 if (EFI_ERROR (Status
)) {
381 // Try reading TE section secondly
385 Status
= Fv
->ReadSection (
392 &AuthenticationStatus
396 if (EFI_ERROR (Status
)) {
397 if (Buffer
!= NULL
) {
398 gBS
->FreePool (Buffer
);
404 // Verify File Authentication through the Security2 Architectural Protocol
406 if (mSecurity2
!= NULL
) {
407 SecurityStatus
= mSecurity2
->FileAuthentication (
417 // Verify the Authentication Status through the Security Architectural Protocol
418 // Only on images that have been read using Firmware Volume protocol.
419 // All SMM images are from FV protocol.
421 if (!EFI_ERROR (SecurityStatus
) && (mSecurity
!= NULL
)) {
422 SecurityStatus
= mSecurity
->FileAuthenticationState (
424 AuthenticationStatus
,
429 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
430 Status
= SecurityStatus
;
435 // Initialize ImageContext
437 ImageContext
.Handle
= Buffer
;
438 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
441 // Get information about the image being loaded
443 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
444 if (EFI_ERROR (Status
)) {
445 if (Buffer
!= NULL
) {
446 gBS
->FreePool (Buffer
);
451 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE
452 // to hold the Smm driver code
454 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
456 // Get the fixed loading address assigned by Build tool
458 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
459 if (!EFI_ERROR (Status
)) {
461 // Since the memory range to load Smm core already been cut out, so no need to allocate and free this range
462 // following statements is to bypass SmmFreePages
465 DstBuffer
= (UINTN
)gLoadModuleAtFixAddressSmramBase
;
467 DEBUG ((DEBUG_INFO
|DEBUG_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
469 // allocate the memory to load the SMM driver
471 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
472 DstBuffer
= (UINTN
)(-1);
474 Status
= SmmAllocatePages (
476 EfiRuntimeServicesCode
,
480 if (EFI_ERROR (Status
)) {
481 if (Buffer
!= NULL
) {
482 gBS
->FreePool (Buffer
);
486 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
489 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
490 DstBuffer
= (UINTN
)(-1);
492 Status
= SmmAllocatePages (
494 EfiRuntimeServicesCode
,
498 if (EFI_ERROR (Status
)) {
499 if (Buffer
!= NULL
) {
500 gBS
->FreePool (Buffer
);
505 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
508 // Align buffer on section boundary
510 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
511 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)ImageContext
.SectionAlignment
- 1);
514 // Load the image to our new buffer
516 Status
= PeCoffLoaderLoadImage (&ImageContext
);
517 if (EFI_ERROR (Status
)) {
518 if (Buffer
!= NULL
) {
519 gBS
->FreePool (Buffer
);
521 SmmFreePages (DstBuffer
, PageCount
);
526 // Relocate the image in our new buffer
528 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
529 if (EFI_ERROR (Status
)) {
530 if (Buffer
!= NULL
) {
531 gBS
->FreePool (Buffer
);
533 SmmFreePages (DstBuffer
, PageCount
);
538 // Flush the instruction cache so the image data are written before we execute it
540 InvalidateInstructionCacheRange ((VOID
*)(UINTN
) ImageContext
.ImageAddress
, (UINTN
) ImageContext
.ImageSize
);
543 // Save Image EntryPoint in DriverEntry
545 DriverEntry
->ImageEntryPoint
= ImageContext
.EntryPoint
;
546 DriverEntry
->ImageBuffer
= DstBuffer
;
547 DriverEntry
->NumberOfPage
= PageCount
;
550 // Allocate a Loaded Image Protocol in EfiBootServicesData
552 Status
= gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
), (VOID
**)&DriverEntry
->LoadedImage
);
553 if (EFI_ERROR (Status
)) {
554 if (Buffer
!= NULL
) {
555 gBS
->FreePool (Buffer
);
557 SmmFreePages (DstBuffer
, PageCount
);
561 ZeroMem (DriverEntry
->LoadedImage
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
));
563 // Fill in the remaining fields of the Loaded Image Protocol instance.
564 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
566 DriverEntry
->LoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
567 DriverEntry
->LoadedImage
->ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
568 DriverEntry
->LoadedImage
->SystemTable
= gST
;
569 DriverEntry
->LoadedImage
->DeviceHandle
= DeviceHandle
;
571 DriverEntry
->SmmLoadedImage
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
572 DriverEntry
->SmmLoadedImage
.ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
573 DriverEntry
->SmmLoadedImage
.SystemTable
= gST
;
574 DriverEntry
->SmmLoadedImage
.DeviceHandle
= DeviceHandle
;
577 // Make an EfiBootServicesData buffer copy of FilePath
579 Status
= gBS
->AllocatePool (EfiBootServicesData
, GetDevicePathSize (FilePath
), (VOID
**)&DriverEntry
->LoadedImage
->FilePath
);
580 if (EFI_ERROR (Status
)) {
581 if (Buffer
!= NULL
) {
582 gBS
->FreePool (Buffer
);
584 SmmFreePages (DstBuffer
, PageCount
);
587 CopyMem (DriverEntry
->LoadedImage
->FilePath
, FilePath
, GetDevicePathSize (FilePath
));
589 DriverEntry
->LoadedImage
->ImageBase
= (VOID
*)(UINTN
) ImageContext
.ImageAddress
;
590 DriverEntry
->LoadedImage
->ImageSize
= ImageContext
.ImageSize
;
591 DriverEntry
->LoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
592 DriverEntry
->LoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
595 // Make a buffer copy of FilePath
597 Status
= SmmAllocatePool (EfiRuntimeServicesData
, GetDevicePathSize(FilePath
), (VOID
**)&DriverEntry
->SmmLoadedImage
.FilePath
);
598 if (EFI_ERROR (Status
)) {
599 if (Buffer
!= NULL
) {
600 gBS
->FreePool (Buffer
);
602 gBS
->FreePool (DriverEntry
->LoadedImage
->FilePath
);
603 SmmFreePages (DstBuffer
, PageCount
);
606 CopyMem (DriverEntry
->SmmLoadedImage
.FilePath
, FilePath
, GetDevicePathSize(FilePath
));
608 DriverEntry
->SmmLoadedImage
.ImageBase
= (VOID
*)(UINTN
) ImageContext
.ImageAddress
;
609 DriverEntry
->SmmLoadedImage
.ImageSize
= ImageContext
.ImageSize
;
610 DriverEntry
->SmmLoadedImage
.ImageCodeType
= EfiRuntimeServicesCode
;
611 DriverEntry
->SmmLoadedImage
.ImageDataType
= EfiRuntimeServicesData
;
614 // Create a new image handle in the UEFI handle database for the SMM Driver
616 DriverEntry
->ImageHandle
= NULL
;
617 Status
= gBS
->InstallMultipleProtocolInterfaces (
618 &DriverEntry
->ImageHandle
,
619 &gEfiLoadedImageProtocolGuid
, DriverEntry
->LoadedImage
,
624 // Create a new image handle in the SMM handle database for the SMM Driver
626 DriverEntry
->SmmImageHandle
= NULL
;
627 Status
= SmmInstallProtocolInterface (
628 &DriverEntry
->SmmImageHandle
,
629 &gEfiLoadedImageProtocolGuid
,
630 EFI_NATIVE_INTERFACE
,
631 &DriverEntry
->SmmLoadedImage
634 PERF_LOAD_IMAGE_END (DriverEntry
->ImageHandle
);
637 // Print the load address and the PDB file name if it is available
644 CHAR8 EfiFileName
[256];
647 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
648 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",
649 (VOID
*)(UINTN
) ImageContext
.ImageAddress
,
650 FUNCTION_ENTRY_POINT (ImageContext
.EntryPoint
)));
654 // Print Module Name by Pdb file path.
655 // Windows and Unix style file path are all trimmed correctly.
657 if (ImageContext
.PdbPointer
!= NULL
) {
659 for (Index
= 0; ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
660 if ((ImageContext
.PdbPointer
[Index
] == '\\') || (ImageContext
.PdbPointer
[Index
] == '/')) {
661 StartIndex
= Index
+ 1;
665 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
666 // The PDB file name is limited in the range of 0~255.
667 // If the length is bigger than 255, trim the redundant characters to avoid overflow in array boundary.
669 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
670 EfiFileName
[Index
] = ImageContext
.PdbPointer
[Index
+ StartIndex
];
671 if (EfiFileName
[Index
] == 0) {
672 EfiFileName
[Index
] = '.';
674 if (EfiFileName
[Index
] == '.') {
675 EfiFileName
[Index
+ 1] = 'e';
676 EfiFileName
[Index
+ 2] = 'f';
677 EfiFileName
[Index
+ 3] = 'i';
678 EfiFileName
[Index
+ 4] = 0;
683 if (Index
== sizeof (EfiFileName
) - 4) {
684 EfiFileName
[Index
] = 0;
686 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
688 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
693 // Free buffer allocated by Fv->ReadSection.
695 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
696 // used the UEFI Boot Services AllocatePool() function
698 Status
= gBS
->FreePool(Buffer
);
699 if (!EFI_ERROR (Status
) && EFI_ERROR (SecurityStatus
)) {
700 Status
= SecurityStatus
;
706 Preprocess dependency expression and update DriverEntry to reflect the
707 state of Before and After dependencies. If DriverEntry->Before
708 or DriverEntry->After is set it will never be cleared.
710 @param DriverEntry DriverEntry element to update .
712 @retval EFI_SUCCESS It always works.
717 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
722 Iterator
= DriverEntry
->Depex
;
723 DriverEntry
->Dependent
= TRUE
;
725 if (*Iterator
== EFI_DEP_BEFORE
) {
726 DriverEntry
->Before
= TRUE
;
727 } else if (*Iterator
== EFI_DEP_AFTER
) {
728 DriverEntry
->After
= TRUE
;
731 if (DriverEntry
->Before
|| DriverEntry
->After
) {
732 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
739 Read Depex and pre-process the Depex for Before and After. If Section Extraction
740 protocol returns an error via ReadSection defer the reading of the Depex.
742 @param DriverEntry Driver to work on.
744 @retval EFI_SUCCESS Depex read and preprocessed
745 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
746 and Depex reading needs to be retried.
747 @retval Error DEPEX not found.
751 SmmGetDepexSectionAndPreProccess (
752 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
756 EFI_SECTION_TYPE SectionType
;
757 UINT32 AuthenticationStatus
;
758 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
760 Fv
= DriverEntry
->Fv
;
763 // Grab Depex info, it will never be free'ed.
764 // (Note: DriverEntry->Depex is in DXE memory)
766 SectionType
= EFI_SECTION_SMM_DEPEX
;
767 Status
= Fv
->ReadSection (
769 &DriverEntry
->FileName
,
773 (UINTN
*)&DriverEntry
->DepexSize
,
774 &AuthenticationStatus
776 if (EFI_ERROR (Status
)) {
777 if (Status
== EFI_PROTOCOL_ERROR
) {
779 // The section extraction protocol failed so set protocol error flag
781 DriverEntry
->DepexProtocolError
= TRUE
;
784 // If no Depex assume depend on all architectural protocols
786 DriverEntry
->Depex
= NULL
;
787 DriverEntry
->Dependent
= TRUE
;
788 DriverEntry
->DepexProtocolError
= FALSE
;
792 // Set Before and After state information based on Depex
793 // Driver will be put in Dependent state
795 SmmPreProcessDepex (DriverEntry
);
796 DriverEntry
->DepexProtocolError
= FALSE
;
803 This is the main Dispatcher for SMM and it exits when there are no more
804 drivers to run. Drain the mScheduledQueue and load and start a PE
805 image for each driver. Search the mDiscoveredList to see if any driver can
806 be placed on the mScheduledQueue. If no drivers are placed on the
807 mScheduledQueue exit the function.
809 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched
810 have been run and the SMM Entry Point has been
812 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point
814 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.
815 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
825 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
827 BOOLEAN PreviousSmmEntryPointRegistered
;
829 if (!gRequestDispatch
) {
830 return EFI_NOT_FOUND
;
833 if (gDispatcherRunning
) {
835 // If the dispatcher is running don't let it be restarted.
837 return EFI_ALREADY_STARTED
;
840 gDispatcherRunning
= TRUE
;
844 // Drain the Scheduled Queue
846 while (!IsListEmpty (&mScheduledQueue
)) {
848 mScheduledQueue
.ForwardLink
,
849 EFI_SMM_DRIVER_ENTRY
,
851 EFI_SMM_DRIVER_ENTRY_SIGNATURE
855 // Load the SMM Driver image into memory. If the Driver was transitioned from
856 // Untrused to Scheduled it would have already been loaded so we may need to
857 // skip the LoadImage
859 if (DriverEntry
->ImageHandle
== NULL
) {
860 Status
= SmmLoadImage (DriverEntry
);
863 // Update the driver state to reflect that it's been loaded
865 if (EFI_ERROR (Status
)) {
867 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
868 // Take driver from Scheduled to Initialized.
870 DriverEntry
->Initialized
= TRUE
;
871 DriverEntry
->Scheduled
= FALSE
;
872 RemoveEntryList (&DriverEntry
->ScheduledLink
);
875 // If it's an error don't try the StartImage
881 DriverEntry
->Scheduled
= FALSE
;
882 DriverEntry
->Initialized
= TRUE
;
883 RemoveEntryList (&DriverEntry
->ScheduledLink
);
885 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
887 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_BEGIN
,
888 &DriverEntry
->ImageHandle
,
889 sizeof (DriverEntry
->ImageHandle
)
893 // Cache state of SmmEntryPointRegistered before calling entry point
895 PreviousSmmEntryPointRegistered
= gSmmCorePrivate
->SmmEntryPointRegistered
;
898 // For each SMM driver, pass NULL as ImageHandle
900 RegisterSmramProfileImage (DriverEntry
, TRUE
);
901 PERF_START_IMAGE_BEGIN (DriverEntry
->ImageHandle
);
902 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
)(DriverEntry
->ImageHandle
, gST
);
903 PERF_START_IMAGE_END (DriverEntry
->ImageHandle
);
904 if (EFI_ERROR(Status
)){
907 "Error: SMM image at %11p start failed: %r\n",
908 DriverEntry
->SmmLoadedImage
.ImageBase
,
911 UnregisterSmramProfileImage (DriverEntry
, TRUE
);
912 SmmFreePages(DriverEntry
->ImageBuffer
, DriverEntry
->NumberOfPage
);
914 // Uninstall LoadedImage
916 Status
= gBS
->UninstallProtocolInterface (
917 DriverEntry
->ImageHandle
,
918 &gEfiLoadedImageProtocolGuid
,
919 DriverEntry
->LoadedImage
921 if (!EFI_ERROR (Status
)) {
922 if (DriverEntry
->LoadedImage
->FilePath
!= NULL
) {
923 gBS
->FreePool (DriverEntry
->LoadedImage
->FilePath
);
925 gBS
->FreePool (DriverEntry
->LoadedImage
);
927 Status
= SmmUninstallProtocolInterface (
928 DriverEntry
->SmmImageHandle
,
929 &gEfiLoadedImageProtocolGuid
,
930 &DriverEntry
->SmmLoadedImage
932 if (!EFI_ERROR(Status
)) {
933 if (DriverEntry
->SmmLoadedImage
.FilePath
!= NULL
) {
934 SmmFreePool (DriverEntry
->SmmLoadedImage
.FilePath
);
939 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
941 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_END
,
942 &DriverEntry
->ImageHandle
,
943 sizeof (DriverEntry
->ImageHandle
)
946 if (!PreviousSmmEntryPointRegistered
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
948 // Return immediately if the SMM Entry Point was registered by the SMM
949 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM
950 // Core Dispatcher. This is required so SMM Mode may be enabled as soon
951 // as all the dependent SMM Drivers for SMM Mode have been dispatched.
952 // Once the SMM Entry Point has been registered, then SMM Mode will be
955 gRequestDispatch
= TRUE
;
956 gDispatcherRunning
= FALSE
;
957 return EFI_NOT_READY
;
962 // Search DriverList for items to place on Scheduled Queue
965 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
966 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
968 if (DriverEntry
->DepexProtocolError
){
970 // If Section Extraction Protocol did not let the Depex be read before retry the read
972 Status
= SmmGetDepexSectionAndPreProccess (DriverEntry
);
975 if (DriverEntry
->Dependent
) {
976 if (SmmIsSchedulable (DriverEntry
)) {
977 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
982 } while (ReadyToRun
);
985 // If there is no more SMM driver to dispatch, stop the dispatch request
987 gRequestDispatch
= FALSE
;
988 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
989 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
991 if (!DriverEntry
->Initialized
){
993 // We have SMM driver pending to dispatch
995 gRequestDispatch
= TRUE
;
1000 gDispatcherRunning
= FALSE
;
1006 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
1007 must add any driver with a before dependency on InsertedDriverEntry first.
1008 You do this by recursively calling this routine. After all the Befores are
1009 processed you can add InsertedDriverEntry to the mScheduledQueue.
1010 Then you can add any driver with an After dependency on InsertedDriverEntry
1011 by recursively calling this routine.
1013 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
1017 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
1018 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
1022 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1025 // Process Before Dependency
1027 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1028 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1029 if (DriverEntry
->Before
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
1030 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1031 DEBUG ((DEBUG_DISPATCH
, " BEFORE FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
1032 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
1034 // Recursively process BEFORE
1036 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
1037 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
1039 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
1045 // Convert driver from Dependent to Scheduled state
1048 InsertedDriverEntry
->Dependent
= FALSE
;
1049 InsertedDriverEntry
->Scheduled
= TRUE
;
1050 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
1054 // Process After Dependency
1056 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1057 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1058 if (DriverEntry
->After
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
1059 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1060 DEBUG ((DEBUG_DISPATCH
, " AFTER FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
1061 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
1063 // Recursively process AFTER
1065 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
1066 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
1068 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
1075 Return TRUE if the Fv has been processed, FALSE if not.
1077 @param FvHandle The handle of a FV that's being tested
1079 @retval TRUE Fv protocol on FvHandle has been processed
1080 @retval FALSE Fv protocol on FvHandle has not yet been
1085 FvHasBeenProcessed (
1086 IN EFI_HANDLE FvHandle
1090 KNOWN_HANDLE
*KnownHandle
;
1092 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
1093 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
1094 if (KnownHandle
->Handle
== FvHandle
) {
1102 Remember that Fv protocol on FvHandle has had its drivers placed on the
1103 mDiscoveredList. This function adds entries on the mFvHandleList. Items are
1104 never removed/freed from the mFvHandleList.
1106 @param FvHandle The handle of a FV that has been processed
1110 FvIsBeingProcessed (
1111 IN EFI_HANDLE FvHandle
1114 KNOWN_HANDLE
*KnownHandle
;
1116 KnownHandle
= AllocatePool (sizeof (KNOWN_HANDLE
));
1117 ASSERT (KnownHandle
!= NULL
);
1119 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
1120 KnownHandle
->Handle
= FvHandle
;
1121 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
1125 Convert FvHandle and DriverName into an EFI device path
1127 @param Fv Fv protocol, needed to read Depex info out of
1129 @param FvHandle Handle for Fv, needed in the
1130 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1131 read out of the FV at a later time.
1132 @param DriverName Name of driver to add to mDiscoveredList.
1134 @return Pointer to device path constructed from FvHandle and DriverName
1137 EFI_DEVICE_PATH_PROTOCOL
*
1139 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1140 IN EFI_HANDLE FvHandle
,
1141 IN EFI_GUID
*DriverName
1145 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1146 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
1149 // Remember the device path of the FV
1151 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1152 if (EFI_ERROR (Status
)) {
1153 FileNameDevicePath
= NULL
;
1156 // Build a device path to the file in the FV to pass into gBS->LoadImage
1158 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
1159 SetDevicePathEndNode (&mFvDevicePath
.End
);
1162 // Note: FileNameDevicePath is in DXE memory
1164 FileNameDevicePath
= AppendDevicePath (
1166 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
1169 return FileNameDevicePath
;
1173 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1174 and initialize any state variables. Read the Depex from the FV and store it
1175 in DriverEntry. Pre-process the Depex to set the Before and After state.
1176 The Discovered list is never free'ed and contains booleans that represent the
1177 other possible SMM driver states.
1179 @param Fv Fv protocol, needed to read Depex info out of
1181 @param FvHandle Handle for Fv, needed in the
1182 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1183 read out of the FV at a later time.
1184 @param DriverName Name of driver to add to mDiscoveredList.
1186 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1187 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1188 DriverName may be active in the system at any one
1193 SmmAddToDriverList (
1194 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1195 IN EFI_HANDLE FvHandle
,
1196 IN EFI_GUID
*DriverName
1199 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1202 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1205 DriverEntry
= AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY
));
1206 ASSERT (DriverEntry
!= NULL
);
1208 DriverEntry
->Signature
= EFI_SMM_DRIVER_ENTRY_SIGNATURE
;
1209 CopyGuid (&DriverEntry
->FileName
, DriverName
);
1210 DriverEntry
->FvHandle
= FvHandle
;
1211 DriverEntry
->Fv
= Fv
;
1212 DriverEntry
->FvFileDevicePath
= SmmFvToDevicePath (Fv
, FvHandle
, DriverName
);
1214 SmmGetDepexSectionAndPreProccess (DriverEntry
);
1216 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
1217 gRequestDispatch
= TRUE
;
1223 This function is the main entry point for an SMM handler dispatch
1224 or communicate-based callback.
1226 Event notification that is fired every time a FV dispatch protocol is added.
1227 More than one protocol may have been added when this event is fired, so you
1228 must loop on SmmLocateHandle () to see how many protocols were added and
1229 do the following to each FV:
1230 If the Fv has already been processed, skip it. If the Fv has not been
1231 processed then mark it as being processed, as we are about to process it.
1232 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1233 mDiscoveredList is never free'ed and contains variables that define
1234 the other states the SMM driver transitions to..
1235 While you are at it read the A Priori file into memory.
1236 Place drivers in the A Priori list onto the mScheduledQueue.
1238 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1239 @param Context Points to an optional handler context which was specified when the handler was registered.
1240 @param CommBuffer A pointer to a collection of data in memory that will
1241 be conveyed from a non-SMM environment into an SMM environment.
1242 @param CommBufferSize The size of the CommBuffer.
1249 SmmDriverDispatchHandler (
1250 IN EFI_HANDLE DispatchHandle
,
1251 IN CONST VOID
*Context OPTIONAL
,
1252 IN OUT VOID
*CommBuffer OPTIONAL
,
1253 IN OUT UINTN
*CommBufferSize OPTIONAL
1258 EFI_HANDLE
*HandleBuffer
;
1259 EFI_STATUS GetNextFileStatus
;
1260 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1261 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1262 EFI_HANDLE FvHandle
;
1265 EFI_FV_FILETYPE Type
;
1266 EFI_FV_FILE_ATTRIBUTES Attributes
;
1268 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1269 EFI_GUID
*AprioriFile
;
1270 UINTN AprioriEntryCount
;
1275 UINT32 AuthenticationStatus
;
1278 HandleBuffer
= NULL
;
1279 Status
= gBS
->LocateHandleBuffer (
1281 &gEfiFirmwareVolume2ProtocolGuid
,
1286 if (EFI_ERROR (Status
)) {
1287 return EFI_NOT_FOUND
;
1290 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
1291 FvHandle
= HandleBuffer
[HandleIndex
];
1293 if (FvHasBeenProcessed (FvHandle
)) {
1295 // This Fv has already been processed so lets skip it!
1301 // Since we are about to process this Fv mark it as processed.
1303 FvIsBeingProcessed (FvHandle
);
1305 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1306 if (EFI_ERROR (Status
)) {
1308 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1314 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1315 if (EFI_ERROR (Status
)) {
1317 // The Firmware volume doesn't have device path, can't be dispatched.
1323 // Discover Drivers in FV and add them to the Discovered Driver List.
1324 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1325 // EFI_FV_FILETYPE_SMM_CORE is processed to produce a Loaded Image protocol for the core
1327 for (SmmTypeIndex
= 0; SmmTypeIndex
< sizeof (mSmmFileTypes
)/sizeof (EFI_FV_FILETYPE
); SmmTypeIndex
++) {
1329 // Initialize the search key
1333 Type
= mSmmFileTypes
[SmmTypeIndex
];
1334 GetNextFileStatus
= Fv
->GetNextFile (
1342 if (!EFI_ERROR (GetNextFileStatus
)) {
1343 if (Type
== EFI_FV_FILETYPE_SMM_CORE
) {
1345 // If this is the SMM core fill in it's DevicePath & DeviceHandle
1347 if (mSmmCoreLoadedImage
->FilePath
== NULL
) {
1349 // Maybe one special FV contains only one SMM_CORE module, so its device path must
1350 // be initialized completely.
1352 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, &NameGuid
);
1353 SetDevicePathEndNode (&mFvDevicePath
.End
);
1356 // Make an EfiBootServicesData buffer copy of FilePath
1358 Status
= gBS
->AllocatePool (
1359 EfiBootServicesData
,
1360 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
),
1361 (VOID
**)&mSmmCoreLoadedImage
->FilePath
1363 ASSERT_EFI_ERROR (Status
);
1364 CopyMem (mSmmCoreLoadedImage
->FilePath
, &mFvDevicePath
, GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
));
1366 mSmmCoreLoadedImage
->DeviceHandle
= FvHandle
;
1368 if (mSmmCoreDriverEntry
->SmmLoadedImage
.FilePath
== NULL
) {
1370 // Maybe one special FV contains only one SMM_CORE module, so its device path must
1371 // be initialized completely.
1373 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, &NameGuid
);
1374 SetDevicePathEndNode (&mFvDevicePath
.End
);
1377 // Make a buffer copy FilePath
1379 Status
= SmmAllocatePool (
1380 EfiRuntimeServicesData
,
1381 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
),
1382 (VOID
**)&mSmmCoreDriverEntry
->SmmLoadedImage
.FilePath
1384 ASSERT_EFI_ERROR (Status
);
1385 CopyMem (mSmmCoreDriverEntry
->SmmLoadedImage
.FilePath
, &mFvDevicePath
, GetDevicePathSize((EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
));
1387 mSmmCoreDriverEntry
->SmmLoadedImage
.DeviceHandle
= FvHandle
;
1390 SmmAddToDriverList (Fv
, FvHandle
, &NameGuid
);
1393 } while (!EFI_ERROR (GetNextFileStatus
));
1397 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1398 // (Note: AprioriFile is in DXE memory)
1401 Status
= Fv
->ReadSection (
1406 (VOID
**)&AprioriFile
,
1408 &AuthenticationStatus
1410 if (!EFI_ERROR (Status
)) {
1411 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1413 AprioriEntryCount
= 0;
1417 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1418 // drivers not in the current FV and these must be skipped since the a priori list
1419 // is only valid for the FV that it resided in.
1422 for (AprioriIndex
= 0; AprioriIndex
< AprioriEntryCount
; AprioriIndex
++) {
1423 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1424 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1425 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[AprioriIndex
]) &&
1426 (FvHandle
== DriverEntry
->FvHandle
)) {
1427 DriverEntry
->Dependent
= FALSE
;
1428 DriverEntry
->Scheduled
= TRUE
;
1429 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1430 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
1431 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1438 // Free data allocated by Fv->ReadSection ()
1440 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1441 // used the UEFI Boot Services AllocatePool() function
1443 gBS
->FreePool (AprioriFile
);
1447 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1448 // discovered SMM drivers that have been discovered but not dispatched.
1450 Status
= SmmDispatcher ();
1453 // Check to see if CommBuffer and CommBufferSize are valid
1455 if (CommBuffer
!= NULL
&& CommBufferSize
!= NULL
) {
1456 if (*CommBufferSize
> 0) {
1457 if (Status
== EFI_NOT_READY
) {
1459 // If a the SMM Core Entry Point was just registered, then set flag to
1460 // request the SMM Dispatcher to be restarted.
1462 *(UINT8
*)CommBuffer
= COMM_BUFFER_SMM_DISPATCH_RESTART
;
1463 } else if (!EFI_ERROR (Status
)) {
1465 // Set the flag to show that the SMM Dispatcher executed without errors
1467 *(UINT8
*)CommBuffer
= COMM_BUFFER_SMM_DISPATCH_SUCCESS
;
1470 // Set the flag to show that the SMM Dispatcher encountered an error
1472 *(UINT8
*)CommBuffer
= COMM_BUFFER_SMM_DISPATCH_ERROR
;
1481 Traverse the discovered list for any drivers that were discovered but not loaded
1482 because the dependency expressions evaluated to false.
1486 SmmDisplayDiscoveredNotDispatched (
1491 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1493 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1494 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1495 if (DriverEntry
->Dependent
) {
1496 DEBUG ((DEBUG_LOAD
, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));