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