]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
MdeModulePkg/Variable: Parameterize GetNextVariableInternal () stores
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableExLib.c
1 /** @file
2 Provides variable driver extended services.
3
4 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "Variable.h"
10 #include "VariableParsing.h"
11
12 /**
13 Finds variable in storage blocks of volatile and non-volatile storage areas.
14
15 This code finds variable in storage blocks of volatile and non-volatile storage areas.
16 If VariableName is an empty string, then we just return the first
17 qualified variable without comparing VariableName and VendorGuid.
18
19 @param[in] VariableName Name of the variable to be found.
20 @param[in] VendorGuid Variable vendor GUID to be found.
21 @param[out] AuthVariableInfo Pointer to AUTH_VARIABLE_INFO structure for
22 output of the variable found.
23
24 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
25 while VendorGuid is NULL.
26 @retval EFI_SUCCESS Variable successfully found.
27 @retval EFI_NOT_FOUND Variable not found
28
29 **/
30 EFI_STATUS
31 EFIAPI
32 VariableExLibFindVariable (
33 IN CHAR16 *VariableName,
34 IN EFI_GUID *VendorGuid,
35 OUT AUTH_VARIABLE_INFO *AuthVariableInfo
36 )
37 {
38 EFI_STATUS Status;
39 VARIABLE_POINTER_TRACK Variable;
40 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
41
42 Status = FindVariable (
43 VariableName,
44 VendorGuid,
45 &Variable,
46 &mVariableModuleGlobal->VariableGlobal,
47 FALSE
48 );
49 if (EFI_ERROR (Status)) {
50 AuthVariableInfo->Data = NULL;
51 AuthVariableInfo->DataSize = 0;
52 AuthVariableInfo->Attributes = 0;
53 AuthVariableInfo->PubKeyIndex = 0;
54 AuthVariableInfo->MonotonicCount = 0;
55 AuthVariableInfo->TimeStamp = NULL;
56 return Status;
57 }
58
59 AuthVariableInfo->DataSize = DataSizeOfVariable (Variable.CurrPtr);
60 AuthVariableInfo->Data = GetVariableDataPtr (Variable.CurrPtr);
61 AuthVariableInfo->Attributes = Variable.CurrPtr->Attributes;
62 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
63 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable.CurrPtr;
64 AuthVariableInfo->PubKeyIndex = AuthVariable->PubKeyIndex;
65 AuthVariableInfo->MonotonicCount = ReadUnaligned64 (&(AuthVariable->MonotonicCount));
66 AuthVariableInfo->TimeStamp = &AuthVariable->TimeStamp;
67 }
68
69 return EFI_SUCCESS;
70 }
71
72 /**
73 Finds next variable in storage blocks of volatile and non-volatile storage areas.
74
75 This code finds next variable in storage blocks of volatile and non-volatile storage areas.
76 If VariableName is an empty string, then we just return the first
77 qualified variable without comparing VariableName and VendorGuid.
78
79 @param[in] VariableName Name of the variable to be found.
80 @param[in] VendorGuid Variable vendor GUID to be found.
81 @param[out] AuthVariableInfo Pointer to AUTH_VARIABLE_INFO structure for
82 output of the next variable.
83
84 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
85 while VendorGuid is NULL.
86 @retval EFI_SUCCESS Variable successfully found.
87 @retval EFI_NOT_FOUND Variable not found
88
89 **/
90 EFI_STATUS
91 EFIAPI
92 VariableExLibFindNextVariable (
93 IN CHAR16 *VariableName,
94 IN EFI_GUID *VendorGuid,
95 OUT AUTH_VARIABLE_INFO *AuthVariableInfo
96 )
97 {
98 EFI_STATUS Status;
99 VARIABLE_HEADER *VariablePtr;
100 AUTHENTICATED_VARIABLE_HEADER *AuthVariablePtr;
101 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
102
103 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
104 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
105 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
106
107 Status = VariableServiceGetNextVariableInternal (
108 VariableName,
109 VendorGuid,
110 VariableStoreHeader,
111 &VariablePtr
112 );
113 if (EFI_ERROR (Status)) {
114 AuthVariableInfo->VariableName = NULL;
115 AuthVariableInfo->VendorGuid = NULL;
116 AuthVariableInfo->Data = NULL;
117 AuthVariableInfo->DataSize = 0;
118 AuthVariableInfo->Attributes = 0;
119 AuthVariableInfo->PubKeyIndex = 0;
120 AuthVariableInfo->MonotonicCount = 0;
121 AuthVariableInfo->TimeStamp = NULL;
122 return Status;
123 }
124
125 AuthVariableInfo->VariableName = GetVariableNamePtr (VariablePtr);
126 AuthVariableInfo->VendorGuid = GetVendorGuidPtr (VariablePtr);
127 AuthVariableInfo->DataSize = DataSizeOfVariable (VariablePtr);
128 AuthVariableInfo->Data = GetVariableDataPtr (VariablePtr);
129 AuthVariableInfo->Attributes = VariablePtr->Attributes;
130 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
131 AuthVariablePtr = (AUTHENTICATED_VARIABLE_HEADER *) VariablePtr;
132 AuthVariableInfo->PubKeyIndex = AuthVariablePtr->PubKeyIndex;
133 AuthVariableInfo->MonotonicCount = ReadUnaligned64 (&(AuthVariablePtr->MonotonicCount));
134 AuthVariableInfo->TimeStamp = &AuthVariablePtr->TimeStamp;
135 }
136
137 return EFI_SUCCESS;
138 }
139
140 /**
141 Update the variable region with Variable information.
142
143 @param[in] AuthVariableInfo Pointer AUTH_VARIABLE_INFO structure for
144 input of the variable.
145
146 @retval EFI_SUCCESS The update operation is success.
147 @retval EFI_INVALID_PARAMETER Invalid parameter.
148 @retval EFI_WRITE_PROTECTED Variable is write-protected.
149 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
150
151 **/
152 EFI_STATUS
153 EFIAPI
154 VariableExLibUpdateVariable (
155 IN AUTH_VARIABLE_INFO *AuthVariableInfo
156 )
157 {
158 VARIABLE_POINTER_TRACK Variable;
159
160 FindVariable (AuthVariableInfo->VariableName, AuthVariableInfo->VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
161 return UpdateVariable (
162 AuthVariableInfo->VariableName,
163 AuthVariableInfo->VendorGuid,
164 AuthVariableInfo->Data,
165 AuthVariableInfo->DataSize,
166 AuthVariableInfo->Attributes,
167 AuthVariableInfo->PubKeyIndex,
168 AuthVariableInfo->MonotonicCount,
169 &Variable,
170 AuthVariableInfo->TimeStamp
171 );
172 }
173
174 /**
175 Get scratch buffer.
176
177 @param[in, out] ScratchBufferSize Scratch buffer size. If input size is greater than
178 the maximum supported buffer size, this value contains
179 the maximum supported buffer size as output.
180 @param[out] ScratchBuffer Pointer to scratch buffer address.
181
182 @retval EFI_SUCCESS Get scratch buffer successfully.
183 @retval EFI_UNSUPPORTED If input size is greater than the maximum supported buffer size.
184
185 **/
186 EFI_STATUS
187 EFIAPI
188 VariableExLibGetScratchBuffer (
189 IN OUT UINTN *ScratchBufferSize,
190 OUT VOID **ScratchBuffer
191 )
192 {
193 UINTN MaxBufferSize;
194
195 MaxBufferSize = mVariableModuleGlobal->ScratchBufferSize;
196 if (*ScratchBufferSize > MaxBufferSize) {
197 *ScratchBufferSize = MaxBufferSize;
198 return EFI_UNSUPPORTED;
199 }
200
201 *ScratchBuffer = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
202 return EFI_SUCCESS;
203 }
204
205 /**
206 This function is to check if the remaining variable space is enough to set
207 all Variables from argument list successfully. The purpose of the check
208 is to keep the consistency of the Variables to be in variable storage.
209
210 Note: Variables are assumed to be in same storage.
211 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
212 so follow the argument sequence to check the Variables.
213
214 @param[in] Attributes Variable attributes for Variable entries.
215 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
216 A NULL terminates the list. The VariableSize of
217 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
218 It will be changed to variable total size as output.
219
220 @retval TRUE Have enough variable space to set the Variables successfully.
221 @retval FALSE No enough variable space to set the Variables successfully.
222
223 **/
224 BOOLEAN
225 EFIAPI
226 VariableExLibCheckRemainingSpaceForConsistency (
227 IN UINT32 Attributes,
228 ...
229 )
230 {
231 VA_LIST Marker;
232 BOOLEAN Return;
233
234 VA_START (Marker, Attributes);
235
236 Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);
237
238 VA_END (Marker);
239
240 return Return;
241 }
242
243 /**
244 Return TRUE if at OS runtime.
245
246 @retval TRUE If at OS runtime.
247 @retval FALSE If at boot time.
248
249 **/
250 BOOLEAN
251 EFIAPI
252 VariableExLibAtRuntime (
253 VOID
254 )
255 {
256 return AtRuntime ();
257 }