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