3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 Variable/Map manipulations routines
21 EfiLibHiiVariablePackGetMap (
22 IN EFI_HII_VARIABLE_PACK
*Pack
,
23 OUT CHAR16
**Name
, OPTIONAL
24 OUT EFI_GUID
**Guid
, OPTIONAL
25 OUT UINT16
*Id
, OPTIONAL
26 OUT VOID
**Var
, OPTIONAL
27 OUT UINTN
*Size OPTIONAL
33 Extracts a variable form a Pack.
37 Pack - List of variables
38 Name - Name of the variable/map
39 Guid - GUID of the variable/map
40 Var - Pointer to the variable/map
41 Size - Size of the variable/map in bytes
50 *Name
= (VOID
*) (Pack
+ 1);
54 *Guid
= (EFI_GUID
*)(UINTN
)&Pack
->VariableGuid
;
59 *Id
= Pack
->VariableId
;
63 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
67 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
73 EfiLibHiiVariablePackListGetMapCnt (
74 IN EFI_HII_VARIABLE_PACK_LIST
*List
81 Finds a count of the variables/maps in the List.
85 List - List of variables
89 UINTN - The number of map count.
95 while (NULL
!= List
) {
97 List
= List
->NextVariablePack
;
104 EfiLibHiiVariablePackListForEachVar (
105 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
106 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK
*Callback
112 Will iterate all variable/maps as appearing
113 in List and for each, it will call the Callback.
117 List - List of variables
118 Callback - Routine to be called for each iterated variable.
133 while (NULL
!= List
) {
134 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
138 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
139 List
= List
->NextVariablePack
;
145 EfiLibHiiVariablePackListGetMapByIdx (
147 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
148 OUT CHAR16
**Name
, OPTIONAL
149 OUT EFI_GUID
**Guid
, OPTIONAL
150 OUT UINT16
*Id
, OPTIONAL
159 Finds a variable form List given
160 the order number as appears in the List.
164 Idx - The index of the variable/map to retrieve
165 List - List of variables
166 Name - Name of the variable/map
167 Guid - GUID of the variable/map
168 Var - Pointer to the variable/map
169 Size - Size of the variable/map in bytes
173 EFI_SUCCESS - Variable is found, OUT parameters are valid
174 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
184 while (NULL
!= List
) {
185 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
202 return EFI_SUCCESS
; // Map found
204 List
= List
->NextVariablePack
;
207 // If here, the map is not found
209 return EFI_NOT_FOUND
;
214 EfiLibHiiVariablePackListGetMapById (
216 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
217 OUT CHAR16
**Name
, OPTIONAL
218 OUT EFI_GUID
**Guid
, OPTIONAL
227 Finds a variable form List given the
228 order number as appears in the List.
232 Id - The ID of the variable/map to retrieve
233 List - List of variables
234 Name - Name of the variable/map
235 Guid - GUID of the variable/map
236 Var - Pointer to the variable/map
237 Size - Size of the variable/map in bytes
241 EFI_SUCCESS - Variable is found, OUT parameters are valid
242 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
253 while (NULL
!= List
) {
254 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
269 List
= List
->NextVariablePack
;
272 // If here, the map is not found
274 return EFI_NOT_FOUND
;
279 EfiLibHiiVariablePackListGetMap (
280 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
292 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
296 List - List of variables
297 Name - Name of the variable/map to be found
298 Guid - GUID of the variable/map to be found
299 Var - Pointer to the variable/map found
300 Size - Size of the variable/map in bytes found
304 EFI_SUCCESS - variable is found, OUT parameters are valid
305 EFI_NOT_FOUND - variable is not found, OUT parameters are not valid
316 while (NULL
!= List
) {
317 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
318 if ((0 == StrCmp (Name
, MapName
)) && CompareGuid (Guid
, MapGuid
)) {
324 List
= List
->NextVariablePack
;
327 // If here, the map is not found
329 return EFI_NOT_FOUND
;
333 EfiLibHiiVariableRetrieveFromNv (
342 Finds out if a variable of specific Name/Guid/Size exists in NV.
343 If it does, it will retrieve it into the Var.
346 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
347 Var - Variable will be retrieved into buffer pointed by this pointer.
348 If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.
350 EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
351 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
352 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
360 // Test for existence of the variable.
363 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, NULL
);
364 if (EFI_BUFFER_TOO_SMALL
!= Status
) {
365 ASSERT (EFI_SUCCESS
!= Status
);
366 return EFI_NOT_FOUND
;
368 if (SizeNv
!= Size
) {
370 // The variable is considered corrupt, as it has different size from expected.
372 return EFI_LOAD_ERROR
;
376 *Var
= AllocatePool (Size
);
377 ASSERT (NULL
!= *Var
);
381 // Final read into the Var
383 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, *Var
);
385 // No tolerance for random failures. Such behavior is undetermined and not validated.
387 ASSERT_EFI_ERROR (Status
);
388 ASSERT (SizeNv
== Size
);
395 EfiLibHiiVariableOverrideIfSuffix (
405 Overrrides the variable with NV data if found.
406 But it only does it if the Name ends with specified Suffix.
407 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",
408 the Suffix matches the end of Name, so the variable will be loaded from NV
409 provided the variable exists and the GUID and Size matches.
412 Suffix - Suffix the Name should end with.
413 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
414 Var - Variable will be retrieved into this buffer.
415 Caller is responsible for providing storage of exactly Size size in bytes.
417 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
418 EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.
419 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
420 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
427 StrLength
= StrLen (Name
);
428 StrLenSuffix
= StrLen (Suffix
);
429 if ((StrLength
<= StrLenSuffix
) || (0 != StrCmp (Suffix
, &Name
[StrLength
- StrLenSuffix
]))) {
431 // Not ending with <Suffix>.
433 return EFI_INVALID_PARAMETER
;
435 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
439 EfiLibHiiVariableOverrideBySuffix (
449 Overrrides the variable with NV data if found.
450 But it only does it if the NV contains the same variable with Name is appended with Suffix.
451 For example, if Suffix="MyOverride" and the Name="XyzSetup",
452 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
453 will be loaded from NV provided the variable exists and the GUID and Size matches.
456 Suffix - Suffix the variable will be appended with.
457 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
458 Var - Variable will be retrieved into this buffer.
459 Caller is responsible for providing storage of exactly Size size in bytes.
462 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
463 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
464 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
469 CHAR16
*NameSuffixed
;
472 // enough to concatenate both strings.
474 NameSuffixed
= AllocateZeroPool ((StrLen (Name
) + StrLen (Suffix
) + 1) * sizeof (CHAR16
));
476 StrCpy (NameSuffixed
, Name
);
477 StrCat (NameSuffixed
, Suffix
);
479 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
480 gBS
->FreePool (NameSuffixed
);