]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxPeiLib.c
MdeModulePkg/SmmLockBoxLib: Use 'DEBUG_' prefix instead of 'EFI_D_'
[mirror_edk2.git] / MdeModulePkg / Library / SmmLockBoxLib / SmmLockBoxPeiLib.c
CommitLineData
993532d0 1/** @file\r
2\r
481ffd6f 3Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>\r
993532d0 4\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions\r
7of the BSD License which accompanies this distribution. The\r
8full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiPei.h>\r
17#include <PiDxe.h>\r
18#include <PiSmm.h>\r
19#include <Library/PeiServicesTablePointerLib.h>\r
20#include <Library/PeiServicesLib.h>\r
21#include <Library/BaseLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
23#include <Library/LockBoxLib.h>\r
24#include <Library/HobLib.h>\r
25#include <Library/DebugLib.h>\r
26#include <Library/PcdLib.h>\r
27#include <Protocol/SmmCommunication.h>\r
28#include <Ppi/SmmCommunication.h>\r
29#include <Ppi/SmmAccess.h>\r
30#include <Guid/AcpiS3Context.h>\r
31#include <Guid/SmmLockBox.h>\r
32\r
33#include "SmmLockBoxLibPrivate.h"\r
34\r
35#if defined (MDE_CPU_IA32)\r
36typedef struct _LIST_ENTRY64 LIST_ENTRY64;\r
37struct _LIST_ENTRY64 {\r
38 LIST_ENTRY64 *ForwardLink;\r
39 UINT32 Reserved1;\r
40 LIST_ENTRY64 *BackLink;\r
41 UINT32 Reserved2;\r
42};\r
43\r
44typedef struct {\r
45 EFI_TABLE_HEADER Hdr;\r
46 UINT64 SmmFirmwareVendor;\r
47 UINT64 SmmFirmwareRevision;\r
48 UINT64 SmmInstallConfigurationTable;\r
49 UINT64 SmmIoMemRead;\r
50 UINT64 SmmIoMemWrite;\r
51 UINT64 SmmIoIoRead;\r
52 UINT64 SmmIoIoWrite;\r
53 UINT64 SmmAllocatePool;\r
54 UINT64 SmmFreePool;\r
55 UINT64 SmmAllocatePages;\r
56 UINT64 SmmFreePages;\r
57 UINT64 SmmStartupThisAp;\r
58 UINT64 CurrentlyExecutingCpu;\r
59 UINT64 NumberOfCpus;\r
60 UINT64 CpuSaveStateSize;\r
61 UINT64 CpuSaveState;\r
62 UINT64 NumberOfTableEntries;\r
63 UINT64 SmmConfigurationTable;\r
64} EFI_SMM_SYSTEM_TABLE2_64;\r
65\r
66typedef struct {\r
67 EFI_GUID VendorGuid;\r
68 UINT64 VendorTable;\r
69} EFI_CONFIGURATION_TABLE64;\r
70#endif\r
71\r
72#if defined (MDE_CPU_X64)\r
73typedef LIST_ENTRY LIST_ENTRY64;\r
74typedef EFI_SMM_SYSTEM_TABLE2 EFI_SMM_SYSTEM_TABLE2_64;\r
75typedef EFI_CONFIGURATION_TABLE EFI_CONFIGURATION_TABLE64;\r
76#endif\r
77\r
78/**\r
79 This function return first node of LinkList queue.\r
80\r
81 @param LockBoxQueue LinkList queue\r
82\r
83 @return first node of LinkList queue\r
84**/\r
85LIST_ENTRY *\r
86InternalInitLinkDxe (\r
87 IN LIST_ENTRY *LinkList\r
88 )\r
89{\r
90 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) ) {\r
91 //\r
92 // 32 PEI + 64 DXE\r
93 //\r
94 return (LIST_ENTRY *)(((LIST_ENTRY64 *)LinkList)->ForwardLink);\r
95 } else {\r
96 return LinkList->ForwardLink;\r
97 }\r
98}\r
99\r
100/**\r
101 This function return next node of LinkList.\r
102\r
103 @param Link LinkList node\r
104\r
105 @return next node of LinkList\r
106**/\r
107LIST_ENTRY *\r
108InternalNextLinkDxe (\r
109 IN LIST_ENTRY *Link\r
110 )\r
111{\r
112 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) ) {\r
113 //\r
114 // 32 PEI + 64 DXE\r
115 //\r
116 return (LIST_ENTRY *)(((LIST_ENTRY64 *)Link)->ForwardLink);\r
117 } else {\r
118 return Link->ForwardLink;\r
119 }\r
120}\r
121\r
122/**\r
123 This function find LockBox by GUID from SMRAM.\r
124\r
125 @param LockBoxQueue The LockBox queue in SMRAM\r
126 @param Guid The guid to indentify the LockBox\r
127\r
128 @return LockBoxData\r
129**/\r
130SMM_LOCK_BOX_DATA *\r
131InternalFindLockBoxByGuidFromSmram (\r
132 IN LIST_ENTRY *LockBoxQueue,\r
133 IN EFI_GUID *Guid\r
134 )\r
135{\r
136 LIST_ENTRY *Link;\r
137 SMM_LOCK_BOX_DATA *LockBox;\r
138\r
139 for (Link = InternalInitLinkDxe (LockBoxQueue);\r
140 Link != LockBoxQueue;\r
141 Link = InternalNextLinkDxe (Link)) {\r
142 LockBox = BASE_CR (\r
143 Link,\r
144 SMM_LOCK_BOX_DATA,\r
145 Link\r
146 );\r
147 if (CompareGuid (&LockBox->Guid, Guid)) {\r
148 return LockBox;\r
149 }\r
150 }\r
151 return NULL;\r
152}\r
153\r
154/**\r
155 Get VendorTable by VendorGuid in Smst.\r
156\r
157 @param Signature Signature of SMM_S3_RESUME_STATE\r
158 @param Smst SMM system table\r
159 @param VendorGuid vendor guid\r
160\r
161 @return vendor table.\r
162**/\r
163VOID *\r
164InternalSmstGetVendorTableByGuid (\r
165 IN UINT64 Signature,\r
166 IN EFI_SMM_SYSTEM_TABLE2 *Smst,\r
167 IN EFI_GUID *VendorGuid\r
168 )\r
169{\r
170 EFI_CONFIGURATION_TABLE *SmmConfigurationTable;\r
171 UINTN NumberOfTableEntries;\r
172 UINTN Index;\r
173 EFI_SMM_SYSTEM_TABLE2_64 *Smst64;\r
174 EFI_CONFIGURATION_TABLE64 *SmmConfigurationTable64;\r
175\r
176 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode))) {\r
177 //\r
178 // 32 PEI + 64 DXE\r
179 //\r
180 Smst64 = (EFI_SMM_SYSTEM_TABLE2_64 *)Smst;\r
181 SmmConfigurationTable64 = (EFI_CONFIGURATION_TABLE64 *)(UINTN)Smst64->SmmConfigurationTable;\r
182 NumberOfTableEntries = (UINTN)Smst64->NumberOfTableEntries;\r
183 for (Index = 0; Index < NumberOfTableEntries; Index++) {\r
184 if (CompareGuid (&SmmConfigurationTable64[Index].VendorGuid, VendorGuid)) {\r
185 return (VOID *)(UINTN)SmmConfigurationTable64[Index].VendorTable;\r
186 }\r
187 }\r
188 return NULL;\r
189 } else {\r
190 SmmConfigurationTable = Smst->SmmConfigurationTable;\r
191 NumberOfTableEntries = Smst->NumberOfTableEntries;\r
192 for (Index = 0; Index < NumberOfTableEntries; Index++) {\r
193 if (CompareGuid (&SmmConfigurationTable[Index].VendorGuid, VendorGuid)) {\r
194 return (VOID *)SmmConfigurationTable[Index].VendorTable;\r
195 }\r
196 }\r
197 return NULL;\r
198 }\r
199}\r
200\r
201/**\r
202 Get SMM LockBox context.\r
203\r
204 @return SMM LockBox context.\r
205**/\r
206SMM_LOCK_BOX_CONTEXT *\r
207InternalGetSmmLockBoxContext (\r
208 VOID\r
209 )\r
210{\r
211 EFI_SMRAM_DESCRIPTOR *SmramDescriptor;\r
212 SMM_S3_RESUME_STATE *SmmS3ResumeState;\r
213 VOID *GuidHob;\r
214 SMM_LOCK_BOX_CONTEXT *SmmLockBoxContext;\r
215\r
216 GuidHob = GetFirstGuidHob (&gEfiAcpiVariableGuid);\r
217 ASSERT (GuidHob != NULL);\r
218 SmramDescriptor = (EFI_SMRAM_DESCRIPTOR *) GET_GUID_HOB_DATA (GuidHob);\r
219 SmmS3ResumeState = (SMM_S3_RESUME_STATE *)(UINTN)SmramDescriptor->CpuStart;\r
220\r
221 SmmLockBoxContext = (SMM_LOCK_BOX_CONTEXT *)InternalSmstGetVendorTableByGuid (\r
222 SmmS3ResumeState->Signature,\r
223 (EFI_SMM_SYSTEM_TABLE2 *)(UINTN)SmmS3ResumeState->Smst,\r
224 &gEfiSmmLockBoxCommunicationGuid\r
225 );\r
226 ASSERT (SmmLockBoxContext != NULL);\r
227\r
228 return SmmLockBoxContext;\r
229}\r
230\r
231/**\r
232 This function will restore confidential information from lockbox in SMRAM directly.\r
233\r
234 @param Guid the guid to identify the confidential information\r
235 @param Buffer the address of the restored confidential information\r
236 NULL means restored to original address, Length MUST be NULL at same time.\r
237 @param Length the length of the restored confidential information\r
238\r
239 @retval RETURN_SUCCESS the information is restored successfully.\r
d1102dba 240 @retval RETURN_WRITE_PROTECTED Buffer and Length are NULL, but the LockBox has no\r
993532d0 241 LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE attribute.\r
242 @retval RETURN_BUFFER_TOO_SMALL the Length is too small to hold the confidential information.\r
243 @retval RETURN_NOT_FOUND the requested GUID not found.\r
244**/\r
245EFI_STATUS\r
246InternalRestoreLockBoxFromSmram (\r
247 IN GUID *Guid,\r
248 IN VOID *Buffer, OPTIONAL\r
249 IN OUT UINTN *Length OPTIONAL\r
250 )\r
251{\r
252 PEI_SMM_ACCESS_PPI *SmmAccess;\r
253 UINTN Index;\r
254 EFI_STATUS Status;\r
255 SMM_LOCK_BOX_CONTEXT *SmmLockBoxContext;\r
256 LIST_ENTRY *LockBoxQueue;\r
257 SMM_LOCK_BOX_DATA *LockBox;\r
258 VOID *RestoreBuffer;\r
259\r
260 //\r
261 // Get needed resource\r
262 //\r
263 Status = PeiServicesLocatePpi (\r
264 &gPeiSmmAccessPpiGuid,\r
265 0,\r
266 NULL,\r
267 (VOID **)&SmmAccess\r
268 );\r
269 if (!EFI_ERROR (Status)) {\r
270 for (Index = 0; !EFI_ERROR (Status); Index++) {\r
271 Status = SmmAccess->Open ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), SmmAccess, Index);\r
272 }\r
273 }\r
274\r
275 //\r
276 // Get LockBox context\r
277 //\r
278 SmmLockBoxContext = InternalGetSmmLockBoxContext ();\r
279 LockBoxQueue = (LIST_ENTRY *)(UINTN)SmmLockBoxContext->LockBoxDataAddress;\r
280\r
281 //\r
282 // We do NOT check Buffer address in SMRAM, because if SMRAM not locked, we trust the caller.\r
283 //\r
284\r
285 //\r
286 // Restore this, Buffer and Length MUST be both NULL or both non-NULL\r
287 //\r
288\r
289 //\r
290 // Find LockBox\r
291 //\r
292 LockBox = InternalFindLockBoxByGuidFromSmram (LockBoxQueue, Guid);\r
293 if (LockBox == NULL) {\r
294 //\r
295 // Not found\r
296 //\r
297 return EFI_NOT_FOUND;\r
298 }\r
299\r
300 //\r
301 // Set RestoreBuffer\r
302 //\r
303 if (Buffer != NULL) {\r
304 //\r
305 // restore to new buffer\r
306 //\r
307 RestoreBuffer = Buffer;\r
308 } else {\r
309 //\r
310 // restore to original buffer\r
311 //\r
312 if ((LockBox->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) == 0) {\r
313 return EFI_WRITE_PROTECTED;\r
314 }\r
315 RestoreBuffer = (VOID *)(UINTN)LockBox->Buffer;\r
316 }\r
317\r
318 //\r
319 // Set RestoreLength\r
320 //\r
321 if (Length != NULL) {\r
322 if (*Length < (UINTN)LockBox->Length) {\r
323 //\r
324 // Input buffer is too small to hold all data.\r
325 //\r
326 *Length = (UINTN)LockBox->Length;\r
327 return EFI_BUFFER_TOO_SMALL;\r
328 }\r
329 *Length = (UINTN)LockBox->Length;\r
330 }\r
331\r
332 //\r
333 // Restore data\r
334 //\r
335 CopyMem (RestoreBuffer, (VOID *)(UINTN)LockBox->SmramBuffer, (UINTN)LockBox->Length);\r
336\r
337 //\r
338 // Done\r
339 //\r
340 return EFI_SUCCESS;\r
341}\r
342\r
343/**\r
344 This function will restore confidential information from all lockbox which have RestoreInPlace attribute.\r
345\r
346 @retval RETURN_SUCCESS the information is restored successfully.\r
347**/\r
348EFI_STATUS\r
349InternalRestoreAllLockBoxInPlaceFromSmram (\r
350 VOID\r
351 )\r
352{\r
353 PEI_SMM_ACCESS_PPI *SmmAccess;\r
354 UINTN Index;\r
355 EFI_STATUS Status;\r
356 SMM_LOCK_BOX_CONTEXT *SmmLockBoxContext;\r
357 LIST_ENTRY *LockBoxQueue;\r
358 SMM_LOCK_BOX_DATA *LockBox;\r
359 LIST_ENTRY *Link;\r
360\r
361 //\r
362 // Get needed resource\r
363 //\r
364 Status = PeiServicesLocatePpi (\r
365 &gPeiSmmAccessPpiGuid,\r
366 0,\r
367 NULL,\r
368 (VOID **)&SmmAccess\r
369 );\r
370 if (!EFI_ERROR (Status)) {\r
371 for (Index = 0; !EFI_ERROR (Status); Index++) {\r
372 Status = SmmAccess->Open ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), SmmAccess, Index);\r
373 }\r
374 }\r
375\r
376 //\r
377 // Get LockBox context\r
378 //\r
379 SmmLockBoxContext = InternalGetSmmLockBoxContext ();\r
380 LockBoxQueue = (LIST_ENTRY *)(UINTN)SmmLockBoxContext->LockBoxDataAddress;\r
381\r
382 //\r
383 // We do NOT check Buffer address in SMRAM, because if SMRAM not locked, we trust the caller.\r
384 //\r
385\r
386 //\r
387 // Restore all, Buffer and Length MUST be NULL\r
388 //\r
389 for (Link = InternalInitLinkDxe (LockBoxQueue);\r
390 Link != LockBoxQueue;\r
391 Link = InternalNextLinkDxe (Link)) {\r
392 LockBox = BASE_CR (\r
393 Link,\r
394 SMM_LOCK_BOX_DATA,\r
395 Link\r
396 );\r
397 if ((LockBox->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) != 0) {\r
398 //\r
399 // Restore data\r
400 //\r
401 CopyMem ((VOID *)(UINTN)LockBox->Buffer, (VOID *)(UINTN)LockBox->SmramBuffer, (UINTN)LockBox->Length);\r
402 }\r
403 }\r
404 //\r
405 // Done\r
406 //\r
407 return EFI_SUCCESS;\r
408}\r
409\r
410/**\r
411 This function will save confidential information to lockbox.\r
412\r
413 @param Guid the guid to identify the confidential information\r
414 @param Buffer the address of the confidential information\r
415 @param Length the length of the confidential information\r
416\r
417 @retval RETURN_SUCCESS the information is saved successfully.\r
418 @retval RETURN_INVALID_PARAMETER the Guid is NULL, or Buffer is NULL, or Length is 0\r
419 @retval RETURN_ALREADY_STARTED the requested GUID already exist.\r
420 @retval RETURN_OUT_OF_RESOURCES no enough resource to save the information.\r
421 @retval RETURN_ACCESS_DENIED it is too late to invoke this interface\r
422 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
423 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
424**/\r
425RETURN_STATUS\r
426EFIAPI\r
427SaveLockBox (\r
428 IN GUID *Guid,\r
429 IN VOID *Buffer,\r
430 IN UINTN Length\r
431 )\r
432{\r
433 ASSERT (FALSE);\r
434\r
435 //\r
436 // No support to save at PEI phase\r
437 //\r
438 return RETURN_UNSUPPORTED;\r
439}\r
440\r
441/**\r
442 This function will set lockbox attributes.\r
443\r
444 @param Guid the guid to identify the confidential information\r
445 @param Attributes the attributes of the lockbox\r
446\r
447 @retval RETURN_SUCCESS the information is saved successfully.\r
448 @retval RETURN_INVALID_PARAMETER attributes is invalid.\r
449 @retval RETURN_NOT_FOUND the requested GUID not found.\r
450 @retval RETURN_ACCESS_DENIED it is too late to invoke this interface\r
451 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
452 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
453**/\r
454RETURN_STATUS\r
455EFIAPI\r
456SetLockBoxAttributes (\r
457 IN GUID *Guid,\r
458 IN UINT64 Attributes\r
459 )\r
460{\r
461 ASSERT (FALSE);\r
462\r
463 //\r
464 // No support to save at PEI phase\r
465 //\r
466 return RETURN_UNSUPPORTED;\r
467}\r
468\r
469/**\r
470 This function will update confidential information to lockbox.\r
471\r
472 @param Guid the guid to identify the original confidential information\r
473 @param Offset the offset of the original confidential information\r
474 @param Buffer the address of the updated confidential information\r
475 @param Length the length of the updated confidential information\r
476\r
477 @retval RETURN_SUCCESS the information is saved successfully.\r
478 @retval RETURN_INVALID_PARAMETER the Guid is NULL, or Buffer is NULL, or Length is 0.\r
479 @retval RETURN_NOT_FOUND the requested GUID not found.\r
480 @retval RETURN_BUFFER_TOO_SMALL the original buffer to too small to hold new information.\r
481 @retval RETURN_ACCESS_DENIED it is too late to invoke this interface\r
482 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
483 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
484**/\r
485RETURN_STATUS\r
486EFIAPI\r
487UpdateLockBox (\r
488 IN GUID *Guid,\r
489 IN UINTN Offset,\r
490 IN VOID *Buffer,\r
491 IN UINTN Length\r
492 )\r
493{\r
494 ASSERT (FALSE);\r
495\r
496 //\r
497 // No support to update at PEI phase\r
498 //\r
499 return RETURN_UNSUPPORTED;\r
500}\r
501\r
502/**\r
503 This function will restore confidential information from lockbox.\r
504\r
505 @param Guid the guid to identify the confidential information\r
506 @param Buffer the address of the restored confidential information\r
507 NULL means restored to original address, Length MUST be NULL at same time.\r
508 @param Length the length of the restored confidential information\r
509\r
510 @retval RETURN_SUCCESS the information is restored successfully.\r
511 @retval RETURN_INVALID_PARAMETER the Guid is NULL, or one of Buffer and Length is NULL.\r
d1102dba 512 @retval RETURN_WRITE_PROTECTED Buffer and Length are NULL, but the LockBox has no\r
993532d0 513 LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE attribute.\r
514 @retval RETURN_BUFFER_TOO_SMALL the Length is too small to hold the confidential information.\r
515 @retval RETURN_NOT_FOUND the requested GUID not found.\r
516 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
517 @retval RETURN_ACCESS_DENIED not allow to restore to the address\r
518 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
519**/\r
520RETURN_STATUS\r
521EFIAPI\r
522RestoreLockBox (\r
523 IN GUID *Guid,\r
524 IN VOID *Buffer, OPTIONAL\r
525 IN OUT UINTN *Length OPTIONAL\r
526 )\r
527{\r
528 EFI_STATUS Status;\r
529 EFI_PEI_SMM_COMMUNICATION_PPI *SmmCommunicationPpi;\r
530 EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *LockBoxParameterRestore;\r
531 EFI_SMM_COMMUNICATE_HEADER *CommHeader;\r
532 UINT8 CommBuffer[sizeof(EFI_GUID) + sizeof(UINT64) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)];\r
533 UINTN CommSize;\r
534 UINT64 MessageLength;\r
535\r
536 //\r
537 // Please aware that there is UINTN in EFI_SMM_COMMUNICATE_HEADER. It might be UINT64 in DXE, while it is UINT32 in PEI.\r
538 // typedef struct {\r
539 // EFI_GUID HeaderGuid;\r
540 // UINTN MessageLength;\r
541 // UINT8 Data[1];\r
542 // } EFI_SMM_COMMUNICATE_HEADER;\r
543 //\r
544\r
481ffd6f 545 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib RestoreLockBox - Enter\n"));\r
993532d0 546\r
547 //\r
548 // Basic check\r
549 //\r
550 if ((Guid == NULL) ||\r
551 ((Buffer == NULL) && (Length != NULL)) ||\r
552 ((Buffer != NULL) && (Length == NULL))) {\r
553 return EFI_INVALID_PARAMETER;\r
554 }\r
555\r
556 //\r
557 // Get needed resource\r
558 //\r
559 Status = PeiServicesLocatePpi (\r
560 &gEfiPeiSmmCommunicationPpiGuid,\r
561 0,\r
562 NULL,\r
563 (VOID **)&SmmCommunicationPpi\r
564 );\r
565 if (EFI_ERROR (Status)) {\r
481ffd6f 566 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib LocatePpi - (%r)\n", Status));\r
bd3afeb1 567 Status = InternalRestoreLockBoxFromSmram (Guid, Buffer, Length);\r
481ffd6f 568 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib RestoreLockBox - Exit (%r)\n", Status));\r
bd3afeb1 569 return Status;\r
993532d0 570 }\r
571\r
572 //\r
573 // Prepare parameter\r
574 //\r
575 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
576 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid));\r
577 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) ) {\r
578 MessageLength = sizeof(*LockBoxParameterRestore);\r
579 CopyMem (&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, MessageLength)], &MessageLength, sizeof(MessageLength));\r
580 } else {\r
581 CommHeader->MessageLength = sizeof(*LockBoxParameterRestore);\r
582 }\r
583\r
481ffd6f 584 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib CommBuffer - %x\n", &CommBuffer[0]));\r
993532d0 585 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) ) {\r
586 LockBoxParameterRestore = (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, MessageLength) + sizeof(UINT64)];\r
587 } else {\r
588 LockBoxParameterRestore = (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, MessageLength) + sizeof(UINTN)];\r
589 }\r
481ffd6f 590 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib LockBoxParameterRestore - %x\n", LockBoxParameterRestore));\r
993532d0 591 LockBoxParameterRestore->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_RESTORE;\r
592 LockBoxParameterRestore->Header.DataLength = sizeof(*LockBoxParameterRestore);\r
593 LockBoxParameterRestore->Header.ReturnStatus = (UINT64)-1;\r
594 if (Guid != 0) {\r
595 CopyMem (&LockBoxParameterRestore->Guid, Guid, sizeof(*Guid));\r
596 } else {\r
597 ZeroMem (&LockBoxParameterRestore->Guid, sizeof(*Guid));\r
598 }\r
599 LockBoxParameterRestore->Buffer = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;\r
600 if (Length != NULL) {\r
601 LockBoxParameterRestore->Length = (EFI_PHYSICAL_ADDRESS)*Length;\r
602 } else {\r
603 LockBoxParameterRestore->Length = 0;\r
604 }\r
605\r
606 //\r
607 // Send command\r
608 //\r
609 CommSize = sizeof(CommBuffer);\r
610 Status = SmmCommunicationPpi->Communicate (\r
611 SmmCommunicationPpi,\r
612 &CommBuffer[0],\r
613 &CommSize\r
614 );\r
615 if (Status == EFI_NOT_STARTED) {\r
616 //\r
617 // Pei SMM communication not ready yet, so we access SMRAM directly\r
618 //\r
481ffd6f 619 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib Communicate - (%r)\n", Status));\r
993532d0 620 Status = InternalRestoreLockBoxFromSmram (Guid, Buffer, Length);\r
621 LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;\r
622 if (Length != NULL) {\r
623 LockBoxParameterRestore->Length = (UINT64)*Length;\r
624 }\r
625 }\r
626 ASSERT_EFI_ERROR (Status);\r
627\r
628 if (Length != NULL) {\r
629 *Length = (UINTN)LockBoxParameterRestore->Length;\r
630 }\r
631\r
632 Status = (EFI_STATUS)LockBoxParameterRestore->Header.ReturnStatus;\r
633 if (Status != EFI_SUCCESS) {\r
634 // Need or MAX_BIT, because there might be case that SMM is X64 while PEI is IA32.\r
635 Status |= MAX_BIT;\r
636 }\r
637\r
481ffd6f 638 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib RestoreLockBox - Exit (%r)\n", Status));\r
993532d0 639\r
640 //\r
641 // Done\r
642 //\r
643 return Status;\r
644}\r
645\r
646/**\r
647 This function will restore confidential information from all lockbox which have RestoreInPlace attribute.\r
648\r
649 @retval RETURN_SUCCESS the information is restored successfully.\r
650 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
651 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
652**/\r
653RETURN_STATUS\r
654EFIAPI\r
655RestoreAllLockBoxInPlace (\r
656 VOID\r
657 )\r
658{\r
659 EFI_STATUS Status;\r
660 EFI_PEI_SMM_COMMUNICATION_PPI *SmmCommunicationPpi;\r
661 EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *LockBoxParameterRestoreAllInPlace;\r
662 EFI_SMM_COMMUNICATE_HEADER *CommHeader;\r
663 UINT8 CommBuffer[sizeof(EFI_GUID) + sizeof(UINT64) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)];\r
664 UINTN CommSize;\r
665 UINT64 MessageLength;\r
666\r
667 //\r
668 // Please aware that there is UINTN in EFI_SMM_COMMUNICATE_HEADER. It might be UINT64 in DXE, while it is UINT32 in PEI.\r
669 // typedef struct {\r
670 // EFI_GUID HeaderGuid;\r
671 // UINTN MessageLength;\r
672 // UINT8 Data[1];\r
673 // } EFI_SMM_COMMUNICATE_HEADER;\r
674 //\r
675\r
481ffd6f 676 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib RestoreAllLockBoxInPlace - Enter\n"));\r
993532d0 677\r
678 //\r
679 // Get needed resource\r
680 //\r
681 Status = PeiServicesLocatePpi (\r
682 &gEfiPeiSmmCommunicationPpiGuid,\r
683 0,\r
684 NULL,\r
685 (VOID **)&SmmCommunicationPpi\r
686 );\r
687 if (EFI_ERROR (Status)) {\r
481ffd6f 688 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib LocatePpi - (%r)\n", Status));\r
bd3afeb1 689 Status = InternalRestoreAllLockBoxInPlaceFromSmram ();\r
481ffd6f 690 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib RestoreAllLockBoxInPlace - Exit (%r)\n", Status));\r
bd3afeb1 691 return Status;\r
993532d0 692 }\r
693\r
694 //\r
695 // Prepare parameter\r
696 //\r
697 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
698 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid));\r
699 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) ) {\r
700 MessageLength = sizeof(*LockBoxParameterRestoreAllInPlace);\r
701 CopyMem (&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, MessageLength)], &MessageLength, sizeof(MessageLength));\r
702 } else {\r
703 CommHeader->MessageLength = sizeof(*LockBoxParameterRestoreAllInPlace);\r
704 }\r
705\r
706 if ((sizeof(UINTN) == sizeof(UINT32)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) ) {\r
707 LockBoxParameterRestoreAllInPlace = (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, MessageLength) + sizeof(UINT64)];\r
708 } else {\r
709 LockBoxParameterRestoreAllInPlace = (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, MessageLength) + sizeof(UINTN)];\r
710 }\r
711 LockBoxParameterRestoreAllInPlace->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE;\r
712 LockBoxParameterRestoreAllInPlace->Header.DataLength = sizeof(*LockBoxParameterRestoreAllInPlace);\r
713 LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)-1;\r
714\r
715 //\r
716 // Send command\r
717 //\r
718 CommSize = sizeof(CommBuffer);\r
719 Status = SmmCommunicationPpi->Communicate (\r
720 SmmCommunicationPpi,\r
721 &CommBuffer[0],\r
722 &CommSize\r
723 );\r
724 if (Status == EFI_NOT_STARTED) {\r
725 //\r
726 // Pei SMM communication not ready yet, so we access SMRAM directly\r
727 //\r
481ffd6f 728 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib Communicate - (%r)\n", Status));\r
993532d0 729 Status = InternalRestoreAllLockBoxInPlaceFromSmram ();\r
730 LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)Status;\r
731 }\r
732 ASSERT_EFI_ERROR (Status);\r
733\r
734 Status = (EFI_STATUS)LockBoxParameterRestoreAllInPlace->Header.ReturnStatus;\r
735 if (Status != EFI_SUCCESS) {\r
736 // Need or MAX_BIT, because there might be case that SMM is X64 while PEI is IA32.\r
737 Status |= MAX_BIT;\r
738 }\r
739\r
481ffd6f 740 DEBUG ((DEBUG_INFO, "SmmLockBoxPeiLib RestoreAllLockBoxInPlace - Exit (%r)\n", Status));\r
993532d0 741\r
742 //\r
743 // Done\r
744 //\r
745 return Status;\r
746}\r
747\r