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