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