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) 2014, Hewlett-Packard Development Company, L.P.
31 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
32 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
34 This program and the accompanying materials are licensed and made available
35 under the terms and conditions of the BSD License which accompanies this
36 distribution. The full text of the license may be found at
37 http://opensource.org/licenses/bsd-license.php
39 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
40 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
44 #include "StandaloneMmCore.h"
47 // MM Dispatcher Data structures
49 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
53 LIST_ENTRY Link
; // mFvHandleList
58 // Function Prototypes
62 MmCoreFfsFindMmDriver (
63 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
67 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
68 must add any driver with a before dependency on InsertedDriverEntry first.
69 You do this by recursively calling this routine. After all the Befores are
70 processed you can add InsertedDriverEntry to the mScheduledQueue.
71 Then you can add any driver with an After dependency on InsertedDriverEntry
72 by recursively calling this routine.
74 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
78 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
79 IN EFI_MM_DRIVER_ENTRY
*InsertedDriverEntry
83 // The Driver List contains one copy of every driver that has been discovered.
84 // Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
86 LIST_ENTRY mDiscoveredList
= INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList
);
89 // Queue of drivers that are ready to dispatch. This queue is a subset of the
90 // mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
92 LIST_ENTRY mScheduledQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue
);
95 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
97 LIST_ENTRY mFvHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList
);
100 // Flag for the MM Dispacher. TRUE if dispatcher is execuing.
102 BOOLEAN gDispatcherRunning
= FALSE
;
105 // Flag for the MM Dispacher. TRUE if there is one or more MM drivers ready to be dispatched
107 BOOLEAN gRequestDispatch
= FALSE
;
110 // The global variable is defined for Loading modules at fixed address feature to track the MM code
111 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding
112 // memory page available or not.
114 GLOBAL_REMOVE_IF_UNREFERENCED UINT64
*mMmCodeMemoryRangeUsageBitMap
=NULL
;
117 To check memory usage bit map array to figure out if the memory range in which the image will be loaded
118 is available or not. If memory range is avaliable, the function will mark the correponding bits to 1
119 which indicates the memory range is used. The function is only invoked when load modules at fixed address
122 @param ImageBase The base addres the image will be loaded at.
123 @param ImageSize The size of the image
125 @retval EFI_SUCCESS The memory range the image will be loaded in is available
126 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
129 CheckAndMarkFixLoadingMemoryUsageBitMap (
130 IN EFI_PHYSICAL_ADDRESS ImageBase
,
134 UINT32 MmCodePageNumber
;
136 EFI_PHYSICAL_ADDRESS MmCodeBase
;
137 UINTN BaseOffsetPageNumber
;
138 UINTN TopOffsetPageNumber
;
142 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
144 MmCodePageNumber
= 0;
145 MmCodeSize
= EFI_PAGES_TO_SIZE (MmCodePageNumber
);
146 MmCodeBase
= gLoadModuleAtFixAddressMmramBase
;
149 // If the memory usage bit map is not initialized, do it. Every bit in the array
150 // indicate the status of the corresponding memory page, available or not
152 if (mMmCodeMemoryRangeUsageBitMap
== NULL
) {
153 mMmCodeMemoryRangeUsageBitMap
= AllocateZeroPool (((MmCodePageNumber
/ 64) + 1) * sizeof (UINT64
));
157 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
159 if (mMmCodeMemoryRangeUsageBitMap
== NULL
) {
160 return EFI_NOT_FOUND
;
164 // see if the memory range for loading the image is in the MM code range.
166 if (MmCodeBase
+ MmCodeSize
< ImageBase
+ ImageSize
|| MmCodeBase
> ImageBase
) {
167 return EFI_NOT_FOUND
;
171 // Test if the memory is avalaible or not.
173 BaseOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES ((UINT32
)(ImageBase
- MmCodeBase
));
174 TopOffsetPageNumber
= (UINTN
)EFI_SIZE_TO_PAGES ((UINT32
)(ImageBase
+ ImageSize
- MmCodeBase
));
175 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
176 if ((mMmCodeMemoryRangeUsageBitMap
[Index
/ 64] & LShiftU64 (1, (Index
% 64))) != 0) {
178 // This page is already used.
180 return EFI_NOT_FOUND
;
185 // Being here means the memory range is available. So mark the bits for the memory range
187 for (Index
= BaseOffsetPageNumber
; Index
< TopOffsetPageNumber
; Index
++) {
188 mMmCodeMemoryRangeUsageBitMap
[Index
/ 64] |= LShiftU64 (1, (Index
% 64));
194 Get the fixed loading address from image header assigned by build tool. This function only be called
195 when Loading module at Fixed address feature enabled.
197 @param ImageContext Pointer to the image context structure that describes the PE/COFF
198 image that needs to be examined by this function.
199 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
200 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
204 GetPeCoffImageFixLoadingAssignedAddress(
205 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
208 UINTN SectionHeaderOffset
;
210 EFI_IMAGE_SECTION_HEADER SectionHeader
;
211 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
212 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
215 UINT16 NumberOfSections
;
216 UINT64 ValueInSectionHeader
;
218 FixLoadingAddress
= 0;
219 Status
= EFI_NOT_FOUND
;
222 // Get PeHeader pointer
224 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
225 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+ sizeof (UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) +
226 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
227 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
230 // Get base address from the first section header that doesn't point to code section.
232 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
234 // Read section header from file
236 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
237 Status
= ImageContext
->ImageRead (
238 ImageContext
->Handle
,
243 if (EFI_ERROR (Status
)) {
247 Status
= EFI_NOT_FOUND
;
249 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
251 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields
252 // in the first section header that doesn't point to code section in image header. So there
253 // is an assumption that when the feature is enabled, if a module with a loading address
254 // assigned by tools, the PointerToRelocations & PointerToLineNumbers fields should not be
255 // Zero, or else, these 2 fields should be set to Zero
257 ValueInSectionHeader
= ReadUnaligned64 ((UINT64
*)&SectionHeader
.PointerToRelocations
);
258 if (ValueInSectionHeader
!= 0) {
260 // Found first section header that doesn't point to code section in which build tool saves the
261 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
263 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressMmramBase
+ (INT64
)ValueInSectionHeader
);
265 // Check if the memory range is available.
267 Status
= CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress
, (UINTN
)(ImageContext
->ImageSize
+ ImageContext
->SectionAlignment
));
268 if (!EFI_ERROR(Status
)) {
270 // The assigned address is valid. Return the specified loading address
272 ImageContext
->ImageAddress
= FixLoadingAddress
;
277 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
279 DEBUG ((DEBUG_INFO
|DEBUG_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n",
280 FixLoadingAddress
, Status
));
284 Loads an EFI image into SMRAM.
286 @param DriverEntry EFI_MM_DRIVER_ENTRY instance
294 IN OUT EFI_MM_DRIVER_ENTRY
*DriverEntry
299 EFI_PHYSICAL_ADDRESS DstBuffer
;
300 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
302 DEBUG ((DEBUG_INFO
, "MmLoadImage - %g\n", &DriverEntry
->FileName
));
304 Status
= EFI_SUCCESS
;
307 // Initialize ImageContext
309 ImageContext
.Handle
= DriverEntry
->Pe32Data
;
310 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
313 // Get information about the image being loaded
315 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
316 if (EFI_ERROR (Status
)) {
320 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES ((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
321 DstBuffer
= (UINTN
)(-1);
323 Status
= MmAllocatePages (
325 EfiRuntimeServicesCode
,
329 if (EFI_ERROR (Status
)) {
333 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)DstBuffer
;
336 // Align buffer on section boundry
338 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
339 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)(ImageContext
.SectionAlignment
- 1));
342 // Load the image to our new buffer
344 Status
= PeCoffLoaderLoadImage (&ImageContext
);
345 if (EFI_ERROR (Status
)) {
346 MmFreePages (DstBuffer
, PageCount
);
351 // Relocate the image in our new buffer
353 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
354 if (EFI_ERROR (Status
)) {
355 MmFreePages (DstBuffer
, PageCount
);
360 // Flush the instruction cache so the image data are written before we execute it
362 InvalidateInstructionCacheRange ((VOID
*)(UINTN
) ImageContext
.ImageAddress
, (UINTN
) ImageContext
.ImageSize
);
365 // Save Image EntryPoint in DriverEntry
367 DriverEntry
->ImageEntryPoint
= ImageContext
.EntryPoint
;
368 DriverEntry
->ImageBuffer
= DstBuffer
;
369 DriverEntry
->NumberOfPage
= PageCount
;
371 if (mEfiSystemTable
!= NULL
) {
372 Status
= mEfiSystemTable
->BootServices
->AllocatePool (
374 sizeof (EFI_LOADED_IMAGE_PROTOCOL
),
375 (VOID
**)&DriverEntry
->LoadedImage
377 if (EFI_ERROR (Status
)) {
378 MmFreePages (DstBuffer
, PageCount
);
382 ZeroMem (DriverEntry
->LoadedImage
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
));
384 // Fill in the remaining fields of the Loaded Image Protocol instance.
385 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
387 DriverEntry
->LoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
388 DriverEntry
->LoadedImage
->ParentHandle
= NULL
;
389 DriverEntry
->LoadedImage
->SystemTable
= mEfiSystemTable
;
390 DriverEntry
->LoadedImage
->DeviceHandle
= NULL
;
391 DriverEntry
->LoadedImage
->FilePath
= NULL
;
393 DriverEntry
->LoadedImage
->ImageBase
= (VOID
*)(UINTN
)DriverEntry
->ImageBuffer
;
394 DriverEntry
->LoadedImage
->ImageSize
= ImageContext
.ImageSize
;
395 DriverEntry
->LoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
396 DriverEntry
->LoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
399 // Create a new image handle in the UEFI handle database for the MM Driver
401 DriverEntry
->ImageHandle
= NULL
;
402 Status
= mEfiSystemTable
->BootServices
->InstallMultipleProtocolInterfaces (
403 &DriverEntry
->ImageHandle
,
404 &gEfiLoadedImageProtocolGuid
,
405 DriverEntry
->LoadedImage
,
411 // Print the load address and the PDB file name if it is available
417 CHAR8 EfiFileName
[256];
419 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
,
420 "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
421 (VOID
*)(UINTN
) ImageContext
.ImageAddress
,
422 FUNCTION_ENTRY_POINT (ImageContext
.EntryPoint
)));
425 // Print Module Name by Pdb file path.
426 // Windows and Unix style file path are all trimmed correctly.
428 if (ImageContext
.PdbPointer
!= NULL
) {
430 for (Index
= 0; ImageContext
.PdbPointer
[Index
] != 0; Index
++) {
431 if ((ImageContext
.PdbPointer
[Index
] == '\\') || (ImageContext
.PdbPointer
[Index
] == '/')) {
432 StartIndex
= Index
+ 1;
437 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
438 // The PDB file name is limited in the range of 0~255.
439 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
441 for (Index
= 0; Index
< sizeof (EfiFileName
) - 4; Index
++) {
442 EfiFileName
[Index
] = ImageContext
.PdbPointer
[Index
+ StartIndex
];
443 if (EfiFileName
[Index
] == 0) {
444 EfiFileName
[Index
] = '.';
446 if (EfiFileName
[Index
] == '.') {
447 EfiFileName
[Index
+ 1] = 'e';
448 EfiFileName
[Index
+ 2] = 'f';
449 EfiFileName
[Index
+ 3] = 'i';
450 EfiFileName
[Index
+ 4] = 0;
455 if (Index
== sizeof (EfiFileName
) - 4) {
456 EfiFileName
[Index
] = 0;
458 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "%a", EfiFileName
));
460 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "\n"));
468 Preprocess dependency expression and update DriverEntry to reflect the
469 state of Before and After dependencies. If DriverEntry->Before
470 or DriverEntry->After is set it will never be cleared.
472 @param DriverEntry DriverEntry element to update .
474 @retval EFI_SUCCESS It always works.
479 IN EFI_MM_DRIVER_ENTRY
*DriverEntry
484 Iterator
= DriverEntry
->Depex
;
485 DriverEntry
->Dependent
= TRUE
;
487 if (*Iterator
== EFI_DEP_BEFORE
) {
488 DriverEntry
->Before
= TRUE
;
489 } else if (*Iterator
== EFI_DEP_AFTER
) {
490 DriverEntry
->After
= TRUE
;
493 if (DriverEntry
->Before
|| DriverEntry
->After
) {
494 CopyMem (&DriverEntry
->BeforeAfterGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
501 Read Depex and pre-process the Depex for Before and After. If Section Extraction
502 protocol returns an error via ReadSection defer the reading of the Depex.
504 @param DriverEntry Driver to work on.
506 @retval EFI_SUCCESS Depex read and preprossesed
507 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
508 and Depex reading needs to be retried.
509 @retval Error DEPEX not found.
513 MmGetDepexSectionAndPreProccess (
514 IN EFI_MM_DRIVER_ENTRY
*DriverEntry
522 if (DriverEntry
->Depex
== NULL
) {
523 Status
= EFI_NOT_FOUND
;
525 Status
= EFI_SUCCESS
;
527 if (EFI_ERROR (Status
)) {
528 if (Status
== EFI_PROTOCOL_ERROR
) {
530 // The section extraction protocol failed so set protocol error flag
532 DriverEntry
->DepexProtocolError
= TRUE
;
535 // If no Depex assume depend on all architectural protocols
537 DriverEntry
->Depex
= NULL
;
538 DriverEntry
->Dependent
= TRUE
;
539 DriverEntry
->DepexProtocolError
= FALSE
;
543 // Set Before and After state information based on Depex
544 // Driver will be put in Dependent state
546 MmPreProcessDepex (DriverEntry
);
547 DriverEntry
->DepexProtocolError
= FALSE
;
554 This is the main Dispatcher for MM and it exits when there are no more
555 drivers to run. Drain the mScheduledQueue and load and start a PE
556 image for each driver. Search the mDiscoveredList to see if any driver can
557 be placed on the mScheduledQueue. If no drivers are placed on the
558 mScheduledQueue exit the function.
560 @retval EFI_SUCCESS All of the MM Drivers that could be dispatched
561 have been run and the MM Entry Point has been
563 @retval EFI_NOT_READY The MM Driver that registered the MM Entry Point
565 @retval EFI_NOT_FOUND There are no MM Drivers available to be dispatched.
566 @retval EFI_ALREADY_STARTED The MM Dispatcher is already running
576 EFI_MM_DRIVER_ENTRY
*DriverEntry
;
578 BOOLEAN PreviousMmEntryPointRegistered
;
580 DEBUG ((DEBUG_INFO
, "MmDispatcher\n"));
582 if (!gRequestDispatch
) {
583 DEBUG ((DEBUG_INFO
, " !gRequestDispatch\n"));
584 return EFI_NOT_FOUND
;
587 if (gDispatcherRunning
) {
588 DEBUG ((DEBUG_INFO
, " gDispatcherRunning\n"));
590 // If the dispatcher is running don't let it be restarted.
592 return EFI_ALREADY_STARTED
;
595 gDispatcherRunning
= TRUE
;
599 // Drain the Scheduled Queue
601 DEBUG ((DEBUG_INFO
, " Drain the Scheduled Queue\n"));
602 while (!IsListEmpty (&mScheduledQueue
)) {
604 mScheduledQueue
.ForwardLink
,
607 EFI_MM_DRIVER_ENTRY_SIGNATURE
609 DEBUG ((DEBUG_INFO
, " DriverEntry (Scheduled) - %g\n", &DriverEntry
->FileName
));
612 // Load the MM Driver image into memory. If the Driver was transitioned from
613 // Untrused to Scheduled it would have already been loaded so we may need to
614 // skip the LoadImage
616 if (DriverEntry
->ImageHandle
== NULL
) {
617 Status
= MmLoadImage (DriverEntry
);
620 // Update the driver state to reflect that it's been loaded
622 if (EFI_ERROR (Status
)) {
624 // The MM Driver could not be loaded, and do not attempt to load or start it again.
625 // Take driver from Scheduled to Initialized.
627 DriverEntry
->Initialized
= TRUE
;
628 DriverEntry
->Scheduled
= FALSE
;
629 RemoveEntryList (&DriverEntry
->ScheduledLink
);
632 // If it's an error don't try the StartImage
638 DriverEntry
->Scheduled
= FALSE
;
639 DriverEntry
->Initialized
= TRUE
;
640 RemoveEntryList (&DriverEntry
->ScheduledLink
);
643 // Cache state of MmEntryPointRegistered before calling entry point
645 PreviousMmEntryPointRegistered
= gMmCorePrivate
->MmEntryPointRegistered
;
648 // For each MM driver, pass NULL as ImageHandle
650 if (mEfiSystemTable
== NULL
) {
651 DEBUG ((DEBUG_INFO
, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry
->ImageEntryPoint
));
652 Status
= ((MM_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
) (DriverEntry
->ImageHandle
, &gMmCoreMmst
);
654 DEBUG ((DEBUG_INFO
, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry
->ImageEntryPoint
));
655 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)DriverEntry
->ImageEntryPoint
) (
656 DriverEntry
->ImageHandle
,
660 if (EFI_ERROR(Status
)) {
661 DEBUG ((DEBUG_INFO
, "StartImage Status - %r\n", Status
));
662 MmFreePages(DriverEntry
->ImageBuffer
, DriverEntry
->NumberOfPage
);
665 if (!PreviousMmEntryPointRegistered
&& gMmCorePrivate
->MmEntryPointRegistered
) {
667 // Return immediately if the MM Entry Point was registered by the MM
668 // Driver that was just dispatched. The MM IPL will reinvoke the MM
669 // Core Dispatcher. This is required so MM Mode may be enabled as soon
670 // as all the dependent MM Drivers for MM Mode have been dispatched.
671 // Once the MM Entry Point has been registered, then MM Mode will be
674 gRequestDispatch
= TRUE
;
675 gDispatcherRunning
= FALSE
;
676 return EFI_NOT_READY
;
681 // Search DriverList for items to place on Scheduled Queue
683 DEBUG ((DEBUG_INFO
, " Search DriverList for items to place on Scheduled Queue\n"));
685 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
686 DriverEntry
= CR (Link
, EFI_MM_DRIVER_ENTRY
, Link
, EFI_MM_DRIVER_ENTRY_SIGNATURE
);
687 DEBUG ((DEBUG_INFO
, " DriverEntry (Discovered) - %g\n", &DriverEntry
->FileName
));
689 if (DriverEntry
->DepexProtocolError
) {
691 // If Section Extraction Protocol did not let the Depex be read before retry the read
693 Status
= MmGetDepexSectionAndPreProccess (DriverEntry
);
696 if (DriverEntry
->Dependent
) {
697 if (MmIsSchedulable (DriverEntry
)) {
698 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
703 } while (ReadyToRun
);
706 // If there is no more MM driver to dispatch, stop the dispatch request
708 DEBUG ((DEBUG_INFO
, " no more MM driver to dispatch, stop the dispatch request\n"));
709 gRequestDispatch
= FALSE
;
710 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
711 DriverEntry
= CR (Link
, EFI_MM_DRIVER_ENTRY
, Link
, EFI_MM_DRIVER_ENTRY_SIGNATURE
);
712 DEBUG ((DEBUG_INFO
, " DriverEntry (Discovered) - %g\n", &DriverEntry
->FileName
));
714 if (!DriverEntry
->Initialized
) {
716 // We have MM driver pending to dispatch
718 gRequestDispatch
= TRUE
;
723 gDispatcherRunning
= FALSE
;
729 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
730 must add any driver with a before dependency on InsertedDriverEntry first.
731 You do this by recursively calling this routine. After all the Befores are
732 processed you can add InsertedDriverEntry to the mScheduledQueue.
733 Then you can add any driver with an After dependency on InsertedDriverEntry
734 by recursively calling this routine.
736 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
740 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
741 IN EFI_MM_DRIVER_ENTRY
*InsertedDriverEntry
745 EFI_MM_DRIVER_ENTRY
*DriverEntry
;
748 // Process Before Dependency
750 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
751 DriverEntry
= CR(Link
, EFI_MM_DRIVER_ENTRY
, Link
, EFI_MM_DRIVER_ENTRY_SIGNATURE
);
752 if (DriverEntry
->Before
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
753 DEBUG ((DEBUG_DISPATCH
, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
754 DEBUG ((DEBUG_DISPATCH
, " BEFORE FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
755 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
757 // Recursively process BEFORE
759 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
760 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
762 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
768 // Convert driver from Dependent to Scheduled state
771 InsertedDriverEntry
->Dependent
= FALSE
;
772 InsertedDriverEntry
->Scheduled
= TRUE
;
773 InsertTailList (&mScheduledQueue
, &InsertedDriverEntry
->ScheduledLink
);
777 // Process After Dependency
779 for (Link
= mDiscoveredList
.ForwardLink
; Link
!= &mDiscoveredList
; Link
= Link
->ForwardLink
) {
780 DriverEntry
= CR(Link
, EFI_MM_DRIVER_ENTRY
, Link
, EFI_MM_DRIVER_ENTRY_SIGNATURE
);
781 if (DriverEntry
->After
&& DriverEntry
->Dependent
&& DriverEntry
!= InsertedDriverEntry
) {
782 DEBUG ((DEBUG_DISPATCH
, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
783 DEBUG ((DEBUG_DISPATCH
, " AFTER FFS(%g) = ", &DriverEntry
->BeforeAfterGuid
));
784 if (CompareGuid (&InsertedDriverEntry
->FileName
, &DriverEntry
->BeforeAfterGuid
)) {
786 // Recursively process AFTER
788 DEBUG ((DEBUG_DISPATCH
, "TRUE\n END\n RESULT = TRUE\n"));
789 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry
);
791 DEBUG ((DEBUG_DISPATCH
, "FALSE\n END\n RESULT = FALSE\n"));
798 Return TRUE if the Fv has been processed, FALSE if not.
800 @param FvHandle The handle of a FV that's being tested
802 @retval TRUE Fv protocol on FvHandle has been processed
803 @retval FALSE Fv protocol on FvHandle has not yet been
809 IN EFI_HANDLE FvHandle
813 KNOWN_HANDLE
*KnownHandle
;
815 for (Link
= mFvHandleList
.ForwardLink
; Link
!= &mFvHandleList
; Link
= Link
->ForwardLink
) {
816 KnownHandle
= CR (Link
, KNOWN_HANDLE
, Link
, KNOWN_HANDLE_SIGNATURE
);
817 if (KnownHandle
->Handle
== FvHandle
) {
825 Remember that Fv protocol on FvHandle has had it's drivers placed on the
826 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
827 never removed/freed from the mFvHandleList.
829 @param FvHandle The handle of a FV that has been processed
833 FvIsBeingProcesssed (
834 IN EFI_HANDLE FvHandle
837 KNOWN_HANDLE
*KnownHandle
;
839 DEBUG ((DEBUG_INFO
, "FvIsBeingProcesssed - 0x%08x\n", FvHandle
));
841 KnownHandle
= AllocatePool (sizeof (KNOWN_HANDLE
));
842 ASSERT (KnownHandle
!= NULL
);
844 KnownHandle
->Signature
= KNOWN_HANDLE_SIGNATURE
;
845 KnownHandle
->Handle
= FvHandle
;
846 InsertTailList (&mFvHandleList
, &KnownHandle
->Link
);
850 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
851 and initilize any state variables. Read the Depex from the FV and store it
852 in DriverEntry. Pre-process the Depex to set the Before and After state.
853 The Discovered list is never free'ed and contains booleans that represent the
854 other possible MM driver states.
856 @param Fv Fv protocol, needed to read Depex info out of
858 @param FvHandle Handle for Fv, needed in the
859 EFI_MM_DRIVER_ENTRY so that the PE image can be
860 read out of the FV at a later time.
861 @param DriverName Name of driver to add to mDiscoveredList.
863 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
864 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
865 DriverName may be active in the system at any one
871 IN EFI_HANDLE FvHandle
,
873 IN UINTN Pe32DataSize
,
876 IN EFI_GUID
*DriverName
879 EFI_MM_DRIVER_ENTRY
*DriverEntry
;
881 DEBUG ((DEBUG_INFO
, "MmAddToDriverList - %g (0x%08x)\n", DriverName
, Pe32Data
));
884 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
887 DriverEntry
= AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY
));
888 ASSERT (DriverEntry
!= NULL
);
890 DriverEntry
->Signature
= EFI_MM_DRIVER_ENTRY_SIGNATURE
;
891 CopyGuid (&DriverEntry
->FileName
, DriverName
);
892 DriverEntry
->FvHandle
= FvHandle
;
893 DriverEntry
->Pe32Data
= Pe32Data
;
894 DriverEntry
->Pe32DataSize
= Pe32DataSize
;
895 DriverEntry
->Depex
= Depex
;
896 DriverEntry
->DepexSize
= DepexSize
;
898 MmGetDepexSectionAndPreProccess (DriverEntry
);
900 InsertTailList (&mDiscoveredList
, &DriverEntry
->Link
);
901 gRequestDispatch
= TRUE
;
907 This function is the main entry point for an MM handler dispatch
908 or communicate-based callback.
910 Event notification that is fired every time a FV dispatch protocol is added.
911 More than one protocol may have been added when this event is fired, so you
912 must loop on MmLocateHandle () to see how many protocols were added and
913 do the following to each FV:
914 If the Fv has already been processed, skip it. If the Fv has not been
915 processed then mark it as being processed, as we are about to process it.
916 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
917 mDiscoveredList is never free'ed and contains variables that define
918 the other states the MM driver transitions to..
919 While you are at it read the A Priori file into memory.
920 Place drivers in the A Priori list onto the mScheduledQueue.
922 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
923 @param Context Points to an optional handler context which was specified when the handler was registered.
924 @param CommBuffer A pointer to a collection of data in memory that will
925 be conveyed from a non-MM environment into an MM environment.
926 @param CommBufferSize The size of the CommBuffer.
933 MmDriverDispatchHandler (
934 IN EFI_HANDLE DispatchHandle
,
935 IN CONST VOID
*Context
, OPTIONAL
936 IN OUT VOID
*CommBuffer
, OPTIONAL
937 IN OUT UINTN
*CommBufferSize OPTIONAL
942 DEBUG ((DEBUG_INFO
, "MmDriverDispatchHandler\n"));
945 // Execute the MM Dispatcher on any newly discovered FVs and previously
946 // discovered MM drivers that have been discovered but not dispatched.
948 Status
= MmDispatcher ();
951 // Check to see if CommBuffer and CommBufferSize are valid
953 if (CommBuffer
!= NULL
&& CommBufferSize
!= NULL
) {
954 if (*CommBufferSize
> 0) {
955 if (Status
== EFI_NOT_READY
) {
957 // If a the MM Core Entry Point was just registered, then set flag to
958 // request the MM Dispatcher to be restarted.
960 *(UINT8
*)CommBuffer
= COMM_BUFFER_MM_DISPATCH_RESTART
;
961 } else if (!EFI_ERROR (Status
)) {
963 // Set the flag to show that the MM Dispatcher executed without errors
965 *(UINT8
*)CommBuffer
= COMM_BUFFER_MM_DISPATCH_SUCCESS
;
968 // Set the flag to show that the MM Dispatcher encountered an error
970 *(UINT8
*)CommBuffer
= COMM_BUFFER_MM_DISPATCH_ERROR
;
979 This function is the main entry point for an MM handler dispatch
980 or communicate-based callback.
982 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
983 @param Context Points to an optional handler context which was specified when the handler was registered.
984 @param CommBuffer A pointer to a collection of data in memory that will
985 be conveyed from a non-MM environment into an MM environment.
986 @param CommBufferSize The size of the CommBuffer.
993 MmFvDispatchHandler (
994 IN EFI_HANDLE DispatchHandle
,
995 IN CONST VOID
*Context
, OPTIONAL
996 IN OUT VOID
*CommBuffer
, OPTIONAL
997 IN OUT UINTN
*CommBufferSize OPTIONAL
1001 EFI_MM_COMMUNICATE_FV_DISPATCH_DATA
*CommunicationFvDispatchData
;
1002 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1004 DEBUG ((DEBUG_INFO
, "MmFvDispatchHandler\n"));
1006 CommunicationFvDispatchData
= CommBuffer
;
1008 DEBUG ((DEBUG_INFO
, " Dispatch - 0x%016lx - 0x%016lx\n", CommunicationFvDispatchData
->Address
,
1009 CommunicationFvDispatchData
->Size
));
1011 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)CommunicationFvDispatchData
->Address
;
1013 MmCoreFfsFindMmDriver (FwVolHeader
);
1016 // Execute the MM Dispatcher on any newly discovered FVs and previously
1017 // discovered MM drivers that have been discovered but not dispatched.
1019 Status
= MmDispatcher ();
1025 Traverse the discovered list for any drivers that were discovered but not loaded
1026 because the dependency experessions evaluated to false.
1030 MmDisplayDiscoveredNotDispatched (
1035 EFI_MM_DRIVER_ENTRY
*DriverEntry
;
1037 for (Link
= mDiscoveredList
.ForwardLink
;Link
!=&mDiscoveredList
; Link
= Link
->ForwardLink
) {
1038 DriverEntry
= CR (Link
, EFI_MM_DRIVER_ENTRY
, Link
, EFI_MM_DRIVER_ENTRY_SIGNATURE
);
1039 if (DriverEntry
->Dependent
) {
1040 DEBUG ((DEBUG_LOAD
, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry
->FileName
));