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