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