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