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