]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2WrapperPkg/FspmWrapperPeim/FspmWrapperPeim.c
49fbb27eca948827cb208039d998a0400add178a
[mirror_edk2.git] / IntelFsp2WrapperPkg / FspmWrapperPeim / FspmWrapperPeim.c
1 /** @file
2 This will be invoked only once. It will call FspMemoryInit API,
3 register TemporaryRamDonePpi to call TempRamExit API, and register MemoryDiscoveredPpi
4 notify to call FspSiliconInit API.
5
6 Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiPei.h>
12
13 #include <Library/PeimEntryPoint.h>
14 #include <Library/PeiServicesLib.h>
15 #include <Library/PeiServicesTablePointerLib.h>
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/HobLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/TimerLib.h>
23 #include <Library/PerformanceLib.h>
24 #include <Library/FspWrapperPlatformLib.h>
25 #include <Library/FspWrapperHobProcessLib.h>
26 #include <Library/FspWrapperApiLib.h>
27 #include <Library/FspMeasurementLib.h>
28
29 #include <Ppi/FspSiliconInitDone.h>
30 #include <Ppi/EndOfPeiPhase.h>
31 #include <Ppi/MemoryDiscovered.h>
32 #include <Ppi/SecPlatformInformation.h>
33 #include <Ppi/Tcg.h>
34 #include <Ppi/FirmwareVolumeInfoMeasurementExcluded.h>
35 #include <Library/FspWrapperApiTestLib.h>
36 #include <FspEas.h>
37 #include <FspStatusCode.h>
38
39 extern EFI_GUID gFspHobGuid;
40
41 /**
42 Get the FSP M UPD Data address
43
44 @return FSP-M UPD Data Address
45 **/
46
47 UINTN
48 EFIAPI
49 GetFspmUpdDataAddress (
50 VOID
51 )
52 {
53 if (PcdGet64 (PcdFspmUpdDataAddress64) != 0) {
54 return (UINTN) PcdGet64 (PcdFspmUpdDataAddress64);
55 } else {
56 return (UINTN) PcdGet32 (PcdFspmUpdDataAddress);
57 }
58 }
59
60 /**
61 Call FspMemoryInit API.
62
63 @return Status returned by FspMemoryInit API.
64 **/
65 EFI_STATUS
66 PeiFspMemoryInit (
67 VOID
68 )
69 {
70 FSP_INFO_HEADER *FspmHeaderPtr;
71 EFI_STATUS Status;
72 UINT64 TimeStampCounterStart;
73 VOID *FspHobListPtr;
74 VOID *HobData;
75 FSPM_UPD_COMMON *FspmUpdDataPtr;
76 UINTN *SourceData;
77
78 DEBUG ((DEBUG_INFO, "PeiFspMemoryInit enter\n"));
79
80 FspHobListPtr = NULL;
81 FspmUpdDataPtr = NULL;
82
83 FspmHeaderPtr = (FSP_INFO_HEADER *)FspFindFspHeader (PcdGet32 (PcdFspmBaseAddress));
84 DEBUG ((DEBUG_INFO, "FspmHeaderPtr - 0x%x\n", FspmHeaderPtr));
85 if (FspmHeaderPtr == NULL) {
86 return EFI_DEVICE_ERROR;
87 }
88
89 if ((GetFspmUpdDataAddress () == 0) && (FspmHeaderPtr->CfgRegionSize != 0) && (FspmHeaderPtr->CfgRegionOffset != 0)) {
90 //
91 // Copy default FSP-M UPD data from Flash
92 //
93 FspmUpdDataPtr = (FSPM_UPD_COMMON *)AllocateZeroPool ((UINTN)FspmHeaderPtr->CfgRegionSize);
94 ASSERT (FspmUpdDataPtr != NULL);
95 SourceData = (UINTN *)((UINTN)FspmHeaderPtr->ImageBase + (UINTN)FspmHeaderPtr->CfgRegionOffset);
96 CopyMem (FspmUpdDataPtr, SourceData, (UINTN)FspmHeaderPtr->CfgRegionSize);
97 } else {
98 //
99 // External UPD is ready, get the buffer from PCD pointer.
100 //
101 FspmUpdDataPtr = (FSPM_UPD_COMMON *) GetFspmUpdDataAddress();
102 ASSERT (FspmUpdDataPtr != NULL);
103 }
104
105 DEBUG ((DEBUG_INFO, "UpdateFspmUpdData enter\n"));
106 UpdateFspmUpdData ((VOID *)FspmUpdDataPtr);
107 DEBUG ((DEBUG_INFO, " NvsBufferPtr - 0x%x\n", FspmUpdDataPtr->FspmArchUpd.NvsBufferPtr));
108 DEBUG ((DEBUG_INFO, " StackBase - 0x%x\n", FspmUpdDataPtr->FspmArchUpd.StackBase));
109 DEBUG ((DEBUG_INFO, " StackSize - 0x%x\n", FspmUpdDataPtr->FspmArchUpd.StackSize));
110 DEBUG ((DEBUG_INFO, " BootLoaderTolumSize - 0x%x\n", FspmUpdDataPtr->FspmArchUpd.BootLoaderTolumSize));
111 DEBUG ((DEBUG_INFO, " BootMode - 0x%x\n", FspmUpdDataPtr->FspmArchUpd.BootMode));
112 DEBUG ((DEBUG_INFO, " HobListPtr - 0x%x\n", &FspHobListPtr));
113
114 TimeStampCounterStart = AsmReadTsc ();
115 Status = CallFspMemoryInit (FspmUpdDataPtr, &FspHobListPtr);
116 // Create hobs after memory initialization and not in temp RAM. Hence passing the recorded timestamp here
117 PERF_START_EX (&gFspApiPerformanceGuid, "EventRec", NULL, TimeStampCounterStart, FSP_STATUS_CODE_MEMORY_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_ENTRY);
118 PERF_END_EX (&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_MEMORY_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_EXIT);
119 DEBUG ((DEBUG_INFO, "Total time spent executing FspMemoryInitApi: %d millisecond\n", DivU64x32 (GetTimeInNanoSecond (AsmReadTsc () - TimeStampCounterStart), 1000000)));
120
121 //
122 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
123 //
124 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
125 DEBUG ((DEBUG_INFO, "FspMemoryInitApi requested reset 0x%x\n", Status));
126 CallFspWrapperResetSystem ((UINT32)Status);
127 }
128
129 if (EFI_ERROR (Status)) {
130 DEBUG ((DEBUG_ERROR, "ERROR - Failed to execute FspMemoryInitApi(), Status = %r\n", Status));
131 }
132
133 DEBUG ((DEBUG_INFO, "FspMemoryInit status: 0x%x\n", Status));
134 ASSERT_EFI_ERROR (Status);
135
136 Status = TestFspMemoryInitApiOutput (FspmUpdDataPtr, &FspHobListPtr);
137 if (EFI_ERROR (Status)) {
138 DEBUG ((DEBUG_ERROR, "ERROR - TestFspMemoryInitApiOutput () fail, Status = %r\n", Status));
139 }
140
141 DEBUG ((DEBUG_INFO, " FspHobListPtr (returned) - 0x%x\n", FspHobListPtr));
142 ASSERT (FspHobListPtr != NULL);
143
144 PostFspmHobProcess (FspHobListPtr);
145
146 //
147 // FspHobList is not complete at this moment.
148 // Save FspHobList pointer to hob, so that it can be got later
149 //
150 HobData = BuildGuidHob (
151 &gFspHobGuid,
152 sizeof (VOID *)
153 );
154 ASSERT (HobData != NULL);
155 CopyMem (HobData, &FspHobListPtr, sizeof (FspHobListPtr));
156
157 return Status;
158 }
159
160 /**
161 Do FSP initialization.
162
163 @return FSP initialization status.
164 **/
165 EFI_STATUS
166 EFIAPI
167 FspmWrapperInit (
168 VOID
169 )
170 {
171 EFI_STATUS Status;
172 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *MeasurementExcludedFvPpi;
173 EFI_PEI_PPI_DESCRIPTOR *MeasurementExcludedPpiList;
174
175 MeasurementExcludedFvPpi = AllocatePool (sizeof (*MeasurementExcludedFvPpi));
176 ASSERT (MeasurementExcludedFvPpi != NULL);
177 MeasurementExcludedFvPpi->Count = 1;
178 MeasurementExcludedFvPpi->Fv[0].FvBase = PcdGet32 (PcdFspmBaseAddress);
179 MeasurementExcludedFvPpi->Fv[0].FvLength = ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFspmBaseAddress))->FvLength;
180
181 MeasurementExcludedPpiList = AllocatePool (sizeof (*MeasurementExcludedPpiList));
182 ASSERT (MeasurementExcludedPpiList != NULL);
183 MeasurementExcludedPpiList->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
184 MeasurementExcludedPpiList->Guid = &gEfiPeiFirmwareVolumeInfoMeasurementExcludedPpiGuid;
185 MeasurementExcludedPpiList->Ppi = MeasurementExcludedFvPpi;
186
187 Status = EFI_SUCCESS;
188
189 if (PcdGet8 (PcdFspModeSelection) == 1) {
190 Status = PeiFspMemoryInit ();
191 ASSERT_EFI_ERROR (Status);
192 } else {
193 Status = PeiServicesInstallPpi (MeasurementExcludedPpiList);
194 ASSERT_EFI_ERROR (Status);
195
196 PeiServicesInstallFvInfoPpi (
197 NULL,
198 (VOID *)(UINTN)PcdGet32 (PcdFspmBaseAddress),
199 (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFspmBaseAddress))->FvLength,
200 NULL,
201 NULL
202 );
203 }
204
205 return Status;
206 }
207
208 /**
209 This function is called after TCG installed PPI.
210
211 @param[in] PeiServices Pointer to PEI Services Table.
212 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
213 caused this function to execute.
214 @param[in] Ppi Pointer to the PPI data associated with this function.
215
216 @retval EFI_STATUS Always return EFI_SUCCESS
217 **/
218 EFI_STATUS
219 EFIAPI
220 TcgPpiNotify (
221 IN EFI_PEI_SERVICES **PeiServices,
222 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
223 IN VOID *Ppi
224 );
225
226 EFI_PEI_NOTIFY_DESCRIPTOR mTcgPpiNotifyDesc = {
227 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
228 &gEdkiiTcgPpiGuid,
229 TcgPpiNotify
230 };
231
232 /**
233 This function is called after TCG installed PPI.
234
235 @param[in] PeiServices Pointer to PEI Services Table.
236 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
237 caused this function to execute.
238 @param[in] Ppi Pointer to the PPI data associated with this function.
239
240 @retval EFI_STATUS Always return EFI_SUCCESS
241 **/
242 EFI_STATUS
243 EFIAPI
244 TcgPpiNotify (
245 IN EFI_PEI_SERVICES **PeiServices,
246 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
247 IN VOID *Ppi
248 )
249 {
250 UINT32 FspMeasureMask;
251
252 DEBUG ((DEBUG_INFO, "TcgPpiNotify FSPM\n"));
253
254 FspMeasureMask = PcdGet32 (PcdFspMeasurementConfig);
255
256 if ((FspMeasureMask & FSP_MEASURE_FSPT) != 0) {
257 MeasureFspFirmwareBlob (
258 0,
259 "FSPT",
260 PcdGet32 (PcdFsptBaseAddress),
261 (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFsptBaseAddress))->FvLength
262 );
263 }
264
265 if ((FspMeasureMask & FSP_MEASURE_FSPM) != 0) {
266 MeasureFspFirmwareBlob (
267 0,
268 "FSPM",
269 PcdGet32 (PcdFspmBaseAddress),
270 (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFspmBaseAddress))->FvLength
271 );
272 }
273
274 return EFI_SUCCESS;
275 }
276
277 /**
278 This is the entrypoint of PEIM
279
280 @param[in] FileHandle Handle of the file being invoked.
281 @param[in] PeiServices Describes the list of possible PEI Services.
282
283 @retval EFI_SUCCESS if it completed successfully.
284 **/
285 EFI_STATUS
286 EFIAPI
287 FspmWrapperPeimEntryPoint (
288 IN EFI_PEI_FILE_HANDLE FileHandle,
289 IN CONST EFI_PEI_SERVICES **PeiServices
290 )
291 {
292 EFI_STATUS Status;
293
294 DEBUG ((DEBUG_INFO, "FspmWrapperPeimEntryPoint\n"));
295
296 Status = PeiServicesNotifyPpi (&mTcgPpiNotifyDesc);
297 ASSERT_EFI_ERROR (Status);
298
299 FspmWrapperInit ();
300
301 return EFI_SUCCESS;
302 }