2 EFI PEI Core dispatch services
4 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
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.
18 /// temporary memory is filled with this initial value during SEC phase
20 #define INIT_CAR_VALUE 0x5AA55AA5
24 Discover all Peims and optional Apriori file in one FV. There is at most one
25 Apriori file in one FV.
28 @param Private Pointer to the private data passed in from caller
29 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.
33 DiscoverPeimsAndOrderWithApriori (
34 IN PEI_CORE_INSTANCE
*Private
,
35 IN PEI_CORE_FV_HANDLE
*CoreFileHandle
39 EFI_PEI_FILE_HANDLE FileHandle
;
40 EFI_PEI_FILE_HANDLE AprioriFileHandle
;
47 EFI_PEI_FILE_HANDLE
*TempFileHandles
;
49 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
50 EFI_FV_FILE_INFO FileInfo
;
52 FvPpi
= CoreFileHandle
->FvPpi
;
55 // Walk the FV and find all the PEIMs and the Apriori file.
57 AprioriFileHandle
= NULL
;
58 Private
->CurrentFvFileHandles
[0] = NULL
;
61 TempFileHandles
= Private
->FileHandles
;
62 FileGuid
= Private
->FileGuid
;
65 // If the current Fv has been scanned, directly get its cachable record.
67 if (Private
->Fv
[Private
->CurrentPeimFvCount
].ScanFv
) {
68 CopyMem (Private
->CurrentFvFileHandles
, Private
->Fv
[Private
->CurrentPeimFvCount
].FvFileHandles
, sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
73 // Go ahead to scan this Fv, and cache FileHandles within it.
75 Status
= EFI_NOT_FOUND
;
76 for (PeimCount
= 0; PeimCount
<= PcdGet32 (PcdPeiCoreMaxPeimPerFv
); PeimCount
++) {
77 Status
= FvPpi
->FindFileByType (FvPpi
, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE
, CoreFileHandle
->FvHandle
, &FileHandle
);
78 if (Status
!= EFI_SUCCESS
|| PeimCount
== PcdGet32 (PcdPeiCoreMaxPeimPerFv
)) {
82 Private
->CurrentFvFileHandles
[PeimCount
] = FileHandle
;
86 // Check whether the count of files exceeds the max support files in a FV image
87 // If more files are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.
89 ASSERT ((Status
!= EFI_SUCCESS
) || (PeimCount
< PcdGet32 (PcdPeiCoreMaxPeimPerFv
)));
92 // Get Apriori File handle
94 Private
->AprioriCount
= 0;
95 Status
= FvPpi
->FindFileByName (FvPpi
, &gPeiAprioriFileNameGuid
, &CoreFileHandle
->FvHandle
, &AprioriFileHandle
);
96 if (!EFI_ERROR(Status
) && AprioriFileHandle
!= NULL
) {
98 // Read the Apriori file
100 Status
= FvPpi
->FindSectionByType (FvPpi
, EFI_SECTION_RAW
, AprioriFileHandle
, (VOID
**) &Apriori
);
101 if (!EFI_ERROR (Status
)) {
103 // Calculate the number of PEIMs in the A Priori list
105 Status
= FvPpi
->GetFileInfo (FvPpi
, AprioriFileHandle
, &FileInfo
);
106 ASSERT_EFI_ERROR (Status
);
107 Private
->AprioriCount
= FileInfo
.BufferSize
;
108 if (IS_SECTION2 (FileInfo
.Buffer
)) {
109 Private
->AprioriCount
-= sizeof (EFI_COMMON_SECTION_HEADER2
);
111 Private
->AprioriCount
-= sizeof (EFI_COMMON_SECTION_HEADER
);
113 Private
->AprioriCount
/= sizeof (EFI_GUID
);
115 for (Index
= 0; Index
< PeimCount
; Index
++) {
117 // Make an array of file name guids that matches the FileHandle array so we can convert
118 // quickly from file name to file handle
120 Status
= FvPpi
->GetFileInfo (FvPpi
, Private
->CurrentFvFileHandles
[Index
], &FileInfo
);
121 CopyMem (&FileGuid
[Index
], &FileInfo
.FileName
, sizeof(EFI_GUID
));
125 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.
126 // Add available PEIMs in Apriori file into TempFileHandles array at first.
129 for (Index
= 0; Index2
< Private
->AprioriCount
; Index
++) {
130 while (Index2
< Private
->AprioriCount
) {
131 Guid
= ScanGuid (FileGuid
, PeimCount
* sizeof (EFI_GUID
), &Apriori
[Index2
++]);
139 PeimIndex
= ((UINTN
)Guid
- (UINTN
)&FileGuid
[0])/sizeof (EFI_GUID
);
140 TempFileHandles
[Index
] = Private
->CurrentFvFileHandles
[PeimIndex
];
143 // Since we have copied the file handle we can remove it from this list.
145 Private
->CurrentFvFileHandles
[PeimIndex
] = NULL
;
149 // Update valid Aprioricount
151 Private
->AprioriCount
= Index
;
154 // Add in any PEIMs not in the Apriori file
156 for (;Index
< PeimCount
; Index
++) {
157 for (Index2
= 0; Index2
< PeimCount
; Index2
++) {
158 if (Private
->CurrentFvFileHandles
[Index2
] != NULL
) {
159 TempFileHandles
[Index
] = Private
->CurrentFvFileHandles
[Index2
];
160 Private
->CurrentFvFileHandles
[Index2
] = NULL
;
166 //Index the end of array contains re-range Pei moudle.
168 TempFileHandles
[Index
] = NULL
;
171 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.
172 // We need to update it to start with files in the A Priori list and
173 // then the remaining files in PEIM order.
175 CopyMem (Private
->CurrentFvFileHandles
, TempFileHandles
, sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
179 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.
180 // Instead, we can retrieve the file handles within this Fv from cachable data.
182 Private
->Fv
[Private
->CurrentPeimFvCount
].ScanFv
= TRUE
;
183 CopyMem (Private
->Fv
[Private
->CurrentPeimFvCount
].FvFileHandles
, Private
->CurrentFvFileHandles
, sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
));
188 // This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,
189 // This part of memory still need reserved on the very top of memory so that the DXE Core could
190 // use these memory for data initialization. This macro should be sync with the same marco
191 // defined in DXE Core.
193 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
195 This function is to test if the memory range described in resource HOB is available or not.
197 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the
198 memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is
201 @param PrivateData Pointer to the private data passed in from caller
202 @param ResourceHob Pointer to a resource HOB which described the memory range described by the input resource HOB
205 PeiLoadFixAddressIsMemoryRangeAvailable (
206 IN PEI_CORE_INSTANCE
*PrivateData
,
207 IN EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
210 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
212 EFI_PEI_HOB_POINTERS Hob
;
215 if (PrivateData
== NULL
|| ResourceHob
== NULL
) {
219 // test if the memory range describe in the HOB is already allocated.
221 for (Hob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
223 // See if this is a memory allocation HOB
225 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
226 MemoryHob
= Hob
.MemoryAllocation
;
227 if(MemoryHob
->AllocDescriptor
.MemoryBaseAddress
== ResourceHob
->PhysicalStart
&&
228 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryHob
->AllocDescriptor
.MemoryLength
== ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
) {
239 Hook function for Loading Module at Fixed Address feature
241 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is
242 configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When
243 feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general.
244 And also the function will re-install PEI memory.
246 @param PrivateData Pointer to the private data passed in from caller
250 PeiLoadFixAddressHook(
251 IN PEI_CORE_INSTANCE
*PrivateData
254 EFI_PHYSICAL_ADDRESS TopLoadingAddress
;
255 UINT64 PeiMemorySize
;
256 UINT64 TotalReservedMemorySize
;
257 UINT64 MemoryRangeEnd
;
258 EFI_PHYSICAL_ADDRESS HighAddress
;
259 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
260 EFI_HOB_RESOURCE_DESCRIPTOR
*NextResourceHob
;
261 EFI_HOB_RESOURCE_DESCRIPTOR
*CurrentResourceHob
;
262 EFI_PEI_HOB_POINTERS CurrentHob
;
263 EFI_PEI_HOB_POINTERS Hob
;
264 EFI_PEI_HOB_POINTERS NextHob
;
265 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
267 // Initialize Local Variables
269 CurrentResourceHob
= NULL
;
271 NextResourceHob
= NULL
;
273 TopLoadingAddress
= 0;
275 CurrentHob
.Raw
= PrivateData
->HobList
.Raw
;
276 PeiMemorySize
= PrivateData
->PhysicalMemoryLength
;
278 // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE
279 // then RuntimeCodePage range and Boot time code range.
281 TotalReservedMemorySize
= MINIMUM_INITIAL_MEMORY_SIZE
+ EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
));
282 TotalReservedMemorySize
+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
)) ;
284 // PEI memory range lies below the top reserved memory
286 TotalReservedMemorySize
+= PeiMemorySize
;
288 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
)));
289 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
)));
290 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber
)));
291 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize
));
293 // Loop through the system memory typed hob to merge the adjacent memory range
295 for (Hob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
297 // See if this is a resource descriptor HOB
299 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
301 ResourceHob
= Hob
.ResourceDescriptor
;
303 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.
305 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
||
306 ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
> MAX_ADDRESS
) {
310 for (NextHob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(NextHob
); NextHob
.Raw
= GET_NEXT_HOB(NextHob
)) {
311 if (NextHob
.Raw
== Hob
.Raw
){
315 // See if this is a resource descriptor HOB
317 if (GET_HOB_TYPE (NextHob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
319 NextResourceHob
= NextHob
.ResourceDescriptor
;
321 // test if range described in this NextResourceHob is system memory and have the same attribute.
322 // Note: Here is a assumption that system memory should always be healthy even without test.
324 if (NextResourceHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
&&
325 (((NextResourceHob
->ResourceAttribute
^ResourceHob
->ResourceAttribute
)&(~EFI_RESOURCE_ATTRIBUTE_TESTED
)) == 0)){
328 // See if the memory range described in ResourceHob and NextResourceHob is adjacent
330 if ((ResourceHob
->PhysicalStart
<= NextResourceHob
->PhysicalStart
&&
331 ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
>= NextResourceHob
->PhysicalStart
)||
332 (ResourceHob
->PhysicalStart
>= NextResourceHob
->PhysicalStart
&&
333 ResourceHob
->PhysicalStart
<= NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
)) {
335 MemoryRangeEnd
= ((ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
)>(NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
)) ?
336 (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
):(NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
);
338 ResourceHob
->PhysicalStart
= (ResourceHob
->PhysicalStart
< NextResourceHob
->PhysicalStart
) ?
339 ResourceHob
->PhysicalStart
: NextResourceHob
->PhysicalStart
;
342 ResourceHob
->ResourceLength
= (MemoryRangeEnd
- ResourceHob
->PhysicalStart
);
344 ResourceHob
->ResourceAttribute
= ResourceHob
->ResourceAttribute
& (~EFI_RESOURCE_ATTRIBUTE_TESTED
);
346 // Delete the NextResourceHob by marking it as unused.
348 GET_HOB_TYPE (NextHob
) = EFI_HOB_TYPE_UNUSED
;
357 // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe
358 // the allocated memory range
360 for (Hob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
362 // See if this is a memory allocation HOB
364 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
365 MemoryHob
= Hob
.MemoryAllocation
;
366 for (NextHob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(NextHob
); NextHob
.Raw
= GET_NEXT_HOB(NextHob
)) {
368 // See if this is a resource descriptor HOB
370 if (GET_HOB_TYPE (NextHob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
371 NextResourceHob
= NextHob
.ResourceDescriptor
;
373 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.
375 if (NextResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
|| NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
> MAX_ADDRESS
) {
379 // If the range describe in memory allocation HOB belongs to the memroy range described by the resource hob
381 if (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
>= NextResourceHob
->PhysicalStart
&&
382 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryHob
->AllocDescriptor
.MemoryLength
<= NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
) {
384 // Build seperate resource hob for this allocated range
386 if (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
> NextResourceHob
->PhysicalStart
) {
387 BuildResourceDescriptorHob (
388 EFI_RESOURCE_SYSTEM_MEMORY
,
389 NextResourceHob
->ResourceAttribute
,
390 NextResourceHob
->PhysicalStart
,
391 (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
- NextResourceHob
->PhysicalStart
)
394 if (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryHob
->AllocDescriptor
.MemoryLength
< NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
) {
395 BuildResourceDescriptorHob (
396 EFI_RESOURCE_SYSTEM_MEMORY
,
397 NextResourceHob
->ResourceAttribute
,
398 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryHob
->AllocDescriptor
.MemoryLength
,
399 (NextResourceHob
->PhysicalStart
+ NextResourceHob
->ResourceLength
-(MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryHob
->AllocDescriptor
.MemoryLength
))
402 NextResourceHob
->PhysicalStart
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
403 NextResourceHob
->ResourceLength
= MemoryHob
->AllocDescriptor
.MemoryLength
;
412 // Try to find and validate the TOP address.
414 if ((INT64
)PcdGet64(PcdLoadModuleAtFixAddressEnable
) > 0 ) {
416 // The LMFA feature is enabled as load module at fixed absolute address.
418 TopLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)PcdGet64(PcdLoadModuleAtFixAddressEnable
);
419 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n"));
421 // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range
423 if ((TopLoadingAddress
& EFI_PAGE_MASK
) != 0) {
424 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress
));
428 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies
430 for (Hob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
432 // See if this is a resource descriptor HOB
434 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
436 ResourceHob
= Hob
.ResourceDescriptor
;
438 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS
440 if (ResourceHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
&&
441 ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
<= MAX_ADDRESS
) {
443 // See if Top address specified by user is valid.
445 if (ResourceHob
->PhysicalStart
+ TotalReservedMemorySize
< TopLoadingAddress
&&
446 (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- MINIMUM_INITIAL_MEMORY_SIZE
) >= TopLoadingAddress
&&
447 PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData
, ResourceHob
)) {
448 CurrentResourceHob
= ResourceHob
;
455 if (CurrentResourceHob
!= NULL
) {
456 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress
));
457 TopLoadingAddress
+= MINIMUM_INITIAL_MEMORY_SIZE
;
459 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n", TopLoadingAddress
));
460 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n"));
462 // Print the recomended Top address range.
464 for (Hob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
466 // See if this is a resource descriptor HOB
468 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
470 ResourceHob
= Hob
.ResourceDescriptor
;
472 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS
474 if (ResourceHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
&&
475 ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
<= MAX_ADDRESS
) {
477 // See if Top address specified by user is valid.
479 if (ResourceHob
->ResourceLength
> TotalReservedMemorySize
&& PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData
, ResourceHob
)) {
480 DEBUG ((EFI_D_INFO
, "(0x%lx, 0x%lx)\n",
481 (ResourceHob
->PhysicalStart
+ TotalReservedMemorySize
-MINIMUM_INITIAL_MEMORY_SIZE
),
482 (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
-MINIMUM_INITIAL_MEMORY_SIZE
)
496 // The LMFA feature is enabled as load module at fixed offset relative to TOLM
497 // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG)
500 // Search for a tested memory region that is below MAX_ADDRESS
502 for (Hob
.Raw
= PrivateData
->HobList
.Raw
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
504 // See if this is a resource descriptor HOB
506 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
508 ResourceHob
= Hob
.ResourceDescriptor
;
510 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS
512 if (ResourceHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
&&
513 ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
<= MAX_ADDRESS
&&
514 ResourceHob
->ResourceLength
> TotalReservedMemorySize
&& PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData
, ResourceHob
)) {
516 // See if this is the highest largest system memory region below MaxAddress
518 if (ResourceHob
->PhysicalStart
> HighAddress
) {
519 CurrentResourceHob
= ResourceHob
;
521 HighAddress
= CurrentResourceHob
->PhysicalStart
;
526 if (CurrentResourceHob
== NULL
) {
527 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR:The System Memory is too small\n"));
534 TopLoadingAddress
= CurrentResourceHob
->PhysicalStart
+ CurrentResourceHob
->ResourceLength
;
538 if (CurrentResourceHob
!= NULL
) {
540 // rebuild resource HOB for PEI memmory and reserved memory
542 BuildResourceDescriptorHob (
543 EFI_RESOURCE_SYSTEM_MEMORY
,
545 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
546 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
547 EFI_RESOURCE_ATTRIBUTE_TESTED
|
548 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
549 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
550 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
551 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
553 (TopLoadingAddress
- TotalReservedMemorySize
),
554 TotalReservedMemorySize
557 // rebuild resource for the remain memory if necessary
559 if (CurrentResourceHob
->PhysicalStart
< TopLoadingAddress
- TotalReservedMemorySize
) {
560 BuildResourceDescriptorHob (
561 EFI_RESOURCE_SYSTEM_MEMORY
,
563 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
564 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
565 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
566 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
567 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
568 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
570 CurrentResourceHob
->PhysicalStart
,
571 (TopLoadingAddress
- TotalReservedMemorySize
- CurrentResourceHob
->PhysicalStart
)
574 if (CurrentResourceHob
->PhysicalStart
+ CurrentResourceHob
->ResourceLength
> TopLoadingAddress
) {
575 BuildResourceDescriptorHob (
576 EFI_RESOURCE_SYSTEM_MEMORY
,
578 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
579 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
580 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
581 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
582 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
583 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
586 (CurrentResourceHob
->PhysicalStart
+ CurrentResourceHob
->ResourceLength
- TopLoadingAddress
)
590 // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt.
592 GET_HOB_TYPE (CurrentHob
) = EFI_HOB_TYPE_UNUSED
;
596 // Cache the top address for Loading Module at Fixed Address feature
598 PrivateData
->LoadModuleAtFixAddressTopAddress
= TopLoadingAddress
- MINIMUM_INITIAL_MEMORY_SIZE
;
599 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData
->LoadModuleAtFixAddressTopAddress
));
601 // reinstall the PEI memory relative to TopLoadingAddress
603 PrivateData
->PhysicalMemoryBegin
= TopLoadingAddress
- TotalReservedMemorySize
;
604 PrivateData
->FreePhysicalMemoryTop
= PrivateData
->PhysicalMemoryBegin
+ PeiMemorySize
;
608 This routine is invoked in switch stack as PeiCore Entry.
610 @param SecCoreData Points to a data structure containing information about the PEI core's operating
611 environment, such as the size and location of temporary RAM, the stack location and
613 @param Private Pointer to old core data that is used to initialize the
619 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
620 IN PEI_CORE_INSTANCE
*Private
626 PeiCore (SecCoreData
, NULL
, Private
);
630 Conduct PEIM dispatch.
632 @param SecCoreData Points to a data structure containing information about the PEI core's operating
633 environment, such as the size and location of temporary RAM, the stack location and
635 @param Private Pointer to the private data passed in from caller
640 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
641 IN PEI_CORE_INSTANCE
*Private
647 CONST EFI_PEI_SERVICES
**PeiServices
;
648 EFI_PEI_FILE_HANDLE PeimFileHandle
;
651 UINT32 AuthenticationState
;
652 EFI_PHYSICAL_ADDRESS EntryPoint
;
653 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint
;
654 UINTN SaveCurrentPeimCount
;
655 UINTN SaveCurrentFvCount
;
656 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle
;
657 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI
*TemporaryRamSupportPpi
;
659 UINTN HeapTemporaryRamSize
;
660 EFI_PHYSICAL_ADDRESS BaseOfNewHeap
;
661 EFI_PHYSICAL_ADDRESS TopOfNewStack
;
662 EFI_PHYSICAL_ADDRESS TopOfOldStack
;
663 EFI_PHYSICAL_ADDRESS TemporaryRamBase
;
664 UINTN TemporaryRamSize
;
665 UINTN TemporaryStackSize
;
666 VOID
*TemporaryStackBase
;
667 UINTN PeiTemporaryRamSize
;
668 VOID
*PeiTemporaryRamBase
;
670 BOOLEAN StackOffsetPositive
;
671 EFI_PHYSICAL_ADDRESS HoleMemBase
;
673 EFI_FV_FILE_INFO FvFileInfo
;
674 PEI_CORE_FV_HANDLE
*CoreFvHandle
;
675 VOID
*LoadFixPeiCodeBegin
;
676 EFI_PHYSICAL_ADDRESS TempBase1
;
678 EFI_PHYSICAL_ADDRESS TempBase2
;
682 PeiServices
= (CONST EFI_PEI_SERVICES
**) &Private
->Ps
;
683 PeimEntryPoint
= NULL
;
684 PeimFileHandle
= NULL
;
687 if ((Private
->PeiMemoryInstalled
) && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
|| PcdGetBool (PcdShadowPeimOnS3Boot
))) {
689 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile
690 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.
692 SaveCurrentPeimCount
= Private
->CurrentPeimCount
;
693 SaveCurrentFvCount
= Private
->CurrentPeimFvCount
;
694 SaveCurrentFileHandle
= Private
->CurrentFileHandle
;
696 for (Index1
= 0; Index1
<= SaveCurrentFvCount
; Index1
++) {
697 for (Index2
= 0; (Index2
< PcdGet32 (PcdPeiCoreMaxPeimPerFv
)) && (Private
->Fv
[Index1
].FvFileHandles
[Index2
] != NULL
); Index2
++) {
698 if (Private
->Fv
[Index1
].PeimState
[Index2
] == PEIM_STATE_REGISITER_FOR_SHADOW
) {
699 PeimFileHandle
= Private
->Fv
[Index1
].FvFileHandles
[Index2
];
700 Private
->CurrentFileHandle
= PeimFileHandle
;
701 Private
->CurrentPeimFvCount
= Index1
;
702 Private
->CurrentPeimCount
= Index2
;
703 Status
= PeiLoadImage (
704 (CONST EFI_PEI_SERVICES
**) &Private
->Ps
,
706 PEIM_STATE_REGISITER_FOR_SHADOW
,
710 if (Status
== EFI_SUCCESS
) {
712 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
714 Private
->Fv
[Index1
].PeimState
[Index2
]++;
716 // Call the PEIM entry point
718 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT2
)(UINTN
)EntryPoint
;
720 PERF_START (PeimFileHandle
, "PEIM", NULL
, 0);
721 PeimEntryPoint(PeimFileHandle
, (const EFI_PEI_SERVICES
**) &Private
->Ps
);
722 PERF_END (PeimFileHandle
, "PEIM", NULL
, 0);
726 // Process the Notify list and dispatch any notifies for
727 // newly installed PPIs.
729 ProcessNotifyList (Private
);
733 Private
->CurrentFileHandle
= SaveCurrentFileHandle
;
734 Private
->CurrentPeimFvCount
= SaveCurrentFvCount
;
735 Private
->CurrentPeimCount
= SaveCurrentPeimCount
;
739 // This is the main dispatch loop. It will search known FVs for PEIMs and
740 // attempt to dispatch them. If any PEIM gets dispatched through a single
741 // pass of the dispatcher, it will start over from the Bfv again to see
742 // if any new PEIMs dependencies got satisfied. With a well ordered
743 // FV where PEIMs are found in the order their dependencies are also
744 // satisfied, this dipatcher should run only once.
748 // In case that reenter PeiCore happens, the last pass record is still available.
750 if (!Private
->PeimDispatcherReenter
) {
751 Private
->PeimNeedingDispatch
= FALSE
;
752 Private
->PeimDispatchOnThisPass
= FALSE
;
754 Private
->PeimDispatcherReenter
= FALSE
;
757 for (FvCount
= Private
->CurrentPeimFvCount
; FvCount
< Private
->FvCount
; FvCount
++) {
758 CoreFvHandle
= FindNextCoreFvHandle (Private
, FvCount
);
759 ASSERT (CoreFvHandle
!= NULL
);
762 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.
764 if (CoreFvHandle
->FvPpi
== NULL
) {
768 Private
->CurrentPeimFvCount
= FvCount
;
770 if (Private
->CurrentPeimCount
== 0) {
772 // When going through each FV, at first, search Apriori file to
773 // reorder all PEIMs to ensure the PEIMs in Apriori file to get
774 // dispatch at first.
776 DiscoverPeimsAndOrderWithApriori (Private
, CoreFvHandle
);
780 // Start to dispatch all modules within the current Fv.
782 for (PeimCount
= Private
->CurrentPeimCount
;
783 (PeimCount
< PcdGet32 (PcdPeiCoreMaxPeimPerFv
)) && (Private
->CurrentFvFileHandles
[PeimCount
] != NULL
);
785 Private
->CurrentPeimCount
= PeimCount
;
786 PeimFileHandle
= Private
->CurrentFileHandle
= Private
->CurrentFvFileHandles
[PeimCount
];
788 if (Private
->Fv
[FvCount
].PeimState
[PeimCount
] == PEIM_STATE_NOT_DISPATCHED
) {
789 if (!DepexSatisfied (Private
, PeimFileHandle
, PeimCount
)) {
790 Private
->PeimNeedingDispatch
= TRUE
;
792 Status
= CoreFvHandle
->FvPpi
->GetFileInfo (CoreFvHandle
->FvPpi
, PeimFileHandle
, &FvFileInfo
);
793 ASSERT_EFI_ERROR (Status
);
794 if (FvFileInfo
.FileType
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
796 // For Fv type file, Produce new FV PPI and FV hob
798 Status
= ProcessFvFile (Private
, &Private
->Fv
[FvCount
], PeimFileHandle
);
799 if (Status
== EFI_SUCCESS
) {
801 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
803 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
804 Private
->PeimDispatchOnThisPass
= TRUE
;
808 // For PEIM driver, Load its entry point
810 Status
= PeiLoadImage (
813 PEIM_STATE_NOT_DISPATCHED
,
817 if (Status
== EFI_SUCCESS
) {
819 // The PEIM has its dependencies satisfied, and its entry point
820 // has been found, so invoke it.
822 PERF_START (PeimFileHandle
, "PEIM", NULL
, 0);
824 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
826 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT_BEGIN
),
827 (VOID
*)(&PeimFileHandle
),
828 sizeof (PeimFileHandle
)
831 Status
= VerifyPeim (Private
, CoreFvHandle
->FvHandle
, PeimFileHandle
, AuthenticationState
);
832 if (Status
!= EFI_SECURITY_VIOLATION
) {
834 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
836 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
838 // Call the PEIM entry point for PEIM driver
840 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT2
)(UINTN
)EntryPoint
;
841 PeimEntryPoint (PeimFileHandle
, (const EFI_PEI_SERVICES
**) PeiServices
);
842 Private
->PeimDispatchOnThisPass
= TRUE
;
845 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
847 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT_END
),
848 (VOID
*)(&PeimFileHandle
),
849 sizeof (PeimFileHandle
)
851 PERF_END (PeimFileHandle
, "PEIM", NULL
, 0);
856 if (Private
->SwitchStackSignal
) {
858 // Before switch stack from temporary memory to permenent memory, calculate the heap and stack
859 // usage in temporary memory for debuging.
862 UINT32
*StackPointer
;
864 for (StackPointer
= (UINT32
*)SecCoreData
->StackBase
;
865 (StackPointer
< (UINT32
*)((UINTN
)SecCoreData
->StackBase
+ SecCoreData
->StackSize
)) \
866 && (*StackPointer
== INIT_CAR_VALUE
);
869 DEBUG ((EFI_D_INFO
, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData
->StackBase
, (UINT32
)SecCoreData
->StackSize
));
870 DEBUG ((EFI_D_INFO
, "Temp Heap : BaseAddress=0x%p Length=0x%X\n", Private
->HobList
.Raw
, (UINT32
)((UINTN
) Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryBottom
- (UINTN
) Private
->HobList
.Raw
)));
871 DEBUG ((EFI_D_INFO
, "Total temporary memory: %d bytes.\n", (UINT32
)SecCoreData
->TemporaryRamSize
));
872 DEBUG ((EFI_D_INFO
, " temporary memory stack ever used: %d bytes.\n",
873 (UINT32
)(SecCoreData
->StackSize
- ((UINTN
) StackPointer
- (UINTN
)SecCoreData
->StackBase
))
875 DEBUG ((EFI_D_INFO
, " temporary memory heap used: %d bytes.\n",
876 (UINT32
)((UINTN
)Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryBottom
- (UINTN
)Private
->HobList
.Raw
)
880 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
882 // Loading Module at Fixed Address is enabled
884 PeiLoadFixAddressHook (Private
);
887 // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.
889 LoadFixPeiCodeBegin
= AllocatePages((UINTN
)PcdGet32(PcdLoadFixAddressPeiCodePageNumber
));
890 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: PeiCodeBegin = 0x%lX, PeiCodeTop= 0x%lX\n", (UINT64
)(UINTN
)LoadFixPeiCodeBegin
, (UINT64
)((UINTN
)LoadFixPeiCodeBegin
+ PcdGet32(PcdLoadFixAddressPeiCodePageNumber
) * EFI_PAGE_SIZE
)));
894 // Reserve the size of new stack at bottom of physical memory
896 // The size of new stack in permenent memory must be the same size
897 // or larger than the size of old stack in temporary memory.
898 // But if new stack is smaller than the size of old stack, we also reserve
899 // the size of old stack at bottom of permenent memory.
901 NewStackSize
= RShiftU64 (Private
->PhysicalMemoryLength
, 1);
902 NewStackSize
= ALIGN_VALUE (NewStackSize
, EFI_PAGE_SIZE
);
903 NewStackSize
= MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize
), NewStackSize
);
904 DEBUG ((EFI_D_INFO
, "Old Stack size %d, New stack size %d\n", (UINT32
)SecCoreData
->StackSize
, (UINT32
)NewStackSize
));
905 ASSERT (NewStackSize
>= SecCoreData
->StackSize
);
908 // Calculate stack offset and heap offset between temporary memory and new permement
909 // memory seperately.
911 TopOfOldStack
= (UINTN
)SecCoreData
->StackBase
+ SecCoreData
->StackSize
;
912 TopOfNewStack
= Private
->PhysicalMemoryBegin
+ NewStackSize
;
913 if (TopOfNewStack
>= TopOfOldStack
) {
914 StackOffsetPositive
= TRUE
;
915 StackOffset
= (UINTN
)(TopOfNewStack
- TopOfOldStack
);
917 StackOffsetPositive
= FALSE
;
918 StackOffset
= (UINTN
)(TopOfOldStack
- TopOfNewStack
);
920 Private
->StackOffsetPositive
= StackOffsetPositive
;
921 Private
->StackOffset
= StackOffset
;
924 // Build Stack HOB that describes the permanent memory stack
926 DEBUG ((EFI_D_INFO
, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack
- NewStackSize
, NewStackSize
));
927 BuildStackHob (TopOfNewStack
- NewStackSize
, NewStackSize
);
930 // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address
932 TemporaryRamBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)SecCoreData
->TemporaryRamBase
;
933 TemporaryRamSize
= SecCoreData
->TemporaryRamSize
;
934 TemporaryStackSize
= SecCoreData
->StackSize
;
935 TemporaryStackBase
= SecCoreData
->StackBase
;
936 PeiTemporaryRamSize
= SecCoreData
->PeiTemporaryRamSize
;
937 PeiTemporaryRamBase
= SecCoreData
->PeiTemporaryRamBase
;
940 // TemporaryRamSupportPpi is produced by platform's SEC
942 Status
= PeiServicesLocatePpi (
943 &gEfiTemporaryRamSupportPpiGuid
,
946 (VOID
**)&TemporaryRamSupportPpi
948 if (!EFI_ERROR (Status
)) {
952 BaseOfNewHeap
= TopOfNewStack
;
953 if (BaseOfNewHeap
>= (UINTN
)SecCoreData
->PeiTemporaryRamBase
) {
954 Private
->HeapOffsetPositive
= TRUE
;
955 Private
->HeapOffset
= (UINTN
)(BaseOfNewHeap
- (UINTN
)SecCoreData
->PeiTemporaryRamBase
);
957 Private
->HeapOffsetPositive
= FALSE
;
958 Private
->HeapOffset
= (UINTN
)((UINTN
)SecCoreData
->PeiTemporaryRamBase
- BaseOfNewHeap
);
961 DEBUG ((EFI_D_INFO
, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64
) Private
->HeapOffset
, (UINT64
) Private
->StackOffset
));
964 // Calculate new HandOffTable and PrivateData address in permanent memory's stack
966 if (StackOffsetPositive
) {
967 SecCoreData
= (CONST EFI_SEC_PEI_HAND_OFF
*)((UINTN
)(VOID
*)SecCoreData
+ StackOffset
);
968 Private
= (PEI_CORE_INSTANCE
*)((UINTN
)(VOID
*)Private
+ StackOffset
);
970 SecCoreData
= (CONST EFI_SEC_PEI_HAND_OFF
*)((UINTN
)(VOID
*)SecCoreData
- StackOffset
);
971 Private
= (PEI_CORE_INSTANCE
*)((UINTN
)(VOID
*)Private
- StackOffset
);
975 // Temporary Ram Support PPI is provided by platform, it will copy
976 // temporary memory to permenent memory and do stack switching.
977 // After invoking Temporary Ram Support PPI, the following code's
978 // stack is in permanent memory.
980 TemporaryRamSupportPpi
->TemporaryRamMigration (
983 (EFI_PHYSICAL_ADDRESS
)(UINTN
)(TopOfNewStack
- TemporaryStackSize
),
990 PeiCore (SecCoreData
, NULL
, Private
);
993 // Migrate the PEI Services Table pointer from temporary RAM to permanent RAM.
995 MigratePeiServicesTablePointer ();
1000 BaseOfNewHeap
= TopOfNewStack
;
1001 HoleMemBase
= TopOfNewStack
;
1002 HoleMemSize
= TemporaryRamSize
- PeiTemporaryRamSize
- TemporaryStackSize
;
1003 if (HoleMemSize
!= 0) {
1005 // Make sure HOB List start address is 8 byte alignment.
1007 BaseOfNewHeap
= ALIGN_VALUE (BaseOfNewHeap
+ HoleMemSize
, 8);
1009 if (BaseOfNewHeap
>= (UINTN
)SecCoreData
->PeiTemporaryRamBase
) {
1010 Private
->HeapOffsetPositive
= TRUE
;
1011 Private
->HeapOffset
= (UINTN
)(BaseOfNewHeap
- (UINTN
)SecCoreData
->PeiTemporaryRamBase
);
1013 Private
->HeapOffsetPositive
= FALSE
;
1014 Private
->HeapOffset
= (UINTN
)((UINTN
)SecCoreData
->PeiTemporaryRamBase
- BaseOfNewHeap
);
1017 DEBUG ((EFI_D_INFO
, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64
) Private
->HeapOffset
, (UINT64
) Private
->StackOffset
));
1022 HeapTemporaryRamSize
= (UINTN
) (Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryBottom
- Private
->HobList
.HandoffInformationTable
->EfiMemoryBottom
);
1023 ASSERT (BaseOfNewHeap
+ HeapTemporaryRamSize
<= Private
->FreePhysicalMemoryTop
);
1024 CopyMem ((UINT8
*) (UINTN
) BaseOfNewHeap
, (UINT8
*) PeiTemporaryRamBase
, HeapTemporaryRamSize
);
1029 CopyMem ((UINT8
*) (UINTN
) (TopOfNewStack
- TemporaryStackSize
), TemporaryStackBase
, TemporaryStackSize
);
1032 // Copy Hole Range Data
1033 // Convert PPI from Hole.
1035 if (HoleMemSize
!= 0) {
1039 if (PeiTemporaryRamBase
< TemporaryStackBase
) {
1040 TempBase1
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) PeiTemporaryRamBase
;
1041 TempSize1
= PeiTemporaryRamSize
;
1042 TempBase2
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) TemporaryStackBase
;
1043 TempSize2
= TemporaryStackSize
;
1045 TempBase1
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) TemporaryStackBase
;
1046 TempSize1
= TemporaryStackSize
;
1047 TempBase2
=(EFI_PHYSICAL_ADDRESS
) (UINTN
) PeiTemporaryRamBase
;
1048 TempSize2
= PeiTemporaryRamSize
;
1050 if (TemporaryRamBase
< TempBase1
) {
1051 Private
->HoleData
[0].Base
= TemporaryRamBase
;
1052 Private
->HoleData
[0].Size
= (UINTN
) (TempBase1
- TemporaryRamBase
);
1054 if (TempBase1
+ TempSize1
< TempBase2
) {
1055 Private
->HoleData
[1].Base
= TempBase1
+ TempSize1
;
1056 Private
->HoleData
[1].Size
= (UINTN
) (TempBase2
- TempBase1
- TempSize1
);
1058 if (TempBase2
+ TempSize2
< TemporaryRamBase
+ TemporaryRamSize
) {
1059 Private
->HoleData
[2].Base
= TempBase2
+ TempSize2
;
1060 Private
->HoleData
[2].Size
= (UINTN
) (TemporaryRamBase
+ TemporaryRamSize
- TempBase2
- TempSize2
);
1064 // Copy Hole Range data.
1066 for (Index
= 0; Index
< HOLE_MAX_NUMBER
; Index
++) {
1067 if (Private
->HoleData
[Index
].Size
> 0) {
1068 if (HoleMemBase
> Private
->HoleData
[Index
].Base
) {
1069 Private
->HoleData
[Index
].OffsetPositive
= TRUE
;
1070 Private
->HoleData
[Index
].Offset
= (UINTN
) (HoleMemBase
- Private
->HoleData
[Index
].Base
);
1072 Private
->HoleData
[Index
].OffsetPositive
= FALSE
;
1073 Private
->HoleData
[Index
].Offset
= (UINTN
) (Private
->HoleData
[Index
].Base
- HoleMemBase
);
1075 CopyMem ((VOID
*) (UINTN
) HoleMemBase
, (VOID
*) (UINTN
) Private
->HoleData
[Index
].Base
, Private
->HoleData
[Index
].Size
);
1076 HoleMemBase
= HoleMemBase
+ Private
->HoleData
[Index
].Size
;
1085 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)PeiCoreEntry
,
1086 (VOID
*) SecCoreData
,
1088 (VOID
*) (UINTN
) TopOfNewStack
1093 // Code should not come here
1099 // Process the Notify list and dispatch any notifies for
1100 // newly installed PPIs.
1102 ProcessNotifyList (Private
);
1104 if ((Private
->PeiMemoryInstalled
) && (Private
->Fv
[FvCount
].PeimState
[PeimCount
] == PEIM_STATE_REGISITER_FOR_SHADOW
) && \
1105 (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
|| PcdGetBool (PcdShadowPeimOnS3Boot
))) {
1107 // If memory is availble we shadow images by default for performance reasons.
1108 // We call the entry point a 2nd time so the module knows it's shadowed.
1110 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
1111 if ((Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
) && !PcdGetBool (PcdShadowPeimOnBoot
)) {
1113 // Load PEIM into Memory for Register for shadow PEIM.
1115 Status
= PeiLoadImage (
1118 PEIM_STATE_REGISITER_FOR_SHADOW
,
1120 &AuthenticationState
1122 if (Status
== EFI_SUCCESS
) {
1123 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT2
)(UINTN
)EntryPoint
;
1126 ASSERT (PeimEntryPoint
!= NULL
);
1127 PeimEntryPoint (PeimFileHandle
, (const EFI_PEI_SERVICES
**) PeiServices
);
1128 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
1131 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
1133 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
1136 // Process the Notify list and dispatch any notifies for
1137 // newly installed PPIs.
1139 ProcessNotifyList (Private
);
1146 // We set to NULL here to optimize the 2nd entry to this routine after
1147 // memory is found. This reprevents rescanning of the FV. We set to
1148 // NULL here so we start at the begining of the next FV
1150 Private
->CurrentFileHandle
= NULL
;
1151 Private
->CurrentPeimCount
= 0;
1153 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL
1155 SetMem (Private
->CurrentFvFileHandles
, sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
), 0);
1159 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go
1160 // through all the FV.
1162 Private
->CurrentPeimFvCount
= 0;
1165 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get
1166 // dispatched. So we need to make another pass
1168 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this
1169 // pass. If we did not dispatch a PEIM there is no point in trying again
1170 // as it will fail the next time too (nothing has changed).
1172 } while (Private
->PeimNeedingDispatch
&& Private
->PeimDispatchOnThisPass
);
1177 Initialize the Dispatcher's data members
1179 @param PrivateData PeiCore's private data structure
1180 @param OldCoreData Old data from SecCore
1181 NULL if being run in non-permament memory mode.
1182 @param SecCoreData Points to a data structure containing information about the PEI core's operating
1183 environment, such as the size and location of temporary RAM, the stack location and
1190 InitializeDispatcherData (
1191 IN PEI_CORE_INSTANCE
*PrivateData
,
1192 IN PEI_CORE_INSTANCE
*OldCoreData
,
1193 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
1196 if (OldCoreData
== NULL
) {
1197 PrivateData
->PeimDispatcherReenter
= FALSE
;
1198 PeiInitializeFv (PrivateData
, SecCoreData
);
1200 PeiReinitializeFv (PrivateData
);
1207 This routine parses the Dependency Expression, if available, and
1208 decides if the module can be executed.
1211 @param Private PeiCore's private data structure
1212 @param FileHandle PEIM's file handle
1213 @param PeimCount Peim count in all dispatched PEIMs.
1215 @retval TRUE Can be dispatched
1216 @retval FALSE Cannot be dispatched
1221 IN PEI_CORE_INSTANCE
*Private
,
1222 IN EFI_PEI_FILE_HANDLE FileHandle
,
1228 EFI_FV_FILE_INFO FileInfo
;
1230 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &FileInfo
);
1231 if (EFI_ERROR (Status
)) {
1232 DEBUG ((DEBUG_DISPATCH
, "Evaluate PEI DEPEX for FFS(Unknown)\n"));
1234 DEBUG ((DEBUG_DISPATCH
, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo
.FileName
));
1237 if (PeimCount
< Private
->AprioriCount
) {
1239 // If its in the A priori file then we set Depex to TRUE
1241 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1246 // Depex section not in the encapsulated section.
1248 Status
= PeiServicesFfsFindSectionData (
1249 EFI_SECTION_PEI_DEPEX
,
1254 if (EFI_ERROR (Status
)) {
1256 // If there is no DEPEX, assume the module can be executed
1258 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (No DEPEX)\n"));
1263 // Evaluate a given DEPEX
1265 return PeimDispatchReadiness (&Private
->Ps
, DepexData
);
1269 This routine enable a PEIM to register itself to shadow when PEI Foundation
1270 discovery permanent memory.
1272 @param FileHandle File handle of a PEIM.
1274 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.
1275 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.
1276 @retval EFI_SUCCESS Successfully to register itself.
1281 PeiRegisterForShadow (
1282 IN EFI_PEI_FILE_HANDLE FileHandle
1285 PEI_CORE_INSTANCE
*Private
;
1286 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
1288 if (Private
->CurrentFileHandle
!= FileHandle
) {
1290 // The FileHandle must be for the current PEIM
1292 return EFI_NOT_FOUND
;
1295 if (Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] >= PEIM_STATE_REGISITER_FOR_SHADOW
) {
1297 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started
1299 return EFI_ALREADY_STARTED
;
1302 Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] = PEIM_STATE_REGISITER_FOR_SHADOW
;