]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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, mVariableModuleGlobal->VariableGlobal.AuthFormat);
60 AuthVariableInfo->Data = GetVariableDataPtr (Variable.CurrPtr, mVariableModuleGlobal->VariableGlobal.AuthFormat);
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 mVariableModuleGlobal->VariableGlobal.AuthFormat
113 );
114 if (EFI_ERROR (Status)) {
115 AuthVariableInfo->VariableName = NULL;
116 AuthVariableInfo->VendorGuid = NULL;
117 AuthVariableInfo->Data = NULL;
118 AuthVariableInfo->DataSize = 0;
119 AuthVariableInfo->Attributes = 0;
120 AuthVariableInfo->PubKeyIndex = 0;
121 AuthVariableInfo->MonotonicCount = 0;
122 AuthVariableInfo->TimeStamp = NULL;
123 return Status;
124 }
125
126 AuthVariableInfo->VariableName = GetVariableNamePtr (VariablePtr, mVariableModuleGlobal->VariableGlobal.AuthFormat);
127 AuthVariableInfo->VendorGuid = GetVendorGuidPtr (VariablePtr, mVariableModuleGlobal->VariableGlobal.AuthFormat);
128 AuthVariableInfo->DataSize = DataSizeOfVariable (VariablePtr, mVariableModuleGlobal->VariableGlobal.AuthFormat);
129 AuthVariableInfo->Data = GetVariableDataPtr (VariablePtr, mVariableModuleGlobal->VariableGlobal.AuthFormat);
130 AuthVariableInfo->Attributes = VariablePtr->Attributes;
131 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
132 AuthVariablePtr = (AUTHENTICATED_VARIABLE_HEADER *) VariablePtr;
133 AuthVariableInfo->PubKeyIndex = AuthVariablePtr->PubKeyIndex;
134 AuthVariableInfo->MonotonicCount = ReadUnaligned64 (&(AuthVariablePtr->MonotonicCount));
135 AuthVariableInfo->TimeStamp = &AuthVariablePtr->TimeStamp;
136 }
137
138 return EFI_SUCCESS;
139 }
140
141 /**
142 Update the variable region with Variable information.
143
144 @param[in] AuthVariableInfo Pointer AUTH_VARIABLE_INFO structure for
145 input of the variable.
146
147 @retval EFI_SUCCESS The update operation is success.
148 @retval EFI_INVALID_PARAMETER Invalid parameter.
149 @retval EFI_WRITE_PROTECTED Variable is write-protected.
150 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
151
152 **/
153 EFI_STATUS
154 EFIAPI
155 VariableExLibUpdateVariable (
156 IN AUTH_VARIABLE_INFO *AuthVariableInfo
157 )
158 {
159 VARIABLE_POINTER_TRACK Variable;
160
161 FindVariable (AuthVariableInfo->VariableName, AuthVariableInfo->VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
162 return UpdateVariable (
163 AuthVariableInfo->VariableName,
164 AuthVariableInfo->VendorGuid,
165 AuthVariableInfo->Data,
166 AuthVariableInfo->DataSize,
167 AuthVariableInfo->Attributes,
168 AuthVariableInfo->PubKeyIndex,
169 AuthVariableInfo->MonotonicCount,
170 &Variable,
171 AuthVariableInfo->TimeStamp
172 );
173 }
174
175 /**
176 Get scratch buffer.
177
178 @param[in, out] ScratchBufferSize Scratch buffer size. If input size is greater than
179 the maximum supported buffer size, this value contains
180 the maximum supported buffer size as output.
181 @param[out] ScratchBuffer Pointer to scratch buffer address.
182
183 @retval EFI_SUCCESS Get scratch buffer successfully.
184 @retval EFI_UNSUPPORTED If input size is greater than the maximum supported buffer size.
185
186 **/
187 EFI_STATUS
188 EFIAPI
189 VariableExLibGetScratchBuffer (
190 IN OUT UINTN *ScratchBufferSize,
191 OUT VOID **ScratchBuffer
192 )
193 {
194 UINTN MaxBufferSize;
195
196 MaxBufferSize = mVariableModuleGlobal->ScratchBufferSize;
197 if (*ScratchBufferSize > MaxBufferSize) {
198 *ScratchBufferSize = MaxBufferSize;
199 return EFI_UNSUPPORTED;
200 }
201
202 *ScratchBuffer = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
203 return EFI_SUCCESS;
204 }
205
206 /**
207 This function is to check if the remaining variable space is enough to set
208 all Variables from argument list successfully. The purpose of the check
209 is to keep the consistency of the Variables to be in variable storage.
210
211 Note: Variables are assumed to be in same storage.
212 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
213 so follow the argument sequence to check the Variables.
214
215 @param[in] Attributes Variable attributes for Variable entries.
216 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
217 A NULL terminates the list. The VariableSize of
218 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
219 It will be changed to variable total size as output.
220
221 @retval TRUE Have enough variable space to set the Variables successfully.
222 @retval FALSE No enough variable space to set the Variables successfully.
223
224 **/
225 BOOLEAN
226 EFIAPI
227 VariableExLibCheckRemainingSpaceForConsistency (
228 IN UINT32 Attributes,
229 ...
230 )
231 {
232 VA_LIST Marker;
233 BOOLEAN Return;
234
235 VA_START (Marker, Attributes);
236
237 Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);
238
239 VA_END (Marker);
240
241 return Return;
242 }
243
244 /**
245 Return TRUE if at OS runtime.
246
247 @retval TRUE If at OS runtime.
248 @retval FALSE If at boot time.
249
250 **/
251 BOOLEAN
252 EFIAPI
253 VariableExLibAtRuntime (
254 VOID
255 )
256 {
257 return AtRuntime ();
258 }