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 Check SwitchStackSignal and switch stack if SwitchStackSignal is TRUE.
632 @param[in] 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[in] Private Pointer to the private data passed in from caller.
639 PeiCheckAndSwitchStack (
640 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
641 IN PEI_CORE_INSTANCE
*Private
644 VOID
*LoadFixPeiCodeBegin
;
646 CONST EFI_PEI_SERVICES
**PeiServices
;
648 EFI_PHYSICAL_ADDRESS TopOfOldStack
;
649 EFI_PHYSICAL_ADDRESS TopOfNewStack
;
651 BOOLEAN StackOffsetPositive
;
652 EFI_PHYSICAL_ADDRESS TemporaryRamBase
;
653 UINTN TemporaryRamSize
;
654 UINTN TemporaryStackSize
;
655 VOID
*TemporaryStackBase
;
656 UINTN PeiTemporaryRamSize
;
657 VOID
*PeiTemporaryRamBase
;
658 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI
*TemporaryRamSupportPpi
;
659 EFI_PHYSICAL_ADDRESS BaseOfNewHeap
;
660 EFI_PHYSICAL_ADDRESS HoleMemBase
;
662 UINTN HeapTemporaryRamSize
;
663 EFI_PHYSICAL_ADDRESS TempBase1
;
665 EFI_PHYSICAL_ADDRESS TempBase2
;
669 PeiServices
= (CONST EFI_PEI_SERVICES
**) &Private
->Ps
;
671 if (Private
->SwitchStackSignal
) {
673 // Before switch stack from temporary memory to permenent memory, calculate the heap and stack
674 // usage in temporary memory for debuging.
677 UINT32
*StackPointer
;
679 for (StackPointer
= (UINT32
*)SecCoreData
->StackBase
;
680 (StackPointer
< (UINT32
*)((UINTN
)SecCoreData
->StackBase
+ SecCoreData
->StackSize
)) \
681 && (*StackPointer
== INIT_CAR_VALUE
);
684 DEBUG ((EFI_D_INFO
, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData
->StackBase
, (UINT32
)SecCoreData
->StackSize
));
685 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
)));
686 DEBUG ((EFI_D_INFO
, "Total temporary memory: %d bytes.\n", (UINT32
)SecCoreData
->TemporaryRamSize
));
687 DEBUG ((EFI_D_INFO
, " temporary memory stack ever used: %d bytes.\n",
688 (UINT32
)(SecCoreData
->StackSize
- ((UINTN
) StackPointer
- (UINTN
)SecCoreData
->StackBase
))
690 DEBUG ((EFI_D_INFO
, " temporary memory heap used: %d bytes.\n",
691 (UINT32
)((UINTN
)Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryBottom
- (UINTN
)Private
->HobList
.Raw
)
695 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0 && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
)) {
697 // Loading Module at Fixed Address is enabled
699 PeiLoadFixAddressHook (Private
);
702 // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.
704 LoadFixPeiCodeBegin
= AllocatePages((UINTN
)PcdGet32(PcdLoadFixAddressPeiCodePageNumber
));
705 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
)));
709 // Reserve the size of new stack at bottom of physical memory
711 // The size of new stack in permenent memory must be the same size
712 // or larger than the size of old stack in temporary memory.
713 // But if new stack is smaller than the size of old stack, we also reserve
714 // the size of old stack at bottom of permenent memory.
716 NewStackSize
= RShiftU64 (Private
->PhysicalMemoryLength
, 1);
717 NewStackSize
= ALIGN_VALUE (NewStackSize
, EFI_PAGE_SIZE
);
718 NewStackSize
= MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize
), NewStackSize
);
719 DEBUG ((EFI_D_INFO
, "Old Stack size %d, New stack size %d\n", (UINT32
)SecCoreData
->StackSize
, (UINT32
)NewStackSize
));
720 ASSERT (NewStackSize
>= SecCoreData
->StackSize
);
723 // Calculate stack offset and heap offset between temporary memory and new permement
724 // memory seperately.
726 TopOfOldStack
= (UINTN
)SecCoreData
->StackBase
+ SecCoreData
->StackSize
;
727 TopOfNewStack
= Private
->PhysicalMemoryBegin
+ NewStackSize
;
728 if (TopOfNewStack
>= TopOfOldStack
) {
729 StackOffsetPositive
= TRUE
;
730 StackOffset
= (UINTN
)(TopOfNewStack
- TopOfOldStack
);
732 StackOffsetPositive
= FALSE
;
733 StackOffset
= (UINTN
)(TopOfOldStack
- TopOfNewStack
);
735 Private
->StackOffsetPositive
= StackOffsetPositive
;
736 Private
->StackOffset
= StackOffset
;
739 // Build Stack HOB that describes the permanent memory stack
741 DEBUG ((EFI_D_INFO
, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack
- NewStackSize
, NewStackSize
));
742 BuildStackHob (TopOfNewStack
- NewStackSize
, NewStackSize
);
745 // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address
747 TemporaryRamBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)SecCoreData
->TemporaryRamBase
;
748 TemporaryRamSize
= SecCoreData
->TemporaryRamSize
;
749 TemporaryStackSize
= SecCoreData
->StackSize
;
750 TemporaryStackBase
= SecCoreData
->StackBase
;
751 PeiTemporaryRamSize
= SecCoreData
->PeiTemporaryRamSize
;
752 PeiTemporaryRamBase
= SecCoreData
->PeiTemporaryRamBase
;
755 // TemporaryRamSupportPpi is produced by platform's SEC
757 Status
= PeiServicesLocatePpi (
758 &gEfiTemporaryRamSupportPpiGuid
,
761 (VOID
**)&TemporaryRamSupportPpi
763 if (!EFI_ERROR (Status
)) {
767 BaseOfNewHeap
= TopOfNewStack
;
768 if (BaseOfNewHeap
>= (UINTN
)SecCoreData
->PeiTemporaryRamBase
) {
769 Private
->HeapOffsetPositive
= TRUE
;
770 Private
->HeapOffset
= (UINTN
)(BaseOfNewHeap
- (UINTN
)SecCoreData
->PeiTemporaryRamBase
);
772 Private
->HeapOffsetPositive
= FALSE
;
773 Private
->HeapOffset
= (UINTN
)((UINTN
)SecCoreData
->PeiTemporaryRamBase
- BaseOfNewHeap
);
776 DEBUG ((EFI_D_INFO
, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64
) Private
->HeapOffset
, (UINT64
) Private
->StackOffset
));
779 // Calculate new HandOffTable and PrivateData address in permanent memory's stack
781 if (StackOffsetPositive
) {
782 SecCoreData
= (CONST EFI_SEC_PEI_HAND_OFF
*)((UINTN
)(VOID
*)SecCoreData
+ StackOffset
);
783 Private
= (PEI_CORE_INSTANCE
*)((UINTN
)(VOID
*)Private
+ StackOffset
);
785 SecCoreData
= (CONST EFI_SEC_PEI_HAND_OFF
*)((UINTN
)(VOID
*)SecCoreData
- StackOffset
);
786 Private
= (PEI_CORE_INSTANCE
*)((UINTN
)(VOID
*)Private
- StackOffset
);
790 // Temporary Ram Support PPI is provided by platform, it will copy
791 // temporary memory to permenent memory and do stack switching.
792 // After invoking Temporary Ram Support PPI, the following code's
793 // stack is in permanent memory.
795 TemporaryRamSupportPpi
->TemporaryRamMigration (
798 (EFI_PHYSICAL_ADDRESS
)(UINTN
)(TopOfNewStack
- TemporaryStackSize
),
805 PeiCore (SecCoreData
, NULL
, Private
);
808 // Migrate the PEI Services Table pointer from temporary RAM to permanent RAM.
810 MigratePeiServicesTablePointer ();
815 BaseOfNewHeap
= TopOfNewStack
;
816 HoleMemBase
= TopOfNewStack
;
817 HoleMemSize
= TemporaryRamSize
- PeiTemporaryRamSize
- TemporaryStackSize
;
818 if (HoleMemSize
!= 0) {
820 // Make sure HOB List start address is 8 byte alignment.
822 BaseOfNewHeap
= ALIGN_VALUE (BaseOfNewHeap
+ HoleMemSize
, 8);
824 if (BaseOfNewHeap
>= (UINTN
)SecCoreData
->PeiTemporaryRamBase
) {
825 Private
->HeapOffsetPositive
= TRUE
;
826 Private
->HeapOffset
= (UINTN
)(BaseOfNewHeap
- (UINTN
)SecCoreData
->PeiTemporaryRamBase
);
828 Private
->HeapOffsetPositive
= FALSE
;
829 Private
->HeapOffset
= (UINTN
)((UINTN
)SecCoreData
->PeiTemporaryRamBase
- BaseOfNewHeap
);
832 DEBUG ((EFI_D_INFO
, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64
) Private
->HeapOffset
, (UINT64
) Private
->StackOffset
));
837 HeapTemporaryRamSize
= (UINTN
) (Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryBottom
- Private
->HobList
.HandoffInformationTable
->EfiMemoryBottom
);
838 ASSERT (BaseOfNewHeap
+ HeapTemporaryRamSize
<= Private
->FreePhysicalMemoryTop
);
839 CopyMem ((UINT8
*) (UINTN
) BaseOfNewHeap
, (UINT8
*) PeiTemporaryRamBase
, HeapTemporaryRamSize
);
844 CopyMem ((UINT8
*) (UINTN
) (TopOfNewStack
- TemporaryStackSize
), TemporaryStackBase
, TemporaryStackSize
);
847 // Copy Hole Range Data
848 // Convert PPI from Hole.
850 if (HoleMemSize
!= 0) {
854 if (PeiTemporaryRamBase
< TemporaryStackBase
) {
855 TempBase1
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) PeiTemporaryRamBase
;
856 TempSize1
= PeiTemporaryRamSize
;
857 TempBase2
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) TemporaryStackBase
;
858 TempSize2
= TemporaryStackSize
;
860 TempBase1
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) TemporaryStackBase
;
861 TempSize1
= TemporaryStackSize
;
862 TempBase2
=(EFI_PHYSICAL_ADDRESS
) (UINTN
) PeiTemporaryRamBase
;
863 TempSize2
= PeiTemporaryRamSize
;
865 if (TemporaryRamBase
< TempBase1
) {
866 Private
->HoleData
[0].Base
= TemporaryRamBase
;
867 Private
->HoleData
[0].Size
= (UINTN
) (TempBase1
- TemporaryRamBase
);
869 if (TempBase1
+ TempSize1
< TempBase2
) {
870 Private
->HoleData
[1].Base
= TempBase1
+ TempSize1
;
871 Private
->HoleData
[1].Size
= (UINTN
) (TempBase2
- TempBase1
- TempSize1
);
873 if (TempBase2
+ TempSize2
< TemporaryRamBase
+ TemporaryRamSize
) {
874 Private
->HoleData
[2].Base
= TempBase2
+ TempSize2
;
875 Private
->HoleData
[2].Size
= (UINTN
) (TemporaryRamBase
+ TemporaryRamSize
- TempBase2
- TempSize2
);
879 // Copy Hole Range data.
881 for (Index
= 0; Index
< HOLE_MAX_NUMBER
; Index
++) {
882 if (Private
->HoleData
[Index
].Size
> 0) {
883 if (HoleMemBase
> Private
->HoleData
[Index
].Base
) {
884 Private
->HoleData
[Index
].OffsetPositive
= TRUE
;
885 Private
->HoleData
[Index
].Offset
= (UINTN
) (HoleMemBase
- Private
->HoleData
[Index
].Base
);
887 Private
->HoleData
[Index
].OffsetPositive
= FALSE
;
888 Private
->HoleData
[Index
].Offset
= (UINTN
) (Private
->HoleData
[Index
].Base
- HoleMemBase
);
890 CopyMem ((VOID
*) (UINTN
) HoleMemBase
, (VOID
*) (UINTN
) Private
->HoleData
[Index
].Base
, Private
->HoleData
[Index
].Size
);
891 HoleMemBase
= HoleMemBase
+ Private
->HoleData
[Index
].Size
;
900 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)PeiCoreEntry
,
901 (VOID
*) SecCoreData
,
903 (VOID
*) (UINTN
) TopOfNewStack
908 // Code should not come here
915 Conduct PEIM dispatch.
917 @param SecCoreData Points to a data structure containing information about the PEI core's operating
918 environment, such as the size and location of temporary RAM, the stack location and
920 @param Private Pointer to the private data passed in from caller
925 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
926 IN PEI_CORE_INSTANCE
*Private
932 CONST EFI_PEI_SERVICES
**PeiServices
;
933 EFI_PEI_FILE_HANDLE PeimFileHandle
;
936 UINT32 AuthenticationState
;
937 EFI_PHYSICAL_ADDRESS EntryPoint
;
938 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint
;
939 UINTN SaveCurrentPeimCount
;
940 UINTN SaveCurrentFvCount
;
941 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle
;
942 EFI_FV_FILE_INFO FvFileInfo
;
943 PEI_CORE_FV_HANDLE
*CoreFvHandle
;
945 PeiServices
= (CONST EFI_PEI_SERVICES
**) &Private
->Ps
;
946 PeimEntryPoint
= NULL
;
947 PeimFileHandle
= NULL
;
950 if ((Private
->PeiMemoryInstalled
) && (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
|| PcdGetBool (PcdShadowPeimOnS3Boot
))) {
952 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile
953 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.
955 SaveCurrentPeimCount
= Private
->CurrentPeimCount
;
956 SaveCurrentFvCount
= Private
->CurrentPeimFvCount
;
957 SaveCurrentFileHandle
= Private
->CurrentFileHandle
;
959 for (Index1
= 0; Index1
<= SaveCurrentFvCount
; Index1
++) {
960 for (Index2
= 0; (Index2
< PcdGet32 (PcdPeiCoreMaxPeimPerFv
)) && (Private
->Fv
[Index1
].FvFileHandles
[Index2
] != NULL
); Index2
++) {
961 if (Private
->Fv
[Index1
].PeimState
[Index2
] == PEIM_STATE_REGISITER_FOR_SHADOW
) {
962 PeimFileHandle
= Private
->Fv
[Index1
].FvFileHandles
[Index2
];
963 Private
->CurrentFileHandle
= PeimFileHandle
;
964 Private
->CurrentPeimFvCount
= Index1
;
965 Private
->CurrentPeimCount
= Index2
;
966 Status
= PeiLoadImage (
967 (CONST EFI_PEI_SERVICES
**) &Private
->Ps
,
969 PEIM_STATE_REGISITER_FOR_SHADOW
,
973 if (Status
== EFI_SUCCESS
) {
975 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
977 Private
->Fv
[Index1
].PeimState
[Index2
]++;
979 // Call the PEIM entry point
981 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT2
)(UINTN
)EntryPoint
;
983 PERF_START (PeimFileHandle
, "PEIM", NULL
, 0);
984 PeimEntryPoint(PeimFileHandle
, (const EFI_PEI_SERVICES
**) &Private
->Ps
);
985 PERF_END (PeimFileHandle
, "PEIM", NULL
, 0);
989 // Process the Notify list and dispatch any notifies for
990 // newly installed PPIs.
992 ProcessNotifyList (Private
);
996 Private
->CurrentFileHandle
= SaveCurrentFileHandle
;
997 Private
->CurrentPeimFvCount
= SaveCurrentFvCount
;
998 Private
->CurrentPeimCount
= SaveCurrentPeimCount
;
1002 // This is the main dispatch loop. It will search known FVs for PEIMs and
1003 // attempt to dispatch them. If any PEIM gets dispatched through a single
1004 // pass of the dispatcher, it will start over from the Bfv again to see
1005 // if any new PEIMs dependencies got satisfied. With a well ordered
1006 // FV where PEIMs are found in the order their dependencies are also
1007 // satisfied, this dipatcher should run only once.
1011 // In case that reenter PeiCore happens, the last pass record is still available.
1013 if (!Private
->PeimDispatcherReenter
) {
1014 Private
->PeimNeedingDispatch
= FALSE
;
1015 Private
->PeimDispatchOnThisPass
= FALSE
;
1017 Private
->PeimDispatcherReenter
= FALSE
;
1020 for (FvCount
= Private
->CurrentPeimFvCount
; FvCount
< Private
->FvCount
; FvCount
++) {
1021 CoreFvHandle
= FindNextCoreFvHandle (Private
, FvCount
);
1022 ASSERT (CoreFvHandle
!= NULL
);
1025 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.
1027 if (CoreFvHandle
->FvPpi
== NULL
) {
1031 Private
->CurrentPeimFvCount
= FvCount
;
1033 if (Private
->CurrentPeimCount
== 0) {
1035 // When going through each FV, at first, search Apriori file to
1036 // reorder all PEIMs to ensure the PEIMs in Apriori file to get
1037 // dispatch at first.
1039 DiscoverPeimsAndOrderWithApriori (Private
, CoreFvHandle
);
1043 // Start to dispatch all modules within the current Fv.
1045 for (PeimCount
= Private
->CurrentPeimCount
;
1046 (PeimCount
< PcdGet32 (PcdPeiCoreMaxPeimPerFv
)) && (Private
->CurrentFvFileHandles
[PeimCount
] != NULL
);
1048 Private
->CurrentPeimCount
= PeimCount
;
1049 PeimFileHandle
= Private
->CurrentFileHandle
= Private
->CurrentFvFileHandles
[PeimCount
];
1051 if (Private
->Fv
[FvCount
].PeimState
[PeimCount
] == PEIM_STATE_NOT_DISPATCHED
) {
1052 if (!DepexSatisfied (Private
, PeimFileHandle
, PeimCount
)) {
1053 Private
->PeimNeedingDispatch
= TRUE
;
1055 Status
= CoreFvHandle
->FvPpi
->GetFileInfo (CoreFvHandle
->FvPpi
, PeimFileHandle
, &FvFileInfo
);
1056 ASSERT_EFI_ERROR (Status
);
1057 if (FvFileInfo
.FileType
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
1059 // For Fv type file, Produce new FvInfo PPI and FV hob
1061 Status
= ProcessFvFile (Private
, &Private
->Fv
[FvCount
], PeimFileHandle
);
1062 if (Status
== EFI_SUCCESS
) {
1064 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
1066 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
1067 Private
->PeimDispatchOnThisPass
= TRUE
;
1070 // The related GuidedSectionExtraction/Decompress PPI for the
1071 // encapsulated FV image section may be installed in the rest
1072 // of this do-while loop, so need to make another pass.
1074 Private
->PeimNeedingDispatch
= TRUE
;
1078 // For PEIM driver, Load its entry point
1080 Status
= PeiLoadImage (
1083 PEIM_STATE_NOT_DISPATCHED
,
1085 &AuthenticationState
1087 if (Status
== EFI_SUCCESS
) {
1089 // The PEIM has its dependencies satisfied, and its entry point
1090 // has been found, so invoke it.
1092 PERF_START (PeimFileHandle
, "PEIM", NULL
, 0);
1094 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
1096 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT_BEGIN
),
1097 (VOID
*)(&PeimFileHandle
),
1098 sizeof (PeimFileHandle
)
1101 Status
= VerifyPeim (Private
, CoreFvHandle
->FvHandle
, PeimFileHandle
, AuthenticationState
);
1102 if (Status
!= EFI_SECURITY_VIOLATION
) {
1104 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
1106 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
1108 // Call the PEIM entry point for PEIM driver
1110 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT2
)(UINTN
)EntryPoint
;
1111 PeimEntryPoint (PeimFileHandle
, (const EFI_PEI_SERVICES
**) PeiServices
);
1112 Private
->PeimDispatchOnThisPass
= TRUE
;
1115 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
1117 (EFI_SOFTWARE_PEI_CORE
| EFI_SW_PC_INIT_END
),
1118 (VOID
*)(&PeimFileHandle
),
1119 sizeof (PeimFileHandle
)
1121 PERF_END (PeimFileHandle
, "PEIM", NULL
, 0);
1126 PeiCheckAndSwitchStack (SecCoreData
, Private
);
1129 // Process the Notify list and dispatch any notifies for
1130 // newly installed PPIs.
1132 ProcessNotifyList (Private
);
1135 // Recheck SwitchStackSignal after ProcessNotifyList()
1136 // in case PeiInstallPeiMemory() is done in a callback with
1137 // EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH.
1139 PeiCheckAndSwitchStack (SecCoreData
, Private
);
1141 if ((Private
->PeiMemoryInstalled
) && (Private
->Fv
[FvCount
].PeimState
[PeimCount
] == PEIM_STATE_REGISITER_FOR_SHADOW
) && \
1142 (Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
|| PcdGetBool (PcdShadowPeimOnS3Boot
))) {
1144 // If memory is availble we shadow images by default for performance reasons.
1145 // We call the entry point a 2nd time so the module knows it's shadowed.
1147 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
1148 if ((Private
->HobList
.HandoffInformationTable
->BootMode
!= BOOT_ON_S3_RESUME
) && !PcdGetBool (PcdShadowPeimOnBoot
)) {
1150 // Load PEIM into Memory for Register for shadow PEIM.
1152 Status
= PeiLoadImage (
1155 PEIM_STATE_REGISITER_FOR_SHADOW
,
1157 &AuthenticationState
1159 if (Status
== EFI_SUCCESS
) {
1160 PeimEntryPoint
= (EFI_PEIM_ENTRY_POINT2
)(UINTN
)EntryPoint
;
1163 ASSERT (PeimEntryPoint
!= NULL
);
1164 PeimEntryPoint (PeimFileHandle
, (const EFI_PEI_SERVICES
**) PeiServices
);
1165 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
1168 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
1170 Private
->Fv
[FvCount
].PeimState
[PeimCount
]++;
1173 // Process the Notify list and dispatch any notifies for
1174 // newly installed PPIs.
1176 ProcessNotifyList (Private
);
1183 // We set to NULL here to optimize the 2nd entry to this routine after
1184 // memory is found. This reprevents rescanning of the FV. We set to
1185 // NULL here so we start at the begining of the next FV
1187 Private
->CurrentFileHandle
= NULL
;
1188 Private
->CurrentPeimCount
= 0;
1190 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL
1192 SetMem (Private
->CurrentFvFileHandles
, sizeof (EFI_PEI_FILE_HANDLE
) * PcdGet32 (PcdPeiCoreMaxPeimPerFv
), 0);
1196 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go
1197 // through all the FV.
1199 Private
->CurrentPeimFvCount
= 0;
1202 // PeimNeedingDispatch being TRUE means we found a PEIM/FV that did not get
1203 // dispatched. So we need to make another pass
1205 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM/FV on this
1206 // pass. If we did not dispatch a PEIM/FV there is no point in trying again
1207 // as it will fail the next time too (nothing has changed).
1209 } while (Private
->PeimNeedingDispatch
&& Private
->PeimDispatchOnThisPass
);
1214 Initialize the Dispatcher's data members
1216 @param PrivateData PeiCore's private data structure
1217 @param OldCoreData Old data from SecCore
1218 NULL if being run in non-permament memory mode.
1219 @param SecCoreData Points to a data structure containing information about the PEI core's operating
1220 environment, such as the size and location of temporary RAM, the stack location and
1227 InitializeDispatcherData (
1228 IN PEI_CORE_INSTANCE
*PrivateData
,
1229 IN PEI_CORE_INSTANCE
*OldCoreData
,
1230 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
1233 if (OldCoreData
== NULL
) {
1234 PrivateData
->PeimDispatcherReenter
= FALSE
;
1235 PeiInitializeFv (PrivateData
, SecCoreData
);
1237 PeiReinitializeFv (PrivateData
);
1244 This routine parses the Dependency Expression, if available, and
1245 decides if the module can be executed.
1248 @param Private PeiCore's private data structure
1249 @param FileHandle PEIM's file handle
1250 @param PeimCount Peim count in all dispatched PEIMs.
1252 @retval TRUE Can be dispatched
1253 @retval FALSE Cannot be dispatched
1258 IN PEI_CORE_INSTANCE
*Private
,
1259 IN EFI_PEI_FILE_HANDLE FileHandle
,
1265 EFI_FV_FILE_INFO FileInfo
;
1267 Status
= PeiServicesFfsGetFileInfo (FileHandle
, &FileInfo
);
1268 if (EFI_ERROR (Status
)) {
1269 DEBUG ((DEBUG_DISPATCH
, "Evaluate PEI DEPEX for FFS(Unknown)\n"));
1271 DEBUG ((DEBUG_DISPATCH
, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo
.FileName
));
1274 if (PeimCount
< Private
->AprioriCount
) {
1276 // If its in the A priori file then we set Depex to TRUE
1278 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (Apriori)\n"));
1283 // Depex section not in the encapsulated section.
1285 Status
= PeiServicesFfsFindSectionData (
1286 EFI_SECTION_PEI_DEPEX
,
1291 if (EFI_ERROR (Status
)) {
1293 // If there is no DEPEX, assume the module can be executed
1295 DEBUG ((DEBUG_DISPATCH
, " RESULT = TRUE (No DEPEX)\n"));
1300 // Evaluate a given DEPEX
1302 return PeimDispatchReadiness (&Private
->Ps
, DepexData
);
1306 This routine enable a PEIM to register itself to shadow when PEI Foundation
1307 discovery permanent memory.
1309 @param FileHandle File handle of a PEIM.
1311 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.
1312 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.
1313 @retval EFI_SUCCESS Successfully to register itself.
1318 PeiRegisterForShadow (
1319 IN EFI_PEI_FILE_HANDLE FileHandle
1322 PEI_CORE_INSTANCE
*Private
;
1323 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
1325 if (Private
->CurrentFileHandle
!= FileHandle
) {
1327 // The FileHandle must be for the current PEIM
1329 return EFI_NOT_FOUND
;
1332 if (Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] >= PEIM_STATE_REGISITER_FOR_SHADOW
) {
1334 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started
1336 return EFI_ALREADY_STARTED
;
1339 Private
->Fv
[Private
->CurrentPeimFvCount
].PeimState
[Private
->CurrentPeimCount
] = PEIM_STATE_REGISITER_FOR_SHADOW
;