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