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