]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/SecMigrationPei/SecMigrationPei.c
UefiPayloadPkg: Add a common FVB SMM module
[mirror_edk2.git] / UefiCpuPkg / SecMigrationPei / SecMigrationPei.c
1 /** @file
2 Migrates SEC structures after permanent memory is installed.
3
4 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Base.h>
10
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/PeiServicesLib.h>
17 #include <Library/PeiServicesTablePointerLib.h>
18
19 #include "SecMigrationPei.h"
20
21 STATIC REPUBLISH_SEC_PPI_PPI mEdkiiRepublishSecPpiPpi = {
22 RepublishSecPpis
23 };
24
25 GLOBAL_REMOVE_IF_UNREFERENCED EFI_SEC_PLATFORM_INFORMATION_PPI mSecPlatformInformationPostMemoryPpi = {
26 SecPlatformInformationPostMemory
27 };
28
29
30 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_TEMPORARY_RAM_DONE_PPI mSecTemporaryRamDonePostMemoryPpi = {
31 SecTemporaryRamDonePostMemory
32 };
33
34 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPostMemoryPpi = {
35 SecTemporaryRamSupportPostMemory
36 };
37
38 GLOBAL_REMOVE_IF_UNREFERENCED PEI_SEC_PERFORMANCE_PPI mSecPerformancePpi = {
39 GetPerformancePostMemory
40 };
41
42 STATIC EFI_PEI_PPI_DESCRIPTOR mEdkiiRepublishSecPpiDescriptor = {
43 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
44 &gRepublishSecPpiPpiGuid,
45 &mEdkiiRepublishSecPpiPpi
46 };
47
48 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecPlatformInformationPostMemoryDescriptor = {
49 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
50 &gEfiSecPlatformInformationPpiGuid,
51 &mSecPlatformInformationPostMemoryPpi
52 };
53
54 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecTemporaryRamDonePostMemoryDescriptor = {
55 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
56 &gEfiTemporaryRamDonePpiGuid,
57 &mSecTemporaryRamDonePostMemoryPpi
58 };
59
60 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecTemporaryRamSupportPostMemoryDescriptor = {
61 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
62 &gEfiTemporaryRamSupportPpiGuid,
63 &mSecTemporaryRamSupportPostMemoryPpi
64 };
65
66 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecPerformancePpiDescriptor = {
67 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
68 &gPeiSecPerformancePpiGuid,
69 &mSecPerformancePpi
70 };
71
72 /**
73 Disables the use of Temporary RAM.
74
75 If present, this service is invoked by the PEI Foundation after
76 the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.
77
78 @retval EFI_SUCCESS Dummy function, alway return this value.
79
80 **/
81 EFI_STATUS
82 EFIAPI
83 SecTemporaryRamDonePostMemory (
84 VOID
85 )
86 {
87 //
88 // Temporary RAM Done is already done in post-memory
89 // install a stub function that is located in permanent memory
90 //
91 return EFI_SUCCESS;
92 }
93
94 /**
95 This service of the EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
96 permanent memory.
97
98 @param PeiServices Pointer to the PEI Services Table.
99 @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
100 Temporary RAM contents.
101 @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
102 Temporary RAM contents.
103 @param CopySize Amount of memory to migrate from temporary to permanent memory.
104
105 @retval EFI_SUCCESS The data was successfully returned.
106 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
107 TemporaryMemoryBase > PermanentMemoryBase.
108
109 **/
110 EFI_STATUS
111 EFIAPI
112 SecTemporaryRamSupportPostMemory (
113 IN CONST EFI_PEI_SERVICES **PeiServices,
114 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
115 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
116 IN UINTN CopySize
117 )
118 {
119 //
120 // Temporary RAM Support is already done in post-memory
121 // install a stub function that is located in permanent memory
122 //
123 return EFI_SUCCESS;
124 }
125
126 /**
127 This interface conveys performance information out of the Security (SEC) phase into PEI.
128
129 This service is published by the SEC phase. The SEC phase handoff has an optional
130 EFI_PEI_PPI_DESCRIPTOR list as its final argument when control is passed from SEC into the
131 PEI Foundation. As such, if the platform supports collecting performance data in SEC,
132 this information is encapsulated into the data structure abstracted by this service.
133 This information is collected for the boot-strap processor (BSP) on IA-32.
134
135 @param[in] PeiServices The pointer to the PEI Services Table.
136 @param[in] This The pointer to this instance of the PEI_SEC_PERFORMANCE_PPI.
137 @param[out] Performance The pointer to performance data collected in SEC phase.
138
139 @retval EFI_SUCCESS The performance data was successfully returned.
140 @retval EFI_INVALID_PARAMETER The This or Performance is NULL.
141 @retval EFI_NOT_FOUND Can't found the HOB created by the SecMigrationPei component.
142
143 **/
144 EFI_STATUS
145 EFIAPI
146 GetPerformancePostMemory (
147 IN CONST EFI_PEI_SERVICES **PeiServices,
148 IN PEI_SEC_PERFORMANCE_PPI *This,
149 OUT FIRMWARE_SEC_PERFORMANCE *Performance
150 )
151 {
152 SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContexHob;
153
154 if (This == NULL || Performance == NULL) {
155 return EFI_INVALID_PARAMETER;
156 }
157
158 SecPlatformInformationContexHob = GetFirstGuidHob (&gEfiCallerIdGuid);
159 if (SecPlatformInformationContexHob == NULL) {
160 return EFI_NOT_FOUND;
161 }
162
163 Performance->ResetEnd = SecPlatformInformationContexHob->FirmwareSecPerformance.ResetEnd;
164
165 return EFI_SUCCESS;
166 }
167
168 /**
169 This interface conveys state information out of the Security (SEC) phase into PEI.
170
171 @param[in] PeiServices Pointer to the PEI Services Table.
172 @param[in,out] StructureSize Pointer to the variable describing size of the input buffer.
173 @param[out] PlatformInformationRecord Pointer to the EFI_SEC_PLATFORM_INFORMATION_RECORD.
174
175 @retval EFI_SUCCESS The data was successfully returned.
176 @retval EFI_NOT_FOUND Can't found the HOB created by SecMigrationPei component.
177 @retval EFI_BUFFER_TOO_SMALL The size of buffer pointed by StructureSize is too small and will return
178 the minimal required size in the buffer pointed by StructureSize.
179 @retval EFI_INVALID_PARAMETER The StructureSize is NULL or PlatformInformationRecord is NULL.
180
181 **/
182 EFI_STATUS
183 EFIAPI
184 SecPlatformInformationPostMemory (
185 IN CONST EFI_PEI_SERVICES **PeiServices,
186 IN OUT UINT64 *StructureSize,
187 OUT EFI_SEC_PLATFORM_INFORMATION_RECORD *PlatformInformationRecord
188 )
189 {
190 SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContexHob;
191
192 if (StructureSize == NULL) {
193 return EFI_INVALID_PARAMETER;
194 }
195
196 SecPlatformInformationContexHob = GetFirstGuidHob (&gEfiCallerIdGuid);
197 if (SecPlatformInformationContexHob == NULL) {
198 return EFI_NOT_FOUND;
199 }
200
201 if (*StructureSize < SecPlatformInformationContexHob->Context.StructureSize) {
202 *StructureSize = SecPlatformInformationContexHob->Context.StructureSize;
203 return EFI_BUFFER_TOO_SMALL;
204 }
205
206 if (PlatformInformationRecord == NULL) {
207 return EFI_INVALID_PARAMETER;
208 }
209
210 *StructureSize = SecPlatformInformationContexHob->Context.StructureSize;
211 CopyMem (
212 (VOID *) PlatformInformationRecord,
213 (VOID *) SecPlatformInformationContexHob->Context.PlatformInformationRecord,
214 (UINTN) SecPlatformInformationContexHob->Context.StructureSize
215 );
216
217 return EFI_SUCCESS;
218 }
219
220 /**
221 This interface re-installs PPIs installed in SecCore from a post-memory PEIM.
222
223 This is to allow a platform that may not support relocation of SecCore to update the PPI instance to a post-memory
224 copy from a PEIM that has been shadowed to permanent memory.
225
226 @retval EFI_SUCCESS The SecCore PPIs were re-installed successfully.
227 @retval Others An error occurred re-installing the SecCore PPIs.
228
229 **/
230 EFI_STATUS
231 EFIAPI
232 RepublishSecPpis (
233 VOID
234 )
235 {
236 EFI_STATUS Status;
237 EFI_PEI_PPI_DESCRIPTOR *PeiPpiDescriptor;
238 VOID *PeiPpi;
239 SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContextHob;
240 EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformationPtr;
241 UINT64 SecStructureSize;
242
243 SecPlatformInformationPtr = NULL;
244 SecStructureSize = 0;
245
246 Status = PeiServicesLocatePpi (
247 &gEfiTemporaryRamDonePpiGuid,
248 0,
249 &PeiPpiDescriptor,
250 (VOID **) &PeiPpi
251 );
252 if (!EFI_ERROR (Status)) {
253 Status = PeiServicesReInstallPpi (
254 PeiPpiDescriptor,
255 &mSecTemporaryRamDonePostMemoryDescriptor
256 );
257 ASSERT_EFI_ERROR (Status);
258 }
259
260 Status = PeiServicesLocatePpi (
261 &gEfiTemporaryRamSupportPpiGuid,
262 0,
263 &PeiPpiDescriptor,
264 (VOID **) &PeiPpi
265 );
266 if (!EFI_ERROR (Status)) {
267 Status = PeiServicesReInstallPpi (
268 PeiPpiDescriptor,
269 &mSecTemporaryRamSupportPostMemoryDescriptor
270 );
271 ASSERT_EFI_ERROR (Status);
272 }
273
274 Status = PeiServicesCreateHob (
275 EFI_HOB_TYPE_GUID_EXTENSION,
276 sizeof (SEC_PLATFORM_INFORMATION_CONTEXT_HOB),
277 (VOID **) &SecPlatformInformationContextHob
278 );
279 ASSERT_EFI_ERROR (Status);
280 if (EFI_ERROR (Status)) {
281 DEBUG ((DEBUG_ERROR, "SecPlatformInformation Context HOB could not be created.\n"));
282 return Status;
283 }
284
285 SecPlatformInformationContextHob->Header.Name = gEfiCallerIdGuid;
286 SecPlatformInformationContextHob->Revision = 1;
287
288 Status = PeiServicesLocatePpi (
289 &gPeiSecPerformancePpiGuid,
290 0,
291 &PeiPpiDescriptor,
292 (VOID **) &PeiPpi
293 );
294 if (!EFI_ERROR (Status)) {
295 Status = ((PEI_SEC_PERFORMANCE_PPI *) PeiPpi)->GetPerformance (
296 GetPeiServicesTablePointer (),
297 (PEI_SEC_PERFORMANCE_PPI *) PeiPpi,
298 &SecPlatformInformationContextHob->FirmwareSecPerformance
299 );
300 ASSERT_EFI_ERROR (Status);
301 if (!EFI_ERROR (Status)) {
302 Status = PeiServicesReInstallPpi (
303 PeiPpiDescriptor,
304 &mSecPerformancePpiDescriptor
305 );
306 ASSERT_EFI_ERROR (Status);
307 }
308 }
309
310 Status = PeiServicesLocatePpi (
311 &gEfiSecPlatformInformationPpiGuid,
312 0,
313 &PeiPpiDescriptor,
314 (VOID **) &PeiPpi
315 );
316 if (!EFI_ERROR (Status)) {
317 Status = ((EFI_SEC_PLATFORM_INFORMATION_PPI *) PeiPpi)->PlatformInformation (
318 GetPeiServicesTablePointer (),
319 &SecStructureSize,
320 SecPlatformInformationPtr
321 );
322 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
323 if (Status != EFI_BUFFER_TOO_SMALL) {
324 return EFI_NOT_FOUND;
325 }
326
327 ZeroMem ((VOID *) &(SecPlatformInformationContextHob->Context), sizeof (SEC_PLATFORM_INFORMATION_CONTEXT));
328 SecPlatformInformationContextHob->Context.PlatformInformationRecord = AllocatePool ((UINTN) SecStructureSize);
329 ASSERT (SecPlatformInformationContextHob->Context.PlatformInformationRecord != NULL);
330 if (SecPlatformInformationContextHob->Context.PlatformInformationRecord == NULL) {
331 return EFI_OUT_OF_RESOURCES;
332 }
333 SecPlatformInformationContextHob->Context.StructureSize = SecStructureSize;
334
335 Status = ((EFI_SEC_PLATFORM_INFORMATION_PPI *) PeiPpi)->PlatformInformation (
336 GetPeiServicesTablePointer (),
337 &(SecPlatformInformationContextHob->Context.StructureSize),
338 SecPlatformInformationContextHob->Context.PlatformInformationRecord
339 );
340 ASSERT_EFI_ERROR (Status);
341 if (!EFI_ERROR (Status)) {
342 Status = PeiServicesReInstallPpi (
343 PeiPpiDescriptor,
344 &mSecPlatformInformationPostMemoryDescriptor
345 );
346 ASSERT_EFI_ERROR (Status);
347 }
348 }
349
350 return EFI_SUCCESS;
351 }
352
353 /**
354 This function is the entry point which installs an instance of REPUBLISH_SEC_PPI_PPI.
355
356 It install the RepublishSecPpi depent on PcdMigrateTemporaryRamFirmwareVolumes, install
357 the PPI when the PcdMigrateTemporaryRamFirmwareVolumes enabled.
358
359 @param[in] FileHandle Pointer to image file handle.
360 @param[in] PeiServices Pointer to PEI Services Table
361
362 @retval EFI_ABORTED Disable evacuate temporary memory feature by disable
363 PcdMigrateTemporaryRamFirmwareVolumes.
364 @retval EFI_SUCCESS An instance of REPUBLISH_SEC_PPI_PPI was installed successfully.
365 @retval Others An error occurred installing and instance of REPUBLISH_SEC_PPI_PPI.
366
367 **/
368 EFI_STATUS
369 EFIAPI
370 SecMigrationPeiInitialize (
371 IN EFI_PEI_FILE_HANDLE FileHandle,
372 IN CONST EFI_PEI_SERVICES **PeiServices
373 )
374 {
375 EFI_STATUS Status;
376
377 Status = EFI_ABORTED;
378
379 if (PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes)) {
380 Status = PeiServicesInstallPpi (&mEdkiiRepublishSecPpiDescriptor);
381 ASSERT_EFI_ERROR (Status);
382 }
383
384 return Status;
385 }