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