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