]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
MdeModulePkg PeiCore: Remove the using of PcdPeiCoreMaxFvSupported
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
... / ...
CommitLineData
1/** @file\r
2 EFI PEI Core dispatch services\r
3\r
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
6This program and the accompanying materials\r
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
14**/\r
15\r
16#include "PeiMain.h"\r
17\r
18/**\r
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
23\r
24 @param Private Pointer to the private data passed in from caller\r
25 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.\r
26\r
27**/\r
28VOID\r
29DiscoverPeimsAndOrderWithApriori (\r
30 IN PEI_CORE_INSTANCE *Private,\r
31 IN PEI_CORE_FV_HANDLE *CoreFileHandle\r
32 )\r
33{\r
34 EFI_STATUS Status;\r
35 EFI_PEI_FILE_HANDLE FileHandle;\r
36 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
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
43 EFI_PEI_FILE_HANDLE *TempFileHandles;\r
44 EFI_GUID *TempFileGuid;\r
45 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
46 EFI_FV_FILE_INFO FileInfo;\r
47\r
48 FvPpi = CoreFileHandle->FvPpi;\r
49\r
50 //\r
51 // Walk the FV and find all the PEIMs and the Apriori file.\r
52 //\r
53 AprioriFileHandle = NULL;\r
54 Private->CurrentFvFileHandles = NULL;\r
55 Guid = NULL;\r
56\r
57 //\r
58 // If the current Fv has been scanned, directly get its cached records.\r
59 //\r
60 if (CoreFileHandle->ScanFv) {\r
61 Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
62 return;\r
63 }\r
64\r
65 TempFileHandles = Private->TempFileHandles;\r
66 TempFileGuid = Private->TempFileGuid;\r
67\r
68 //\r
69 // Go ahead to scan this Fv, get PeimCount and cache FileHandles within it to TempFileHandles.\r
70 //\r
71 PeimCount = 0;\r
72 FileHandle = NULL;\r
73 do {\r
74 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
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
102 }\r
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
112\r
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
119 }\r
120\r
121 //\r
122 // Record PeimCount, allocate buffer for PeimState and FvFileHandles.\r
123 //\r
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
129\r
130 //\r
131 // Get Apriori File handle\r
132 //\r
133 Private->AprioriCount = 0;\r
134 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);\r
135 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {\r
136 //\r
137 // Read the Apriori file\r
138 //\r
139 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
140 if (!EFI_ERROR (Status)) {\r
141 //\r
142 // Calculate the number of PEIMs in the Apriori file\r
143 //\r
144 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
145 ASSERT_EFI_ERROR (Status);\r
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
152 Private->AprioriCount /= sizeof (EFI_GUID);\r
153\r
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
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
162 }\r
163\r
164 //\r
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
167 //\r
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
174\r
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
180 }\r
181\r
182 //\r
183 // Update valid AprioriCount\r
184 //\r
185 Private->AprioriCount = Index;\r
186\r
187 //\r
188 // Add in any PEIMs not in the Apriori file\r
189 //\r
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
194 }\r
195 }\r
196 ASSERT (Index == PeimCount);\r
197 }\r
198 } else {\r
199 CopyMem (CoreFileHandle->FvFileHandles, TempFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
200 }\r
201\r
202 //\r
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
205 //\r
206 CoreFileHandle->ScanFv = TRUE;\r
207 Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
208}\r
209\r
210//\r
211// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
212// This part of memory still need reserved on the very top of memory so that the DXE Core could\r
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
217/**\r
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
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
233 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
234 BOOLEAN IsAvailable;\r
235 EFI_PEI_HOB_POINTERS Hob;\r
236\r
237 IsAvailable = TRUE;\r
238 if (PrivateData == NULL || ResourceHob == NULL) {\r
239 return FALSE;\r
240 }\r
241 //\r
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
245 //\r
246 // See if this is a memory allocation HOB\r
247 //\r
248 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
249 MemoryHob = Hob.MemoryAllocation;\r
250 if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart &&\r
251 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) {\r
252 IsAvailable = FALSE;\r
253 break;\r
254 }\r
255 }\r
256 }\r
257\r
258 return IsAvailable;\r
259\r
260}\r
261/**\r
262 Hook function for Loading Module at Fixed Address feature\r
263\r
264 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is\r
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
268\r
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
281 EFI_PHYSICAL_ADDRESS HighAddress;\r
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
288 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
289 //\r
290 // Initialize Local Variables\r
291 //\r
292 CurrentResourceHob = NULL;\r
293 ResourceHob = NULL;\r
294 NextResourceHob = NULL;\r
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
303 //\r
304 TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber));\r
305 TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ;\r
306 //\r
307 // PEI memory range lies below the top reserved memory\r
308 //\r
309 TotalReservedMemorySize += PeiMemorySize;\r
310\r
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
313 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber)));\r
314 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize));\r
315 //\r
316 // Loop through the system memory typed hob to merge the adjacent memory range\r
317 //\r
318 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
319 //\r
320 // See if this is a resource descriptor HOB\r
321 //\r
322 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
323\r
324 ResourceHob = Hob.ResourceDescriptor;\r
325 //\r
326 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
327 //\r
328 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY ||\r
329 ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) {\r
330 continue;\r
331 }\r
332\r
333 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
334 if (NextHob.Raw == Hob.Raw){\r
335 continue;\r
336 }\r
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
341\r
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
346 //\r
347 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
348 (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){\r
349\r
350 //\r
351 // See if the memory range described in ResourceHob and NextResourceHob is adjacent\r
352 //\r
353 if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart &&\r
354 ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)||\r
355 (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&\r
356 ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {\r
357\r
358 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?\r
359 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);\r
360\r
361 ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ?\r
362 ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;\r
363\r
364\r
365 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);\r
366\r
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
372\r
373 }\r
374 }\r
375 }\r
376 }\r
377 }\r
378 }\r
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
384 //\r
385 // See if this is a memory allocation HOB\r
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
394 NextResourceHob = NextHob.ResourceDescriptor;\r
395 //\r
396 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
397 //\r
398 if (NextResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY || NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) {\r
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
403 //\r
404 if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart &&\r
405 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
406 //\r
407 // Build seperate resource hob for this allocated range\r
408 //\r
409 if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) {\r
410 BuildResourceDescriptorHob (\r
411 EFI_RESOURCE_SYSTEM_MEMORY,\r
412 NextResourceHob->ResourceAttribute,\r
413 NextResourceHob->PhysicalStart,\r
414 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)\r
415 );\r
416 }\r
417 if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
418 BuildResourceDescriptorHob (\r
419 EFI_RESOURCE_SYSTEM_MEMORY,\r
420 NextResourceHob->ResourceAttribute,\r
421 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,\r
422 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))\r
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
434 //\r
435 // Try to find and validate the TOP address.\r
436 //\r
437 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {\r
438 //\r
439 // The LMFA feature is enabled as load module at fixed absolute address.\r
440 //\r
441 TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)PcdGet64(PcdLoadModuleAtFixAddressEnable);\r
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
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
449 }\r
450 //\r
451 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies\r
452 //\r
453 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
454 //\r
455 // See if this is a resource descriptor HOB\r
456 //\r
457 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
458\r
459 ResourceHob = Hob.ResourceDescriptor;\r
460 //\r
461 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
462 //\r
463 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
464 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
465 //\r
466 // See if Top address specified by user is valid.\r
467 //\r
468 if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress &&\r
469 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress &&\r
470 PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
471 CurrentResourceHob = ResourceHob;\r
472 CurrentHob = Hob;\r
473 break;\r
474 }\r
475 }\r
476 }\r
477 }\r
478 if (CurrentResourceHob != NULL) {\r
479 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress));\r
480 TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE;\r
481 } else {\r
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
484 //\r
485 // Print the recomended Top address range.\r
486 //\r
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
492\r
493 ResourceHob = Hob.ResourceDescriptor;\r
494 //\r
495 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
496 //\r
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
502 if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
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
507 }\r
508 }\r
509 }\r
510 }\r
511 //\r
512 // Assert here\r
513 //\r
514 ASSERT (FALSE);\r
515 return;\r
516 }\r
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
527 // See if this is a resource descriptor HOB\r
528 //\r
529 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
530\r
531 ResourceHob = Hob.ResourceDescriptor;\r
532 //\r
533 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
534 //\r
535 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
536 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
537 ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
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
547 }\r
548 }\r
549 if (CurrentResourceHob == NULL) {\r
550 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n"));\r
551 //\r
552 // Assert here\r
553 //\r
554 ASSERT (FALSE);\r
555 return;\r
556 } else {\r
557 TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ;\r
558 }\r
559 }\r
560\r
561 if (CurrentResourceHob != NULL) {\r
562 //\r
563 // rebuild resource HOB for PEI memmory and reserved memory\r
564 //\r
565 BuildResourceDescriptorHob (\r
566 EFI_RESOURCE_SYSTEM_MEMORY,\r
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
576 (TopLoadingAddress - TotalReservedMemorySize),\r
577 TotalReservedMemorySize\r
578 );\r
579 //\r
580 // rebuild resource for the remain memory if necessary\r
581 //\r
582 if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
583 BuildResourceDescriptorHob (\r
584 EFI_RESOURCE_SYSTEM_MEMORY,\r
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
593 CurrentResourceHob->PhysicalStart,\r
594 (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)\r
595 );\r
596 }\r
597 if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) {\r
598 BuildResourceDescriptorHob (\r
599 EFI_RESOURCE_SYSTEM_MEMORY,\r
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
608 TopLoadingAddress,\r
609 (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress)\r
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
615 GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED;\r
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
622 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData->LoadModuleAtFixAddressTopAddress));\r
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
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
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
696 // Before switch stack from temporary memory to permanent memory, calculate the heap and stack\r
697 // usage in temporary memory for debugging.\r
698 //\r
699 DEBUG_CODE_BEGIN ();\r
700 UINT32 *StackPointer;\r
701 EFI_PEI_HOB_POINTERS Hob;\r
702\r
703 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
704 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
705 && (*StackPointer == PcdGet32 (PcdInitValueInTempStack));\r
706 StackPointer ++) {\r
707 }\r
708\r
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
713 (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
714 ));\r
715 DEBUG ((DEBUG_INFO, " temporary memory heap used for HobList: %d bytes.\n",\r
716 (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)\r
717 ));\r
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
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
747 // The size of new stack in permanent memory must be the same size\r
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
750 // the size of old stack at bottom of permanent memory.\r
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
759 // Calculate stack offset and heap offset between temporary memory and new permement\r
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
826 // Temporary Ram Support PPI is provided by platform, it will copy\r
827 // temporary memory to permanent memory and do stack switching.\r
828 // After invoking Temporary Ram Support PPI, the following code's\r
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
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
845 //\r
846 // Entry PEI Phase 2\r
847 //\r
848 PeiCore (SecCoreData, NULL, Private);\r
849 } else {\r
850 //\r
851 // Migrate memory pages allocated in pre-memory phase.\r
852 //\r
853 MigrateMemoryPages (Private, FALSE);\r
854\r
855 //\r
856 // Migrate the PEI Services Table pointer from temporary RAM to permanent RAM.\r
857 //\r
858 MigratePeiServicesTablePointer ();\r
859\r
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
887 CopyMem ((UINT8 *) (UINTN) BaseOfNewHeap, PeiTemporaryRamBase, HeapTemporaryRamSize);\r
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
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
961/**\r
962 Conduct PEIM dispatch.\r
963\r
964 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
965 environment, such as the size and location of temporary RAM, the stack location and\r
966 the BFV location.\r
967 @param Private Pointer to the private data passed in from caller\r
968\r
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
975{\r
976 EFI_STATUS Status;\r
977 UINT32 Index1;\r
978 UINT32 Index2;\r
979 CONST EFI_PEI_SERVICES **PeiServices;\r
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
985 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
986 UINTN SaveCurrentPeimCount;\r
987 UINTN SaveCurrentFvCount;\r
988 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
989 EFI_FV_FILE_INFO FvFileInfo;\r
990 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
991\r
992 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
993 PeimEntryPoint = NULL;\r
994 PeimFileHandle = NULL;\r
995 EntryPoint = 0;\r
996\r
997 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
998 //\r
999 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
1000 // update the modules' status from PEIM_STATE_REGISTER_FOR_SHADOW to PEIM_STATE_DONE.\r
1001 //\r
1002 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
1003 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
1004 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
1005\r
1006 for (Index1 = 0; Index1 < Private->FvCount; Index1++) {\r
1007 for (Index2 = 0; Index2 < Private->Fv[Index1].PeimCount; Index2++) {\r
1008 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
1009 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
1010 Private->CurrentFileHandle = PeimFileHandle;\r
1011 Private->CurrentPeimFvCount = Index1;\r
1012 Private->CurrentPeimCount = Index2;\r
1013 Status = PeiLoadImage (\r
1014 (CONST EFI_PEI_SERVICES **) &Private->Ps,\r
1015 PeimFileHandle,\r
1016 PEIM_STATE_REGISTER_FOR_SHADOW,\r
1017 &EntryPoint,\r
1018 &AuthenticationState\r
1019 );\r
1020 if (Status == EFI_SUCCESS) {\r
1021 //\r
1022 // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
1023 //\r
1024 Private->Fv[Index1].PeimState[Index2]++;\r
1025 //\r
1026 // Call the PEIM entry point\r
1027 //\r
1028 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1029\r
1030 PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
1031 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps);\r
1032 PERF_START_IMAGE_END (PeimFileHandle);\r
1033 }\r
1034\r
1035 //\r
1036 // Process the Notify list and dispatch any notifies for\r
1037 // newly installed PPIs.\r
1038 //\r
1039 ProcessNotifyList (Private);\r
1040 }\r
1041 }\r
1042 }\r
1043 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
1044 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
1045 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
1046 }\r
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
1056 do {\r
1057 //\r
1058 // In case that reenter PeiCore happens, the last pass record is still available.\r
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
1066\r
1067 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
1068 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
1069 ASSERT (CoreFvHandle != NULL);\r
1070\r
1071 //\r
1072 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
1073 //\r
1074 if (CoreFvHandle->FvPpi == NULL) {\r
1075 continue;\r
1076 }\r
1077\r
1078 Private->CurrentPeimFvCount = FvCount;\r
1079\r
1080 if (Private->CurrentPeimCount == 0) {\r
1081 //\r
1082 // When going through each FV, at first, search Apriori file to\r
1083 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
1084 // dispatch at first.\r
1085 //\r
1086 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);\r
1087 }\r
1088\r
1089 //\r
1090 // Start to dispatch all modules within the current Fv.\r
1091 //\r
1092 for (PeimCount = Private->CurrentPeimCount;\r
1093 PeimCount < Private->Fv[FvCount].PeimCount;\r
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
1100 Private->PeimNeedingDispatch = TRUE;\r
1101 } else {\r
1102 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);\r
1103 ASSERT_EFI_ERROR (Status);\r
1104 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
1105 //\r
1106 // For Fv type file, Produce new FvInfo PPI and FV hob\r
1107 //\r
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
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
1122 }\r
1123 } else {\r
1124 //\r
1125 // For PEIM driver, Load its entry point\r
1126 //\r
1127 Status = PeiLoadImage (\r
1128 PeiServices,\r
1129 PeimFileHandle,\r
1130 PEIM_STATE_NOT_DISPATCHED,\r
1131 &EntryPoint,\r
1132 &AuthenticationState\r
1133 );\r
1134 if (Status == EFI_SUCCESS) {\r
1135 //\r
1136 // The PEIM has its dependencies satisfied, and its entry point\r
1137 // has been found, so invoke it.\r
1138 //\r
1139 PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
1140\r
1141 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
1142 EFI_PROGRESS_CODE,\r
1143 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
1144 (VOID *)(&PeimFileHandle),\r
1145 sizeof (PeimFileHandle)\r
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
1154 //\r
1155 // Call the PEIM entry point for PEIM driver\r
1156 //\r
1157 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1158 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
1159 Private->PeimDispatchOnThisPass = TRUE;\r
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
1167 }\r
1168\r
1169 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
1170 EFI_PROGRESS_CODE,\r
1171 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END),\r
1172 (VOID *)(&PeimFileHandle),\r
1173 sizeof (PeimFileHandle)\r
1174 );\r
1175 PERF_START_IMAGE_END (PeimFileHandle);\r
1176\r
1177 }\r
1178 }\r
1179\r
1180 PeiCheckAndSwitchStack (SecCoreData, Private);\r
1181\r
1182 //\r
1183 // Process the Notify list and dispatch any notifies for\r
1184 // newly installed PPIs.\r
1185 //\r
1186 ProcessNotifyList (Private);\r
1187\r
1188 //\r
1189 // Recheck SwitchStackSignal after ProcessNotifyList()\r
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
1195 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW) && \\r
1196 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
1197 //\r
1198 // If memory is available we shadow images by default for performance reasons.\r
1199 // We call the entry point a 2nd time so the module knows it's shadowed.\r
1200 //\r
1201 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
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
1209 PEIM_STATE_REGISTER_FOR_SHADOW,\r
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
1217 ASSERT (PeimEntryPoint != NULL);\r
1218 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
1219 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
1220\r
1221 //\r
1222 // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
1223 //\r
1224 Private->Fv[FvCount].PeimState[PeimCount]++;\r
1225\r
1226 //\r
1227 // Process the Notify list and dispatch any notifies for\r
1228 // newly installed PPIs.\r
1229 //\r
1230 ProcessNotifyList (Private);\r
1231 }\r
1232 }\r
1233 }\r
1234 }\r
1235\r
1236 //\r
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
1239 //\r
1240 Private->CurrentFileHandle = NULL;\r
1241 Private->CurrentPeimCount = 0;\r
1242 Private->CurrentFvFileHandles = NULL;\r
1243 }\r
1244\r
1245 //\r
1246 // Before making another pass, we should set it to 0 to\r
1247 // go through all the FVs.\r
1248 //\r
1249 Private->CurrentPeimFvCount = 0;\r
1250\r
1251 //\r
1252 // PeimNeedingDispatch being TRUE means we found a PEIM/FV that did not get\r
1253 // dispatched. So we need to make another pass\r
1254 //\r
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
1257 // as it will fail the next time too (nothing has changed).\r
1258 //\r
1259 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
1260\r
1261}\r
1262\r
1263/**\r
1264 Initialize the Dispatcher's data members\r
1265\r
1266 @param PrivateData PeiCore's private data structure\r
1267 @param OldCoreData Old data from SecCore\r
1268 NULL if being run in non-permament memory mode.\r
1269 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
1270 environment, such as the size and location of temporary RAM, the stack location and\r
1271 the BFV location.\r
1272\r
1273 @return None.\r
1274\r
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
1282{\r
1283 if (OldCoreData == NULL) {\r
1284 PrivateData->PeimDispatcherReenter = FALSE;\r
1285 PeiInitializeFv (PrivateData, SecCoreData);\r
1286 } else {\r
1287 PeiReinitializeFv (PrivateData);\r
1288 }\r
1289\r
1290 return;\r
1291}\r
1292\r
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
1301\r
1302 @retval TRUE Can be dispatched\r
1303 @retval FALSE Cannot be dispatched\r
1304\r
1305**/\r
1306BOOLEAN\r
1307DepexSatisfied (\r
1308 IN PEI_CORE_INSTANCE *Private,\r
1309 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1310 IN UINTN PeimCount\r
1311 )\r
1312{\r
1313 EFI_STATUS Status;\r
1314 VOID *DepexData;\r
1315 EFI_FV_FILE_INFO FileInfo;\r
1316\r
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
1323\r
1324 if (PeimCount < Private->AprioriCount) {\r
1325 //\r
1326 // If it's in the Apriori file then we set Depex to TRUE\r
1327 //\r
1328 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
1329 return TRUE;\r
1330 }\r
1331\r
1332 //\r
1333 // Depex section not in the encapsulated section.\r
1334 //\r
1335 Status = PeiServicesFfsFindSectionData (\r
1336 EFI_SECTION_PEI_DEPEX,\r
1337 FileHandle,\r
1338 (VOID **)&DepexData\r
1339 );\r
1340\r
1341 if (EFI_ERROR (Status)) {\r
1342 //\r
1343 // If there is no DEPEX, assume the module can be executed\r
1344 //\r
1345 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n"));\r
1346 return TRUE;\r
1347 }\r
1348\r
1349 //\r
1350 // Evaluate a given DEPEX\r
1351 //\r
1352 return PeimDispatchReadiness (&Private->Ps, DepexData);\r
1353}\r
1354\r
1355/**\r
1356 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
1357 discovery permanent memory.\r
1358\r
1359 @param FileHandle File handle of a PEIM.\r
1360\r
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
1364\r
1365**/\r
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
1382 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISTER_FOR_SHADOW) {\r
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
1388\r
1389 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISTER_FOR_SHADOW;\r
1390\r
1391 return EFI_SUCCESS;\r
1392}\r
1393\r
1394\r
1395\r