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