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