]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
MdeModulePkg: Clean up source files
[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 - 2018, 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 // Function Prototypes
96 //
97 /**
98 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
99 must add any driver with a before dependency on InsertedDriverEntry first.
100 You do this by recursively calling this routine. After all the Befores are
101 processed you can add InsertedDriverEntry to the mScheduledQueue.
102 Then you can add any driver with an After dependency on InsertedDriverEntry
103 by recursively calling this routine.
104
105 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
106
107 **/
108 VOID
109 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
110 IN EFI_CORE_DRIVER_ENTRY *InsertedDriverEntry
111 );
112
113 /**
114 Event notification that is fired every time a FV dispatch protocol is added.
115 More than one protocol may have been added when this event is fired, so you
116 must loop on CoreLocateHandle () to see how many protocols were added and
117 do the following to each FV:
118 If the Fv has already been processed, skip it. If the Fv has not been
119 processed then mark it as being processed, as we are about to process it.
120 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
121 mDiscoveredList is never free'ed and contains variables that define
122 the other states the DXE driver transitions to..
123 While you are at it read the A Priori file into memory.
124 Place drivers in the A Priori list onto the mScheduledQueue.
125
126 @param Event The Event that is being processed, not used.
127 @param Context Event Context, not used.
128
129 **/
130 VOID
131 EFIAPI
132 CoreFwVolEventProtocolNotify (
133 IN EFI_EVENT Event,
134 IN VOID *Context
135 );
136
137 /**
138 Convert FvHandle and DriverName into an EFI device path
139
140 @param Fv Fv protocol, needed to read Depex info out of
141 FLASH.
142 @param FvHandle Handle for Fv, needed in the
143 EFI_CORE_DRIVER_ENTRY so that the PE image can be
144 read out of the FV at a later time.
145 @param DriverName Name of driver to add to mDiscoveredList.
146
147 @return Pointer to device path constructed from FvHandle and DriverName
148
149 **/
150 EFI_DEVICE_PATH_PROTOCOL *
151 CoreFvToDevicePath (
152 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
153 IN EFI_HANDLE FvHandle,
154 IN EFI_GUID *DriverName
155 );
156
157 /**
158 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
159 and initilize any state variables. Read the Depex from the FV and store it
160 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
161 The Discovered list is never free'ed and contains booleans that represent the
162 other possible DXE driver states.
163
164 @param Fv Fv protocol, needed to read Depex info out of
165 FLASH.
166 @param FvHandle Handle for Fv, needed in the
167 EFI_CORE_DRIVER_ENTRY so that the PE image can be
168 read out of the FV at a later time.
169 @param DriverName Name of driver to add to mDiscoveredList.
170 @param Type Fv File Type of file 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 IN EFI_FV_FILETYPE Type
184 );
185
186 /**
187 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
188
189 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.
190 @param FvHandle The handle which FVB protocol installed on.
191 @param DriverName The driver guid specified.
192
193 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.
194 @retval EFI_VOLUME_CORRUPTED Corrupted volume.
195 @retval EFI_SUCCESS Function successfully returned.
196
197 **/
198 EFI_STATUS
199 CoreProcessFvImageFile (
200 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
201 IN EFI_HANDLE FvHandle,
202 IN EFI_GUID *DriverName
203 );
204
205
206 /**
207 Enter critical section by gaining lock on mDispatcherLock.
208
209 **/
210 VOID
211 CoreAcquireDispatcherLock (
212 VOID
213 )
214 {
215 CoreAcquireLock (&mDispatcherLock);
216 }
217
218
219 /**
220 Exit critical section by releasing lock on mDispatcherLock.
221
222 **/
223 VOID
224 CoreReleaseDispatcherLock (
225 VOID
226 )
227 {
228 CoreReleaseLock (&mDispatcherLock);
229 }
230
231
232 /**
233 Read Depex and pre-process the Depex for Before and After. If Section Extraction
234 protocol returns an error via ReadSection defer the reading of the Depex.
235
236 @param DriverEntry Driver to work on.
237
238 @retval EFI_SUCCESS Depex read and preprossesed
239 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
240 and Depex reading needs to be retried.
241 @retval Error DEPEX not found.
242
243 **/
244 EFI_STATUS
245 CoreGetDepexSectionAndPreProccess (
246 IN EFI_CORE_DRIVER_ENTRY *DriverEntry
247 )
248 {
249 EFI_STATUS Status;
250 EFI_SECTION_TYPE SectionType;
251 UINT32 AuthenticationStatus;
252 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
253
254
255 Fv = DriverEntry->Fv;
256
257 //
258 // Grab Depex info, it will never be free'ed.
259 //
260 SectionType = EFI_SECTION_DXE_DEPEX;
261 Status = Fv->ReadSection (
262 DriverEntry->Fv,
263 &DriverEntry->FileName,
264 SectionType,
265 0,
266 &DriverEntry->Depex,
267 (UINTN *)&DriverEntry->DepexSize,
268 &AuthenticationStatus
269 );
270 if (EFI_ERROR (Status)) {
271 if (Status == EFI_PROTOCOL_ERROR) {
272 //
273 // The section extraction protocol failed so set protocol error flag
274 //
275 DriverEntry->DepexProtocolError = TRUE;
276 } else {
277 //
278 // If no Depex assume UEFI 2.0 driver model
279 //
280 DriverEntry->Depex = NULL;
281 DriverEntry->Dependent = TRUE;
282 DriverEntry->DepexProtocolError = FALSE;
283 }
284 } else {
285 //
286 // Set Before, After, and Unrequested state information based on Depex
287 // Driver will be put in Dependent or Unrequested state
288 //
289 CorePreProcessDepex (DriverEntry);
290 DriverEntry->DepexProtocolError = FALSE;
291 }
292
293 return Status;
294 }
295
296
297 /**
298 Check every driver and locate a matching one. If the driver is found, the Unrequested
299 state flag is cleared.
300
301 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
302 the firmware file specified by DriverName.
303 @param DriverName The Driver name to put in the Dependent state.
304
305 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
306 cleared
307 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
308 not set.
309
310 **/
311 EFI_STATUS
312 EFIAPI
313 CoreSchedule (
314 IN EFI_HANDLE FirmwareVolumeHandle,
315 IN EFI_GUID *DriverName
316 )
317 {
318 LIST_ENTRY *Link;
319 EFI_CORE_DRIVER_ENTRY *DriverEntry;
320
321 //
322 // Check every driver
323 //
324 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
325 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
326 if (DriverEntry->FvHandle == FirmwareVolumeHandle &&
327 DriverEntry->Unrequested &&
328 CompareGuid (DriverName, &DriverEntry->FileName)) {
329 //
330 // Move the driver from the Unrequested to the Dependent state
331 //
332 CoreAcquireDispatcherLock ();
333 DriverEntry->Unrequested = FALSE;
334 DriverEntry->Dependent = TRUE;
335 CoreReleaseDispatcherLock ();
336
337 DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_SUCCESS\n", DriverName));
338
339 return EFI_SUCCESS;
340 }
341 }
342
343 DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_NOT_FOUND\n", DriverName));
344
345 return EFI_NOT_FOUND;
346 }
347
348
349
350 /**
351 Convert a driver from the Untrused back to the Scheduled state.
352
353 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
354 the firmware file specified by DriverName.
355 @param DriverName The Driver name to put in the Scheduled state
356
357 @retval EFI_SUCCESS The file was found in the untrusted state, and it
358 was promoted to the trusted state.
359 @retval EFI_NOT_FOUND The file was not found in the untrusted state.
360
361 **/
362 EFI_STATUS
363 EFIAPI
364 CoreTrust (
365 IN EFI_HANDLE FirmwareVolumeHandle,
366 IN EFI_GUID *DriverName
367 )
368 {
369 LIST_ENTRY *Link;
370 EFI_CORE_DRIVER_ENTRY *DriverEntry;
371
372 //
373 // Check every driver
374 //
375 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
376 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
377 if (DriverEntry->FvHandle == FirmwareVolumeHandle &&
378 DriverEntry->Untrusted &&
379 CompareGuid (DriverName, &DriverEntry->FileName)) {
380 //
381 // Transition driver from Untrusted to Scheduled state.
382 //
383 CoreAcquireDispatcherLock ();
384 DriverEntry->Untrusted = FALSE;
385 DriverEntry->Scheduled = TRUE;
386 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);
387 CoreReleaseDispatcherLock ();
388
389 return EFI_SUCCESS;
390 }
391 }
392 return EFI_NOT_FOUND;
393 }
394
395 /**
396 This is the main Dispatcher for DXE and it exits when there are no more
397 drivers to run. Drain the mScheduledQueue and load and start a PE
398 image for each driver. Search the mDiscoveredList to see if any driver can
399 be placed on the mScheduledQueue. If no drivers are placed on the
400 mScheduledQueue exit the function. On exit it is assumed the Bds()
401 will be called, and when the Bds() exits the Dispatcher will be called
402 again.
403
404 @retval EFI_ALREADY_STARTED The DXE Dispatcher is already running
405 @retval EFI_NOT_FOUND No DXE Drivers were dispatched
406 @retval EFI_SUCCESS One or more DXE Drivers were dispatched
407
408 **/
409 EFI_STATUS
410 EFIAPI
411 CoreDispatcher (
412 VOID
413 )
414 {
415 EFI_STATUS Status;
416 EFI_STATUS ReturnStatus;
417 LIST_ENTRY *Link;
418 EFI_CORE_DRIVER_ENTRY *DriverEntry;
419 BOOLEAN ReadyToRun;
420 EFI_EVENT DxeDispatchEvent;
421
422 PERF_FUNCTION_BEGIN ();
423
424 if (gDispatcherRunning) {
425 //
426 // If the dispatcher is running don't let it be restarted.
427 //
428 return EFI_ALREADY_STARTED;
429 }
430
431 gDispatcherRunning = TRUE;
432
433 Status = CoreCreateEventEx (
434 EVT_NOTIFY_SIGNAL,
435 TPL_NOTIFY,
436 EfiEventEmptyFunction,
437 NULL,
438 &gEfiEventDxeDispatchGuid,
439 &DxeDispatchEvent
440 );
441 if (EFI_ERROR (Status)) {
442 return Status;
443 }
444
445 ReturnStatus = EFI_NOT_FOUND;
446 do {
447 //
448 // Drain the Scheduled Queue
449 //
450 while (!IsListEmpty (&mScheduledQueue)) {
451 DriverEntry = CR (
452 mScheduledQueue.ForwardLink,
453 EFI_CORE_DRIVER_ENTRY,
454 ScheduledLink,
455 EFI_CORE_DRIVER_ENTRY_SIGNATURE
456 );
457
458 //
459 // Load the DXE Driver image into memory. If the Driver was transitioned from
460 // Untrused to Scheduled it would have already been loaded so we may need to
461 // skip the LoadImage
462 //
463 if (DriverEntry->ImageHandle == NULL && !DriverEntry->IsFvImage) {
464 DEBUG ((DEBUG_INFO, "Loading driver %g\n", &DriverEntry->FileName));
465 Status = CoreLoadImage (
466 FALSE,
467 gDxeCoreImageHandle,
468 DriverEntry->FvFileDevicePath,
469 NULL,
470 0,
471 &DriverEntry->ImageHandle
472 );
473
474 //
475 // Update the driver state to reflect that it's been loaded
476 //
477 if (EFI_ERROR (Status)) {
478 CoreAcquireDispatcherLock ();
479
480 if (Status == EFI_SECURITY_VIOLATION) {
481 //
482 // Take driver from Scheduled to Untrused state
483 //
484 DriverEntry->Untrusted = TRUE;
485 } else {
486 //
487 // The DXE Driver could not be loaded, and do not attempt to load or start it again.
488 // Take driver from Scheduled to Initialized.
489 //
490 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
491 //
492 DriverEntry->Initialized = TRUE;
493 }
494
495 DriverEntry->Scheduled = FALSE;
496 RemoveEntryList (&DriverEntry->ScheduledLink);
497
498 CoreReleaseDispatcherLock ();
499
500 //
501 // If it's an error don't try the StartImage
502 //
503 continue;
504 }
505 }
506
507 CoreAcquireDispatcherLock ();
508
509 DriverEntry->Scheduled = FALSE;
510 DriverEntry->Initialized = TRUE;
511 RemoveEntryList (&DriverEntry->ScheduledLink);
512
513 CoreReleaseDispatcherLock ();
514
515
516 if (DriverEntry->IsFvImage) {
517 //
518 // Produce a firmware volume block protocol for FvImage so it gets dispatched from.
519 //
520 Status = CoreProcessFvImageFile (DriverEntry->Fv, DriverEntry->FvHandle, &DriverEntry->FileName);
521 } else {
522 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
523 EFI_PROGRESS_CODE,
524 (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_BEGIN),
525 &DriverEntry->ImageHandle,
526 sizeof (DriverEntry->ImageHandle)
527 );
528 ASSERT (DriverEntry->ImageHandle != NULL);
529
530 Status = CoreStartImage (DriverEntry->ImageHandle, NULL, NULL);
531
532 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
533 EFI_PROGRESS_CODE,
534 (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_END),
535 &DriverEntry->ImageHandle,
536 sizeof (DriverEntry->ImageHandle)
537 );
538 }
539
540 ReturnStatus = EFI_SUCCESS;
541 }
542
543 //
544 // Now DXE Dispatcher finished one round of dispatch, signal an event group
545 // so that SMM Dispatcher get chance to dispatch SMM Drivers which depend
546 // on UEFI protocols
547 //
548 if (!EFI_ERROR (ReturnStatus)) {
549 CoreSignalEvent (DxeDispatchEvent);
550 }
551
552 //
553 // Search DriverList for items to place on Scheduled Queue
554 //
555 ReadyToRun = FALSE;
556 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
557 DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
558
559 if (DriverEntry->DepexProtocolError){
560 //
561 // If Section Extraction Protocol did not let the Depex be read before retry the read
562 //
563 Status = CoreGetDepexSectionAndPreProccess (DriverEntry);
564 }
565
566 if (DriverEntry->Dependent) {
567 if (CoreIsSchedulable (DriverEntry)) {
568 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
569 ReadyToRun = TRUE;
570 }
571 } else {
572 if (DriverEntry->Unrequested) {
573 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));
574 DEBUG ((DEBUG_DISPATCH, " SOR = Not Requested\n"));
575 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE\n"));
576 }
577 }
578 }
579 } while (ReadyToRun);
580
581 //
582 // Close DXE dispatch Event
583 //
584 CoreCloseEvent (DxeDispatchEvent);
585
586 gDispatcherRunning = FALSE;
587
588 PERF_FUNCTION_END ();
589
590 return ReturnStatus;
591 }
592
593
594 /**
595 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
596 must add any driver with a before dependency on InsertedDriverEntry first.
597 You do this by recursively calling this routine. After all the Befores are
598 processed you can add InsertedDriverEntry to the mScheduledQueue.
599 Then you can add any driver with an After dependency on InsertedDriverEntry
600 by recursively calling this routine.
601
602 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
603
604 **/
605 VOID
606 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
607 IN EFI_CORE_DRIVER_ENTRY *InsertedDriverEntry
608 )
609 {
610 LIST_ENTRY *Link;
611 EFI_CORE_DRIVER_ENTRY *DriverEntry;
612
613 //
614 // Process Before Dependency
615 //
616 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
617 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
618 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
619 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));
620 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
621 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
622 //
623 // Recursively process BEFORE
624 //
625 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
626 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
627 } else {
628 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
629 }
630 }
631 }
632
633 //
634 // Convert driver from Dependent to Scheduled state
635 //
636 CoreAcquireDispatcherLock ();
637
638 InsertedDriverEntry->Dependent = FALSE;
639 InsertedDriverEntry->Scheduled = TRUE;
640 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
641
642 CoreReleaseDispatcherLock ();
643
644 //
645 // Process After Dependency
646 //
647 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
648 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
649 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
650 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));
651 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
652 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
653 //
654 // Recursively process AFTER
655 //
656 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
657 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
658 } else {
659 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
660 }
661 }
662 }
663 }
664
665
666 /**
667 Return TRUE if the Fv has been processed, FALSE if not.
668
669 @param FvHandle The handle of a FV that's being tested
670
671 @retval TRUE Fv protocol on FvHandle has been processed
672 @retval FALSE Fv protocol on FvHandle has not yet been processed
673
674 **/
675 BOOLEAN
676 FvHasBeenProcessed (
677 IN EFI_HANDLE FvHandle
678 )
679 {
680 LIST_ENTRY *Link;
681 KNOWN_HANDLE *KnownHandle;
682
683 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
684 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
685 if (KnownHandle->Handle == FvHandle) {
686 return TRUE;
687 }
688 }
689 return FALSE;
690 }
691
692
693 /**
694 Remember that Fv protocol on FvHandle has had it's drivers placed on the
695 mDiscoveredList. This fucntion adds entries on the mFvHandleList if new
696 entry is different from one in mFvHandleList by checking FvImage Guid.
697 Items are never removed/freed from the mFvHandleList.
698
699 @param FvHandle The handle of a FV that has been processed
700
701 @return A point to new added FvHandle entry. If FvHandle with the same FvImage guid
702 has been added, NULL will return.
703
704 **/
705 KNOWN_HANDLE *
706 FvIsBeingProcesssed (
707 IN EFI_HANDLE FvHandle
708 )
709 {
710 EFI_STATUS Status;
711 EFI_GUID FvNameGuid;
712 BOOLEAN FvNameGuidIsFound;
713 UINT32 ExtHeaderOffset;
714 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
715 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
716 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
717 UINTN LbaOffset;
718 UINTN Index;
719 EFI_LBA LbaIndex;
720 LIST_ENTRY *Link;
721 KNOWN_HANDLE *KnownHandle;
722
723 FwVolHeader = NULL;
724
725 //
726 // Get the FirmwareVolumeBlock protocol on that handle
727 //
728 FvNameGuidIsFound = FALSE;
729 Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);
730 if (!EFI_ERROR (Status)) {
731 //
732 // Get the full FV header based on FVB protocol.
733 //
734 ASSERT (Fvb != NULL);
735 Status = GetFwVolHeader (Fvb, &FwVolHeader);
736 if (!EFI_ERROR (Status)) {
737 ASSERT (FwVolHeader != NULL);
738 if (VerifyFvHeaderChecksum (FwVolHeader) && FwVolHeader->ExtHeaderOffset != 0) {
739 ExtHeaderOffset = (UINT32) FwVolHeader->ExtHeaderOffset;
740 BlockMap = FwVolHeader->BlockMap;
741 LbaIndex = 0;
742 LbaOffset = 0;
743 //
744 // Find LbaIndex and LbaOffset for FV extension header based on BlockMap.
745 //
746 while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {
747 for (Index = 0; Index < BlockMap->NumBlocks && ExtHeaderOffset >= BlockMap->Length; Index ++) {
748 ExtHeaderOffset -= BlockMap->Length;
749 LbaIndex ++;
750 }
751 //
752 // Check whether FvExtHeader is crossing the multi block range.
753 //
754 if (Index < BlockMap->NumBlocks) {
755 LbaOffset = ExtHeaderOffset;
756 break;
757 }
758 BlockMap++;
759 }
760 //
761 // Read FvNameGuid from FV extension header.
762 //
763 Status = ReadFvbData (Fvb, &LbaIndex, &LbaOffset, sizeof (FvNameGuid), (UINT8 *) &FvNameGuid);
764 if (!EFI_ERROR (Status)) {
765 FvNameGuidIsFound = TRUE;
766 }
767 }
768 CoreFreePool (FwVolHeader);
769 }
770 }
771
772 if (FvNameGuidIsFound) {
773 //
774 // Check whether the FV image with the found FvNameGuid has been processed.
775 //
776 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
777 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
778 if (CompareGuid (&FvNameGuid, &KnownHandle->FvNameGuid)) {
779 DEBUG ((EFI_D_ERROR, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle, KnownHandle->Handle, &FvNameGuid));
780 return NULL;
781 }
782 }
783 }
784
785 KnownHandle = AllocateZeroPool (sizeof (KNOWN_HANDLE));
786 ASSERT (KnownHandle != NULL);
787
788 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
789 KnownHandle->Handle = FvHandle;
790 if (FvNameGuidIsFound) {
791 CopyGuid (&KnownHandle->FvNameGuid, &FvNameGuid);
792 }
793 InsertTailList (&mFvHandleList, &KnownHandle->Link);
794 return KnownHandle;
795 }
796
797
798
799
800 /**
801 Convert FvHandle and DriverName into an EFI device path
802
803 @param Fv Fv protocol, needed to read Depex info out of
804 FLASH.
805 @param FvHandle Handle for Fv, needed in the
806 EFI_CORE_DRIVER_ENTRY so that the PE image can be
807 read out of the FV at a later time.
808 @param DriverName Name of driver to add to mDiscoveredList.
809
810 @return Pointer to device path constructed from FvHandle and DriverName
811
812 **/
813 EFI_DEVICE_PATH_PROTOCOL *
814 CoreFvToDevicePath (
815 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
816 IN EFI_HANDLE FvHandle,
817 IN EFI_GUID *DriverName
818 )
819 {
820 EFI_STATUS Status;
821 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
822 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;
823
824 //
825 // Remember the device path of the FV
826 //
827 Status = CoreHandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
828 if (EFI_ERROR (Status)) {
829 FileNameDevicePath = NULL;
830 } else {
831 //
832 // Build a device path to the file in the FV to pass into gBS->LoadImage
833 //
834 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);
835 SetDevicePathEndNode (&mFvDevicePath.End);
836
837 FileNameDevicePath = AppendDevicePath (
838 FvDevicePath,
839 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath
840 );
841 }
842
843 return FileNameDevicePath;
844 }
845
846
847
848 /**
849 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
850 and initilize any state variables. Read the Depex from the FV and store it
851 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
852 The Discovered list is never free'ed and contains booleans that represent the
853 other possible DXE driver states.
854
855 @param Fv Fv protocol, needed to read Depex info out of
856 FLASH.
857 @param FvHandle Handle for Fv, needed in the
858 EFI_CORE_DRIVER_ENTRY so that the PE image can be
859 read out of the FV at a later time.
860 @param DriverName Name of driver to add to mDiscoveredList.
861 @param Type Fv File Type of file to add to mDiscoveredList.
862
863 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
864 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
865 DriverName may be active in the system at any one
866 time.
867
868 **/
869 EFI_STATUS
870 CoreAddToDriverList (
871 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
872 IN EFI_HANDLE FvHandle,
873 IN EFI_GUID *DriverName,
874 IN EFI_FV_FILETYPE Type
875 )
876 {
877 EFI_CORE_DRIVER_ENTRY *DriverEntry;
878
879
880 //
881 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
882 // NULL or FALSE.
883 //
884 DriverEntry = AllocateZeroPool (sizeof (EFI_CORE_DRIVER_ENTRY));
885 ASSERT (DriverEntry != NULL);
886 if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
887 DriverEntry->IsFvImage = TRUE;
888 }
889
890 DriverEntry->Signature = EFI_CORE_DRIVER_ENTRY_SIGNATURE;
891 CopyGuid (&DriverEntry->FileName, DriverName);
892 DriverEntry->FvHandle = FvHandle;
893 DriverEntry->Fv = Fv;
894 DriverEntry->FvFileDevicePath = CoreFvToDevicePath (Fv, FvHandle, DriverName);
895
896 CoreGetDepexSectionAndPreProccess (DriverEntry);
897
898 CoreAcquireDispatcherLock ();
899
900 InsertTailList (&mDiscoveredList, &DriverEntry->Link);
901
902 CoreReleaseDispatcherLock ();
903
904 return EFI_SUCCESS;
905 }
906
907
908 /**
909 Check if a FV Image type file (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) is
910 described by a EFI_HOB_FIRMWARE_VOLUME2 Hob.
911
912 @param FvNameGuid The FV image guid specified.
913 @param DriverName The driver guid specified.
914
915 @retval TRUE This file is found in a EFI_HOB_FIRMWARE_VOLUME2
916 Hob.
917 @retval FALSE Not found.
918
919 **/
920 BOOLEAN
921 FvFoundInHobFv2 (
922 IN CONST EFI_GUID *FvNameGuid,
923 IN CONST EFI_GUID *DriverName
924 )
925 {
926 EFI_PEI_HOB_POINTERS HobFv2;
927
928 HobFv2.Raw = GetHobList ();
929
930 while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {
931 //
932 // Compare parent FvNameGuid and FileGuid both.
933 //
934 if (CompareGuid (DriverName, &HobFv2.FirmwareVolume2->FileName) &&
935 CompareGuid (FvNameGuid, &HobFv2.FirmwareVolume2->FvName)) {
936 return TRUE;
937 }
938 HobFv2.Raw = GET_NEXT_HOB (HobFv2);
939 }
940
941 return FALSE;
942 }
943
944 /**
945 Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.
946
947 @param[in] FvHeader Pointer to FV header.
948 @param[out] FvUsedSize Pointer to FV used size returned,
949 only valid if USED_SIZE FV_EXT_TYPE entry is found.
950 @param[out] EraseByte Pointer to erase byte returned,
951 only valid if USED_SIZE FV_EXT_TYPE entry is found.
952
953 @retval TRUE USED_SIZE FV_EXT_TYPE entry is found,
954 FV used size and erase byte are returned.
955 @retval FALSE No USED_SIZE FV_EXT_TYPE entry found.
956
957 **/
958 BOOLEAN
959 GetFvUsedSize (
960 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,
961 OUT UINT32 *FvUsedSize,
962 OUT UINT8 *EraseByte
963 )
964 {
965 UINT16 ExtHeaderOffset;
966 EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader;
967 EFI_FIRMWARE_VOLUME_EXT_ENTRY *ExtEntryList;
968 EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *ExtEntryUsedSize;
969
970 ExtHeaderOffset = ReadUnaligned16 (&FvHeader->ExtHeaderOffset);
971 if (ExtHeaderOffset != 0) {
972 ExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FvHeader + ExtHeaderOffset);
973 ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *) (ExtHeader + 1);
974 while ((UINTN) ExtEntryList < ((UINTN) ExtHeader + ReadUnaligned32 (&ExtHeader->ExtHeaderSize))) {
975 if (ReadUnaligned16 (&ExtEntryList->ExtEntryType) == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {
976 //
977 // USED_SIZE FV_EXT_TYPE entry is found.
978 //
979 ExtEntryUsedSize = (EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *) ExtEntryList;
980 *FvUsedSize = ReadUnaligned32 (&ExtEntryUsedSize->UsedSize);
981 if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ERASE_POLARITY) != 0) {
982 *EraseByte = 0xFF;
983 } else {
984 *EraseByte = 0;
985 }
986 DEBUG ((
987 DEBUG_INFO,
988 "FV at 0x%x has 0x%x used size, and erase byte is 0x%02x\n",
989 FvHeader,
990 *FvUsedSize,
991 *EraseByte
992 ));
993 return TRUE;
994 }
995 ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)
996 ((UINT8 *) ExtEntryList + ReadUnaligned16 (&ExtEntryList->ExtEntrySize));
997 }
998 }
999
1000 //
1001 // No USED_SIZE FV_EXT_TYPE entry found.
1002 //
1003 return FALSE;
1004 }
1005
1006 /**
1007 Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
1008
1009 @param Fv The FIRMWARE_VOLUME protocol installed on the FV.
1010 @param FvHandle The handle which FVB protocol installed on.
1011 @param DriverName The driver guid specified.
1012
1013 @retval EFI_OUT_OF_RESOURCES No enough memory or other resource.
1014 @retval EFI_VOLUME_CORRUPTED Corrupted volume.
1015 @retval EFI_SUCCESS Function successfully returned.
1016
1017 **/
1018 EFI_STATUS
1019 CoreProcessFvImageFile (
1020 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1021 IN EFI_HANDLE FvHandle,
1022 IN EFI_GUID *DriverName
1023 )
1024 {
1025 EFI_STATUS Status;
1026 EFI_SECTION_TYPE SectionType;
1027 UINT32 AuthenticationStatus;
1028 VOID *Buffer;
1029 VOID *AlignedBuffer;
1030 UINTN BufferSize;
1031 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
1032 UINT32 FvAlignment;
1033 EFI_DEVICE_PATH_PROTOCOL *FvFileDevicePath;
1034 UINT32 FvUsedSize;
1035 UINT8 EraseByte;
1036
1037 //
1038 // Read the first (and only the first) firmware volume section
1039 //
1040 SectionType = EFI_SECTION_FIRMWARE_VOLUME_IMAGE;
1041 FvHeader = NULL;
1042 FvAlignment = 0;
1043 Buffer = NULL;
1044 BufferSize = 0;
1045 AlignedBuffer = NULL;
1046 Status = Fv->ReadSection (
1047 Fv,
1048 DriverName,
1049 SectionType,
1050 0,
1051 &Buffer,
1052 &BufferSize,
1053 &AuthenticationStatus
1054 );
1055 if (!EFI_ERROR (Status)) {
1056 //
1057 // Evaluate the authentication status of the Firmware Volume through
1058 // Security Architectural Protocol
1059 //
1060 if (gSecurity != NULL) {
1061 FvFileDevicePath = CoreFvToDevicePath (Fv, FvHandle, DriverName);
1062 Status = gSecurity->FileAuthenticationState (
1063 gSecurity,
1064 AuthenticationStatus,
1065 FvFileDevicePath
1066 );
1067 if (FvFileDevicePath != NULL) {
1068 FreePool (FvFileDevicePath);
1069 }
1070
1071 if (Status != EFI_SUCCESS) {
1072 //
1073 // Security check failed. The firmware volume should not be used for any purpose.
1074 //
1075 if (Buffer != NULL) {
1076 FreePool (Buffer);
1077 }
1078 return Status;
1079 }
1080 }
1081
1082 //
1083 // FvImage should be at its required alignment.
1084 //
1085 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) Buffer;
1086 //
1087 // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume
1088 // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from
1089 // its initial linked location and maintain its alignment.
1090 //
1091 if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {
1092 //
1093 // Get FvHeader alignment
1094 //
1095 FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ALIGNMENT) >> 16);
1096 //
1097 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.
1098 //
1099 if (FvAlignment < 8) {
1100 FvAlignment = 8;
1101 }
1102
1103 DEBUG ((
1104 DEBUG_INFO,
1105 "%a() FV at 0x%x, FvAlignment required is 0x%x\n",
1106 __FUNCTION__,
1107 FvHeader,
1108 FvAlignment
1109 ));
1110
1111 //
1112 // Check FvImage alignment.
1113 //
1114 if ((UINTN) FvHeader % FvAlignment != 0) {
1115 //
1116 // Allocate the aligned buffer for the FvImage.
1117 //
1118 AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN) FvAlignment);
1119 if (AlignedBuffer == NULL) {
1120 FreePool (Buffer);
1121 return EFI_OUT_OF_RESOURCES;
1122 } else {
1123 //
1124 // Move FvImage into the aligned buffer and release the original buffer.
1125 //
1126 if (GetFvUsedSize (FvHeader, &FvUsedSize, &EraseByte)) {
1127 //
1128 // Copy the used bytes and fill the rest with the erase value.
1129 //
1130 CopyMem (AlignedBuffer, FvHeader, (UINTN) FvUsedSize);
1131 SetMem (
1132 (UINT8 *) AlignedBuffer + FvUsedSize,
1133 (UINTN) (BufferSize - FvUsedSize),
1134 EraseByte
1135 );
1136 } else {
1137 CopyMem (AlignedBuffer, Buffer, BufferSize);
1138 }
1139 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) AlignedBuffer;
1140 CoreFreePool (Buffer);
1141 Buffer = NULL;
1142 }
1143 }
1144 }
1145 //
1146 // Produce a FVB protocol for the file
1147 //
1148 Status = ProduceFVBProtocolOnBuffer (
1149 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
1150 (UINT64)BufferSize,
1151 FvHandle,
1152 AuthenticationStatus,
1153 NULL
1154 );
1155 }
1156
1157 if (EFI_ERROR (Status)) {
1158 //
1159 // ReadSection or Produce FVB failed, Free data buffer
1160 //
1161 if (Buffer != NULL) {
1162 FreePool (Buffer);
1163 }
1164
1165 if (AlignedBuffer != NULL) {
1166 FreeAlignedPages (AlignedBuffer, EFI_SIZE_TO_PAGES (BufferSize));
1167 }
1168 }
1169
1170 return Status;
1171 }
1172
1173
1174 /**
1175 Event notification that is fired every time a FV dispatch protocol is added.
1176 More than one protocol may have been added when this event is fired, so you
1177 must loop on CoreLocateHandle () to see how many protocols were added and
1178 do the following to each FV:
1179 If the Fv has already been processed, skip it. If the Fv has not been
1180 processed then mark it as being processed, as we are about to process it.
1181 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1182 mDiscoveredList is never free'ed and contains variables that define
1183 the other states the DXE driver transitions to..
1184 While you are at it read the A Priori file into memory.
1185 Place drivers in the A Priori list onto the mScheduledQueue.
1186
1187 @param Event The Event that is being processed, not used.
1188 @param Context Event Context, not used.
1189
1190 **/
1191 VOID
1192 EFIAPI
1193 CoreFwVolEventProtocolNotify (
1194 IN EFI_EVENT Event,
1195 IN VOID *Context
1196 )
1197 {
1198 EFI_STATUS Status;
1199 EFI_STATUS GetNextFileStatus;
1200 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1201 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1202 EFI_HANDLE FvHandle;
1203 UINTN BufferSize;
1204 EFI_GUID NameGuid;
1205 UINTN Key;
1206 EFI_FV_FILETYPE Type;
1207 EFI_FV_FILE_ATTRIBUTES Attributes;
1208 UINTN Size;
1209 EFI_CORE_DRIVER_ENTRY *DriverEntry;
1210 EFI_GUID *AprioriFile;
1211 UINTN AprioriEntryCount;
1212 UINTN Index;
1213 LIST_ENTRY *Link;
1214 UINT32 AuthenticationStatus;
1215 UINTN SizeOfBuffer;
1216 VOID *DepexBuffer;
1217 KNOWN_HANDLE *KnownHandle;
1218
1219 FvHandle = NULL;
1220
1221 while (TRUE) {
1222 BufferSize = sizeof (EFI_HANDLE);
1223 Status = CoreLocateHandle (
1224 ByRegisterNotify,
1225 NULL,
1226 mFwVolEventRegistration,
1227 &BufferSize,
1228 &FvHandle
1229 );
1230 if (EFI_ERROR (Status)) {
1231 //
1232 // If no more notification events exit
1233 //
1234 return;
1235 }
1236
1237 if (FvHasBeenProcessed (FvHandle)) {
1238 //
1239 // This Fv has already been processed so lets skip it!
1240 //
1241 continue;
1242 }
1243
1244 //
1245 // Since we are about to process this Fv mark it as processed.
1246 //
1247 KnownHandle = FvIsBeingProcesssed (FvHandle);
1248 if (KnownHandle == NULL) {
1249 //
1250 // The FV with the same FV name guid has already been processed.
1251 // So lets skip it!
1252 //
1253 continue;
1254 }
1255
1256 Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
1257 if (EFI_ERROR (Status) || Fv == NULL) {
1258 //
1259 // FvHandle must have Firmware Volume2 protocol thus we should never get here.
1260 //
1261 ASSERT (FALSE);
1262 continue;
1263 }
1264
1265 Status = CoreHandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1266 if (EFI_ERROR (Status)) {
1267 //
1268 // The Firmware volume doesn't have device path, can't be dispatched.
1269 //
1270 continue;
1271 }
1272
1273 //
1274 // Discover Drivers in FV and add them to the Discovered Driver List.
1275 // Process EFI_FV_FILETYPE_DRIVER type and then EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
1276 // EFI_FV_FILETYPE_DXE_CORE is processed to produce a Loaded Image protocol for the core
1277 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE is processed to create a Fvb
1278 //
1279 for (Index = 0; Index < sizeof (mDxeFileTypes) / sizeof (EFI_FV_FILETYPE); Index++) {
1280 //
1281 // Initialize the search key
1282 //
1283 Key = 0;
1284 do {
1285 Type = mDxeFileTypes[Index];
1286 GetNextFileStatus = Fv->GetNextFile (
1287 Fv,
1288 &Key,
1289 &Type,
1290 &NameGuid,
1291 &Attributes,
1292 &Size
1293 );
1294 if (!EFI_ERROR (GetNextFileStatus)) {
1295 if (Type == EFI_FV_FILETYPE_DXE_CORE) {
1296 //
1297 // If this is the DXE core fill in it's DevicePath & DeviceHandle
1298 //
1299 if (gDxeCoreLoadedImage->FilePath == NULL) {
1300 if (CompareGuid (&NameGuid, gDxeCoreFileName)) {
1301 //
1302 // Maybe One specail Fv cantains only one DXE_CORE module, so its device path must
1303 // be initialized completely.
1304 //
1305 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);
1306 SetDevicePathEndNode (&mFvDevicePath.End);
1307
1308 gDxeCoreLoadedImage->FilePath = DuplicateDevicePath (
1309 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath
1310 );
1311 gDxeCoreLoadedImage->DeviceHandle = FvHandle;
1312 }
1313 }
1314 } else if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
1315 //
1316 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
1317 // been extracted.
1318 //
1319 if (FvFoundInHobFv2 (&KnownHandle->FvNameGuid, &NameGuid)) {
1320 continue;
1321 }
1322
1323 //
1324 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has SMM depex section.
1325 //
1326 DepexBuffer = NULL;
1327 SizeOfBuffer = 0;
1328 Status = Fv->ReadSection (
1329 Fv,
1330 &NameGuid,
1331 EFI_SECTION_SMM_DEPEX,
1332 0,
1333 &DepexBuffer,
1334 &SizeOfBuffer,
1335 &AuthenticationStatus
1336 );
1337 if (!EFI_ERROR (Status)) {
1338 //
1339 // If SMM depex section is found, this FV image is invalid to be supported.
1340 // ASSERT FALSE to report this FV image.
1341 //
1342 FreePool (DepexBuffer);
1343 ASSERT (FALSE);
1344 }
1345
1346 //
1347 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has DXE depex section.
1348 //
1349 DepexBuffer = NULL;
1350 SizeOfBuffer = 0;
1351 Status = Fv->ReadSection (
1352 Fv,
1353 &NameGuid,
1354 EFI_SECTION_DXE_DEPEX,
1355 0,
1356 &DepexBuffer,
1357 &SizeOfBuffer,
1358 &AuthenticationStatus
1359 );
1360 if (EFI_ERROR (Status)) {
1361 //
1362 // If no depex section, produce a firmware volume block protocol for it so it gets dispatched from.
1363 //
1364 CoreProcessFvImageFile (Fv, FvHandle, &NameGuid);
1365 } else {
1366 //
1367 // If depex section is found, this FV image will be dispatched until its depex is evaluated to TRUE.
1368 //
1369 FreePool (DepexBuffer);
1370 CoreAddToDriverList (Fv, FvHandle, &NameGuid, Type);
1371 }
1372 } else {
1373 //
1374 // Transition driver from Undiscovered to Discovered state
1375 //
1376 CoreAddToDriverList (Fv, FvHandle, &NameGuid, Type);
1377 }
1378 }
1379 } while (!EFI_ERROR (GetNextFileStatus));
1380 }
1381
1382 //
1383 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1384 //
1385 AprioriFile = NULL;
1386 Status = Fv->ReadSection (
1387 Fv,
1388 &gAprioriGuid,
1389 EFI_SECTION_RAW,
1390 0,
1391 (VOID **)&AprioriFile,
1392 &SizeOfBuffer,
1393 &AuthenticationStatus
1394 );
1395 if (!EFI_ERROR (Status)) {
1396 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);
1397 } else {
1398 AprioriEntryCount = 0;
1399 }
1400
1401 //
1402 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1403 // drivers not in the current FV and these must be skipped since the a priori list
1404 // is only valid for the FV that it resided in.
1405 //
1406
1407 for (Index = 0; Index < AprioriEntryCount; Index++) {
1408 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
1409 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
1410 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&
1411 (FvHandle == DriverEntry->FvHandle)) {
1412 CoreAcquireDispatcherLock ();
1413 DriverEntry->Dependent = FALSE;
1414 DriverEntry->Scheduled = TRUE;
1415 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);
1416 CoreReleaseDispatcherLock ();
1417 DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));
1418 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));
1419 break;
1420 }
1421 }
1422 }
1423
1424 //
1425 // Free data allocated by Fv->ReadSection ()
1426 //
1427 CoreFreePool (AprioriFile);
1428 }
1429 }
1430
1431
1432
1433 /**
1434 Initialize the dispatcher. Initialize the notification function that runs when
1435 an FV2 protocol is added to the system.
1436
1437 **/
1438 VOID
1439 CoreInitializeDispatcher (
1440 VOID
1441 )
1442 {
1443 PERF_FUNCTION_BEGIN ();
1444
1445 mFwVolEvent = EfiCreateProtocolNotifyEvent (
1446 &gEfiFirmwareVolume2ProtocolGuid,
1447 TPL_CALLBACK,
1448 CoreFwVolEventProtocolNotify,
1449 NULL,
1450 &mFwVolEventRegistration
1451 );
1452
1453 PERF_FUNCTION_END ();
1454 }
1455
1456 //
1457 // Function only used in debug builds
1458 //
1459
1460 /**
1461 Traverse the discovered list for any drivers that were discovered but not loaded
1462 because the dependency experessions evaluated to false.
1463
1464 **/
1465 VOID
1466 CoreDisplayDiscoveredNotDispatched (
1467 VOID
1468 )
1469 {
1470 LIST_ENTRY *Link;
1471 EFI_CORE_DRIVER_ENTRY *DriverEntry;
1472
1473 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
1474 DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);
1475 if (DriverEntry->Dependent) {
1476 DEBUG ((DEBUG_LOAD, "Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
1477 }
1478 }
1479 }