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