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 Id - The index of the variable/map to retrieve
27 @param Var - Pointer to the variable/map
28 @param Size - Size of the variable/map in bytes
31 EfiLibHiiVariablePackGetMap (
32 IN EFI_HII_VARIABLE_PACK
*Pack
,
33 OUT CHAR16
**Name
, OPTIONAL
34 OUT EFI_GUID
**Guid
, OPTIONAL
35 OUT UINT16
*Id
, OPTIONAL
36 OUT VOID
**Var
, OPTIONAL
37 OUT UINTN
*Size OPTIONAL
42 *Name
= (VOID
*) (Pack
+ 1);
46 *Guid
= (EFI_GUID
*)(UINTN
)&Pack
->VariableGuid
;
51 *Id
= Pack
->VariableId
;
55 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
59 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
64 Finds a count of the variables/maps in the List.
66 @param List - List of variables
68 @return The number of map count.
71 EfiLibHiiVariablePackListGetMapCnt (
72 IN EFI_HII_VARIABLE_PACK_LIST
*List
76 while (NULL
!= List
) {
78 List
= List
->NextVariablePack
;
84 Will iterate all variable/maps as appearing
85 in List and for each, it will call the Callback.
87 @param List - List of variables
88 @param Callback - Routine to be called for each iterated variable.
92 EfiLibHiiVariablePackListForEachVar (
93 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
94 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK
*Callback
105 while (NULL
!= List
) {
106 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
110 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
111 List
= List
->NextVariablePack
;
116 Finds a variable form List given
117 the order number as appears in the List.
119 @param Idx - The index of the variable/map to retrieve
120 @param List - List of variables
121 @param Name - Name of the variable/map
122 @param Guid - GUID of the variable/map
123 @param Id - Id of the variable/map
124 @param Var - Pointer to the variable/map
125 @param Size - Size of the variable/map in bytes
127 @return EFI_SUCCESS - Variable is found, OUT parameters are valid
128 @return EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
131 EfiLibHiiVariablePackListGetMapByIdx (
133 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
134 OUT CHAR16
**Name
, OPTIONAL
135 OUT EFI_GUID
**Guid
, OPTIONAL
136 OUT UINT16
*Id
, OPTIONAL
149 while (NULL
!= List
) {
150 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
167 return EFI_SUCCESS
; // Map found
169 List
= List
->NextVariablePack
;
172 // If here, the map is not found
174 return EFI_NOT_FOUND
;
178 Finds a variable form List given the
179 order number as appears in the List.
181 @param Id - The ID of the variable/map to retrieve
182 @param List - List of variables
183 @param Name - Name of the variable/map
184 @param Guid - GUID of the variable/map
185 @param Var - Pointer to the variable/map
186 @param Size - Size of the variable/map in bytes
188 @retval EFI_SUCCESS - Variable is found, OUT parameters are valid
189 @retval EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
193 EfiLibHiiVariablePackListGetMapById (
195 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
196 OUT CHAR16
**Name
, OPTIONAL
197 OUT EFI_GUID
**Guid
, OPTIONAL
208 while (NULL
!= List
) {
209 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
224 List
= List
->NextVariablePack
;
227 // If here, the map is not found
229 return EFI_NOT_FOUND
;
233 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
235 @param List - List of variables
236 @param Name - Name of the variable/map to be found
237 @param Guid - GUID of the variable/map to be found
238 @param Id - Id of the variable/map to be found
239 @param Var - Pointer to the variable/map found
240 @param Size - Size of the variable/map in bytes found
242 @retval EFI_SUCCESS - variable is found, OUT parameters are valid
243 @retval EFI_NOT_FOUND - variable is not found, OUT parameters are not valid
246 EfiLibHiiVariablePackListGetMap (
247 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
261 while (NULL
!= List
) {
262 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
263 if ((0 == StrCmp (Name
, MapName
)) && CompareGuid (Guid
, MapGuid
)) {
269 List
= List
->NextVariablePack
;
272 // If here, the map is not found
274 return EFI_NOT_FOUND
;
278 Finds out if a variable of specific Name/Guid/Size exists in NV.
279 If it does, it will retrieve it into the Var.
281 @param Name Parameters of the variable to retrieve. Must match exactly.
282 @param Guid Parameters of the variable to retrieve. Must match exactly.
283 @param Size Parameters of the variable to retrieve. Must match exactly.
284 @param Var Variable will be retrieved into buffer pointed by this pointer.
285 If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.
287 @retval EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
288 @retval EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
289 @retval EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
293 EfiLibHiiVariableRetrieveFromNv (
305 // Test for existence of the variable.
308 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, NULL
);
309 if (EFI_BUFFER_TOO_SMALL
!= Status
) {
310 ASSERT (EFI_SUCCESS
!= Status
);
311 return EFI_NOT_FOUND
;
313 if (SizeNv
!= Size
) {
315 // The variable is considered corrupt, as it has different size from expected.
317 return EFI_LOAD_ERROR
;
321 *Var
= AllocatePool (Size
);
322 ASSERT (NULL
!= *Var
);
326 // Final read into the Var
328 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, *Var
);
330 // No tolerance for random failures. Such behavior is undetermined and not validated.
332 ASSERT_EFI_ERROR (Status
);
333 ASSERT (SizeNv
== Size
);
338 Overrrides the variable with NV data if found.
339 But it only does it if the Name ends with specified Suffix.
340 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",
341 the Suffix matches the end of Name, so the variable will be loaded from NV
342 provided the variable exists and the GUID and Size matches.
344 @param Suffix Suffix the Name should end with.
345 @param Name, Guid, Size Parameters of the variable to retrieve. Must match exactly.
346 @param Var Variable will be retrieved into this buffer.
347 Caller is responsible for providing storage of exactly Size size in bytes.
349 @retval EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
350 @retval EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.
351 @retval EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
352 @retval EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
355 EfiLibHiiVariableOverrideIfSuffix (
366 StrLength
= StrLen (Name
);
367 StrLenSuffix
= StrLen (Suffix
);
368 if ((StrLength
<= StrLenSuffix
) || (0 != StrCmp (Suffix
, &Name
[StrLength
- StrLenSuffix
]))) {
370 // Not ending with <Suffix>.
372 return EFI_INVALID_PARAMETER
;
374 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
378 Overrrides the variable with NV data if found.
379 But it only does it if the NV contains the same variable with Name is appended with Suffix.
380 For example, if Suffix="MyOverride" and the Name="XyzSetup",
381 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
382 will be loaded from NV provided the variable exists and the GUID and Size matches.
384 @param Suffix Suffix the variable will be appended with.
385 @param Name, Guid, Size Parameters of the variable to retrieve. Must match exactly.
386 @param Var Variable will be retrieved into this buffer.
387 Caller is responsible for providing storage of exactly Size size in bytes.
389 @retval EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
390 @retval EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
391 @retval EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
395 EfiLibHiiVariableOverrideBySuffix (
405 CHAR16
*NameSuffixed
;
410 // enough to concatenate both strings.
412 NameLength
= StrLen (Name
);
413 SuffixLength
= StrLen (Suffix
);
414 NameSuffixed
= AllocateZeroPool ((NameLength
+ SuffixLength
+ 1) * sizeof (CHAR16
));
416 StrCpy (NameSuffixed
, Name
);
417 StrCat (NameSuffixed
, Suffix
);
419 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
420 gBS
->FreePool (NameSuffixed
);