3 The internal header file includes the common header files, defines
4 internal structure and functions used by Variable modules.
6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Protocol/VariableWrite.h>
22 #include <Protocol/FaultTolerantWrite.h>
23 #include <Protocol/FirmwareVolumeBlock.h>
24 #include <Protocol/Variable.h>
25 #include <Protocol/VariableLock.h>
26 #include <Library/PcdLib.h>
27 #include <Library/HobLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/DxeServicesTableLib.h>
30 #include <Library/UefiRuntimeLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/UefiLib.h>
35 #include <Library/BaseLib.h>
36 #include <Library/SynchronizationLib.h>
37 #include <Library/MemoryAllocationLib.h>
38 #include <Guid/GlobalVariable.h>
39 #include <Guid/EventGroup.h>
40 #include <Guid/VariableFormat.h>
41 #include <Guid/SystemNvDataGuid.h>
42 #include <Guid/HardwareErrorVariable.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
;
61 // If both ADDED and IN_DELETED_TRANSITION variable are present,
62 // InDeletedTransitionPtr will point to the IN_DELETED_TRANSITION one.
63 // Otherwise, CurrPtr will point to the ADDED or IN_DELETED_TRANSITION one,
64 // and InDeletedTransitionPtr will be NULL at the same time.
66 VARIABLE_HEADER
*InDeletedTransitionPtr
;
67 VARIABLE_HEADER
*EndPtr
;
68 VARIABLE_HEADER
*StartPtr
;
70 } VARIABLE_POINTER_TRACK
;
73 EFI_PHYSICAL_ADDRESS HobVariableBase
;
74 EFI_PHYSICAL_ADDRESS VolatileVariableBase
;
75 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase
;
76 EFI_LOCK VariableServicesLock
;
77 UINT32 ReentrantState
;
81 VARIABLE_GLOBAL VariableGlobal
;
82 UINTN VolatileLastVariableOffset
;
83 UINTN NonVolatileLastVariableOffset
;
84 UINTN CommonVariableTotalSize
;
85 UINTN HwErrVariableTotalSize
;
86 CHAR8
*PlatformLangCodes
;
89 CHAR8 Lang
[ISO_639_2_ENTRY_SIZE
+ 1];
90 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbInstance
;
91 } VARIABLE_MODULE_GLOBAL
;
99 } VARIABLE_CACHE_ENTRY
;
108 Flush the HOB variable to flash.
110 @param[in] VariableName Name of variable has been updated or deleted.
111 @param[in] VendorGuid Guid of variable has been updated or deleted.
115 FlushHobVariableToFlash (
116 IN CHAR16
*VariableName
,
117 IN EFI_GUID
*VendorGuid
121 Writes a buffer to variable storage space, in the working block.
123 This function writes a buffer to variable storage space into a firmware
124 volume block device. The destination is specified by the parameter
125 VariableBase. Fault Tolerant Write protocol is used for writing.
127 @param VariableBase Base address of the variable to write.
128 @param Buffer Point to the data buffer.
129 @param BufferSize The number of bytes of the data Buffer.
131 @retval EFI_SUCCESS The function completed successfully.
132 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
133 @retval EFI_ABORTED The function could not complete successfully.
138 IN EFI_PHYSICAL_ADDRESS VariableBase
,
145 Update the variable region with Variable information. These are the same
146 arguments as the EFI Variable services.
148 @param[in] VariableName Name of variable.
150 @param[in] VendorGuid Guid of variable.
152 @param[in] Data Variable data.
154 @param[in] DataSize Size of data. 0 means delete.
156 @param[in] Attributes Attribues of the variable.
158 @param[in, out] Variable The variable information that is used to keep track of variable usage.
160 @retval EFI_SUCCESS The update operation is success.
162 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.
167 IN CHAR16
*VariableName
,
168 IN EFI_GUID
*VendorGuid
,
171 IN UINT32 Attributes OPTIONAL
,
172 IN OUT VARIABLE_POINTER_TRACK
*Variable
177 Return TRUE if ExitBootServices () has been called.
179 @retval TRUE If ExitBootServices () has been called.
187 Initializes a basic mutual exclusion lock.
189 This function initializes a basic mutual exclusion lock to the released state
190 and returns the lock. Each lock provides mutual exclusion access at its task
191 priority level. Since there is no preemption or multiprocessor support in EFI,
192 acquiring the lock only consists of raising to the locks TPL.
193 If Lock is NULL, then ASSERT().
194 If Priority is not a valid TPL value, then ASSERT().
196 @param Lock A pointer to the lock data structure to initialize.
197 @param Priority EFI TPL is associated with the lock.
204 IN OUT EFI_LOCK
*Lock
,
210 Acquires lock only at boot time. Simply returns at runtime.
212 This is a temperary function that will be removed when
213 EfiAcquireLock() in UefiLib can handle the call in UEFI
214 Runtimer driver in RT phase.
215 It calls EfiAcquireLock() at boot time, and simply returns
218 @param Lock A pointer to the lock to acquire.
222 AcquireLockOnlyAtBootTime (
228 Releases lock only at boot time. Simply returns at runtime.
230 This is a temperary function which will be removed when
231 EfiReleaseLock() in UefiLib can handle the call in UEFI
232 Runtimer driver in RT phase.
233 It calls EfiReleaseLock() at boot time and simply returns
236 @param Lock A pointer to the lock to release.
240 ReleaseLockOnlyAtBootTime (
245 Retrive the FVB protocol interface by HANDLE.
247 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
248 reading, writing, and erasing the target block.
249 @param[out] FvBlock The interface of FVB protocol
251 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
252 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
253 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
258 IN EFI_HANDLE FvBlockHandle
,
259 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
264 Retrive the Swap Address Range protocol interface.
266 @param[out] SarProtocol The interface of SAR protocol
268 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
269 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
270 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
275 OUT VOID
**SarProtocol
279 Function returns an array of handles that support the FVB protocol
280 in a buffer allocated from pool.
282 @param[out] NumberHandles The number of handles returned in Buffer.
283 @param[out] Buffer A pointer to the buffer to return the requested
284 array of handles that support FVB protocol.
286 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
287 handles in Buffer was returned in NumberHandles.
288 @retval EFI_NOT_FOUND No FVB handle was found.
289 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
290 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
294 GetFvbCountAndBuffer (
295 OUT UINTN
*NumberHandles
,
296 OUT EFI_HANDLE
**Buffer
300 Initializes variable store area for non-volatile and volatile variable.
302 @retval EFI_SUCCESS Function successfully executed.
303 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
307 VariableCommonInitialize (
312 This function reclaims variable storage if free size is below the threshold.
322 Initializes variable write service after FVB was ready.
324 @retval EFI_SUCCESS Function successfully executed.
325 @retval Others Fail to initialize the variable service.
329 VariableWriteServiceInitialize (
334 Retrive the SMM Fault Tolerent Write protocol interface.
336 @param[out] FtwProtocol The interface of SMM Ftw protocol
338 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
339 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
340 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
345 OUT VOID
**FtwProtocol
349 Get the proper fvb handle and/or fvb protocol by the given Flash address.
351 @param[in] Address The Flash address.
352 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
353 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
357 GetFvbInfoByAddress (
358 IN EFI_PHYSICAL_ADDRESS Address
,
359 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
360 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
365 This code finds variable in storage blocks (Volatile or Non-Volatile).
367 @param VariableName Name of Variable to be found.
368 @param VendorGuid Variable vendor GUID.
369 @param Attributes Attribute value of the variable found.
370 @param DataSize Size of Data found. If size is less than the
371 data, this value contains the required size.
372 @param Data Data pointer.
374 @return EFI_INVALID_PARAMETER Invalid parameter.
375 @return EFI_SUCCESS Find the specified variable.
376 @return EFI_NOT_FOUND Not found.
377 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
382 VariableServiceGetVariable (
383 IN CHAR16
*VariableName
,
384 IN EFI_GUID
*VendorGuid
,
385 OUT UINT32
*Attributes OPTIONAL
,
386 IN OUT UINTN
*DataSize
,
392 This code Finds the Next available variable.
394 @param VariableNameSize Size of the variable name.
395 @param VariableName Pointer to variable name.
396 @param VendorGuid Variable Vendor Guid.
398 @return EFI_INVALID_PARAMETER Invalid parameter.
399 @return EFI_SUCCESS Find the specified variable.
400 @return EFI_NOT_FOUND Not found.
401 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
406 VariableServiceGetNextVariableName (
407 IN OUT UINTN
*VariableNameSize
,
408 IN OUT CHAR16
*VariableName
,
409 IN OUT EFI_GUID
*VendorGuid
414 This code sets variable in storage blocks (Volatile or Non-Volatile).
416 @param VariableName Name of Variable to be found.
417 @param VendorGuid Variable vendor GUID.
418 @param Attributes Attribute value of the variable found
419 @param DataSize Size of Data found. If size is less than the
420 data, this value contains the required size.
421 @param Data Data pointer.
423 @return EFI_INVALID_PARAMETER Invalid parameter.
424 @return EFI_SUCCESS Set successfully.
425 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
426 @return EFI_NOT_FOUND Not found.
427 @return EFI_WRITE_PROTECTED Variable is read-only.
432 VariableServiceSetVariable (
433 IN CHAR16
*VariableName
,
434 IN EFI_GUID
*VendorGuid
,
435 IN UINT32 Attributes
,
442 This code returns information about the EFI variables.
444 @param Attributes Attributes bitmask to specify the type of variables
445 on which to return information.
446 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
447 for the EFI variables associated with the attributes specified.
448 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
449 for EFI variables associated with the attributes specified.
450 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
451 associated with the attributes specified.
453 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
454 @return EFI_SUCCESS Query successfully.
455 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
460 VariableServiceQueryVariableInfo (
461 IN UINT32 Attributes
,
462 OUT UINT64
*MaximumVariableStorageSize
,
463 OUT UINT64
*RemainingVariableStorageSize
,
464 OUT UINT64
*MaximumVariableSize
468 Mark a variable that will become read-only after leaving the DXE phase of execution.
470 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
471 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
472 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
474 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
475 as pending to be read-only.
476 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
477 Or VariableName is an empty string.
478 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
479 already been signaled.
480 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
484 VariableLockRequestToLock (
485 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
486 IN CHAR16
*VariableName
,
487 IN EFI_GUID
*VendorGuid
490 extern VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;