]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
IntelSiliconPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core dispatch services\r
d1102dba
LG
3\r
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
d7bd924f 5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
cd5ebaa0 6This program and the accompanying materials\r
192f6d4c 7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
615c6dd0 14**/\r
192f6d4c 15\r
0d516397 16#include "PeiMain.h"\r
192f6d4c 17\r
b1f6a7c6 18/**\r
b0d803fe 19\r
20 Discover all Peims and optional Apriori file in one FV. There is at most one\r
21 Apriori file in one FV.\r
22\r
b0d803fe 23\r
3b428ade 24 @param Private Pointer to the private data passed in from caller\r
25 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.\r
b0d803fe 26\r
b1f6a7c6 27**/\r
28VOID\r
29DiscoverPeimsAndOrderWithApriori (\r
30 IN PEI_CORE_INSTANCE *Private,\r
3b428ade 31 IN PEI_CORE_FV_HANDLE *CoreFileHandle\r
b1f6a7c6 32 )\r
b0d803fe 33{\r
34 EFI_STATUS Status;\r
890e5417 35 EFI_PEI_FILE_HANDLE FileHandle;\r
177aabe6 36 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
b0d803fe 37 EFI_GUID *Apriori;\r
38 UINTN Index;\r
39 UINTN Index2;\r
40 UINTN PeimIndex;\r
41 UINTN PeimCount;\r
42 EFI_GUID *Guid;\r
fe781940 43 EFI_PEI_FILE_HANDLE *TempFileHandles;\r
b62fe570 44 EFI_GUID *TempFileGuid;\r
3b428ade 45 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
46 EFI_FV_FILE_INFO FileInfo;\r
d1102dba 47\r
3b428ade 48 FvPpi = CoreFileHandle->FvPpi;\r
d1102dba 49\r
b0d803fe 50 //\r
51 // Walk the FV and find all the PEIMs and the Apriori file.\r
52 //\r
53 AprioriFileHandle = NULL;\r
b62fe570 54 Private->CurrentFvFileHandles = NULL;\r
b0d803fe 55 Guid = NULL;\r
b0d803fe 56\r
57 //\r
b62fe570 58 // If the current Fv has been scanned, directly get its cached records.\r
b0d803fe 59 //\r
b62fe570
SZ
60 if (CoreFileHandle->ScanFv) {\r
61 Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
b0d803fe 62 return;\r
63 }\r
64\r
b62fe570
SZ
65 TempFileHandles = Private->TempFileHandles;\r
66 TempFileGuid = Private->TempFileGuid;\r
67\r
b0d803fe 68 //\r
b62fe570 69 // Go ahead to scan this Fv, get PeimCount and cache FileHandles within it to TempFileHandles.\r
b0d803fe 70 //\r
b62fe570
SZ
71 PeimCount = 0;\r
72 FileHandle = NULL;\r
73 do {\r
3b428ade 74 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
b62fe570
SZ
75 if (!EFI_ERROR (Status)) {\r
76 if (PeimCount >= Private->TempPeimCount) {\r
77 //\r
78 // Run out of room, grow the buffer.\r
79 //\r
80 TempFileHandles = AllocatePool (\r
81 sizeof (EFI_PEI_FILE_HANDLE) * (Private->TempPeimCount + TEMP_FILE_GROWTH_STEP));\r
82 ASSERT (TempFileHandles != NULL);\r
83 CopyMem (\r
84 TempFileHandles,\r
85 Private->TempFileHandles,\r
86 sizeof (EFI_PEI_FILE_HANDLE) * Private->TempPeimCount\r
87 );\r
88 Private->TempFileHandles = TempFileHandles;\r
89 TempFileGuid = AllocatePool (\r
90 sizeof (EFI_GUID) * (Private->TempPeimCount + TEMP_FILE_GROWTH_STEP));\r
91 ASSERT (TempFileGuid != NULL);\r
92 CopyMem (\r
93 TempFileGuid,\r
94 Private->TempFileGuid,\r
95 sizeof (EFI_GUID) * Private->TempPeimCount\r
96 );\r
97 Private->TempFileGuid = TempFileGuid;\r
98 Private->TempPeimCount = Private->TempPeimCount + TEMP_FILE_GROWTH_STEP;\r
99 }\r
100\r
101 TempFileHandles[PeimCount++] = FileHandle;\r
b0d803fe 102 }\r
b62fe570
SZ
103 } while (!EFI_ERROR (Status));\r
104\r
105 DEBUG ((\r
106 DEBUG_INFO,\r
107 "%a(): Found 0x%x PEI FFS files in the %dth FV\n",\r
108 __FUNCTION__,\r
109 PeimCount,\r
110 Private->CurrentPeimFvCount\r
111 ));\r
58dcdada 112\r
b62fe570
SZ
113 if (PeimCount == 0) {\r
114 //\r
115 // No PEIM FFS file is found, set ScanFv flag and return.\r
116 //\r
117 CoreFileHandle->ScanFv = TRUE;\r
118 return;\r
b0d803fe 119 }\r
9b8e61be 120\r
97b2c9b5 121 //\r
b62fe570 122 // Record PeimCount, allocate buffer for PeimState and FvFileHandles.\r
97b2c9b5 123 //\r
b62fe570
SZ
124 CoreFileHandle->PeimCount = PeimCount;\r
125 CoreFileHandle->PeimState = AllocateZeroPool (sizeof (UINT8) * PeimCount);\r
126 ASSERT (CoreFileHandle->PeimState != NULL);\r
127 CoreFileHandle->FvFileHandles = AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
128 ASSERT (CoreFileHandle->FvFileHandles != NULL);\r
b0d803fe 129\r
3b428ade 130 //\r
131 // Get Apriori File handle\r
132 //\r
58dcdada 133 Private->AprioriCount = 0;\r
3b428ade 134 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);\r
135 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {\r
b0d803fe 136 //\r
137 // Read the Apriori file\r
138 //\r
3b428ade 139 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
b0d803fe 140 if (!EFI_ERROR (Status)) {\r
141 //\r
b62fe570 142 // Calculate the number of PEIMs in the Apriori file\r
b0d803fe 143 //\r
3b428ade 144 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
145 ASSERT_EFI_ERROR (Status);\r
890e5417
SZ
146 Private->AprioriCount = FileInfo.BufferSize;\r
147 if (IS_SECTION2 (FileInfo.Buffer)) {\r
148 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER2);\r
149 } else {\r
150 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);\r
151 }\r
b0d803fe 152 Private->AprioriCount /= sizeof (EFI_GUID);\r
58dcdada 153\r
b0d803fe 154 for (Index = 0; Index < PeimCount; Index++) {\r
155 //\r
156 // Make an array of file name guids that matches the FileHandle array so we can convert\r
157 // quickly from file name to file handle\r
158 //\r
b62fe570
SZ
159 Status = FvPpi->GetFileInfo (FvPpi, TempFileHandles[Index], &FileInfo);\r
160 ASSERT_EFI_ERROR (Status);\r
161 CopyMem (&TempFileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
b0d803fe 162 }\r
163\r
164 //\r
b62fe570
SZ
165 // Walk through TempFileGuid array to find out who is invalid PEIM guid in Apriori file.\r
166 // Add available PEIMs in Apriori file into FvFileHandles array.\r
b0d803fe 167 //\r
b62fe570
SZ
168 Index = 0;\r
169 for (Index2 = 0; Index2 < Private->AprioriCount; Index2++) {\r
170 Guid = ScanGuid (TempFileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2]);\r
171 if (Guid != NULL) {\r
172 PeimIndex = ((UINTN)Guid - (UINTN)&TempFileGuid[0])/sizeof (EFI_GUID);\r
173 CoreFileHandle->FvFileHandles[Index++] = TempFileHandles[PeimIndex];\r
b0d803fe 174\r
b62fe570
SZ
175 //\r
176 // Since we have copied the file handle we can remove it from this list.\r
177 //\r
178 TempFileHandles[PeimIndex] = NULL;\r
179 }\r
b0d803fe 180 }\r
181\r
182 //\r
b62fe570 183 // Update valid AprioriCount\r
b0d803fe 184 //\r
185 Private->AprioriCount = Index;\r
58dcdada 186\r
b0d803fe 187 //\r
188 // Add in any PEIMs not in the Apriori file\r
189 //\r
b62fe570
SZ
190 for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
191 if (TempFileHandles[Index2] != NULL) {\r
192 CoreFileHandle->FvFileHandles[Index++] = TempFileHandles[Index2];\r
193 TempFileHandles[Index2] = NULL;\r
b0d803fe 194 }\r
195 }\r
b62fe570 196 ASSERT (Index == PeimCount);\r
b0d803fe 197 }\r
b62fe570
SZ
198 } else {\r
199 CopyMem (CoreFileHandle->FvFileHandles, TempFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
b0d803fe 200 }\r
b62fe570 201\r
b0d803fe 202 //\r
b62fe570
SZ
203 // The current Fv File Handles have been cached. So that we don't have to scan the Fv again.\r
204 // Instead, we can retrieve the file handles within this Fv from cached records.\r
b0d803fe 205 //\r
b62fe570
SZ
206 CoreFileHandle->ScanFv = TRUE;\r
207 Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
58dcdada 208}\r
209\r
54ea99a7 210//\r
211// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
d1102dba 212// This part of memory still need reserved on the very top of memory so that the DXE Core could\r
54ea99a7 213// use these memory for data initialization. This macro should be sync with the same marco\r
214// defined in DXE Core.\r
215//\r
216#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
9bfb4940 217/**\r
d1102dba
LG
218 This function is to test if the memory range described in resource HOB is available or not.\r
219\r
220 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the\r
9bfb4940 221 memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is\r
222 available or not.\r
223\r
224 @param PrivateData Pointer to the private data passed in from caller\r
225 @param ResourceHob Pointer to a resource HOB which described the memory range described by the input resource HOB\r
226**/\r
227BOOLEAN\r
228PeiLoadFixAddressIsMemoryRangeAvailable (\r
229 IN PEI_CORE_INSTANCE *PrivateData,\r
230 IN EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob\r
231 )\r
232{\r
d1102dba
LG
233 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
234 BOOLEAN IsAvailable;\r
235 EFI_PEI_HOB_POINTERS Hob;\r
236\r
9bfb4940 237 IsAvailable = TRUE;\r
d1102dba
LG
238 if (PrivateData == NULL || ResourceHob == NULL) {\r
239 return FALSE;\r
240 }\r
241 //\r
9bfb4940 242 // test if the memory range describe in the HOB is already allocated.\r
243 //\r
244 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
9bfb4940 245 //\r
d1102dba
LG
246 // See if this is a memory allocation HOB\r
247 //\r
248 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
9bfb4940 249 MemoryHob = Hob.MemoryAllocation;\r
d1102dba 250 if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart &&\r
9bfb4940 251 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) {\r
252 IsAvailable = FALSE;\r
d1102dba 253 break;\r
9bfb4940 254 }\r
255 }\r
256 }\r
d1102dba 257\r
9bfb4940 258 return IsAvailable;\r
d1102dba 259\r
9bfb4940 260}\r
54ea99a7 261/**\r
262 Hook function for Loading Module at Fixed Address feature\r
d1102dba 263\r
54ea99a7 264 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is\r
d1102dba
LG
265 configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When\r
266 feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general.\r
267 And also the function will re-install PEI memory.\r
b0d803fe 268\r
54ea99a7 269 @param PrivateData Pointer to the private data passed in from caller\r
270\r
271**/\r
272VOID\r
273PeiLoadFixAddressHook(\r
274 IN PEI_CORE_INSTANCE *PrivateData\r
275 )\r
276{\r
277 EFI_PHYSICAL_ADDRESS TopLoadingAddress;\r
278 UINT64 PeiMemorySize;\r
279 UINT64 TotalReservedMemorySize;\r
280 UINT64 MemoryRangeEnd;\r
d1102dba 281 EFI_PHYSICAL_ADDRESS HighAddress;\r
54ea99a7 282 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
283 EFI_HOB_RESOURCE_DESCRIPTOR *NextResourceHob;\r
284 EFI_HOB_RESOURCE_DESCRIPTOR *CurrentResourceHob;\r
285 EFI_PEI_HOB_POINTERS CurrentHob;\r
286 EFI_PEI_HOB_POINTERS Hob;\r
287 EFI_PEI_HOB_POINTERS NextHob;\r
9bfb4940 288 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
54ea99a7 289 //\r
290 // Initialize Local Variables\r
291 //\r
292 CurrentResourceHob = NULL;\r
293 ResourceHob = NULL;\r
294 NextResourceHob = NULL;\r
54ea99a7 295 HighAddress = 0;\r
296 TopLoadingAddress = 0;\r
297 MemoryRangeEnd = 0;\r
298 CurrentHob.Raw = PrivateData->HobList.Raw;\r
299 PeiMemorySize = PrivateData->PhysicalMemoryLength;\r
300 //\r
301 // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE\r
302 // then RuntimeCodePage range and Boot time code range.\r
d1102dba 303 //\r
e18fa167 304 TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber));\r
d1102dba 305 TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ;\r
54ea99a7 306 //\r
307 // PEI memory range lies below the top reserved memory\r
d1102dba 308 //\r
54ea99a7 309 TotalReservedMemorySize += PeiMemorySize;\r
d1102dba 310\r
852081fc 311 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)));\r
312 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)));\r
d1102dba 313 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber)));\r
852081fc 314 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize));\r
54ea99a7 315 //\r
d1102dba 316 // Loop through the system memory typed hob to merge the adjacent memory range\r
54ea99a7 317 //\r
318 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
d1102dba
LG
319 //\r
320 // See if this is a resource descriptor HOB\r
54ea99a7 321 //\r
322 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
d1102dba
LG
323\r
324 ResourceHob = Hob.ResourceDescriptor;\r
54ea99a7 325 //\r
326 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
327 //\r
63e70348 328 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY ||\r
54ea99a7 329 ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) {\r
330 continue;\r
d1102dba
LG
331 }\r
332\r
333 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
54ea99a7 334 if (NextHob.Raw == Hob.Raw){\r
335 continue;\r
d1102dba 336 }\r
54ea99a7 337 //\r
338 // See if this is a resource descriptor HOB\r
339 //\r
340 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
d1102dba 341\r
54ea99a7 342 NextResourceHob = NextHob.ResourceDescriptor;\r
343 //\r
344 // test if range described in this NextResourceHob is system memory and have the same attribute.\r
345 // Note: Here is a assumption that system memory should always be healthy even without test.\r
d1102dba 346 //\r
54ea99a7 347 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
348 (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){\r
d1102dba 349\r
54ea99a7 350 //\r
351 // See if the memory range described in ResourceHob and NextResourceHob is adjacent\r
352 //\r
d1102dba
LG
353 if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart &&\r
354 ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)||\r
54ea99a7 355 (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&\r
356 ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {\r
d1102dba 357\r
54ea99a7 358 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?\r
359 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);\r
d1102dba
LG
360\r
361 ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ?\r
54ea99a7 362 ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;\r
d1102dba
LG
363\r
364\r
54ea99a7 365 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);\r
d1102dba 366\r
54ea99a7 367 ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED);\r
368 //\r
369 // Delete the NextResourceHob by marking it as unused.\r
370 //\r
371 GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED;\r
d1102dba 372\r
54ea99a7 373 }\r
374 }\r
d1102dba 375 }\r
54ea99a7 376 }\r
d1102dba 377 }\r
54ea99a7 378 }\r
9bfb4940 379 //\r
380 // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe\r
381 // the allocated memory range\r
382 //\r
383 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
d1102dba
LG
384 //\r
385 // See if this is a memory allocation HOB\r
9bfb4940 386 //\r
387 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
388 MemoryHob = Hob.MemoryAllocation;\r
389 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
390 //\r
391 // See if this is a resource descriptor HOB\r
392 //\r
393 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
d1102dba 394 NextResourceHob = NextHob.ResourceDescriptor;\r
9bfb4940 395 //\r
396 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
397 //\r
63e70348 398 if (NextResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY || NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) {\r
9bfb4940 399 continue;\r
400 }\r
401 //\r
402 // If the range describe in memory allocation HOB belongs to the memroy range described by the resource hob\r
d1102dba
LG
403 //\r
404 if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart &&\r
9bfb4940 405 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
406 //\r
407 // Build seperate resource hob for this allocated range\r
d1102dba 408 //\r
9bfb4940 409 if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) {\r
410 BuildResourceDescriptorHob (\r
d1102dba 411 EFI_RESOURCE_SYSTEM_MEMORY,\r
9bfb4940 412 NextResourceHob->ResourceAttribute,\r
d1102dba
LG
413 NextResourceHob->PhysicalStart,\r
414 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)\r
9bfb4940 415 );\r
416 }\r
417 if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
418 BuildResourceDescriptorHob (\r
d1102dba 419 EFI_RESOURCE_SYSTEM_MEMORY,\r
9bfb4940 420 NextResourceHob->ResourceAttribute,\r
d1102dba
LG
421 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,\r
422 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))\r
9bfb4940 423 );\r
424 }\r
425 NextResourceHob->PhysicalStart = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
426 NextResourceHob->ResourceLength = MemoryHob->AllocDescriptor.MemoryLength;\r
427 break;\r
428 }\r
429 }\r
430 }\r
431 }\r
432 }\r
433\r
54ea99a7 434 //\r
435 // Try to find and validate the TOP address.\r
d1102dba 436 //\r
852081fc 437 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {\r
54ea99a7 438 //\r
439 // The LMFA feature is enabled as load module at fixed absolute address.\r
440 //\r
852081fc 441 TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)PcdGet64(PcdLoadModuleAtFixAddressEnable);\r
54ea99a7 442 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n"));\r
443 //\r
444 // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range\r
445 //\r
446 if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) {\r
d1102dba
LG
447 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress));\r
448 ASSERT (FALSE);\r
54ea99a7 449 }\r
450 //\r
d1102dba 451 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies\r
54ea99a7 452 //\r
453 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
454 //\r
455 // See if this is a resource descriptor HOB\r
456 //\r
457 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
458\r
459 ResourceHob = Hob.ResourceDescriptor;\r
460 //\r
461 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
d1102dba 462 //\r
54ea99a7 463 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
464 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
465 //\r
466 // See if Top address specified by user is valid.\r
467 //\r
d1102dba
LG
468 if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress &&\r
469 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress &&\r
9bfb4940 470 PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
d1102dba 471 CurrentResourceHob = ResourceHob;\r
54ea99a7 472 CurrentHob = Hob;\r
473 break;\r
9bfb4940 474 }\r
54ea99a7 475 }\r
d1102dba
LG
476 }\r
477 }\r
54ea99a7 478 if (CurrentResourceHob != NULL) {\r
852081fc 479 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress));\r
d1102dba 480 TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE;\r
54ea99a7 481 } else {\r
d1102dba
LG
482 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n", TopLoadingAddress));\r
483 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n"));\r
54ea99a7 484 //\r
485 // Print the recomended Top address range.\r
d1102dba 486 //\r
54ea99a7 487 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
488 //\r
489 // See if this is a resource descriptor HOB\r
490 //\r
491 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
d1102dba 492\r
54ea99a7 493 ResourceHob = Hob.ResourceDescriptor;\r
494 //\r
495 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
d1102dba 496 //\r
54ea99a7 497 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
498 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
499 //\r
500 // See if Top address specified by user is valid.\r
501 //\r
9bfb4940 502 if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
d1102dba
LG
503 DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n",\r
504 (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE),\r
505 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE)\r
506 ));\r
54ea99a7 507 }\r
508 }\r
509 }\r
d1102dba 510 }\r
54ea99a7 511 //\r
d1102dba 512 // Assert here\r
54ea99a7 513 //\r
d1102dba
LG
514 ASSERT (FALSE);\r
515 return;\r
516 }\r
54ea99a7 517 } else {\r
518 //\r
519 // The LMFA feature is enabled as load module at fixed offset relative to TOLM\r
520 // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG)\r
521 //\r
522 //\r
523 // Search for a tested memory region that is below MAX_ADDRESS\r
524 //\r
525 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
526 //\r
d1102dba 527 // See if this is a resource descriptor HOB\r
54ea99a7 528 //\r
529 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
d1102dba
LG
530\r
531 ResourceHob = Hob.ResourceDescriptor;\r
54ea99a7 532 //\r
533 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
534 //\r
d1102dba 535 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
54ea99a7 536 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
9bfb4940 537 ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
54ea99a7 538 //\r
539 // See if this is the highest largest system memory region below MaxAddress\r
540 //\r
541 if (ResourceHob->PhysicalStart > HighAddress) {\r
542 CurrentResourceHob = ResourceHob;\r
543 CurrentHob = Hob;\r
544 HighAddress = CurrentResourceHob->PhysicalStart;\r
545 }\r
546 }\r
d1102dba 547 }\r
54ea99a7 548 }\r
549 if (CurrentResourceHob == NULL) {\r
d1102dba 550 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n"));\r
54ea99a7 551 //\r
d1102dba 552 // Assert here\r
54ea99a7 553 //\r
9bfb4940 554 ASSERT (FALSE);\r
d1102dba 555 return;\r
54ea99a7 556 } else {\r
d1102dba
LG
557 TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ;\r
558 }\r
54ea99a7 559 }\r
d1102dba 560\r
54ea99a7 561 if (CurrentResourceHob != NULL) {\r
562 //\r
9bfb4940 563 // rebuild resource HOB for PEI memmory and reserved memory\r
54ea99a7 564 //\r
565 BuildResourceDescriptorHob (\r
d1102dba 566 EFI_RESOURCE_SYSTEM_MEMORY,\r
54ea99a7 567 (\r
568 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
569 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
570 EFI_RESOURCE_ATTRIBUTE_TESTED |\r
571 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
572 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
573 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
574 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
575 ),\r
d1102dba
LG
576 (TopLoadingAddress - TotalReservedMemorySize),\r
577 TotalReservedMemorySize\r
54ea99a7 578 );\r
579 //\r
9bfb4940 580 // rebuild resource for the remain memory if necessary\r
54ea99a7 581 //\r
582 if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
583 BuildResourceDescriptorHob (\r
d1102dba 584 EFI_RESOURCE_SYSTEM_MEMORY,\r
54ea99a7 585 (\r
586 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
587 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
588 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
589 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
590 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
591 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
592 ),\r
d1102dba
LG
593 CurrentResourceHob->PhysicalStart,\r
594 (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)\r
54ea99a7 595 );\r
596 }\r
597 if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) {\r
598 BuildResourceDescriptorHob (\r
d1102dba 599 EFI_RESOURCE_SYSTEM_MEMORY,\r
54ea99a7 600 (\r
601 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
602 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
603 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
604 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
605 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
606 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
607 ),\r
d1102dba
LG
608 TopLoadingAddress,\r
609 (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress)\r
54ea99a7 610 );\r
611 }\r
612 //\r
613 // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt.\r
614 //\r
d1102dba 615 GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED;\r
54ea99a7 616 }\r
617\r
618 //\r
619 // Cache the top address for Loading Module at Fixed Address feature\r
620 //\r
621 PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE;\r
d1102dba 622 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData->LoadModuleAtFixAddressTopAddress));\r
54ea99a7 623 //\r
624 // reinstall the PEI memory relative to TopLoadingAddress\r
625 //\r
626 PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize;\r
627 PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize;\r
628}\r
0f9ebb32
LG
629\r
630/**\r
631 This routine is invoked in switch stack as PeiCore Entry.\r
632\r
633 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
634 environment, such as the size and location of temporary RAM, the stack location and\r
635 the BFV location.\r
636 @param Private Pointer to old core data that is used to initialize the\r
637 core's data areas.\r
638**/\r
639VOID\r
640EFIAPI\r
641PeiCoreEntry (\r
642 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
643 IN PEI_CORE_INSTANCE *Private\r
644 )\r
645{\r
646 //\r
647 // Entry PEI Phase 2\r
648 //\r
649 PeiCore (SecCoreData, NULL, Private);\r
650}\r
651\r
bfb685da
SZ
652/**\r
653 Check SwitchStackSignal and switch stack if SwitchStackSignal is TRUE.\r
654\r
655 @param[in] SecCoreData Points to a data structure containing information about the PEI core's operating\r
656 environment, such as the size and location of temporary RAM, the stack location and\r
657 the BFV location.\r
658 @param[in] Private Pointer to the private data passed in from caller.\r
659\r
660**/\r
661VOID\r
662PeiCheckAndSwitchStack (\r
663 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
664 IN PEI_CORE_INSTANCE *Private\r
665 )\r
666{\r
667 VOID *LoadFixPeiCodeBegin;\r
668 EFI_STATUS Status;\r
669 CONST EFI_PEI_SERVICES **PeiServices;\r
670 UINT64 NewStackSize;\r
671 EFI_PHYSICAL_ADDRESS TopOfOldStack;\r
672 EFI_PHYSICAL_ADDRESS TopOfNewStack;\r
673 UINTN StackOffset;\r
674 BOOLEAN StackOffsetPositive;\r
675 EFI_PHYSICAL_ADDRESS TemporaryRamBase;\r
676 UINTN TemporaryRamSize;\r
677 UINTN TemporaryStackSize;\r
678 VOID *TemporaryStackBase;\r
679 UINTN PeiTemporaryRamSize;\r
680 VOID *PeiTemporaryRamBase;\r
681 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
682 EFI_PHYSICAL_ADDRESS BaseOfNewHeap;\r
683 EFI_PHYSICAL_ADDRESS HoleMemBase;\r
684 UINTN HoleMemSize;\r
685 UINTN HeapTemporaryRamSize;\r
686 EFI_PHYSICAL_ADDRESS TempBase1;\r
687 UINTN TempSize1;\r
688 EFI_PHYSICAL_ADDRESS TempBase2;\r
689 UINTN TempSize2;\r
690 UINTN Index;\r
691\r
692 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
693\r
694 if (Private->SwitchStackSignal) {\r
695 //\r
6393d9c8
GL
696 // Before switch stack from temporary memory to permanent memory, calculate the heap and stack\r
697 // usage in temporary memory for debugging.\r
bfb685da
SZ
698 //\r
699 DEBUG_CODE_BEGIN ();\r
aa57c0f0
SZ
700 UINT32 *StackPointer;\r
701 EFI_PEI_HOB_POINTERS Hob;\r
bfb685da
SZ
702\r
703 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
704 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
98127071 705 && (*StackPointer == PcdGet32 (PcdInitValueInTempStack));\r
be18cb03
LG
706 StackPointer ++) {\r
707 }\r
bfb685da 708\r
b2374cec
SZ
709 DEBUG ((DEBUG_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
710 DEBUG ((DEBUG_INFO, "Temp Heap : BaseAddress=0x%p Length=0x%X\n", SecCoreData->PeiTemporaryRamBase, (UINT32)SecCoreData->PeiTemporaryRamSize));\r
711 DEBUG ((DEBUG_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
712 DEBUG ((DEBUG_INFO, " temporary memory stack ever used: %d bytes.\n",\r
df56e808
AF
713 (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
714 ));\r
b2374cec 715 DEBUG ((DEBUG_INFO, " temporary memory heap used for HobList: %d bytes.\n",\r
df56e808
AF
716 (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)\r
717 ));\r
aa57c0f0
SZ
718 DEBUG ((DEBUG_INFO, " temporary memory heap occupied by memory pages: %d bytes.\n",\r
719 (UINT32)(UINTN)(Private->HobList.HandoffInformationTable->EfiMemoryTop - Private->HobList.HandoffInformationTable->EfiFreeMemoryTop)\r
720 ));\r
721 for (Hob.Raw = Private->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
722 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
723 DEBUG ((DEBUG_INFO, "Memory Allocation 0x%08x 0x%0lx - 0x%0lx\n", \\r
724 Hob.MemoryAllocation->AllocDescriptor.MemoryType, \\r
725 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress, \\r
726 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress + Hob.MemoryAllocation->AllocDescriptor.MemoryLength - 1));\r
727 }\r
728 }\r
bfb685da
SZ
729 DEBUG_CODE_END ();\r
730\r
731 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
732 //\r
733 // Loading Module at Fixed Address is enabled\r
734 //\r
735 PeiLoadFixAddressHook (Private);\r
736\r
737 //\r
738 // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.\r
739 //\r
740 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
741 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
742 }\r
743\r
744 //\r
745 // Reserve the size of new stack at bottom of physical memory\r
746 //\r
6393d9c8 747 // The size of new stack in permanent memory must be the same size\r
bfb685da
SZ
748 // or larger than the size of old stack in temporary memory.\r
749 // But if new stack is smaller than the size of old stack, we also reserve\r
6393d9c8 750 // the size of old stack at bottom of permanent memory.\r
bfb685da
SZ
751 //\r
752 NewStackSize = RShiftU64 (Private->PhysicalMemoryLength, 1);\r
753 NewStackSize = ALIGN_VALUE (NewStackSize, EFI_PAGE_SIZE);\r
754 NewStackSize = MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize), NewStackSize);\r
755 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (UINT32)SecCoreData->StackSize, (UINT32)NewStackSize));\r
756 ASSERT (NewStackSize >= SecCoreData->StackSize);\r
757\r
758 //\r
d1102dba 759 // Calculate stack offset and heap offset between temporary memory and new permement\r
bfb685da
SZ
760 // memory seperately.\r
761 //\r
762 TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize;\r
763 TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize;\r
764 if (TopOfNewStack >= TopOfOldStack) {\r
765 StackOffsetPositive = TRUE;\r
766 StackOffset = (UINTN)(TopOfNewStack - TopOfOldStack);\r
767 } else {\r
768 StackOffsetPositive = FALSE;\r
769 StackOffset = (UINTN)(TopOfOldStack - TopOfNewStack);\r
770 }\r
771 Private->StackOffsetPositive = StackOffsetPositive;\r
772 Private->StackOffset = StackOffset;\r
773\r
774 //\r
775 // Build Stack HOB that describes the permanent memory stack\r
776 //\r
777 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize));\r
778 BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize);\r
779\r
780 //\r
781 // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address\r
782 //\r
783 TemporaryRamBase = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase;\r
784 TemporaryRamSize = SecCoreData->TemporaryRamSize;\r
785 TemporaryStackSize = SecCoreData->StackSize;\r
786 TemporaryStackBase = SecCoreData->StackBase;\r
787 PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize;\r
788 PeiTemporaryRamBase = SecCoreData->PeiTemporaryRamBase;\r
789\r
790 //\r
791 // TemporaryRamSupportPpi is produced by platform's SEC\r
792 //\r
793 Status = PeiServicesLocatePpi (\r
794 &gEfiTemporaryRamSupportPpiGuid,\r
795 0,\r
796 NULL,\r
797 (VOID**)&TemporaryRamSupportPpi\r
798 );\r
799 if (!EFI_ERROR (Status)) {\r
800 //\r
801 // Heap Offset\r
802 //\r
803 BaseOfNewHeap = TopOfNewStack;\r
804 if (BaseOfNewHeap >= (UINTN)SecCoreData->PeiTemporaryRamBase) {\r
805 Private->HeapOffsetPositive = TRUE;\r
806 Private->HeapOffset = (UINTN)(BaseOfNewHeap - (UINTN)SecCoreData->PeiTemporaryRamBase);\r
807 } else {\r
808 Private->HeapOffsetPositive = FALSE;\r
809 Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - BaseOfNewHeap);\r
810 }\r
811\r
812 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64) Private->HeapOffset, (UINT64) Private->StackOffset));\r
813\r
814 //\r
815 // Calculate new HandOffTable and PrivateData address in permanent memory's stack\r
816 //\r
817 if (StackOffsetPositive) {\r
818 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData + StackOffset);\r
819 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private + StackOffset);\r
820 } else {\r
821 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData - StackOffset);\r
822 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private - StackOffset);\r
823 }\r
824\r
825 //\r
d1102dba 826 // Temporary Ram Support PPI is provided by platform, it will copy\r
6393d9c8 827 // temporary memory to permanent memory and do stack switching.\r
d1102dba 828 // After invoking Temporary Ram Support PPI, the following code's\r
bfb685da
SZ
829 // stack is in permanent memory.\r
830 //\r
831 TemporaryRamSupportPpi->TemporaryRamMigration (\r
832 PeiServices,\r
833 TemporaryRamBase,\r
834 (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize),\r
835 TemporaryRamSize\r
836 );\r
837\r
b2374cec
SZ
838 //\r
839 // Migrate memory pages allocated in pre-memory phase.\r
840 // It could not be called before calling TemporaryRamSupportPpi->TemporaryRamMigration()\r
841 // as the migrated memory pages may be overridden by TemporaryRamSupportPpi->TemporaryRamMigration().\r
842 //\r
843 MigrateMemoryPages (Private, TRUE);\r
844\r
bfb685da
SZ
845 //\r
846 // Entry PEI Phase 2\r
847 //\r
848 PeiCore (SecCoreData, NULL, Private);\r
849 } else {\r
b2374cec
SZ
850 //\r
851 // Migrate memory pages allocated in pre-memory phase.\r
852 //\r
853 MigrateMemoryPages (Private, FALSE);\r
854\r
bfb685da
SZ
855 //\r
856 // Migrate the PEI Services Table pointer from temporary RAM to permanent RAM.\r
857 //\r
858 MigratePeiServicesTablePointer ();\r
b2374cec 859\r
bfb685da
SZ
860 //\r
861 // Heap Offset\r
862 //\r
863 BaseOfNewHeap = TopOfNewStack;\r
864 HoleMemBase = TopOfNewStack;\r
865 HoleMemSize = TemporaryRamSize - PeiTemporaryRamSize - TemporaryStackSize;\r
866 if (HoleMemSize != 0) {\r
867 //\r
868 // Make sure HOB List start address is 8 byte alignment.\r
869 //\r
870 BaseOfNewHeap = ALIGN_VALUE (BaseOfNewHeap + HoleMemSize, 8);\r
871 }\r
872 if (BaseOfNewHeap >= (UINTN)SecCoreData->PeiTemporaryRamBase) {\r
873 Private->HeapOffsetPositive = TRUE;\r
874 Private->HeapOffset = (UINTN)(BaseOfNewHeap - (UINTN)SecCoreData->PeiTemporaryRamBase);\r
875 } else {\r
876 Private->HeapOffsetPositive = FALSE;\r
877 Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - BaseOfNewHeap);\r
878 }\r
879\r
880 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64) Private->HeapOffset, (UINT64) Private->StackOffset));\r
881\r
882 //\r
883 // Migrate Heap\r
884 //\r
885 HeapTemporaryRamSize = (UINTN) (Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - Private->HobList.HandoffInformationTable->EfiMemoryBottom);\r
886 ASSERT (BaseOfNewHeap + HeapTemporaryRamSize <= Private->FreePhysicalMemoryTop);\r
b2374cec 887 CopyMem ((UINT8 *) (UINTN) BaseOfNewHeap, PeiTemporaryRamBase, HeapTemporaryRamSize);\r
bfb685da
SZ
888\r
889 //\r
890 // Migrate Stack\r
891 //\r
892 CopyMem ((UINT8 *) (UINTN) (TopOfNewStack - TemporaryStackSize), TemporaryStackBase, TemporaryStackSize);\r
893\r
894 //\r
895 // Copy Hole Range Data\r
bfb685da
SZ
896 //\r
897 if (HoleMemSize != 0) {\r
898 //\r
899 // Prepare Hole\r
900 //\r
901 if (PeiTemporaryRamBase < TemporaryStackBase) {\r
902 TempBase1 = (EFI_PHYSICAL_ADDRESS) (UINTN) PeiTemporaryRamBase;\r
903 TempSize1 = PeiTemporaryRamSize;\r
904 TempBase2 = (EFI_PHYSICAL_ADDRESS) (UINTN) TemporaryStackBase;\r
905 TempSize2 = TemporaryStackSize;\r
906 } else {\r
907 TempBase1 = (EFI_PHYSICAL_ADDRESS) (UINTN) TemporaryStackBase;\r
908 TempSize1 = TemporaryStackSize;\r
909 TempBase2 =(EFI_PHYSICAL_ADDRESS) (UINTN) PeiTemporaryRamBase;\r
910 TempSize2 = PeiTemporaryRamSize;\r
911 }\r
912 if (TemporaryRamBase < TempBase1) {\r
913 Private->HoleData[0].Base = TemporaryRamBase;\r
914 Private->HoleData[0].Size = (UINTN) (TempBase1 - TemporaryRamBase);\r
915 }\r
916 if (TempBase1 + TempSize1 < TempBase2) {\r
917 Private->HoleData[1].Base = TempBase1 + TempSize1;\r
918 Private->HoleData[1].Size = (UINTN) (TempBase2 - TempBase1 - TempSize1);\r
919 }\r
920 if (TempBase2 + TempSize2 < TemporaryRamBase + TemporaryRamSize) {\r
921 Private->HoleData[2].Base = TempBase2 + TempSize2;\r
922 Private->HoleData[2].Size = (UINTN) (TemporaryRamBase + TemporaryRamSize - TempBase2 - TempSize2);\r
923 }\r
924\r
925 //\r
926 // Copy Hole Range data.\r
927 //\r
928 for (Index = 0; Index < HOLE_MAX_NUMBER; Index ++) {\r
929 if (Private->HoleData[Index].Size > 0) {\r
930 if (HoleMemBase > Private->HoleData[Index].Base) {\r
931 Private->HoleData[Index].OffsetPositive = TRUE;\r
932 Private->HoleData[Index].Offset = (UINTN) (HoleMemBase - Private->HoleData[Index].Base);\r
933 } else {\r
934 Private->HoleData[Index].OffsetPositive = FALSE;\r
935 Private->HoleData[Index].Offset = (UINTN) (Private->HoleData[Index].Base - HoleMemBase);\r
936 }\r
937 CopyMem ((VOID *) (UINTN) HoleMemBase, (VOID *) (UINTN) Private->HoleData[Index].Base, Private->HoleData[Index].Size);\r
938 HoleMemBase = HoleMemBase + Private->HoleData[Index].Size;\r
939 }\r
940 }\r
941 }\r
942\r
943 //\r
944 // Switch new stack\r
945 //\r
946 SwitchStack (\r
947 (SWITCH_STACK_ENTRY_POINT)(UINTN)PeiCoreEntry,\r
948 (VOID *) SecCoreData,\r
949 (VOID *) Private,\r
950 (VOID *) (UINTN) TopOfNewStack\r
951 );\r
952 }\r
953\r
954 //\r
955 // Code should not come here\r
956 //\r
957 ASSERT (FALSE);\r
958 }\r
959}\r
960\r
b1f6a7c6 961/**\r
192f6d4c 962 Conduct PEIM dispatch.\r
963\r
b1f6a7c6 964 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 965 environment, such as the size and location of temporary RAM, the stack location and\r
966 the BFV location.\r
b1f6a7c6 967 @param Private Pointer to the private data passed in from caller\r
192f6d4c 968\r
b1f6a7c6 969**/\r
970VOID\r
971PeiDispatcher (\r
972 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
973 IN PEI_CORE_INSTANCE *Private\r
974 )\r
192f6d4c 975{\r
b0d803fe 976 EFI_STATUS Status;\r
977 UINT32 Index1;\r
978 UINT32 Index2;\r
6c7a807a 979 CONST EFI_PEI_SERVICES **PeiServices;\r
b0d803fe 980 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
981 UINTN FvCount;\r
982 UINTN PeimCount;\r
983 UINT32 AuthenticationState;\r
984 EFI_PHYSICAL_ADDRESS EntryPoint;\r
797a9d67 985 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
b0d803fe 986 UINTN SaveCurrentPeimCount;\r
1053e0c5 987 UINTN SaveCurrentFvCount;\r
b0d803fe 988 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
288f9b38 989 EFI_FV_FILE_INFO FvFileInfo;\r
3b428ade 990 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
d1102dba 991\r
4140a663 992 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
b0d803fe 993 PeimEntryPoint = NULL;\r
994 PeimFileHandle = NULL;\r
288f9b38 995 EntryPoint = 0;\r
b0d803fe 996\r
5d7f3126 997 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
b0d803fe 998 //\r
999 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
c2c4199b 1000 // update the modules' status from PEIM_STATE_REGISTER_FOR_SHADOW to PEIM_STATE_DONE.\r
b0d803fe 1001 //\r
1002 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
1053e0c5 1003 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
b0d803fe 1004 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
1005\r
b22d0931 1006 for (Index1 = 0; Index1 < Private->FvCount; Index1++) {\r
b62fe570 1007 for (Index2 = 0; Index2 < Private->Fv[Index1].PeimCount; Index2++) {\r
c2c4199b 1008 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
58dcdada 1009 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
3d44658c
LG
1010 Private->CurrentFileHandle = PeimFileHandle;\r
1011 Private->CurrentPeimFvCount = Index1;\r
1012 Private->CurrentPeimCount = Index2;\r
b0d803fe 1013 Status = PeiLoadImage (\r
4140a663 1014 (CONST EFI_PEI_SERVICES **) &Private->Ps,\r
58dcdada 1015 PeimFileHandle,\r
c2c4199b 1016 PEIM_STATE_REGISTER_FOR_SHADOW,\r
58dcdada 1017 &EntryPoint,\r
b0d803fe 1018 &AuthenticationState\r
1019 );\r
1020 if (Status == EFI_SUCCESS) {\r
1021 //\r
c2c4199b 1022 // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
b0d803fe 1023 //\r
1024 Private->Fv[Index1].PeimState[Index2]++;\r
b0d803fe 1025 //\r
1026 // Call the PEIM entry point\r
1027 //\r
797a9d67 1028 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
58dcdada 1029\r
67e9ab84 1030 PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
4140a663 1031 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps);\r
67e9ab84 1032 PERF_START_IMAGE_END (PeimFileHandle);\r
58dcdada 1033 }\r
1034\r
b0d803fe 1035 //\r
1036 // Process the Notify list and dispatch any notifies for\r
1037 // newly installed PPIs.\r
1038 //\r
f2bc359c 1039 ProcessDispatchNotifyList (Private);\r
b0d803fe 1040 }\r
1041 }\r
1042 }\r
58dcdada 1043 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
1044 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
1045 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
b0d803fe 1046 }\r
192f6d4c 1047\r
1048 //\r
1049 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
1050 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
1051 // pass of the dispatcher, it will start over from the Bfv again to see\r
1052 // if any new PEIMs dependencies got satisfied. With a well ordered\r
1053 // FV where PEIMs are found in the order their dependencies are also\r
1054 // satisfied, this dipatcher should run only once.\r
1055 //\r
b0d803fe 1056 do {\r
82b8c8df 1057 //\r
d1102dba 1058 // In case that reenter PeiCore happens, the last pass record is still available.\r
82b8c8df 1059 //\r
1060 if (!Private->PeimDispatcherReenter) {\r
1061 Private->PeimNeedingDispatch = FALSE;\r
1062 Private->PeimDispatchOnThisPass = FALSE;\r
1063 } else {\r
1064 Private->PeimDispatcherReenter = FALSE;\r
1065 }\r
d1102dba 1066\r
b0d803fe 1067 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
3b428ade 1068 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
1069 ASSERT (CoreFvHandle != NULL);\r
d1102dba 1070\r
2a00326e 1071 //\r
3b428ade 1072 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
2a00326e 1073 //\r
3b428ade 1074 if (CoreFvHandle->FvPpi == NULL) {\r
1075 continue;\r
1076 }\r
d1102dba 1077\r
3b428ade 1078 Private->CurrentPeimFvCount = FvCount;\r
192f6d4c 1079\r
b0d803fe 1080 if (Private->CurrentPeimCount == 0) {\r
1081 //\r
1082 // When going through each FV, at first, search Apriori file to\r
58dcdada 1083 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
b0d803fe 1084 // dispatch at first.\r
1085 //\r
3b428ade 1086 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);\r
b0d803fe 1087 }\r
192f6d4c 1088\r
1089 //\r
b0d803fe 1090 // Start to dispatch all modules within the current Fv.\r
192f6d4c 1091 //\r
58dcdada 1092 for (PeimCount = Private->CurrentPeimCount;\r
b62fe570 1093 PeimCount < Private->Fv[FvCount].PeimCount;\r
b0d803fe 1094 PeimCount++) {\r
1095 Private->CurrentPeimCount = PeimCount;\r
1096 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
1097\r
1098 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
1099 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
82b8c8df 1100 Private->PeimNeedingDispatch = TRUE;\r
b0d803fe 1101 } else {\r
3b428ade 1102 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);\r
288f9b38
LG
1103 ASSERT_EFI_ERROR (Status);\r
1104 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
1105 //\r
116cd856 1106 // For Fv type file, Produce new FvInfo PPI and FV hob\r
288f9b38 1107 //\r
c7935105
SZ
1108 Status = ProcessFvFile (Private, &Private->Fv[FvCount], PeimFileHandle);\r
1109 if (Status == EFI_SUCCESS) {\r
1110 //\r
1111 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
1112 //\r
1113 Private->Fv[FvCount].PeimState[PeimCount]++;\r
1114 Private->PeimDispatchOnThisPass = TRUE;\r
116cd856
SZ
1115 } else {\r
1116 //\r
1117 // The related GuidedSectionExtraction/Decompress PPI for the\r
1118 // encapsulated FV image section may be installed in the rest\r
1119 // of this do-while loop, so need to make another pass.\r
1120 //\r
1121 Private->PeimNeedingDispatch = TRUE;\r
c7935105 1122 }\r
288f9b38
LG
1123 } else {\r
1124 //\r
1125 // For PEIM driver, Load its entry point\r
1126 //\r
1127 Status = PeiLoadImage (\r
58dcdada 1128 PeiServices,\r
1129 PeimFileHandle,\r
341a658f 1130 PEIM_STATE_NOT_DISPATCHED,\r
58dcdada 1131 &EntryPoint,\r
288f9b38
LG
1132 &AuthenticationState\r
1133 );\r
c7935105 1134 if (Status == EFI_SUCCESS) {\r
b0d803fe 1135 //\r
c7935105
SZ
1136 // The PEIM has its dependencies satisfied, and its entry point\r
1137 // has been found, so invoke it.\r
b0d803fe 1138 //\r
67e9ab84 1139 PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
58dcdada 1140\r
c7935105
SZ
1141 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
1142 EFI_PROGRESS_CODE,\r
1143 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
fa3d30ea
LG
1144 (VOID *)(&PeimFileHandle),\r
1145 sizeof (PeimFileHandle)\r
c7935105
SZ
1146 );\r
1147\r
1148 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle, AuthenticationState);\r
1149 if (Status != EFI_SECURITY_VIOLATION) {\r
1150 //\r
1151 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
1152 //\r
1153 Private->Fv[FvCount].PeimState[PeimCount]++;\r
288f9b38
LG
1154 //\r
1155 // Call the PEIM entry point for PEIM driver\r
1156 //\r
797a9d67 1157 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1158 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
c7935105 1159 Private->PeimDispatchOnThisPass = TRUE;\r
9f671b47
LG
1160 } else {\r
1161 //\r
1162 // The related GuidedSectionExtraction PPI for the\r
1163 // signed PEIM image section may be installed in the rest\r
1164 // of this do-while loop, so need to make another pass.\r
1165 //\r
1166 Private->PeimNeedingDispatch = TRUE;\r
288f9b38 1167 }\r
797a9d67 1168\r
c7935105
SZ
1169 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
1170 EFI_PROGRESS_CODE,\r
1171 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END),\r
fa3d30ea
LG
1172 (VOID *)(&PeimFileHandle),\r
1173 sizeof (PeimFileHandle)\r
c7935105 1174 );\r
67e9ab84 1175 PERF_START_IMAGE_END (PeimFileHandle);\r
b0d803fe 1176\r
c7935105 1177 }\r
58dcdada 1178 }\r
1179\r
bfb685da 1180 PeiCheckAndSwitchStack (SecCoreData, Private);\r
192f6d4c 1181\r
58dcdada 1182 //\r
1183 // Process the Notify list and dispatch any notifies for\r
1184 // newly installed PPIs.\r
1185 //\r
f2bc359c 1186 ProcessDispatchNotifyList (Private);\r
58dcdada 1187\r
bfb685da 1188 //\r
f2bc359c 1189 // Recheck SwitchStackSignal after ProcessDispatchNotifyList()\r
bfb685da
SZ
1190 // in case PeiInstallPeiMemory() is done in a callback with\r
1191 // EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH.\r
1192 //\r
1193 PeiCheckAndSwitchStack (SecCoreData, Private);\r
1194\r
c2c4199b 1195 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW) && \\r
5d7f3126 1196 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
b0d803fe 1197 //\r
6393d9c8 1198 // If memory is available we shadow images by default for performance reasons.\r
58dcdada 1199 // We call the entry point a 2nd time so the module knows it's shadowed.\r
b0d803fe 1200 //\r
1201 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
3d44658c
LG
1202 if ((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) && !PcdGetBool (PcdShadowPeimOnBoot)) {\r
1203 //\r
1204 // Load PEIM into Memory for Register for shadow PEIM.\r
1205 //\r
1206 Status = PeiLoadImage (\r
1207 PeiServices,\r
1208 PeimFileHandle,\r
c2c4199b 1209 PEIM_STATE_REGISTER_FOR_SHADOW,\r
3d44658c
LG
1210 &EntryPoint,\r
1211 &AuthenticationState\r
1212 );\r
1213 if (Status == EFI_SUCCESS) {\r
1214 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1215 }\r
1216 }\r
e67ca95c 1217 ASSERT (PeimEntryPoint != NULL);\r
797a9d67 1218 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
b0d803fe 1219 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
58dcdada 1220\r
b0d803fe 1221 //\r
c2c4199b 1222 // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
b0d803fe 1223 //\r
1224 Private->Fv[FvCount].PeimState[PeimCount]++;\r
192f6d4c 1225\r
192f6d4c 1226 //\r
b0d803fe 1227 // Process the Notify list and dispatch any notifies for\r
1228 // newly installed PPIs.\r
192f6d4c 1229 //\r
f2bc359c 1230 ProcessDispatchNotifyList (Private);\r
192f6d4c 1231 }\r
1232 }\r
1233 }\r
192f6d4c 1234 }\r
192f6d4c 1235\r
b0d803fe 1236 //\r
b62fe570
SZ
1237 // Before walking through the next FV, we should set them to NULL/0 to\r
1238 // start at the begining of the next FV.\r
b0d803fe 1239 //\r
1240 Private->CurrentFileHandle = NULL;\r
1241 Private->CurrentPeimCount = 0;\r
b62fe570 1242 Private->CurrentFvFileHandles = NULL;\r
192f6d4c 1243 }\r
1244\r
1245 //\r
b62fe570
SZ
1246 // Before making another pass, we should set it to 0 to\r
1247 // go through all the FVs.\r
192f6d4c 1248 //\r
b0d803fe 1249 Private->CurrentPeimFvCount = 0;\r
192f6d4c 1250\r
1251 //\r
116cd856 1252 // PeimNeedingDispatch being TRUE means we found a PEIM/FV that did not get\r
b0d803fe 1253 // dispatched. So we need to make another pass\r
192f6d4c 1254 //\r
116cd856
SZ
1255 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM/FV on this\r
1256 // pass. If we did not dispatch a PEIM/FV there is no point in trying again\r
b0d803fe 1257 // as it will fail the next time too (nothing has changed).\r
192f6d4c 1258 //\r
82b8c8df 1259 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
192f6d4c 1260\r
192f6d4c 1261}\r
1262\r
b1f6a7c6 1263/**\r
192f6d4c 1264 Initialize the Dispatcher's data members\r
1265\r
b1f6a7c6 1266 @param PrivateData PeiCore's private data structure\r
1267 @param OldCoreData Old data from SecCore\r
192f6d4c 1268 NULL if being run in non-permament memory mode.\r
b1f6a7c6 1269 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 1270 environment, such as the size and location of temporary RAM, the stack location and\r
1271 the BFV location.\r
192f6d4c 1272\r
b1f6a7c6 1273 @return None.\r
192f6d4c 1274\r
b1f6a7c6 1275**/\r
1276VOID\r
1277InitializeDispatcherData (\r
1278 IN PEI_CORE_INSTANCE *PrivateData,\r
1279 IN PEI_CORE_INSTANCE *OldCoreData,\r
1280 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
1281 )\r
192f6d4c 1282{\r
192f6d4c 1283 if (OldCoreData == NULL) {\r
82b8c8df 1284 PrivateData->PeimDispatcherReenter = FALSE;\r
b0d803fe 1285 PeiInitializeFv (PrivateData, SecCoreData);\r
8e0e40ed 1286 } else {\r
7ec93917 1287 PeiReinitializeFv (PrivateData);\r
192f6d4c 1288 }\r
1289\r
1290 return;\r
1291}\r
1292\r
b1f6a7c6 1293/**\r
1294 This routine parses the Dependency Expression, if available, and\r
1295 decides if the module can be executed.\r
1296\r
1297\r
1298 @param Private PeiCore's private data structure\r
1299 @param FileHandle PEIM's file handle\r
1300 @param PeimCount Peim count in all dispatched PEIMs.\r
192f6d4c 1301\r
b1f6a7c6 1302 @retval TRUE Can be dispatched\r
1303 @retval FALSE Cannot be dispatched\r
1304\r
1305**/\r
192f6d4c 1306BOOLEAN\r
1307DepexSatisfied (\r
b0d803fe 1308 IN PEI_CORE_INSTANCE *Private,\r
1309 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1310 IN UINTN PeimCount\r
192f6d4c 1311 )\r
192f6d4c 1312{\r
288f9b38
LG
1313 EFI_STATUS Status;\r
1314 VOID *DepexData;\r
6a55eea3 1315 EFI_FV_FILE_INFO FileInfo;\r
b0d803fe 1316\r
6a55eea3 1317 Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r
1318 if (EFI_ERROR (Status)) {\r
1319 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(Unknown)\n"));\r
1320 } else {\r
1321 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName));\r
1322 }\r
d1102dba 1323\r
b0d803fe 1324 if (PeimCount < Private->AprioriCount) {\r
1325 //\r
b62fe570 1326 // If it's in the Apriori file then we set Depex to TRUE\r
b0d803fe 1327 //\r
6a55eea3 1328 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
b0d803fe 1329 return TRUE;\r
1330 }\r
58dcdada 1331\r
288f9b38 1332 //\r
58dcdada 1333 // Depex section not in the encapsulated section.\r
288f9b38
LG
1334 //\r
1335 Status = PeiServicesFfsFindSectionData (\r
1336 EFI_SECTION_PEI_DEPEX,\r
58dcdada 1337 FileHandle,\r
288f9b38
LG
1338 (VOID **)&DepexData\r
1339 );\r
b0d803fe 1340\r
192f6d4c 1341 if (EFI_ERROR (Status)) {\r
b0d803fe 1342 //\r
1343 // If there is no DEPEX, assume the module can be executed\r
1344 //\r
6a55eea3 1345 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n"));\r
192f6d4c 1346 return TRUE;\r
1347 }\r
1348\r
1349 //\r
1350 // Evaluate a given DEPEX\r
1351 //\r
4140a663 1352 return PeimDispatchReadiness (&Private->Ps, DepexData);\r
192f6d4c 1353}\r
1354\r
14e8823a 1355/**\r
1356 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
1357 discovery permanent memory.\r
1358\r
b1f6a7c6 1359 @param FileHandle File handle of a PEIM.\r
58dcdada 1360\r
b1f6a7c6 1361 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
1362 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
1363 @retval EFI_SUCCESS Successfully to register itself.\r
14e8823a 1364\r
58dcdada 1365**/\r
14e8823a 1366EFI_STATUS\r
1367EFIAPI\r
1368PeiRegisterForShadow (\r
1369 IN EFI_PEI_FILE_HANDLE FileHandle\r
1370 )\r
1371{\r
1372 PEI_CORE_INSTANCE *Private;\r
1373 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
1374\r
1375 if (Private->CurrentFileHandle != FileHandle) {\r
1376 //\r
1377 // The FileHandle must be for the current PEIM\r
1378 //\r
1379 return EFI_NOT_FOUND;\r
1380 }\r
1381\r
c2c4199b 1382 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISTER_FOR_SHADOW) {\r
14e8823a 1383 //\r
1384 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
1385 //\r
1386 return EFI_ALREADY_STARTED;\r
1387 }\r
58dcdada 1388\r
c2c4199b 1389 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISTER_FOR_SHADOW;\r
14e8823a 1390\r
1391 return EFI_SUCCESS;\r
1392}\r
1393\r
3b428ade 1394\r
341a658f 1395\r