2 Null instance of Platform Sec Lib.
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/PeiServicesLib.h>
12 #include <Library/PeiServicesTablePointerLib.h>
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/HobLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/FspPlatformInfoLib.h>
20 #include <Guid/GuidHobFsp.h>
21 #include <Guid/MemoryTypeInformation.h>
22 #include <Ppi/Capsule.h>
24 #include <PlatformFspLib.h>
25 #include <Guid/SmramMemoryReserve.h>
26 EFI_GUID gFspReservedMemoryResourceHobTsegGuid
= {0xd038747c, 0xd00c, 0x4980, {0xb3, 0x19, 0x49, 0x01, 0x99, 0xa4, 0x7d, 0x55}};
29 // Additional pages are used by DXE memory manager.
30 // It should be consistent between RetrieveRequiredMemorySize() and GetPeiMemSize()
32 #define PEI_ADDITIONAL_MEMORY_SIZE (16 * EFI_PAGE_SIZE)
35 Get the mem size in memory type infromation table.
37 @param PeiServices PEI Services table.
39 @return the mem size in memory type infromation table.
42 GetMemorySizeInMemoryTypeInformation (
43 IN EFI_PEI_SERVICES
**PeiServices
46 EFI_PEI_HOB_POINTERS Hob
;
47 EFI_MEMORY_TYPE_INFORMATION
*MemoryData
;
52 (*PeiServices
)->GetHobList ((CONST EFI_PEI_SERVICES
**)PeiServices
, (VOID
**) &Hob
.Raw
);
53 while (!END_OF_HOB_LIST (Hob
)) {
54 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_GUID_EXTENSION
&&
55 CompareGuid (&Hob
.Guid
->Name
, &gEfiMemoryTypeInformationGuid
)) {
56 MemoryData
= (EFI_MEMORY_TYPE_INFORMATION
*) (Hob
.Raw
+ sizeof (EFI_HOB_GENERIC_HEADER
) + sizeof (EFI_GUID
));
60 Hob
.Raw
= GET_NEXT_HOB (Hob
);
63 if (MemoryData
== NULL
) {
68 for (Index
= 0; MemoryData
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
70 // Accumulate default memory size requirements
72 TempPageNum
+= MemoryData
[Index
].NumberOfPages
;
75 return TempPageNum
* EFI_PAGE_SIZE
;
79 Get the mem size need to be reserved in PEI phase.
81 @param PeiServices PEI Services table.
83 @return the mem size need to be reserved in PEI phase.
86 RetrieveRequiredMemorySize (
87 IN EFI_PEI_SERVICES
**PeiServices
92 Size
= GetMemorySizeInMemoryTypeInformation (PeiServices
);
93 return Size
+ PEI_ADDITIONAL_MEMORY_SIZE
;
97 Get the mem size need to be consumed and reserved in PEI phase.
99 @param PeiServices PEI Services table.
100 @param BootMode Current boot mode.
102 @return the mem size need to be consumed and reserved in PEI phase.
106 IN EFI_PEI_SERVICES
**PeiServices
,
113 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
114 return PcdGet32 (PcdPeiRecoveryMinMemSize
);
117 Size
= GetMemorySizeInMemoryTypeInformation (PeiServices
);
119 if (BootMode
== BOOT_ON_FLASH_UPDATE
) {
121 // Maybe more size when in CapsuleUpdate phase ?
123 MinSize
= PcdGet32 (PcdPeiMinMemSize
);
125 MinSize
= PcdGet32 (PcdPeiMinMemSize
);
128 return MinSize
+ Size
+ PEI_ADDITIONAL_MEMORY_SIZE
;
132 BIOS process FspBobList.
134 @param FspHobList Pointer to the HOB data structure produced by FSP.
136 @return If platform process the FSP hob list successfully.
140 FspHobProcessForMemoryResource (
144 EFI_PEI_HOB_POINTERS Hob
;
145 UINT64 LowMemorySize
;
146 UINT64 FspMemorySize
;
147 EFI_PHYSICAL_ADDRESS FspMemoryBase
;
149 EFI_PHYSICAL_ADDRESS PeiMemBase
;
151 EFI_PHYSICAL_ADDRESS S3PeiMemBase
;
152 BOOLEAN FoundFspMemHob
;
154 EFI_BOOT_MODE BootMode
;
155 PEI_CAPSULE_PPI
*Capsule
;
157 UINTN CapsuleBufferLength
;
158 UINT64 RequiredMemSize
;
159 EFI_PEI_SERVICES
**PeiServices
;
161 EFI_PHYSICAL_ADDRESS TsegBase
;
162 BOOLEAN FoundTsegHob
;
164 PeiServices
= (EFI_PEI_SERVICES
**)GetPeiServicesTablePointer ();
166 PeiServicesGetBootMode (&BootMode
);
172 FoundFspMemHob
= FALSE
;
175 FoundTsegHob
= FALSE
;
178 // Parse the hob list from fsp
179 // Report all the resource hob except the memory between 1M and 4G
181 Hob
.Raw
= (UINT8
*)(UINTN
)FspHobList
;
182 DEBUG((DEBUG_INFO
, "FspHobList - 0x%x\n", FspHobList
));
184 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, Hob
.Raw
)) != NULL
) {
185 DEBUG((DEBUG_INFO
, "\nResourceType: 0x%x\n", Hob
.ResourceDescriptor
->ResourceType
));
186 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) ||
187 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
)) {
188 DEBUG((DEBUG_INFO
, "ResourceAttribute: 0x%x\n", Hob
.ResourceDescriptor
->ResourceAttribute
));
189 DEBUG((DEBUG_INFO
, "PhysicalStart: 0x%x\n", Hob
.ResourceDescriptor
->PhysicalStart
));
190 DEBUG((DEBUG_INFO
, "ResourceLength: 0x%x\n", Hob
.ResourceDescriptor
->ResourceLength
));
191 DEBUG((DEBUG_INFO
, "Owner: %g\n\n", &Hob
.ResourceDescriptor
->Owner
));
194 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) // Found the low memory length below 4G
195 && (Hob
.ResourceDescriptor
->PhysicalStart
>= BASE_1MB
)
196 && (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
<= BASE_4GB
)) {
197 LowMemorySize
+= Hob
.ResourceDescriptor
->ResourceLength
;
198 Hob
.Raw
= GET_NEXT_HOB (Hob
);
202 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
) // Found the low memory length below 4G
203 && (Hob
.ResourceDescriptor
->PhysicalStart
>= BASE_1MB
)
204 && (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
<= BASE_4GB
)
205 && (CompareGuid (&Hob
.ResourceDescriptor
->Owner
, &gFspReservedMemoryResourceHobGuid
))) {
206 FoundFspMemHob
= TRUE
;
207 FspMemoryBase
= Hob
.ResourceDescriptor
->PhysicalStart
;
208 FspMemorySize
= Hob
.ResourceDescriptor
->ResourceLength
;
209 DEBUG((DEBUG_INFO
, "Find fsp mem hob, base 0x%x, len 0x%x\n", FspMemoryBase
, FspMemorySize
));
212 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
) // Found the low memory length below 4G
213 && (Hob
.ResourceDescriptor
->PhysicalStart
>= 0x100000)
214 && (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
<= 0x100000000)
215 && (CompareGuid (&Hob
.ResourceDescriptor
->Owner
, &gFspReservedMemoryResourceHobTsegGuid
))) {
217 TsegBase
= Hob
.ResourceDescriptor
->PhysicalStart
;
220 if ((Hob
.ResourceDescriptor
->ResourceLength
== 0 ) || (Hob
.ResourceDescriptor
->ResourceLength
> 0x800000)){
221 Hob
.ResourceDescriptor
->ResourceLength
= 0x800000;
225 TsegSize
= Hob
.ResourceDescriptor
->ResourceLength
;
226 DEBUG((EFI_D_ERROR
, "Find Tseg mem hob, base 0x%lx, len 0x%lx\n", TsegBase
, TsegSize
));
230 // Report the resource hob
232 BuildResourceDescriptorHob (
233 Hob
.ResourceDescriptor
->ResourceType
,
234 Hob
.ResourceDescriptor
->ResourceAttribute
,
235 Hob
.ResourceDescriptor
->PhysicalStart
,
236 Hob
.ResourceDescriptor
->ResourceLength
239 Hob
.Raw
= GET_NEXT_HOB (Hob
);
242 if (!FoundFspMemHob
) {
243 DEBUG((DEBUG_INFO
, "Didn't find the fsp used memory information.\n"));
247 DEBUG((DEBUG_INFO
, "LowMemorySize: 0x%x.\n", LowMemorySize
));
248 DEBUG((DEBUG_INFO
, "FspMemoryBase: 0x%x.\n", FspMemoryBase
));
249 DEBUG((DEBUG_INFO
, "FspMemorySize: 0x%x.\n", FspMemorySize
));
251 if (BootMode
== BOOT_ON_S3_RESUME
) {
252 BuildResourceDescriptorHob (
253 EFI_RESOURCE_SYSTEM_MEMORY
,
255 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
256 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
257 // EFI_RESOURCE_ATTRIBUTE_TESTED |
258 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
259 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
260 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
261 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
267 Status
= GetS3MemoryInfo (&S3PeiMemBase
, &S3PeiMemSize
);
268 ASSERT_EFI_ERROR (Status
);
269 DEBUG((DEBUG_INFO
, "S3 memory %Xh - %Xh bytes\n", S3PeiMemBase
, S3PeiMemSize
));
272 // Make sure Stack and PeiMemory are not overlap - JYAO1
275 Status
= PeiServicesInstallPeiMemory (
279 ASSERT_EFI_ERROR (Status
);
281 PeiMemSize
= GetPeiMemSize (PeiServices
, BootMode
);
282 DEBUG((DEBUG_INFO
, "PEI memory size = %Xh bytes\n", PeiMemSize
));
288 CapsuleBuffer
= NULL
;
289 CapsuleBufferLength
= 0;
290 if (BootMode
== BOOT_ON_FLASH_UPDATE
) {
291 Status
= PeiServicesLocatePpi (
297 ASSERT_EFI_ERROR (Status
);
299 if (Status
== EFI_SUCCESS
) {
301 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
303 CapsuleBuffer
= (VOID
*)(UINTN
)BASE_1MB
;
304 CapsuleBufferLength
= (UINTN
)(LowMemorySize
- PeiMemSize
);
306 // Call the Capsule PPI Coalesce function to coalesce the capsule data.
308 Status
= Capsule
->Coalesce (PeiServices
, &CapsuleBuffer
, &CapsuleBufferLength
);
312 RequiredMemSize
= RetrieveRequiredMemorySize (PeiServices
);
313 DEBUG((DEBUG_INFO
, "Required memory size = %Xh bytes\n", RequiredMemSize
));
316 // Report the main memory
318 BuildResourceDescriptorHob (
319 EFI_RESOURCE_SYSTEM_MEMORY
,
321 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
322 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
323 EFI_RESOURCE_ATTRIBUTE_TESTED
|
324 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
325 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
326 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
327 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
334 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
338 // Install efi memory
340 PeiMemBase
= BASE_1MB
+ LowMemorySize
- PeiMemSize
;
341 Status
= PeiServicesInstallPeiMemory (
343 PeiMemSize
- RequiredMemSize
345 ASSERT_EFI_ERROR (Status
);
347 if (Capsule
!= NULL
) {
348 Status
= Capsule
->CreateState (PeiServices
, CapsuleBuffer
, CapsuleBufferLength
);
353 // Report GUIDed HOB for reserving SMRAM regions
356 EFI_SMRAM_HOB_DESCRIPTOR_BLOCK
*SmramHobDescriptorBlock
;
358 SmramHobDescriptorBlock
= BuildGuidHob (
359 &gEfiSmmPeiSmramMemoryReserveGuid
,
360 sizeof (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK
)
362 ASSERT (SmramHobDescriptorBlock
!= NULL
);
364 SmramHobDescriptorBlock
->NumberOfSmmReservedRegions
= 1;
366 SmramHobDescriptorBlock
->Descriptor
[0].PhysicalStart
= TsegBase
;
367 SmramHobDescriptorBlock
->Descriptor
[0].CpuStart
= TsegBase
;
368 SmramHobDescriptorBlock
->Descriptor
[0].PhysicalSize
= TsegSize
;
369 SmramHobDescriptorBlock
->Descriptor
[0].RegionState
= EFI_SMRAM_CLOSED
;
375 BIOS process FspBobList for other data (not Memory Resource Descriptor).
377 @param[in] FspHobList Pointer to the HOB data structure produced by FSP.
379 @return If platform process the FSP hob list successfully.
383 FspHobProcessForOtherData (
387 EFI_PEI_SERVICES
**PeiServices
;
389 PeiServices
= (EFI_PEI_SERVICES
**)GetPeiServicesTablePointer ();
392 // Other hob for platform
394 PlatformHobCreateFromFsp ((CONST EFI_PEI_SERVICES
**) PeiServices
, FspHobList
);
400 BIOS process FspBobList.
402 @param[in] FspHobList Pointer to the HOB data structure produced by FSP.
404 @return If platform process the FSP hob list successfully.
414 Status
= FspHobProcessForMemoryResource (FspHobList
);
415 if (EFI_ERROR (Status
)) {
418 Status
= FspHobProcessForOtherData (FspHobList
);