]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Fix coding style issue.
[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
656 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
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
3b428ade 767 Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle);\r
768 AuthenticationState = 0;\r
288f9b38
LG
769 } else {\r
770 //\r
771 // For PEIM driver, Load its entry point\r
772 //\r
773 Status = PeiLoadImage (\r
58dcdada 774 PeiServices,\r
775 PeimFileHandle,\r
341a658f 776 PEIM_STATE_NOT_DISPATCHED,\r
58dcdada 777 &EntryPoint,\r
288f9b38
LG
778 &AuthenticationState\r
779 );\r
780 }\r
781\r
63e78d52 782 if (Status == EFI_SUCCESS) {\r
192f6d4c 783 //\r
58dcdada 784 // The PEIM has its dependencies satisfied, and its entry point\r
785 // has been found, so invoke it.\r
192f6d4c 786 //\r
087e13cb 787 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
192f6d4c 788\r
b0d803fe 789 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
192f6d4c 790\r
791 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
792 EFI_PROGRESS_CODE,\r
f9876ecf 793 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
192f6d4c 794 (VOID *)(&ExtendedData),\r
795 sizeof (ExtendedData)\r
796 );\r
797\r
3b428ade 798 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle);\r
b0d803fe 799 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {\r
800 //\r
801 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
802 //\r
803 Private->Fv[FvCount].PeimState[PeimCount]++;\r
58dcdada 804\r
288f9b38
LG
805 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
806 //\r
807 // Call the PEIM entry point for PEIM driver\r
808 //\r
797a9d67 809 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
810 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
288f9b38 811 }\r
797a9d67 812\r
82b8c8df 813 Private->PeimDispatchOnThisPass = TRUE;\r
192f6d4c 814 }\r
815\r
816 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
817 EFI_PROGRESS_CODE,\r
619b3e0e 818 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END),\r
192f6d4c 819 (VOID *)(&ExtendedData),\r
820 sizeof (ExtendedData)\r
821 );\r
087e13cb 822 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
b0d803fe 823\r
58dcdada 824 }\r
825\r
826 if (Private->SwitchStackSignal) {\r
a7715e73 827 //\r
3d4d0c34 828 // Before switch stack from temporary memory to permenent memory, caculate the heap and stack\r
a7715e73 829 // usage in temporary memory for debuging.\r
830 //\r
831 DEBUG_CODE_BEGIN ();\r
96317468 832 UINT32 *StackPointer;\r
a7715e73 833 \r
96317468 834 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
835 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
a7715e73 836 && (*StackPointer == INIT_CAR_VALUE);\r
837 StackPointer ++);\r
838 \r
ef05e063 839 DEBUG ((EFI_D_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
840 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 841 DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
842 DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n",\r
ef05e063 843 (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
a7715e73 844 ));\r
3d4d0c34 845 DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n",\r
ef05e063 846 (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)\r
a7715e73 847 ));\r
848 DEBUG_CODE_END ();\r
849 \r
f73b3df8 850 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 851 //\r
852 // Loading Module at Fixed Address is enabled\r
853 //\r
ef05e063 854 PeiLoadFixAddressHook (Private);\r
855\r
4fb72076 856 //\r
ef05e063 857 // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.\r
4fb72076 858 //\r
859 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
3978f5d9 860 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 861 }\r
862 \r
a3a15d21 863 //\r
58dcdada 864 // Reserve the size of new stack at bottom of physical memory\r
a3a15d21 865 //\r
ef05e063 866 // The size of new stack in permenent memory must be the same size \r
867 // or larger than the size of old stack in temporary memory.\r
58dcdada 868 // But if new stack is smaller than the size of old stack, we also reserve\r
869 // the size of old stack at bottom of permenent memory.\r
870 //\r
ef05e063 871 NewStackSize = RShiftU64 (Private->PhysicalMemoryLength, 1);\r
872 NewStackSize = ALIGN_VALUE (NewStackSize, EFI_PAGE_SIZE);\r
873 NewStackSize = MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize), NewStackSize);\r
874 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (UINT32)SecCoreData->StackSize, (UINT32)NewStackSize));\r
875 ASSERT (NewStackSize >= SecCoreData->StackSize);\r
d74eeda8 876\r
58dcdada 877 //\r
ef05e063 878 // Caculate stack offset and heap offset between temporary memory and new permement \r
879 // memory seperately.\r
58dcdada 880 //\r
ef05e063 881 TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize;\r
882 TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize;\r
883 if (TopOfNewStack >= (UINTN)SecCoreData->PeiTemporaryRamBase) {\r
884 Private->HeapOffsetPositive = TRUE;\r
885 Private->HeapOffset = (UINTN)(TopOfNewStack - (UINTN)SecCoreData->PeiTemporaryRamBase);\r
886 } else {\r
887 Private->HeapOffsetPositive = FALSE;\r
888 Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - TopOfNewStack);\r
889 }\r
890 if (TopOfNewStack >= TopOfOldStack) {\r
891 StackOffsetPositive = TRUE;\r
892 StackOffset = (UINTN)(TopOfNewStack - TopOfOldStack);\r
893 } else {\r
894 StackOffsetPositive = FALSE;\r
895 StackOffset = (UINTN)(TopOfOldStack - TopOfNewStack);\r
896 }\r
424b7c9f 897 Private->StackOffsetPositive = StackOffsetPositive;\r
898 Private->StackOffset = StackOffset;\r
ef05e063 899\r
900 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64)Private->HeapOffset, (UINT64)(StackOffset)));\r
192f6d4c 901\r
902 //\r
ef05e063 903 // Build Stack HOB that describes the permanent memory stack\r
192f6d4c 904 //\r
ef05e063 905 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize));\r
906 BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize);\r
907\r
40f26b8f 908 //\r
ef05e063 909 // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address\r
40f26b8f 910 //\r
ef05e063 911 TemporaryRamBase = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase;\r
912 TemporaryRamSize = SecCoreData->TemporaryRamSize;\r
913 TemporaryStackSize = SecCoreData->StackSize;\r
914\r
40f26b8f 915 //\r
ef05e063 916 // Caculate new HandOffTable and PrivateData address in permanent memory's stack\r
40f26b8f 917 //\r
ef05e063 918 if (StackOffsetPositive) {\r
919 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData + StackOffset);\r
920 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private + StackOffset);\r
921 } else {\r
922 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData - StackOffset);\r
923 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private - StackOffset);\r
924 }\r
192f6d4c 925\r
926 //\r
58dcdada 927 // TemporaryRamSupportPpi is produced by platform's SEC\r
192f6d4c 928 //\r
ef05e063 929 Status = PeiServicesLocatePpi (\r
58dcdada 930 &gEfiTemporaryRamSupportPpiGuid,\r
931 0,\r
932 NULL,\r
933 (VOID**)&TemporaryRamSupportPpi\r
934 );\r
58dcdada 935 if (!EFI_ERROR (Status)) {\r
40f26b8f 936 //\r
ef05e063 937 // Temporary Ram Support PPI is provided by platform, it will copy \r
40f26b8f 938 // temporary memory to permenent memory and do stack switching.\r
ef05e063 939 // After invoking Temporary Ram Support PPI, the following code's \r
940 // stack is in permanent memory.\r
40f26b8f 941 //\r
58dcdada 942 TemporaryRamSupportPpi->TemporaryRamMigration (\r
ef05e063 943 PeiServices,\r
944 TemporaryRamBase,\r
945 (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize),\r
946 TemporaryRamSize\r
58dcdada 947 );\r
948\r
949 } else {\r
b414ea4b 950 //\r
951 // In IA32/x64/Itanium architecture, we need platform provide\r
ef05e063 952 // TEMPORARY_RAM_MIGRATION_PPI.\r
b414ea4b 953 //\r
954 ASSERT (FALSE);\r
58dcdada 955 }\r
956\r
b0d803fe 957 //\r
58dcdada 958 // Entry PEI Phase 2\r
b0d803fe 959 //\r
ef05e063 960 PeiCore (SecCoreData, NULL, Private);\r
b0d803fe 961\r
58dcdada 962 //\r
963 // Code should not come here\r
964 //\r
ef05e063 965 ASSERT (FALSE);\r
192f6d4c 966 }\r
192f6d4c 967\r
58dcdada 968 //\r
969 // Process the Notify list and dispatch any notifies for\r
970 // newly installed PPIs.\r
971 //\r
972 ProcessNotifyList (Private);\r
973\r
b0d803fe 974 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
975 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
976 //\r
58dcdada 977 // If memory is availble we shadow images by default for performance reasons.\r
978 // We call the entry point a 2nd time so the module knows it's shadowed.\r
b0d803fe 979 //\r
980 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
e67ca95c 981 ASSERT (PeimEntryPoint != NULL);\r
797a9d67 982 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
b0d803fe 983 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
58dcdada 984\r
b0d803fe 985 //\r
986 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
987 //\r
988 Private->Fv[FvCount].PeimState[PeimCount]++;\r
192f6d4c 989\r
192f6d4c 990 //\r
b0d803fe 991 // Process the Notify list and dispatch any notifies for\r
992 // newly installed PPIs.\r
192f6d4c 993 //\r
b0d803fe 994 ProcessNotifyList (Private);\r
192f6d4c 995 }\r
996 }\r
997 }\r
192f6d4c 998 }\r
192f6d4c 999\r
b0d803fe 1000 //\r
1001 // We set to NULL here to optimize the 2nd entry to this routine after\r
1002 // memory is found. This reprevents rescanning of the FV. We set to\r
1003 // NULL here so we start at the begining of the next FV\r
1004 //\r
1005 Private->CurrentFileHandle = NULL;\r
1006 Private->CurrentPeimCount = 0;\r
1007 //\r
1008 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
1009 //\r
1010 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
192f6d4c 1011 }\r
1012\r
1013 //\r
58dcdada 1014 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
b0d803fe 1015 // through all the FV.\r
192f6d4c 1016 //\r
b0d803fe 1017 Private->CurrentPeimFvCount = 0;\r
192f6d4c 1018\r
1019 //\r
58dcdada 1020 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
b0d803fe 1021 // dispatched. So we need to make another pass\r
192f6d4c 1022 //\r
58dcdada 1023 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
b0d803fe 1024 // pass. If we did not dispatch a PEIM there is no point in trying again\r
1025 // as it will fail the next time too (nothing has changed).\r
192f6d4c 1026 //\r
82b8c8df 1027 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
192f6d4c 1028\r
192f6d4c 1029}\r
1030\r
b1f6a7c6 1031/**\r
192f6d4c 1032 Initialize the Dispatcher's data members\r
1033\r
b1f6a7c6 1034 @param PrivateData PeiCore's private data structure\r
1035 @param OldCoreData Old data from SecCore\r
192f6d4c 1036 NULL if being run in non-permament memory mode.\r
b1f6a7c6 1037 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 1038 environment, such as the size and location of temporary RAM, the stack location and\r
1039 the BFV location.\r
192f6d4c 1040\r
b1f6a7c6 1041 @return None.\r
192f6d4c 1042\r
b1f6a7c6 1043**/\r
1044VOID\r
1045InitializeDispatcherData (\r
1046 IN PEI_CORE_INSTANCE *PrivateData,\r
1047 IN PEI_CORE_INSTANCE *OldCoreData,\r
1048 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
1049 )\r
192f6d4c 1050{\r
192f6d4c 1051 if (OldCoreData == NULL) {\r
82b8c8df 1052 PrivateData->PeimDispatcherReenter = FALSE;\r
b0d803fe 1053 PeiInitializeFv (PrivateData, SecCoreData);\r
8e0e40ed 1054 } else {\r
7ec93917 1055 PeiReinitializeFv (PrivateData);\r
192f6d4c 1056 }\r
1057\r
1058 return;\r
1059}\r
1060\r
b1f6a7c6 1061/**\r
1062 This routine parses the Dependency Expression, if available, and\r
1063 decides if the module can be executed.\r
1064\r
1065\r
1066 @param Private PeiCore's private data structure\r
1067 @param FileHandle PEIM's file handle\r
1068 @param PeimCount Peim count in all dispatched PEIMs.\r
192f6d4c 1069\r
b1f6a7c6 1070 @retval TRUE Can be dispatched\r
1071 @retval FALSE Cannot be dispatched\r
1072\r
1073**/\r
192f6d4c 1074BOOLEAN\r
1075DepexSatisfied (\r
b0d803fe 1076 IN PEI_CORE_INSTANCE *Private,\r
1077 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1078 IN UINTN PeimCount\r
192f6d4c 1079 )\r
192f6d4c 1080{\r
288f9b38
LG
1081 EFI_STATUS Status;\r
1082 VOID *DepexData;\r
6a55eea3 1083 EFI_FV_FILE_INFO FileInfo;\r
b0d803fe 1084\r
6a55eea3 1085 Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r
1086 if (EFI_ERROR (Status)) {\r
1087 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(Unknown)\n"));\r
1088 } else {\r
1089 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName));\r
1090 }\r
1091 \r
b0d803fe 1092 if (PeimCount < Private->AprioriCount) {\r
1093 //\r
1094 // If its in the A priori file then we set Depex to TRUE\r
1095 //\r
6a55eea3 1096 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
b0d803fe 1097 return TRUE;\r
1098 }\r
58dcdada 1099\r
288f9b38 1100 //\r
58dcdada 1101 // Depex section not in the encapsulated section.\r
288f9b38
LG
1102 //\r
1103 Status = PeiServicesFfsFindSectionData (\r
1104 EFI_SECTION_PEI_DEPEX,\r
58dcdada 1105 FileHandle,\r
288f9b38
LG
1106 (VOID **)&DepexData\r
1107 );\r
b0d803fe 1108\r
192f6d4c 1109 if (EFI_ERROR (Status)) {\r
b0d803fe 1110 //\r
1111 // If there is no DEPEX, assume the module can be executed\r
1112 //\r
6a55eea3 1113 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n"));\r
192f6d4c 1114 return TRUE;\r
1115 }\r
1116\r
1117 //\r
1118 // Evaluate a given DEPEX\r
1119 //\r
4140a663 1120 return PeimDispatchReadiness (&Private->Ps, DepexData);\r
192f6d4c 1121}\r
1122\r
14e8823a 1123/**\r
1124 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
1125 discovery permanent memory.\r
1126\r
b1f6a7c6 1127 @param FileHandle File handle of a PEIM.\r
58dcdada 1128\r
b1f6a7c6 1129 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
1130 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
1131 @retval EFI_SUCCESS Successfully to register itself.\r
14e8823a 1132\r
58dcdada 1133**/\r
14e8823a 1134EFI_STATUS\r
1135EFIAPI\r
1136PeiRegisterForShadow (\r
1137 IN EFI_PEI_FILE_HANDLE FileHandle\r
1138 )\r
1139{\r
1140 PEI_CORE_INSTANCE *Private;\r
1141 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
1142\r
1143 if (Private->CurrentFileHandle != FileHandle) {\r
1144 //\r
1145 // The FileHandle must be for the current PEIM\r
1146 //\r
1147 return EFI_NOT_FOUND;\r
1148 }\r
1149\r
1150 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
1151 //\r
1152 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
1153 //\r
1154 return EFI_ALREADY_STARTED;\r
1155 }\r
58dcdada 1156\r
14e8823a 1157 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
1158\r
1159 return EFI_SUCCESS;\r
1160}\r
1161\r
3b428ade 1162\r
341a658f 1163\r