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