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