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
29 EfiLibHiiVariablePackGetMap (
30 IN EFI_HII_VARIABLE_PACK
*Pack
,
31 OUT CHAR16
**Name
, OPTIONAL
32 OUT EFI_GUID
**Guid
, OPTIONAL
33 OUT UINT16
*Id
, OPTIONAL
34 OUT VOID
**Var
, OPTIONAL
35 OUT UINTN
*Size OPTIONAL
39 *Name
= (VOID
*) (Pack
+ 1);
43 *Guid
= (EFI_GUID
*)(UINTN
)&Pack
->VariableGuid
;
48 *Id
= Pack
->VariableId
;
52 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
56 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
61 Finds a count of the variables/maps in the List.
63 @param List List of variables
65 @return The number of map count.
69 EfiLibHiiVariablePackListGetMapCnt (
70 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
103 while (NULL
!= List
) {
104 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
108 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
109 List
= List
->NextVariablePack
;
114 Finds a variable form List given
115 the order number as appears in the List.
117 @param Idx The index of the variable/map to retrieve
118 @param List List of variables
119 @param Name Name of the variable/map
120 @param Guid GUID of the variable/map
121 @param Id Id of the variable/map
122 @param Var Pointer to the variable/map
123 @param Size Size of the variable/map in bytes
125 @return EFI_SUCCESS Variable is found, OUT parameters are valid
126 @return EFI_NOT_FOUND Variable is not found, OUT parameters are not valid
130 EfiLibHiiVariablePackListGetMapByIdx (
132 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
133 OUT CHAR16
**Name
, OPTIONAL
134 OUT EFI_GUID
**Guid
, OPTIONAL
135 OUT UINT16
*Id
, OPTIONAL
146 while (NULL
!= List
) {
147 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
168 List
= List
->NextVariablePack
;
171 // If here, the map is not found
173 return EFI_NOT_FOUND
;
177 Finds a variable form List given the
178 order number as appears in the List.
180 @param Id The ID of the variable/map to retrieve
181 @param List List of variables
182 @param Name Name of the variable/map
183 @param Guid GUID of the variable/map
184 @param Var Pointer to the variable/map
185 @param Size Size of the variable/map in bytes
187 @retval EFI_SUCCESS Variable is found, OUT parameters are valid
188 @retval EFI_NOT_FOUND Variable is not found, OUT parameters are not valid
192 EfiLibHiiVariablePackListGetMapById (
194 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
195 OUT CHAR16
**Name
, OPTIONAL
196 OUT EFI_GUID
**Guid
, OPTIONAL
207 while (NULL
!= List
) {
208 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
223 List
= List
->NextVariablePack
;
226 // If here, the map is not found
228 return EFI_NOT_FOUND
;
232 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
234 @param List List of variables
235 @param Name Name of the variable/map to be found
236 @param Guid GUID of the variable/map to be found
237 @param Id Id of the variable/map to be found
238 @param Var Pointer to the variable/map found
239 @param Size Size of the variable/map in bytes found
241 @retval EFI_SUCCESS variable is found, OUT parameters are valid
242 @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.
286 Caller is responsible for releasing the buffer.
288 @retval EFI_SUCCESS The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
289 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.
290 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.
294 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 Name of the variable to retrieve.
346 @param Guid Guid of the variable to retrieve.
347 @param Size Parameters of the variable to retrieve.
348 @param Var Variable will be retrieved into this buffer.
349 Caller is responsible for providing storage of exactly Size size in bytes.
351 @retval EFI_SUCCESS The variable was overriden with NV variable of same Name/Guid/Size.
352 @retval EFI_INVALID_PARAMETER The name of the variable does not end with <Suffix>.
353 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.
354 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.
358 EfiLibHiiVariableOverrideIfSuffix (
369 StrLength
= StrLen (Name
);
370 StrLenSuffix
= StrLen (Suffix
);
371 if ((StrLength
<= StrLenSuffix
) || (0 != StrCmp (Suffix
, &Name
[StrLength
- StrLenSuffix
]))) {
373 // Not ending with <Suffix>.
375 return EFI_INVALID_PARAMETER
;
377 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
381 Overrrides the variable with NV data if found.
382 But it only does it if the NV contains the same variable with Name is appended with Suffix.
383 For example, if Suffix="MyOverride" and the Name="XyzSetup",
384 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
385 will be loaded from NV provided the variable exists and the GUID and Size matches.
387 @param Suffix Suffix the variable will be appended with.
388 @param Name Parameters of the Name variable to retrieve.
389 @param Guid Parameters of the Guid variable to retrieve.
390 @param Size Parameters of the Size variable to retrieve.
391 @param Var Variable will be retrieved into this buffer.
392 Caller is responsible for providing storage of exactly Size size in bytes.
394 @retval EFI_SUCCESS The variable was overriden with NV variable of same Name/Guid/Size.
395 @retval EFI_NOT_FOUND The variable of this Name/Guid was not found in the NV.
396 @retval EFI_LOAD_ERROR The variable in the NV was of different size, or NV API returned error.
400 EfiLibHiiVariableOverrideBySuffix (
409 CHAR16
*NameSuffixed
;
414 // enough to concatenate both strings.
416 NameLength
= StrLen (Name
);
417 SuffixLength
= StrLen (Suffix
);
418 NameSuffixed
= AllocateZeroPool ((NameLength
+ SuffixLength
+ 1) * sizeof (CHAR16
));
420 StrCpy (NameSuffixed
, Name
);
421 StrCat (NameSuffixed
, Suffix
);
423 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
424 FreePool (NameSuffixed
);