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