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