878ddf1f |
1 | /*++\r |
2 | \r |
3 | Copyright (c) 2006, Intel Corporation\r |
4 | All rights reserved. This program and the accompanying materials\r |
5 | are licensed and made available under the terms and conditions of the BSD License\r |
6 | which accompanies this distribution. The full text of the license may be found at\r |
7 | http://opensource.org/licenses/bsd-license.php\r |
8 | \r |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
11 | \r |
12 | Module Name:\r |
13 | \r |
14 | Dispatcher.c\r |
15 | \r |
16 | Abstract:\r |
17 | \r |
18 | EFI PEI Core dispatch services\r |
19 | \r |
20 | Revision History\r |
21 | \r |
22 | --*/\r |
23 | \r |
24 | #include <PeiMain.h>\r |
25 | \r |
26 | VOID *\r |
27 | TransferOldDataToNewDataRange (\r |
28 | IN PEI_CORE_INSTANCE *PrivateData\r |
29 | );\r |
30 | \r |
31 | EFI_GUID gEfiPeiCorePrivateGuid = EFI_PEI_CORE_PRIVATE_GUID;\r |
32 | \r |
33 | \r |
34 | EFI_STATUS\r |
35 | PeiDispatcher (\r |
36 | IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor,\r |
37 | IN PEI_CORE_INSTANCE *PrivateData,\r |
38 | IN PEI_CORE_DISPATCH_DATA *DispatchData\r |
39 | )\r |
40 | \r |
41 | /*++\r |
42 | \r |
43 | Routine Description:\r |
44 | \r |
45 | Conduct PEIM dispatch.\r |
46 | \r |
47 | Arguments:\r |
48 | \r |
49 | PeiStartupDescriptor - Pointer to IN EFI_PEI_STARTUP_DESCRIPTOR\r |
50 | PrivateData - Pointer to the private data passed in from caller\r |
51 | DispatchData - Pointer to PEI_CORE_DISPATCH_DATA data.\r |
52 | \r |
53 | Returns:\r |
54 | \r |
55 | EFI_SUCCESS - Successfully dispatched PEIM.\r |
56 | EFI_NOT_FOUND - The dispatch failed.\r |
57 | \r |
58 | --*/\r |
59 | {\r |
60 | EFI_STATUS Status;\r |
61 | PEI_CORE_TEMP_POINTERS TempPtr;\r |
62 | UINTN PrivateDataInMem;\r |
63 | BOOLEAN NextFvFound;\r |
64 | EFI_FIRMWARE_VOLUME_HEADER *NextFvAddress;\r |
65 | EFI_FIRMWARE_VOLUME_HEADER *DefaultFvAddress;\r |
66 | //\r |
67 | // Debug data for uninstalled Peim list\r |
68 | //\r |
69 | EFI_GUID DebugFoundPeimList[32];\r |
70 | REPORT_STATUS_CODE_LIBRARY_DEVICE_HANDLE_EXTENDED_DATA ExtendedData;\r |
71 | \r |
72 | //\r |
73 | // save the Current FV Address so that we will not process it again if FindFv returns it later\r |
74 | //\r |
75 | DefaultFvAddress = DispatchData->BootFvAddress;\r |
76 | \r |
77 | //\r |
78 | // This is the main dispatch loop. It will search known FVs for PEIMs and\r |
79 | // attempt to dispatch them. If any PEIM gets dispatched through a single\r |
80 | // pass of the dispatcher, it will start over from the Bfv again to see\r |
81 | // if any new PEIMs dependencies got satisfied. With a well ordered\r |
82 | // FV where PEIMs are found in the order their dependencies are also\r |
83 | // satisfied, this dipatcher should run only once.\r |
84 | //\r |
85 | for (;;) {\r |
86 | //\r |
87 | // This is the PEIM search loop. It will scan through all PEIMs it can find\r |
88 | // looking for PEIMs to dispatch, and will dipatch them if they have not\r |
89 | // already been dispatched and all of their dependencies are met.\r |
90 | // If no more PEIMs can be found in this pass through all known FVs,\r |
91 | // then it will break out of this loop.\r |
92 | //\r |
93 | for (;;) {\r |
94 | \r |
95 | Status = FindNextPeim (\r |
96 | &PrivateData->PS,\r |
97 | DispatchData->CurrentFvAddress,\r |
98 | &DispatchData->CurrentPeimAddress\r |
99 | );\r |
100 | \r |
101 | //\r |
102 | // If we found a PEIM, check if it is dispatched. If so, go to the\r |
103 | // next PEIM. If not, dispatch it if its dependencies are satisfied.\r |
104 | // If its dependencies are not satisfied, go to the next PEIM.\r |
105 | //\r |
106 | if (Status == EFI_SUCCESS) {\r |
107 | \r |
2ce31132 |
108 | DEBUG_CODE_BEGIN ();\r |
878ddf1f |
109 | \r |
110 | //\r |
111 | // Fill list of found Peims for later list of those not installed\r |
112 | //\r |
113 | CopyMem (\r |
114 | &DebugFoundPeimList[DispatchData->CurrentPeim],\r |
115 | &DispatchData->CurrentPeimAddress->Name,\r |
116 | sizeof (EFI_GUID)\r |
117 | );\r |
118 | \r |
2ce31132 |
119 | DEBUG_CODE_END ();\r |
878ddf1f |
120 | \r |
121 | if (!Dispatched (\r |
122 | DispatchData->CurrentPeim,\r |
123 | DispatchData->DispatchedPeimBitMap\r |
124 | )) {\r |
125 | if (DepexSatisfied (&PrivateData->PS, DispatchData->CurrentPeimAddress)) {\r |
126 | Status = PeiLoadImage (\r |
127 | &PrivateData->PS,\r |
128 | DispatchData->CurrentPeimAddress,\r |
129 | &TempPtr.Raw\r |
130 | );\r |
131 | if (Status == EFI_SUCCESS) {\r |
132 | \r |
133 | //\r |
134 | // The PEIM has its dependencies satisfied, and its entry point\r |
135 | // has been found, so invoke it.\r |
136 | //\r |
137 | PERF_START (\r |
138 | (VOID *) (UINTN) (DispatchData->CurrentPeimAddress),\r |
139 | "PEIM",\r |
140 | NULL,\r |
141 | 0\r |
142 | );\r |
143 | \r |
144 | //\r |
145 | // BUGBUG: Used to be EFI_PEI_REPORT_STATUS_CODE_CODE\r |
146 | //\r |
147 | ExtendedData.Handle = (EFI_HANDLE)DispatchData->CurrentPeimAddress;\r |
148 | \r |
149 | REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r |
150 | EFI_PROGRESS_CODE,\r |
151 | EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN,\r |
152 | (VOID *)(&ExtendedData),\r |
153 | sizeof (ExtendedData)\r |
154 | );\r |
155 | \r |
156 | //\r |
157 | // Is this a authentic image\r |
158 | //\r |
159 | Status = VerifyPeim (\r |
160 | &PrivateData->PS,\r |
161 | DispatchData->CurrentPeimAddress\r |
162 | );\r |
163 | \r |
164 | if (Status != EFI_SECURITY_VIOLATION) {\r |
165 | \r |
166 | Status = TempPtr.PeimEntry (\r |
167 | DispatchData->CurrentPeimAddress,\r |
168 | &PrivateData->PS\r |
169 | );\r |
170 | }\r |
171 | \r |
172 | REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r |
173 | EFI_PROGRESS_CODE,\r |
174 | EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END,\r |
175 | (VOID *)(&ExtendedData),\r |
176 | sizeof (ExtendedData)\r |
177 | );\r |
178 | \r |
179 | PERF_END ((VOID *) (UINTN) (DispatchData->CurrentPeimAddress), "PEIM", NULL, 0);\r |
180 | \r |
181 | //\r |
182 | // Mark the PEIM as dispatched so we don't attempt to run it again\r |
183 | //\r |
184 | SetDispatched (\r |
185 | &PrivateData->PS,\r |
186 | DispatchData->CurrentPeim,\r |
187 | &DispatchData->DispatchedPeimBitMap\r |
188 | );\r |
189 | \r |
190 | //\r |
191 | // Process the Notify list and dispatch any notifies for\r |
192 | // newly installed PPIs.\r |
193 | //\r |
194 | ProcessNotifyList (&PrivateData->PS);\r |
195 | \r |
196 | //\r |
197 | // If real system memory was discovered and installed by this\r |
198 | // PEIM, switch the stacks to the new memory. Since we are\r |
199 | // at dispatch level, only the Core's private data is preserved,\r |
200 | // nobody else should have any data on the stack.\r |
201 | //\r |
202 | if (PrivateData->SwitchStackSignal) {\r |
203 | TempPtr.PeiCore = (PEI_CORE_ENTRY_POINT)PeiCore;\r |
204 | PrivateDataInMem = (UINTN) TransferOldDataToNewDataRange (PrivateData);\r |
205 | ASSERT (PrivateDataInMem != 0);\r |
206 | //\r |
207 | //Subtract 0x10 from the 4th parameter indicating the new stack base,\r |
208 | //in order to provide buffer protection against possible illegal stack\r |
209 | //access that might corrupt the stack.\r |
210 | //\r |
211 | SwitchStack (\r |
212 | (SWITCH_STACK_ENTRY_POINT)(UINTN)TempPtr.Raw,\r |
213 | PeiStartupDescriptor,\r |
214 | (VOID*)PrivateDataInMem,\r |
215 | (VOID*)((UINTN)PrivateData->StackBase + (UINTN)PrivateData->StackSize)\r |
216 | );\r |
217 | }\r |
218 | }\r |
219 | }\r |
220 | }\r |
221 | DispatchData->CurrentPeim++;\r |
222 | continue;\r |
223 | \r |
224 | } else {\r |
225 | \r |
226 | //\r |
227 | // If we could not find another PEIM in the current FV, go try\r |
228 | // the FindFv PPI to look in other FVs for more PEIMs. If we can\r |
229 | // not locate the FindFv PPI, or if the FindFv PPI can not find\r |
230 | // anymore FVs, then exit the PEIM search loop.\r |
231 | //\r |
232 | if (DispatchData->FindFv == NULL) {\r |
84a99d48 |
233 | Status = PeiServicesLocatePpi (\r |
878ddf1f |
234 | &gEfiFindFvPpiGuid,\r |
235 | 0,\r |
236 | NULL,\r |
237 | (VOID **)&DispatchData->FindFv\r |
238 | );\r |
239 | if (Status != EFI_SUCCESS) {\r |
240 | break;\r |
241 | }\r |
242 | }\r |
243 | NextFvFound = FALSE;\r |
244 | while (!NextFvFound) {\r |
245 | Status = DispatchData->FindFv->FindFv (\r |
246 | DispatchData->FindFv,\r |
247 | &PrivateData->PS,\r |
248 | &DispatchData->CurrentFv,\r |
249 | &NextFvAddress\r |
250 | );\r |
251 | //\r |
252 | // if there is no next fv, get out of this loop of finding FVs\r |
253 | //\r |
254 | if (Status != EFI_SUCCESS) {\r |
255 | break;\r |
256 | }\r |
257 | //\r |
258 | // don't process the default Fv again. (we don't know the order in which the hobs were created)\r |
259 | //\r |
260 | if ((NextFvAddress != DefaultFvAddress) &&\r |
261 | (NextFvAddress != DispatchData->CurrentFvAddress)) {\r |
262 | \r |
263 | //\r |
264 | // VerifyFv() is currently returns SUCCESS all the time, add code to it to\r |
265 | // actually verify the given FV\r |
266 | //\r |
267 | Status = VerifyFv (NextFvAddress);\r |
268 | if (Status == EFI_SUCCESS) {\r |
269 | NextFvFound = TRUE;\r |
270 | DispatchData->CurrentFvAddress = NextFvAddress;\r |
271 | DispatchData->CurrentPeimAddress = NULL;\r |
272 | //\r |
273 | // current PRIM number (CurrentPeim) must continue as is, don't reset it here\r |
274 | //\r |
275 | }\r |
276 | }\r |
277 | }\r |
278 | //\r |
279 | // if there is no next fv, get out of this loop of dispatching PEIMs\r |
280 | //\r |
281 | if (!NextFvFound) {\r |
282 | break;\r |
283 | }\r |
284 | //\r |
285 | // continue in the inner for(;;) loop with a new FV;\r |
286 | //\r |
287 | }\r |
288 | }\r |
289 | \r |
290 | //\r |
291 | // If all the PEIMs that we have found have been dispatched, then\r |
292 | // there is nothing left to dispatch and we don't need to go search\r |
293 | // through all PEIMs again.\r |
294 | //\r |
295 | if ((~(DispatchData->DispatchedPeimBitMap) &\r |
296 | ((1 << DispatchData->CurrentPeim)-1)) == 0) {\r |
297 | break;\r |
298 | }\r |
299 | \r |
300 | //\r |
301 | // Check if no more PEIMs that depex was satisfied\r |
302 | //\r |
303 | if (DispatchData->DispatchedPeimBitMap == DispatchData->PreviousPeimBitMap) {\r |
304 | break;\r |
305 | }\r |
306 | \r |
307 | //\r |
308 | // Case when Depex is not satisfied and has to traverse the list again\r |
309 | //\r |
310 | DispatchData->CurrentPeim = 0;\r |
311 | DispatchData->CurrentPeimAddress = 0;\r |
312 | DispatchData->PreviousPeimBitMap = DispatchData->DispatchedPeimBitMap;\r |
313 | \r |
314 | //\r |
315 | // don't go back to the loop without making sure that the CurrentFvAddress is the\r |
316 | // same as the 1st (or default) FV we started with. otherwise we will interpret the bimap wrongly and\r |
317 | // mess it up, always start processing the PEIMs from the default FV just like in the first time around.\r |
318 | //\r |
319 | DispatchData->CurrentFv = 0;\r |
320 | DispatchData->CurrentFvAddress = DefaultFvAddress;\r |
321 | }\r |
322 | \r |
2ce31132 |
323 | DEBUG_CODE_BEGIN ();\r |
878ddf1f |
324 | //\r |
325 | // Debug data for uninstalled Peim list\r |
326 | //\r |
327 | UINT32 DebugNotDispatchedBitmap;\r |
328 | UINT8 DebugFoundPeimPoint;\r |
329 | \r |
330 | DebugFoundPeimPoint = 0;\r |
331 | //\r |
332 | // Get bitmap of Peims that were not dispatched,\r |
333 | //\r |
334 | \r |
335 | DebugNotDispatchedBitmap = ((DispatchData->DispatchedPeimBitMap) ^ ((1 << DispatchData->CurrentPeim)-1));\r |
336 | //\r |
337 | // Scan bitmap of Peims not installed and print GUIDS\r |
338 | //\r |
339 | while (DebugNotDispatchedBitmap != 0) {\r |
340 | if ((DebugNotDispatchedBitmap & 1) != 0) {\r |
341 | DEBUG ((EFI_D_INFO, "WARNING -> InstallPpi: Not Installed: %g\n",\r |
342 | &DebugFoundPeimList[DebugFoundPeimPoint]\r |
343 | ));\r |
344 | }\r |
345 | DebugFoundPeimPoint++;\r |
346 | DebugNotDispatchedBitmap >>= 1;\r |
347 | }\r |
2ce31132 |
348 | \r |
349 | DEBUG_CODE_END ();\r |
878ddf1f |
350 | \r |
2ce31132 |
351 | return EFI_NOT_FOUND;\r |
878ddf1f |
352 | }\r |
353 | \r |
354 | VOID\r |
355 | InitializeDispatcherData (\r |
356 | IN EFI_PEI_SERVICES **PeiServices,\r |
357 | IN PEI_CORE_INSTANCE *OldCoreData,\r |
358 | IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor\r |
359 | )\r |
360 | /*++\r |
361 | \r |
362 | Routine Description:\r |
363 | \r |
364 | Initialize the Dispatcher's data members\r |
365 | \r |
366 | Arguments:\r |
367 | \r |
368 | PeiServices - The PEI core services table.\r |
369 | OldCoreData - Pointer to old core data (before switching stack).\r |
370 | NULL if being run in non-permament memory mode.\r |
371 | PeiStartupDescriptor - Information and services provided by SEC phase.\r |
372 | \r |
373 | Returns:\r |
374 | \r |
375 | None.\r |
376 | \r |
377 | --*/\r |
378 | {\r |
379 | PEI_CORE_INSTANCE *PrivateData;\r |
380 | \r |
381 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r |
382 | \r |
383 | if (OldCoreData == NULL) {\r |
384 | PrivateData->DispatchData.CurrentFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) PeiStartupDescriptor->BootFirmwareVolume;\r |
385 | PrivateData->DispatchData.BootFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) PeiStartupDescriptor->BootFirmwareVolume;\r |
386 | } else {\r |
387 | \r |
388 | //\r |
389 | // Current peim has been dispatched, but not count\r |
390 | //\r |
391 | PrivateData->DispatchData.CurrentPeim = (UINT8)(OldCoreData->DispatchData.CurrentPeim + 1);\r |
392 | }\r |
393 | \r |
394 | return;\r |
395 | }\r |
396 | \r |
397 | \r |
398 | BOOLEAN\r |
399 | Dispatched (\r |
400 | IN UINT8 CurrentPeim,\r |
401 | IN UINT32 DispatchedPeimBitMap\r |
402 | )\r |
403 | /*++\r |
404 | \r |
405 | Routine Description:\r |
406 | \r |
407 | This routine checks to see if a particular PEIM has been dispatched during\r |
408 | the PEI core dispatch.\r |
409 | \r |
410 | Arguments:\r |
411 | CurrentPeim - The PEIM/FV in the bit array to check.\r |
412 | DispatchedPeimBitMap - Bit array, each bit corresponds to a PEIM/FV.\r |
413 | \r |
414 | Returns:\r |
415 | TRUE - PEIM already dispatched\r |
416 | FALSE - Otherwise\r |
417 | \r |
418 | --*/\r |
419 | {\r |
420 | return (BOOLEAN)((DispatchedPeimBitMap & (1 << CurrentPeim)) != 0);\r |
421 | }\r |
422 | \r |
423 | VOID\r |
424 | SetDispatched (\r |
425 | IN EFI_PEI_SERVICES **PeiServices,\r |
426 | IN UINT8 CurrentPeim,\r |
427 | OUT UINT32 *DispatchedPeimBitMap\r |
428 | )\r |
429 | /*++\r |
430 | \r |
431 | Routine Description:\r |
432 | \r |
433 | This routine sets a PEIM as having been dispatched once its entry\r |
434 | point has been invoked.\r |
435 | \r |
436 | Arguments:\r |
437 | \r |
438 | PeiServices - The PEI core services table.\r |
439 | CurrentPeim - The PEIM/FV in the bit array to check.\r |
440 | DispatchedPeimBitMap - Bit array, each bit corresponds to a PEIM/FV.\r |
441 | \r |
442 | Returns:\r |
443 | None\r |
444 | \r |
445 | --*/\r |
446 | {\r |
447 | //\r |
448 | // Check if the total number of PEIMs exceed the bitmap.\r |
449 | // CurrentPeim is 0-based\r |
450 | //\r |
4ba61e5e |
451 | ASSERT (CurrentPeim < (sizeof (*DispatchedPeimBitMap) * 8));\r |
878ddf1f |
452 | *DispatchedPeimBitMap |= (1 << CurrentPeim);\r |
453 | return;\r |
454 | }\r |
455 | \r |
456 | BOOLEAN\r |
457 | DepexSatisfied (\r |
458 | IN EFI_PEI_SERVICES **PeiServices,\r |
459 | IN VOID *CurrentPeimAddress\r |
460 | )\r |
461 | /*++\r |
462 | \r |
463 | Routine Description:\r |
464 | \r |
465 | This routine parses the Dependency Expression, if available, and\r |
466 | decides if the module can be executed.\r |
467 | \r |
468 | Arguments:\r |
469 | PeiServices - The PEI Service Table\r |
470 | CurrentPeimAddress - Address of the PEIM Firmware File under investigation\r |
471 | \r |
472 | Returns:\r |
473 | TRUE - Can be dispatched\r |
474 | FALSE - Cannot be dispatched\r |
475 | \r |
476 | --*/\r |
477 | {\r |
478 | EFI_STATUS Status;\r |
479 | INT8 *DepexData;\r |
480 | BOOLEAN Runnable;\r |
481 | \r |
84a99d48 |
482 | Status = PeiServicesFfsFindSectionData (\r |
878ddf1f |
483 | EFI_SECTION_PEI_DEPEX,\r |
484 | CurrentPeimAddress,\r |
485 | (VOID **)&DepexData\r |
486 | );\r |
487 | //\r |
488 | // If there is no DEPEX, assume the module can be executed\r |
489 | //\r |
490 | if (EFI_ERROR (Status)) {\r |
491 | return TRUE;\r |
492 | }\r |
493 | \r |
494 | //\r |
495 | // Evaluate a given DEPEX\r |
496 | //\r |
497 | Status = PeimDispatchReadiness (\r |
498 | PeiServices,\r |
499 | DepexData,\r |
500 | &Runnable\r |
501 | );\r |
502 | \r |
503 | return Runnable;\r |
504 | }\r |
505 | \r |
506 | \r |
507 | VOID *\r |
508 | TransferOldDataToNewDataRange (\r |
509 | IN PEI_CORE_INSTANCE *PrivateData\r |
510 | )\r |
511 | /*++\r |
512 | \r |
513 | Routine Description:\r |
514 | \r |
515 | This routine transfers the contents of the pre-permanent memory\r |
516 | PEI Core private data to a post-permanent memory data location.\r |
517 | \r |
518 | Arguments:\r |
519 | \r |
520 | PrivateData - Pointer to the current PEI Core private data pre-permanent memory\r |
521 | \r |
522 | Returns:\r |
523 | \r |
524 | Pointer to the PrivateData once the private data has been transferred to permanent memory\r |
525 | \r |
526 | --*/\r |
527 | {\r |
528 | return BuildGuidDataHob (&gEfiPeiCorePrivateGuid, PrivateData, sizeof (PEI_CORE_INSTANCE));\r |
529 | }\r |
530 | \r |