]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
MdeModulePkg/Variable: Add RT GetVariable() cache support
[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 return EFI_UNSUPPORTED;
41 }
42
43 if (*(VariableRuntimeCacheContext->PendingUpdate)) {
44 if (VariableRuntimeCacheContext->VariableRuntimeHobCache.Store != NULL &&
45 mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) {
46 CopyMem (
47 (VOID *) (
48 ((UINT8 *) (UINTN) VariableRuntimeCacheContext->VariableRuntimeHobCache.Store) +
49 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOffset
50 ),
51 (VOID *) (
52 ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase) +
53 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOffset
54 ),
55 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateLength
56 );
57 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateLength = 0;
58 VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOffset = 0;
59 }
60
61 CopyMem (
62 (VOID *) (
63 ((UINT8 *) (UINTN) VariableRuntimeCacheContext->VariableRuntimeNvCache.Store) +
64 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffset
65 ),
66 (VOID *) (
67 ((UINT8 *) (UINTN) mNvVariableCache) +
68 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffset
69 ),
70 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateLength
71 );
72 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateLength = 0;
73 VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffset = 0;
74
75 CopyMem (
76 (VOID *) (
77 ((UINT8 *) (UINTN) VariableRuntimeCacheContext->VariableRuntimeVolatileCache.Store) +
78 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset
79 ),
80 (VOID *) (
81 ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase) +
82 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset
83 ),
84 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateLength
85 );
86 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateLength = 0;
87 VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset = 0;
88 *(VariableRuntimeCacheContext->PendingUpdate) = FALSE;
89 }
90
91 return EFI_SUCCESS;
92 }
93
94 /**
95 Synchronizes the runtime variable caches with all pending updates outside runtime.
96
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.
101
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.
105
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.
109
110 **/
111 EFI_STATUS
112 SynchronizeRuntimeVariableCache (
113 IN VARIABLE_RUNTIME_CACHE *VariableRuntimeCache,
114 IN UINTN Offset,
115 IN UINTN Length
116 )
117 {
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.
123 return EFI_SUCCESS;
124 }
125
126 if (mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL ||
127 mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.ReadLock == NULL) {
128 return EFI_UNSUPPORTED;
129 }
130
131 if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) &&
132 VariableRuntimeCache->PendingUpdateLength > 0) {
133 VariableRuntimeCache->PendingUpdateLength =
134 (UINT32) (
135 MAX (
136 (UINTN) (VariableRuntimeCache->PendingUpdateOffset + VariableRuntimeCache->PendingUpdateLength),
137 Offset + Length
138 ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset)
139 );
140 VariableRuntimeCache->PendingUpdateOffset =
141 (UINT32) MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset);
142 } else {
143 VariableRuntimeCache->PendingUpdateLength = (UINT32) Length;
144 VariableRuntimeCache->PendingUpdateOffset = (UINT32) Offset;
145 }
146 *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = TRUE;
147
148 if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.ReadLock) == FALSE) {
149 return FlushPendingRuntimeVariableCacheUpdates ();
150 }
151
152 return EFI_SUCCESS;
153 }