Add PeiCore module for enabling NT32Pkg, please attention this PeiCore does follows...
[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
24//\r
25// Include common header file for this module.\r
26//\r
27#include "CommonHeader.h"\r
28\r
29#include <PeiMain.h>\r
30\r
31STATIC\r
32VOID *\r
33TransferOldDataToNewDataRange (\r
34 IN PEI_CORE_INSTANCE *PrivateData\r
35 );\r
36\r
37EFI_STATUS\r
38PeiDispatcher (\r
39 IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor,\r
40 IN PEI_CORE_INSTANCE *PrivateData,\r
41 IN PEI_CORE_DISPATCH_DATA *DispatchData\r
42 )\r
43\r
44/*++\r
45\r
46Routine Description:\r
47\r
48 Conduct PEIM dispatch.\r
49\r
50Arguments:\r
51\r
52 PeiStartupDescriptor - Pointer to IN EFI_PEI_STARTUP_DESCRIPTOR\r
53 PrivateData - Pointer to the private data passed in from caller\r
54 DispatchData - Pointer to PEI_CORE_DISPATCH_DATA data.\r
55\r
56Returns:\r
57\r
58 EFI_SUCCESS - Successfully dispatched PEIM.\r
59 EFI_NOT_FOUND - The dispatch failed.\r
60\r
61--*/\r
62{\r
63 EFI_STATUS Status;\r
64 PEI_CORE_TEMP_POINTERS TempPtr;\r
65 UINTN PrivateDataInMem;\r
66 BOOLEAN NextFvFound;\r
67 EFI_FIRMWARE_VOLUME_HEADER *NextFvAddress;\r
68 EFI_FIRMWARE_VOLUME_HEADER *DefaultFvAddress;\r
69 VOID *TopOfStack;\r
70 //\r
71 // Debug data for uninstalled Peim list\r
72 //\r
73 EFI_GUID DebugFoundPeimList[32];\r
74 EFI_DEVICE_HANDLE_EXTENDED_DATA ExtendedData;\r
75\r
76 //\r
77 // save the Current FV Address so that we will not process it again if FindFv returns it later\r
78 //\r
79 DefaultFvAddress = DispatchData->BootFvAddress;\r
80\r
81 //\r
82 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
83 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
84 // pass of the dispatcher, it will start over from the Bfv again to see\r
85 // if any new PEIMs dependencies got satisfied. With a well ordered\r
86 // FV where PEIMs are found in the order their dependencies are also\r
87 // satisfied, this dipatcher should run only once.\r
88 //\r
89 for (;;) {\r
90 //\r
91 // This is the PEIM search loop. It will scan through all PEIMs it can find\r
92 // looking for PEIMs to dispatch, and will dipatch them if they have not\r
93 // already been dispatched and all of their dependencies are met.\r
94 // If no more PEIMs can be found in this pass through all known FVs,\r
95 // then it will break out of this loop.\r
96 //\r
97 for (;;) {\r
98\r
99 Status = FindNextPeim (\r
100 &PrivateData->PS,\r
101 DispatchData->CurrentFvAddress,\r
102 &DispatchData->CurrentPeimAddress\r
103 );\r
104\r
105 //\r
106 // If we found a PEIM, check if it is dispatched. If so, go to the\r
107 // next PEIM. If not, dispatch it if its dependencies are satisfied.\r
108 // If its dependencies are not satisfied, go to the next PEIM.\r
109 //\r
110 if (Status == EFI_SUCCESS) {\r
111\r
112 DEBUG_CODE_BEGIN ();\r
113\r
114 //\r
115 // Fill list of found Peims for later list of those not installed\r
116 //\r
117 CopyMem (\r
118 &DebugFoundPeimList[DispatchData->CurrentPeim],\r
119 &DispatchData->CurrentPeimAddress->Name,\r
120 sizeof (EFI_GUID)\r
121 );\r
122\r
123 DEBUG_CODE_END ();\r
124\r
125 if (!Dispatched (\r
126 DispatchData->CurrentPeim,\r
127 DispatchData->DispatchedPeimBitMap\r
128 )) {\r
129 if (DepexSatisfied (&PrivateData->PS, DispatchData->CurrentPeimAddress)) {\r
130 Status = PeiLoadImage (\r
131 &PrivateData->PS,\r
132 DispatchData->CurrentPeimAddress,\r
133 &TempPtr.Raw\r
134 );\r
135 if (Status == EFI_SUCCESS) {\r
136\r
137 //\r
138 // The PEIM has its dependencies satisfied, and its entry point\r
139 // has been found, so invoke it.\r
140 //\r
141 PERF_START (\r
142 (VOID *) (UINTN) (DispatchData->CurrentPeimAddress),\r
143 "PEIM",\r
144 NULL,\r
145 0\r
146 );\r
147\r
148 //\r
149 // BUGBUG: Used to be EFI_PEI_REPORT_STATUS_CODE_CODE\r
150 //\r
151 ExtendedData.Handle = (EFI_HANDLE)DispatchData->CurrentPeimAddress;\r
152\r
153 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
154 EFI_PROGRESS_CODE,\r
155 EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN,\r
156 (VOID *)(&ExtendedData),\r
157 sizeof (ExtendedData)\r
158 );\r
159\r
160 //\r
161 // Is this a authentic image\r
162 //\r
163 Status = VerifyPeim (\r
164 &PrivateData->PS,\r
165 DispatchData->CurrentPeimAddress\r
166 );\r
167\r
168 if (Status != EFI_SECURITY_VIOLATION) {\r
169\r
170 //\r
171 // BUGBUG: Before enable PI, we need cast EFI_FFS_FILE_HEADER* to EFI_PEI_FILE_HANDLE*\r
172 // Because we use new MdePkg's definition, but they are binary compatible in fact.\r
173 //\r
174 Status = TempPtr.PeimEntry (\r
175 (EFI_PEI_FILE_HANDLE*)DispatchData->CurrentPeimAddress,\r
176 &PrivateData->PS\r
177 );\r
178 }\r
179\r
180 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
181 EFI_PROGRESS_CODE,\r
182 EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END,\r
183 (VOID *)(&ExtendedData),\r
184 sizeof (ExtendedData)\r
185 );\r
186\r
187 PERF_END ((VOID *) (UINTN) (DispatchData->CurrentPeimAddress), "PEIM", NULL, 0);\r
188\r
189 //\r
190 // Mark the PEIM as dispatched so we don't attempt to run it again\r
191 //\r
192 SetDispatched (\r
193 &PrivateData->PS,\r
194 DispatchData->CurrentPeim,\r
195 &DispatchData->DispatchedPeimBitMap\r
196 );\r
197\r
198 //\r
199 // Process the Notify list and dispatch any notifies for\r
200 // newly installed PPIs.\r
201 //\r
202 ProcessNotifyList (&PrivateData->PS);\r
203\r
204 //\r
205 // If real system memory was discovered and installed by this\r
206 // PEIM, switch the stacks to the new memory. Since we are\r
207 // at dispatch level, only the Core's private data is preserved,\r
208 // nobody else should have any data on the stack.\r
209 //\r
210 if (PrivateData->SwitchStackSignal) {\r
211 TempPtr.PeiCore = (PEI_CORE_ENTRY_POINT)PeiCore;\r
212 PrivateDataInMem = (UINTN) TransferOldDataToNewDataRange (PrivateData);\r
213 ASSERT (PrivateDataInMem != 0);\r
214 //\r
215 // Adjust the top of stack to be aligned at CPU_STACK_ALIGNMENT\r
216 //\r
217 TopOfStack = (VOID *)((UINTN)PrivateData->StackBase + (UINTN)PrivateData->StackSize - CPU_STACK_ALIGNMENT);\r
218 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
219\r
220 PeiSwitchStacks (\r
221 (SWITCH_STACK_ENTRY_POINT)(UINTN)TempPtr.Raw,\r
222 PeiStartupDescriptor,\r
223 (VOID*)PrivateDataInMem,\r
224 TopOfStack,\r
225 (VOID*)(UINTN)PrivateData->StackBase\r
226 );\r
227 }\r
228 }\r
229 }\r
230 }\r
231 DispatchData->CurrentPeim++;\r
232 continue;\r
233\r
234 } else {\r
235\r
236 //\r
237 // If we could not find another PEIM in the current FV, go try\r
238 // the FindFv PPI to look in other FVs for more PEIMs. If we can\r
239 // not locate the FindFv PPI, or if the FindFv PPI can not find\r
240 // anymore FVs, then exit the PEIM search loop.\r
241 //\r
242 if (DispatchData->FindFv == NULL) {\r
243 Status = PeiServicesLocatePpi (\r
244 &gEfiFindFvPpiGuid,\r
245 0,\r
246 NULL,\r
247 (VOID **)&DispatchData->FindFv\r
248 );\r
249 if (Status != EFI_SUCCESS) {\r
250 break;\r
251 }\r
252 }\r
253 NextFvFound = FALSE;\r
254 while (!NextFvFound) {\r
255 Status = DispatchData->FindFv->FindFv (\r
256 DispatchData->FindFv,\r
257 &PrivateData->PS,\r
258 &DispatchData->CurrentFv,\r
259 &NextFvAddress\r
260 );\r
261 //\r
262 // if there is no next fv, get out of this loop of finding FVs\r
263 //\r
264 if (Status != EFI_SUCCESS) {\r
265 break;\r
266 }\r
267 //\r
268 // don't process the default Fv again. (we don't know the order in which the hobs were created)\r
269 //\r
270 if ((NextFvAddress != DefaultFvAddress) &&\r
271 (NextFvAddress != DispatchData->CurrentFvAddress)) {\r
272\r
273 //\r
274 // VerifyFv() is currently returns SUCCESS all the time, add code to it to\r
275 // actually verify the given FV\r
276 //\r
277 Status = VerifyFv (NextFvAddress);\r
278 if (Status == EFI_SUCCESS) {\r
279 NextFvFound = TRUE;\r
280 DispatchData->CurrentFvAddress = NextFvAddress;\r
281 DispatchData->CurrentPeimAddress = NULL;\r
282 //\r
283 // current PRIM number (CurrentPeim) must continue as is, don't reset it here\r
284 //\r
285 }\r
286 }\r
287 }\r
288 //\r
289 // if there is no next fv, get out of this loop of dispatching PEIMs\r
290 //\r
291 if (!NextFvFound) {\r
292 break;\r
293 }\r
294 //\r
295 // continue in the inner for(;;) loop with a new FV;\r
296 //\r
297 }\r
298 }\r
299\r
300 //\r
301 // If all the PEIMs that we have found have been dispatched, then\r
302 // there is nothing left to dispatch and we don't need to go search\r
303 // through all PEIMs again.\r
304 //\r
305 if ((~(DispatchData->DispatchedPeimBitMap) &\r
306 ((1 << DispatchData->CurrentPeim)-1)) == 0) {\r
307 break;\r
308 }\r
309\r
310 //\r
311 // Check if no more PEIMs that depex was satisfied\r
312 //\r
313 if (DispatchData->DispatchedPeimBitMap == DispatchData->PreviousPeimBitMap) {\r
314 break;\r
315 }\r
316\r
317 //\r
318 // Case when Depex is not satisfied and has to traverse the list again\r
319 //\r
320 DispatchData->CurrentPeim = 0;\r
321 DispatchData->CurrentPeimAddress = 0;\r
322 DispatchData->PreviousPeimBitMap = DispatchData->DispatchedPeimBitMap;\r
323\r
324 //\r
325 // don't go back to the loop without making sure that the CurrentFvAddress is the\r
326 // same as the 1st (or default) FV we started with. otherwise we will interpret the bimap wrongly and\r
327 // mess it up, always start processing the PEIMs from the default FV just like in the first time around.\r
328 //\r
329 DispatchData->CurrentFv = 0;\r
330 DispatchData->CurrentFvAddress = DefaultFvAddress;\r
331 }\r
332\r
333 DEBUG_CODE_BEGIN ();\r
334 //\r
335 // Debug data for uninstalled Peim list\r
336 //\r
337 UINT32 DebugNotDispatchedBitmap;\r
338 UINT8 DebugFoundPeimPoint;\r
339\r
340 DebugFoundPeimPoint = 0;\r
341 //\r
342 // Get bitmap of Peims that were not dispatched,\r
343 //\r
344\r
345 DebugNotDispatchedBitmap = ((DispatchData->DispatchedPeimBitMap) ^ ((1 << DispatchData->CurrentPeim)-1));\r
346 //\r
347 // Scan bitmap of Peims not installed and print GUIDS\r
348 //\r
349 while (DebugNotDispatchedBitmap != 0) {\r
350 if ((DebugNotDispatchedBitmap & 1) != 0) {\r
351 DEBUG ((EFI_D_INFO, "WARNING -> InstallPpi: Not Installed: %g\n",\r
352 &DebugFoundPeimList[DebugFoundPeimPoint]\r
353 ));\r
354 }\r
355 DebugFoundPeimPoint++;\r
356 DebugNotDispatchedBitmap >>= 1;\r
357 }\r
358\r
359 DEBUG_CODE_END ();\r
360\r
361 return EFI_NOT_FOUND;\r
362}\r
363\r
364VOID\r
365InitializeDispatcherData (\r
366 IN EFI_PEI_SERVICES **PeiServices,\r
367 IN PEI_CORE_INSTANCE *OldCoreData,\r
368 IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor\r
369 )\r
370/*++\r
371\r
372Routine Description:\r
373\r
374 Initialize the Dispatcher's data members\r
375\r
376Arguments:\r
377\r
378 PeiServices - The PEI core services table.\r
379 OldCoreData - Pointer to old core data (before switching stack).\r
380 NULL if being run in non-permament memory mode.\r
381 PeiStartupDescriptor - Information and services provided by SEC phase.\r
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
394 PrivateData->DispatchData.CurrentFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) PeiStartupDescriptor->BootFirmwareVolume;\r
395 PrivateData->DispatchData.BootFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) PeiStartupDescriptor->BootFirmwareVolume;\r
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