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 // Include common header file for this module.
23 #include "CommonHeader.h"
26 EfiLibHiiVariablePackGetMap (
27 IN EFI_HII_VARIABLE_PACK
*Pack
,
28 OUT CHAR16
**Name
, OPTIONAL
29 OUT EFI_GUID
**Guid
, OPTIONAL
30 OUT UINT16
*Id
, OPTIONAL
31 OUT VOID
**Var
, OPTIONAL
32 OUT UINTN
*Size OPTIONAL
38 Extracts a variable form a Pack.
42 Pack - List of variables
43 Name - Name of the variable/map
44 Guid - GUID of the variable/map
45 Var - Pointer to the variable/map
46 Size - Size of the variable/map in bytes
55 *Name
= (VOID
*) (Pack
+ 1);
59 *Guid
= (EFI_GUID
*)(UINTN
)&Pack
->VariableGuid
;
64 *Id
= Pack
->VariableId
;
68 *Var
= (VOID
*) ((CHAR8
*) (Pack
+ 1) + Pack
->VariableNameLength
);
72 *Size
= Pack
->Header
.Length
- sizeof (*Pack
) - Pack
->VariableNameLength
;
78 EfiLibHiiVariablePackListGetMapCnt (
79 IN EFI_HII_VARIABLE_PACK_LIST
*List
86 Finds a count of the variables/maps in the List.
90 List - List of variables
94 UINTN - The number of map count.
100 while (NULL
!= List
) {
102 List
= List
->NextVariablePack
;
109 EfiLibHiiVariablePackListForEachVar (
110 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
111 IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK
*Callback
117 Will iterate all variable/maps as appearing
118 in List and for each, it will call the Callback.
122 List - List of variables
123 Callback - Routine to be called for each iterated variable.
138 while (NULL
!= List
) {
139 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
143 Callback (MapName
, MapGuid
, MapId
, Map
, MapSize
);
144 List
= List
->NextVariablePack
;
150 EfiLibHiiVariablePackListGetMapByIdx (
152 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
153 OUT CHAR16
**Name
, OPTIONAL
154 OUT EFI_GUID
**Guid
, OPTIONAL
155 OUT UINT16
*Id
, OPTIONAL
164 Finds a variable form List given
165 the order number as appears in the List.
169 Idx - The index of the variable/map to retrieve
170 List - List of variables
171 Name - Name of the variable/map
172 Guid - GUID of the variable/map
173 Var - Pointer to the variable/map
174 Size - Size of the variable/map in bytes
178 EFI_SUCCESS - Variable is found, OUT parameters are valid
179 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
189 while (NULL
!= List
) {
190 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
207 return EFI_SUCCESS
; // Map found
209 List
= List
->NextVariablePack
;
212 // If here, the map is not found
214 return EFI_NOT_FOUND
;
219 EfiLibHiiVariablePackListGetMapById (
221 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
222 OUT CHAR16
**Name
, OPTIONAL
223 OUT EFI_GUID
**Guid
, OPTIONAL
232 Finds a variable form List given the
233 order number as appears in the List.
237 Id - The ID of the variable/map to retrieve
238 List - List of variables
239 Name - Name of the variable/map
240 Guid - GUID of the variable/map
241 Var - Pointer to the variable/map
242 Size - Size of the variable/map in bytes
246 EFI_SUCCESS - Variable is found, OUT parameters are valid
247 EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid
258 while (NULL
!= List
) {
259 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
274 List
= List
->NextVariablePack
;
277 // If here, the map is not found
279 return EFI_NOT_FOUND
;
284 EfiLibHiiVariablePackListGetMap (
285 IN EFI_HII_VARIABLE_PACK_LIST
*List
,
297 Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.
301 List - List of variables
302 Name - Name of the variable/map to be found
303 Guid - GUID of the variable/map to be found
304 Var - Pointer to the variable/map found
305 Size - Size of the variable/map in bytes found
309 EFI_SUCCESS - variable is found, OUT parameters are valid
310 EFI_NOT_FOUND - variable is not found, OUT parameters are not valid
321 while (NULL
!= List
) {
322 EfiLibHiiVariablePackGetMap (List
->VariablePack
, &MapName
, &MapGuid
, &MapId
, &Map
, &MapSize
);
323 if ((0 == StrCmp (Name
, MapName
)) && CompareGuid (Guid
, MapGuid
)) {
329 List
= List
->NextVariablePack
;
332 // If here, the map is not found
334 return EFI_NOT_FOUND
;
338 EfiLibHiiVariableRetrieveFromNv (
347 Finds out if a variable of specific Name/Guid/Size exists in NV.
348 If it does, it will retrieve it into the Var.
351 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
352 Var - Variable will be retrieved into buffer pointed by this pointer.
353 If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.
355 EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.
356 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
357 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
365 // Test for existence of the variable.
368 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, NULL
);
369 if (EFI_BUFFER_TOO_SMALL
!= Status
) {
370 ASSERT (EFI_SUCCESS
!= Status
);
371 return EFI_NOT_FOUND
;
373 if (SizeNv
!= Size
) {
375 // The variable is considered corrupt, as it has different size from expected.
377 return EFI_LOAD_ERROR
;
381 *Var
= AllocatePool (Size
);
382 ASSERT (NULL
!= *Var
);
386 // Final read into the Var
388 Status
= gRT
->GetVariable (Name
, Guid
, NULL
, &SizeNv
, *Var
);
390 // No tolerance for random failures. Such behavior is undetermined and not validated.
392 ASSERT_EFI_ERROR (Status
);
393 ASSERT (SizeNv
== Size
);
400 EfiLibHiiVariableOverrideIfSuffix (
410 Overrrides the variable with NV data if found.
411 But it only does it if the Name ends with specified Suffix.
412 For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",
413 the Suffix matches the end of Name, so the variable will be loaded from NV
414 provided the variable exists and the GUID and Size matches.
417 Suffix - Suffix the Name should end with.
418 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
419 Var - Variable will be retrieved into this buffer.
420 Caller is responsible for providing storage of exactly Size size in bytes.
422 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
423 EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.
424 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
425 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
432 StrLength
= StrLen (Name
);
433 StrLenSuffix
= StrLen (Suffix
);
434 if ((StrLength
<= StrLenSuffix
) || (0 != StrCmp (Suffix
, &Name
[StrLength
- StrLenSuffix
]))) {
436 // Not ending with <Suffix>.
438 return EFI_INVALID_PARAMETER
;
440 return EfiLibHiiVariableRetrieveFromNv (Name
, Guid
, Size
, &Var
);
444 EfiLibHiiVariableOverrideBySuffix (
454 Overrrides the variable with NV data if found.
455 But it only does it if the NV contains the same variable with Name is appended with Suffix.
456 For example, if Suffix="MyOverride" and the Name="XyzSetup",
457 the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"
458 will be loaded from NV provided the variable exists and the GUID and Size matches.
461 Suffix - Suffix the variable will be appended with.
462 Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.
463 Var - Variable will be retrieved into this buffer.
464 Caller is responsible for providing storage of exactly Size size in bytes.
467 EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.
468 EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.
469 EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.
474 CHAR16
*NameSuffixed
;
479 // enough to concatenate both strings.
481 NameLength
= StrLen (Name
);
482 SuffixLength
= StrLen (Suffix
);
483 NameSuffixed
= AllocateZeroPool ((NameLength
+ SuffixLength
+ 1) * sizeof (CHAR16
));
485 StrCpy (NameSuffixed
, Name
);
486 StrCat (NameSuffixed
, Suffix
);
488 Status
= EfiLibHiiVariableRetrieveFromNv (NameSuffixed
, Guid
, Size
, &Var
);
489 gBS
->FreePool (NameSuffixed
);