]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h
1. Remove “Force clear PK” feature in AuthVarialbe driver.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / Variable.h
1 /** @file
2 The internal header file includes the common header files, defines
3 internal structure and functions used by Variable modules.
4
5 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #ifndef _VARIABLE_H_
17 #define _VARIABLE_H_
18
19 #include <PiDxe.h>
20 #include <Protocol/VariableWrite.h>
21 #include <Protocol/FaultTolerantWrite.h>
22 #include <Protocol/FirmwareVolumeBlock.h>
23 #include <Protocol/Variable.h>
24 #include <Library/PcdLib.h>
25 #include <Library/HobLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/DxeServicesTableLib.h>
28 #include <Library/UefiRuntimeLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/BaseLib.h>
34 #include <Library/SynchronizationLib.h>
35 #include <Library/MemoryAllocationLib.h>
36 #include <Library/BaseCryptLib.h>
37 #include <Library/PlatformSecureLib.h>
38 #include <Guid/GlobalVariable.h>
39 #include <Guid/EventGroup.h>
40 #include <Guid/AuthenticatedVariableFormat.h>
41 #include <Guid/ImageAuthentication.h>
42 #include <Guid/SystemNvDataGuid.h>
43
44 #define VARIABLE_RECLAIM_THRESHOLD (1024)
45
46 ///
47 /// The size of a 3 character ISO639 language code.
48 ///
49 #define ISO_639_2_ENTRY_SIZE 3
50
51 typedef enum {
52 VariableStoreTypeVolatile,
53 VariableStoreTypeHob,
54 VariableStoreTypeNv,
55 VariableStoreTypeMax
56 } VARIABLE_STORE_TYPE;
57
58 typedef struct {
59 VARIABLE_HEADER *CurrPtr;
60 VARIABLE_HEADER *EndPtr;
61 VARIABLE_HEADER *StartPtr;
62 BOOLEAN Volatile;
63 } VARIABLE_POINTER_TRACK;
64
65 typedef struct {
66 EFI_PHYSICAL_ADDRESS HobVariableBase;
67 EFI_PHYSICAL_ADDRESS VolatileVariableBase;
68 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase;
69 EFI_LOCK VariableServicesLock;
70 UINT32 ReentrantState;
71 } VARIABLE_GLOBAL;
72
73 typedef struct {
74 VARIABLE_GLOBAL VariableGlobal;
75 UINTN VolatileLastVariableOffset;
76 UINTN NonVolatileLastVariableOffset;
77 UINTN CommonVariableTotalSize;
78 UINTN HwErrVariableTotalSize;
79 CHAR8 *PlatformLangCodes;
80 CHAR8 *LangCodes;
81 CHAR8 *PlatformLang;
82 CHAR8 Lang[ISO_639_2_ENTRY_SIZE + 1];
83 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbInstance;
84 } VARIABLE_MODULE_GLOBAL;
85
86 typedef struct {
87 EFI_GUID *Guid;
88 CHAR16 *Name;
89 UINT32 Attributes;
90 UINTN DataSize;
91 VOID *Data;
92 } VARIABLE_CACHE_ENTRY;
93
94 /**
95 Writes a buffer to variable storage space, in the working block.
96
97 This function writes a buffer to variable storage space into a firmware
98 volume block device. The destination is specified by the parameter
99 VariableBase. Fault Tolerant Write protocol is used for writing.
100
101 @param VariableBase Base address of the variable to write.
102 @param Buffer Point to the data buffer.
103 @param BufferSize The number of bytes of the data Buffer.
104
105 @retval EFI_SUCCESS The function completed successfully.
106 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
107 @retval EFI_ABORTED The function could not complete successfully.
108
109 **/
110 EFI_STATUS
111 FtwVariableSpace (
112 IN EFI_PHYSICAL_ADDRESS VariableBase,
113 IN UINT8 *Buffer,
114 IN UINTN BufferSize
115 );
116
117 /**
118 Finds variable in storage blocks of volatile and non-volatile storage areas.
119
120 This code finds variable in storage blocks of volatile and non-volatile storage areas.
121 If VariableName is an empty string, then we just return the first
122 qualified variable without comparing VariableName and VendorGuid.
123 If IgnoreRtAttribute is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS Attribute
124 when searching existing variable, only VariableName and VendorGuid are compared.
125 Otherwise, variables with EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
126
127 @param[in] VariableName Name of the variable to be found.
128 @param[in] VendorGuid Vendor GUID to be found.
129 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
130 including the range searched and the target position.
131 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
132 base of volatile variable storage area, base of
133 NV variable storage area, and a lock.
134 @param[in] IgnoreRtAttribute Ignore RUNTIME_ACCESS attribute when searching variable.
135
136 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
137 VendorGuid is NULL.
138 @retval EFI_SUCCESS Variable successfully found.
139 @retval EFI_NOT_FOUND Variable not found
140
141 **/
142 EFI_STATUS
143 FindVariable (
144 IN CHAR16 *VariableName,
145 IN EFI_GUID *VendorGuid,
146 OUT VARIABLE_POINTER_TRACK *PtrTrack,
147 IN VARIABLE_GLOBAL *Global,
148 IN BOOLEAN IgnoreRtAttribute
149 );
150
151 /**
152
153 This code gets the pointer to the variable data.
154
155 @param Variable Pointer to the Variable Header.
156
157 @return Pointer to Variable Data.
158
159 **/
160 UINT8 *
161 GetVariableDataPtr (
162 IN VARIABLE_HEADER *Variable
163 );
164
165 /**
166
167 This code gets the size of variable data.
168
169 @param Variable Pointer to the Variable Header.
170
171 @return Size of variable in bytes.
172
173 **/
174 UINTN
175 DataSizeOfVariable (
176 IN VARIABLE_HEADER *Variable
177 );
178
179 /**
180 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
181 index of associated public key is needed.
182
183 @param[in] VariableName Name of variable.
184 @param[in] VendorGuid Guid of variable.
185 @param[in] Data Variable data.
186 @param[in] DataSize Size of data. 0 means delete.
187 @param[in] Attributes Attributes of the variable.
188 @param[in] KeyIndex Index of associated public key.
189 @param[in] MonotonicCount Value of associated monotonic count.
190 @param[in] Variable The variable information that is used to keep track of variable usage.
191
192 @param[in] TimeStamp Value of associated TimeStamp.
193
194 @retval EFI_SUCCESS The update operation is success.
195 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.
196
197 **/
198 EFI_STATUS
199 UpdateVariable (
200 IN CHAR16 *VariableName,
201 IN EFI_GUID *VendorGuid,
202 IN VOID *Data,
203 IN UINTN DataSize,
204 IN UINT32 Attributes OPTIONAL,
205 IN UINT32 KeyIndex OPTIONAL,
206 IN UINT64 MonotonicCount OPTIONAL,
207 IN VARIABLE_POINTER_TRACK *Variable,
208 IN EFI_TIME *TimeStamp OPTIONAL
209 );
210
211
212 /**
213 Return TRUE if ExitBootServices () has been called.
214
215 @retval TRUE If ExitBootServices () has been called.
216 **/
217 BOOLEAN
218 AtRuntime (
219 VOID
220 );
221
222 /**
223 Initializes a basic mutual exclusion lock.
224
225 This function initializes a basic mutual exclusion lock to the released state
226 and returns the lock. Each lock provides mutual exclusion access at its task
227 priority level. Since there is no preemption or multiprocessor support in EFI,
228 acquiring the lock only consists of raising to the locks TPL.
229 If Lock is NULL, then ASSERT().
230 If Priority is not a valid TPL value, then ASSERT().
231
232 @param Lock A pointer to the lock data structure to initialize.
233 @param Priority EFI TPL is associated with the lock.
234
235 @return The lock.
236
237 **/
238 EFI_LOCK *
239 InitializeLock (
240 IN OUT EFI_LOCK *Lock,
241 IN EFI_TPL Priority
242 );
243
244
245 /**
246 Acquires lock only at boot time. Simply returns at runtime.
247
248 This is a temperary function that will be removed when
249 EfiAcquireLock() in UefiLib can handle the call in UEFI
250 Runtimer driver in RT phase.
251 It calls EfiAcquireLock() at boot time, and simply returns
252 at runtime.
253
254 @param Lock A pointer to the lock to acquire.
255
256 **/
257 VOID
258 AcquireLockOnlyAtBootTime (
259 IN EFI_LOCK *Lock
260 );
261
262
263 /**
264 Releases lock only at boot time. Simply returns at runtime.
265
266 This is a temperary function which will be removed when
267 EfiReleaseLock() in UefiLib can handle the call in UEFI
268 Runtimer driver in RT phase.
269 It calls EfiReleaseLock() at boot time and simply returns
270 at runtime.
271
272 @param Lock A pointer to the lock to release.
273
274 **/
275 VOID
276 ReleaseLockOnlyAtBootTime (
277 IN EFI_LOCK *Lock
278 );
279
280 /**
281 Retrive the FVB protocol interface by HANDLE.
282
283 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
284 reading, writing, and erasing the target block.
285 @param[out] FvBlock The interface of FVB protocol
286
287 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
288 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
289 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
290
291 **/
292 EFI_STATUS
293 GetFvbByHandle (
294 IN EFI_HANDLE FvBlockHandle,
295 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
296 );
297
298
299 /**
300 Retrive the Swap Address Range protocol interface.
301
302 @param[out] SarProtocol The interface of SAR protocol
303
304 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
305 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
306 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
307
308 **/
309 EFI_STATUS
310 GetSarProtocol (
311 OUT VOID **SarProtocol
312 );
313
314 /**
315 Function returns an array of handles that support the FVB protocol
316 in a buffer allocated from pool.
317
318 @param[out] NumberHandles The number of handles returned in Buffer.
319 @param[out] Buffer A pointer to the buffer to return the requested
320 array of handles that support FVB protocol.
321
322 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
323 handles in Buffer was returned in NumberHandles.
324 @retval EFI_NOT_FOUND No FVB handle was found.
325 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
326 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
327
328 **/
329 EFI_STATUS
330 GetFvbCountAndBuffer (
331 OUT UINTN *NumberHandles,
332 OUT EFI_HANDLE **Buffer
333 );
334
335 /**
336 Initializes variable store area for non-volatile and volatile variable.
337
338 @retval EFI_SUCCESS Function successfully executed.
339 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
340
341 **/
342 EFI_STATUS
343 VariableCommonInitialize (
344 VOID
345 );
346
347 /**
348 This function reclaims variable storage if free size is below the threshold.
349
350 **/
351 VOID
352 ReclaimForOS(
353 VOID
354 );
355
356
357 /**
358 Initializes variable write service after FVB was ready.
359
360 @retval EFI_SUCCESS Function successfully executed.
361 @retval Others Fail to initialize the variable service.
362
363 **/
364 EFI_STATUS
365 VariableWriteServiceInitialize (
366 VOID
367 );
368
369 /**
370 Retrive the SMM Fault Tolerent Write protocol interface.
371
372 @param[out] FtwProtocol The interface of SMM Ftw protocol
373
374 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
375 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
376 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
377
378 **/
379 EFI_STATUS
380 GetFtwProtocol (
381 OUT VOID **FtwProtocol
382 );
383
384 /**
385 Get the proper fvb handle and/or fvb protocol by the given Flash address.
386
387 @param[in] Address The Flash address.
388 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
389 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
390
391 **/
392 EFI_STATUS
393 GetFvbInfoByAddress (
394 IN EFI_PHYSICAL_ADDRESS Address,
395 OUT EFI_HANDLE *FvbHandle OPTIONAL,
396 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
397 );
398
399 /**
400
401 This code finds variable in storage blocks (Volatile or Non-Volatile).
402
403 @param VariableName Name of Variable to be found.
404 @param VendorGuid Variable vendor GUID.
405 @param Attributes Attribute value of the variable found.
406 @param DataSize Size of Data found. If size is less than the
407 data, this value contains the required size.
408 @param Data Data pointer.
409
410 @return EFI_INVALID_PARAMETER Invalid parameter.
411 @return EFI_SUCCESS Find the specified variable.
412 @return EFI_NOT_FOUND Not found.
413 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
414
415 **/
416 EFI_STATUS
417 EFIAPI
418 VariableServiceGetVariable (
419 IN CHAR16 *VariableName,
420 IN EFI_GUID *VendorGuid,
421 OUT UINT32 *Attributes OPTIONAL,
422 IN OUT UINTN *DataSize,
423 OUT VOID *Data
424 );
425
426 /**
427
428 This code Finds the Next available variable.
429
430 @param VariableNameSize Size of the variable name.
431 @param VariableName Pointer to variable name.
432 @param VendorGuid Variable Vendor Guid.
433
434 @return EFI_INVALID_PARAMETER Invalid parameter.
435 @return EFI_SUCCESS Find the specified variable.
436 @return EFI_NOT_FOUND Not found.
437 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
438
439 **/
440 EFI_STATUS
441 EFIAPI
442 VariableServiceGetNextVariableName (
443 IN OUT UINTN *VariableNameSize,
444 IN OUT CHAR16 *VariableName,
445 IN OUT EFI_GUID *VendorGuid
446 );
447
448 /**
449
450 This code sets variable in storage blocks (Volatile or Non-Volatile).
451
452 @param VariableName Name of Variable to be found.
453 @param VendorGuid Variable vendor GUID.
454 @param Attributes Attribute value of the variable found
455 @param DataSize Size of Data found. If size is less than the
456 data, this value contains the required size.
457 @param Data Data pointer.
458
459 @return EFI_INVALID_PARAMETER Invalid parameter.
460 @return EFI_SUCCESS Set successfully.
461 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
462 @return EFI_NOT_FOUND Not found.
463 @return EFI_WRITE_PROTECTED Variable is read-only.
464
465 **/
466 EFI_STATUS
467 EFIAPI
468 VariableServiceSetVariable (
469 IN CHAR16 *VariableName,
470 IN EFI_GUID *VendorGuid,
471 IN UINT32 Attributes,
472 IN UINTN DataSize,
473 IN VOID *Data
474 );
475
476 /**
477
478 This code returns information about the EFI variables.
479
480 @param Attributes Attributes bitmask to specify the type of variables
481 on which to return information.
482 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
483 for the EFI variables associated with the attributes specified.
484 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
485 for EFI variables associated with the attributes specified.
486 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
487 associated with the attributes specified.
488
489 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
490 @return EFI_SUCCESS Query successfully.
491 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
492
493 **/
494 EFI_STATUS
495 EFIAPI
496 VariableServiceQueryVariableInfo (
497 IN UINT32 Attributes,
498 OUT UINT64 *MaximumVariableStorageSize,
499 OUT UINT64 *RemainingVariableStorageSize,
500 OUT UINT64 *MaximumVariableSize
501 );
502
503 extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
504
505 #endif