]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Core/Pei/Dispatcher/Dispatcher.c
added EFIAPI for those constructor functions
[mirror_edk2.git] / EdkModulePkg / Core / Pei / Dispatcher / Dispatcher.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 Dispatcher.c\r
15\r
16Abstract:\r
17\r
18 EFI PEI Core dispatch services\r
19\r
20Revision History\r
21\r
22--*/\r
23\r
24#include <PeiMain.h>\r
25\r
26VOID *\r
27TransferOldDataToNewDataRange (\r
28 IN PEI_CORE_INSTANCE *PrivateData\r
29 );\r
30\r
31EFI_GUID gEfiPeiCorePrivateGuid = EFI_PEI_CORE_PRIVATE_GUID;\r
32\r
33\r
34EFI_STATUS\r
35PeiDispatcher (\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
43Routine Description:\r
44\r
45 Conduct PEIM dispatch.\r
46\r
47Arguments:\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
53Returns:\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
eeb1cd5a 211 PeiSwitchStacks (\r
878ddf1f 212 (SWITCH_STACK_ENTRY_POINT)(UINTN)TempPtr.Raw,\r
213 PeiStartupDescriptor,\r
214 (VOID*)PrivateDataInMem,\r
eeb1cd5a 215 (VOID*)((UINTN)PrivateData->StackBase + (UINTN)PrivateData->StackSize),\r
216 (VOID*)(UINTN)PrivateData->StackBase\r
217 );\r
878ddf1f 218 }\r
219 }\r
220 }\r
221 }\r
222 DispatchData->CurrentPeim++;\r
223 continue;\r
224\r
225 } else {\r
226\r
227 //\r
228 // If we could not find another PEIM in the current FV, go try\r
229 // the FindFv PPI to look in other FVs for more PEIMs. If we can\r
230 // not locate the FindFv PPI, or if the FindFv PPI can not find\r
231 // anymore FVs, then exit the PEIM search loop.\r
232 //\r
233 if (DispatchData->FindFv == NULL) {\r
84a99d48 234 Status = PeiServicesLocatePpi (\r
878ddf1f 235 &gEfiFindFvPpiGuid,\r
236 0,\r
237 NULL,\r
238 (VOID **)&DispatchData->FindFv\r
239 );\r
240 if (Status != EFI_SUCCESS) {\r
241 break;\r
242 }\r
243 }\r
244 NextFvFound = FALSE;\r
245 while (!NextFvFound) {\r
246 Status = DispatchData->FindFv->FindFv (\r
247 DispatchData->FindFv,\r
248 &PrivateData->PS,\r
249 &DispatchData->CurrentFv,\r
250 &NextFvAddress\r
251 );\r
252 //\r
253 // if there is no next fv, get out of this loop of finding FVs\r
254 //\r
255 if (Status != EFI_SUCCESS) {\r
256 break;\r
257 }\r
258 //\r
259 // don't process the default Fv again. (we don't know the order in which the hobs were created)\r
260 //\r
261 if ((NextFvAddress != DefaultFvAddress) &&\r
262 (NextFvAddress != DispatchData->CurrentFvAddress)) {\r
263\r
264 //\r
265 // VerifyFv() is currently returns SUCCESS all the time, add code to it to\r
266 // actually verify the given FV\r
267 //\r
268 Status = VerifyFv (NextFvAddress);\r
269 if (Status == EFI_SUCCESS) {\r
270 NextFvFound = TRUE;\r
271 DispatchData->CurrentFvAddress = NextFvAddress;\r
272 DispatchData->CurrentPeimAddress = NULL;\r
273 //\r
274 // current PRIM number (CurrentPeim) must continue as is, don't reset it here\r
275 //\r
276 }\r
277 }\r
278 }\r
279 //\r
280 // if there is no next fv, get out of this loop of dispatching PEIMs\r
281 //\r
282 if (!NextFvFound) {\r
283 break;\r
284 }\r
285 //\r
286 // continue in the inner for(;;) loop with a new FV;\r
287 //\r
288 }\r
289 }\r
290\r
291 //\r
292 // If all the PEIMs that we have found have been dispatched, then\r
293 // there is nothing left to dispatch and we don't need to go search\r
294 // through all PEIMs again.\r
295 //\r
296 if ((~(DispatchData->DispatchedPeimBitMap) &\r
297 ((1 << DispatchData->CurrentPeim)-1)) == 0) {\r
298 break;\r
299 }\r
300\r
301 //\r
302 // Check if no more PEIMs that depex was satisfied\r
303 //\r
304 if (DispatchData->DispatchedPeimBitMap == DispatchData->PreviousPeimBitMap) {\r
305 break;\r
306 }\r
307\r
308 //\r
309 // Case when Depex is not satisfied and has to traverse the list again\r
310 //\r
311 DispatchData->CurrentPeim = 0;\r
312 DispatchData->CurrentPeimAddress = 0;\r
313 DispatchData->PreviousPeimBitMap = DispatchData->DispatchedPeimBitMap;\r
314\r
315 //\r
316 // don't go back to the loop without making sure that the CurrentFvAddress is the\r
317 // same as the 1st (or default) FV we started with. otherwise we will interpret the bimap wrongly and\r
318 // mess it up, always start processing the PEIMs from the default FV just like in the first time around.\r
319 //\r
320 DispatchData->CurrentFv = 0;\r
321 DispatchData->CurrentFvAddress = DefaultFvAddress;\r
322 }\r
323\r
2ce31132 324 DEBUG_CODE_BEGIN ();\r
878ddf1f 325 //\r
326 // Debug data for uninstalled Peim list\r
327 //\r
328 UINT32 DebugNotDispatchedBitmap;\r
329 UINT8 DebugFoundPeimPoint;\r
330\r
331 DebugFoundPeimPoint = 0;\r
332 //\r
333 // Get bitmap of Peims that were not dispatched,\r
334 //\r
335\r
336 DebugNotDispatchedBitmap = ((DispatchData->DispatchedPeimBitMap) ^ ((1 << DispatchData->CurrentPeim)-1));\r
337 //\r
338 // Scan bitmap of Peims not installed and print GUIDS\r
339 //\r
340 while (DebugNotDispatchedBitmap != 0) {\r
341 if ((DebugNotDispatchedBitmap & 1) != 0) {\r
342 DEBUG ((EFI_D_INFO, "WARNING -> InstallPpi: Not Installed: %g\n",\r
343 &DebugFoundPeimList[DebugFoundPeimPoint]\r
344 ));\r
345 }\r
346 DebugFoundPeimPoint++;\r
347 DebugNotDispatchedBitmap >>= 1;\r
348 }\r
eeb1cd5a 349\r
2ce31132 350 DEBUG_CODE_END ();\r
878ddf1f 351\r
2ce31132 352 return EFI_NOT_FOUND;\r
878ddf1f 353}\r
354\r
355VOID\r
356InitializeDispatcherData (\r
357 IN EFI_PEI_SERVICES **PeiServices,\r
358 IN PEI_CORE_INSTANCE *OldCoreData,\r
359 IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor\r
360 )\r
361/*++\r
362\r
363Routine Description:\r
364\r
365 Initialize the Dispatcher's data members\r
366\r
367Arguments:\r
368\r
369 PeiServices - The PEI core services table.\r
370 OldCoreData - Pointer to old core data (before switching stack).\r
371 NULL if being run in non-permament memory mode.\r
372 PeiStartupDescriptor - Information and services provided by SEC phase.\r
373\r
374Returns:\r
375\r
376 None.\r
377\r
378--*/\r
379{\r
380 PEI_CORE_INSTANCE *PrivateData;\r
381\r
382 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
383\r
384 if (OldCoreData == NULL) {\r
385 PrivateData->DispatchData.CurrentFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) PeiStartupDescriptor->BootFirmwareVolume;\r
386 PrivateData->DispatchData.BootFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) PeiStartupDescriptor->BootFirmwareVolume;\r
387 } else {\r
388\r
389 //\r
390 // Current peim has been dispatched, but not count\r
391 //\r
392 PrivateData->DispatchData.CurrentPeim = (UINT8)(OldCoreData->DispatchData.CurrentPeim + 1);\r
393 }\r
394\r
395 return;\r
396}\r
397\r
398\r
399BOOLEAN\r
400Dispatched (\r
401 IN UINT8 CurrentPeim,\r
402 IN UINT32 DispatchedPeimBitMap\r
403 )\r
404/*++\r
405\r
406Routine Description:\r
407\r
408 This routine checks to see if a particular PEIM has been dispatched during\r
409 the PEI core dispatch.\r
410\r
411Arguments:\r
412 CurrentPeim - The PEIM/FV in the bit array to check.\r
413 DispatchedPeimBitMap - Bit array, each bit corresponds to a PEIM/FV.\r
414\r
415Returns:\r
416 TRUE - PEIM already dispatched\r
417 FALSE - Otherwise\r
418\r
419--*/\r
420{\r
421 return (BOOLEAN)((DispatchedPeimBitMap & (1 << CurrentPeim)) != 0);\r
422}\r
423\r
424VOID\r
425SetDispatched (\r
426 IN EFI_PEI_SERVICES **PeiServices,\r
427 IN UINT8 CurrentPeim,\r
428 OUT UINT32 *DispatchedPeimBitMap\r
429 )\r
430/*++\r
431\r
432Routine Description:\r
433\r
434 This routine sets a PEIM as having been dispatched once its entry\r
435 point has been invoked.\r
436\r
437Arguments:\r
438\r
439 PeiServices - The PEI core services table.\r
440 CurrentPeim - The PEIM/FV in the bit array to check.\r
441 DispatchedPeimBitMap - Bit array, each bit corresponds to a PEIM/FV.\r
442\r
443Returns:\r
444 None\r
445\r
446--*/\r
447{\r
448 //\r
449 // Check if the total number of PEIMs exceed the bitmap.\r
450 // CurrentPeim is 0-based\r
451 //\r
4ba61e5e 452 ASSERT (CurrentPeim < (sizeof (*DispatchedPeimBitMap) * 8));\r
878ddf1f 453 *DispatchedPeimBitMap |= (1 << CurrentPeim);\r
454 return;\r
455}\r
456\r
457BOOLEAN\r
458DepexSatisfied (\r
459 IN EFI_PEI_SERVICES **PeiServices,\r
460 IN VOID *CurrentPeimAddress\r
461 )\r
462/*++\r
463\r
464Routine Description:\r
465\r
466 This routine parses the Dependency Expression, if available, and\r
467 decides if the module can be executed.\r
468\r
469Arguments:\r
470 PeiServices - The PEI Service Table\r
471 CurrentPeimAddress - Address of the PEIM Firmware File under investigation\r
472\r
473Returns:\r
474 TRUE - Can be dispatched\r
475 FALSE - Cannot be dispatched\r
476\r
477--*/\r
478{\r
479 EFI_STATUS Status;\r
480 INT8 *DepexData;\r
481 BOOLEAN Runnable;\r
482\r
84a99d48 483 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 484 EFI_SECTION_PEI_DEPEX,\r
485 CurrentPeimAddress,\r
486 (VOID **)&DepexData\r
487 );\r
488 //\r
489 // If there is no DEPEX, assume the module can be executed\r
490 //\r
491 if (EFI_ERROR (Status)) {\r
492 return TRUE;\r
493 }\r
494\r
495 //\r
496 // Evaluate a given DEPEX\r
497 //\r
498 Status = PeimDispatchReadiness (\r
499 PeiServices,\r
500 DepexData,\r
501 &Runnable\r
502 );\r
503\r
504 return Runnable;\r
505}\r
506\r
507\r
508VOID *\r
509TransferOldDataToNewDataRange (\r
510 IN PEI_CORE_INSTANCE *PrivateData\r
511 )\r
512/*++\r
513\r
514Routine Description:\r
515\r
516 This routine transfers the contents of the pre-permanent memory\r
517 PEI Core private data to a post-permanent memory data location.\r
518\r
519Arguments:\r
520\r
521 PrivateData - Pointer to the current PEI Core private data pre-permanent memory\r
522\r
523Returns:\r
524\r
525 Pointer to the PrivateData once the private data has been transferred to permanent memory\r
526\r
527--*/\r
528{\r
529 return BuildGuidDataHob (&gEfiPeiCorePrivateGuid, PrivateData, sizeof (PEI_CORE_INSTANCE));\r
530}\r
531\r