]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkIfrSupportLib/IfrVariable.c
correct some coding style issues.
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkIfrSupportLib / IfrVariable.c
CommitLineData
cf7e50f8 1/** @file\r
2ec4d269 2 Variable/Map manipulations routines\r
3 \r
5d01b0f7 4Copyright (c) 2006, Intel Corporation \r
5All rights reserved. This 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
cf7e50f8 13**/\r
5d01b0f7 14\r
82096802 15#include "IfrSupportLibInternal.h"\r
5d01b0f7 16\r
2ec4d269 17/**\r
18 Extracts a variable form a Pack.\r
19\r
49a049e6 20 @param Pack List of variables\r
21 @param Name Name of the variable/map\r
22 @param Guid GUID of the variable/map\r
23 @param Id The index of the variable/map to retrieve\r
24 @param Var Pointer to the variable/map\r
25 @param Size Size of the variable/map in bytes\r
2ec4d269 26**/\r
5d01b0f7 27VOID\r
28EfiLibHiiVariablePackGetMap (\r
49a049e6 29 IN EFI_HII_VARIABLE_PACK *Pack, \r
30 OUT CHAR16 **Name, OPTIONAL\r
31 OUT EFI_GUID **Guid, OPTIONAL\r
32 OUT UINT16 *Id, OPTIONAL\r
33 OUT VOID **Var, OPTIONAL\r
34 OUT UINTN *Size OPTIONAL\r
5d01b0f7 35 )\r
5d01b0f7 36{\r
37 if (NULL != Name) {\r
38 *Name = (VOID *) (Pack + 1);\r
39 }\r
40 \r
41 if (NULL != Guid) { \r
42 *Guid = (EFI_GUID *)(UINTN)&Pack->VariableGuid;\r
43 }\r
44 \r
45 \r
46 if (NULL != Id) {\r
47 *Id = Pack->VariableId;\r
48 }\r
49 \r
50 if (NULL != Var) {\r
51 *Var = (VOID *) ((CHAR8 *) (Pack + 1) + Pack->VariableNameLength);\r
52 }\r
53 \r
54 if (NULL != Size) {\r
55 *Size = Pack->Header.Length - sizeof (*Pack) - Pack->VariableNameLength;\r
56 }\r
57}\r
58\r
2ec4d269 59/**\r
60 Finds a count of the variables/maps in the List.\r
61\r
49a049e6 62 @param List List of variables\r
5d01b0f7 63\r
49a049e6 64 @return The number of map count.\r
2ec4d269 65**/\r
5d01b0f7 66UINTN\r
67EfiLibHiiVariablePackListGetMapCnt (\r
49a049e6 68 IN EFI_HII_VARIABLE_PACK_LIST *List\r
5d01b0f7 69 )\r
5d01b0f7 70{\r
49a049e6 71 UINTN Cnt;\r
72 \r
73 Cnt = 0;\r
5d01b0f7 74 while (NULL != List) {\r
75 Cnt++;\r
76 List = List->NextVariablePack;\r
77 }\r
78 return Cnt;\r
79}\r
80\r
2ec4d269 81/**\r
82 Will iterate all variable/maps as appearing \r
83 in List and for each, it will call the Callback.\r
84\r
49a049e6 85 @param List List of variables\r
86 @param Callback Routine to be called for each iterated variable.\r
2ec4d269 87**/\r
5d01b0f7 88VOID\r
89EfiLibHiiVariablePackListForEachVar (\r
49a049e6 90 IN EFI_HII_VARIABLE_PACK_LIST *List,\r
91 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK *Callback\r
5d01b0f7 92 )\r
5d01b0f7 93{\r
49a049e6 94 CHAR16 *MapName;\r
95 EFI_GUID *MapGuid;\r
96 UINT16 MapId;\r
97 VOID *Map;\r
98 UINTN MapSize;\r
5d01b0f7 99\r
100 while (NULL != List) {\r
101 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
102 //\r
103 // call the callback\r
104 //\r
105 Callback (MapName, MapGuid, MapId, Map, MapSize); \r
106 List = List->NextVariablePack;\r
107 }\r
108}\r
109\r
2ec4d269 110/**\r
111 Finds a variable form List given \r
112 the order number as appears in the List.\r
113\r
49a049e6 114 @param Idx The index of the variable/map to retrieve\r
115 @param List List of variables\r
116 @param Name Name of the variable/map\r
117 @param Guid GUID of the variable/map\r
118 @param Id Id of the variable/map\r
119 @param Var Pointer to the variable/map\r
120 @param Size Size of the variable/map in bytes\r
5d01b0f7 121\r
49a049e6 122 @return EFI_SUCCESS Variable is found, OUT parameters are valid\r
123 @return EFI_NOT_FOUND Variable is not found, OUT parameters are not valid\r
2ec4d269 124**/\r
5d01b0f7 125EFI_STATUS\r
126EfiLibHiiVariablePackListGetMapByIdx (\r
49a049e6 127 IN UINTN Idx, \r
128 IN EFI_HII_VARIABLE_PACK_LIST *List, \r
129 OUT CHAR16 **Name, OPTIONAL\r
130 OUT EFI_GUID **Guid, OPTIONAL\r
131 OUT UINT16 *Id, OPTIONAL\r
132 OUT VOID **Var,\r
133 OUT UINTN *Size\r
5d01b0f7 134 )\r
5d01b0f7 135{\r
49a049e6 136 CHAR16 *MapName;\r
137 EFI_GUID *MapGuid;\r
138 UINT16 MapId;\r
139 VOID *Map;\r
140 UINTN MapSize;\r
5d01b0f7 141\r
142 while (NULL != List) {\r
143 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
144 if (0 == Idx--) {\r
145 *Var = Map;\r
146 *Size = MapSize;\r
147\r
148 if (NULL != Name) {\r
149 *Name = MapName;\r
150 }\r
151\r
152 if (NULL != Guid) {\r
153 *Guid = MapGuid;\r
154 }\r
155 \r
156 if (NULL != Id) {\r
157 *Id = MapId;\r
158 }\r
159 \r
160 return EFI_SUCCESS; // Map found\r
161 }\r
162 List = List->NextVariablePack;\r
163 }\r
164 //\r
165 // If here, the map is not found\r
166 //\r
167 return EFI_NOT_FOUND; \r
168}\r
169\r
2ec4d269 170/**\r
171 Finds a variable form List given the \r
172 order number as appears in the List.\r
173\r
49a049e6 174 @param Id The ID of the variable/map to retrieve\r
175 @param List List of variables\r
176 @param Name Name of the variable/map\r
177 @param Guid GUID of the variable/map\r
178 @param Var Pointer to the variable/map\r
179 @param Size Size of the variable/map in bytes\r
2ec4d269 180\r
49a049e6 181 @retval EFI_SUCCESS Variable is found, OUT parameters are valid\r
182 @retval EFI_NOT_FOUND Variable is not found, OUT parameters are not valid\r
2ec4d269 183**/\r
5d01b0f7 184EFI_STATUS\r
185EfiLibHiiVariablePackListGetMapById (\r
49a049e6 186 IN UINT16 Id, \r
187 IN EFI_HII_VARIABLE_PACK_LIST *List,\r
188 OUT CHAR16 **Name, OPTIONAL\r
189 OUT EFI_GUID **Guid, OPTIONAL\r
190 OUT VOID **Var,\r
191 OUT UINTN *Size\r
5d01b0f7 192 )\r
5d01b0f7 193{ \r
49a049e6 194 CHAR16 *MapName;\r
195 EFI_GUID *MapGuid;\r
196 UINT16 MapId;\r
197 VOID *Map;\r
198 UINTN MapSize;\r
5d01b0f7 199\r
200 while (NULL != List) {\r
201 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
202 if (MapId == Id) {\r
203 *Var = Map;\r
204 *Size = MapSize;\r
205 if (NULL != Name) {\r
206 *Name = MapName;\r
207 }\r
208 if (NULL != Guid) {\r
209 *Guid = MapGuid;\r
210 }\r
211 //\r
212 // Map found\r
213 //\r
214 return EFI_SUCCESS; \r
215 }\r
216 List = List->NextVariablePack;\r
217 }\r
218 //\r
219 // If here, the map is not found\r
220 //\r
221 return EFI_NOT_FOUND; \r
222}\r
223\r
2ec4d269 224/**\r
225 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.\r
5d01b0f7 226\r
49a049e6 227 @param List List of variables\r
228 @param Name Name of the variable/map to be found\r
229 @param Guid GUID of the variable/map to be found\r
230 @param Id Id of the variable/map to be found\r
231 @param Var Pointer to the variable/map found\r
232 @param Size Size of the variable/map in bytes found\r
2ec4d269 233\r
49a049e6 234 @retval EFI_SUCCESS variable is found, OUT parameters are valid\r
235 @retval EFI_NOT_FOUND variable is not found, OUT parameters are not valid\r
2ec4d269 236**/\r
5d01b0f7 237EFI_STATUS\r
238EfiLibHiiVariablePackListGetMap (\r
49a049e6 239 IN EFI_HII_VARIABLE_PACK_LIST *List,\r
240 IN CHAR16 *Name,\r
241 IN EFI_GUID *Guid,\r
242 OUT UINT16 *Id,\r
243 OUT VOID **Var, \r
244 OUT UINTN *Size\r
5d01b0f7 245 )\r
5d01b0f7 246{ \r
49a049e6 247 VOID *Map;\r
248 UINTN MapSize;\r
249 UINT16 MapId;\r
250 CHAR16 *MapName;\r
251 EFI_GUID *MapGuid;\r
5d01b0f7 252\r
253 while (NULL != List) {\r
254 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
255 if ((0 == StrCmp (Name, MapName)) && CompareGuid (Guid, MapGuid)) {\r
256 *Id = MapId;\r
257 *Var = Map;\r
258 *Size = MapSize;\r
259 return EFI_SUCCESS;\r
260 }\r
261 List = List->NextVariablePack;\r
262 }\r
263 //\r
264 // If here, the map is not found\r
265 //\r
266 return EFI_NOT_FOUND;\r
267}\r
268\r
2ec4d269 269/**\r
270 Finds out if a variable of specific Name/Guid/Size exists in NV. \r
271 If it does, it will retrieve it into the Var. \r
272\r
49a049e6 273 @param Name Parameters of the variable to retrieve. Must match exactly.\r
274 @param Guid Parameters of the variable to retrieve. Must match exactly.\r
275 @param Size Parameters of the variable to retrieve. Must match exactly.\r
276 @param Var Variable will be retrieved into buffer pointed by this pointer.\r
277 If pointing to NULL, the buffer will be allocated. \r
278 Caller is responsible for releasing the buffer.\r
2ec4d269 279\r
49a049e6 280 @retval EFI_SUCCESS The variable of exact Name/Guid/Size parameters was retrieved and written to Var.\r
281 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.\r
282 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.\r
2ec4d269 283**/\r
5d01b0f7 284EFI_STATUS\r
285EfiLibHiiVariableRetrieveFromNv (\r
49a049e6 286 IN CHAR16 *Name,\r
287 IN EFI_GUID *Guid,\r
288 IN UINTN Size,\r
289 OUT VOID **Var\r
5d01b0f7 290 )\r
5d01b0f7 291{\r
49a049e6 292 EFI_STATUS Status;\r
293 UINTN SizeNv;\r
5d01b0f7 294\r
295 //\r
296 // Test for existence of the variable.\r
297 //\r
298 SizeNv = 0;\r
299 Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, NULL);\r
300 if (EFI_BUFFER_TOO_SMALL != Status) {\r
301 ASSERT (EFI_SUCCESS != Status);\r
302 return EFI_NOT_FOUND;\r
303 }\r
304 if (SizeNv != Size) {\r
305 //\r
306 // The variable is considered corrupt, as it has different size from expected.\r
307 //\r
308 return EFI_LOAD_ERROR; \r
309 }\r
310\r
311 if (NULL == *Var) {\r
312 *Var = AllocatePool (Size);\r
313 ASSERT (NULL != *Var);\r
314 }\r
315 SizeNv = Size;\r
316 //\r
317 // Final read into the Var\r
318 //\r
319 Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, *Var); \r
320 //\r
321 // No tolerance for random failures. Such behavior is undetermined and not validated.\r
322 //\r
323 ASSERT_EFI_ERROR (Status); \r
324 ASSERT (SizeNv == Size);\r
325 return EFI_SUCCESS;\r
326}\r
327\r
2ec4d269 328/**\r
329 Overrrides the variable with NV data if found.\r
330 But it only does it if the Name ends with specified Suffix.\r
331 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",\r
332 the Suffix matches the end of Name, so the variable will be loaded from NV\r
333 provided the variable exists and the GUID and Size matches.\r
5d01b0f7 334\r
49a049e6 335 @param Suffix Suffix the Name should end with.\r
336 @param Name Name of the variable to retrieve.\r
337 @Param Guid Guid of the variable to retrieve.\r
338 @Param Size Parameters of the variable to retrieve.\r
339 @param Var Variable will be retrieved into this buffer.\r
2ec4d269 340 Caller is responsible for providing storage of exactly Size size in bytes.\r
5d01b0f7 341\r
49a049e6 342 @retval EFI_SUCCESS The variable was overriden with NV variable of same Name/Guid/Size.\r
343 @retval EFI_INVALID_PARAMETER The name of the variable does not end with <Suffix>.\r
344 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.\r
345 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.\r
2ec4d269 346**/\r
5d01b0f7 347EFI_STATUS\r
348EfiLibHiiVariableOverrideIfSuffix (\r
49a049e6 349 IN CHAR16 *Suffix,\r
350 IN CHAR16 *Name,\r
351 IN EFI_GUID *Guid,\r
352 IN UINTN Size,\r
353 OUT VOID *Var\r
5d01b0f7 354 ) \r
5d01b0f7 355{\r
49a049e6 356 UINTN StrLength;\r
357 UINTN StrLenSuffix;\r
5d01b0f7 358\r
359 StrLength = StrLen (Name);\r
360 StrLenSuffix = StrLen (Suffix);\r
361 if ((StrLength <= StrLenSuffix) || (0 != StrCmp (Suffix, &Name[StrLength - StrLenSuffix]))) {\r
362 //\r
363 // Not ending with <Suffix>.\r
364 //\r
365 return EFI_INVALID_PARAMETER; \r
366 }\r
367 return EfiLibHiiVariableRetrieveFromNv (Name, Guid, Size, &Var);\r
368}\r
369\r
2ec4d269 370/**\r
5d01b0f7 371 Overrrides the variable with NV data if found.\r
372 But it only does it if the NV contains the same variable with Name is appended with Suffix. \r
373 For example, if Suffix="MyOverride" and the Name="XyzSetup",\r
374 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"\r
375 will be loaded from NV provided the variable exists and the GUID and Size matches.\r
376\r
49a049e6 377 @param Suffix Suffix the variable will be appended with.\r
378 @param Name Parameters of the Name variable to retrieve.\r
379 @param Guid Parameters of the Guid variable to retrieve.\r
380 @param Size Parameters of the Size variable to retrieve.\r
381 @param Var Variable will be retrieved into this buffer.\r
382 Caller is responsible for providing storage of exactly Size size in bytes.\r
5d01b0f7 383\r
49a049e6 384 @retval EFI_SUCCESS The variable was overriden with NV variable of same Name/Guid/Size.\r
385 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.\r
386 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.\r
387**/\r
2ec4d269 388EFI_STATUS\r
389EfiLibHiiVariableOverrideBySuffix (\r
49a049e6 390 IN CHAR16 *Suffix,\r
391 IN CHAR16 *Name,\r
392 IN EFI_GUID *Guid,\r
393 IN UINTN Size,\r
394 OUT VOID *Var\r
395 )\r
5d01b0f7 396{\r
49a049e6 397 EFI_STATUS Status;\r
398 CHAR16 *NameSuffixed;\r
399 UINTN NameLength;\r
400 UINTN SuffixLength;\r
5d01b0f7 401\r
402 //\r
403 // enough to concatenate both strings.\r
404 //\r
405 NameLength = StrLen (Name);\r
406 SuffixLength = StrLen (Suffix);\r
407 NameSuffixed = AllocateZeroPool ((NameLength + SuffixLength + 1) * sizeof (CHAR16)); \r
408 \r
409 StrCpy (NameSuffixed, Name);\r
410 StrCat (NameSuffixed, Suffix);\r
411 \r
412 Status = EfiLibHiiVariableRetrieveFromNv (NameSuffixed, Guid, Size, &Var);\r
413 gBS->FreePool (NameSuffixed);\r
414 \r
415 return Status;\r
416}\r
417\r