]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/SecMigrationPei/SecMigrationPei.c
.github/dependabot.yml: Enable dependabot
[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 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_TEMPORARY_RAM_DONE_PPI mSecTemporaryRamDonePostMemoryPpi = {
30 SecTemporaryRamDonePostMemory
31 };
32
33 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPostMemoryPpi = {
34 SecTemporaryRamSupportPostMemory
35 };
36
37 GLOBAL_REMOVE_IF_UNREFERENCED PEI_SEC_PERFORMANCE_PPI mSecPerformancePpi = {
38 GetPerformancePostMemory
39 };
40
41 STATIC EFI_PEI_PPI_DESCRIPTOR mEdkiiRepublishSecPpiDescriptor = {
42 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
43 &gRepublishSecPpiPpiGuid,
44 &mEdkiiRepublishSecPpiPpi
45 };
46
47 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecPlatformInformationPostMemoryDescriptor = {
48 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
49 &gEfiSecPlatformInformationPpiGuid,
50 &mSecPlatformInformationPostMemoryPpi
51 };
52
53 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecTemporaryRamDonePostMemoryDescriptor = {
54 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
55 &gEfiTemporaryRamDonePpiGuid,
56 &mSecTemporaryRamDonePostMemoryPpi
57 };
58
59 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecTemporaryRamSupportPostMemoryDescriptor = {
60 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
61 &gEfiTemporaryRamSupportPpiGuid,
62 &mSecTemporaryRamSupportPostMemoryPpi
63 };
64
65 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecPerformancePpiDescriptor = {
66 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
67 &gPeiSecPerformancePpiGuid,
68 &mSecPerformancePpi
69 };
70
71 /**
72 Disables the use of Temporary RAM.
73
74 If present, this service is invoked by the PEI Foundation after
75 the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.
76
77 @retval EFI_SUCCESS Dummy function, alway return this value.
78
79 **/
80 EFI_STATUS
81 EFIAPI
82 SecTemporaryRamDonePostMemory (
83 VOID
84 )
85 {
86 //
87 // Temporary RAM Done is already done in post-memory
88 // install a stub function that is located in permanent memory
89 //
90 return EFI_SUCCESS;
91 }
92
93 /**
94 This service of the EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
95 permanent memory.
96
97 @param PeiServices Pointer to the PEI Services Table.
98 @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
99 Temporary RAM contents.
100 @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
101 Temporary RAM contents.
102 @param CopySize Amount of memory to migrate from temporary to permanent memory.
103
104 @retval EFI_SUCCESS The data was successfully returned.
105 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
106 TemporaryMemoryBase > PermanentMemoryBase.
107
108 **/
109 EFI_STATUS
110 EFIAPI
111 SecTemporaryRamSupportPostMemory (
112 IN CONST EFI_PEI_SERVICES **PeiServices,
113 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
114 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
115 IN UINTN CopySize
116 )
117 {
118 //
119 // Temporary RAM Support is already done in post-memory
120 // install a stub function that is located in permanent memory
121 //
122 return EFI_SUCCESS;
123 }
124
125 /**
126 This interface conveys performance information out of the Security (SEC) phase into PEI.
127
128 This service is published by the SEC phase. The SEC phase handoff has an optional
129 EFI_PEI_PPI_DESCRIPTOR list as its final argument when control is passed from SEC into the
130 PEI Foundation. As such, if the platform supports collecting performance data in SEC,
131 this information is encapsulated into the data structure abstracted by this service.
132 This information is collected for the boot-strap processor (BSP) on IA-32.
133
134 @param[in] PeiServices The pointer to the PEI Services Table.
135 @param[in] This The pointer to this instance of the PEI_SEC_PERFORMANCE_PPI.
136 @param[out] Performance The pointer to performance data collected in SEC phase.
137
138 @retval EFI_SUCCESS The performance data was successfully returned.
139 @retval EFI_INVALID_PARAMETER The This or Performance is NULL.
140 @retval EFI_NOT_FOUND Can't found the HOB created by the SecMigrationPei component.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 GetPerformancePostMemory (
146 IN CONST EFI_PEI_SERVICES **PeiServices,
147 IN PEI_SEC_PERFORMANCE_PPI *This,
148 OUT FIRMWARE_SEC_PERFORMANCE *Performance
149 )
150 {
151 SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContexHob;
152
153 if ((This == NULL) || (Performance == NULL)) {
154 return EFI_INVALID_PARAMETER;
155 }
156
157 SecPlatformInformationContexHob = GetFirstGuidHob (&gEfiCallerIdGuid);
158 if (SecPlatformInformationContexHob == NULL) {
159 return EFI_NOT_FOUND;
160 }
161
162 Performance->ResetEnd = SecPlatformInformationContexHob->FirmwareSecPerformance.ResetEnd;
163
164 return EFI_SUCCESS;
165 }
166
167 /**
168 This interface conveys state information out of the Security (SEC) phase into PEI.
169
170 @param[in] PeiServices Pointer to the PEI Services Table.
171 @param[in,out] StructureSize Pointer to the variable describing size of the input buffer.
172 @param[out] PlatformInformationRecord Pointer to the EFI_SEC_PLATFORM_INFORMATION_RECORD.
173
174 @retval EFI_SUCCESS The data was successfully returned.
175 @retval EFI_NOT_FOUND Can't found the HOB created by SecMigrationPei component.
176 @retval EFI_BUFFER_TOO_SMALL The size of buffer pointed by StructureSize is too small and will return
177 the minimal required size in the buffer pointed by StructureSize.
178 @retval EFI_INVALID_PARAMETER The StructureSize is NULL or PlatformInformationRecord is NULL.
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 SecPlatformInformationPostMemory (
184 IN CONST EFI_PEI_SERVICES **PeiServices,
185 IN OUT UINT64 *StructureSize,
186 OUT EFI_SEC_PLATFORM_INFORMATION_RECORD *PlatformInformationRecord
187 )
188 {
189 SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContexHob;
190
191 if (StructureSize == NULL) {
192 return EFI_INVALID_PARAMETER;
193 }
194
195 SecPlatformInformationContexHob = GetFirstGuidHob (&gEfiCallerIdGuid);
196 if (SecPlatformInformationContexHob == NULL) {
197 return EFI_NOT_FOUND;
198 }
199
200 if (*StructureSize < SecPlatformInformationContexHob->Context.StructureSize) {
201 *StructureSize = SecPlatformInformationContexHob->Context.StructureSize;
202 return EFI_BUFFER_TOO_SMALL;
203 }
204
205 if (PlatformInformationRecord == NULL) {
206 return EFI_INVALID_PARAMETER;
207 }
208
209 *StructureSize = SecPlatformInformationContexHob->Context.StructureSize;
210 CopyMem (
211 (VOID *)PlatformInformationRecord,
212 (VOID *)SecPlatformInformationContexHob->Context.PlatformInformationRecord,
213 (UINTN)SecPlatformInformationContexHob->Context.StructureSize
214 );
215
216 return EFI_SUCCESS;
217 }
218
219 /**
220 This interface re-installs PPIs installed in SecCore from a post-memory PEIM.
221
222 This is to allow a platform that may not support relocation of SecCore to update the PPI instance to a post-memory
223 copy from a PEIM that has been shadowed to permanent memory.
224
225 @retval EFI_SUCCESS The SecCore PPIs were re-installed successfully.
226 @retval Others An error occurred re-installing the SecCore PPIs.
227
228 **/
229 EFI_STATUS
230 EFIAPI
231 RepublishSecPpis (
232 VOID
233 )
234 {
235 EFI_STATUS Status;
236 EFI_PEI_PPI_DESCRIPTOR *PeiPpiDescriptor;
237 VOID *PeiPpi;
238 SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContextHob;
239 EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformationPtr;
240 UINT64 SecStructureSize;
241
242 SecPlatformInformationPtr = NULL;
243 SecStructureSize = 0;
244
245 Status = PeiServicesLocatePpi (
246 &gEfiTemporaryRamDonePpiGuid,
247 0,
248 &PeiPpiDescriptor,
249 (VOID **)&PeiPpi
250 );
251 if (!EFI_ERROR (Status)) {
252 Status = PeiServicesReInstallPpi (
253 PeiPpiDescriptor,
254 &mSecTemporaryRamDonePostMemoryDescriptor
255 );
256 ASSERT_EFI_ERROR (Status);
257 }
258
259 Status = PeiServicesLocatePpi (
260 &gEfiTemporaryRamSupportPpiGuid,
261 0,
262 &PeiPpiDescriptor,
263 (VOID **)&PeiPpi
264 );
265 if (!EFI_ERROR (Status)) {
266 Status = PeiServicesReInstallPpi (
267 PeiPpiDescriptor,
268 &mSecTemporaryRamSupportPostMemoryDescriptor
269 );
270 ASSERT_EFI_ERROR (Status);
271 }
272
273 Status = PeiServicesCreateHob (
274 EFI_HOB_TYPE_GUID_EXTENSION,
275 sizeof (SEC_PLATFORM_INFORMATION_CONTEXT_HOB),
276 (VOID **)&SecPlatformInformationContextHob
277 );
278 ASSERT_EFI_ERROR (Status);
279 if (EFI_ERROR (Status)) {
280 DEBUG ((DEBUG_ERROR, "SecPlatformInformation Context HOB could not be created.\n"));
281 return Status;
282 }
283
284 SecPlatformInformationContextHob->Header.Name = gEfiCallerIdGuid;
285 SecPlatformInformationContextHob->Revision = 1;
286
287 Status = PeiServicesLocatePpi (
288 &gPeiSecPerformancePpiGuid,
289 0,
290 &PeiPpiDescriptor,
291 (VOID **)&PeiPpi
292 );
293 if (!EFI_ERROR (Status)) {
294 Status = ((PEI_SEC_PERFORMANCE_PPI *)PeiPpi)->GetPerformance (
295 GetPeiServicesTablePointer (),
296 (PEI_SEC_PERFORMANCE_PPI *)PeiPpi,
297 &SecPlatformInformationContextHob->FirmwareSecPerformance
298 );
299 ASSERT_EFI_ERROR (Status);
300 if (!EFI_ERROR (Status)) {
301 Status = PeiServicesReInstallPpi (
302 PeiPpiDescriptor,
303 &mSecPerformancePpiDescriptor
304 );
305 ASSERT_EFI_ERROR (Status);
306 }
307 }
308
309 Status = PeiServicesLocatePpi (
310 &gEfiSecPlatformInformationPpiGuid,
311 0,
312 &PeiPpiDescriptor,
313 (VOID **)&PeiPpi
314 );
315 if (!EFI_ERROR (Status)) {
316 Status = ((EFI_SEC_PLATFORM_INFORMATION_PPI *)PeiPpi)->PlatformInformation (
317 GetPeiServicesTablePointer (),
318 &SecStructureSize,
319 SecPlatformInformationPtr
320 );
321 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
322 if (Status != EFI_BUFFER_TOO_SMALL) {
323 return EFI_NOT_FOUND;
324 }
325
326 ZeroMem ((VOID *)&(SecPlatformInformationContextHob->Context), sizeof (SEC_PLATFORM_INFORMATION_CONTEXT));
327 SecPlatformInformationContextHob->Context.PlatformInformationRecord = AllocatePool ((UINTN)SecStructureSize);
328 ASSERT (SecPlatformInformationContextHob->Context.PlatformInformationRecord != NULL);
329 if (SecPlatformInformationContextHob->Context.PlatformInformationRecord == NULL) {
330 return EFI_OUT_OF_RESOURCES;
331 }
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 }