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