4 Step #1 - When a FV protocol is added to the system every driver in the FV
5 is added to the mDiscoveredList. The SOR, Before, and After Depex are
6 pre-processed as drivers are added to the mDiscoveredList. If an Apriori
7 file exists in the FV those drivers are addeded to the
8 mScheduledQueue. The mFvHandleList is used to make sure a
9 FV is only processed once.
11 Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
12 start it. After mScheduledQueue is drained check the
13 mDiscoveredList to see if any item has a Depex that is ready to
14 be placed on the mScheduledQueue.
16 Step #3 - Adding to the mScheduledQueue requires that you process Before
17 and After dependencies. This is done recursively as the call to add
18 to the mScheduledQueue checks for Before and recursively adds
19 all Befores. It then addes the item that was passed in and then
20 processess the After dependecies by recursively calling the routine.
23 The rules for the dispatcher are similar to the DXE dispatcher.
25 The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
26 is the state diagram for the DXE dispatcher
28 Depex - Dependency Expresion.
29 SOR - Schedule On Request - Don't schedule if this bit is set.
31 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
32 This program and the accompanying materials are licensed and made available
33 under the terms and conditions of the BSD License which accompanies this
34 distribution. The full text of the license may be found at
35 http://opensource.org/licenses/bsd-license.php
37 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
42 #include "PiSmmCore.h"
45 // SMM Dispatcher Data structures
47 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
50 LIST_ENTRY Link
; // mFvHandleList
55 // Function Prototypes
59 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
60 must add any driver with a before dependency on InsertedDriverEntry first.
61 You do this by recursively calling this routine. After all the Befores are
62 processed you can add InsertedDriverEntry to the mScheduledQueue.
63 Then you can add any driver with an After dependency on InsertedDriverEntry
64 by recursively calling this routine.
66 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
70 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
71 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
75 // The Driver List contains one copy of every driver that has been discovered.
76 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
78 LIST_ENTRY mDiscoveredList
= INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList
);
81 // Queue of drivers that are ready to dispatch. This queue is a subset of the
82 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
84 LIST_ENTRY mScheduledQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue
);
87 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
89 LIST_ENTRY mFvHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList
);
92 // Flag for the SMM Dispacher. TRUE if dispatcher is execuing.
94 BOOLEAN gDispatcherRunning
= FALSE
;
97 // Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched
99 BOOLEAN gRequestDispatch
= FALSE
;
102 // List of file types supported by dispatcher
104 EFI_FV_FILETYPE mSmmFileTypes
[] = {
106 EFI_FV_FILETYPE_COMBINED_SMM_DXE
108 // Note: DXE core will process the FV image file, so skip it in SMM core
109 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
114 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File
;
115 EFI_DEVICE_PATH_PROTOCOL End
;
116 } FV_FILEPATH_DEVICE_PATH
;
118 FV_FILEPATH_DEVICE_PATH mFvDevicePath
;
121 // DXE Architecture Protocols
123 EFI_SECURITY_ARCH_PROTOCOL
*mSecurity
= NULL
;
126 // The global variable is defined for Loading modules at fixed address feature to track the SMM code
127 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding
128 // memory page available or not.
130 GLOBAL_REMOVE_IF_UNREFERENCED UINT64
*mSmmCodeMemoryRangeUsageBitMap
=NULL
;
133 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
134 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
135 The function is only invoked when load modules at fixed address feature is enabled.
137 @param ImageBase The base addres the image will be loaded at.
138 @param ImageSize The size of the image
140 @retval EFI_SUCCESS The memory range the image will be loaded in is available
141 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
144 CheckAndMarkFixLoadingMemoryUsageBitMap (
145 IN EFI_PHYSICAL_ADDRESS ImageBase
,
149 UINT32 SmmCodePageNumber
;
151 EFI_PHYSICAL_ADDRESS SmmCodeBase
;
152 UINTN BaseOffsetPageNumber
;
153 UINTN TopOffsetPageNumber
;
156 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
158 SmmCodePageNumber
= PcdGet32(PcdLoadFixAddressSmmCodePageNumber
);
159 SmmCodeSize
= EFI_PAGES_TO_SIZE (SmmCodePageNumber
);
160 SmmCodeBase
= gLoadModuleAtFixAddressSmramBase
;
163 // If the memory usage bit map is not initialized, do it. Every bit in the array
164 // indicate the status of the corresponding memory page, available or not
166 if (mSmmCodeMemoryRangeUsageBitMap
== NULL
) {
167 mSmmCodeMemoryRangeUsageBitMap
= AllocateZeroPool(((SmmCodePageNumber
/ 64) + 1)*sizeof(UINT64
));
170 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
172 if (mSmmCodeMemoryRangeUsageBitMap
== NULL
) {
173 return EFI_NOT_FOUND
;
176 // see if the memory range for loading the image is in the SMM code range.
178 if (SmmCodeBase
+ SmmCodeSize
< ImageBase
+ ImageSize
|| SmmCodeBase
> ImageBase
) {
179 return EFI_NOT_FOUND
;
182 // Test if the memory is avalaible or not.
184 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
- SmmCodeBase
));
185 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES((UINT32
)(ImageBase
+ ImageSize
- SmmCodeBase
));
186 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
187 if ((mSmmCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64(1, (Index
% 64))) != 0) {
189 // This page is already used.
191 return EFI_NOT_FOUND
;
196 // Being here means the memory range is available. So mark the bits for the memory range
198 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
199 mSmmCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64(1, (Index
% 64));
204 Get the fixed loadding address from image header assigned by build tool. This function only be called
205 when Loading module at Fixed address feature enabled.
207 @param ImageContext Pointer to the image context structure that describes the PE/COFF
208 image that needs to be examined by this function.
209 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
210 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
214 GetPeCoffImageFixLoadingAssignedAddress(
215 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
218 UINTN SectionHeaderOffset
;
220 EFI_IMAGE_SECTION_HEADER SectionHeader
;
221 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
222 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
225 UINT16 NumberOfSections
;
226 UINT64 ValueInSectionHeader
;
228 FixLoaddingAddress
= 0;
229 Status
= EFI_NOT_FOUND
;
232 // Get PeHeader pointer
234 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
235 SectionHeaderOffset
= (UINTN
)(
236 ImageContext
->PeCoffHeaderOffset
+
238 sizeof (EFI_IMAGE_FILE_HEADER
) +
239 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
241 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
244 // Get base address from the first section header that doesn't point to code section.
246 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
248 // Read section header from file
250 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
251 Status
= ImageContext
->ImageRead (
252 ImageContext
->Handle
,
257 if (EFI_ERROR (Status
)) {
261 Status
= EFI_NOT_FOUND
;
263 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
265 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
266 // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
267 // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
268 // should not be Zero, or else, these 2 fileds should be set to Zero
270 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
271 if (ValueInSectionHeader
!= 0) {
273 // Found first section header that doesn't point to code section in which uild tool saves the
274 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
276 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressSmramBase
+ (INT64
)ValueInSectionHeader
);
278 // Check if the memory range is avaliable.
280 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoaddingAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
281 if (!EFI_ERROR(Status
)) {
283 // The assigned address is valid. Return the specified loadding address
285 ImageContext
->ImageAddress
= FixLoaddingAddress
;
290 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
292 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoaddingAddress
, Status
));
296 Loads an EFI image into SMRAM.
298 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
306 IN OUT EFI_SMM_DRIVER_ENTRY
*DriverEntry
309 UINT32 AuthenticationStatus
;
316 EFI_STATUS SecurityStatus
;
317 EFI_HANDLE DeviceHandle
;
318 EFI_PHYSICAL_ADDRESS DstBuffer
;
319 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
320 EFI_DEVICE_PATH_PROTOCOL
*OriginalFilePath
;
321 EFI_DEVICE_PATH_PROTOCOL
*HandleFilePath
;
322 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
323 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
327 Fv
= DriverEntry
->Fv
;
328 NameGuid
= &DriverEntry
->FileName
;
329 FilePath
= DriverEntry
->FvFileDevicePath
;
331 OriginalFilePath
= FilePath
;
332 HandleFilePath
= FilePath
;
334 SecurityStatus
= EFI_SUCCESS
;
335 Status
= EFI_SUCCESS
;
336 AuthenticationStatus
= 0;
339 // Try to get the image device handle by checking the match protocol.
341 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &HandleFilePath
, &DeviceHandle
);
342 if (EFI_ERROR(Status
)) {
347 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
349 if (mSecurity
== NULL
) {
350 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
354 // Verify the Authentication Status through the Security Architectural Protocol
356 if ((mSecurity
!= NULL
) && (OriginalFilePath
!= NULL
)) {
357 SecurityStatus
= mSecurity
->FileAuthenticationState (
359 AuthenticationStatus
,
362 if (EFI_ERROR (SecurityStatus
) && SecurityStatus
!= EFI_SECURITY_VIOLATION
) {
363 Status
= SecurityStatus
;
369 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
371 FilePath
= OriginalFilePath
;
372 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&HandleFilePath
);
373 if (!EFI_ERROR (Status
)) {
374 FilePathSize
= GetDevicePathSize (HandleFilePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
375 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*)FilePath
) + FilePathSize
);
379 // Try reading PE32 section firstly
381 Status
= Fv
->ReadSection (
388 &AuthenticationStatus
391 if (EFI_ERROR (Status
)) {
393 // Try reading TE section secondly
397 Status
= Fv
->ReadSection (
404 &AuthenticationStatus
408 if (EFI_ERROR (Status
)) {
409 if (Buffer
!= NULL
) {
410 Status
= gBS
->FreePool (Buffer
);
416 // Initialize ImageContext
418 ImageContext
.Handle
= Buffer
;
419 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
422 // Get information about the image being loaded
424 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
425 if (EFI_ERROR (Status
)) {
426 if (Buffer
!= NULL
) {
427 Status
= gBS
->FreePool (Buffer
);
432 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE
433 // to hold the Smm driver code
435 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
437 // Get the fixed loading address assigned by Build tool
439 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
440 if (!EFI_ERROR (Status
)) {
442 // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range
443 // following statements is to bypass SmmFreePages
446 DstBuffer
= (UINTN
)gLoadModuleAtFixAddressSmramBase
;
448 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
450 // allocate the memory to load the SMM driver
452 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
453 DstBuffer
= (UINTN
)(-1);
455 Status
= SmmAllocatePages (
457 EfiRuntimeServicesCode
,
461 if (EFI_ERROR (Status
)) {
462 if (Buffer
!= NULL
) {
463 Status
= gBS
->FreePool (Buffer
);
467 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
470 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
471 DstBuffer
= (UINTN
)(-1);
473 Status
= SmmAllocatePages (
475 EfiRuntimeServicesCode
,
479 if (EFI_ERROR (Status
)) {
480 if (Buffer
!= NULL
) {
481 Status
= gBS
->FreePool (Buffer
);
486 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
489 // Align buffer on section boundry
491 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
492 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
495 // Load the image to our new buffer
497 Status
= PeCoffLoaderLoadImage (&ImageContext
);
498 if (EFI_ERROR (Status
)) {
499 if (Buffer
!= NULL
) {
500 Status
= gBS
->FreePool (Buffer
);
502 SmmFreePages (DstBuffer
, PageCount
);
507 // Relocate the image in our new buffer
509 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
510 if (EFI_ERROR (Status
)) {
511 if (Buffer
!= NULL
) {
512 Status
= gBS
->FreePool (Buffer
);
514 SmmFreePages (DstBuffer
, PageCount
);
519 // Flush the instruction cache so the image data are written before we execute it
521 InvalidateInstructionCacheRange ((VOID
*)(UINTN
) ImageContext
.ImageAddress
, (UINTN
) ImageContext
.ImageSize
);
524 // Save Image EntryPoint in DriverEntry
526 DriverEntry
->ImageEntryPoint
= ImageContext
.EntryPoint
;
527 DriverEntry
->ImageBuffer
= DstBuffer
;
528 DriverEntry
->NumberOfPage
= PageCount
;
531 // Allocate a Loaded Image Protocol in EfiBootServicesData
533 Status
= gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
), (VOID
**)&DriverEntry
->LoadedImage
);
534 if (EFI_ERROR (Status
)) {
535 if (Buffer
!= NULL
) {
536 Status
= gBS
->FreePool (Buffer
);
538 SmmFreePages (DstBuffer
, PageCount
);
543 // Fill in the remaining fields of the Loaded Image Protocol instance.
544 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
546 DriverEntry
->LoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
547 DriverEntry
->LoadedImage
->ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
548 DriverEntry
->LoadedImage
->SystemTable
= gST
;
549 DriverEntry
->LoadedImage
->DeviceHandle
= DeviceHandle
;
552 // Make an EfiBootServicesData buffer copy of FilePath
554 Status
= gBS
->AllocatePool (EfiBootServicesData
, GetDevicePathSize (FilePath
), (VOID
**)&DriverEntry
->LoadedImage
->FilePath
);
555 if (EFI_ERROR (Status
)) {
556 if (Buffer
!= NULL
) {
557 Status
= gBS
->FreePool (Buffer
);
559 SmmFreePages (DstBuffer
, PageCount
);
562 CopyMem (DriverEntry
->LoadedImage
->FilePath
, FilePath
, GetDevicePathSize (FilePath
));
564 DriverEntry
->LoadedImage
->ImageBase
= (VOID
*)(UINTN
)DriverEntry
->ImageBuffer
;
565 DriverEntry
->LoadedImage
->ImageSize
= ImageContext
.ImageSize
;
566 DriverEntry
->LoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
567 DriverEntry
->LoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
570 // Create a new image handle in the UEFI handle database for the SMM Driver
572 DriverEntry
->ImageHandle
= NULL
;
573 Status
= gBS
->InstallMultipleProtocolInterfaces (
574 &DriverEntry
->ImageHandle
,
575 &gEfiLoadedImageProtocolGuid
, DriverEntry
->LoadedImage
,
580 // Print the load address and the PDB file name if it is available
587 CHAR8 EfiFileName
[256];
590 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
591 "Loading driver at 0x%11p EntryPoint=0x%11p ",
592 (VOID
*)(UINTN
) ImageContext
.ImageAddress
,
593 FUNCTION_ENTRY_POINT (ImageContext
.EntryPoint
)));
597 // Print Module Name by Pdb file path.
598 // Windows and Unix style file path are all trimmed correctly.
600 if (ImageContext
.PdbPointer
!= NULL
) {
602 for (Index
= 0; ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
603 if ((ImageContext
.PdbPointer
[Index
] == '\\') || (ImageContext
.PdbPointer
[Index
] == '/')) {
604 StartIndex
= Index
+ 1;
608 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
609 // The PDB file name is limited in the range of 0~255.
610 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
612 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
613 EfiFileName
[Index
] = ImageContext
.PdbPointer
[Index
+ StartIndex
];
614 if (EfiFileName
[Index
] == 0) {
615 EfiFileName
[Index
] = '.';
617 if (EfiFileName
[Index
] == '.') {
618 EfiFileName
[Index
+ 1] = 'e';
619 EfiFileName
[Index
+ 2] = 'f';
620 EfiFileName
[Index
+ 3] = 'i';
621 EfiFileName
[Index
+ 4] = 0;
626 if (Index
== sizeof (EfiFileName
) - 4) {
627 EfiFileName
[Index
] = 0;
629 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
)); // &Image->ImageContext.PdbPointer[StartIndex]));
631 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
636 // Free buffer allocated by Fv->ReadSection.
638 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
639 // used the UEFI Boot Services AllocatePool() function
641 Status
= gBS
->FreePool(Buffer
);
646 Preprocess dependency expression and update DriverEntry to reflect the
647 state of Before, After, and SOR dependencies. If DriverEntry->Before
648 or DriverEntry->After is set it will never be cleared. If SOR is set
649 it will be cleared by SmmSchedule(), and then the driver can be
652 @param DriverEntry DriverEntry element to update .
654 @retval EFI_SUCCESS It always works.
659 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
664 Iterator
= DriverEntry
->Depex
;
665 if (*Iterator
== EFI_DEP_SOR
) {
666 DriverEntry
->Unrequested
= TRUE
;
668 DriverEntry
->Dependent
= TRUE
;
671 if (*Iterator
== EFI_DEP_BEFORE
) {
672 DriverEntry
->Before
= TRUE
;
673 } else if (*Iterator
== EFI_DEP_AFTER
) {
674 DriverEntry
->After
= TRUE
;
677 if (DriverEntry
->Before
|| DriverEntry
->After
) {
678 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
685 Read Depex and pre-process the Depex for Before and After. If Section Extraction
686 protocol returns an error via ReadSection defer the reading of the Depex.
688 @param DriverEntry Driver to work on.
690 @retval EFI_SUCCESS Depex read and preprossesed
691 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
692 and Depex reading needs to be retried.
693 @retval Error DEPEX not found.
697 SmmGetDepexSectionAndPreProccess (
698 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
702 EFI_SECTION_TYPE SectionType
;
703 UINT32 AuthenticationStatus
;
704 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
706 Fv
= DriverEntry
->Fv
;
709 // Grab Depex info, it will never be free'ed.
710 // (Note: DriverEntry->Depex is in DXE memory)
712 SectionType
= EFI_SECTION_SMM_DEPEX
;
713 Status
= Fv
->ReadSection (
715 &DriverEntry
->FileName
,
719 (UINTN
*)&DriverEntry
->DepexSize
,
720 &AuthenticationStatus
722 if (EFI_ERROR (Status
)) {
723 if (Status
== EFI_PROTOCOL_ERROR
) {
725 // The section extraction protocol failed so set protocol error flag
727 DriverEntry
->DepexProtocolError
= TRUE
;
730 // If no Depex assume depend on all architectural protocols
732 DriverEntry
->Depex
= NULL
;
733 DriverEntry
->Dependent
= TRUE
;
734 DriverEntry
->DepexProtocolError
= FALSE
;
738 // Set Before, After, and Unrequested state information based on Depex
739 // Driver will be put in Dependent or Unrequested state
741 SmmPreProcessDepex (DriverEntry
);
742 DriverEntry
->DepexProtocolError
= FALSE
;
749 Check every driver and locate a matching one. If the driver is found, the Unrequested
750 state flag is cleared.
752 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
753 the firmware file specified by DriverName.
754 @param DriverName The Driver name to put in the Dependent state.
756 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
758 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
764 IN EFI_HANDLE FirmwareVolumeHandle
,
765 IN EFI_GUID
*DriverName
769 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
772 // Check every driver
774 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
775 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
776 if (DriverEntry
->FvHandle
== FirmwareVolumeHandle
&&
777 DriverEntry
->Unrequested
&&
778 CompareGuid (DriverName
, &DriverEntry
->FileName
)) {
780 // Move the driver from the Unrequested to the Dependent state
782 DriverEntry
->Unrequested
= FALSE
;
783 DriverEntry
->Dependent
= TRUE
;
788 return EFI_NOT_FOUND
;
792 This is the main Dispatcher for SMM and it exits when there are no more
793 drivers to run. Drain the mScheduledQueue and load and start a PE
794 image for each driver. Search the mDiscoveredList to see if any driver can
795 be placed on the mScheduledQueue. If no drivers are placed on the
796 mScheduledQueue exit the function. On exit it is assumed the Bds()
797 will be called, and when the Bds() exits the Dispatcher will be called
800 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
801 @retval EFI_NOT_FOUND No SMM Drivers were dispatched
802 @retval EFI_SUCCESS One or more SMM Drivers were dispatched
811 EFI_STATUS ReturnStatus
;
813 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
816 if (!gRequestDispatch
) {
817 return EFI_NOT_FOUND
;
820 if (gDispatcherRunning
) {
822 // If the dispatcher is running don't let it be restarted.
824 return EFI_ALREADY_STARTED
;
827 gDispatcherRunning
= TRUE
;
829 ReturnStatus
= EFI_NOT_FOUND
;
832 // Drain the Scheduled Queue
834 while (!IsListEmpty (&mScheduledQueue
)) {
836 mScheduledQueue
.ForwardLink
,
837 EFI_SMM_DRIVER_ENTRY
,
839 EFI_SMM_DRIVER_ENTRY_SIGNATURE
843 // Load the SMM Driver image into memory. If the Driver was transitioned from
844 // Untrused to Scheduled it would have already been loaded so we may need to
845 // skip the LoadImage
847 if (DriverEntry
->ImageHandle
== NULL
) {
848 Status
= SmmLoadImage (DriverEntry
);
851 // Update the driver state to reflect that it's been loaded
853 if (EFI_ERROR (Status
)) {
855 if (Status
== EFI_SECURITY_VIOLATION
) {
857 // Take driver from Scheduled to Untrused state
859 DriverEntry
->Untrusted
= TRUE
;
862 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
863 // Take driver from Scheduled to Initialized.
865 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
867 DriverEntry
->Initialized
= TRUE
;
870 DriverEntry
->Scheduled
= FALSE
;
871 RemoveEntryList (&DriverEntry
->ScheduledLink
);
874 // If it's an error don't try the StartImage
880 DriverEntry
->Scheduled
= FALSE
;
881 DriverEntry
->Initialized
= TRUE
;
882 RemoveEntryList (&DriverEntry
->ScheduledLink
);
884 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
886 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_BEGIN
,
887 &DriverEntry
->ImageHandle
,
888 sizeof (DriverEntry
->ImageHandle
)
892 // For each SMM driver, pass NULL as ImageHandle
894 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
)(DriverEntry
->ImageHandle
, gST
);
895 if (EFI_ERROR(Status
)){
896 SmmFreePages(DriverEntry
->ImageBuffer
, DriverEntry
->NumberOfPage
);
899 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
901 EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_PC_INIT_END
,
902 &DriverEntry
->ImageHandle
,
903 sizeof (DriverEntry
->ImageHandle
)
906 ReturnStatus
= EFI_SUCCESS
;
910 // Search DriverList for items to place on Scheduled Queue
913 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
914 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
916 if (DriverEntry
->DepexProtocolError
){
918 // If Section Extraction Protocol did not let the Depex be read before retry the read
920 Status
= SmmGetDepexSectionAndPreProccess (DriverEntry
);
923 if (DriverEntry
->Dependent
) {
924 if (SmmIsSchedulable (DriverEntry
)) {
925 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
930 } while (ReadyToRun
);
933 // If there is no more SMM driver to dispatch, stop the dispatch request
935 gRequestDispatch
= FALSE
;
936 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
937 DriverEntry
= CR (Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
939 if (!DriverEntry
->Initialized
){
941 // We have SMM driver pending to dispatch
943 gRequestDispatch
= TRUE
;
948 gDispatcherRunning
= FALSE
;
954 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
955 must add any driver with a before dependency on InsertedDriverEntry first.
956 You do this by recursively calling this routine. After all the Befores are
957 processed you can add InsertedDriverEntry to the mScheduledQueue.
958 Then you can add any driver with an After dependency on InsertedDriverEntry
959 by recursively calling this routine.
961 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
965 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
966 IN EFI_SMM_DRIVER_ENTRY
*InsertedDriverEntry
970 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
973 // Process Before Dependency
975 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
976 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
977 if (DriverEntry
->Before
&& DriverEntry
->Dependent
) {
978 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
980 // Recursively process BEFORE
982 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
988 // Convert driver from Dependent to Scheduled state
991 InsertedDriverEntry
->Dependent
= FALSE
;
992 InsertedDriverEntry
->Scheduled
= TRUE
;
993 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
997 // Process After Dependency
999 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1000 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1001 if (DriverEntry
->After
&& DriverEntry
->Dependent
) {
1002 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
1004 // Recursively process AFTER
1006 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
1013 Return TRUE if the Fv has been processed, FALSE if not.
1015 @param FvHandle The handle of a FV that's being tested
1017 @retval TRUE Fv protocol on FvHandle has been processed
1018 @retval FALSE Fv protocol on FvHandle has not yet been
1023 FvHasBeenProcessed (
1024 IN EFI_HANDLE FvHandle
1028 KNOWN_HANDLE
*KnownHandle
;
1030 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
1031 KnownHandle
= CR(Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
1032 if (KnownHandle
->Handle
== FvHandle
) {
1040 Remember that Fv protocol on FvHandle has had it's drivers placed on the
1041 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
1042 never removed/freed from the mFvHandleList.
1044 @param FvHandle The handle of a FV that has been processed
1048 FvIsBeingProcesssed (
1049 IN EFI_HANDLE FvHandle
1052 KNOWN_HANDLE
*KnownHandle
;
1054 KnownHandle
= AllocatePool (sizeof (KNOWN_HANDLE
));
1055 ASSERT (KnownHandle
!= NULL
);
1057 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
1058 KnownHandle
->Handle
= FvHandle
;
1059 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
1063 Convert FvHandle and DriverName into an EFI device path
1065 @param Fv Fv protocol, needed to read Depex info out of
1067 @param FvHandle Handle for Fv, needed in the
1068 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1069 read out of the FV at a later time.
1070 @param DriverName Name of driver to add to mDiscoveredList.
1072 @return Pointer to device path constructed from FvHandle and DriverName
1075 EFI_DEVICE_PATH_PROTOCOL
*
1077 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1078 IN EFI_HANDLE FvHandle
,
1079 IN EFI_GUID
*DriverName
1083 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1084 EFI_DEVICE_PATH_PROTOCOL
*FileNameDevicePath
;
1087 // Remember the device path of the FV
1089 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1090 if (EFI_ERROR (Status
)) {
1091 FileNameDevicePath
= NULL
;
1094 // Build a device path to the file in the FV to pass into gBS->LoadImage
1096 EfiInitializeFwVolDevicepathNode (&mFvDevicePath
.File
, DriverName
);
1097 SetDevicePathEndNode (&mFvDevicePath
.End
);
1100 // Note: FileNameDevicePath is in DXE memory
1102 FileNameDevicePath
= AppendDevicePath (
1104 (EFI_DEVICE_PATH_PROTOCOL
*)&mFvDevicePath
1107 return FileNameDevicePath
;
1111 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1112 and initilize any state variables. Read the Depex from the FV and store it
1113 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
1114 The Discovered list is never free'ed and contains booleans that represent the
1115 other possible SMM driver states.
1117 @param Fv Fv protocol, needed to read Depex info out of
1119 @param FvHandle Handle for Fv, needed in the
1120 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1121 read out of the FV at a later time.
1122 @param DriverName Name of driver to add to mDiscoveredList.
1124 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1125 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1126 DriverName may be active in the system at any one
1131 SmmAddToDriverList (
1132 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
,
1133 IN EFI_HANDLE FvHandle
,
1134 IN EFI_GUID
*DriverName
1137 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1140 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1143 DriverEntry
= AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY
));
1144 ASSERT (DriverEntry
!= NULL
);
1146 DriverEntry
->Signature
= EFI_SMM_DRIVER_ENTRY_SIGNATURE
;
1147 CopyGuid (&DriverEntry
->FileName
, DriverName
);
1148 DriverEntry
->FvHandle
= FvHandle
;
1149 DriverEntry
->Fv
= Fv
;
1150 DriverEntry
->FvFileDevicePath
= SmmFvToDevicePath (Fv
, FvHandle
, DriverName
);
1152 SmmGetDepexSectionAndPreProccess (DriverEntry
);
1154 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
1155 gRequestDispatch
= TRUE
;
1161 This function is the main entry point for an SMM handler dispatch
1162 or communicate-based callback.
1164 Event notification that is fired every time a FV dispatch protocol is added.
1165 More than one protocol may have been added when this event is fired, so you
1166 must loop on SmmLocateHandle () to see how many protocols were added and
1167 do the following to each FV:
1168 If the Fv has already been processed, skip it. If the Fv has not been
1169 processed then mark it as being processed, as we are about to process it.
1170 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1171 mDiscoveredList is never free'ed and contains variables that define
1172 the other states the SMM driver transitions to..
1173 While you are at it read the A Priori file into memory.
1174 Place drivers in the A Priori list onto the mScheduledQueue.
1176 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1177 @param Context Points to an optional handler context which was specified when the handler was registered.
1178 @param CommBuffer A pointer to a collection of data in memory that will
1179 be conveyed from a non-SMM environment into an SMM environment.
1180 @param CommBufferSize The size of the CommBuffer.
1187 SmmDriverDispatchHandler (
1188 IN EFI_HANDLE DispatchHandle
,
1189 IN CONST VOID
*Context
, OPTIONAL
1190 IN OUT VOID
*CommBuffer
, OPTIONAL
1191 IN OUT UINTN
*CommBufferSize OPTIONAL
1196 EFI_HANDLE
*HandleBuffer
;
1197 EFI_STATUS GetNextFileStatus
;
1198 EFI_STATUS SecurityStatus
;
1199 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1200 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
1201 EFI_HANDLE FvHandle
;
1204 EFI_FV_FILETYPE Type
;
1205 EFI_FV_FILE_ATTRIBUTES Attributes
;
1207 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1208 EFI_GUID
*AprioriFile
;
1209 UINTN AprioriEntryCount
;
1212 UINT32 AuthenticationStatus
;
1215 HandleBuffer
= NULL
;
1216 Status
= gBS
->LocateHandleBuffer (
1218 &gEfiFirmwareVolume2ProtocolGuid
,
1223 if (EFI_ERROR (Status
)) {
1224 return EFI_NOT_FOUND
;
1227 for (Index
= 0; Index
< HandleCount
; Index
++) {
1228 FvHandle
= HandleBuffer
[Index
];
1230 if (FvHasBeenProcessed (FvHandle
)) {
1232 // This Fv has already been processed so lets skip it!
1238 // Since we are about to process this Fv mark it as processed.
1240 FvIsBeingProcesssed (FvHandle
);
1242 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
1243 if (EFI_ERROR (Status
)) {
1245 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1251 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1252 if (EFI_ERROR (Status
)) {
1254 // The Firmware volume doesn't have device path, can't be dispatched.
1260 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1262 if (mSecurity
== NULL
) {
1263 gBS
->LocateProtocol (&gEfiSecurityArchProtocolGuid
, NULL
, (VOID
**)&mSecurity
);
1267 // Evaluate the authentication status of the Firmware Volume through
1268 // Security Architectural Protocol
1270 if (mSecurity
!= NULL
) {
1271 SecurityStatus
= mSecurity
->FileAuthenticationState (
1276 if (SecurityStatus
!= EFI_SUCCESS
) {
1278 // Security check failed. The firmware volume should not be used for any purpose.
1285 // Discover Drivers in FV and add them to the Discovered Driver List.
1286 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1288 for (Index
= 0; Index
< sizeof (mSmmFileTypes
)/sizeof (EFI_FV_FILETYPE
); Index
++) {
1290 // Initialize the search key
1294 Type
= mSmmFileTypes
[Index
];
1295 GetNextFileStatus
= Fv
->GetNextFile (
1303 if (!EFI_ERROR (GetNextFileStatus
)) {
1304 SmmAddToDriverList (Fv
, FvHandle
, &NameGuid
);
1306 } while (!EFI_ERROR (GetNextFileStatus
));
1310 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1311 // (Note: AprioriFile is in DXE memory)
1314 Status
= Fv
->ReadSection (
1319 (VOID
**)&AprioriFile
,
1321 &AuthenticationStatus
1323 if (!EFI_ERROR (Status
)) {
1324 AprioriEntryCount
= SizeOfBuffer
/ sizeof (EFI_GUID
);
1326 AprioriEntryCount
= 0;
1330 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1331 // drivers not in the current FV and these must be skipped since the a priori list
1332 // is only valid for the FV that it resided in.
1335 for (Index
= 0; Index
< AprioriEntryCount
; Index
++) {
1336 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
1337 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1338 if (CompareGuid (&DriverEntry
->FileName
, &AprioriFile
[Index
]) &&
1339 (FvHandle
== DriverEntry
->FvHandle
)) {
1340 DriverEntry
->Dependent
= FALSE
;
1341 DriverEntry
->Scheduled
= TRUE
;
1342 InsertTailList (&mScheduledQueue
, &DriverEntry
->ScheduledLink
);
1349 // Free data allocated by Fv->ReadSection ()
1351 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1352 // used the UEFI Boot Services AllocatePool() function
1354 gBS
->FreePool (AprioriFile
);
1358 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1359 // discovered SMM drivers that have been discovered but not dispatched.
1361 return SmmDispatcher ();
1365 Traverse the discovered list for any drivers that were discovered but not loaded
1366 because the dependency experessions evaluated to false.
1370 SmmDisplayDiscoveredNotDispatched (
1375 EFI_SMM_DRIVER_ENTRY
*DriverEntry
;
1377 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1378 DriverEntry
= CR(Link
, EFI_SMM_DRIVER_ENTRY
, Link
, EFI_SMM_DRIVER_ENTRY_SIGNATURE
);
1379 if (DriverEntry
->Dependent
) {
1380 DEBUG ((DEBUG_LOAD
, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));