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