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