2 Null instance of Platform Sec Lib.
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
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.
17 #include <Library/PeiServicesLib.h>
18 #include <Library/PeiServicesTablePointerLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/FspPlatformInfoLib.h>
26 #include <Guid/GuidHobFsp.h>
27 #include <Guid/MemoryTypeInformation.h>
28 #include <Ppi/Capsule.h>
30 #include <PlatformFspLib.h>
31 #include <Guid/SmramMemoryReserve.h>
32 EFI_GUID gFspReservedMemoryResourceHobTsegGuid
= {0xd038747c, 0xd00c, 0x4980, {0xb3, 0x19, 0x49, 0x01, 0x99, 0xa4, 0x7d, 0x55}};
35 // Additional pages are used by DXE memory manager.
36 // It should be consistent between RetrieveRequiredMemorySize() and GetPeiMemSize()
38 #define PEI_ADDITIONAL_MEMORY_SIZE (16 * EFI_PAGE_SIZE)
41 Get the mem size in memory type infromation table.
43 @param PeiServices PEI Services table.
45 @return the mem size in memory type infromation table.
48 GetMemorySizeInMemoryTypeInformation (
49 IN EFI_PEI_SERVICES
**PeiServices
53 EFI_PEI_HOB_POINTERS Hob
;
54 EFI_MEMORY_TYPE_INFORMATION
*MemoryData
;
59 Status
= (*PeiServices
)->GetHobList (PeiServices
, (VOID
**) &Hob
.Raw
);
60 while (!END_OF_HOB_LIST (Hob
)) {
61 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_GUID_EXTENSION
&&
62 CompareGuid (&Hob
.Guid
->Name
, &gEfiMemoryTypeInformationGuid
)) {
63 MemoryData
= (EFI_MEMORY_TYPE_INFORMATION
*) (Hob
.Raw
+ sizeof (EFI_HOB_GENERIC_HEADER
) + sizeof (EFI_GUID
));
67 Hob
.Raw
= GET_NEXT_HOB (Hob
);
70 if (MemoryData
== NULL
) {
75 for (Index
= 0; MemoryData
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
77 // Accumulate default memory size requirements
79 TempPageNum
+= MemoryData
[Index
].NumberOfPages
;
82 return TempPageNum
* EFI_PAGE_SIZE
;
86 Get the mem size need to be reserved in PEI phase.
88 @param PeiServices PEI Services table.
90 @return the mem size need to be reserved in PEI phase.
93 RetrieveRequiredMemorySize (
94 IN EFI_PEI_SERVICES
**PeiServices
99 Size
= GetMemorySizeInMemoryTypeInformation (PeiServices
);
100 return Size
+ PEI_ADDITIONAL_MEMORY_SIZE
;
104 Get the mem size need to be consumed and reserved in PEI phase.
106 @param PeiServices PEI Services table.
107 @param BootMode Current boot mode.
109 @return the mem size need to be consumed and reserved in PEI phase.
113 IN EFI_PEI_SERVICES
**PeiServices
,
120 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
121 return PcdGet32 (PcdPeiRecoveryMinMemSize
);
124 Size
= GetMemorySizeInMemoryTypeInformation (PeiServices
);
126 if (BootMode
== BOOT_ON_FLASH_UPDATE
) {
128 // Maybe more size when in CapsuleUpdate phase ?
130 MinSize
= PcdGet32 (PcdPeiMinMemSize
);
132 MinSize
= PcdGet32 (PcdPeiMinMemSize
);
135 return MinSize
+ Size
+ PEI_ADDITIONAL_MEMORY_SIZE
;
139 BIOS process FspBobList.
141 @param FspHobList Pointer to the HOB data structure produced by FSP.
143 @return If platform process the FSP hob list successfully.
151 EFI_PEI_HOB_POINTERS Hob
;
152 UINT64 LowMemorySize
;
153 UINT64 FspMemorySize
;
154 EFI_PHYSICAL_ADDRESS FspMemoryBase
;
156 EFI_PHYSICAL_ADDRESS PeiMemBase
;
158 EFI_PHYSICAL_ADDRESS S3PeiMemBase
;
159 BOOLEAN FoundFspMemHob
;
161 EFI_BOOT_MODE BootMode
;
162 PEI_CAPSULE_PPI
*Capsule
;
164 UINTN CapsuleBufferLength
;
165 UINT64 RequiredMemSize
;
166 EFI_PEI_SERVICES
**PeiServices
;
168 EFI_PHYSICAL_ADDRESS TsegBase
;
169 BOOLEAN FoundTsegHob
;
171 PeiServices
= (EFI_PEI_SERVICES
**)GetPeiServicesTablePointer ();
173 PeiServicesGetBootMode (&BootMode
);
179 FoundFspMemHob
= FALSE
;
182 FoundTsegHob
= FALSE
;
185 // Parse the hob list from fsp
186 // Report all the resource hob except the memory between 1M and 4G
188 Hob
.Raw
= (UINT8
*)(UINTN
)FspHobList
;
189 DEBUG((DEBUG_INFO
, "FspHobList - 0x%x\n", FspHobList
));
191 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, Hob
.Raw
)) != NULL
) {
192 DEBUG((DEBUG_INFO
, "\nResourceType: 0x%x\n", Hob
.ResourceDescriptor
->ResourceType
));
193 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) ||
194 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
)) {
195 DEBUG((DEBUG_INFO
, "ResourceAttribute: 0x%x\n", Hob
.ResourceDescriptor
->ResourceAttribute
));
196 DEBUG((DEBUG_INFO
, "PhysicalStart: 0x%x\n", Hob
.ResourceDescriptor
->PhysicalStart
));
197 DEBUG((DEBUG_INFO
, "ResourceLength: 0x%x\n", Hob
.ResourceDescriptor
->ResourceLength
));
198 DEBUG((DEBUG_INFO
, "Owner: %g\n\n", &Hob
.ResourceDescriptor
->Owner
));
201 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) // Found the low memory length below 4G
202 && (Hob
.ResourceDescriptor
->PhysicalStart
>= BASE_1MB
)
203 && (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
<= BASE_4GB
)) {
204 LowMemorySize
+= Hob
.ResourceDescriptor
->ResourceLength
;
205 Hob
.Raw
= GET_NEXT_HOB (Hob
);
209 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
) // Found the low memory length below 4G
210 && (Hob
.ResourceDescriptor
->PhysicalStart
>= BASE_1MB
)
211 && (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
<= BASE_4GB
)
212 && (CompareGuid (&Hob
.ResourceDescriptor
->Owner
, &gFspReservedMemoryResourceHobGuid
))) {
213 FoundFspMemHob
= TRUE
;
214 FspMemoryBase
= Hob
.ResourceDescriptor
->PhysicalStart
;
215 FspMemorySize
= Hob
.ResourceDescriptor
->ResourceLength
;
216 DEBUG((DEBUG_INFO
, "Find fsp mem hob, base 0x%x, len 0x%x\n", FspMemoryBase
, FspMemorySize
));
219 if ((Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
) // Found the low memory length below 4G
220 && (Hob
.ResourceDescriptor
->PhysicalStart
>= 0x100000)
221 && (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
<= 0x100000000)
222 && (CompareGuid (&Hob
.ResourceDescriptor
->Owner
, &gFspReservedMemoryResourceHobTsegGuid
))) {
224 TsegBase
= Hob
.ResourceDescriptor
->PhysicalStart
;
227 if ((Hob
.ResourceDescriptor
->ResourceLength
== 0 ) || (Hob
.ResourceDescriptor
->ResourceLength
> 0x800000)){
228 Hob
.ResourceDescriptor
->ResourceLength
= 0x800000;
232 TsegSize
= Hob
.ResourceDescriptor
->ResourceLength
;
233 DEBUG((EFI_D_ERROR
, "Find Tseg mem hob, base 0x%lx, len 0x%lx\n", TsegBase
, TsegSize
));
237 // Report the resource hob
239 BuildResourceDescriptorHob (
240 Hob
.ResourceDescriptor
->ResourceType
,
241 Hob
.ResourceDescriptor
->ResourceAttribute
,
242 Hob
.ResourceDescriptor
->PhysicalStart
,
243 Hob
.ResourceDescriptor
->ResourceLength
246 Hob
.Raw
= GET_NEXT_HOB (Hob
);
249 if (!FoundFspMemHob
) {
250 DEBUG((DEBUG_INFO
, "Didn't find the fsp used memory information.\n"));
254 DEBUG((DEBUG_INFO
, "LowMemorySize: 0x%x.\n", LowMemorySize
));
255 DEBUG((DEBUG_INFO
, "FspMemoryBase: 0x%x.\n", FspMemoryBase
));
256 DEBUG((DEBUG_INFO
, "FspMemorySize: 0x%x.\n", FspMemorySize
));
258 if (BootMode
== BOOT_ON_S3_RESUME
) {
259 BuildResourceDescriptorHob (
260 EFI_RESOURCE_SYSTEM_MEMORY
,
262 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
263 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
264 // EFI_RESOURCE_ATTRIBUTE_TESTED |
265 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
266 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
267 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
268 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
274 Status
= GetS3MemoryInfo (&S3PeiMemBase
, &S3PeiMemSize
);
275 ASSERT_EFI_ERROR (Status
);
276 DEBUG((DEBUG_INFO
, "S3 memory %Xh - %Xh bytes\n", S3PeiMemBase
, S3PeiMemSize
));
279 // Make sure Stack and PeiMemory are not overlap - JYAO1
282 Status
= PeiServicesInstallPeiMemory (
286 ASSERT_EFI_ERROR (Status
);
288 PeiMemSize
= GetPeiMemSize (PeiServices
, BootMode
);
289 DEBUG((DEBUG_INFO
, "PEI memory size = %Xh bytes\n", PeiMemSize
));
295 CapsuleBuffer
= NULL
;
296 CapsuleBufferLength
= 0;
297 if (BootMode
== BOOT_ON_FLASH_UPDATE
) {
298 Status
= PeiServicesLocatePpi (
304 ASSERT_EFI_ERROR (Status
);
306 if (Status
== EFI_SUCCESS
) {
308 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
310 CapsuleBuffer
= (VOID
*)(UINTN
)BASE_1MB
;
311 CapsuleBufferLength
= (UINTN
)(LowMemorySize
- PeiMemSize
);
313 // Call the Capsule PPI Coalesce function to coalesce the capsule data.
315 Status
= Capsule
->Coalesce (PeiServices
, &CapsuleBuffer
, &CapsuleBufferLength
);
319 RequiredMemSize
= RetrieveRequiredMemorySize (PeiServices
);
320 DEBUG((DEBUG_INFO
, "Required memory size = %Xh bytes\n", RequiredMemSize
));
323 // Report the main memory
325 BuildResourceDescriptorHob (
326 EFI_RESOURCE_SYSTEM_MEMORY
,
328 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
329 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
330 EFI_RESOURCE_ATTRIBUTE_TESTED
|
331 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
332 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
333 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
334 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
341 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
345 // Install efi memory
347 PeiMemBase
= BASE_1MB
+ LowMemorySize
- PeiMemSize
;
348 Status
= PeiServicesInstallPeiMemory (
350 PeiMemSize
- RequiredMemSize
352 ASSERT_EFI_ERROR (Status
);
354 if (Capsule
!= NULL
) {
355 Status
= Capsule
->CreateState (PeiServices
, CapsuleBuffer
, CapsuleBufferLength
);
360 // Report GUIDed HOB for reserving SMRAM regions
363 EFI_SMRAM_HOB_DESCRIPTOR_BLOCK
*SmramHobDescriptorBlock
;
365 SmramHobDescriptorBlock
= BuildGuidHob (
366 &gEfiSmmPeiSmramMemoryReserveGuid
,
367 sizeof (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK
)
369 ASSERT (SmramHobDescriptorBlock
!= NULL
);
371 SmramHobDescriptorBlock
->NumberOfSmmReservedRegions
= 1;
373 SmramHobDescriptorBlock
->Descriptor
[0].PhysicalStart
= TsegBase
;
374 SmramHobDescriptorBlock
->Descriptor
[0].CpuStart
= TsegBase
;
375 SmramHobDescriptorBlock
->Descriptor
[0].PhysicalSize
= TsegSize
;
376 SmramHobDescriptorBlock
->Descriptor
[0].RegionState
= EFI_SMRAM_CLOSED
;
382 // Other hob for platform
384 PlatformHobCreateFromFsp ( PeiServices
, FspHobList
);