TianoCore EDK2 master
Loading...
Searching...
No Matches
VariablePolicyHelperLib.c
1
12#include <Uefi.h>
13
14#include <Library/BaseLib.h>
15#include <Library/DebugLib.h>
18
19#include <Protocol/VariablePolicy.h>
20
37VOID
38PopulateCommonData (
40 IN CONST EFI_GUID *Namespace,
41 IN UINT32 MinSize,
42 IN UINT32 MaxSize,
43 IN UINT32 AttributesMustHave,
44 IN UINT32 AttributesCantHave,
45 IN UINT8 LockPolicyType
46 )
47{
48 EntPtr->Version = VARIABLE_POLICY_ENTRY_REVISION;
49 CopyGuid (&EntPtr->Namespace, Namespace);
50 EntPtr->MinSize = MinSize;
51 EntPtr->MaxSize = MaxSize;
52 EntPtr->AttributesMustHave = AttributesMustHave;
53 EntPtr->AttributesCantHave = AttributesCantHave;
54 EntPtr->LockPolicyType = LockPolicyType;
55
56 // NOTE: As a heler, fix up MaxSize for compatibility with the old model.
57 if (EntPtr->MaxSize == 0) {
58 EntPtr->MaxSize = VARIABLE_POLICY_NO_MAX_SIZE;
59 }
60
61 return;
62}
63
90EFIAPI
91CreateBasicVariablePolicy (
92 IN CONST EFI_GUID *Namespace,
93 IN CONST CHAR16 *Name OPTIONAL,
94 IN UINT32 MinSize,
95 IN UINT32 MaxSize,
96 IN UINT32 AttributesMustHave,
97 IN UINT32 AttributesCantHave,
98 IN UINT8 LockPolicyType,
99 OUT VARIABLE_POLICY_ENTRY **NewEntry
100 )
101{
102 UINTN TotalSize;
103 UINTN NameSize;
104 VARIABLE_POLICY_ENTRY *EntPtr;
105 CHAR16 *CopyName;
106
107 // Check some initial invalid parameters for this function.
108 if ((Namespace == NULL) || (NewEntry == NULL)) {
109 return EFI_INVALID_PARAMETER;
110 }
111
112 if ((LockPolicyType != VARIABLE_POLICY_TYPE_NO_LOCK) &&
113 (LockPolicyType != VARIABLE_POLICY_TYPE_LOCK_NOW) &&
114 (LockPolicyType != VARIABLE_POLICY_TYPE_LOCK_ON_CREATE))
115 {
116 return EFI_INVALID_PARAMETER;
117 }
118
119 //
120 // Set NameSize to suppress incorrect compiler/analyzer warnings
121 //
122 NameSize = 0;
123
124 // Now we've gotta determine the total size of the buffer required for
125 // the VariablePolicy structure.
126 TotalSize = sizeof (VARIABLE_POLICY_ENTRY);
127 if (Name != NULL) {
128 NameSize = StrnSizeS (Name, MAX_UINT16);
129 TotalSize += NameSize;
130 }
131
132 // Make sure the size fits within a VARIABLE_POLICY_ENTRY.Size.
133 ASSERT (TotalSize <= MAX_UINT16);
134 if (TotalSize > MAX_UINT16) {
135 return EFI_BUFFER_TOO_SMALL;
136 }
137
138 // Allocate a buffer to hold all the data. We're on the home stretch.
139 *NewEntry = AllocatePool (TotalSize);
140 if (*NewEntry == NULL) {
141 return EFI_OUT_OF_RESOURCES;
142 }
143
144 // If we're still here, we're basically done.
145 // Copy the data and GET... OUT....
146 EntPtr = *NewEntry;
147 PopulateCommonData (
148 EntPtr,
149 Namespace,
150 MinSize,
151 MaxSize,
152 AttributesMustHave,
153 AttributesCantHave,
154 LockPolicyType
155 );
156 EntPtr->Size = (UINT16)TotalSize; // This is safe because we've already checked.
157 EntPtr->OffsetToName = sizeof (VARIABLE_POLICY_ENTRY);
158 if (Name != NULL) {
159 CopyName = (CHAR16 *)((UINT8 *)EntPtr + EntPtr->OffsetToName);
160 CopyMem (CopyName, Name, NameSize);
161 }
162
163 return EFI_SUCCESS;
164}
165
192EFIAPI
193CreateVarStateVariablePolicy (
194 IN CONST EFI_GUID *Namespace,
195 IN CONST CHAR16 *Name OPTIONAL,
196 IN UINT32 MinSize,
197 IN UINT32 MaxSize,
198 IN UINT32 AttributesMustHave,
199 IN UINT32 AttributesCantHave,
200 IN CONST EFI_GUID *VarStateNamespace,
201 IN UINT8 VarStateValue,
202 IN CONST CHAR16 *VarStateName,
203 OUT VARIABLE_POLICY_ENTRY **NewEntry
204 )
205{
206 UINTN TotalSize;
207 UINTN NameSize;
208 UINTN VarStateNameSize;
209 VARIABLE_POLICY_ENTRY *EntPtr;
210 CHAR16 *CopyName;
212
213 // Check some initial invalid parameters for this function.
214 if ((Namespace == NULL) || (VarStateNamespace == NULL) ||
215 (VarStateName == NULL) || (NewEntry == NULL))
216 {
217 return EFI_INVALID_PARAMETER;
218 }
219
220 // Now we've gotta determine the total size of the buffer required for
221 // the VariablePolicy structure.
222 VarStateNameSize = StrnSizeS (VarStateName, MAX_UINT16);
223 TotalSize = sizeof (VARIABLE_POLICY_ENTRY) +
225 VarStateNameSize;
226 if (Name != NULL) {
227 NameSize = StrnSizeS (Name, MAX_UINT16);
228 TotalSize += NameSize;
229 }
230
231 // Make sure the size fits within a VARIABLE_POLICY_ENTRY.Size.
232 ASSERT (TotalSize <= MAX_UINT16);
233 if (TotalSize > MAX_UINT16) {
234 return EFI_BUFFER_TOO_SMALL;
235 }
236
237 // Allocate a buffer to hold all the data. We're on the home stretch.
238 *NewEntry = AllocatePool (TotalSize);
239 if (*NewEntry == NULL) {
240 return EFI_OUT_OF_RESOURCES;
241 }
242
243 // If we're still here, we're basically done.
244 // Copy the data and GET... OUT....
245 EntPtr = *NewEntry;
246 PopulateCommonData (
247 EntPtr,
248 Namespace,
249 MinSize,
250 MaxSize,
251 AttributesMustHave,
252 AttributesCantHave,
253 VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE
254 );
255 EntPtr->Size = (UINT16)TotalSize; // This is safe because we've already checked.
256 EntPtr->OffsetToName = sizeof (VARIABLE_POLICY_ENTRY) +
258 (UINT16)VarStateNameSize;
259
260 CopyPolicy = (VARIABLE_LOCK_ON_VAR_STATE_POLICY *)((UINT8 *)EntPtr + sizeof (VARIABLE_POLICY_ENTRY));
261 CopyName = (CHAR16 *)((UINT8 *)CopyPolicy + sizeof (VARIABLE_LOCK_ON_VAR_STATE_POLICY));
262 CopyGuid (&CopyPolicy->Namespace, VarStateNamespace);
263 CopyPolicy->Value = VarStateValue;
264 CopyMem (CopyName, VarStateName, VarStateNameSize);
265
266 if (Name != NULL) {
267 CopyName = (CHAR16 *)((UINT8 *)EntPtr + EntPtr->OffsetToName);
268 CopyMem (CopyName, Name, NameSize);
269 }
270
271 return EFI_SUCCESS;
272}
273
294EFIAPI
295RegisterBasicVariablePolicy (
296 IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy,
297 IN CONST EFI_GUID *Namespace,
298 IN CONST CHAR16 *Name OPTIONAL,
299 IN UINT32 MinSize,
300 IN UINT32 MaxSize,
301 IN UINT32 AttributesMustHave,
302 IN UINT32 AttributesCantHave,
303 IN UINT8 LockPolicyType
304 )
305{
306 VARIABLE_POLICY_ENTRY *NewEntry;
307 EFI_STATUS Status;
308
309 // Check the simple things.
310 if (VariablePolicy == NULL) {
311 return EFI_INVALID_PARAMETER;
312 }
313
314 // Create the new entry and make sure that everything worked.
315 NewEntry = NULL;
316 Status = CreateBasicVariablePolicy (
317 Namespace,
318 Name,
319 MinSize,
320 MaxSize,
321 AttributesMustHave,
322 AttributesCantHave,
323 LockPolicyType,
324 &NewEntry
325 );
326
327 // If that was successful, attempt to register the new policy.
328 if (!EFI_ERROR (Status)) {
329 Status = VariablePolicy->RegisterVariablePolicy (NewEntry);
330 }
331
332 // If we allocated the buffer, free the buffer.
333 if (NewEntry != NULL) {
334 FreePool (NewEntry);
335 }
336
337 return Status;
338}
339
362EFIAPI
363RegisterVarStateVariablePolicy (
364 IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy,
365 IN CONST EFI_GUID *Namespace,
366 IN CONST CHAR16 *Name OPTIONAL,
367 IN UINT32 MinSize,
368 IN UINT32 MaxSize,
369 IN UINT32 AttributesMustHave,
370 IN UINT32 AttributesCantHave,
371 IN CONST EFI_GUID *VarStateNamespace,
372 IN CONST CHAR16 *VarStateName,
373 IN UINT8 VarStateValue
374 )
375{
376 VARIABLE_POLICY_ENTRY *NewEntry;
377 EFI_STATUS Status;
378
379 // Check the simple things.
380 if (VariablePolicy == NULL) {
381 return EFI_INVALID_PARAMETER;
382 }
383
384 // Create the new entry and make sure that everything worked.
385 NewEntry = NULL;
386 Status = CreateVarStateVariablePolicy (
387 Namespace,
388 Name,
389 MinSize,
390 MaxSize,
391 AttributesMustHave,
392 AttributesCantHave,
393 VarStateNamespace,
394 VarStateValue,
395 VarStateName,
396 &NewEntry
397 );
398
399 // If that was successful, attempt to register the new policy.
400 if (!EFI_ERROR (Status)) {
401 Status = VariablePolicy->RegisterVariablePolicy (NewEntry);
402 }
403
404 // If we allocated the buffer, free the buffer.
405 if (NewEntry != NULL) {
406 FreePool (NewEntry);
407 }
408
409 return Status;
410}
UINT64 UINTN
UINTN EFIAPI StrnSizeS(IN CONST CHAR16 *String, IN UINTN MaxSize)
Definition: SafeString.c:176
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Base.h:213