2 Functions related to managing the UEFI variable runtime cache. This file should only include functions
3 used by the SMM UEFI variable driver.
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. They may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
10 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include "VariableParsing.h"
16 #include "VariableRuntimeCache.h"
18 extern VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
19 extern VARIABLE_STORE_HEADER
*mNvVariableCache
;
22 Copies any pending updates to runtime variable caches.
24 @retval EFI_UNSUPPORTED The volatile store to be updated is not initialized properly.
25 @retval EFI_SUCCESS The volatile store was updated successfully.
29 FlushPendingRuntimeVariableCacheUpdates (
33 VARIABLE_RUNTIME_CACHE_CONTEXT
*VariableRuntimeCacheContext
;
35 VariableRuntimeCacheContext
= &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
;
37 if (VariableRuntimeCacheContext
->VariableRuntimeNvCache
.Store
== NULL
||
38 VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.Store
== NULL
||
39 VariableRuntimeCacheContext
->PendingUpdate
== NULL
) {
40 return EFI_UNSUPPORTED
;
43 if (*(VariableRuntimeCacheContext
->PendingUpdate
)) {
44 if (VariableRuntimeCacheContext
->VariableRuntimeHobCache
.Store
!= NULL
&&
45 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
> 0) {
48 ((UINT8
*) (UINTN
) VariableRuntimeCacheContext
->VariableRuntimeHobCache
.Store
) +
49 VariableRuntimeCacheContext
->VariableRuntimeHobCache
.PendingUpdateOffset
52 ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
) +
53 VariableRuntimeCacheContext
->VariableRuntimeHobCache
.PendingUpdateOffset
55 VariableRuntimeCacheContext
->VariableRuntimeHobCache
.PendingUpdateLength
57 VariableRuntimeCacheContext
->VariableRuntimeHobCache
.PendingUpdateLength
= 0;
58 VariableRuntimeCacheContext
->VariableRuntimeHobCache
.PendingUpdateOffset
= 0;
63 ((UINT8
*) (UINTN
) VariableRuntimeCacheContext
->VariableRuntimeNvCache
.Store
) +
64 VariableRuntimeCacheContext
->VariableRuntimeNvCache
.PendingUpdateOffset
67 ((UINT8
*) (UINTN
) mNvVariableCache
) +
68 VariableRuntimeCacheContext
->VariableRuntimeNvCache
.PendingUpdateOffset
70 VariableRuntimeCacheContext
->VariableRuntimeNvCache
.PendingUpdateLength
72 VariableRuntimeCacheContext
->VariableRuntimeNvCache
.PendingUpdateLength
= 0;
73 VariableRuntimeCacheContext
->VariableRuntimeNvCache
.PendingUpdateOffset
= 0;
77 ((UINT8
*) (UINTN
) VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.Store
) +
78 VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.PendingUpdateOffset
81 ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
) +
82 VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.PendingUpdateOffset
84 VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.PendingUpdateLength
86 VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.PendingUpdateLength
= 0;
87 VariableRuntimeCacheContext
->VariableRuntimeVolatileCache
.PendingUpdateOffset
= 0;
88 *(VariableRuntimeCacheContext
->PendingUpdate
) = FALSE
;
95 Synchronizes the runtime variable caches with all pending updates outside runtime.
97 Ensures all conditions are met to maintain coherency for runtime cache updates. This function will attempt
98 to write the given update (and any other pending updates) if the ReadLock is available. Otherwise, the
99 update is added as a pending update for the given variable store and it will be flushed to the runtime cache
100 at the next opportunity the ReadLock is available.
102 @param[in] VariableRuntimeCache Variable runtime cache structure for the runtime cache being synchronized.
103 @param[in] Offset Offset in bytes to apply the update.
104 @param[in] Length Length of data in bytes of the update.
106 @retval EFI_SUCCESS The update was added as a pending update successfully. If the variable runtime
107 cache ReadLock was available, the runtime cache was updated successfully.
108 @retval EFI_UNSUPPORTED The volatile store to be updated is not initialized properly.
112 SynchronizeRuntimeVariableCache (
113 IN VARIABLE_RUNTIME_CACHE
*VariableRuntimeCache
,
118 if (VariableRuntimeCache
== NULL
) {
119 return EFI_INVALID_PARAMETER
;
120 } else if (VariableRuntimeCache
->Store
== NULL
) {
121 // The runtime cache may not be active or allocated yet.
122 // In either case, return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET.
126 if (mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.PendingUpdate
== NULL
||
127 mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.ReadLock
== NULL
) {
128 return EFI_UNSUPPORTED
;
131 if (*(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.PendingUpdate
) &&
132 VariableRuntimeCache
->PendingUpdateLength
> 0) {
133 VariableRuntimeCache
->PendingUpdateLength
=
136 (UINTN
) (VariableRuntimeCache
->PendingUpdateOffset
+ VariableRuntimeCache
->PendingUpdateLength
),
138 ) - MIN ((UINTN
) VariableRuntimeCache
->PendingUpdateOffset
, Offset
)
140 VariableRuntimeCache
->PendingUpdateOffset
=
141 (UINT32
) MIN ((UINTN
) VariableRuntimeCache
->PendingUpdateOffset
, Offset
);
143 VariableRuntimeCache
->PendingUpdateLength
= (UINT32
) Length
;
144 VariableRuntimeCache
->PendingUpdateOffset
= (UINT32
) Offset
;
146 *(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.PendingUpdate
) = TRUE
;
148 if (*(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.ReadLock
) == FALSE
) {
149 return FlushPendingRuntimeVariableCacheUpdates ();