]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Update input of disasmembler to support IfThen construct. Add prototype dos script...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
... / ...
CommitLineData
1/** @file\r
2 EFI PEI Core dispatch services\r
3 \r
4Copyright (c) 2006 - 2010, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PeiMain.h"\r
16\r
17///\r
18/// temporary memory is filled with this initial value during SEC phase\r
19///\r
20#define INIT_CAR_VALUE 0x5AA55AA5\r
21\r
22typedef struct {\r
23 EFI_STATUS_CODE_DATA DataHeader;\r
24 EFI_HANDLE Handle;\r
25} PEIM_FILE_HANDLE_EXTENDED_DATA;\r
26\r
27/**\r
28\r
29 Discover all Peims and optional Apriori file in one FV. There is at most one\r
30 Apriori file in one FV.\r
31\r
32\r
33 @param Private Pointer to the private data passed in from caller\r
34 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.\r
35\r
36**/\r
37VOID\r
38DiscoverPeimsAndOrderWithApriori (\r
39 IN PEI_CORE_INSTANCE *Private,\r
40 IN PEI_CORE_FV_HANDLE *CoreFileHandle\r
41 )\r
42{\r
43 EFI_STATUS Status;\r
44 EFI_PEI_FV_HANDLE FileHandle;\r
45 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
46 EFI_GUID *Apriori;\r
47 UINTN Index;\r
48 UINTN Index2;\r
49 UINTN PeimIndex;\r
50 UINTN PeimCount;\r
51 EFI_GUID *Guid;\r
52 EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
53 EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
54 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
55 EFI_FV_FILE_INFO FileInfo;\r
56 \r
57 FvPpi = CoreFileHandle->FvPpi;\r
58 \r
59 //\r
60 // Walk the FV and find all the PEIMs and the Apriori file.\r
61 //\r
62 AprioriFileHandle = NULL;\r
63 Private->CurrentFvFileHandles[0] = NULL;\r
64 Guid = NULL;\r
65 FileHandle = NULL;\r
66\r
67 //\r
68 // If the current Fv has been scanned, directly get its cachable record.\r
69 //\r
70 if (Private->Fv[Private->CurrentPeimFvCount].ScanFv) {\r
71 CopyMem (Private->CurrentFvFileHandles, Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
72 return;\r
73 }\r
74\r
75 //\r
76 // Go ahead to scan this Fv, and cache FileHandles within it.\r
77 //\r
78 for (PeimCount = 0; PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {\r
79 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
80 if (Status != EFI_SUCCESS) {\r
81 break;\r
82 }\r
83\r
84 Private->CurrentFvFileHandles[PeimCount] = FileHandle;\r
85 }\r
86 \r
87 //\r
88 // Check whether the count of Peims exceeds the max support PEIMs in a FV image\r
89 // If more Peims are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.\r
90 //\r
91 ASSERT (PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
92\r
93 //\r
94 // Get Apriori File handle\r
95 //\r
96 Private->AprioriCount = 0;\r
97 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);\r
98 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {\r
99 //\r
100 // Read the Apriori file\r
101 //\r
102 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
103 if (!EFI_ERROR (Status)) {\r
104 //\r
105 // Calculate the number of PEIMs in the A Priori list\r
106 //\r
107 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
108 ASSERT_EFI_ERROR (Status);\r
109 Private->AprioriCount = FileInfo.BufferSize & 0x00FFFFFF;\r
110 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);\r
111 Private->AprioriCount /= sizeof (EFI_GUID);\r
112\r
113 ZeroMem (FileGuid, sizeof (FileGuid));\r
114 for (Index = 0; Index < PeimCount; Index++) {\r
115 //\r
116 // Make an array of file name guids that matches the FileHandle array so we can convert\r
117 // quickly from file name to file handle\r
118 //\r
119 Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo);\r
120 CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
121 }\r
122\r
123 //\r
124 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.\r
125 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.\r
126 //\r
127 Index2 = 0;\r
128 for (Index = 0; Index2 < Private->AprioriCount; Index++) {\r
129 while (Index2 < Private->AprioriCount) {\r
130 Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);\r
131 if (Guid != NULL) {\r
132 break;\r
133 }\r
134 }\r
135 if (Guid == NULL) {\r
136 break;\r
137 }\r
138 PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);\r
139 TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];\r
140\r
141 //\r
142 // Since we have copied the file handle we can remove it from this list.\r
143 //\r
144 Private->CurrentFvFileHandles[PeimIndex] = NULL;\r
145 }\r
146\r
147 //\r
148 // Update valid Aprioricount\r
149 //\r
150 Private->AprioriCount = Index;\r
151\r
152 //\r
153 // Add in any PEIMs not in the Apriori file\r
154 //\r
155 for (;Index < PeimCount; Index++) {\r
156 for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
157 if (Private->CurrentFvFileHandles[Index2] != NULL) {\r
158 TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];\r
159 Private->CurrentFvFileHandles[Index2] = NULL;\r
160 break;\r
161 }\r
162 }\r
163 }\r
164 //\r
165 //Index the end of array contains re-range Pei moudle.\r
166 //\r
167 TempFileHandles[Index] = NULL;\r
168\r
169 //\r
170 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.\r
171 // We need to update it to start with files in the A Priori list and\r
172 // then the remaining files in PEIM order.\r
173 //\r
174 CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles));\r
175 }\r
176 }\r
177 //\r
178 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.\r
179 // Instead, we can retrieve the file handles within this Fv from cachable data.\r
180 //\r
181 Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;\r
182 CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
183\r
184}\r
185\r
186/**\r
187 Shadow PeiCore module from flash to installed memory.\r
188 \r
189 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
190 @param PrivateInMem PeiCore's private data structure\r
191\r
192 @return PeiCore function address after shadowing.\r
193**/\r
194VOID*\r
195ShadowPeiCore(\r
196 IN CONST EFI_PEI_SERVICES **PeiServices,\r
197 IN PEI_CORE_INSTANCE *PrivateInMem\r
198 )\r
199{\r
200 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;\r
201 EFI_PHYSICAL_ADDRESS EntryPoint;\r
202 EFI_STATUS Status;\r
203 UINT32 AuthenticationState;\r
204\r
205 PeiCoreFileHandle = NULL;\r
206\r
207 //\r
208 // Find the PEI Core in the BFV\r
209 //\r
210 Status = PrivateInMem->Fv[0].FvPpi->FindFileByType (\r
211 PrivateInMem->Fv[0].FvPpi,\r
212 EFI_FV_FILETYPE_PEI_CORE,\r
213 PrivateInMem->Fv[0].FvHandle,\r
214 &PeiCoreFileHandle\r
215 );\r
216 ASSERT_EFI_ERROR (Status);\r
217\r
218 //\r
219 // Shadow PEI Core into memory so it will run faster\r
220 //\r
221 Status = PeiLoadImage (\r
222 PeiServices,\r
223 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),\r
224 PEIM_STATE_REGISITER_FOR_SHADOW,\r
225 &EntryPoint,\r
226 &AuthenticationState\r
227 );\r
228 ASSERT_EFI_ERROR (Status);\r
229\r
230 //\r
231 // Compute the PeiCore's function address after shaowed PeiCore.\r
232 // _ModuleEntryPoint is PeiCore main function entry\r
233 //\r
234 return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
235}\r
236\r
237/**\r
238 Conduct PEIM dispatch.\r
239\r
240 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
241 environment, such as the size and location of temporary RAM, the stack location and\r
242 the BFV location.\r
243 @param Private Pointer to the private data passed in from caller\r
244\r
245**/\r
246VOID\r
247PeiDispatcher (\r
248 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
249 IN PEI_CORE_INSTANCE *Private\r
250 )\r
251{\r
252 EFI_STATUS Status;\r
253 UINT32 Index1;\r
254 UINT32 Index2;\r
255 CONST EFI_PEI_SERVICES **PeiServices;\r
256 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
257 UINTN FvCount;\r
258 UINTN PeimCount;\r
259 UINT32 AuthenticationState;\r
260 EFI_PHYSICAL_ADDRESS EntryPoint;\r
261 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
262 UINTN SaveCurrentPeimCount;\r
263 UINTN SaveCurrentFvCount;\r
264 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
265 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;\r
266 EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase;\r
267 TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
268 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable;\r
269 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable;\r
270 INTN StackOffset;\r
271 INTN HeapOffset;\r
272 PEI_CORE_INSTANCE *PrivateInMem;\r
273 UINT64 NewPeiStackSize;\r
274 UINT64 OldPeiStackSize;\r
275 UINT64 StackGap;\r
276 EFI_FV_FILE_INFO FvFileInfo;\r
277 UINTN OldCheckingTop;\r
278 UINTN OldCheckingBottom;\r
279 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
280\r
281 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->PS;\r
282 PeimEntryPoint = NULL;\r
283 PeimFileHandle = NULL;\r
284 EntryPoint = 0;\r
285\r
286 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
287 //\r
288 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
289 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
290 //\r
291 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
292 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
293 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
294\r
295 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
296 for (Index2 = 0; (Index2 < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
297 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
298 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
299 Status = PeiLoadImage (\r
300 (CONST EFI_PEI_SERVICES **) &Private->PS,\r
301 PeimFileHandle,\r
302 PEIM_STATE_REGISITER_FOR_SHADOW,\r
303 &EntryPoint,\r
304 &AuthenticationState\r
305 );\r
306 if (Status == EFI_SUCCESS) {\r
307 //\r
308 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
309 //\r
310 Private->Fv[Index1].PeimState[Index2]++;\r
311 Private->CurrentFileHandle = PeimFileHandle;\r
312 Private->CurrentPeimFvCount = Index1;\r
313 Private->CurrentPeimCount = Index2;\r
314 //\r
315 // Call the PEIM entry point\r
316 //\r
317 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
318\r
319 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
320 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->PS);\r
321 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
322 }\r
323\r
324 //\r
325 // Process the Notify list and dispatch any notifies for\r
326 // newly installed PPIs.\r
327 //\r
328 ProcessNotifyList (Private);\r
329 }\r
330 }\r
331 }\r
332 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
333 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
334 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
335 }\r
336\r
337 //\r
338 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
339 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
340 // pass of the dispatcher, it will start over from the Bfv again to see\r
341 // if any new PEIMs dependencies got satisfied. With a well ordered\r
342 // FV where PEIMs are found in the order their dependencies are also\r
343 // satisfied, this dipatcher should run only once.\r
344 //\r
345 do {\r
346 //\r
347 // In case that reenter PeiCore happens, the last pass record is still available. \r
348 //\r
349 if (!Private->PeimDispatcherReenter) {\r
350 Private->PeimNeedingDispatch = FALSE;\r
351 Private->PeimDispatchOnThisPass = FALSE;\r
352 } else {\r
353 Private->PeimDispatcherReenter = FALSE;\r
354 }\r
355 \r
356 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
357 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
358 ASSERT (CoreFvHandle != NULL);\r
359 \r
360 //\r
361 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
362 //\r
363 if (CoreFvHandle->FvPpi == NULL) {\r
364 continue;\r
365 }\r
366 \r
367 Private->CurrentPeimFvCount = FvCount;\r
368\r
369 if (Private->CurrentPeimCount == 0) {\r
370 //\r
371 // When going through each FV, at first, search Apriori file to\r
372 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
373 // dispatch at first.\r
374 //\r
375 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);\r
376 }\r
377\r
378 //\r
379 // Start to dispatch all modules within the current Fv.\r
380 //\r
381 for (PeimCount = Private->CurrentPeimCount;\r
382 (PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
383 PeimCount++) {\r
384 Private->CurrentPeimCount = PeimCount;\r
385 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
386\r
387 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
388 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
389 Private->PeimNeedingDispatch = TRUE;\r
390 } else {\r
391 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);\r
392 ASSERT_EFI_ERROR (Status);\r
393 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
394 //\r
395 // For Fv type file, Produce new FV PPI and FV hob\r
396 //\r
397 Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle);\r
398 AuthenticationState = 0;\r
399 } else {\r
400 //\r
401 // For PEIM driver, Load its entry point\r
402 //\r
403 Status = PeiLoadImage (\r
404 PeiServices,\r
405 PeimFileHandle,\r
406 PEIM_STATE_NOT_DISPATCHED,\r
407 &EntryPoint,\r
408 &AuthenticationState\r
409 );\r
410 }\r
411\r
412 if ((Status == EFI_SUCCESS)) {\r
413 //\r
414 // The PEIM has its dependencies satisfied, and its entry point\r
415 // has been found, so invoke it.\r
416 //\r
417 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
418\r
419 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
420\r
421 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
422 EFI_PROGRESS_CODE,\r
423 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
424 (VOID *)(&ExtendedData),\r
425 sizeof (ExtendedData)\r
426 );\r
427\r
428 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle);\r
429 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {\r
430 //\r
431 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
432 //\r
433 Private->Fv[FvCount].PeimState[PeimCount]++;\r
434\r
435 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
436 //\r
437 // Call the PEIM entry point for PEIM driver\r
438 //\r
439 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
440 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
441 }\r
442\r
443 Private->PeimDispatchOnThisPass = TRUE;\r
444 }\r
445\r
446 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
447 EFI_PROGRESS_CODE,\r
448 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
449 (VOID *)(&ExtendedData),\r
450 sizeof (ExtendedData)\r
451 );\r
452 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
453\r
454 }\r
455\r
456 if (Private->SwitchStackSignal) {\r
457 //\r
458 // Before switch stack from temporary memory to permenent memory, caculate the heap and stack\r
459 // usage in temporary memory for debuging.\r
460 //\r
461 DEBUG_CODE_BEGIN ();\r
462 UINT32 *StackPointer;\r
463 \r
464 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
465 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
466 && (*StackPointer == INIT_CAR_VALUE);\r
467 StackPointer ++);\r
468 \r
469 DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
470 DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n",\r
471 (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
472 ));\r
473 DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n",\r
474 ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -\r
475 (UINTN) Private->HobList.Raw)\r
476 ));\r
477 DEBUG_CODE_END ();\r
478 \r
479 //\r
480 // Reserve the size of new stack at bottom of physical memory\r
481 //\r
482 OldPeiStackSize = (UINT64) SecCoreData->StackSize;\r
483 NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
484 if (PcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {\r
485 Private->StackSize = NewPeiStackSize;\r
486 } else {\r
487 Private->StackSize = PcdGet32(PcdPeiCoreMaxPeiStackSize);\r
488 }\r
489\r
490 //\r
491 // In theory, the size of new stack in permenent memory should large than\r
492 // size of old stack in temporary memory.\r
493 // But if new stack is smaller than the size of old stack, we also reserve\r
494 // the size of old stack at bottom of permenent memory.\r
495 //\r
496 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize));\r
497 ASSERT (Private->StackSize >= OldPeiStackSize);\r
498 StackGap = Private->StackSize - OldPeiStackSize;\r
499\r
500 //\r
501 // Update HandOffHob for new installed permenent memory\r
502 //\r
503 OldHandOffTable = Private->HobList.HandoffInformationTable;\r
504 OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);\r
505 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
506\r
507 //\r
508 // The whole temporary memory will be migrated to physical memory.\r
509 // CAUTION: The new base is computed accounding to gap of new stack.\r
510 //\r
511 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
512 \r
513 //\r
514 // Caculate stack offset and heap offset between temporary memory and new permement \r
515 // memory seperately.\r
516 //\r
517 StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;\r
518 HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \\r
519 (UINTN) SecCoreData->PeiTemporaryRamBase);\r
520 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset));\r
521 \r
522 //\r
523 // Caculate new HandOffTable and PrivateData address in permenet memory's stack\r
524 //\r
525 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);\r
526 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);\r
527\r
528 //\r
529 // TemporaryRamSupportPpi is produced by platform's SEC\r
530 //\r
531 Status = PeiLocatePpi (\r
532 (CONST EFI_PEI_SERVICES **) PeiServices,\r
533 &gEfiTemporaryRamSupportPpiGuid,\r
534 0,\r
535 NULL,\r
536 (VOID**)&TemporaryRamSupportPpi\r
537 );\r
538\r
539\r
540 if (!EFI_ERROR (Status)) {\r
541 //\r
542 // Temporary Ram support Ppi is provided by platform, it will copy \r
543 // temporary memory to permenent memory and do stack switching.\r
544 // After invoken temporary Ram support, following code's stack is in \r
545 // memory but not in temporary memory.\r
546 //\r
547 TemporaryRamSupportPpi->TemporaryRamMigration (\r
548 (CONST EFI_PEI_SERVICES **) PeiServices,\r
549 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
550 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
551 SecCoreData->TemporaryRamSize\r
552 );\r
553\r
554 } else {\r
555 //\r
556 // In IA32/x64/Itanium architecture, we need platform provide\r
557 // TEMPORAY_RAM_MIGRATION_PPI.\r
558 //\r
559 ASSERT (FALSE);\r
560 }\r
561\r
562\r
563 //\r
564 //\r
565 // Fixup the PeiCore's private data\r
566 //\r
567 PrivateInMem->PS = &PrivateInMem->ServiceTableShadow;\r
568 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;\r
569 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);\r
570 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
571\r
572 PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->PS;\r
573\r
574 //\r
575 // Fixup for PeiService's address\r
576 //\r
577 SetPeiServicesTablePointer(PeiServices);\r
578\r
579 //\r
580 // Update HandOffHob for new installed permenent memory\r
581 //\r
582 NewHandOffTable->EfiEndOfHobList =\r
583 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);\r
584 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +\r
585 PrivateInMem->PhysicalMemoryLength;\r
586 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;\r
587 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;\r
588 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
589 sizeof (EFI_HOB_GENERIC_HEADER);\r
590\r
591 //\r
592 // We need convert the PPI desciptor's pointer\r
593 //\r
594 ConvertPpiPointers (PrivateInMem, \r
595 OldCheckingBottom, \r
596 OldCheckingTop, \r
597 HeapOffset\r
598 );\r
599\r
600 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n",\r
601 PrivateInMem->StackBase,\r
602 PrivateInMem->StackSize));\r
603 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
604\r
605 //\r
606 // After the whole temporary memory is migrated, then we can allocate page in\r
607 // permenent memory.\r
608 //\r
609 PrivateInMem->PeiMemoryInstalled = TRUE;\r
610\r
611 //\r
612 // Indicate that PeiCore reenter\r
613 //\r
614 PrivateInMem->PeimDispatcherReenter = TRUE;\r
615 \r
616 //\r
617 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
618 // PEI Core and PEIMs to get high performance.\r
619 //\r
620 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
621 PeiServices,\r
622 PrivateInMem\r
623 );\r
624 //\r
625 // Process the Notify list and dispatch any notifies for\r
626 // newly installed PPIs.\r
627 //\r
628 ProcessNotifyList (PrivateInMem);\r
629\r
630 //\r
631 // Entry PEI Phase 2\r
632 //\r
633 PeiCore (SecCoreData, NULL, PrivateInMem);\r
634\r
635 //\r
636 // Code should not come here\r
637 //\r
638 ASSERT_EFI_ERROR(FALSE);\r
639 }\r
640\r
641 //\r
642 // Process the Notify list and dispatch any notifies for\r
643 // newly installed PPIs.\r
644 //\r
645 ProcessNotifyList (Private);\r
646\r
647 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
648 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
649 //\r
650 // If memory is availble we shadow images by default for performance reasons.\r
651 // We call the entry point a 2nd time so the module knows it's shadowed.\r
652 //\r
653 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
654 ASSERT (PeimEntryPoint != NULL);\r
655 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
656 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
657\r
658 //\r
659 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
660 //\r
661 Private->Fv[FvCount].PeimState[PeimCount]++;\r
662\r
663 //\r
664 // Process the Notify list and dispatch any notifies for\r
665 // newly installed PPIs.\r
666 //\r
667 ProcessNotifyList (Private);\r
668 }\r
669 }\r
670 }\r
671 }\r
672\r
673 //\r
674 // We set to NULL here to optimize the 2nd entry to this routine after\r
675 // memory is found. This reprevents rescanning of the FV. We set to\r
676 // NULL here so we start at the begining of the next FV\r
677 //\r
678 Private->CurrentFileHandle = NULL;\r
679 Private->CurrentPeimCount = 0;\r
680 //\r
681 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
682 //\r
683 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
684 }\r
685\r
686 //\r
687 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
688 // through all the FV.\r
689 //\r
690 Private->CurrentPeimFvCount = 0;\r
691\r
692 //\r
693 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
694 // dispatched. So we need to make another pass\r
695 //\r
696 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
697 // pass. If we did not dispatch a PEIM there is no point in trying again\r
698 // as it will fail the next time too (nothing has changed).\r
699 //\r
700 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
701\r
702}\r
703\r
704/**\r
705 Initialize the Dispatcher's data members\r
706\r
707 @param PrivateData PeiCore's private data structure\r
708 @param OldCoreData Old data from SecCore\r
709 NULL if being run in non-permament memory mode.\r
710 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
711 environment, such as the size and location of temporary RAM, the stack location and\r
712 the BFV location.\r
713\r
714 @return None.\r
715\r
716**/\r
717VOID\r
718InitializeDispatcherData (\r
719 IN PEI_CORE_INSTANCE *PrivateData,\r
720 IN PEI_CORE_INSTANCE *OldCoreData,\r
721 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
722 )\r
723{\r
724 if (OldCoreData == NULL) {\r
725 PrivateData->PeimDispatcherReenter = FALSE;\r
726 PeiInitializeFv (PrivateData, SecCoreData);\r
727 } else {\r
728 PeiReinitializeFv (PrivateData);\r
729 }\r
730\r
731 return;\r
732}\r
733\r
734/**\r
735 This routine parses the Dependency Expression, if available, and\r
736 decides if the module can be executed.\r
737\r
738\r
739 @param Private PeiCore's private data structure\r
740 @param FileHandle PEIM's file handle\r
741 @param PeimCount Peim count in all dispatched PEIMs.\r
742\r
743 @retval TRUE Can be dispatched\r
744 @retval FALSE Cannot be dispatched\r
745\r
746**/\r
747BOOLEAN\r
748DepexSatisfied (\r
749 IN PEI_CORE_INSTANCE *Private,\r
750 IN EFI_PEI_FILE_HANDLE FileHandle,\r
751 IN UINTN PeimCount\r
752 )\r
753{\r
754 EFI_STATUS Status;\r
755 VOID *DepexData;\r
756\r
757 if (PeimCount < Private->AprioriCount) {\r
758 //\r
759 // If its in the A priori file then we set Depex to TRUE\r
760 //\r
761 return TRUE;\r
762 }\r
763\r
764 //\r
765 // Depex section not in the encapsulated section.\r
766 //\r
767 Status = PeiServicesFfsFindSectionData (\r
768 EFI_SECTION_PEI_DEPEX,\r
769 FileHandle,\r
770 (VOID **)&DepexData\r
771 );\r
772\r
773 if (EFI_ERROR (Status)) {\r
774 //\r
775 // If there is no DEPEX, assume the module can be executed\r
776 //\r
777 return TRUE;\r
778 }\r
779\r
780 //\r
781 // Evaluate a given DEPEX\r
782 //\r
783 return PeimDispatchReadiness (&Private->PS, DepexData);\r
784}\r
785\r
786/**\r
787 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
788 discovery permanent memory.\r
789\r
790 @param FileHandle File handle of a PEIM.\r
791\r
792 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
793 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
794 @retval EFI_SUCCESS Successfully to register itself.\r
795\r
796**/\r
797EFI_STATUS\r
798EFIAPI\r
799PeiRegisterForShadow (\r
800 IN EFI_PEI_FILE_HANDLE FileHandle\r
801 )\r
802{\r
803 PEI_CORE_INSTANCE *Private;\r
804 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
805\r
806 if (Private->CurrentFileHandle != FileHandle) {\r
807 //\r
808 // The FileHandle must be for the current PEIM\r
809 //\r
810 return EFI_NOT_FOUND;\r
811 }\r
812\r
813 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
814 //\r
815 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
816 //\r
817 return EFI_ALREADY_STARTED;\r
818 }\r
819\r
820 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
821\r
822 return EFI_SUCCESS;\r
823}\r
824\r
825\r
826\r