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