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