3 Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>
4 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
20 #include "IfrLibrary.h"
23 EfiLibHiiVariablePackGetMap (
24 IN EFI_HII_VARIABLE_PACK
*Pack
,
25 OUT CHAR16
**Name
, OPTIONAL
26 OUT EFI_GUID
**Guid
, OPTIONAL
27 OUT UINT16
*Id
, OPTIONAL
28 OUT VOID
**Var
, OPTIONAL
29 OUT UINTN
*Size OPTIONAL
35 Extracts a variable form a Pack.
39 Pack - List of variables
40 Name - Name of the variable/map
41 Guid - GUID of the variable/map
42 Var - Pointer to the variable/map
43 Size - Size of the variable/map in bytes
52 *Name
= (VOID
*) (Pack
+ 1);
56 *Guid
= &Pack
->VariableGuid
;
61 *Id
= Pack
->VariableId
;
65 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
69 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
75 EfiLibHiiVariablePackListGetMapCnt (
76 IN EFI_HII_VARIABLE_PACK_LIST
*List
83 Finds a count of the variables/maps in the List.
87 List - List of variables
91 UINTN - The number of map count.
97 while (NULL
!= List
) {
99 List
= List
->NextVariablePack
;
106 EfiLibHiiVariablePackListForEachVar (
107 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
108 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK
*Callback
114 Will iterate all variable/maps as appearing
115 in List and for each, it will call the Callback.
119 List - List of variables
120 Callback - Routine to be called for each iterated variable.
135 while (NULL
!= List
) {
136 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
140 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
141 List
= List
->NextVariablePack
;
147 EfiLibHiiVariablePackListGetMapByIdx (
149 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
150 OUT CHAR16
**Name
, OPTIONAL
151 OUT EFI_GUID
**Guid
, OPTIONAL
152 OUT UINT16
*Id
, OPTIONAL
161 Finds a variable form List given
162 the order number as appears in the List.
166 Idx - The index of the variable/map to retrieve
167 List - List of variables
168 Name - Name of the variable/map
169 Guid - GUID of the variable/map
170 Var - Pointer to the variable/map
171 Size - Size of the variable/map in bytes
175 EFI_SUCCESS - Variable is found, OUT parameters are valid
176 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
186 while (NULL
!= List
) {
187 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
204 return EFI_SUCCESS
; // Map found
206 List
= List
->NextVariablePack
;
209 // If here, the map is not found
211 return EFI_NOT_FOUND
;
216 EfiLibHiiVariablePackListGetMapById (
218 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
219 OUT CHAR16
**Name
, OPTIONAL
220 OUT EFI_GUID
**Guid
, OPTIONAL
229 Finds a variable form List given the
230 order number as appears in the List.
234 Id - The ID of the variable/map to retrieve
235 List - List of variables
236 Name - Name of the variable/map
237 Guid - GUID of the variable/map
238 Var - Pointer to the variable/map
239 Size - Size of the variable/map in bytes
243 EFI_SUCCESS - Variable is found, OUT parameters are valid
244 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
255 while (NULL
!= List
) {
256 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
271 List
= List
->NextVariablePack
;
274 // If here, the map is not found
276 return EFI_NOT_FOUND
;
281 EfiLibHiiVariablePackListGetMap (
282 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
294 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
298 List - List of variables
299 Name - Name of the variable/map to be found
300 Guid - GUID of the variable/map to be found
301 Var - Pointer to the variable/map found
302 Size - Size of the variable/map in bytes found
306 EFI_SUCCESS - variable is found, OUT parameters are valid
307 EFI_NOT_FOUND - variable is not found, OUT parameters are not valid
318 while (NULL
!= List
) {
319 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
320 if ((0 == EfiStrCmp (Name
, MapName
)) && EfiCompareGuid (Guid
, MapGuid
)) {
326 List
= List
->NextVariablePack
;
329 // If here, the map is not found
331 return EFI_NOT_FOUND
;
335 EfiLibHiiVariableRetrieveFromNv (
344 Finds out if a variable of specific Name/Guid/Size exists in NV.
345 If it does, it will retrieve it into the Var.
348 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
349 Var - Variable will be retrieved into buffer pointed by this pointer.
350 If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.
352 EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
353 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
354 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
362 // Test for existence of the variable.
365 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, NULL
);
366 if (EFI_BUFFER_TOO_SMALL
!= Status
) {
367 ASSERT (EFI_SUCCESS
!= Status
);
368 return EFI_NOT_FOUND
;
370 if (SizeNv
!= Size
) {
372 // The variable is considered corrupt, as it has different size from expected.
374 return EFI_LOAD_ERROR
;
378 *Var
= EfiLibAllocatePool (Size
);
379 ASSERT (NULL
!= *Var
);
383 // Final read into the Var
385 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, *Var
);
387 // No tolerance for random failures. Such behavior is undetermined and not validated.
389 ASSERT_EFI_ERROR (Status
);
390 ASSERT (SizeNv
== Size
);
397 EfiLibHiiVariableOverrideIfSuffix (
407 Overrrides the variable with NV data if found.
408 But it only does it if the Name ends with specified Suffix.
409 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",
410 the Suffix matches the end of Name, so the variable will be loaded from NV
411 provided the variable exists and the GUID and Size matches.
414 Suffix - Suffix the Name should end with.
415 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
416 Var - Variable will be retrieved into this buffer.
417 Caller is responsible for providing storage of exactly Size size in bytes.
419 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
420 EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.
421 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
422 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
429 StrLen
= EfiStrLen (Name
);
430 StrLenSuffix
= EfiStrLen (Suffix
);
431 if ((StrLen
<= StrLenSuffix
) || (0 != EfiStrCmp (Suffix
, &Name
[StrLen
- StrLenSuffix
]))) {
433 // Not ending with <Suffix>.
435 return EFI_INVALID_PARAMETER
;
437 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
443 EfiLibHiiVariableOverrideBySuffix (
453 Overrrides the variable with NV data if found.
454 But it only does it if the NV contains the same variable with Name is appended with Suffix.
455 For example, if Suffix="MyOverride" and the Name="XyzSetup",
456 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
457 will be loaded from NV provided the variable exists and the GUID and Size matches.
460 Suffix - Suffix the variable will be appended with.
461 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
462 Var - Variable will be retrieved into this buffer.
463 Caller is responsible for providing storage of exactly Size size in bytes.
466 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
467 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
468 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
473 CHAR16
*NameSuffixed
;
478 // enough to concatenate both strings.
480 NameLength
= EfiStrLen (Name
);
481 SuffixLength
= EfiStrLen (Suffix
);
482 NameSuffixed
= EfiLibAllocateZeroPool ((NameLength
+ SuffixLength
+ 1) * sizeof (CHAR16
));
484 EfiStrCpy (NameSuffixed
, Name
);
485 EfiStrCat (NameSuffixed
, Suffix
);
487 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
488 gBS
->FreePool (NameSuffixed
);