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