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