BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / IntelFspPkg / Library / BaseFspPlatformLib / FspPlatformMemory.c
1 /** @file
2
3 Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include <PiPei.h>
15 #include <Library/BaseLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/HobLib.h>
21 #include <Library/PeiServicesLib.h>
22 #include <Library/FspCommonLib.h>
23 #include <Guid/GuidHobFsp.h>
24 #include <FspGlobalData.h>
25 #include <FspApi.h>
26
27 /**
28 Get system memory from HOB.
29
30 @param[in,out] LowMemoryLength less than 4G memory length
31 @param[in,out] HighMemoryLength greater than 4G memory length
32 **/
33 VOID
34 EFIAPI
35 FspGetSystemMemorySize (
36 IN OUT UINT64 *LowMemoryLength,
37 IN OUT UINT64 *HighMemoryLength
38 )
39 {
40 EFI_PEI_HOB_POINTERS Hob;
41
42 *HighMemoryLength = 0;
43 *LowMemoryLength = SIZE_1MB;
44 //
45 // Get the HOB list for processing
46 //
47 Hob.Raw = GetHobList ();
48
49 //
50 // Collect memory ranges
51 //
52 while (!END_OF_HOB_LIST (Hob)) {
53 if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
54 if (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
55 //
56 // Need memory above 1MB to be collected here
57 //
58 if (Hob.ResourceDescriptor->PhysicalStart >= BASE_1MB &&
59 Hob.ResourceDescriptor->PhysicalStart < (EFI_PHYSICAL_ADDRESS) BASE_4GB) {
60 *LowMemoryLength += (UINT64) (Hob.ResourceDescriptor->ResourceLength);
61 } else if (Hob.ResourceDescriptor->PhysicalStart >= (EFI_PHYSICAL_ADDRESS) BASE_4GB) {
62 *HighMemoryLength += (UINT64) (Hob.ResourceDescriptor->ResourceLength);
63 }
64 }
65 }
66 Hob.Raw = GET_NEXT_HOB (Hob);
67 }
68 }
69
70 /**
71 Migrate BootLoader data before destroying CAR.
72
73 **/
74 VOID
75 EFIAPI
76 FspMigrateTemporaryMemory (
77 VOID
78 )
79 {
80 FSP_INIT_RT_COMMON_BUFFER *FspInitRtBuffer;
81 UINT32 BootLoaderTempRamStart;
82 UINT32 BootLoaderTempRamEnd;
83 UINT32 BootLoaderTempRamSize;
84 UINT32 OffsetGap;
85 UINT32 FspParamPtr;
86 FSP_INIT_PARAMS *FspInitParams;
87 UINT32 *NewStackTop;
88 VOID *BootLoaderTempRamHob;
89 UINT32 UpdDataRgnPtr;
90 UINT32 MemoryInitUpdPtr;
91 UINT32 SiliconInitUpdPtr;
92 VOID *PlatformDataPtr;
93 UINT8 ApiMode;
94
95 ApiMode = GetFspApiCallingMode ();
96
97 //
98 // Get the temporary memory range used by the BootLoader
99 //
100 BootLoaderTempRamStart = PcdGet32(PcdTemporaryRamBase);
101 BootLoaderTempRamSize = PcdGet32(PcdTemporaryRamSize) - PcdGet32(PcdFspTemporaryRamSize);
102 BootLoaderTempRamEnd = BootLoaderTempRamStart + BootLoaderTempRamSize;
103
104 //
105 // Build a Boot Loader Temporary Memory GUID HOB
106 //
107 if (ApiMode == 0) {
108 BootLoaderTempRamHob = BuildGuidHob (&gFspBootLoaderTemporaryMemoryGuid, BootLoaderTempRamSize);
109 } else {
110 BootLoaderTempRamHob = (VOID *)AllocatePages (EFI_SIZE_TO_PAGES (BootLoaderTempRamSize));
111 }
112 ASSERT(BootLoaderTempRamHob != NULL);
113
114 CopyMem (BootLoaderTempRamHob, (VOID *)BootLoaderTempRamStart, BootLoaderTempRamSize);
115 OffsetGap = (UINT32)BootLoaderTempRamHob - BootLoaderTempRamStart;
116
117 //
118 // Set a new stack frame for the continuation function
119 //
120 if (ApiMode == 0) {
121 FspInitParams = (FSP_INIT_PARAMS *)GetFspApiParameter ();
122 FspInitRtBuffer = (FSP_INIT_RT_COMMON_BUFFER *)FspInitParams->RtBufferPtr;
123 NewStackTop = (UINT32 *)FspInitRtBuffer->StackTop - 1;
124 SetFspCoreStackPointer (NewStackTop);
125 }
126
127 //
128 // Fix the FspInit Parameter Pointers to the new location.
129 //
130 FspParamPtr = GetFspApiParameter ();
131 if (FspParamPtr >= BootLoaderTempRamStart && FspParamPtr < BootLoaderTempRamEnd) {
132 SetFspApiParameter(FspParamPtr + OffsetGap);
133 }
134
135 FspInitParams = (FSP_INIT_PARAMS *)GetFspApiParameter ();
136 if ((UINT32)(FspInitParams->RtBufferPtr) >= BootLoaderTempRamStart &&
137 (UINT32)(FspInitParams->RtBufferPtr) < BootLoaderTempRamEnd) {
138 FspInitParams->RtBufferPtr = (VOID *)((UINT32)(FspInitParams->RtBufferPtr) + OffsetGap);
139 }
140
141 if ((UINT32)(FspInitParams->NvsBufferPtr) >= BootLoaderTempRamStart &&
142 (UINT32)(FspInitParams->NvsBufferPtr) < BootLoaderTempRamEnd) {
143 FspInitParams->NvsBufferPtr = (VOID *)((UINT32)(FspInitParams->NvsBufferPtr) + OffsetGap);
144 }
145
146 if ((UINT32)(((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr) >= BootLoaderTempRamStart &&
147 (UINT32)(((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr) < BootLoaderTempRamEnd) {
148 ((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr = \
149 (VOID *)((UINT32)(((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr) + OffsetGap);
150 }
151
152 //
153 // Update UPD pointer in FSP Global Data
154 //
155 if (ApiMode == 0) {
156 UpdDataRgnPtr = (UINT32)((UINT32 *)GetFspUpdDataPointer ());
157 if (UpdDataRgnPtr >= BootLoaderTempRamStart && UpdDataRgnPtr < BootLoaderTempRamEnd) {
158 MemoryInitUpdPtr = (UINT32)((UINT32 *)GetFspMemoryInitUpdDataPointer ());
159 SiliconInitUpdPtr = (UINT32)((UINT32 *)GetFspSiliconInitUpdDataPointer ());
160 SetFspUpdDataPointer ((VOID *)(UpdDataRgnPtr + OffsetGap));
161 SetFspMemoryInitUpdDataPointer ((VOID *)(MemoryInitUpdPtr + OffsetGap));
162 SetFspSiliconInitUpdDataPointer ((VOID *)(SiliconInitUpdPtr + OffsetGap));
163 }
164 } else {
165 MemoryInitUpdPtr = (UINT32)((UINT32 *)GetFspMemoryInitUpdDataPointer ());
166 if (MemoryInitUpdPtr >= BootLoaderTempRamStart && MemoryInitUpdPtr < BootLoaderTempRamEnd) {
167 SetFspMemoryInitUpdDataPointer ((VOID *)(MemoryInitUpdPtr + OffsetGap));
168 }
169 }
170
171 //
172 // Update Platform data pointer in FSP Global Data
173 //
174 PlatformDataPtr = GetFspPlatformDataPointer ();
175 if (((UINT32)PlatformDataPtr >= BootLoaderTempRamStart) &&
176 ((UINT32)PlatformDataPtr < BootLoaderTempRamEnd)) {
177 SetFspPlatformDataPointer ((UINT8 *)PlatformDataPtr + OffsetGap);
178 }
179 }