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