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