]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
9bb30bc1e8040ac80ea1a049b9b5fcd63fb5dc3a
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableRuntimeCache.c
1 /** @file
2 Functions related to managing the UEFI variable runtime cache. This file should only include functions
3 used by the SMM UEFI variable driver.
4
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.
9
10 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
12
13 **/
14
15 #include "VariableParsing.h"
16 #include "VariableRuntimeCache.h"
17
18 extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
19 extern VARIABLE_STORE_HEADER *mNvVariableCache;
20
21 /**
22 Copies any pending updates to runtime variable caches.
23
24 @retval EFI_UNSUPPORTED The volatile store to be updated is not initialized properly.
25 @retval EFI_SUCCESS The volatile store was updated successfully.
26
27 **/
28 EFI_STATUS
29 FlushPendingRuntimeVariableCacheUpdates (
30 VOID
31 )
32 {
33 VARIABLE_RUNTIME_CACHE_CONTEXT *VariableRuntimeCacheContext;
34
35 VariableRuntimeCacheContext = &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext;
36
37 if ((VariableRuntimeCacheContext->VariableRuntimeNvCache.Store == NULL) ||
38 (VariableRuntimeCacheContext->VariableRuntimeVolatileCache.Store == NULL) ||
39 (VariableRuntimeCacheContext->PendingUpdate == NULL))
40 {
41 return EFI_UNSUPPORTED;
42 }
43
44 if (*(VariableRuntimeCacheContext->PendingUpdate)) {
45 if ((VariableRuntimeCacheContext->VariableRuntimeHobCache.Store != NULL) &&
46 (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0))
47 {
48 CopyMem (
49 (VOID *)(
50 ((UINT8 *)(UINTN)VariableRuntimeCacheContext->VariableRuntimeHobCache.Store) +
51 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOffset
52 ),
53 (VOID *)(
54 ((UINT8 *)(UINTN)mVariableModuleGlobal->VariableGlobal.HobVariableBase) +
55 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOffset
56 ),
57 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateLength
58 );
59 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateLength = 0;
60 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOffset = 0;
61 }
62
63 CopyMem (
64 (VOID *)(
65 ((UINT8 *)(UINTN)VariableRuntimeCacheContext->VariableRuntimeNvCache.Store) +
66 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffset
67 ),
68 (VOID *)(
69 ((UINT8 *)(UINTN)mNvVariableCache) +
70 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffset
71 ),
72 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateLength
73 );
74 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateLength = 0;
75 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffset = 0;
76
77 CopyMem (
78 (VOID *)(
79 ((UINT8 *)(UINTN)VariableRuntimeCacheContext->VariableRuntimeVolatileCache.Store) +
80 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset
81 ),
82 (VOID *)(
83 ((UINT8 *)(UINTN)mVariableModuleGlobal->VariableGlobal.VolatileVariableBase) +
84 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset
85 ),
86 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateLength
87 );
88 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateLength = 0;
89 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset = 0;
90 *(VariableRuntimeCacheContext->PendingUpdate) = FALSE;
91 }
92
93 return EFI_SUCCESS;
94 }
95
96 /**
97 Synchronizes the runtime variable caches with all pending updates outside runtime.
98
99 Ensures all conditions are met to maintain coherency for runtime cache updates. This function will attempt
100 to write the given update (and any other pending updates) if the ReadLock is available. Otherwise, the
101 update is added as a pending update for the given variable store and it will be flushed to the runtime cache
102 at the next opportunity the ReadLock is available.
103
104 @param[in] VariableRuntimeCache Variable runtime cache structure for the runtime cache being synchronized.
105 @param[in] Offset Offset in bytes to apply the update.
106 @param[in] Length Length of data in bytes of the update.
107
108 @retval EFI_SUCCESS The update was added as a pending update successfully. If the variable runtime
109 cache ReadLock was available, the runtime cache was updated successfully.
110 @retval EFI_UNSUPPORTED The volatile store to be updated is not initialized properly.
111
112 **/
113 EFI_STATUS
114 SynchronizeRuntimeVariableCache (
115 IN VARIABLE_RUNTIME_CACHE *VariableRuntimeCache,
116 IN UINTN Offset,
117 IN UINTN Length
118 )
119 {
120 if (VariableRuntimeCache == NULL) {
121 return EFI_INVALID_PARAMETER;
122 } else if (VariableRuntimeCache->Store == NULL) {
123 // The runtime cache may not be active or allocated yet.
124 // In either case, return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET.
125 return EFI_SUCCESS;
126 }
127
128 if ((mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL) ||
129 (mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.ReadLock == NULL))
130 {
131 return EFI_UNSUPPORTED;
132 }
133
134 if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) &&
135 (VariableRuntimeCache->PendingUpdateLength > 0))
136 {
137 VariableRuntimeCache->PendingUpdateLength =
138 (UINT32)(
139 MAX (
140 (UINTN)(VariableRuntimeCache->PendingUpdateOffset + VariableRuntimeCache->PendingUpdateLength),
141 Offset + Length
142 ) - MIN ((UINTN)VariableRuntimeCache->PendingUpdateOffset, Offset)
143 );
144 VariableRuntimeCache->PendingUpdateOffset =
145 (UINT32)MIN ((UINTN)VariableRuntimeCache->PendingUpdateOffset, Offset);
146 } else {
147 VariableRuntimeCache->PendingUpdateLength = (UINT32)Length;
148 VariableRuntimeCache->PendingUpdateOffset = (UINT32)Offset;
149 }
150
151 *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = TRUE;
152
153 if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.ReadLock) == FALSE) {
154 return FlushPendingRuntimeVariableCacheUpdates ();
155 }
156
157 return EFI_SUCCESS;
158 }