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