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