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