2 The internal header file includes the common header files, defines
3 internal structure and functions used by Variable modules.
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
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.
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>
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)
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)
62 } GLOBAL_VARIABLE_ENTRY
;
65 /// The size of a 3 character ISO639 language code.
67 #define ISO_639_2_ENTRY_SIZE 3
70 VariableStoreTypeVolatile
,
74 } VARIABLE_STORE_TYPE
;
77 VARIABLE_HEADER
*CurrPtr
;
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.
84 VARIABLE_HEADER
*InDeletedTransitionPtr
;
85 VARIABLE_HEADER
*EndPtr
;
86 VARIABLE_HEADER
*StartPtr
;
88 } VARIABLE_POINTER_TRACK
;
91 EFI_PHYSICAL_ADDRESS HobVariableBase
;
92 EFI_PHYSICAL_ADDRESS VolatileVariableBase
;
93 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase
;
94 EFI_LOCK VariableServicesLock
;
95 UINT32 ReentrantState
;
99 VARIABLE_GLOBAL VariableGlobal
;
100 UINTN VolatileLastVariableOffset
;
101 UINTN NonVolatileLastVariableOffset
;
102 UINTN CommonVariableTotalSize
;
103 UINTN HwErrVariableTotalSize
;
104 CHAR8
*PlatformLangCodes
;
107 CHAR8 Lang
[ISO_639_2_ENTRY_SIZE
+ 1];
108 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbInstance
;
109 } VARIABLE_MODULE_GLOBAL
;
117 } VARIABLE_CACHE_ENTRY
;
126 Flush the HOB variable to flash.
128 @param[in] VariableName Name of variable has been updated or deleted.
129 @param[in] VendorGuid Guid of variable has been updated or deleted.
133 FlushHobVariableToFlash (
134 IN CHAR16
*VariableName
,
135 IN EFI_GUID
*VendorGuid
139 Writes a buffer to variable storage space, in the working block.
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.
145 @param VariableBase Base address of the variable to write.
146 @param VariableBuffer Point to the variable data buffer.
148 @retval EFI_SUCCESS The function completed successfully.
149 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
150 @retval EFI_ABORTED The function could not complete successfully.
155 IN EFI_PHYSICAL_ADDRESS VariableBase
,
156 IN VARIABLE_STORE_HEADER
*VariableBuffer
160 Finds variable in storage blocks of volatile and non-volatile storage areas.
162 This code finds variable in storage blocks of volatile and non-volatile storage areas.
163 If VariableName is an empty string, then we just return the first
164 qualified variable without comparing VariableName and VendorGuid.
165 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
166 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
167 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
169 @param[in] VariableName Name of the variable to be found.
170 @param[in] VendorGuid Vendor GUID to be found.
171 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
172 including the range searched and the target position.
173 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
174 base of volatile variable storage area, base of
175 NV variable storage area, and a lock.
176 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
177 check at runtime when searching variable.
179 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
181 @retval EFI_SUCCESS Variable successfully found.
182 @retval EFI_NOT_FOUND Variable not found
187 IN CHAR16
*VariableName
,
188 IN EFI_GUID
*VendorGuid
,
189 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
190 IN VARIABLE_GLOBAL
*Global
,
191 IN BOOLEAN IgnoreRtCheck
196 This code gets the pointer to the variable data.
198 @param Variable Pointer to the Variable Header.
200 @return Pointer to Variable Data.
205 IN VARIABLE_HEADER
*Variable
210 This code gets the size of variable data.
212 @param Variable Pointer to the Variable Header.
214 @return Size of variable in bytes.
219 IN VARIABLE_HEADER
*Variable
223 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
224 index of associated public key is needed.
226 @param[in] VariableName Name of variable.
227 @param[in] VendorGuid Guid of variable.
228 @param[in] Data Variable data.
229 @param[in] DataSize Size of data. 0 means delete.
230 @param[in] Attributes Attributes of the variable.
231 @param[in] KeyIndex Index of associated public key.
232 @param[in] MonotonicCount Value of associated monotonic count.
233 @param[in, out] Variable The variable information that is used to keep track of variable usage.
235 @param[in] TimeStamp Value of associated TimeStamp.
237 @retval EFI_SUCCESS The update operation is success.
238 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.
243 IN CHAR16
*VariableName
,
244 IN EFI_GUID
*VendorGuid
,
247 IN UINT32 Attributes OPTIONAL
,
248 IN UINT32 KeyIndex OPTIONAL
,
249 IN UINT64 MonotonicCount OPTIONAL
,
250 IN OUT VARIABLE_POINTER_TRACK
*Variable
,
251 IN EFI_TIME
*TimeStamp OPTIONAL
256 Return TRUE if ExitBootServices () has been called.
258 @retval TRUE If ExitBootServices () has been called.
266 Initializes a basic mutual exclusion lock.
268 This function initializes a basic mutual exclusion lock to the released state
269 and returns the lock. Each lock provides mutual exclusion access at its task
270 priority level. Since there is no preemption or multiprocessor support in EFI,
271 acquiring the lock only consists of raising to the locks TPL.
272 If Lock is NULL, then ASSERT().
273 If Priority is not a valid TPL value, then ASSERT().
275 @param Lock A pointer to the lock data structure to initialize.
276 @param Priority EFI TPL is associated with the lock.
283 IN OUT EFI_LOCK
*Lock
,
289 Acquires lock only at boot time. Simply returns at runtime.
291 This is a temperary function that will be removed when
292 EfiAcquireLock() in UefiLib can handle the call in UEFI
293 Runtimer driver in RT phase.
294 It calls EfiAcquireLock() at boot time, and simply returns
297 @param Lock A pointer to the lock to acquire.
301 AcquireLockOnlyAtBootTime (
307 Releases lock only at boot time. Simply returns at runtime.
309 This is a temperary function which will be removed when
310 EfiReleaseLock() in UefiLib can handle the call in UEFI
311 Runtimer driver in RT phase.
312 It calls EfiReleaseLock() at boot time and simply returns
315 @param Lock A pointer to the lock to release.
319 ReleaseLockOnlyAtBootTime (
324 Retrive the FVB protocol interface by HANDLE.
326 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
327 reading, writing, and erasing the target block.
328 @param[out] FvBlock The interface of FVB protocol
330 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
331 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
332 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
337 IN EFI_HANDLE FvBlockHandle
,
338 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
343 Retrive the Swap Address Range protocol interface.
345 @param[out] SarProtocol The interface of SAR protocol
347 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
348 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
349 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
354 OUT VOID
**SarProtocol
358 Function returns an array of handles that support the FVB protocol
359 in a buffer allocated from pool.
361 @param[out] NumberHandles The number of handles returned in Buffer.
362 @param[out] Buffer A pointer to the buffer to return the requested
363 array of handles that support FVB protocol.
365 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
366 handles in Buffer was returned in NumberHandles.
367 @retval EFI_NOT_FOUND No FVB handle was found.
368 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
369 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
373 GetFvbCountAndBuffer (
374 OUT UINTN
*NumberHandles
,
375 OUT EFI_HANDLE
**Buffer
379 Initializes variable store area for non-volatile and volatile variable.
381 @retval EFI_SUCCESS Function successfully executed.
382 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
386 VariableCommonInitialize (
392 Variable store garbage collection and reclaim operation.
394 If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.
395 If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex
396 for all the count-based authenticate variable in NV storage.
398 @param[in] VariableBase Base address of variable store.
399 @param[out] LastVariableOffset Offset of last variable.
400 @param[in] IsVolatile The variable store is volatile or not;
401 if it is non-volatile, need FTW.
402 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
403 @param[in] NewVariable Pointer to new variable.
404 @param[in] NewVariableSize New variable size.
405 @param[in] ReclaimPubKeyStore Reclaim for public key database or not.
407 @return EFI_SUCCESS Reclaim operation has finished successfully.
408 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
409 @return EFI_DEVICE_ERROR The public key database doesn't exist.
410 @return Others Unexpect error happened during reclaim operation.
415 IN EFI_PHYSICAL_ADDRESS VariableBase
,
416 OUT UINTN
*LastVariableOffset
,
417 IN BOOLEAN IsVolatile
,
418 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
419 IN VARIABLE_HEADER
*NewVariable
,
420 IN UINTN NewVariableSize
,
421 IN BOOLEAN ReclaimPubKeyStore
425 This function reclaims variable storage if free size is below the threshold.
435 Initializes variable write service after FVB was ready.
437 @retval EFI_SUCCESS Function successfully executed.
438 @retval Others Fail to initialize the variable service.
442 VariableWriteServiceInitialize (
447 Retrive the SMM Fault Tolerent Write protocol interface.
449 @param[out] FtwProtocol The interface of SMM Ftw protocol
451 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
452 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
453 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
458 OUT VOID
**FtwProtocol
462 Get the proper fvb handle and/or fvb protocol by the given Flash address.
464 @param[in] Address The Flash address.
465 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
466 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
470 GetFvbInfoByAddress (
471 IN EFI_PHYSICAL_ADDRESS Address
,
472 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
473 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
478 This code finds variable in storage blocks (Volatile or Non-Volatile).
480 Caution: This function may receive untrusted input.
481 This function may be invoked in SMM mode, and datasize and data are external input.
482 This function will do basic validation, before parse the data.
484 @param VariableName Name of Variable to be found.
485 @param VendorGuid Variable vendor GUID.
486 @param Attributes Attribute value of the variable found.
487 @param DataSize Size of Data found. If size is less than the
488 data, this value contains the required size.
489 @param Data Data pointer.
491 @return EFI_INVALID_PARAMETER Invalid parameter.
492 @return EFI_SUCCESS Find the specified variable.
493 @return EFI_NOT_FOUND Not found.
494 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
499 VariableServiceGetVariable (
500 IN CHAR16
*VariableName
,
501 IN EFI_GUID
*VendorGuid
,
502 OUT UINT32
*Attributes OPTIONAL
,
503 IN OUT UINTN
*DataSize
,
509 This code Finds the Next available variable.
511 Caution: This function may receive untrusted input.
512 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
514 @param VariableNameSize Size of the variable name.
515 @param VariableName Pointer to variable name.
516 @param VendorGuid Variable Vendor Guid.
518 @return EFI_INVALID_PARAMETER Invalid parameter.
519 @return EFI_SUCCESS Find the specified variable.
520 @return EFI_NOT_FOUND Not found.
521 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
526 VariableServiceGetNextVariableName (
527 IN OUT UINTN
*VariableNameSize
,
528 IN OUT CHAR16
*VariableName
,
529 IN OUT EFI_GUID
*VendorGuid
534 This code sets variable in storage blocks (Volatile or Non-Volatile).
536 Caution: This function may receive untrusted input.
537 This function may be invoked in SMM mode, and datasize and data are external input.
538 This function will do basic validation, before parse the data.
539 This function will parse the authentication carefully to avoid security issues, like
540 buffer overflow, integer overflow.
541 This function will check attribute carefully to avoid authentication bypass.
543 @param VariableName Name of Variable to be found.
544 @param VendorGuid Variable vendor GUID.
545 @param Attributes Attribute value of the variable found
546 @param DataSize Size of Data found. If size is less than the
547 data, this value contains the required size.
548 @param Data Data pointer.
550 @return EFI_INVALID_PARAMETER Invalid parameter.
551 @return EFI_SUCCESS Set successfully.
552 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
553 @return EFI_NOT_FOUND Not found.
554 @return EFI_WRITE_PROTECTED Variable is read-only.
559 VariableServiceSetVariable (
560 IN CHAR16
*VariableName
,
561 IN EFI_GUID
*VendorGuid
,
562 IN UINT32 Attributes
,
569 This code returns information about the EFI variables.
571 Caution: This function may receive untrusted input.
572 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
574 @param Attributes Attributes bitmask to specify the type of variables
575 on which to return information.
576 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
577 for the EFI variables associated with the attributes specified.
578 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
579 for EFI variables associated with the attributes specified.
580 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
581 associated with the attributes specified.
583 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
584 @return EFI_SUCCESS Query successfully.
585 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
590 VariableServiceQueryVariableInfo (
591 IN UINT32 Attributes
,
592 OUT UINT64
*MaximumVariableStorageSize
,
593 OUT UINT64
*RemainingVariableStorageSize
,
594 OUT UINT64
*MaximumVariableSize
598 Mark a variable that will become read-only after leaving the DXE phase of execution.
600 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
601 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
602 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
604 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
605 as pending to be read-only.
606 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
607 Or VariableName is an empty string.
608 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
609 already been signaled.
610 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
614 VariableLockRequestToLock (
615 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
616 IN CHAR16
*VariableName
,
617 IN EFI_GUID
*VendorGuid
620 extern VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;