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