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