2 Variable/Map manipulations routines
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 // Include common header file for this module.
18 #include "IfrSupportLibInternal.h"
21 Extracts a variable form a Pack.
23 @param Pack - List of variables
24 @param Name - Name of the variable/map
25 @param Guid - GUID of the variable/map
26 @param Var - Pointer to the variable/map
27 @param Size - Size of the variable/map in bytes
30 EfiLibHiiVariablePackGetMap (
31 IN EFI_HII_VARIABLE_PACK
*Pack
,
32 OUT CHAR16
**Name
, OPTIONAL
33 OUT EFI_GUID
**Guid
, OPTIONAL
34 OUT UINT16
*Id
, OPTIONAL
35 OUT VOID
**Var
, OPTIONAL
36 OUT UINTN
*Size OPTIONAL
41 *Name
= (VOID
*) (Pack
+ 1);
45 *Guid
= (EFI_GUID
*)(UINTN
)&Pack
->VariableGuid
;
50 *Id
= Pack
->VariableId
;
54 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
58 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
63 Finds a count of the variables/maps in the List.
65 @param List - List of variables
67 @return The number of map count.
70 EfiLibHiiVariablePackListGetMapCnt (
71 IN EFI_HII_VARIABLE_PACK_LIST
*List
75 while (NULL
!= List
) {
77 List
= List
->NextVariablePack
;
83 Will iterate all variable/maps as appearing
84 in List and for each, it will call the Callback.
86 @param List - List of variables
87 @param Callback - Routine to be called for each iterated variable.
91 EfiLibHiiVariablePackListForEachVar (
92 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
93 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK
*Callback
104 while (NULL
!= List
) {
105 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
109 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
110 List
= List
->NextVariablePack
;
115 Finds a variable form List given
116 the order number as appears in the List.
118 @param Idx - The index of the variable/map to retrieve
119 @param List - List of variables
120 @param Name - Name of the variable/map
121 @param Guid - GUID of the variable/map
122 @param Var - Pointer to the variable/map
123 @param Size - Size of the variable/map in bytes
125 @param EFI_SUCCESS - Variable is found, OUT parameters are valid
126 @param EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
129 EfiLibHiiVariablePackListGetMapByIdx (
131 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
132 OUT CHAR16
**Name
, OPTIONAL
133 OUT EFI_GUID
**Guid
, OPTIONAL
134 OUT UINT16
*Id
, OPTIONAL
147 while (NULL
!= List
) {
148 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
165 return EFI_SUCCESS
; // Map found
167 List
= List
->NextVariablePack
;
170 // If here, the map is not found
172 return EFI_NOT_FOUND
;
176 Finds a variable form List given the
177 order number as appears in the List.
179 @param Id - The ID of the variable/map to retrieve
180 @param List - List of variables
181 @param Name - Name of the variable/map
182 @param Guid - GUID of the variable/map
183 @param Var - Pointer to the variable/map
184 @param Size - Size of the variable/map in bytes
186 @retval EFI_SUCCESS - Variable is found, OUT parameters are valid
187 @retval EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
191 EfiLibHiiVariablePackListGetMapById (
193 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
194 OUT CHAR16
**Name
, OPTIONAL
195 OUT EFI_GUID
**Guid
, OPTIONAL
206 while (NULL
!= List
) {
207 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
222 List
= List
->NextVariablePack
;
225 // If here, the map is not found
227 return EFI_NOT_FOUND
;
231 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
233 @param List - List of variables
234 @param Name - Name of the variable/map to be found
235 @param Guid - GUID of the variable/map to be found
236 @param Var - Pointer to the variable/map found
237 @param Size - Size of the variable/map in bytes found
239 @retval EFI_SUCCESS - variable is found, OUT parameters are valid
240 @retval EFI_NOT_FOUND - variable is not found, OUT parameters are not valid
243 EfiLibHiiVariablePackListGetMap (
244 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
258 while (NULL
!= List
) {
259 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
260 if ((0 == StrCmp (Name
, MapName
)) && CompareGuid (Guid
, MapGuid
)) {
266 List
= List
->NextVariablePack
;
269 // If here, the map is not found
271 return EFI_NOT_FOUND
;
275 Finds out if a variable of specific Name/Guid/Size exists in NV.
276 If it does, it will retrieve it into the Var.
278 @param Name Parameters of the variable to retrieve. Must match exactly.
279 @param Guid Parameters of the variable to retrieve. Must match exactly.
280 @param Size Parameters of the variable to retrieve. Must match exactly.
281 @param Var Variable will be retrieved into buffer pointed by this pointer.
282 If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.
284 @retval EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
285 @retval EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
286 @retval EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
290 EfiLibHiiVariableRetrieveFromNv (
302 // Test for existence of the variable.
305 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, NULL
);
306 if (EFI_BUFFER_TOO_SMALL
!= Status
) {
307 ASSERT (EFI_SUCCESS
!= Status
);
308 return EFI_NOT_FOUND
;
310 if (SizeNv
!= Size
) {
312 // The variable is considered corrupt, as it has different size from expected.
314 return EFI_LOAD_ERROR
;
318 *Var
= AllocatePool (Size
);
319 ASSERT (NULL
!= *Var
);
323 // Final read into the Var
325 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, *Var
);
327 // No tolerance for random failures. Such behavior is undetermined and not validated.
329 ASSERT_EFI_ERROR (Status
);
330 ASSERT (SizeNv
== Size
);
335 Overrrides the variable with NV data if found.
336 But it only does it if the Name ends with specified Suffix.
337 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",
338 the Suffix matches the end of Name, so the variable will be loaded from NV
339 provided the variable exists and the GUID and Size matches.
341 @param Suffix Suffix the Name should end with.
342 @param Name, Guid, Size Parameters of the variable to retrieve. Must match exactly.
343 @param Var Variable will be retrieved into this buffer.
344 Caller is responsible for providing storage of exactly Size size in bytes.
346 @retval EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
347 @retval EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.
348 @retval EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
349 @retval EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
352 EfiLibHiiVariableOverrideIfSuffix (
363 StrLength
= StrLen (Name
);
364 StrLenSuffix
= StrLen (Suffix
);
365 if ((StrLength
<= StrLenSuffix
) || (0 != StrCmp (Suffix
, &Name
[StrLength
- StrLenSuffix
]))) {
367 // Not ending with <Suffix>.
369 return EFI_INVALID_PARAMETER
;
371 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
375 Overrrides the variable with NV data if found.
376 But it only does it if the NV contains the same variable with Name is appended with Suffix.
377 For example, if Suffix="MyOverride" and the Name="XyzSetup",
378 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
379 will be loaded from NV provided the variable exists and the GUID and Size matches.
381 @param Suffix Suffix the variable will be appended with.
382 @param Name, Guid, Size Parameters of the variable to retrieve. Must match exactly.
383 @param Var Variable will be retrieved into this buffer.
384 Caller is responsible for providing storage of exactly Size size in bytes.
386 @retval EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
387 @retval EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
388 @retval EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
392 EfiLibHiiVariableOverrideBySuffix (
402 CHAR16
*NameSuffixed
;
407 // enough to concatenate both strings.
409 NameLength
= StrLen (Name
);
410 SuffixLength
= StrLen (Suffix
);
411 NameSuffixed
= AllocateZeroPool ((NameLength
+ SuffixLength
+ 1) * sizeof (CHAR16
));
413 StrCpy (NameSuffixed
, Name
);
414 StrCat (NameSuffixed
, Suffix
);
416 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
417 gBS
->FreePool (NameSuffixed
);