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