]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/Dxe/EfiIfrSupportLib/IfrVariable.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Dxe / EfiIfrSupportLib / IfrVariable.c
CommitLineData
3eb9473e 1/*++\r
2\r
4ea9375a
HT
3Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials \r
3eb9473e 5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13 IfrVariable.c\r
14\r
15Abstract:\r
16 Variable/Map manipulations routines\r
17\r
18--*/\r
19\r
20#include "IfrLibrary.h"\r
21\r
22VOID\r
23EfiLibHiiVariablePackGetMap (\r
24 IN EFI_HII_VARIABLE_PACK *Pack, \r
25 OUT CHAR16 **Name, OPTIONAL\r
26 OUT EFI_GUID **Guid, OPTIONAL\r
27 OUT UINT16 *Id, OPTIONAL\r
28 OUT VOID **Var, OPTIONAL\r
29 OUT UINTN *Size OPTIONAL\r
30 )\r
31/*++\r
32\r
33Routine Description:\r
34\r
35 Extracts a variable form a Pack.\r
36\r
37Arguments:\r
38\r
39 Pack - List of variables\r
40 Name - Name of the variable/map\r
41 Guid - GUID of the variable/map\r
42 Var - Pointer to the variable/map\r
43 Size - Size of the variable/map in bytes\r
44\r
45Returns: \r
46\r
47 VOID\r
48 \r
49--*/\r
50{\r
51 if (NULL != Name) {\r
52 *Name = (VOID *) (Pack + 1);\r
53 }\r
54 \r
55 if (NULL != Guid) { \r
56 *Guid = &Pack->VariableGuid;\r
57 }\r
58 \r
59 \r
60 if (NULL != Id) {\r
61 *Id = Pack->VariableId;\r
62 }\r
63 \r
64 if (NULL != Var) {\r
65 *Var = (VOID *) ((CHAR8 *) (Pack + 1) + Pack->VariableNameLength);\r
66 }\r
67 \r
68 if (NULL != Size) {\r
69 *Size = Pack->Header.Length - sizeof (*Pack) - Pack->VariableNameLength;\r
70 }\r
71}\r
72\r
73\r
74UINTN\r
75EfiLibHiiVariablePackListGetMapCnt (\r
76 IN EFI_HII_VARIABLE_PACK_LIST *List\r
77 )\r
78 \r
79/*++\r
80\r
81Routine Description:\r
82\r
83 Finds a count of the variables/maps in the List.\r
84\r
85Arguments:\r
86\r
87 List - List of variables\r
88\r
89Returns: \r
90\r
91 UINTN - The number of map count.\r
92\r
93--*/\r
94\r
95{\r
96 UINTN Cnt = 0;\r
97 while (NULL != List) {\r
98 Cnt++;\r
99 List = List->NextVariablePack;\r
100 }\r
101 return Cnt;\r
102}\r
103\r
104\r
105VOID\r
106EfiLibHiiVariablePackListForEachVar (\r
107 IN EFI_HII_VARIABLE_PACK_LIST *List,\r
108 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK *Callback\r
109 )\r
110/*++\r
111\r
112Routine Description:\r
113\r
114 Will iterate all variable/maps as appearing \r
115 in List and for each, it will call the Callback.\r
116\r
117Arguments:\r
118\r
119 List - List of variables\r
120 Callback - Routine to be called for each iterated variable.\r
121\r
122Returns: \r
123\r
124 VOID\r
125 \r
126--*/\r
127\r
128{\r
129 CHAR16 *MapName;\r
130 EFI_GUID *MapGuid;\r
131 UINT16 MapId;\r
132 VOID *Map;\r
133 UINTN MapSize;\r
134\r
135 while (NULL != List) {\r
136 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
137 //\r
138 // call the callback\r
139 //\r
140 Callback (MapName, MapGuid, MapId, Map, MapSize); \r
141 List = List->NextVariablePack;\r
142 }\r
143}\r
144\r
145\r
146EFI_STATUS\r
147EfiLibHiiVariablePackListGetMapByIdx (\r
148 IN UINTN Idx, \r
149 IN EFI_HII_VARIABLE_PACK_LIST *List, \r
150 OUT CHAR16 **Name, OPTIONAL\r
151 OUT EFI_GUID **Guid, OPTIONAL\r
152 OUT UINT16 *Id, OPTIONAL\r
153 OUT VOID **Var,\r
154 OUT UINTN *Size\r
155 )\r
156\r
157/*++\r
158\r
159Routine Description:\r
160\r
161 Finds a variable form List given \r
162 the order number as appears in the List.\r
163\r
164Arguments:\r
165\r
166 Idx - The index of the variable/map to retrieve\r
167 List - List of variables\r
168 Name - Name of the variable/map\r
169 Guid - GUID of the variable/map\r
170 Var - Pointer to the variable/map\r
171 Size - Size of the variable/map in bytes\r
172\r
173Returns:\r
174\r
175 EFI_SUCCESS - Variable is found, OUT parameters are valid\r
176 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid\r
177\r
178--*/\r
179{\r
180 CHAR16 *MapName;\r
181 EFI_GUID *MapGuid;\r
182 UINT16 MapId;\r
183 VOID *Map;\r
184 UINTN MapSize;\r
185\r
186 while (NULL != List) {\r
187 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
188 if (0 == Idx--) {\r
189 *Var = Map;\r
190 *Size = MapSize;\r
191\r
192 if (NULL != Name) {\r
193 *Name = MapName;\r
194 }\r
195\r
196 if (NULL != Guid) {\r
197 *Guid = MapGuid;\r
198 }\r
199 \r
200 if (NULL != Id) {\r
201 *Id = MapId;\r
202 }\r
203 \r
204 return EFI_SUCCESS; // Map found\r
205 }\r
206 List = List->NextVariablePack;\r
207 }\r
208 //\r
209 // If here, the map is not found\r
210 //\r
211 return EFI_NOT_FOUND; \r
212}\r
213\r
214\r
215EFI_STATUS\r
216EfiLibHiiVariablePackListGetMapById (\r
217 IN UINT16 Id, \r
218 IN EFI_HII_VARIABLE_PACK_LIST *List,\r
219 OUT CHAR16 **Name, OPTIONAL\r
220 OUT EFI_GUID **Guid, OPTIONAL\r
221 OUT VOID **Var,\r
222 OUT UINTN *Size\r
223 )\r
224 \r
225/*++\r
226\r
227Routine Description:\r
228\r
229 Finds a variable form List given the \r
230 order number as appears in the List.\r
231\r
232Arguments:\r
233\r
234 Id - The ID of the variable/map to retrieve\r
235 List - List of variables\r
236 Name - Name of the variable/map\r
237 Guid - GUID of the variable/map\r
238 Var - Pointer to the variable/map\r
239 Size - Size of the variable/map in bytes\r
240\r
241Returns:\r
242\r
243 EFI_SUCCESS - Variable is found, OUT parameters are valid\r
244 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid\r
245\r
246--*/\r
247\r
248{ \r
249 CHAR16 *MapName;\r
250 EFI_GUID *MapGuid;\r
251 UINT16 MapId;\r
252 VOID *Map;\r
253 UINTN MapSize;\r
254\r
255 while (NULL != List) {\r
256 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
257 if (MapId == Id) {\r
258 *Var = Map;\r
259 *Size = MapSize;\r
260 if (NULL != Name) {\r
261 *Name = MapName;\r
262 }\r
263 if (NULL != Guid) {\r
264 *Guid = MapGuid;\r
265 }\r
266 //\r
267 // Map found\r
268 //\r
269 return EFI_SUCCESS; \r
270 }\r
271 List = List->NextVariablePack;\r
272 }\r
273 //\r
274 // If here, the map is not found\r
275 //\r
276 return EFI_NOT_FOUND; \r
277}\r
278\r
279\r
280EFI_STATUS\r
281EfiLibHiiVariablePackListGetMap (\r
282 IN EFI_HII_VARIABLE_PACK_LIST *List,\r
283 IN CHAR16 *Name,\r
284 IN EFI_GUID *Guid,\r
285 OUT UINT16 *Id,\r
286 OUT VOID **Var, \r
287 OUT UINTN *Size\r
288 )\r
289\r
290/*++\r
291\r
292Routine Description:\r
293\r
294 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.\r
295\r
296Arguments:\r
297\r
298 List - List of variables\r
299 Name - Name of the variable/map to be found\r
300 Guid - GUID of the variable/map to be found\r
301 Var - Pointer to the variable/map found\r
302 Size - Size of the variable/map in bytes found\r
303\r
304Returns:\r
305\r
306 EFI_SUCCESS - variable is found, OUT parameters are valid\r
307 EFI_NOT_FOUND - variable is not found, OUT parameters are not valid\r
308\r
309--*/\r
310\r
311{ \r
312 VOID *Map;\r
313 UINTN MapSize;\r
314 UINT16 MapId;\r
315 CHAR16 *MapName;\r
316 EFI_GUID *MapGuid;\r
317\r
318 while (NULL != List) {\r
319 EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
320 if ((0 == EfiStrCmp (Name, MapName)) && EfiCompareGuid (Guid, MapGuid)) {\r
321 *Id = MapId;\r
322 *Var = Map;\r
323 *Size = MapSize;\r
324 return EFI_SUCCESS;\r
325 }\r
326 List = List->NextVariablePack;\r
327 }\r
328 //\r
329 // If here, the map is not found\r
330 //\r
331 return EFI_NOT_FOUND;\r
332}\r
333\r
334EFI_STATUS\r
335EfiLibHiiVariableRetrieveFromNv (\r
336 IN CHAR16 *Name,\r
337 IN EFI_GUID *Guid,\r
338 IN UINTN Size,\r
339 OUT VOID **Var\r
340 )\r
341/*++\r
342\r
343Routine Description:\r
344 Finds out if a variable of specific Name/Guid/Size exists in NV. \r
345 If it does, it will retrieve it into the Var. \r
346\r
347Arguments:\r
348 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r
349 Var - Variable will be retrieved into buffer pointed by this pointer.\r
350 If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.\r
351Returns:\r
352 EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.\r
353 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r
354 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r
355\r
356--*/\r
357{\r
358 EFI_STATUS Status;\r
359 UINTN SizeNv;\r
360\r
361 //\r
362 // Test for existence of the variable.\r
363 //\r
364 SizeNv = 0;\r
365 Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, NULL);\r
366 if (EFI_BUFFER_TOO_SMALL != Status) {\r
367 ASSERT (EFI_SUCCESS != Status);\r
368 return EFI_NOT_FOUND;\r
369 }\r
370 if (SizeNv != Size) {\r
371 //\r
372 // The variable is considered corrupt, as it has different size from expected.\r
373 //\r
374 return EFI_LOAD_ERROR; \r
375 }\r
376\r
377 if (NULL == *Var) {\r
378 *Var = EfiLibAllocatePool (Size);\r
379 ASSERT (NULL != *Var);\r
380 }\r
381 SizeNv = Size;\r
382 //\r
383 // Final read into the Var\r
384 //\r
385 Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, *Var); \r
386 //\r
387 // No tolerance for random failures. Such behavior is undetermined and not validated.\r
388 //\r
389 ASSERT_EFI_ERROR (Status); \r
390 ASSERT (SizeNv == Size);\r
391 return EFI_SUCCESS;\r
392}\r
393\r
394\r
395\r
396EFI_STATUS\r
397EfiLibHiiVariableOverrideIfSuffix (\r
398 IN CHAR16 *Suffix,\r
399 IN CHAR16 *Name,\r
400 IN EFI_GUID *Guid,\r
401 IN UINTN Size,\r
402 OUT VOID *Var\r
403 ) \r
404/*++\r
405\r
406Routine Description:\r
407 Overrrides the variable with NV data if found.\r
408 But it only does it if the Name ends with specified Suffix.\r
409 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",\r
410 the Suffix matches the end of Name, so the variable will be loaded from NV\r
411 provided the variable exists and the GUID and Size matches.\r
412\r
413Arguments:\r
414 Suffix - Suffix the Name should end with.\r
415 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r
416 Var - Variable will be retrieved into this buffer.\r
417 Caller is responsible for providing storage of exactly Size size in bytes.\r
418Returns:\r
419 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.\r
420 EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.\r
421 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r
422 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r
423\r
424--*/\r
425{\r
426 UINTN StrLen;\r
427 UINTN StrLenSuffix;\r
428\r
429 StrLen = EfiStrLen (Name);\r
430 StrLenSuffix = EfiStrLen (Suffix);\r
431 if ((StrLen <= StrLenSuffix) || (0 != EfiStrCmp (Suffix, &Name[StrLen - StrLenSuffix]))) {\r
432 //\r
433 // Not ending with <Suffix>.\r
434 //\r
435 return EFI_INVALID_PARAMETER; \r
436 }\r
437 return EfiLibHiiVariableRetrieveFromNv (Name, Guid, Size, &Var);\r
438}\r
439\r
440\r
441\r
442EFI_STATUS\r
443EfiLibHiiVariableOverrideBySuffix (\r
444 IN CHAR16 *Suffix,\r
445 IN CHAR16 *Name,\r
446 IN EFI_GUID *Guid,\r
447 IN UINTN Size,\r
448 OUT VOID *Var\r
449 ) \r
450/*++\r
451\r
452Routine Description:\r
453 Overrrides the variable with NV data if found.\r
454 But it only does it if the NV contains the same variable with Name is appended with Suffix. \r
455 For example, if Suffix="MyOverride" and the Name="XyzSetup",\r
456 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"\r
457 will be loaded from NV provided the variable exists and the GUID and Size matches.\r
458\r
459Arguments:\r
460 Suffix - Suffix the variable will be appended with.\r
461 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r
462 Var - Variable will be retrieved into this buffer.\r
463 Caller is responsible for providing storage of exactly Size size in bytes.\r
464\r
465Returns:\r
466 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.\r
467 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r
468 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r
469\r
470--*/\r
471{\r
472 EFI_STATUS Status;\r
473 CHAR16 *NameSuffixed;\r
4cb43192 474 UINTN NameLength;\r
475 UINTN SuffixLength;\r
3eb9473e 476\r
477 //\r
478 // enough to concatenate both strings.\r
479 //\r
4cb43192 480 NameLength = EfiStrLen (Name);\r
481 SuffixLength = EfiStrLen (Suffix);\r
482 NameSuffixed = EfiLibAllocateZeroPool ((NameLength + SuffixLength + 1) * sizeof (CHAR16)); \r
3eb9473e 483 \r
484 EfiStrCpy (NameSuffixed, Name);\r
485 EfiStrCat (NameSuffixed, Suffix);\r
486 \r
487 Status = EfiLibHiiVariableRetrieveFromNv (NameSuffixed, Guid, Size, &Var);\r
488 gBS->FreePool (NameSuffixed);\r
489 \r
490 return Status;\r
491}\r
492\r