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