TianoCore EDK2 master
Loading...
Searching...
No Matches
SmmLockBoxDxeLib.c
Go to the documentation of this file.
1
9#include <PiDxe.h>
12#include <Library/BaseLib.h>
14#include <Library/LockBoxLib.h>
15#include <Library/DebugLib.h>
16#include <Library/UefiLib.h>
18#include <Guid/SmmLockBox.h>
20
22
23EFI_SMM_COMMUNICATION_PROTOCOL *mLockBoxSmmCommProtocol = NULL;
24UINT8 *mLockBoxSmmCommBuffer = NULL;
25
34 VOID
35 )
36{
37 EFI_STATUS Status;
38
39 //
40 // If the protocol has been got previously, return it.
41 //
42 if (mLockBoxSmmCommProtocol != NULL) {
43 return mLockBoxSmmCommProtocol;
44 }
45
46 Status = gBS->LocateProtocol (
47 &gEfiSmmCommunicationProtocolGuid,
48 NULL,
49 (VOID **)&mLockBoxSmmCommProtocol
50 );
51 if (EFI_ERROR (Status)) {
52 mLockBoxSmmCommProtocol = NULL;
53 }
54
55 return mLockBoxSmmCommProtocol;
56}
57
64UINT8 *
66 VOID
67 )
68{
69 EFI_STATUS Status;
70 UINTN MinimalSizeNeeded;
71 EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *PiSmmCommunicationRegionTable;
72 UINT32 Index;
74 UINTN Size;
75
76 //
77 // If the buffer has been got previously, return it.
78 //
79 if (mLockBoxSmmCommBuffer != NULL) {
80 return mLockBoxSmmCommBuffer;
81 }
82
83 MinimalSizeNeeded = sizeof (EFI_GUID) +
84 sizeof (UINTN) +
85 MAX (
87 MAX (
89 MAX (
91 MAX (
94 )
95 )
96 )
97 );
98
100 &gEdkiiPiSmmCommunicationRegionTableGuid,
101 (VOID **)&PiSmmCommunicationRegionTable
102 );
103 if (EFI_ERROR (Status)) {
104 mLockBoxSmmCommBuffer = NULL;
105 return mLockBoxSmmCommBuffer;
106 }
107
108 ASSERT (PiSmmCommunicationRegionTable != NULL);
109 Entry = (EFI_MEMORY_DESCRIPTOR *)(PiSmmCommunicationRegionTable + 1);
110 Size = 0;
111 for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) {
112 if (Entry->Type == EfiConventionalMemory) {
113 Size = EFI_PAGES_TO_SIZE ((UINTN)Entry->NumberOfPages);
114 if (Size >= MinimalSizeNeeded) {
115 break;
116 }
117 }
118
119 Entry = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Entry + PiSmmCommunicationRegionTable->DescriptorSize);
120 }
121
122 if (Index >= PiSmmCommunicationRegionTable->NumberOfEntries) {
123 mLockBoxSmmCommBuffer = NULL;
124 } else {
125 mLockBoxSmmCommBuffer = (UINT8 *)(UINTN)Entry->PhysicalStart;
126 }
127
128 return mLockBoxSmmCommBuffer;
129}
130
146RETURN_STATUS
147EFIAPI
149 IN GUID *Guid,
150 IN VOID *Buffer,
151 IN UINTN Length
152 )
153{
154 EFI_STATUS Status;
155 EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication;
156 EFI_SMM_LOCK_BOX_PARAMETER_SAVE *LockBoxParameterSave;
157 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
158 UINT8 TempCommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)];
159 UINT8 *CommBuffer;
160 UINTN CommSize;
161
162 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib SaveLockBox - Enter\n"));
163
164 //
165 // Basic check
166 //
167 if ((Guid == NULL) || (Buffer == NULL) || (Length == 0)) {
168 return EFI_INVALID_PARAMETER;
169 }
170
171 SmmCommunication = LockBoxGetSmmCommProtocol ();
172 if (SmmCommunication == NULL) {
173 return EFI_NOT_STARTED;
174 }
175
176 //
177 // Prepare parameter
178 //
179 CommBuffer = LockBoxGetSmmCommBuffer ();
180 if (CommBuffer == NULL) {
181 CommBuffer = &TempCommBuffer[0];
182 }
183
184 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];
185 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof (gEfiSmmLockBoxCommunicationGuid));
186 CommHeader->MessageLength = sizeof (*LockBoxParameterSave);
187
188 LockBoxParameterSave = (EFI_SMM_LOCK_BOX_PARAMETER_SAVE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
189 LockBoxParameterSave->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_SAVE;
190 LockBoxParameterSave->Header.DataLength = sizeof (*LockBoxParameterSave);
191 LockBoxParameterSave->Header.ReturnStatus = (UINT64)-1;
192 CopyMem (&LockBoxParameterSave->Guid, Guid, sizeof (*Guid));
193 LockBoxParameterSave->Buffer = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
194 LockBoxParameterSave->Length = (UINT64)Length;
195
196 //
197 // Send command
198 //
199 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE);
200 Status = SmmCommunication->Communicate (
201 SmmCommunication,
202 &CommBuffer[0],
203 &CommSize
204 );
205 ASSERT_EFI_ERROR (Status);
206
207 Status = (EFI_STATUS)LockBoxParameterSave->Header.ReturnStatus;
208
209 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib SaveLockBox - Exit (%r)\n", Status));
210
211 //
212 // Done
213 //
214 return Status;
215}
216
230RETURN_STATUS
231EFIAPI
233 IN GUID *Guid,
234 IN UINT64 Attributes
235 )
236{
237 EFI_STATUS Status;
238 EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication;
239 EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *LockBoxParameterSetAttributes;
240 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
241 UINT8 TempCommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)];
242 UINT8 *CommBuffer;
243 UINTN CommSize;
244
245 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib SetLockBoxAttributes - Enter\n"));
246
247 //
248 // Basic check
249 //
250 if ((Guid == NULL) ||
251 ((Attributes & ~(LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE | LOCK_BOX_ATTRIBUTE_RESTORE_IN_S3_ONLY)) != 0))
252 {
253 return EFI_INVALID_PARAMETER;
254 }
255
256 SmmCommunication = LockBoxGetSmmCommProtocol ();
257 if (SmmCommunication == NULL) {
258 return EFI_NOT_STARTED;
259 }
260
261 //
262 // Prepare parameter
263 //
264 CommBuffer = LockBoxGetSmmCommBuffer ();
265 if (CommBuffer == NULL) {
266 CommBuffer = &TempCommBuffer[0];
267 }
268
269 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];
270 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof (gEfiSmmLockBoxCommunicationGuid));
271 CommHeader->MessageLength = sizeof (*LockBoxParameterSetAttributes);
272
273 LockBoxParameterSetAttributes = (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
274 LockBoxParameterSetAttributes->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES;
275 LockBoxParameterSetAttributes->Header.DataLength = sizeof (*LockBoxParameterSetAttributes);
276 LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)-1;
277 CopyMem (&LockBoxParameterSetAttributes->Guid, Guid, sizeof (*Guid));
278 LockBoxParameterSetAttributes->Attributes = (UINT64)Attributes;
279
280 //
281 // Send command
282 //
283 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES);
284 Status = SmmCommunication->Communicate (
285 SmmCommunication,
286 &CommBuffer[0],
287 &CommSize
288 );
289 ASSERT_EFI_ERROR (Status);
290
291 Status = (EFI_STATUS)LockBoxParameterSetAttributes->Header.ReturnStatus;
292
293 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib SetLockBoxAttributes - Exit (%r)\n", Status));
294
295 //
296 // Done
297 //
298 return Status;
299}
300
320RETURN_STATUS
321EFIAPI
323 IN GUID *Guid,
324 IN UINTN Offset,
325 IN VOID *Buffer,
326 IN UINTN Length
327 )
328{
329 EFI_STATUS Status;
330 EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication;
331 EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *LockBoxParameterUpdate;
332 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
333 UINT8 TempCommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)];
334 UINT8 *CommBuffer;
335 UINTN CommSize;
336
337 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib UpdateLockBox - Enter\n"));
338
339 //
340 // Basic check
341 //
342 if ((Guid == NULL) || (Buffer == NULL) || (Length == 0)) {
343 return EFI_INVALID_PARAMETER;
344 }
345
346 SmmCommunication = LockBoxGetSmmCommProtocol ();
347 if (SmmCommunication == NULL) {
348 return EFI_NOT_STARTED;
349 }
350
351 //
352 // Prepare parameter
353 //
354 CommBuffer = LockBoxGetSmmCommBuffer ();
355 if (CommBuffer == NULL) {
356 CommBuffer = &TempCommBuffer[0];
357 }
358
359 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];
360 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof (gEfiSmmLockBoxCommunicationGuid));
361 CommHeader->MessageLength = sizeof (*LockBoxParameterUpdate);
362
363 LockBoxParameterUpdate = (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *)(UINTN)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
364 LockBoxParameterUpdate->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_UPDATE;
365 LockBoxParameterUpdate->Header.DataLength = sizeof (*LockBoxParameterUpdate);
366 LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)-1;
367 CopyMem (&LockBoxParameterUpdate->Guid, Guid, sizeof (*Guid));
368 LockBoxParameterUpdate->Offset = (UINT64)Offset;
369 LockBoxParameterUpdate->Buffer = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
370 LockBoxParameterUpdate->Length = (UINT64)Length;
371
372 //
373 // Send command
374 //
375 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE);
376 Status = SmmCommunication->Communicate (
377 SmmCommunication,
378 &CommBuffer[0],
379 &CommSize
380 );
381 ASSERT_EFI_ERROR (Status);
382
383 Status = (EFI_STATUS)LockBoxParameterUpdate->Header.ReturnStatus;
384
385 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib UpdateLockBox - Exit (%r)\n", Status));
386
387 //
388 // Done
389 //
390 return Status;
391}
392
411RETURN_STATUS
412EFIAPI
414 IN GUID *Guid,
415 IN VOID *Buffer OPTIONAL,
416 IN OUT UINTN *Length OPTIONAL
417 )
418{
419 EFI_STATUS Status;
420 EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication;
421 EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *LockBoxParameterRestore;
422 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
423 UINT8 TempCommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)];
424 UINT8 *CommBuffer;
425 UINTN CommSize;
426
427 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib RestoreLockBox - Enter\n"));
428
429 //
430 // Basic check
431 //
432 if ((Guid == NULL) ||
433 ((Buffer == NULL) && (Length != NULL)) ||
434 ((Buffer != NULL) && (Length == NULL)))
435 {
436 return EFI_INVALID_PARAMETER;
437 }
438
439 SmmCommunication = LockBoxGetSmmCommProtocol ();
440 if (SmmCommunication == NULL) {
441 return EFI_NOT_STARTED;
442 }
443
444 //
445 // Prepare parameter
446 //
447 CommBuffer = LockBoxGetSmmCommBuffer ();
448 if (CommBuffer == NULL) {
449 CommBuffer = &TempCommBuffer[0];
450 }
451
452 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];
453 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof (gEfiSmmLockBoxCommunicationGuid));
454 CommHeader->MessageLength = sizeof (*LockBoxParameterRestore);
455
456 LockBoxParameterRestore = (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
457 LockBoxParameterRestore->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_RESTORE;
458 LockBoxParameterRestore->Header.DataLength = sizeof (*LockBoxParameterRestore);
459 LockBoxParameterRestore->Header.ReturnStatus = (UINT64)-1;
460 CopyMem (&LockBoxParameterRestore->Guid, Guid, sizeof (*Guid));
461 LockBoxParameterRestore->Buffer = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
462 if (Length != NULL) {
463 LockBoxParameterRestore->Length = (EFI_PHYSICAL_ADDRESS)*Length;
464 } else {
465 LockBoxParameterRestore->Length = 0;
466 }
467
468 //
469 // Send command
470 //
471 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE);
472 Status = SmmCommunication->Communicate (
473 SmmCommunication,
474 &CommBuffer[0],
475 &CommSize
476 );
477 ASSERT_EFI_ERROR (Status);
478
479 if (Length != NULL) {
480 *Length = (UINTN)LockBoxParameterRestore->Length;
481 }
482
483 Status = (EFI_STATUS)LockBoxParameterRestore->Header.ReturnStatus;
484
485 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib RestoreLockBox - Exit (%r)\n", Status));
486
487 //
488 // Done
489 //
490 return Status;
491}
492
500RETURN_STATUS
501EFIAPI
503 VOID
504 )
505{
506 EFI_STATUS Status;
507 EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication;
508 EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *LockBoxParameterRestoreAllInPlace;
509 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
510 UINT8 TempCommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)];
511 UINT8 *CommBuffer;
512 UINTN CommSize;
513
514 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib RestoreAllLockBoxInPlace - Enter\n"));
515
516 SmmCommunication = LockBoxGetSmmCommProtocol ();
517 if (SmmCommunication == NULL) {
518 return EFI_NOT_STARTED;
519 }
520
521 //
522 // Prepare parameter
523 //
524 CommBuffer = LockBoxGetSmmCommBuffer ();
525 if (CommBuffer == NULL) {
526 CommBuffer = &TempCommBuffer[0];
527 }
528
529 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];
530 CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof (gEfiSmmLockBoxCommunicationGuid));
531 CommHeader->MessageLength = sizeof (*LockBoxParameterRestoreAllInPlace);
532
533 LockBoxParameterRestoreAllInPlace = (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
534 LockBoxParameterRestoreAllInPlace->Header.Command = EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE;
535 LockBoxParameterRestoreAllInPlace->Header.DataLength = sizeof (*LockBoxParameterRestoreAllInPlace);
536 LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)-1;
537
538 //
539 // Send command
540 //
541 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE);
542 Status = SmmCommunication->Communicate (
543 SmmCommunication,
544 &CommBuffer[0],
545 &CommSize
546 );
547 ASSERT_EFI_ERROR (Status);
548
549 Status = (EFI_STATUS)LockBoxParameterRestoreAllInPlace->Header.ReturnStatus;
550
551 DEBUG ((DEBUG_INFO, "SmmLockBoxDxeLib RestoreAllLockBoxInPlace - Exit (%r)\n", Status));
552
553 //
554 // Done
555 //
556 return Status;
557}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define IN
Definition: Base.h:279
#define OFFSET_OF(TYPE, Field)
Definition: Base.h:758
#define OUT
Definition: Base.h:284
#define MAX(a, b)
Definition: Base.h:992
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
UINT8 * LockBoxGetSmmCommBuffer(VOID)
RETURN_STATUS EFIAPI SetLockBoxAttributes(IN GUID *Guid, IN UINT64 Attributes)
EFI_SMM_COMMUNICATION_PROTOCOL * LockBoxGetSmmCommProtocol(VOID)
RETURN_STATUS EFIAPI RestoreLockBox(IN GUID *Guid, IN VOID *Buffer OPTIONAL, IN OUT UINTN *Length OPTIONAL)
RETURN_STATUS EFIAPI RestoreAllLockBoxInPlace(VOID)
RETURN_STATUS EFIAPI UpdateLockBox(IN GUID *Guid, IN UINTN Offset, IN VOID *Buffer, IN UINTN Length)
RETURN_STATUS EFIAPI SaveLockBox(IN GUID *Guid, IN VOID *Buffer, IN UINTN Length)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
GUID EFI_GUID
Definition: UefiBaseType.h:25
EFI_BOOT_SERVICES * gBS
EFI_STATUS EFIAPI EfiGetSystemConfigurationTable(IN EFI_GUID *TableGuid, OUT VOID **Table)
Definition: UefiLib.c:82
@ EfiConventionalMemory
Definition: Base.h:213