]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
Add PI SMM IPL and PI SMM Core
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Dispatcher.c
1 /** @file
2 SMM 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 SOR, 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 mFvHandleList 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 SOR - Schedule On Request - Don't schedule if this bit is set.
30
31 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
32 This program and the accompanying materials are licensed and made available
33 under the terms and conditions of the BSD License which accompanies this
34 distribution. The full text of the license may be found at
35 http://opensource.org/licenses/bsd-license.php
36
37 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
39
40 **/
41
42 #include "PiSmmCore.h"
43
44 //
45 // SMM Dispatcher Data structures
46 //
47 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
48 typedef struct {
49 UINTN Signature;
50 LIST_ENTRY Link; // mFvHandleList
51 EFI_HANDLE Handle;
52 } KNOWN_HANDLE;
53
54 //
55 // Function Prototypes
56 //
57
58 /**
59 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
60 must add any driver with a before dependency on InsertedDriverEntry first.
61 You do this by recursively calling this routine. After all the Befores are
62 processed you can add InsertedDriverEntry to the mScheduledQueue.
63 Then you can add any driver with an After dependency on InsertedDriverEntry
64 by recursively calling this routine.
65
66 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
67
68 **/
69 VOID
70 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
71 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
72 );
73
74 //
75 // The Driver List contains one copy of every driver that has been discovered.
76 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
77 //
78 LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
79
80 //
81 // Queue of drivers that are ready to dispatch. This queue is a subset of the
82 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
83 //
84 LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
85
86 //
87 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
88 //
89 LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
90
91 //
92 // Flag for the SMM Dispacher. TRUE if dispatcher is execuing.
93 //
94 BOOLEAN gDispatcherRunning = FALSE;
95
96 //
97 // Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched
98 //
99 BOOLEAN gRequestDispatch = FALSE;
100
101 //
102 // List of file types supported by dispatcher
103 //
104 EFI_FV_FILETYPE mSmmFileTypes[] = {
105 EFI_FV_FILETYPE_SMM,
106 EFI_FV_FILETYPE_COMBINED_SMM_DXE
107 //
108 // Note: DXE core will process the FV image file, so skip it in SMM core
109 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
110 //
111 };
112
113 typedef struct {
114 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File;
115 EFI_DEVICE_PATH_PROTOCOL End;
116 } FV_FILEPATH_DEVICE_PATH;
117
118 FV_FILEPATH_DEVICE_PATH mFvDevicePath;
119
120 //
121 // DXE Architecture Protocols
122 //
123 EFI_SECURITY_ARCH_PROTOCOL *mSecurity = NULL;
124
125 /**
126 Loads an EFI image into SMRAM.
127
128 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
129
130 @return EFI_STATUS
131
132 **/
133 EFI_STATUS
134 EFIAPI
135 SmmLoadImage (
136 IN OUT EFI_SMM_DRIVER_ENTRY *DriverEntry
137 )
138 {
139 UINT32 AuthenticationStatus;
140 UINTN FilePathSize;
141 VOID *Buffer;
142 UINTN Size;
143 UINTN PageCount;
144 EFI_GUID *NameGuid;
145 EFI_STATUS Status;
146 EFI_STATUS SecurityStatus;
147 EFI_HANDLE DeviceHandle;
148 EFI_PHYSICAL_ADDRESS DstBuffer;
149 EFI_DEVICE_PATH_PROTOCOL *FilePath;
150 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
151 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
152 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
153 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
154
155 Buffer = NULL;
156 Size = 0;
157 Fv = DriverEntry->Fv;
158 NameGuid = &DriverEntry->FileName;
159 FilePath = DriverEntry->FvFileDevicePath;
160
161 OriginalFilePath = FilePath;
162 HandleFilePath = FilePath;
163 DeviceHandle = NULL;
164 SecurityStatus = EFI_SUCCESS;
165 Status = EFI_SUCCESS;
166 AuthenticationStatus = 0;
167
168 //
169 // Try to get the image device handle by checking the match protocol.
170 //
171 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);
172 if (EFI_ERROR(Status)) {
173 return Status;
174 }
175
176 //
177 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
178 //
179 if (mSecurity == NULL) {
180 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
181 }
182
183 //
184 // Verify the Authentication Status through the Security Architectural Protocol
185 //
186 if ((mSecurity != NULL) && (OriginalFilePath != NULL)) {
187 SecurityStatus = mSecurity->FileAuthenticationState (
188 mSecurity,
189 AuthenticationStatus,
190 OriginalFilePath
191 );
192 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
193 Status = SecurityStatus;
194 return Status;
195 }
196 }
197
198 //
199 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
200 //
201 FilePath = OriginalFilePath;
202 Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
203 if (!EFI_ERROR (Status)) {
204 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
205 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );
206 }
207
208 //
209 // Try reading PE32 section firstly
210 //
211 Status = Fv->ReadSection (
212 Fv,
213 NameGuid,
214 EFI_SECTION_PE32,
215 0,
216 &Buffer,
217 &Size,
218 &AuthenticationStatus
219 );
220
221 if (EFI_ERROR (Status)) {
222 //
223 // Try reading TE section secondly
224 //
225 Buffer = NULL;
226 Size = 0;
227 Status = Fv->ReadSection (
228 Fv,
229 NameGuid,
230 EFI_SECTION_TE,
231 0,
232 &Buffer,
233 &Size,
234 &AuthenticationStatus
235 );
236 }
237
238 if (EFI_ERROR (Status)) {
239 if (Buffer != NULL) {
240 Status = gBS->FreePool (Buffer);
241 }
242 return Status;
243 }
244
245 //
246 // Initialize ImageContext
247 //
248 ImageContext.Handle = Buffer;
249 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
250
251 //
252 // Get information about the image being loaded
253 //
254 Status = PeCoffLoaderGetImageInfo (&ImageContext);
255 if (EFI_ERROR (Status)) {
256 if (Buffer != NULL) {
257 Status = gBS->FreePool (Buffer);
258 }
259 return Status;
260 }
261
262 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
263 DstBuffer = (UINTN)(-1);
264
265 Status = SmmAllocatePages (
266 AllocateMaxAddress,
267 EfiRuntimeServicesCode,
268 PageCount,
269 &DstBuffer
270 );
271 if (EFI_ERROR (Status)) {
272 if (Buffer != NULL) {
273 Status = gBS->FreePool (Buffer);
274 }
275 return Status;
276 }
277
278 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
279 //
280 // Align buffer on section boundry
281 //
282 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
283 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
284
285 //
286 // Load the image to our new buffer
287 //
288 Status = PeCoffLoaderLoadImage (&ImageContext);
289 if (EFI_ERROR (Status)) {
290 if (Buffer != NULL) {
291 Status = gBS->FreePool (Buffer);
292 }
293 SmmFreePages (DstBuffer, PageCount);
294 return Status;
295 }
296
297 //
298 // Relocate the image in our new buffer
299 //
300 Status = PeCoffLoaderRelocateImage (&ImageContext);
301 if (EFI_ERROR (Status)) {
302 if (Buffer != NULL) {
303 Status = gBS->FreePool (Buffer);
304 }
305 SmmFreePages (DstBuffer, PageCount);
306 return Status;
307 }
308
309 //
310 // Flush the instruction cache so the image data are written before we execute it
311 //
312 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
313
314 //
315 // Save Image EntryPoint in DriverEntry
316 //
317 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;
318 DriverEntry->ImageBuffer = DstBuffer;
319 DriverEntry->NumberOfPage = PageCount;
320
321 //
322 // Allocate a Loaded Image Protocol in EfiBootServicesData
323 //
324 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
325 if (EFI_ERROR (Status)) {
326 if (Buffer != NULL) {
327 Status = gBS->FreePool (Buffer);
328 }
329 SmmFreePages (DstBuffer, PageCount);
330 return Status;
331 }
332
333 //
334 // Fill in the remaining fields of the Loaded Image Protocol instance.
335 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
336 //
337 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
338 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
339 DriverEntry->LoadedImage->SystemTable = gST;
340 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;
341
342 //
343 // Make an EfiBootServicesData buffer copy of FilePath
344 //
345 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);
346 if (EFI_ERROR (Status)) {
347 if (Buffer != NULL) {
348 Status = gBS->FreePool (Buffer);
349 }
350 SmmFreePages (DstBuffer, PageCount);
351 return Status;
352 }
353 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));
354
355 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;
356 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;
357 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
358 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
359
360 //
361 // Create a new image handle in the UEFI handle database for the SMM Driver
362 //
363 DriverEntry->ImageHandle = NULL;
364 Status = gBS->InstallMultipleProtocolInterfaces (
365 &DriverEntry->ImageHandle,
366 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
367 NULL
368 );
369
370 //
371 // Print the load address and the PDB file name if it is available
372 //
373
374 DEBUG_CODE_BEGIN ();
375
376 UINTN Index;
377 UINTN StartIndex;
378 CHAR8 EfiFileName[256];
379
380
381 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
382 "Loading driver at 0x%11p EntryPoint=0x%11p ",
383 (VOID *)(UINTN) ImageContext.ImageAddress,
384 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
385
386
387 //
388 // Print Module Name by Pdb file path.
389 // Windows and Unix style file path are all trimmed correctly.
390 //
391 if (ImageContext.PdbPointer != NULL) {
392 StartIndex = 0;
393 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
394 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
395 StartIndex = Index + 1;
396 }
397 }
398 //
399 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
400 // The PDB file name is limited in the range of 0~255.
401 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
402 //
403 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
404 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
405 if (EfiFileName[Index] == 0) {
406 EfiFileName[Index] = '.';
407 }
408 if (EfiFileName[Index] == '.') {
409 EfiFileName[Index + 1] = 'e';
410 EfiFileName[Index + 2] = 'f';
411 EfiFileName[Index + 3] = 'i';
412 EfiFileName[Index + 4] = 0;
413 break;
414 }
415 }
416
417 if (Index == sizeof (EfiFileName) - 4) {
418 EfiFileName[Index] = 0;
419 }
420 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
421 }
422 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
423
424 DEBUG_CODE_END ();
425
426 //
427 // Free buffer allocated by Fv->ReadSection.
428 //
429 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
430 // used the UEFI Boot Services AllocatePool() function
431 //
432 Status = gBS->FreePool(Buffer);
433 return Status;
434 }
435
436 /**
437 Preprocess dependency expression and update DriverEntry to reflect the
438 state of Before, After, and SOR dependencies. If DriverEntry->Before
439 or DriverEntry->After is set it will never be cleared. If SOR is set
440 it will be cleared by SmmSchedule(), and then the driver can be
441 dispatched.
442
443 @param DriverEntry DriverEntry element to update .
444
445 @retval EFI_SUCCESS It always works.
446
447 **/
448 EFI_STATUS
449 SmmPreProcessDepex (
450 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
451 )
452 {
453 UINT8 *Iterator;
454
455 Iterator = DriverEntry->Depex;
456 if (*Iterator == EFI_DEP_SOR) {
457 DriverEntry->Unrequested = TRUE;
458 } else {
459 DriverEntry->Dependent = TRUE;
460 }
461
462 if (*Iterator == EFI_DEP_BEFORE) {
463 DriverEntry->Before = TRUE;
464 } else if (*Iterator == EFI_DEP_AFTER) {
465 DriverEntry->After = TRUE;
466 }
467
468 if (DriverEntry->Before || DriverEntry->After) {
469 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
470 }
471
472 return EFI_SUCCESS;
473 }
474
475 /**
476 Read Depex and pre-process the Depex for Before and After. If Section Extraction
477 protocol returns an error via ReadSection defer the reading of the Depex.
478
479 @param DriverEntry Driver to work on.
480
481 @retval EFI_SUCCESS Depex read and preprossesed
482 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
483 and Depex reading needs to be retried.
484 @retval Error DEPEX not found.
485
486 **/
487 EFI_STATUS
488 SmmGetDepexSectionAndPreProccess (
489 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
490 )
491 {
492 EFI_STATUS Status;
493 EFI_SECTION_TYPE SectionType;
494 UINT32 AuthenticationStatus;
495 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
496
497 Fv = DriverEntry->Fv;
498
499 //
500 // Grab Depex info, it will never be free'ed.
501 // (Note: DriverEntry->Depex is in DXE memory)
502 //
503 SectionType = EFI_SECTION_SMM_DEPEX;
504 Status = Fv->ReadSection (
505 DriverEntry->Fv,
506 &DriverEntry->FileName,
507 SectionType,
508 0,
509 &DriverEntry->Depex,
510 (UINTN *)&DriverEntry->DepexSize,
511 &AuthenticationStatus
512 );
513 if (EFI_ERROR (Status)) {
514 if (Status == EFI_PROTOCOL_ERROR) {
515 //
516 // The section extraction protocol failed so set protocol error flag
517 //
518 DriverEntry->DepexProtocolError = TRUE;
519 } else {
520 //
521 // If no Depex assume depend on all architectural protocols
522 //
523 DriverEntry->Depex = NULL;
524 DriverEntry->Dependent = TRUE;
525 DriverEntry->DepexProtocolError = FALSE;
526 }
527 } else {
528 //
529 // Set Before, After, and Unrequested state information based on Depex
530 // Driver will be put in Dependent or Unrequested state
531 //
532 SmmPreProcessDepex (DriverEntry);
533 DriverEntry->DepexProtocolError = FALSE;
534 }
535
536 return Status;
537 }
538
539 /**
540 Check every driver and locate a matching one. If the driver is found, the Unrequested
541 state flag is cleared.
542
543 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
544 the firmware file specified by DriverName.
545 @param DriverName The Driver name to put in the Dependent state.
546
547 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
548 cleared
549 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
550 not set.
551
552 **/
553 EFI_STATUS
554 SmmSchedule (
555 IN EFI_HANDLE FirmwareVolumeHandle,
556 IN EFI_GUID *DriverName
557 )
558 {
559 LIST_ENTRY *Link;
560 EFI_SMM_DRIVER_ENTRY *DriverEntry;
561
562 //
563 // Check every driver
564 //
565 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
566 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
567 if (DriverEntry->FvHandle == FirmwareVolumeHandle &&
568 DriverEntry->Unrequested &&
569 CompareGuid (DriverName, &DriverEntry->FileName)) {
570 //
571 // Move the driver from the Unrequested to the Dependent state
572 //
573 DriverEntry->Unrequested = FALSE;
574 DriverEntry->Dependent = TRUE;
575
576 return EFI_SUCCESS;
577 }
578 }
579 return EFI_NOT_FOUND;
580 }
581
582 /**
583 This is the main Dispatcher for SMM and it exits when there are no more
584 drivers to run. Drain the mScheduledQueue and load and start a PE
585 image for each driver. Search the mDiscoveredList to see if any driver can
586 be placed on the mScheduledQueue. If no drivers are placed on the
587 mScheduledQueue exit the function. On exit it is assumed the Bds()
588 will be called, and when the Bds() exits the Dispatcher will be called
589 again.
590
591 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
592 @retval EFI_NOT_FOUND No SMM Drivers were dispatched
593 @retval EFI_SUCCESS One or more SMM Drivers were dispatched
594
595 **/
596 EFI_STATUS
597 SmmDispatcher (
598 VOID
599 )
600 {
601 EFI_STATUS Status;
602 EFI_STATUS ReturnStatus;
603 LIST_ENTRY *Link;
604 EFI_SMM_DRIVER_ENTRY *DriverEntry;
605 BOOLEAN ReadyToRun;
606
607 if (!gRequestDispatch) {
608 return EFI_NOT_FOUND;
609 }
610
611 if (gDispatcherRunning) {
612 //
613 // If the dispatcher is running don't let it be restarted.
614 //
615 return EFI_ALREADY_STARTED;
616 }
617
618 gDispatcherRunning = TRUE;
619
620 ReturnStatus = EFI_NOT_FOUND;
621 do {
622 //
623 // Drain the Scheduled Queue
624 //
625 while (!IsListEmpty (&mScheduledQueue)) {
626 DriverEntry = CR (
627 mScheduledQueue.ForwardLink,
628 EFI_SMM_DRIVER_ENTRY,
629 ScheduledLink,
630 EFI_SMM_DRIVER_ENTRY_SIGNATURE
631 );
632
633 //
634 // Load the SMM Driver image into memory. If the Driver was transitioned from
635 // Untrused to Scheduled it would have already been loaded so we may need to
636 // skip the LoadImage
637 //
638 if (DriverEntry->ImageHandle == NULL) {
639 Status = SmmLoadImage (DriverEntry);
640
641 //
642 // Update the driver state to reflect that it's been loaded
643 //
644 if (EFI_ERROR (Status)) {
645
646 if (Status == EFI_SECURITY_VIOLATION) {
647 //
648 // Take driver from Scheduled to Untrused state
649 //
650 DriverEntry->Untrusted = TRUE;
651 } else {
652 //
653 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
654 // Take driver from Scheduled to Initialized.
655 //
656 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
657 //
658 DriverEntry->Initialized = TRUE;
659 }
660
661 DriverEntry->Scheduled = FALSE;
662 RemoveEntryList (&DriverEntry->ScheduledLink);
663
664 //
665 // If it's an error don't try the StartImage
666 //
667 continue;
668 }
669 }
670
671 DriverEntry->Scheduled = FALSE;
672 DriverEntry->Initialized = TRUE;
673 RemoveEntryList (&DriverEntry->ScheduledLink);
674
675 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
676 EFI_PROGRESS_CODE,
677 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,
678 &DriverEntry->ImageHandle,
679 sizeof (DriverEntry->ImageHandle)
680 );
681
682 //
683 // For each SMM driver, pass NULL as ImageHandle
684 //
685 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);
686 if (EFI_ERROR(Status)){
687 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
688 }
689
690 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
691 EFI_PROGRESS_CODE,
692 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,
693 &DriverEntry->ImageHandle,
694 sizeof (DriverEntry->ImageHandle)
695 );
696
697 ReturnStatus = EFI_SUCCESS;
698 }
699
700 //
701 // Search DriverList for items to place on Scheduled Queue
702 //
703 ReadyToRun = FALSE;
704 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
705 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
706
707 if (DriverEntry->DepexProtocolError){
708 //
709 // If Section Extraction Protocol did not let the Depex be read before retry the read
710 //
711 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);
712 }
713
714 if (DriverEntry->Dependent) {
715 if (SmmIsSchedulable (DriverEntry)) {
716 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
717 ReadyToRun = TRUE;
718 }
719 }
720 }
721 } while (ReadyToRun);
722
723 //
724 // If there is no more SMM driver to dispatch, stop the dispatch request
725 //
726 gRequestDispatch = FALSE;
727 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
728 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
729
730 if (!DriverEntry->Initialized){
731 //
732 // We have SMM driver pending to dispatch
733 //
734 gRequestDispatch = TRUE;
735 break;
736 }
737 }
738
739 gDispatcherRunning = FALSE;
740
741 return ReturnStatus;
742 }
743
744 /**
745 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
746 must add any driver with a before dependency on InsertedDriverEntry first.
747 You do this by recursively calling this routine. After all the Befores are
748 processed you can add InsertedDriverEntry to the mScheduledQueue.
749 Then you can add any driver with an After dependency on InsertedDriverEntry
750 by recursively calling this routine.
751
752 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
753
754 **/
755 VOID
756 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
757 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
758 )
759 {
760 LIST_ENTRY *Link;
761 EFI_SMM_DRIVER_ENTRY *DriverEntry;
762
763 //
764 // Process Before Dependency
765 //
766 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
767 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
768 if (DriverEntry->Before && DriverEntry->Dependent) {
769 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
770 //
771 // Recursively process BEFORE
772 //
773 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
774 }
775 }
776 }
777
778 //
779 // Convert driver from Dependent to Scheduled state
780 //
781
782 InsertedDriverEntry->Dependent = FALSE;
783 InsertedDriverEntry->Scheduled = TRUE;
784 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
785
786
787 //
788 // Process After Dependency
789 //
790 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
791 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
792 if (DriverEntry->After && DriverEntry->Dependent) {
793 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
794 //
795 // Recursively process AFTER
796 //
797 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
798 }
799 }
800 }
801 }
802
803 /**
804 Return TRUE if the Fv has been processed, FALSE if not.
805
806 @param FvHandle The handle of a FV that's being tested
807
808 @retval TRUE Fv protocol on FvHandle has been processed
809 @retval FALSE Fv protocol on FvHandle has not yet been
810 processed
811
812 **/
813 BOOLEAN
814 FvHasBeenProcessed (
815 IN EFI_HANDLE FvHandle
816 )
817 {
818 LIST_ENTRY *Link;
819 KNOWN_HANDLE *KnownHandle;
820
821 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
822 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
823 if (KnownHandle->Handle == FvHandle) {
824 return TRUE;
825 }
826 }
827 return FALSE;
828 }
829
830 /**
831 Remember that Fv protocol on FvHandle has had it's drivers placed on the
832 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
833 never removed/freed from the mFvHandleList.
834
835 @param FvHandle The handle of a FV that has been processed
836
837 **/
838 VOID
839 FvIsBeingProcesssed (
840 IN EFI_HANDLE FvHandle
841 )
842 {
843 KNOWN_HANDLE *KnownHandle;
844
845 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
846 ASSERT (KnownHandle != NULL);
847
848 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
849 KnownHandle->Handle = FvHandle;
850 InsertTailList (&mFvHandleList, &KnownHandle->Link);
851 }
852
853 /**
854 Convert FvHandle and DriverName into an EFI device path
855
856 @param Fv Fv protocol, needed to read Depex info out of
857 FLASH.
858 @param FvHandle Handle for Fv, needed in the
859 EFI_SMM_DRIVER_ENTRY so that the PE image can be
860 read out of the FV at a later time.
861 @param DriverName Name of driver to add to mDiscoveredList.
862
863 @return Pointer to device path constructed from FvHandle and DriverName
864
865 **/
866 EFI_DEVICE_PATH_PROTOCOL *
867 SmmFvToDevicePath (
868 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
869 IN EFI_HANDLE FvHandle,
870 IN EFI_GUID *DriverName
871 )
872 {
873 EFI_STATUS Status;
874 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
875 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;
876
877 //
878 // Remember the device path of the FV
879 //
880 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
881 if (EFI_ERROR (Status)) {
882 FileNameDevicePath = NULL;
883 } else {
884 //
885 // Build a device path to the file in the FV to pass into gBS->LoadImage
886 //
887 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);
888 SetDevicePathEndNode (&mFvDevicePath.End);
889
890 //
891 // Note: FileNameDevicePath is in DXE memory
892 //
893 FileNameDevicePath = AppendDevicePath (
894 FvDevicePath,
895 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath
896 );
897 }
898 return FileNameDevicePath;
899 }
900
901 /**
902 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
903 and initilize any state variables. Read the Depex from the FV and store it
904 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
905 The Discovered list is never free'ed and contains booleans that represent the
906 other possible SMM driver states.
907
908 @param Fv Fv protocol, needed to read Depex info out of
909 FLASH.
910 @param FvHandle Handle for Fv, needed in the
911 EFI_SMM_DRIVER_ENTRY so that the PE image can be
912 read out of the FV at a later time.
913 @param DriverName Name of driver to add to mDiscoveredList.
914
915 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
916 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
917 DriverName may be active in the system at any one
918 time.
919
920 **/
921 EFI_STATUS
922 SmmAddToDriverList (
923 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
924 IN EFI_HANDLE FvHandle,
925 IN EFI_GUID *DriverName
926 )
927 {
928 EFI_SMM_DRIVER_ENTRY *DriverEntry;
929
930 //
931 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
932 // NULL or FALSE.
933 //
934 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));
935 ASSERT (DriverEntry != NULL);
936
937 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
938 CopyGuid (&DriverEntry->FileName, DriverName);
939 DriverEntry->FvHandle = FvHandle;
940 DriverEntry->Fv = Fv;
941 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);
942
943 SmmGetDepexSectionAndPreProccess (DriverEntry);
944
945 InsertTailList (&mDiscoveredList, &DriverEntry->Link);
946 gRequestDispatch = TRUE;
947
948 return EFI_SUCCESS;
949 }
950
951 /**
952 This function is the main entry point for an SMM handler dispatch
953 or communicate-based callback.
954
955 Event notification that is fired every time a FV dispatch protocol is added.
956 More than one protocol may have been added when this event is fired, so you
957 must loop on SmmLocateHandle () to see how many protocols were added and
958 do the following to each FV:
959 If the Fv has already been processed, skip it. If the Fv has not been
960 processed then mark it as being processed, as we are about to process it.
961 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
962 mDiscoveredList is never free'ed and contains variables that define
963 the other states the SMM driver transitions to..
964 While you are at it read the A Priori file into memory.
965 Place drivers in the A Priori list onto the mScheduledQueue.
966
967 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
968 @param Context Points to an optional handler context which was specified when the handler was registered.
969 @param CommBuffer A pointer to a collection of data in memory that will
970 be conveyed from a non-SMM environment into an SMM environment.
971 @param CommBufferSize The size of the CommBuffer.
972
973 @return Status Code
974
975 **/
976 EFI_STATUS
977 EFIAPI
978 SmmDriverDispatchHandler (
979 IN EFI_HANDLE DispatchHandle,
980 IN CONST VOID *Context, OPTIONAL
981 IN OUT VOID *CommBuffer, OPTIONAL
982 IN OUT UINTN *CommBufferSize OPTIONAL
983 )
984 {
985 EFI_STATUS Status;
986 UINTN HandleCount;
987 EFI_HANDLE *HandleBuffer;
988 EFI_STATUS GetNextFileStatus;
989 EFI_STATUS SecurityStatus;
990 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
991 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
992 EFI_HANDLE FvHandle;
993 EFI_GUID NameGuid;
994 UINTN Key;
995 EFI_FV_FILETYPE Type;
996 EFI_FV_FILE_ATTRIBUTES Attributes;
997 UINTN Size;
998 EFI_SMM_DRIVER_ENTRY *DriverEntry;
999 EFI_GUID *AprioriFile;
1000 UINTN AprioriEntryCount;
1001 UINTN Index;
1002 LIST_ENTRY *Link;
1003 UINT32 AuthenticationStatus;
1004 UINTN SizeOfBuffer;
1005
1006 HandleBuffer = NULL;
1007 Status = gBS->LocateHandleBuffer (
1008 ByProtocol,
1009 &gEfiFirmwareVolume2ProtocolGuid,
1010 NULL,
1011 &HandleCount,
1012 &HandleBuffer
1013 );
1014 if (EFI_ERROR (Status)) {
1015 return EFI_NOT_FOUND;
1016 }
1017
1018 for (Index = 0; Index < HandleCount; Index++) {
1019 FvHandle = HandleBuffer[Index];
1020
1021 if (FvHasBeenProcessed (FvHandle)) {
1022 //
1023 // This Fv has already been processed so lets skip it!
1024 //
1025 continue;
1026 }
1027
1028 //
1029 // Since we are about to process this Fv mark it as processed.
1030 //
1031 FvIsBeingProcesssed (FvHandle);
1032
1033 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
1034 if (EFI_ERROR (Status)) {
1035 //
1036 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1037 //
1038 ASSERT (FALSE);
1039 continue;
1040 }
1041
1042 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1043 if (EFI_ERROR (Status)) {
1044 //
1045 // The Firmware volume doesn't have device path, can't be dispatched.
1046 //
1047 continue;
1048 }
1049
1050 //
1051 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1052 //
1053 if (mSecurity == NULL) {
1054 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
1055 }
1056
1057 //
1058 // Evaluate the authentication status of the Firmware Volume through
1059 // Security Architectural Protocol
1060 //
1061 if (mSecurity != NULL) {
1062 SecurityStatus = mSecurity->FileAuthenticationState (
1063 mSecurity,
1064 0,
1065 FvDevicePath
1066 );
1067 if (SecurityStatus != EFI_SUCCESS) {
1068 //
1069 // Security check failed. The firmware volume should not be used for any purpose.
1070 //
1071 continue;
1072 }
1073 }
1074
1075 //
1076 // Discover Drivers in FV and add them to the Discovered Driver List.
1077 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1078 //
1079 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {
1080 //
1081 // Initialize the search key
1082 //
1083 Key = 0;
1084 do {
1085 Type = mSmmFileTypes[Index];
1086 GetNextFileStatus = Fv->GetNextFile (
1087 Fv,
1088 &Key,
1089 &Type,
1090 &NameGuid,
1091 &Attributes,
1092 &Size
1093 );
1094 if (!EFI_ERROR (GetNextFileStatus)) {
1095 SmmAddToDriverList (Fv, FvHandle, &NameGuid);
1096 }
1097 } while (!EFI_ERROR (GetNextFileStatus));
1098 }
1099
1100 //
1101 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1102 // (Note: AprioriFile is in DXE memory)
1103 //
1104 AprioriFile = NULL;
1105 Status = Fv->ReadSection (
1106 Fv,
1107 &gAprioriGuid,
1108 EFI_SECTION_RAW,
1109 0,
1110 (VOID **)&AprioriFile,
1111 &SizeOfBuffer,
1112 &AuthenticationStatus
1113 );
1114 if (!EFI_ERROR (Status)) {
1115 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);
1116 } else {
1117 AprioriEntryCount = 0;
1118 }
1119
1120 //
1121 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1122 // drivers not in the current FV and these must be skipped since the a priori list
1123 // is only valid for the FV that it resided in.
1124 //
1125
1126 for (Index = 0; Index < AprioriEntryCount; Index++) {
1127 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
1128 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1129 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&
1130 (FvHandle == DriverEntry->FvHandle)) {
1131 DriverEntry->Dependent = FALSE;
1132 DriverEntry->Scheduled = TRUE;
1133 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);
1134 break;
1135 }
1136 }
1137 }
1138
1139 //
1140 // Free data allocated by Fv->ReadSection ()
1141 //
1142 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1143 // used the UEFI Boot Services AllocatePool() function
1144 //
1145 gBS->FreePool (AprioriFile);
1146 }
1147
1148 //
1149 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1150 // discovered SMM drivers that have been discovered but not dispatched.
1151 //
1152 return SmmDispatcher ();
1153 }
1154
1155 /**
1156 Traverse the discovered list for any drivers that were discovered but not loaded
1157 because the dependency experessions evaluated to false.
1158
1159 **/
1160 VOID
1161 SmmDisplayDiscoveredNotDispatched (
1162 VOID
1163 )
1164 {
1165 LIST_ENTRY *Link;
1166 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1167
1168 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
1169 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1170 if (DriverEntry->Dependent) {
1171 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
1172 }
1173 }
1174 }