]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Core/Dispatcher.c
7e4bf5e940254978013b158f1d9ea7322ff02d3f
[mirror_edk2.git] / StandaloneMmPkg / Core / Dispatcher.c
1 /** @file
2 MM Driver Dispatcher.
3
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 added to the
8 mScheduledQueue. The mFwVolList is used to make sure a
9 FV is only processed once.
10
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.
15
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 Depexes and recursively
19 adds all Before Depexes. It then adds the item that was passed in
20 and then processess the After dependencies by recursively calling
21 the routine.
22
23 Dispatcher Rules:
24 The rules for the dispatcher are similar to the DXE dispatcher.
25
26 The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
27 is the state diagram for the DXE dispatcher
28
29 Depex - Dependency Expresion.
30
31 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
32 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
33 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.<BR>
34
35 SPDX-License-Identifier: BSD-2-Clause-Patent
36
37 **/
38
39 #include "StandaloneMmCore.h"
40
41 //
42 // MM Dispatcher Data structures
43 //
44 #define KNOWN_FWVOL_SIGNATURE SIGNATURE_32('k','n','o','w')
45
46 typedef struct {
47 UINTN Signature;
48 LIST_ENTRY Link; // mFwVolList
49 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
50 } KNOWN_FWVOL;
51
52 //
53 // Function Prototypes
54 //
55
56 EFI_STATUS
57 MmCoreFfsFindMmDriver (
58 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
59 );
60
61 /**
62 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
63 must add any driver with a before dependency on InsertedDriverEntry first.
64 You do this by recursively calling this routine. After all the Before Depexes
65 are processed you can add InsertedDriverEntry to the mScheduledQueue.
66 Then you can add any driver with an After dependency on InsertedDriverEntry
67 by recursively calling this routine.
68
69 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
70
71 **/
72 VOID
73 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
74 IN EFI_MM_DRIVER_ENTRY *InsertedDriverEntry
75 );
76
77 //
78 // The Driver List contains one copy of every driver that has been discovered.
79 // Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
80 //
81 LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
82
83 //
84 // Queue of drivers that are ready to dispatch. This queue is a subset of the
85 // mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
86 //
87 LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
88
89 //
90 // List of firmware volume headers whose containing firmware volumes have been
91 // parsed and added to the mFwDriverList.
92 //
93 LIST_ENTRY mFwVolList = INITIALIZE_LIST_HEAD_VARIABLE (mFwVolList);
94
95 //
96 // Flag for the MM Dispacher. TRUE if dispatcher is executing.
97 //
98 BOOLEAN gDispatcherRunning = FALSE;
99
100 //
101 // Flag for the MM Dispacher. TRUE if there is one or more MM drivers ready to be dispatched
102 //
103 BOOLEAN gRequestDispatch = FALSE;
104
105 //
106 // The global variable is defined for Loading modules at fixed address feature to track the MM code
107 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding
108 // memory page available or not.
109 //
110 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mMmCodeMemoryRangeUsageBitMap=NULL;
111
112 /**
113 To check memory usage bit map array to figure out if the memory range in which the image will be loaded
114 is available or not. If memory range is avaliable, the function will mark the corresponding bits to 1
115 which indicates the memory range is used. The function is only invoked when load modules at fixed address
116 feature is enabled.
117
118 @param ImageBase The base addres the image will be loaded at.
119 @param ImageSize The size of the image
120
121 @retval EFI_SUCCESS The memory range the image will be loaded in is available
122 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
123 **/
124 EFI_STATUS
125 CheckAndMarkFixLoadingMemoryUsageBitMap (
126 IN EFI_PHYSICAL_ADDRESS ImageBase,
127 IN UINTN ImageSize
128 )
129 {
130 UINT32 MmCodePageNumber;
131 UINT64 MmCodeSize;
132 EFI_PHYSICAL_ADDRESS MmCodeBase;
133 UINTN BaseOffsetPageNumber;
134 UINTN TopOffsetPageNumber;
135 UINTN Index;
136
137 //
138 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
139 //
140 MmCodePageNumber = 0;
141 MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);
142 MmCodeBase = gLoadModuleAtFixAddressMmramBase;
143
144 //
145 // If the memory usage bit map is not initialized, do it. Every bit in the array
146 // indicate the status of the corresponding memory page, available or not
147 //
148 if (mMmCodeMemoryRangeUsageBitMap == NULL) {
149 mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool (((MmCodePageNumber / 64) + 1) * sizeof (UINT64));
150 }
151
152 //
153 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
154 //
155 if (mMmCodeMemoryRangeUsageBitMap == NULL) {
156 return EFI_NOT_FOUND;
157 }
158
159 //
160 // see if the memory range for loading the image is in the MM code range.
161 //
162 if (MmCodeBase + MmCodeSize < ImageBase + ImageSize || MmCodeBase > ImageBase) {
163 return EFI_NOT_FOUND;
164 }
165
166 //
167 // Test if the memory is available or not.
168 //
169 BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES ((UINT32)(ImageBase - MmCodeBase));
170 TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES ((UINT32)(ImageBase + ImageSize - MmCodeBase));
171 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
172 if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64 (1, (Index % 64))) != 0) {
173 //
174 // This page is already used.
175 //
176 return EFI_NOT_FOUND;
177 }
178 }
179
180 //
181 // Being here means the memory range is available. So mark the bits for the memory range
182 //
183 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
184 mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64 (1, (Index % 64));
185 }
186 return EFI_SUCCESS;
187 }
188
189 /**
190 Get the fixed loading address from image header assigned by build tool. This function only be called
191 when Loading module at Fixed address feature enabled.
192
193 @param ImageContext Pointer to the image context structure that describes the PE/COFF
194 image that needs to be examined by this function.
195 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
196 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
197
198 **/
199 EFI_STATUS
200 GetPeCoffImageFixLoadingAssignedAddress(
201 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
202 )
203 {
204 UINTN SectionHeaderOffset;
205 EFI_STATUS Status;
206 EFI_IMAGE_SECTION_HEADER SectionHeader;
207 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
208 EFI_PHYSICAL_ADDRESS FixLoadingAddress;
209 UINT16 Index;
210 UINTN Size;
211 UINT16 NumberOfSections;
212 UINT64 ValueInSectionHeader;
213
214 FixLoadingAddress = 0;
215 Status = EFI_NOT_FOUND;
216
217 //
218 // Get PeHeader pointer
219 //
220 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
221 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset + sizeof (UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) +
222 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
223 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
224
225 //
226 // Get base address from the first section header that doesn't point to code section.
227 //
228 for (Index = 0; Index < NumberOfSections; Index++) {
229 //
230 // Read section header from file
231 //
232 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
233 Status = ImageContext->ImageRead (
234 ImageContext->Handle,
235 SectionHeaderOffset,
236 &Size,
237 &SectionHeader
238 );
239 if (EFI_ERROR (Status)) {
240 return Status;
241 }
242
243 Status = EFI_NOT_FOUND;
244
245 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
246 //
247 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields
248 // in the first section header that doesn't point to code section in image header. So there
249 // is an assumption that when the feature is enabled, if a module with a loading address
250 // assigned by tools, the PointerToRelocations & PointerToLineNumbers fields should not be
251 // Zero, or else, these 2 fields should be set to Zero
252 //
253 ValueInSectionHeader = ReadUnaligned64 ((UINT64*)&SectionHeader.PointerToRelocations);
254 if (ValueInSectionHeader != 0) {
255 //
256 // Found first section header that doesn't point to code section in which build tool saves the
257 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
258 //
259 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);
260 //
261 // Check if the memory range is available.
262 //
263 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
264 if (!EFI_ERROR(Status)) {
265 //
266 // The assigned address is valid. Return the specified loading address
267 //
268 ImageContext->ImageAddress = FixLoadingAddress;
269 }
270 }
271 break;
272 }
273 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
274 }
275 DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n",
276 FixLoadingAddress, Status));
277 return Status;
278 }
279 /**
280 Loads an EFI image into SMRAM.
281
282 @param DriverEntry EFI_MM_DRIVER_ENTRY instance
283
284 @return EFI_STATUS
285
286 **/
287 EFI_STATUS
288 EFIAPI
289 MmLoadImage (
290 IN OUT EFI_MM_DRIVER_ENTRY *DriverEntry
291 )
292 {
293 UINTN PageCount;
294 EFI_STATUS Status;
295 EFI_PHYSICAL_ADDRESS DstBuffer;
296 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
297
298 DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
299
300 Status = EFI_SUCCESS;
301
302 //
303 // Initialize ImageContext
304 //
305 ImageContext.Handle = DriverEntry->Pe32Data;
306 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
307
308 //
309 // Get information about the image being loaded
310 //
311 Status = PeCoffLoaderGetImageInfo (&ImageContext);
312 if (EFI_ERROR (Status)) {
313 return Status;
314 }
315
316 PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
317 DstBuffer = (UINTN)(-1);
318
319 Status = MmAllocatePages (
320 AllocateMaxAddress,
321 EfiRuntimeServicesCode,
322 PageCount,
323 &DstBuffer
324 );
325 if (EFI_ERROR (Status)) {
326 return Status;
327 }
328
329 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
330
331 //
332 // Align buffer on section boundary
333 //
334 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
335 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
336
337 //
338 // Load the image to our new buffer
339 //
340 Status = PeCoffLoaderLoadImage (&ImageContext);
341 if (EFI_ERROR (Status)) {
342 MmFreePages (DstBuffer, PageCount);
343 return Status;
344 }
345
346 //
347 // Relocate the image in our new buffer
348 //
349 Status = PeCoffLoaderRelocateImage (&ImageContext);
350 if (EFI_ERROR (Status)) {
351 MmFreePages (DstBuffer, PageCount);
352 return Status;
353 }
354
355 //
356 // Flush the instruction cache so the image data are written before we execute it
357 //
358 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
359
360 //
361 // Save Image EntryPoint in DriverEntry
362 //
363 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;
364 DriverEntry->ImageBuffer = DstBuffer;
365 DriverEntry->NumberOfPage = PageCount;
366
367 if (mEfiSystemTable != NULL) {
368 Status = mEfiSystemTable->BootServices->AllocatePool (
369 EfiBootServicesData,
370 sizeof (EFI_LOADED_IMAGE_PROTOCOL),
371 (VOID **)&DriverEntry->LoadedImage
372 );
373 if (EFI_ERROR (Status)) {
374 MmFreePages (DstBuffer, PageCount);
375 return Status;
376 }
377
378 ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
379 //
380 // Fill in the remaining fields of the Loaded Image Protocol instance.
381 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
382 //
383 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
384 DriverEntry->LoadedImage->ParentHandle = NULL;
385 DriverEntry->LoadedImage->SystemTable = mEfiSystemTable;
386 DriverEntry->LoadedImage->DeviceHandle = NULL;
387 DriverEntry->LoadedImage->FilePath = NULL;
388
389 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;
390 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;
391 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
392 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
393
394 //
395 // Create a new image handle in the UEFI handle database for the MM Driver
396 //
397 DriverEntry->ImageHandle = NULL;
398 Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (
399 &DriverEntry->ImageHandle,
400 &gEfiLoadedImageProtocolGuid,
401 DriverEntry->LoadedImage,
402 NULL
403 );
404 }
405
406 //
407 // Print the load address and the PDB file name if it is available
408 //
409 DEBUG_CODE_BEGIN ();
410
411 UINTN Index;
412 UINTN StartIndex;
413 CHAR8 EfiFileName[256];
414
415 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
416 "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
417 (VOID *)(UINTN) ImageContext.ImageAddress,
418 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
419
420 //
421 // Print Module Name by Pdb file path.
422 // Windows and Unix style file path are all trimmed correctly.
423 //
424 if (ImageContext.PdbPointer != NULL) {
425 StartIndex = 0;
426 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
427 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
428 StartIndex = Index + 1;
429 }
430 }
431
432 //
433 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
434 // The PDB file name is limited in the range of 0~255.
435 // If the length is bigger than 255, trim the redundant characters to avoid overflow in array boundary.
436 //
437 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
438 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
439 if (EfiFileName[Index] == 0) {
440 EfiFileName[Index] = '.';
441 }
442 if (EfiFileName[Index] == '.') {
443 EfiFileName[Index + 1] = 'e';
444 EfiFileName[Index + 2] = 'f';
445 EfiFileName[Index + 3] = 'i';
446 EfiFileName[Index + 4] = 0;
447 break;
448 }
449 }
450
451 if (Index == sizeof (EfiFileName) - 4) {
452 EfiFileName[Index] = 0;
453 }
454 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName));
455 }
456 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
457
458 DEBUG_CODE_END ();
459
460 return Status;
461 }
462
463 /**
464 Preprocess dependency expression and update DriverEntry to reflect the
465 state of Before and After dependencies. If DriverEntry->Before
466 or DriverEntry->After is set it will never be cleared.
467
468 @param DriverEntry DriverEntry element to update .
469
470 @retval EFI_SUCCESS It always works.
471
472 **/
473 EFI_STATUS
474 MmPreProcessDepex (
475 IN EFI_MM_DRIVER_ENTRY *DriverEntry
476 )
477 {
478 UINT8 *Iterator;
479
480 Iterator = DriverEntry->Depex;
481 DriverEntry->Dependent = TRUE;
482
483 if (*Iterator == EFI_DEP_BEFORE) {
484 DriverEntry->Before = TRUE;
485 } else if (*Iterator == EFI_DEP_AFTER) {
486 DriverEntry->After = TRUE;
487 }
488
489 if (DriverEntry->Before || DriverEntry->After) {
490 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
491 }
492
493 return EFI_SUCCESS;
494 }
495
496 /**
497 Read Depex and pre-process the Depex for Before and After. If Section Extraction
498 protocol returns an error via ReadSection defer the reading of the Depex.
499
500 @param DriverEntry Driver to work on.
501
502 @retval EFI_SUCCESS Depex read and preprossesed
503 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
504 and Depex reading needs to be retried.
505 @retval Error DEPEX not found.
506
507 **/
508 EFI_STATUS
509 MmGetDepexSectionAndPreProccess (
510 IN EFI_MM_DRIVER_ENTRY *DriverEntry
511 )
512 {
513 EFI_STATUS Status;
514
515 //
516 // Data already read
517 //
518 if (DriverEntry->Depex == NULL) {
519 Status = EFI_NOT_FOUND;
520 } else {
521 Status = EFI_SUCCESS;
522 }
523 if (EFI_ERROR (Status)) {
524 if (Status == EFI_PROTOCOL_ERROR) {
525 //
526 // The section extraction protocol failed so set protocol error flag
527 //
528 DriverEntry->DepexProtocolError = TRUE;
529 } else {
530 //
531 // If no Depex assume depend on all architectural protocols
532 //
533 DriverEntry->Depex = NULL;
534 DriverEntry->Dependent = TRUE;
535 DriverEntry->DepexProtocolError = FALSE;
536 }
537 } else {
538 //
539 // Set Before and After state information based on Depex
540 // Driver will be put in Dependent state
541 //
542 MmPreProcessDepex (DriverEntry);
543 DriverEntry->DepexProtocolError = FALSE;
544 }
545
546 return Status;
547 }
548
549 /**
550 This is the main Dispatcher for MM and it exits when there are no more
551 drivers to run. Drain the mScheduledQueue and load and start a PE
552 image for each driver. Search the mDiscoveredList to see if any driver can
553 be placed on the mScheduledQueue. If no drivers are placed on the
554 mScheduledQueue exit the function.
555
556 @retval EFI_SUCCESS All of the MM Drivers that could be dispatched
557 have been run and the MM Entry Point has been
558 registered.
559 @retval EFI_NOT_READY The MM Driver that registered the MM Entry Point
560 was just dispatched.
561 @retval EFI_NOT_FOUND There are no MM Drivers available to be dispatched.
562 @retval EFI_ALREADY_STARTED The MM Dispatcher is already running
563
564 **/
565 EFI_STATUS
566 MmDispatcher (
567 VOID
568 )
569 {
570 EFI_STATUS Status;
571 LIST_ENTRY *Link;
572 EFI_MM_DRIVER_ENTRY *DriverEntry;
573 BOOLEAN ReadyToRun;
574
575 DEBUG ((DEBUG_INFO, "MmDispatcher\n"));
576
577 if (!gRequestDispatch) {
578 DEBUG ((DEBUG_INFO, " !gRequestDispatch\n"));
579 return EFI_NOT_FOUND;
580 }
581
582 if (gDispatcherRunning) {
583 DEBUG ((DEBUG_INFO, " gDispatcherRunning\n"));
584 //
585 // If the dispatcher is running don't let it be restarted.
586 //
587 return EFI_ALREADY_STARTED;
588 }
589
590 gDispatcherRunning = TRUE;
591
592 do {
593 //
594 // Drain the Scheduled Queue
595 //
596 DEBUG ((DEBUG_INFO, " Drain the Scheduled Queue\n"));
597 while (!IsListEmpty (&mScheduledQueue)) {
598 DriverEntry = CR (
599 mScheduledQueue.ForwardLink,
600 EFI_MM_DRIVER_ENTRY,
601 ScheduledLink,
602 EFI_MM_DRIVER_ENTRY_SIGNATURE
603 );
604 DEBUG ((DEBUG_INFO, " DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));
605
606 //
607 // Load the MM Driver image into memory. If the Driver was transitioned from
608 // Untrusted to Scheduled it would have already been loaded so we may need to
609 // skip the LoadImage
610 //
611 if (DriverEntry->ImageHandle == NULL) {
612 Status = MmLoadImage (DriverEntry);
613
614 //
615 // Update the driver state to reflect that it's been loaded
616 //
617 if (EFI_ERROR (Status)) {
618 //
619 // The MM Driver could not be loaded, and do not attempt to load or start it again.
620 // Take driver from Scheduled to Initialized.
621 //
622 DriverEntry->Initialized = TRUE;
623 DriverEntry->Scheduled = FALSE;
624 RemoveEntryList (&DriverEntry->ScheduledLink);
625
626 //
627 // If it's an error don't try the StartImage
628 //
629 continue;
630 }
631 }
632
633 DriverEntry->Scheduled = FALSE;
634 DriverEntry->Initialized = TRUE;
635 RemoveEntryList (&DriverEntry->ScheduledLink);
636
637 //
638 // For each MM driver, pass NULL as ImageHandle
639 //
640 if (mEfiSystemTable == NULL) {
641 DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));
642 Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint) (DriverEntry->ImageHandle, &gMmCoreMmst);
643 } else {
644 DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));
645 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint) (
646 DriverEntry->ImageHandle,
647 mEfiSystemTable
648 );
649 }
650 if (EFI_ERROR(Status)) {
651 DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));
652 MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
653 }
654 }
655
656 //
657 // Search DriverList for items to place on Scheduled Queue
658 //
659 DEBUG ((DEBUG_INFO, " Search DriverList for items to place on Scheduled Queue\n"));
660 ReadyToRun = FALSE;
661 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
662 DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
663 DEBUG ((DEBUG_INFO, " DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
664
665 if (DriverEntry->DepexProtocolError) {
666 //
667 // If Section Extraction Protocol did not let the Depex be read before retry the read
668 //
669 Status = MmGetDepexSectionAndPreProccess (DriverEntry);
670 }
671
672 if (DriverEntry->Dependent) {
673 if (MmIsSchedulable (DriverEntry)) {
674 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
675 ReadyToRun = TRUE;
676 }
677 }
678 }
679 } while (ReadyToRun);
680
681 //
682 // If there is no more MM driver to dispatch, stop the dispatch request
683 //
684 DEBUG ((DEBUG_INFO, " no more MM driver to dispatch, stop the dispatch request\n"));
685 gRequestDispatch = FALSE;
686 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
687 DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
688 DEBUG ((DEBUG_INFO, " DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
689
690 if (!DriverEntry->Initialized) {
691 //
692 // We have MM driver pending to dispatch
693 //
694 gRequestDispatch = TRUE;
695 break;
696 }
697 }
698
699 gDispatcherRunning = FALSE;
700
701 return EFI_SUCCESS;
702 }
703
704 /**
705 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
706 must add any driver with a before dependency on InsertedDriverEntry first.
707 You do this by recursively calling this routine. After all the Before Depexes
708 are processed you can add InsertedDriverEntry to the mScheduledQueue.
709 Then you can add any driver with an After dependency on InsertedDriverEntry
710 by recursively calling this routine.
711
712 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
713
714 **/
715 VOID
716 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
717 IN EFI_MM_DRIVER_ENTRY *InsertedDriverEntry
718 )
719 {
720 LIST_ENTRY *Link;
721 EFI_MM_DRIVER_ENTRY *DriverEntry;
722
723 //
724 // Process Before Dependency
725 //
726 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
727 DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
728 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
729 DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
730 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
731 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
732 //
733 // Recursively process BEFORE
734 //
735 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
736 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
737 } else {
738 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
739 }
740 }
741 }
742
743 //
744 // Convert driver from Dependent to Scheduled state
745 //
746
747 InsertedDriverEntry->Dependent = FALSE;
748 InsertedDriverEntry->Scheduled = TRUE;
749 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
750
751
752 //
753 // Process After Dependency
754 //
755 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
756 DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
757 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
758 DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
759 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
760 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
761 //
762 // Recursively process AFTER
763 //
764 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
765 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
766 } else {
767 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
768 }
769 }
770 }
771 }
772
773 /**
774 Return TRUE if the firmware volume has been processed, FALSE if not.
775
776 @param FwVolHeader The header of the firmware volume that's being
777 tested.
778
779 @retval TRUE The firmware volume denoted by FwVolHeader has
780 been processed
781 @retval FALSE The firmware volume denoted by FwVolHeader has
782 not yet been processed
783
784 **/
785 BOOLEAN
786 FvHasBeenProcessed (
787 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
788 )
789 {
790 LIST_ENTRY *Link;
791 KNOWN_FWVOL *KnownFwVol;
792
793 for (Link = mFwVolList.ForwardLink;
794 Link != &mFwVolList;
795 Link = Link->ForwardLink) {
796 KnownFwVol = CR (Link, KNOWN_FWVOL, Link, KNOWN_FWVOL_SIGNATURE);
797 if (KnownFwVol->FwVolHeader == FwVolHeader) {
798 return TRUE;
799 }
800 }
801 return FALSE;
802 }
803
804 /**
805 Remember that the firmware volume denoted by FwVolHeader has had its drivers
806 placed on mDiscoveredList. This function adds entries to mFwVolList. Items
807 are never removed/freed from mFwVolList.
808
809 @param FwVolHeader The header of the firmware volume that's being
810 processed.
811
812 **/
813 VOID
814 FvIsBeingProcessed (
815 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
816 )
817 {
818 KNOWN_FWVOL *KnownFwVol;
819
820 DEBUG ((DEBUG_INFO, "FvIsBeingProcessed - 0x%08x\n", FwVolHeader));
821
822 KnownFwVol = AllocatePool (sizeof (KNOWN_FWVOL));
823 ASSERT (KnownFwVol != NULL);
824
825 KnownFwVol->Signature = KNOWN_FWVOL_SIGNATURE;
826 KnownFwVol->FwVolHeader = FwVolHeader;
827 InsertTailList (&mFwVolList, &KnownFwVol->Link);
828 }
829
830 /**
831 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
832 and initialise any state variables. Read the Depex from the FV and store it
833 in DriverEntry. Pre-process the Depex to set the Before and After state.
834 The Discovered list is never freed and contains booleans that represent the
835 other possible MM driver states.
836
837 @param [in] FwVolHeader Pointer to the formware volume header.
838 @param [in] Pe32Data Pointer to the PE data.
839 @param [in] Pe32DataSize Size of the PE data.
840 @param [in] Depex Pointer to the Depex info.
841 @param [in] DepexSize Size of the Depex info.
842 @param [in] DriverName Name of driver to add to mDiscoveredList.
843
844 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
845 **/
846 EFI_STATUS
847 MmAddToDriverList (
848 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
849 IN VOID *Pe32Data,
850 IN UINTN Pe32DataSize,
851 IN VOID *Depex,
852 IN UINTN DepexSize,
853 IN EFI_GUID *DriverName
854 )
855 {
856 EFI_MM_DRIVER_ENTRY *DriverEntry;
857
858 DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));
859
860 //
861 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
862 // NULL or FALSE.
863 //
864 DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));
865 ASSERT (DriverEntry != NULL);
866
867 DriverEntry->Signature = EFI_MM_DRIVER_ENTRY_SIGNATURE;
868 CopyGuid (&DriverEntry->FileName, DriverName);
869 DriverEntry->FwVolHeader = FwVolHeader;
870 DriverEntry->Pe32Data = Pe32Data;
871 DriverEntry->Pe32DataSize = Pe32DataSize;
872 DriverEntry->Depex = Depex;
873 DriverEntry->DepexSize = DepexSize;
874
875 MmGetDepexSectionAndPreProccess (DriverEntry);
876
877 InsertTailList (&mDiscoveredList, &DriverEntry->Link);
878 gRequestDispatch = TRUE;
879
880 return EFI_SUCCESS;
881 }
882
883 /**
884 Traverse the discovered list for any drivers that were discovered but not loaded
885 because the dependency expressions evaluated to false.
886
887 **/
888 VOID
889 MmDisplayDiscoveredNotDispatched (
890 VOID
891 )
892 {
893 LIST_ENTRY *Link;
894 EFI_MM_DRIVER_ENTRY *DriverEntry;
895
896 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
897 DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
898 if (DriverEntry->Dependent) {
899 DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
900 }
901 }
902 }