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