]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
revise the comments style
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core dispatch services\r
3 \r
f4391d63 4Copyright (c) 2006 - 2010, Intel Corporation\r
192f6d4c 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
615c6dd0 13**/\r
192f6d4c 14\r
0d516397 15#include "PeiMain.h"\r
192f6d4c 16\r
b1f6a7c6 17///\r
3d4d0c34 18/// temporary memory is filled with this initial value during SEC phase\r
b1f6a7c6 19///\r
a7715e73 20#define INIT_CAR_VALUE 0x5AA55AA5\r
21\r
797a9d67 22typedef struct {\r
23 EFI_STATUS_CODE_DATA DataHeader;\r
24 EFI_HANDLE Handle;\r
25} PEIM_FILE_HANDLE_EXTENDED_DATA;\r
26\r
b1f6a7c6 27/**\r
b0d803fe 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
b0d803fe 32\r
3b428ade 33 @param Private Pointer to the private data passed in from caller\r
34 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.\r
b0d803fe 35\r
b1f6a7c6 36**/\r
37VOID\r
38DiscoverPeimsAndOrderWithApriori (\r
39 IN PEI_CORE_INSTANCE *Private,\r
3b428ade 40 IN PEI_CORE_FV_HANDLE *CoreFileHandle\r
b1f6a7c6 41 )\r
b0d803fe 42{\r
43 EFI_STATUS Status;\r
44 EFI_PEI_FV_HANDLE FileHandle;\r
177aabe6 45 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
b0d803fe 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
177aabe6 52 EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
53 EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
3b428ade 54 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
55 EFI_FV_FILE_INFO FileInfo;\r
56 \r
57 FvPpi = CoreFileHandle->FvPpi;\r
58 \r
b0d803fe 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
188e4e84 78 for (PeimCount = 0; PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {\r
3b428ade 79 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
b0d803fe 80 if (Status != EFI_SUCCESS) {\r
81 break;\r
82 }\r
58dcdada 83\r
b0d803fe 84 Private->CurrentFvFileHandles[PeimCount] = FileHandle;\r
85 }\r
97b2c9b5
LG
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
188e4e84 91 ASSERT (PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
b0d803fe 92\r
3b428ade 93 //\r
94 // Get Apriori File handle\r
95 //\r
58dcdada 96 Private->AprioriCount = 0;\r
3b428ade 97 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);\r
98 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {\r
b0d803fe 99 //\r
100 // Read the Apriori file\r
101 //\r
3b428ade 102 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
b0d803fe 103 if (!EFI_ERROR (Status)) {\r
104 //\r
105 // Calculate the number of PEIMs in the A Priori list\r
106 //\r
3b428ade 107 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
108 ASSERT_EFI_ERROR (Status);\r
109 Private->AprioriCount = FileInfo.BufferSize & 0x00FFFFFF;\r
595d4b4c 110 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);\r
b0d803fe 111 Private->AprioriCount /= sizeof (EFI_GUID);\r
58dcdada 112\r
82b8c8df 113 ZeroMem (FileGuid, sizeof (FileGuid));\r
b0d803fe 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
3b428ade 119 Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo);\r
120 CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
b0d803fe 121 }\r
122\r
123 //\r
124 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.\r
58dcdada 125 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.\r
b0d803fe 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
58dcdada 136 break;\r
b0d803fe 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
58dcdada 151\r
b0d803fe 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
58dcdada 168\r
b0d803fe 169 //\r
170 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.\r
58dcdada 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
b0d803fe 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
58dcdada 183\r
184}\r
185\r
b1f6a7c6 186/**\r
187 Shadow PeiCore module from flash to installed memory.\r
188 \r
d73d93c3 189 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
b1f6a7c6 190 @param PrivateInMem PeiCore's private data structure\r
191\r
82b8c8df 192 @return PeiCore function address after shadowing.\r
b1f6a7c6 193**/\r
58dcdada 194VOID*\r
195ShadowPeiCore(\r
6c7a807a 196 IN CONST EFI_PEI_SERVICES **PeiServices,\r
197 IN PEI_CORE_INSTANCE *PrivateInMem\r
58dcdada 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
3b428ade 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
58dcdada 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
341a658f 224 PEIM_STATE_REGISITER_FOR_SHADOW,\r
58dcdada 225 &EntryPoint,\r
226 &AuthenticationState\r
227 );\r
228 ASSERT_EFI_ERROR (Status);\r
229\r
82b8c8df 230 //\r
231 // Compute the PeiCore's function address after shaowed PeiCore.\r
232 // _ModuleEntryPoint is PeiCore main function entry\r
233 //\r
58dcdada 234 return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
b0d803fe 235}\r
54ea99a7 236//\r
237// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
238// This part of memory still need reserved on the very top of memory so that the DXE Core could \r
239// use these memory for data initialization. This macro should be sync with the same marco\r
240// defined in DXE Core.\r
241//\r
242#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
243/**\r
244 Hook function for Loading Module at Fixed Address feature\r
245 \r
246 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is\r
247 configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When \r
248 feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general. \r
249 And also the function will re-install PEI memory. \r
b0d803fe 250\r
54ea99a7 251 @param PrivateData Pointer to the private data passed in from caller\r
252\r
253**/\r
254VOID\r
255PeiLoadFixAddressHook(\r
256 IN PEI_CORE_INSTANCE *PrivateData\r
257 )\r
258{\r
259 EFI_PHYSICAL_ADDRESS TopLoadingAddress;\r
260 UINT64 PeiMemorySize;\r
261 UINT64 TotalReservedMemorySize;\r
262 UINT64 MemoryRangeEnd;\r
263 EFI_PHYSICAL_ADDRESS HighAddress; \r
264 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
265 EFI_HOB_RESOURCE_DESCRIPTOR *NextResourceHob;\r
266 EFI_HOB_RESOURCE_DESCRIPTOR *CurrentResourceHob;\r
267 EFI_PEI_HOB_POINTERS CurrentHob;\r
268 EFI_PEI_HOB_POINTERS Hob;\r
269 EFI_PEI_HOB_POINTERS NextHob;\r
270 EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress;\r
271 UINT64 MaxMemoryLength;\r
272 //\r
273 // Initialize Local Variables\r
274 //\r
275 CurrentResourceHob = NULL;\r
276 ResourceHob = NULL;\r
277 NextResourceHob = NULL;\r
278 MaxMemoryBaseAddress = 0;\r
279 MaxMemoryLength = 0;\r
280 HighAddress = 0;\r
281 TopLoadingAddress = 0;\r
282 MemoryRangeEnd = 0;\r
283 CurrentHob.Raw = PrivateData->HobList.Raw;\r
284 PeiMemorySize = PrivateData->PhysicalMemoryLength;\r
285 //\r
286 // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE\r
287 // then RuntimeCodePage range and Boot time code range.\r
288 // \r
289 TotalReservedMemorySize = EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)+ PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber))\r
290 + MINIMUM_INITIAL_MEMORY_SIZE; \r
291 \r
292 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= %x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)));\r
293 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= %x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)));\r
294 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= %x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber))); \r
295 //\r
296 // PEI memory range lies below the top reserved memory\r
297 // \r
298 TotalReservedMemorySize += PeiMemorySize;\r
299 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = %lx.\n", TotalReservedMemorySize));\r
300 //\r
301 // Loop through the system memory typed hob to merge the adjacent memory range \r
302 //\r
303 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
304 // \r
305 // See if this is a resource descriptor HOB \r
306 //\r
307 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
308 \r
309 ResourceHob = Hob.ResourceDescriptor; \r
310 //\r
311 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
312 //\r
313 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY &&\r
314 ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) {\r
315 continue;\r
316 } \r
317 \r
318 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) { \r
319 if (NextHob.Raw == Hob.Raw){\r
320 continue;\r
321 } \r
322 //\r
323 // See if this is a resource descriptor HOB\r
324 //\r
325 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
326 \r
327 NextResourceHob = NextHob.ResourceDescriptor;\r
328 //\r
329 // test if range described in this NextResourceHob is system memory and have the same attribute.\r
330 // Note: Here is a assumption that system memory should always be healthy even without test.\r
331 // \r
332 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
333 (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){\r
334 \r
335 //\r
336 // See if the memory range described in ResourceHob and NextResourceHob is adjacent\r
337 //\r
338 if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart && \r
339 ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)|| \r
340 (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&\r
341 ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {\r
342 \r
343 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?\r
344 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);\r
345 \r
346 ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ? \r
347 ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;\r
348 \r
349 \r
350 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);\r
351 \r
352 ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED);\r
353 //\r
354 // Delete the NextResourceHob by marking it as unused.\r
355 //\r
356 GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED;\r
357 \r
358 }\r
359 }\r
360 } \r
361 }\r
362 } \r
363 }\r
364 //\r
365 // Try to find and validate the TOP address.\r
366 // \r
367 if ((INT64)FixedPcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {\r
368 //\r
369 // The LMFA feature is enabled as load module at fixed absolute address.\r
370 //\r
371 TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)FixedPcdGet64(PcdLoadModuleAtFixAddressEnable);\r
372 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n"));\r
373 //\r
374 // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range\r
375 //\r
376 if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) {\r
377 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address %lx is invalid since top address should be page align. \n", TopLoadingAddress)); \r
378 ASSERT (FALSE); \r
379 }\r
380 //\r
381 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies \r
382 //\r
383 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
384 //\r
385 // See if this is a resource descriptor HOB\r
386 //\r
387 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
388\r
389 ResourceHob = Hob.ResourceDescriptor;\r
390 //\r
391 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
392 // \r
393 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
394 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
395 //\r
396 // See if Top address specified by user is valid.\r
397 //\r
398 if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress && \r
399 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress) {\r
400 CurrentResourceHob = ResourceHob; \r
401 CurrentHob = Hob;\r
402 break;\r
403 }\r
404 }\r
405 } \r
406 } \r
407 if (CurrentResourceHob != NULL) {\r
408 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address %lx is valid \n", TopLoadingAddress));\r
409 TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE; \r
410 } else {\r
411 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address %lx is invalid \n", TopLoadingAddress)); \r
412 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n")); \r
413 //\r
414 // Print the recomended Top address range.\r
415 // \r
416 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
417 //\r
418 // See if this is a resource descriptor HOB\r
419 //\r
420 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
421 \r
422 ResourceHob = Hob.ResourceDescriptor;\r
423 //\r
424 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
425 // \r
426 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
427 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
428 //\r
429 // See if Top address specified by user is valid.\r
430 //\r
431 if (ResourceHob->ResourceLength > TotalReservedMemorySize) {\r
432 DEBUG ((EFI_D_INFO, "(%lx, %lx)\n", \r
433 (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE), \r
434 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE) \r
435 )); \r
436 }\r
437 }\r
438 }\r
439 } \r
440 //\r
441 // Assert here \r
442 //\r
443 ASSERT (FALSE); \r
444 } \r
445 } else {\r
446 //\r
447 // The LMFA feature is enabled as load module at fixed offset relative to TOLM\r
448 // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG)\r
449 //\r
450 //\r
451 // Search for a tested memory region that is below MAX_ADDRESS\r
452 //\r
453 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
454 //\r
455 // See if this is a resource descriptor HOB \r
456 //\r
457 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
458 \r
459 ResourceHob = Hob.ResourceDescriptor; \r
460 //\r
461 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
462 //\r
463 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && \r
464 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
465 ResourceHob->ResourceLength > TotalReservedMemorySize) {\r
466 //\r
467 // See if this is the highest largest system memory region below MaxAddress\r
468 //\r
469 if (ResourceHob->PhysicalStart > HighAddress) {\r
470 CurrentResourceHob = ResourceHob;\r
471 CurrentHob = Hob;\r
472 HighAddress = CurrentResourceHob->PhysicalStart;\r
473 }\r
474 }\r
475 } \r
476 }\r
477 if (CurrentResourceHob == NULL) {\r
478 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n")); \r
479 //\r
480 // Assert here \r
481 //\r
482 ASSERT (FALSE); \r
483 } else {\r
484 TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ; \r
485 } \r
486 }\r
487 \r
488 if (CurrentResourceHob != NULL) {\r
489 //\r
490 // rebuild hob for PEI memmory and reserved memory\r
491 //\r
492 BuildResourceDescriptorHob (\r
493 EFI_RESOURCE_SYSTEM_MEMORY, // MemoryType,\r
494 (\r
495 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
496 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
497 EFI_RESOURCE_ATTRIBUTE_TESTED |\r
498 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
499 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
500 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
501 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
502 ),\r
503 (TopLoadingAddress - TotalReservedMemorySize), // MemoryBegin\r
504 TotalReservedMemorySize // MemoryLength\r
505 );\r
506 //\r
507 // rebuild hob for the remain memory if necessary\r
508 //\r
509 if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
510 BuildResourceDescriptorHob (\r
511 EFI_RESOURCE_SYSTEM_MEMORY, // MemoryType,\r
512 (\r
513 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
514 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
515 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
516 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
517 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
518 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
519 ),\r
520 CurrentResourceHob->PhysicalStart, // MemoryBegin\r
521 (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart) // MemoryLength\r
522 );\r
523 }\r
524 if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) {\r
525 BuildResourceDescriptorHob (\r
526 EFI_RESOURCE_SYSTEM_MEMORY, \r
527 (\r
528 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
529 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
530 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
531 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
532 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
533 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
534 ),\r
535 TopLoadingAddress, \r
536 (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress) \r
537 );\r
538 }\r
539 //\r
540 // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt.\r
541 //\r
542 GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED; \r
543 }\r
544\r
545 //\r
546 // Cache the top address for Loading Module at Fixed Address feature\r
547 //\r
548 PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE;\r
549 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = %lx\n", PrivateData->LoadModuleAtFixAddressTopAddress)); \r
550 //\r
551 // reinstall the PEI memory relative to TopLoadingAddress\r
552 //\r
553 PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize;\r
554 PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize;\r
555}\r
b1f6a7c6 556/**\r
192f6d4c 557 Conduct PEIM dispatch.\r
558\r
b1f6a7c6 559 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 560 environment, such as the size and location of temporary RAM, the stack location and\r
561 the BFV location.\r
b1f6a7c6 562 @param Private Pointer to the private data passed in from caller\r
192f6d4c 563\r
b1f6a7c6 564**/\r
565VOID\r
566PeiDispatcher (\r
567 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
568 IN PEI_CORE_INSTANCE *Private\r
569 )\r
192f6d4c 570{\r
b0d803fe 571 EFI_STATUS Status;\r
572 UINT32 Index1;\r
573 UINT32 Index2;\r
6c7a807a 574 CONST EFI_PEI_SERVICES **PeiServices;\r
b0d803fe 575 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
576 UINTN FvCount;\r
577 UINTN PeimCount;\r
578 UINT32 AuthenticationState;\r
579 EFI_PHYSICAL_ADDRESS EntryPoint;\r
797a9d67 580 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
b0d803fe 581 UINTN SaveCurrentPeimCount;\r
1053e0c5 582 UINTN SaveCurrentFvCount;\r
b0d803fe 583 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
797a9d67 584 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;\r
58dcdada 585 EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase;\r
586 TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
587 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable;\r
588 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable;\r
66c69dea 589 INTN StackOffset;\r
590 INTN HeapOffset;\r
58dcdada 591 PEI_CORE_INSTANCE *PrivateInMem;\r
592 UINT64 NewPeiStackSize;\r
593 UINT64 OldPeiStackSize;\r
594 UINT64 StackGap;\r
288f9b38 595 EFI_FV_FILE_INFO FvFileInfo;\r
58dcdada 596 UINTN OldCheckingTop;\r
597 UINTN OldCheckingBottom;\r
3b428ade 598 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
54ea99a7 599 VOID *LoadFixPeiCodeBegin;\r
b0d803fe 600\r
6c7a807a 601 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->PS;\r
b0d803fe 602 PeimEntryPoint = NULL;\r
603 PeimFileHandle = NULL;\r
288f9b38 604 EntryPoint = 0;\r
b0d803fe 605\r
606 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
607 //\r
608 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
609 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
610 //\r
611 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
1053e0c5 612 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
b0d803fe 613 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
614\r
1053e0c5 615 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
188e4e84 616 for (Index2 = 0; (Index2 < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
b0d803fe 617 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
58dcdada 618 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
b0d803fe 619 Status = PeiLoadImage (\r
6c7a807a 620 (CONST EFI_PEI_SERVICES **) &Private->PS,\r
58dcdada 621 PeimFileHandle,\r
341a658f 622 PEIM_STATE_REGISITER_FOR_SHADOW,\r
58dcdada 623 &EntryPoint,\r
b0d803fe 624 &AuthenticationState\r
625 );\r
626 if (Status == EFI_SUCCESS) {\r
627 //\r
628 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
629 //\r
630 Private->Fv[Index1].PeimState[Index2]++;\r
1053e0c5 631 Private->CurrentFileHandle = PeimFileHandle;\r
58dcdada 632 Private->CurrentPeimFvCount = Index1;\r
633 Private->CurrentPeimCount = Index2;\r
b0d803fe 634 //\r
635 // Call the PEIM entry point\r
636 //\r
797a9d67 637 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
58dcdada 638\r
087e13cb 639 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
797a9d67 640 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->PS);\r
087e13cb 641 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
58dcdada 642 }\r
643\r
b0d803fe 644 //\r
645 // Process the Notify list and dispatch any notifies for\r
646 // newly installed PPIs.\r
647 //\r
648 ProcessNotifyList (Private);\r
649 }\r
650 }\r
651 }\r
58dcdada 652 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
653 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
654 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
b0d803fe 655 }\r
192f6d4c 656\r
657 //\r
658 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
659 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
660 // pass of the dispatcher, it will start over from the Bfv again to see\r
661 // if any new PEIMs dependencies got satisfied. With a well ordered\r
662 // FV where PEIMs are found in the order their dependencies are also\r
663 // satisfied, this dipatcher should run only once.\r
664 //\r
b0d803fe 665 do {\r
82b8c8df 666 //\r
667 // In case that reenter PeiCore happens, the last pass record is still available. \r
668 //\r
669 if (!Private->PeimDispatcherReenter) {\r
670 Private->PeimNeedingDispatch = FALSE;\r
671 Private->PeimDispatchOnThisPass = FALSE;\r
672 } else {\r
673 Private->PeimDispatcherReenter = FALSE;\r
674 }\r
675 \r
b0d803fe 676 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
3b428ade 677 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
678 ASSERT (CoreFvHandle != NULL);\r
679 \r
2a00326e 680 //\r
3b428ade 681 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
2a00326e 682 //\r
3b428ade 683 if (CoreFvHandle->FvPpi == NULL) {\r
684 continue;\r
685 }\r
686 \r
687 Private->CurrentPeimFvCount = FvCount;\r
192f6d4c 688\r
b0d803fe 689 if (Private->CurrentPeimCount == 0) {\r
690 //\r
691 // When going through each FV, at first, search Apriori file to\r
58dcdada 692 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
b0d803fe 693 // dispatch at first.\r
694 //\r
3b428ade 695 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);\r
b0d803fe 696 }\r
192f6d4c 697\r
698 //\r
b0d803fe 699 // Start to dispatch all modules within the current Fv.\r
192f6d4c 700 //\r
58dcdada 701 for (PeimCount = Private->CurrentPeimCount;\r
188e4e84 702 (PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
b0d803fe 703 PeimCount++) {\r
704 Private->CurrentPeimCount = PeimCount;\r
705 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
706\r
707 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
708 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
82b8c8df 709 Private->PeimNeedingDispatch = TRUE;\r
b0d803fe 710 } else {\r
3b428ade 711 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);\r
288f9b38
LG
712 ASSERT_EFI_ERROR (Status);\r
713 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
714 //\r
715 // For Fv type file, Produce new FV PPI and FV hob\r
716 //\r
3b428ade 717 Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle);\r
718 AuthenticationState = 0;\r
288f9b38
LG
719 } else {\r
720 //\r
721 // For PEIM driver, Load its entry point\r
722 //\r
723 Status = PeiLoadImage (\r
58dcdada 724 PeiServices,\r
725 PeimFileHandle,\r
341a658f 726 PEIM_STATE_NOT_DISPATCHED,\r
58dcdada 727 &EntryPoint,\r
288f9b38
LG
728 &AuthenticationState\r
729 );\r
730 }\r
731\r
b0d803fe 732 if ((Status == EFI_SUCCESS)) {\r
192f6d4c 733 //\r
58dcdada 734 // The PEIM has its dependencies satisfied, and its entry point\r
735 // has been found, so invoke it.\r
192f6d4c 736 //\r
087e13cb 737 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
192f6d4c 738\r
b0d803fe 739 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
192f6d4c 740\r
741 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
742 EFI_PROGRESS_CODE,\r
f9876ecf 743 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
192f6d4c 744 (VOID *)(&ExtendedData),\r
745 sizeof (ExtendedData)\r
746 );\r
747\r
3b428ade 748 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle);\r
b0d803fe 749 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {\r
750 //\r
751 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
752 //\r
753 Private->Fv[FvCount].PeimState[PeimCount]++;\r
58dcdada 754\r
288f9b38
LG
755 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
756 //\r
757 // Call the PEIM entry point for PEIM driver\r
758 //\r
797a9d67 759 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
760 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
288f9b38 761 }\r
797a9d67 762\r
82b8c8df 763 Private->PeimDispatchOnThisPass = TRUE;\r
192f6d4c 764 }\r
765\r
766 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
767 EFI_PROGRESS_CODE,\r
f9876ecf 768 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
192f6d4c 769 (VOID *)(&ExtendedData),\r
770 sizeof (ExtendedData)\r
771 );\r
087e13cb 772 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
b0d803fe 773\r
58dcdada 774 }\r
775\r
776 if (Private->SwitchStackSignal) {\r
a7715e73 777 //\r
3d4d0c34 778 // Before switch stack from temporary memory to permenent memory, caculate the heap and stack\r
a7715e73 779 // usage in temporary memory for debuging.\r
780 //\r
781 DEBUG_CODE_BEGIN ();\r
96317468 782 UINT32 *StackPointer;\r
a7715e73 783 \r
96317468 784 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
785 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
a7715e73 786 && (*StackPointer == INIT_CAR_VALUE);\r
787 StackPointer ++);\r
788 \r
3d4d0c34 789 DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
790 DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n",\r
a7715e73 791 (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
792 ));\r
3d4d0c34 793 DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n",\r
a7715e73 794 ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -\r
795 (UINTN) Private->HobList.Raw)\r
796 ));\r
797 DEBUG_CODE_END ();\r
798 \r
54ea99a7 799 if (FixedPcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
800 //\r
801 // Loading Module at Fixed Address is enabled\r
802 //\r
803 PeiLoadFixAddressHook(Private);\r
804 }\r
805 \r
a3a15d21 806 //\r
58dcdada 807 // Reserve the size of new stack at bottom of physical memory\r
a3a15d21 808 //\r
63b62331 809 OldPeiStackSize = (UINT64) SecCoreData->StackSize;\r
58dcdada 810 NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
f4391d63 811 if (PcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {\r
58dcdada 812 Private->StackSize = NewPeiStackSize;\r
813 } else {\r
f4391d63 814 Private->StackSize = PcdGet32(PcdPeiCoreMaxPeiStackSize);\r
58dcdada 815 }\r
192f6d4c 816\r
58dcdada 817 //\r
818 // In theory, the size of new stack in permenent memory should large than\r
819 // size of old stack in temporary memory.\r
820 // But if new stack is smaller than the size of old stack, we also reserve\r
821 // the size of old stack at bottom of permenent memory.\r
822 //\r
90e128e2 823 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize));\r
28127cc7
LG
824 ASSERT (Private->StackSize >= OldPeiStackSize);\r
825 StackGap = Private->StackSize - OldPeiStackSize;\r
d74eeda8 826\r
58dcdada 827 //\r
828 // Update HandOffHob for new installed permenent memory\r
829 //\r
830 OldHandOffTable = Private->HobList.HandoffInformationTable;\r
5c5a0601 831 OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);\r
58dcdada 832 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
192f6d4c 833\r
834 //\r
58dcdada 835 // The whole temporary memory will be migrated to physical memory.\r
836 // CAUTION: The new base is computed accounding to gap of new stack.\r
192f6d4c 837 //\r
58dcdada 838 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
40f26b8f 839 \r
840 //\r
3d4d0c34 841 // Caculate stack offset and heap offset between temporary memory and new permement \r
40f26b8f 842 // memory seperately.\r
843 //\r
d74eeda8 844 StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;\r
66c69dea 845 HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \\r
846 (UINTN) SecCoreData->PeiTemporaryRamBase);\r
7df7393f 847 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset));\r
66c69dea 848 \r
40f26b8f 849 //\r
850 // Caculate new HandOffTable and PrivateData address in permenet memory's stack\r
851 //\r
66c69dea 852 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);\r
853 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);\r
192f6d4c 854\r
855 //\r
58dcdada 856 // TemporaryRamSupportPpi is produced by platform's SEC\r
192f6d4c 857 //\r
58dcdada 858 Status = PeiLocatePpi (\r
859 (CONST EFI_PEI_SERVICES **) PeiServices,\r
860 &gEfiTemporaryRamSupportPpiGuid,\r
861 0,\r
862 NULL,\r
863 (VOID**)&TemporaryRamSupportPpi\r
864 );\r
865\r
81c7803c 866\r
58dcdada 867 if (!EFI_ERROR (Status)) {\r
40f26b8f 868 //\r
869 // Temporary Ram support Ppi is provided by platform, it will copy \r
870 // temporary memory to permenent memory and do stack switching.\r
871 // After invoken temporary Ram support, following code's stack is in \r
3d4d0c34 872 // memory but not in temporary memory.\r
40f26b8f 873 //\r
58dcdada 874 TemporaryRamSupportPpi->TemporaryRamMigration (\r
875 (CONST EFI_PEI_SERVICES **) PeiServices,\r
876 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
877 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
878 SecCoreData->TemporaryRamSize\r
879 );\r
880\r
881 } else {\r
b414ea4b 882 //\r
883 // In IA32/x64/Itanium architecture, we need platform provide\r
884 // TEMPORAY_RAM_MIGRATION_PPI.\r
885 //\r
886 ASSERT (FALSE);\r
58dcdada 887 }\r
888\r
889\r
890 //\r
b0d803fe 891 //\r
58dcdada 892 // Fixup the PeiCore's private data\r
b0d803fe 893 //\r
58dcdada 894 PrivateInMem->PS = &PrivateInMem->ServiceTableShadow;\r
895 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;\r
66c69dea 896 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);\r
58dcdada 897 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
898\r
6c7a807a 899 PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->PS;\r
58dcdada 900\r
901 //\r
902 // Fixup for PeiService's address\r
903 //\r
904 SetPeiServicesTablePointer(PeiServices);\r
905\r
906 //\r
907 // Update HandOffHob for new installed permenent memory\r
908 //\r
909 NewHandOffTable->EfiEndOfHobList =\r
66c69dea 910 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);\r
58dcdada 911 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +\r
912 PrivateInMem->PhysicalMemoryLength;\r
913 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;\r
914 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;\r
915 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
916 sizeof (EFI_HOB_GENERIC_HEADER);\r
917\r
918 //\r
919 // We need convert the PPI desciptor's pointer\r
920 //\r
40f26b8f 921 ConvertPpiPointers (PrivateInMem, \r
58dcdada 922 OldCheckingBottom, \r
923 OldCheckingTop, \r
5c5a0601 924 HeapOffset\r
925 );\r
58dcdada 926\r
7df7393f 927 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n",\r
928 PrivateInMem->StackBase,\r
58dcdada 929 PrivateInMem->StackSize));\r
930 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
931\r
932 //\r
933 // After the whole temporary memory is migrated, then we can allocate page in\r
934 // permenent memory.\r
935 //\r
936 PrivateInMem->PeiMemoryInstalled = TRUE;\r
937\r
1bd90a4c 938 //\r
82b8c8df 939 // Indicate that PeiCore reenter\r
1bd90a4c 940 //\r
82b8c8df 941 PrivateInMem->PeimDispatcherReenter = TRUE;\r
942 \r
54ea99a7 943 if (FixedPcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
944 //\r
945 // if Loading Module at Fixed Address is enabled, This is the first invoke to page \r
946 // allocation for Pei Core segment. This memory segment should be reserved for loading PEIM\r
947 //\r
948 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
949 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PeiCodeBegin = %x, PeiCodeTop= %x\n", (UINTN)LoadFixPeiCodeBegin, ((UINTN)LoadFixPeiCodeBegin) + PcdGet32(PcdLoadFixAddressPeiCodePageNumber) * EFI_PAGE_SIZE)); \r
950 //\r
951 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.\r
952 // Every bit in the array indicate the status of the corresponding memory page, available or not\r
953 //\r
954 PrivateInMem->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));\r
955 }\r
192f6d4c 956 //\r
b0d803fe 957 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
958 // PEI Core and PEIMs to get high performance.\r
192f6d4c 959 //\r
58dcdada 960 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
961 PeiServices,\r
962 PrivateInMem\r
963 );\r
b0d803fe 964 //\r
58dcdada 965 // Process the Notify list and dispatch any notifies for\r
966 // newly installed PPIs.\r
b0d803fe 967 //\r
58dcdada 968 ProcessNotifyList (PrivateInMem);\r
969\r
b0d803fe 970 //\r
58dcdada 971 // Entry PEI Phase 2\r
b0d803fe 972 //\r
58dcdada 973 PeiCore (SecCoreData, NULL, PrivateInMem);\r
b0d803fe 974\r
58dcdada 975 //\r
976 // Code should not come here\r
977 //\r
978 ASSERT_EFI_ERROR(FALSE);\r
192f6d4c 979 }\r
192f6d4c 980\r
58dcdada 981 //\r
982 // Process the Notify list and dispatch any notifies for\r
983 // newly installed PPIs.\r
984 //\r
985 ProcessNotifyList (Private);\r
986\r
b0d803fe 987 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
988 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
989 //\r
58dcdada 990 // If memory is availble we shadow images by default for performance reasons.\r
991 // We call the entry point a 2nd time so the module knows it's shadowed.\r
b0d803fe 992 //\r
993 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
e67ca95c 994 ASSERT (PeimEntryPoint != NULL);\r
797a9d67 995 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
b0d803fe 996 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
58dcdada 997\r
b0d803fe 998 //\r
999 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
1000 //\r
1001 Private->Fv[FvCount].PeimState[PeimCount]++;\r
192f6d4c 1002\r
192f6d4c 1003 //\r
b0d803fe 1004 // Process the Notify list and dispatch any notifies for\r
1005 // newly installed PPIs.\r
192f6d4c 1006 //\r
b0d803fe 1007 ProcessNotifyList (Private);\r
192f6d4c 1008 }\r
1009 }\r
1010 }\r
192f6d4c 1011 }\r
192f6d4c 1012\r
b0d803fe 1013 //\r
1014 // We set to NULL here to optimize the 2nd entry to this routine after\r
1015 // memory is found. This reprevents rescanning of the FV. We set to\r
1016 // NULL here so we start at the begining of the next FV\r
1017 //\r
1018 Private->CurrentFileHandle = NULL;\r
1019 Private->CurrentPeimCount = 0;\r
1020 //\r
1021 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
1022 //\r
1023 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
192f6d4c 1024 }\r
1025\r
1026 //\r
58dcdada 1027 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
b0d803fe 1028 // through all the FV.\r
192f6d4c 1029 //\r
b0d803fe 1030 Private->CurrentPeimFvCount = 0;\r
192f6d4c 1031\r
1032 //\r
58dcdada 1033 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
b0d803fe 1034 // dispatched. So we need to make another pass\r
192f6d4c 1035 //\r
58dcdada 1036 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
b0d803fe 1037 // pass. If we did not dispatch a PEIM there is no point in trying again\r
1038 // as it will fail the next time too (nothing has changed).\r
192f6d4c 1039 //\r
82b8c8df 1040 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
192f6d4c 1041\r
192f6d4c 1042}\r
1043\r
b1f6a7c6 1044/**\r
192f6d4c 1045 Initialize the Dispatcher's data members\r
1046\r
b1f6a7c6 1047 @param PrivateData PeiCore's private data structure\r
1048 @param OldCoreData Old data from SecCore\r
192f6d4c 1049 NULL if being run in non-permament memory mode.\r
b1f6a7c6 1050 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 1051 environment, such as the size and location of temporary RAM, the stack location and\r
1052 the BFV location.\r
192f6d4c 1053\r
b1f6a7c6 1054 @return None.\r
192f6d4c 1055\r
b1f6a7c6 1056**/\r
1057VOID\r
1058InitializeDispatcherData (\r
1059 IN PEI_CORE_INSTANCE *PrivateData,\r
1060 IN PEI_CORE_INSTANCE *OldCoreData,\r
1061 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
1062 )\r
192f6d4c 1063{\r
192f6d4c 1064 if (OldCoreData == NULL) {\r
82b8c8df 1065 PrivateData->PeimDispatcherReenter = FALSE;\r
b0d803fe 1066 PeiInitializeFv (PrivateData, SecCoreData);\r
8e0e40ed 1067 } else {\r
7ec93917 1068 PeiReinitializeFv (PrivateData);\r
192f6d4c 1069 }\r
1070\r
1071 return;\r
1072}\r
1073\r
b1f6a7c6 1074/**\r
1075 This routine parses the Dependency Expression, if available, and\r
1076 decides if the module can be executed.\r
1077\r
1078\r
1079 @param Private PeiCore's private data structure\r
1080 @param FileHandle PEIM's file handle\r
1081 @param PeimCount Peim count in all dispatched PEIMs.\r
192f6d4c 1082\r
b1f6a7c6 1083 @retval TRUE Can be dispatched\r
1084 @retval FALSE Cannot be dispatched\r
1085\r
1086**/\r
192f6d4c 1087BOOLEAN\r
1088DepexSatisfied (\r
b0d803fe 1089 IN PEI_CORE_INSTANCE *Private,\r
1090 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1091 IN UINTN PeimCount\r
192f6d4c 1092 )\r
192f6d4c 1093{\r
288f9b38
LG
1094 EFI_STATUS Status;\r
1095 VOID *DepexData;\r
b0d803fe 1096\r
1097 if (PeimCount < Private->AprioriCount) {\r
1098 //\r
1099 // If its in the A priori file then we set Depex to TRUE\r
1100 //\r
1101 return TRUE;\r
1102 }\r
58dcdada 1103\r
288f9b38 1104 //\r
58dcdada 1105 // Depex section not in the encapsulated section.\r
288f9b38
LG
1106 //\r
1107 Status = PeiServicesFfsFindSectionData (\r
1108 EFI_SECTION_PEI_DEPEX,\r
58dcdada 1109 FileHandle,\r
288f9b38
LG
1110 (VOID **)&DepexData\r
1111 );\r
b0d803fe 1112\r
192f6d4c 1113 if (EFI_ERROR (Status)) {\r
b0d803fe 1114 //\r
1115 // If there is no DEPEX, assume the module can be executed\r
1116 //\r
192f6d4c 1117 return TRUE;\r
1118 }\r
1119\r
1120 //\r
1121 // Evaluate a given DEPEX\r
1122 //\r
b0d803fe 1123 return PeimDispatchReadiness (&Private->PS, DepexData);\r
192f6d4c 1124}\r
1125\r
14e8823a 1126/**\r
1127 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
1128 discovery permanent memory.\r
1129\r
b1f6a7c6 1130 @param FileHandle File handle of a PEIM.\r
58dcdada 1131\r
b1f6a7c6 1132 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
1133 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
1134 @retval EFI_SUCCESS Successfully to register itself.\r
14e8823a 1135\r
58dcdada 1136**/\r
14e8823a 1137EFI_STATUS\r
1138EFIAPI\r
1139PeiRegisterForShadow (\r
1140 IN EFI_PEI_FILE_HANDLE FileHandle\r
1141 )\r
1142{\r
1143 PEI_CORE_INSTANCE *Private;\r
1144 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
1145\r
1146 if (Private->CurrentFileHandle != FileHandle) {\r
1147 //\r
1148 // The FileHandle must be for the current PEIM\r
1149 //\r
1150 return EFI_NOT_FOUND;\r
1151 }\r
1152\r
1153 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
1154 //\r
1155 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
1156 //\r
1157 return EFI_ALREADY_STARTED;\r
1158 }\r
58dcdada 1159\r
14e8823a 1160 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
1161\r
1162 return EFI_SUCCESS;\r
1163}\r
1164\r
3b428ade 1165\r
341a658f 1166\r