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