TianoCore EDK2 master
Loading...
Searching...
No Matches
LockBoxLib.c
Go to the documentation of this file.
1
12#include <Uefi.h>
14#include <Library/DebugLib.h>
15#include <Library/LockBoxLib.h>
16#include <Library/PcdLib.h>
17#include <LockBoxLib.h>
18
19#pragma pack(1)
20typedef struct {
21 EFI_GUID Guid;
22 EFI_PHYSICAL_ADDRESS OrigAddress;
23 EFI_PHYSICAL_ADDRESS CopyAddress;
24 UINT32 Size;
25 UINT64 Attributes;
27#pragma pack()
28
29LOCK_BOX_GLOBAL *mLockBoxGlobal = NULL;
30STATIC LOCK_BOX_ENTRY *StartOfEntries = NULL;
31STATIC LOCK_BOX_ENTRY *EndOfEntries = NULL;
32
33RETURN_STATUS
34EFIAPI
35LockBoxLibInitialize (
36 VOID
37 )
38{
39 UINTN NumEntries;
40
41 ASSERT (!FeaturePcdGet (PcdSmmSmramRequire));
42
43 if (PcdGet32 (PcdOvmfLockBoxStorageSize) < sizeof (LOCK_BOX_GLOBAL)) {
44 return RETURN_UNSUPPORTED;
45 }
46
47 mLockBoxGlobal = (LOCK_BOX_GLOBAL *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase);
48 StartOfEntries = ((LOCK_BOX_ENTRY *)(mLockBoxGlobal + 1));
49 NumEntries = ((PcdGet32 (PcdOvmfLockBoxStorageSize) - sizeof (LOCK_BOX_GLOBAL)) /
50 sizeof (LOCK_BOX_ENTRY));
51 EndOfEntries = StartOfEntries + NumEntries;
52 if (mLockBoxGlobal->Signature != LOCK_BOX_GLOBAL_SIGNATURE) {
53 //
54 // Note: This code depends on the lock box being cleared in early
55 // PEI before usage, so the SubPageBuffer and SubPageRemaining
56 // fields don't need to be set to 0.
57 //
58 mLockBoxGlobal->Signature = LOCK_BOX_GLOBAL_SIGNATURE;
59 }
60
61 return RETURN_SUCCESS;
62}
63
81EFIAPI
83 IN CONST EFI_GUID *Guid
84 )
85{
86 LOCK_BOX_ENTRY *Header;
87
88 for (Header = StartOfEntries; Header < EndOfEntries; Header++) {
89 if ((Header->Size == 0) || CompareGuid (Guid, &Header->Guid)) {
90 return Header;
91 }
92 }
93
94 return NULL;
95}
96
114RETURN_STATUS
115EFIAPI
117 IN GUID *Guid,
118 IN VOID *Buffer,
119 IN UINTN Length
120 )
121{
122 LOCK_BOX_ENTRY *Header;
123 VOID *CopyBuffer;
124
125 DEBUG ((
126 DEBUG_VERBOSE,
127 "%a: Guid=%g Buffer=%p Length=0x%x\n",
128 __func__,
129 Guid,
130 Buffer,
131 (UINT32)Length
132 ));
133
134 if ((Guid == NULL) || (Buffer == NULL) || (Length == 0)) {
136 }
137
138 if (Length > 0xFFFFFFFF) {
140 }
141
142 Header = FindHeaderByGuid (Guid);
143 if (Header == NULL) {
145 }
146
147 if (Header->Size > 0) {
149 }
150
151 CopyBuffer = AllocateAcpiNvsPool (Length);
152 if (CopyBuffer == NULL) {
154 }
155
156 //
157 // overwrite the current terminator header with new metadata
158 //
159 CopyGuid (&Header->Guid, Guid);
160 Header->OrigAddress = (UINTN)Buffer;
161 Header->CopyAddress = (UINTN)CopyBuffer;
162 Header->Size = (UINT32)Length;
163 Header->Attributes = 0;
164
165 //
166 // copy contents
167 //
168 CopyMem (CopyBuffer, Buffer, Length);
169
170 return RETURN_SUCCESS;
171}
172
187RETURN_STATUS
188EFIAPI
190 IN GUID *Guid,
191 IN UINT64 Attributes
192 )
193{
194 LOCK_BOX_ENTRY *Header;
195
196 DEBUG ((
197 DEBUG_VERBOSE,
198 "%a: Guid=%g Attributes=0x%Lx\n",
199 __func__,
200 Guid,
201 Attributes
202 ));
203
204 if (Guid == NULL) {
206 }
207
208 Header = FindHeaderByGuid (Guid);
209 if (!Header || (Header->Size == 0)) {
210 return RETURN_NOT_FOUND;
211 }
212
213 Header->Attributes = Attributes;
214
215 return RETURN_SUCCESS;
216}
217
242RETURN_STATUS
243EFIAPI
245 IN GUID *Guid,
246 IN UINTN Offset,
247 IN VOID *Buffer,
248 IN UINTN Length
249 )
250{
251 LOCK_BOX_ENTRY *Header;
252
253 DEBUG ((
254 DEBUG_VERBOSE,
255 "%a: Guid=%g Offset=0x%x Length=0x%x\n",
256 __func__,
257 Guid,
258 (UINT32)Offset,
259 (UINT32)Length
260 ));
261
262 if ((Guid == NULL) || (Buffer == NULL) || (Length == 0)) {
264 }
265
266 Header = FindHeaderByGuid (Guid);
267 if (!Header || (Header->Size == 0)) {
268 return RETURN_NOT_FOUND;
269 }
270
271 if ((Header->Size < Offset) ||
272 (Length > Header->Size - Offset))
273 {
275 }
276
277 CopyMem ((UINT8 *)(UINTN)(Header->CopyAddress) + Offset, Buffer, Length);
278
279 return RETURN_SUCCESS;
280}
281
305RETURN_STATUS
306EFIAPI
308 IN GUID *Guid,
309 IN VOID *Buffer OPTIONAL,
310 IN OUT UINTN *Length OPTIONAL
311 )
312{
313 LOCK_BOX_ENTRY *Header;
314
315 DEBUG ((
316 DEBUG_VERBOSE,
317 "%a: Guid=%g Buffer=%p\n",
318 __func__,
319 Guid,
320 Buffer
321 ));
322
323 if ((Guid == NULL) ||
324 ((Buffer == NULL) && (Length != NULL)) ||
325 ((Buffer != NULL) && (Length == NULL)))
326 {
327 return EFI_INVALID_PARAMETER;
328 }
329
330 Header = FindHeaderByGuid (Guid);
331 if (!Header || (Header->Size == 0)) {
332 return RETURN_NOT_FOUND;
333 }
334
335 if (Buffer == NULL) {
336 if (!(Header->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE)) {
338 }
339
340 if (Header->OrigAddress + (Header->Size - 1) > MAX_ADDRESS) {
341 return RETURN_UNSUPPORTED;
342 }
343
344 Buffer = (VOID *)(UINTN)Header->OrigAddress;
345 }
346
347 //
348 // Set RestoreLength
349 //
350 if (Length != NULL) {
351 if (Header->Size > *Length) {
352 //
353 // Input buffer is too small to hold all data.
354 //
355 *Length = Header->Size;
356 return EFI_BUFFER_TOO_SMALL;
357 }
358
359 *Length = Header->Size;
360 }
361
362 CopyMem (Buffer, (VOID *)(UINTN)Header->CopyAddress, Header->Size);
363
364 return RETURN_SUCCESS;
365}
366
376RETURN_STATUS
377EFIAPI
379 VOID
380 )
381{
382 LOCK_BOX_ENTRY *Header;
383
384 for (Header = StartOfEntries;
385 Header < EndOfEntries && Header->Size > 0;
386 Header++)
387 {
388 if (Header->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) {
389 VOID *Buffer;
390
391 if (Header->OrigAddress + (Header->Size - 1) > MAX_ADDRESS) {
392 return RETURN_UNSUPPORTED;
393 }
394
395 Buffer = (VOID *)(UINTN)Header->OrigAddress;
396 CopyMem (Buffer, (VOID *)(UINTN)Header->CopyAddress, Header->Size);
397 DEBUG ((
398 DEBUG_VERBOSE,
399 "%a: Guid=%g Buffer=%p\n",
400 __func__,
401 &Header->Guid,
402 Buffer
403 ));
404 }
405 }
406
407 return RETURN_SUCCESS;
408}
UINT64 UINTN
#define MAX_ADDRESS
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
VOID *EFIAPI AllocateAcpiNvsPool(IN UINTN AllocationSize)
Definition: LockBoxBase.c:30
RETURN_STATUS EFIAPI SetLockBoxAttributes(IN GUID *Guid, IN UINT64 Attributes)
Definition: LockBoxLib.c:189
RETURN_STATUS EFIAPI RestoreLockBox(IN GUID *Guid, IN VOID *Buffer OPTIONAL, IN OUT UINTN *Length OPTIONAL)
Definition: LockBoxLib.c:307
RETURN_STATUS EFIAPI RestoreAllLockBoxInPlace(VOID)
Definition: LockBoxLib.c:378
STATIC LOCK_BOX_ENTRY *EFIAPI FindHeaderByGuid(IN CONST EFI_GUID *Guid)
Definition: LockBoxLib.c:82
RETURN_STATUS EFIAPI UpdateLockBox(IN GUID *Guid, IN UINTN Offset, IN VOID *Buffer, IN UINTN Length)
Definition: LockBoxLib.c:244
RETURN_STATUS EFIAPI SaveLockBox(IN GUID *Guid, IN VOID *Buffer, IN UINTN Length)
Definition: LockBoxLib.c:116
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_BUFFER_TOO_SMALL
Definition: Base.h:1093
#define STATIC
Definition: Base.h:264
#define RETURN_WRITE_PROTECTED
Definition: Base.h:1109
#define RETURN_NOT_FOUND
Definition: Base.h:1142
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define RETURN_OUT_OF_RESOURCES
Definition: Base.h:1114
#define RETURN_SUCCESS
Definition: Base.h:1066
#define RETURN_ALREADY_STARTED
Definition: Base.h:1172
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define RETURN_INVALID_PARAMETER
Definition: Base.h:1076
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
#define FeaturePcdGet(TokenName)
Definition: PcdLib.h:50
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
Definition: Base.h:213