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