2 The internal header file includes the common header files, defines
3 internal structure and functions used by Variable modules.
5 Copyright (c) 2009 - 2012, 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 <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>
44 #define VARIABLE_RECLAIM_THRESHOLD (1024)
47 /// The size of a 3 character ISO639 language code.
49 #define ISO_639_2_ENTRY_SIZE 3
52 VariableStoreTypeVolatile
,
56 } VARIABLE_STORE_TYPE
;
59 VARIABLE_HEADER
*CurrPtr
;
60 VARIABLE_HEADER
*EndPtr
;
61 VARIABLE_HEADER
*StartPtr
;
63 } VARIABLE_POINTER_TRACK
;
66 EFI_PHYSICAL_ADDRESS HobVariableBase
;
67 EFI_PHYSICAL_ADDRESS VolatileVariableBase
;
68 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase
;
69 EFI_LOCK VariableServicesLock
;
70 UINT32 ReentrantState
;
74 VARIABLE_GLOBAL VariableGlobal
;
75 UINTN VolatileLastVariableOffset
;
76 UINTN NonVolatileLastVariableOffset
;
77 UINTN CommonVariableTotalSize
;
78 UINTN HwErrVariableTotalSize
;
79 CHAR8
*PlatformLangCodes
;
82 CHAR8 Lang
[ISO_639_2_ENTRY_SIZE
+ 1];
83 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbInstance
;
84 } VARIABLE_MODULE_GLOBAL
;
92 } VARIABLE_CACHE_ENTRY
;
95 Writes a buffer to variable storage space, in the working block.
97 This function writes a buffer to variable storage space into a firmware
98 volume block device. The destination is specified by the parameter
99 VariableBase. Fault Tolerant Write protocol is used for writing.
101 @param VariableBase Base address of the variable to write.
102 @param Buffer Point to the data buffer.
103 @param BufferSize The number of bytes of the data Buffer.
105 @retval EFI_SUCCESS The function completed successfully.
106 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
107 @retval EFI_ABORTED The function could not complete successfully.
112 IN EFI_PHYSICAL_ADDRESS VariableBase
,
118 Finds variable in storage blocks of volatile and non-volatile storage areas.
120 This code finds variable in storage blocks of volatile and non-volatile storage areas.
121 If VariableName is an empty string, then we just return the first
122 qualified variable without comparing VariableName and VendorGuid.
123 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
124 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
125 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
127 @param[in] VariableName Name of the variable to be found.
128 @param[in] VendorGuid Vendor GUID to be found.
129 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
130 including the range searched and the target position.
131 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
132 base of volatile variable storage area, base of
133 NV variable storage area, and a lock.
134 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
135 check at runtime when searching variable.
137 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
139 @retval EFI_SUCCESS Variable successfully found.
140 @retval EFI_NOT_FOUND Variable not found
145 IN CHAR16
*VariableName
,
146 IN EFI_GUID
*VendorGuid
,
147 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
148 IN VARIABLE_GLOBAL
*Global
,
149 IN BOOLEAN IgnoreRtCheck
154 This code gets the pointer to the variable data.
156 @param Variable Pointer to the Variable Header.
158 @return Pointer to Variable Data.
163 IN VARIABLE_HEADER
*Variable
168 This code gets the size of variable data.
170 @param Variable Pointer to the Variable Header.
172 @return Size of variable in bytes.
177 IN VARIABLE_HEADER
*Variable
181 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
182 index of associated public key is needed.
184 @param[in] VariableName Name of variable.
185 @param[in] VendorGuid Guid of variable.
186 @param[in] Data Variable data.
187 @param[in] DataSize Size of data. 0 means delete.
188 @param[in] Attributes Attributes of the variable.
189 @param[in] KeyIndex Index of associated public key.
190 @param[in] MonotonicCount Value of associated monotonic count.
191 @param[in] Variable The variable information that is used to keep track of variable usage.
193 @param[in] TimeStamp Value of associated TimeStamp.
195 @retval EFI_SUCCESS The update operation is success.
196 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.
201 IN CHAR16
*VariableName
,
202 IN EFI_GUID
*VendorGuid
,
205 IN UINT32 Attributes OPTIONAL
,
206 IN UINT32 KeyIndex OPTIONAL
,
207 IN UINT64 MonotonicCount OPTIONAL
,
208 IN VARIABLE_POINTER_TRACK
*Variable
,
209 IN EFI_TIME
*TimeStamp OPTIONAL
214 Return TRUE if ExitBootServices () has been called.
216 @retval TRUE If ExitBootServices () has been called.
224 Initializes a basic mutual exclusion lock.
226 This function initializes a basic mutual exclusion lock to the released state
227 and returns the lock. Each lock provides mutual exclusion access at its task
228 priority level. Since there is no preemption or multiprocessor support in EFI,
229 acquiring the lock only consists of raising to the locks TPL.
230 If Lock is NULL, then ASSERT().
231 If Priority is not a valid TPL value, then ASSERT().
233 @param Lock A pointer to the lock data structure to initialize.
234 @param Priority EFI TPL is associated with the lock.
241 IN OUT EFI_LOCK
*Lock
,
247 Acquires lock only at boot time. Simply returns at runtime.
249 This is a temperary function that will be removed when
250 EfiAcquireLock() in UefiLib can handle the call in UEFI
251 Runtimer driver in RT phase.
252 It calls EfiAcquireLock() at boot time, and simply returns
255 @param Lock A pointer to the lock to acquire.
259 AcquireLockOnlyAtBootTime (
265 Releases lock only at boot time. Simply returns at runtime.
267 This is a temperary function which will be removed when
268 EfiReleaseLock() in UefiLib can handle the call in UEFI
269 Runtimer driver in RT phase.
270 It calls EfiReleaseLock() at boot time and simply returns
273 @param Lock A pointer to the lock to release.
277 ReleaseLockOnlyAtBootTime (
282 Retrive the FVB protocol interface by HANDLE.
284 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
285 reading, writing, and erasing the target block.
286 @param[out] FvBlock The interface of FVB protocol
288 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
289 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
290 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
295 IN EFI_HANDLE FvBlockHandle
,
296 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
301 Retrive the Swap Address Range protocol interface.
303 @param[out] SarProtocol The interface of SAR protocol
305 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
306 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
307 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
312 OUT VOID
**SarProtocol
316 Function returns an array of handles that support the FVB protocol
317 in a buffer allocated from pool.
319 @param[out] NumberHandles The number of handles returned in Buffer.
320 @param[out] Buffer A pointer to the buffer to return the requested
321 array of handles that support FVB protocol.
323 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
324 handles in Buffer was returned in NumberHandles.
325 @retval EFI_NOT_FOUND No FVB handle was found.
326 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
327 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
331 GetFvbCountAndBuffer (
332 OUT UINTN
*NumberHandles
,
333 OUT EFI_HANDLE
**Buffer
337 Initializes variable store area for non-volatile and volatile variable.
339 @retval EFI_SUCCESS Function successfully executed.
340 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
344 VariableCommonInitialize (
349 This function reclaims variable storage if free size is below the threshold.
359 Initializes variable write service after FVB was ready.
361 @retval EFI_SUCCESS Function successfully executed.
362 @retval Others Fail to initialize the variable service.
366 VariableWriteServiceInitialize (
371 Retrive the SMM Fault Tolerent Write protocol interface.
373 @param[out] FtwProtocol The interface of SMM Ftw protocol
375 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
376 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
377 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
382 OUT VOID
**FtwProtocol
386 Get the proper fvb handle and/or fvb protocol by the given Flash address.
388 @param[in] Address The Flash address.
389 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
390 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
394 GetFvbInfoByAddress (
395 IN EFI_PHYSICAL_ADDRESS Address
,
396 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
397 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
402 This code finds variable in storage blocks (Volatile or Non-Volatile).
404 @param VariableName Name of Variable to be found.
405 @param VendorGuid Variable vendor GUID.
406 @param Attributes Attribute value of the variable found.
407 @param DataSize Size of Data found. If size is less than the
408 data, this value contains the required size.
409 @param Data Data pointer.
411 @return EFI_INVALID_PARAMETER Invalid parameter.
412 @return EFI_SUCCESS Find the specified variable.
413 @return EFI_NOT_FOUND Not found.
414 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
419 VariableServiceGetVariable (
420 IN CHAR16
*VariableName
,
421 IN EFI_GUID
*VendorGuid
,
422 OUT UINT32
*Attributes OPTIONAL
,
423 IN OUT UINTN
*DataSize
,
429 This code Finds the Next available variable.
431 @param VariableNameSize Size of the variable name.
432 @param VariableName Pointer to variable name.
433 @param VendorGuid Variable Vendor Guid.
435 @return EFI_INVALID_PARAMETER Invalid parameter.
436 @return EFI_SUCCESS Find the specified variable.
437 @return EFI_NOT_FOUND Not found.
438 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
443 VariableServiceGetNextVariableName (
444 IN OUT UINTN
*VariableNameSize
,
445 IN OUT CHAR16
*VariableName
,
446 IN OUT EFI_GUID
*VendorGuid
451 This code sets variable in storage blocks (Volatile or Non-Volatile).
453 @param VariableName Name of Variable to be found.
454 @param VendorGuid Variable vendor GUID.
455 @param Attributes Attribute value of the variable found
456 @param DataSize Size of Data found. If size is less than the
457 data, this value contains the required size.
458 @param Data Data pointer.
460 @return EFI_INVALID_PARAMETER Invalid parameter.
461 @return EFI_SUCCESS Set successfully.
462 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
463 @return EFI_NOT_FOUND Not found.
464 @return EFI_WRITE_PROTECTED Variable is read-only.
469 VariableServiceSetVariable (
470 IN CHAR16
*VariableName
,
471 IN EFI_GUID
*VendorGuid
,
472 IN UINT32 Attributes
,
479 This code returns information about the EFI variables.
481 @param Attributes Attributes bitmask to specify the type of variables
482 on which to return information.
483 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
484 for the EFI variables associated with the attributes specified.
485 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
486 for EFI variables associated with the attributes specified.
487 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
488 associated with the attributes specified.
490 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
491 @return EFI_SUCCESS Query successfully.
492 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
497 VariableServiceQueryVariableInfo (
498 IN UINT32 Attributes
,
499 OUT UINT64
*MaximumVariableStorageSize
,
500 OUT UINT64
*RemainingVariableStorageSize
,
501 OUT UINT64
*MaximumVariableSize
504 extern VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;