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