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.
15 #include "IfrSupportLibInternal.h"
18 Extracts a variable form a Pack.
20 @param Pack List of variables
21 @param Name Name of the variable/map
22 @param Guid GUID of the variable/map
23 @param Id The index of the variable/map to retrieve
24 @param Var Pointer to the variable/map
25 @param Size Size of the variable/map in bytes
28 EfiLibHiiVariablePackGetMap (
29 IN EFI_HII_VARIABLE_PACK
*Pack
,
30 OUT CHAR16
**Name
, OPTIONAL
31 OUT EFI_GUID
**Guid
, OPTIONAL
32 OUT UINT16
*Id
, OPTIONAL
33 OUT VOID
**Var
, OPTIONAL
34 OUT UINTN
*Size OPTIONAL
38 *Name
= (VOID
*) (Pack
+ 1);
42 *Guid
= (EFI_GUID
*)(UINTN
)&Pack
->VariableGuid
;
47 *Id
= Pack
->VariableId
;
51 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
55 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
60 Finds a count of the variables/maps in the List.
62 @param List List of variables
64 @return The number of map count.
67 EfiLibHiiVariablePackListGetMapCnt (
68 IN EFI_HII_VARIABLE_PACK_LIST
*List
74 while (NULL
!= List
) {
76 List
= List
->NextVariablePack
;
82 Will iterate all variable/maps as appearing
83 in List and for each, it will call the Callback.
85 @param List List of variables
86 @param Callback Routine to be called for each iterated variable.
89 EfiLibHiiVariablePackListForEachVar (
90 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
91 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK
*Callback
100 while (NULL
!= List
) {
101 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
105 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
106 List
= List
->NextVariablePack
;
111 Finds a variable form List given
112 the order number as appears in the List.
114 @param Idx The index of the variable/map to retrieve
115 @param List List of variables
116 @param Name Name of the variable/map
117 @param Guid GUID of the variable/map
118 @param Id Id of the variable/map
119 @param Var Pointer to the variable/map
120 @param Size Size of the variable/map in bytes
122 @return EFI_SUCCESS Variable is found, OUT parameters are valid
123 @return EFI_NOT_FOUND Variable is not found, OUT parameters are not valid
126 EfiLibHiiVariablePackListGetMapByIdx (
128 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
129 OUT CHAR16
**Name
, OPTIONAL
130 OUT EFI_GUID
**Guid
, OPTIONAL
131 OUT UINT16
*Id
, OPTIONAL
142 while (NULL
!= List
) {
143 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
160 return EFI_SUCCESS
; // Map found
162 List
= List
->NextVariablePack
;
165 // If here, the map is not found
167 return EFI_NOT_FOUND
;
171 Finds a variable form List given the
172 order number as appears in the List.
174 @param Id The ID of the variable/map to retrieve
175 @param List List of variables
176 @param Name Name of the variable/map
177 @param Guid GUID of the variable/map
178 @param Var Pointer to the variable/map
179 @param Size Size of the variable/map in bytes
181 @retval EFI_SUCCESS Variable is found, OUT parameters are valid
182 @retval EFI_NOT_FOUND Variable is not found, OUT parameters are not valid
185 EfiLibHiiVariablePackListGetMapById (
187 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
188 OUT CHAR16
**Name
, OPTIONAL
189 OUT EFI_GUID
**Guid
, OPTIONAL
200 while (NULL
!= List
) {
201 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
216 List
= List
->NextVariablePack
;
219 // If here, the map is not found
221 return EFI_NOT_FOUND
;
225 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
227 @param List List of variables
228 @param Name Name of the variable/map to be found
229 @param Guid GUID of the variable/map to be found
230 @param Id Id of the variable/map to be found
231 @param Var Pointer to the variable/map found
232 @param Size Size of the variable/map in bytes found
234 @retval EFI_SUCCESS variable is found, OUT parameters are valid
235 @retval EFI_NOT_FOUND variable is not found, OUT parameters are not valid
238 EfiLibHiiVariablePackListGetMap (
239 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
253 while (NULL
!= List
) {
254 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
255 if ((0 == StrCmp (Name
, MapName
)) && CompareGuid (Guid
, MapGuid
)) {
261 List
= List
->NextVariablePack
;
264 // If here, the map is not found
266 return EFI_NOT_FOUND
;
270 Finds out if a variable of specific Name/Guid/Size exists in NV.
271 If it does, it will retrieve it into the Var.
273 @param Name Parameters of the variable to retrieve. Must match exactly.
274 @param Guid Parameters of the variable to retrieve. Must match exactly.
275 @param Size Parameters of the variable to retrieve. Must match exactly.
276 @param Var Variable will be retrieved into buffer pointed by this pointer.
277 If pointing to NULL, the buffer will be allocated.
278 Caller is responsible for releasing the buffer.
280 @retval EFI_SUCCESS The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
281 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.
282 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.
285 EfiLibHiiVariableRetrieveFromNv (
296 // Test for existence of the variable.
299 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, NULL
);
300 if (EFI_BUFFER_TOO_SMALL
!= Status
) {
301 ASSERT (EFI_SUCCESS
!= Status
);
302 return EFI_NOT_FOUND
;
304 if (SizeNv
!= Size
) {
306 // The variable is considered corrupt, as it has different size from expected.
308 return EFI_LOAD_ERROR
;
312 *Var
= AllocatePool (Size
);
313 ASSERT (NULL
!= *Var
);
317 // Final read into the Var
319 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, *Var
);
321 // No tolerance for random failures. Such behavior is undetermined and not validated.
323 ASSERT_EFI_ERROR (Status
);
324 ASSERT (SizeNv
== Size
);
329 Overrrides the variable with NV data if found.
330 But it only does it if the Name ends with specified Suffix.
331 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",
332 the Suffix matches the end of Name, so the variable will be loaded from NV
333 provided the variable exists and the GUID and Size matches.
335 @param Suffix Suffix the Name should end with.
336 @param Name Name of the variable to retrieve.
337 @Param Guid Guid of the variable to retrieve.
338 @Param Size Parameters of the variable to retrieve.
339 @param Var Variable will be retrieved into this buffer.
340 Caller is responsible for providing storage of exactly Size size in bytes.
342 @retval EFI_SUCCESS The variable was overriden with NV variable of same Name/Guid/Size.
343 @retval EFI_INVALID_PARAMETER The name of the variable does not end with <Suffix>.
344 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.
345 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.
348 EfiLibHiiVariableOverrideIfSuffix (
359 StrLength
= StrLen (Name
);
360 StrLenSuffix
= StrLen (Suffix
);
361 if ((StrLength
<= StrLenSuffix
) || (0 != StrCmp (Suffix
, &Name
[StrLength
- StrLenSuffix
]))) {
363 // Not ending with <Suffix>.
365 return EFI_INVALID_PARAMETER
;
367 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
371 Overrrides the variable with NV data if found.
372 But it only does it if the NV contains the same variable with Name is appended with Suffix.
373 For example, if Suffix="MyOverride" and the Name="XyzSetup",
374 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
375 will be loaded from NV provided the variable exists and the GUID and Size matches.
377 @param Suffix Suffix the variable will be appended with.
378 @param Name Parameters of the Name variable to retrieve.
379 @param Guid Parameters of the Guid variable to retrieve.
380 @param Size Parameters of the Size variable to retrieve.
381 @param Var Variable will be retrieved into this buffer.
382 Caller is responsible for providing storage of exactly Size size in bytes.
384 @retval EFI_SUCCESS The variable was overriden with NV variable of same Name/Guid/Size.
385 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.
386 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.
389 EfiLibHiiVariableOverrideBySuffix (
398 CHAR16
*NameSuffixed
;
403 // enough to concatenate both strings.
405 NameLength
= StrLen (Name
);
406 SuffixLength
= StrLen (Suffix
);
407 NameSuffixed
= AllocateZeroPool ((NameLength
+ SuffixLength
+ 1) * sizeof (CHAR16
));
409 StrCpy (NameSuffixed
, Name
);
410 StrCat (NameSuffixed
, Suffix
);
412 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
413 gBS
->FreePool (NameSuffixed
);