1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/** @file -- VariablePolicyHelperLib.c
This library contains helper functions for marshalling and registering
new policies with the VariablePolicy infrastructure.
This library is currently written against VariablePolicy revision 0x00010000.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Protocol/VariablePolicy.h>
/**
This internal helper function populates the header structure,
all common fields, and takes care of fix-ups.
NOTE: Only use this internally. Assumes correctly-sized buffers.
@param[out] EntPtr Pointer to the buffer to be populated.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] LockPolicyType LockPolicyType for the VariablePolicy.
**/
STATIC
VOID
PopulateCommonData (
OUT VARIABLE_POLICY_ENTRY *EntPtr,
IN CONST EFI_GUID *Namespace,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN UINT8 LockPolicyType
)
{
EntPtr->Version = VARIABLE_POLICY_ENTRY_REVISION;
CopyGuid( &EntPtr->Namespace, Namespace );
EntPtr->MinSize = MinSize;
EntPtr->MaxSize = MaxSize;
EntPtr->AttributesMustHave = AttributesMustHave;
EntPtr->AttributesCantHave = AttributesCantHave;
EntPtr->LockPolicyType = LockPolicyType;
// NOTE: As a heler, fix up MaxSize for compatibility with the old model.
if (EntPtr->MaxSize == 0) {
EntPtr->MaxSize = VARIABLE_POLICY_NO_MAX_SIZE;
}
return;
}
/**
This helper function will allocate and populate a new VariablePolicy
structure for a policy that does not contain any sub-structures (such as
VARIABLE_LOCK_ON_VAR_STATE_POLICY).
NOTE: Caller will need to free structure once finished.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] LockPolicyType LockPolicyType for the VariablePolicy.
@param[out] NewEntry If successful, will be set to a pointer to the allocated buffer containing the
new policy.
@retval EFI_SUCCESS Operation completed successfully and structure is populated.
@retval EFI_INVALID_PARAMETER Namespace is NULL.
@retval EFI_INVALID_PARAMETER LockPolicyType is invalid for a basic structure.
@retval EFI_BUFFER_TOO_SMALL Finished structure would not fit in UINT16 size.
@retval EFI_OUT_OF_RESOURCES Could not allocate sufficient space for structure.
**/
EFI_STATUS
EFIAPI
CreateBasicVariablePolicy (
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN UINT8 LockPolicyType,
OUT VARIABLE_POLICY_ENTRY **NewEntry
)
{
UINTN TotalSize;
UINTN NameSize;
VARIABLE_POLICY_ENTRY *EntPtr;
CHAR16 *CopyName;
// Check some initial invalid parameters for this function.
if (Namespace == NULL || NewEntry == NULL) {
return EFI_INVALID_PARAMETER;
}
if (LockPolicyType != VARIABLE_POLICY_TYPE_NO_LOCK &&
LockPolicyType != VARIABLE_POLICY_TYPE_LOCK_NOW &&
LockPolicyType != VARIABLE_POLICY_TYPE_LOCK_ON_CREATE) {
return EFI_INVALID_PARAMETER;
}
//
// Set NameSize to suppress incorrect compiler/analyzer warnings
//
NameSize = 0;
// Now we've gotta determine the total size of the buffer required for
// the VariablePolicy structure.
TotalSize = sizeof( VARIABLE_POLICY_ENTRY );
if (Name != NULL) {
NameSize = StrnSizeS( Name, MAX_UINT16 );
TotalSize += NameSize;
}
// Make sure the size fits within a VARIABLE_POLICY_ENTRY.Size.
ASSERT( TotalSize <= MAX_UINT16 );
if (TotalSize > MAX_UINT16) {
return EFI_BUFFER_TOO_SMALL;
}
// Allocate a buffer to hold all the data. We're on the home stretch.
*NewEntry = AllocatePool( TotalSize );
if (*NewEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
// If we're still here, we're basically done.
// Copy the data and GET... OUT....
EntPtr = *NewEntry;
PopulateCommonData ( EntPtr,
Namespace,
MinSize,
MaxSize,
AttributesMustHave,
AttributesCantHave,
LockPolicyType );
EntPtr->Size = (UINT16)TotalSize; // This is safe because we've already checked.
EntPtr->OffsetToName = sizeof(VARIABLE_POLICY_ENTRY);
if (Name != NULL) {
CopyName = (CHAR16*)((UINT8*)EntPtr + EntPtr->OffsetToName);
CopyMem( CopyName, Name, NameSize );
}
return EFI_SUCCESS;
}
/**
This helper function will allocate and populate a new VariablePolicy
structure for a policy with a lock type of VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE.
NOTE: Caller will need to free structure once finished.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] VarStateNamespace Pointer to the EFI_GUID for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace.
@param[in] VarStateValue Value for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value.
@param[in] VarStateName Pointer to the CHAR16 array for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name.
@param[out] NewEntry If successful, will be set to a pointer to the allocated buffer containing the
new policy.
@retval EFI_SUCCESS Operation completed successfully and structure is populated.
@retval EFI_INVALID_PARAMETER Namespace, VarStateNamespace, VarStateName is NULL.
@retval EFI_BUFFER_TOO_SMALL Finished structure would not fit in UINT16 size.
@retval EFI_OUT_OF_RESOURCES Could not allocate sufficient space for structure.
**/
EFI_STATUS
EFIAPI
CreateVarStateVariablePolicy (
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN CONST EFI_GUID *VarStateNamespace,
IN UINT8 VarStateValue,
IN CONST CHAR16 *VarStateName,
OUT VARIABLE_POLICY_ENTRY **NewEntry
)
{
UINTN TotalSize;
UINTN NameSize;
UINTN VarStateNameSize;
VARIABLE_POLICY_ENTRY *EntPtr;
CHAR16 *CopyName;
VARIABLE_LOCK_ON_VAR_STATE_POLICY *CopyPolicy;
// Check some initial invalid parameters for this function.
if (Namespace == NULL || VarStateNamespace == NULL ||
VarStateName == NULL || NewEntry == NULL) {
return EFI_INVALID_PARAMETER;
}
// Now we've gotta determine the total size of the buffer required for
// the VariablePolicy structure.
VarStateNameSize = StrnSizeS( VarStateName, MAX_UINT16 );
TotalSize = sizeof( VARIABLE_POLICY_ENTRY ) +
sizeof(VARIABLE_LOCK_ON_VAR_STATE_POLICY) +
VarStateNameSize;
if (Name != NULL) {
NameSize = StrnSizeS( Name, MAX_UINT16 );
TotalSize += NameSize;
}
// Make sure the size fits within a VARIABLE_POLICY_ENTRY.Size.
ASSERT( TotalSize <= MAX_UINT16 );
if (TotalSize > MAX_UINT16) {
return EFI_BUFFER_TOO_SMALL;
}
// Allocate a buffer to hold all the data. We're on the home stretch.
*NewEntry = AllocatePool( TotalSize );
if (*NewEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
// If we're still here, we're basically done.
// Copy the data and GET... OUT....
EntPtr = *NewEntry;
PopulateCommonData ( EntPtr,
Namespace,
MinSize,
MaxSize,
AttributesMustHave,
AttributesCantHave,
VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE );
EntPtr->Size = (UINT16)TotalSize; // This is safe because we've already checked.
EntPtr->OffsetToName = sizeof(VARIABLE_POLICY_ENTRY) +
sizeof(VARIABLE_LOCK_ON_VAR_STATE_POLICY) +
(UINT16)VarStateNameSize;
CopyPolicy = (VARIABLE_LOCK_ON_VAR_STATE_POLICY*)((UINT8*)EntPtr + sizeof(VARIABLE_POLICY_ENTRY));
CopyName = (CHAR16*)((UINT8*)CopyPolicy + sizeof(VARIABLE_LOCK_ON_VAR_STATE_POLICY));
CopyGuid( &CopyPolicy->Namespace, VarStateNamespace );
CopyPolicy->Value = VarStateValue;
CopyMem( CopyName, VarStateName, VarStateNameSize );
if (Name != NULL) {
CopyName = (CHAR16*)((UINT8*)EntPtr + EntPtr->OffsetToName);
CopyMem( CopyName, Name, NameSize );
}
return EFI_SUCCESS;
}
/**
This helper function does everything that CreateBasicVariablePolicy() does, but also
uses the passed in protocol to register the policy with the infrastructure.
Does not return a buffer, does not require the caller to free anything.
@param[in] VariablePolicy Pointer to a valid instance of the VariablePolicy protocol.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] LockPolicyType LockPolicyType for the VariablePolicy.
@retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL.
@retval EFI_STATUS Status returned by CreateBasicVariablePolicy() or RegisterVariablePolicy().
**/
EFI_STATUS
EFIAPI
RegisterBasicVariablePolicy (
IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy,
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN UINT8 LockPolicyType
)
{
VARIABLE_POLICY_ENTRY *NewEntry;
EFI_STATUS Status;
// Check the simple things.
if (VariablePolicy == NULL) {
return EFI_INVALID_PARAMETER;
}
// Create the new entry and make sure that everything worked.
NewEntry = NULL;
Status = CreateBasicVariablePolicy( Namespace,
Name,
MinSize,
MaxSize,
AttributesMustHave,
AttributesCantHave,
LockPolicyType,
&NewEntry );
// If that was successful, attempt to register the new policy.
if (!EFI_ERROR( Status )) {
Status = VariablePolicy->RegisterVariablePolicy( NewEntry );
}
// If we allocated the buffer, free the buffer.
if (NewEntry != NULL) {
FreePool( NewEntry );
}
return Status;
}
/**
This helper function does everything that CreateBasicVariablePolicy() does, but also
uses the passed in protocol to register the policy with the infrastructure.
Does not return a buffer, does not require the caller to free anything.
@param[in] VariablePolicy Pointer to a valid instance of the VariablePolicy protocol.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] VarStateNamespace Pointer to the EFI_GUID for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace.
@param[in] VarStateName Pointer to the CHAR16 array for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name.
@param[in] VarStateValue Value for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value.
@retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL.
@retval EFI_STATUS Status returned by CreateBasicVariablePolicy() or RegisterVariablePolicy().
**/
EFI_STATUS
EFIAPI
RegisterVarStateVariablePolicy (
IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy,
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN CONST EFI_GUID *VarStateNamespace,
IN CONST CHAR16 *VarStateName,
IN UINT8 VarStateValue
)
{
VARIABLE_POLICY_ENTRY *NewEntry;
EFI_STATUS Status;
// Check the simple things.
if (VariablePolicy == NULL) {
return EFI_INVALID_PARAMETER;
}
// Create the new entry and make sure that everything worked.
NewEntry = NULL;
Status = CreateVarStateVariablePolicy( Namespace,
Name,
MinSize,
MaxSize,
AttributesMustHave,
AttributesCantHave,
VarStateNamespace,
VarStateValue,
VarStateName,
&NewEntry );
// If that was successful, attempt to register the new policy.
if (!EFI_ERROR( Status )) {
Status = VariablePolicy->RegisterVariablePolicy( NewEntry );
}
// If we allocated the buffer, free the buffer.
if (NewEntry != NULL) {
FreePool( NewEntry );
}
return Status;
}
|