+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
- IfrVariable.c\r
-\r
-Abstract:\r
- Variable/Map manipulations routines\r
-\r
---*/\r
-\r
-#include "IfrLibrary.h"\r
-\r
-VOID\r
-EfiLibHiiVariablePackGetMap (\r
- IN EFI_HII_VARIABLE_PACK *Pack, \r
- OUT CHAR16 **Name, OPTIONAL\r
- OUT EFI_GUID **Guid, OPTIONAL\r
- OUT UINT16 *Id, OPTIONAL\r
- OUT VOID **Var, OPTIONAL\r
- OUT UINTN *Size OPTIONAL\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Extracts a variable form a Pack.\r
-\r
-Arguments:\r
-\r
- Pack - List of variables\r
- Name - Name of the variable/map\r
- Guid - GUID of the variable/map\r
- Var - Pointer to the variable/map\r
- Size - Size of the variable/map in bytes\r
-\r
-Returns: \r
-\r
- VOID\r
- \r
---*/\r
-{\r
- if (NULL != Name) {\r
- *Name = (VOID *) (Pack + 1);\r
- }\r
- \r
- if (NULL != Guid) { \r
- *Guid = &Pack->VariableGuid;\r
- }\r
- \r
- \r
- if (NULL != Id) {\r
- *Id = Pack->VariableId;\r
- }\r
- \r
- if (NULL != Var) {\r
- *Var = (VOID *) ((CHAR8 *) (Pack + 1) + Pack->VariableNameLength);\r
- }\r
- \r
- if (NULL != Size) {\r
- *Size = Pack->Header.Length - sizeof (*Pack) - Pack->VariableNameLength;\r
- }\r
-}\r
-\r
-\r
-UINTN\r
-EfiLibHiiVariablePackListGetMapCnt (\r
- IN EFI_HII_VARIABLE_PACK_LIST *List\r
- )\r
- \r
-/*++\r
-\r
-Routine Description:\r
-\r
- Finds a count of the variables/maps in the List.\r
-\r
-Arguments:\r
-\r
- List - List of variables\r
-\r
-Returns: \r
-\r
- UINTN - The number of map count.\r
-\r
---*/\r
-\r
-{\r
- UINTN Cnt = 0;\r
- while (NULL != List) {\r
- Cnt++;\r
- List = List->NextVariablePack;\r
- }\r
- return Cnt;\r
-}\r
-\r
-\r
-VOID\r
-EfiLibHiiVariablePackListForEachVar (\r
- IN EFI_HII_VARIABLE_PACK_LIST *List,\r
- IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK *Callback\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Will iterate all variable/maps as appearing \r
- in List and for each, it will call the Callback.\r
-\r
-Arguments:\r
-\r
- List - List of variables\r
- Callback - Routine to be called for each iterated variable.\r
-\r
-Returns: \r
-\r
- VOID\r
- \r
---*/\r
-\r
-{\r
- CHAR16 *MapName;\r
- EFI_GUID *MapGuid;\r
- UINT16 MapId;\r
- VOID *Map;\r
- UINTN MapSize;\r
-\r
- while (NULL != List) {\r
- EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
- //\r
- // call the callback\r
- //\r
- Callback (MapName, MapGuid, MapId, Map, MapSize); \r
- List = List->NextVariablePack;\r
- }\r
-}\r
-\r
-\r
-EFI_STATUS\r
-EfiLibHiiVariablePackListGetMapByIdx (\r
- IN UINTN Idx, \r
- IN EFI_HII_VARIABLE_PACK_LIST *List, \r
- OUT CHAR16 **Name, OPTIONAL\r
- OUT EFI_GUID **Guid, OPTIONAL\r
- OUT UINT16 *Id, OPTIONAL\r
- OUT VOID **Var,\r
- OUT UINTN *Size\r
- )\r
-\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Finds a variable form List given \r
- the order number as appears in the List.\r
-\r
-Arguments:\r
-\r
- Idx - The index of the variable/map to retrieve\r
- List - List of variables\r
- Name - Name of the variable/map\r
- Guid - GUID of the variable/map\r
- Var - Pointer to the variable/map\r
- Size - Size of the variable/map in bytes\r
-\r
-Returns:\r
-\r
- EFI_SUCCESS - Variable is found, OUT parameters are valid\r
- EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid\r
-\r
---*/\r
-{\r
- CHAR16 *MapName;\r
- EFI_GUID *MapGuid;\r
- UINT16 MapId;\r
- VOID *Map;\r
- UINTN MapSize;\r
-\r
- while (NULL != List) {\r
- EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
- if (0 == Idx--) {\r
- *Var = Map;\r
- *Size = MapSize;\r
-\r
- if (NULL != Name) {\r
- *Name = MapName;\r
- }\r
-\r
- if (NULL != Guid) {\r
- *Guid = MapGuid;\r
- }\r
- \r
- if (NULL != Id) {\r
- *Id = MapId;\r
- }\r
- \r
- return EFI_SUCCESS; // Map found\r
- }\r
- List = List->NextVariablePack;\r
- }\r
- //\r
- // If here, the map is not found\r
- //\r
- return EFI_NOT_FOUND; \r
-}\r
-\r
-\r
-EFI_STATUS\r
-EfiLibHiiVariablePackListGetMapById (\r
- IN UINT16 Id, \r
- IN EFI_HII_VARIABLE_PACK_LIST *List,\r
- OUT CHAR16 **Name, OPTIONAL\r
- OUT EFI_GUID **Guid, OPTIONAL\r
- OUT VOID **Var,\r
- OUT UINTN *Size\r
- )\r
- \r
-/*++\r
-\r
-Routine Description:\r
-\r
- Finds a variable form List given the \r
- order number as appears in the List.\r
-\r
-Arguments:\r
-\r
- Id - The ID of the variable/map to retrieve\r
- List - List of variables\r
- Name - Name of the variable/map\r
- Guid - GUID of the variable/map\r
- Var - Pointer to the variable/map\r
- Size - Size of the variable/map in bytes\r
-\r
-Returns:\r
-\r
- EFI_SUCCESS - Variable is found, OUT parameters are valid\r
- EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid\r
-\r
---*/\r
-\r
-{ \r
- CHAR16 *MapName;\r
- EFI_GUID *MapGuid;\r
- UINT16 MapId;\r
- VOID *Map;\r
- UINTN MapSize;\r
-\r
- while (NULL != List) {\r
- EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
- if (MapId == Id) {\r
- *Var = Map;\r
- *Size = MapSize;\r
- if (NULL != Name) {\r
- *Name = MapName;\r
- }\r
- if (NULL != Guid) {\r
- *Guid = MapGuid;\r
- }\r
- //\r
- // Map found\r
- //\r
- return EFI_SUCCESS; \r
- }\r
- List = List->NextVariablePack;\r
- }\r
- //\r
- // If here, the map is not found\r
- //\r
- return EFI_NOT_FOUND; \r
-}\r
-\r
-\r
-EFI_STATUS\r
-EfiLibHiiVariablePackListGetMap (\r
- IN EFI_HII_VARIABLE_PACK_LIST *List,\r
- IN CHAR16 *Name,\r
- IN EFI_GUID *Guid,\r
- OUT UINT16 *Id,\r
- OUT VOID **Var, \r
- OUT UINTN *Size\r
- )\r
-\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.\r
-\r
-Arguments:\r
-\r
- List - List of variables\r
- Name - Name of the variable/map to be found\r
- Guid - GUID of the variable/map to be found\r
- Var - Pointer to the variable/map found\r
- Size - Size of the variable/map in bytes found\r
-\r
-Returns:\r
-\r
- EFI_SUCCESS - variable is found, OUT parameters are valid\r
- EFI_NOT_FOUND - variable is not found, OUT parameters are not valid\r
-\r
---*/\r
-\r
-{ \r
- VOID *Map;\r
- UINTN MapSize;\r
- UINT16 MapId;\r
- CHAR16 *MapName;\r
- EFI_GUID *MapGuid;\r
-\r
- while (NULL != List) {\r
- EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r
- if ((0 == EfiStrCmp (Name, MapName)) && EfiCompareGuid (Guid, MapGuid)) {\r
- *Id = MapId;\r
- *Var = Map;\r
- *Size = MapSize;\r
- return EFI_SUCCESS;\r
- }\r
- List = List->NextVariablePack;\r
- }\r
- //\r
- // If here, the map is not found\r
- //\r
- return EFI_NOT_FOUND;\r
-}\r
-\r
-EFI_STATUS\r
-EfiLibHiiVariableRetrieveFromNv (\r
- IN CHAR16 *Name,\r
- IN EFI_GUID *Guid,\r
- IN UINTN Size,\r
- OUT VOID **Var\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Finds out if a variable of specific Name/Guid/Size exists in NV. \r
- If it does, it will retrieve it into the Var. \r
-\r
-Arguments:\r
- Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r
- Var - Variable will be retrieved into buffer pointed by this pointer.\r
- If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.\r
-Returns:\r
- EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.\r
- EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r
- EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\r
- UINTN SizeNv;\r
-\r
- //\r
- // Test for existence of the variable.\r
- //\r
- SizeNv = 0;\r
- Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, NULL);\r
- if (EFI_BUFFER_TOO_SMALL != Status) {\r
- ASSERT (EFI_SUCCESS != Status);\r
- return EFI_NOT_FOUND;\r
- }\r
- if (SizeNv != Size) {\r
- //\r
- // The variable is considered corrupt, as it has different size from expected.\r
- //\r
- return EFI_LOAD_ERROR; \r
- }\r
-\r
- if (NULL == *Var) {\r
- *Var = EfiLibAllocatePool (Size);\r
- ASSERT (NULL != *Var);\r
- }\r
- SizeNv = Size;\r
- //\r
- // Final read into the Var\r
- //\r
- Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, *Var); \r
- //\r
- // No tolerance for random failures. Such behavior is undetermined and not validated.\r
- //\r
- ASSERT_EFI_ERROR (Status); \r
- ASSERT (SizeNv == Size);\r
- return EFI_SUCCESS;\r
-}\r
-\r
-\r
-\r
-EFI_STATUS\r
-EfiLibHiiVariableOverrideIfSuffix (\r
- IN CHAR16 *Suffix,\r
- IN CHAR16 *Name,\r
- IN EFI_GUID *Guid,\r
- IN UINTN Size,\r
- OUT VOID *Var\r
- ) \r
-/*++\r
-\r
-Routine Description:\r
- Overrrides the variable with NV data if found.\r
- But it only does it if the Name ends with specified Suffix.\r
- For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",\r
- the Suffix matches the end of Name, so the variable will be loaded from NV\r
- provided the variable exists and the GUID and Size matches.\r
-\r
-Arguments:\r
- Suffix - Suffix the Name should end with.\r
- Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r
- Var - Variable will be retrieved into this buffer.\r
- Caller is responsible for providing storage of exactly Size size in bytes.\r
-Returns:\r
- EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.\r
- EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.\r
- EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r
- EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r
-\r
---*/\r
-{\r
- UINTN StrLen;\r
- UINTN StrLenSuffix;\r
-\r
- StrLen = EfiStrLen (Name);\r
- StrLenSuffix = EfiStrLen (Suffix);\r
- if ((StrLen <= StrLenSuffix) || (0 != EfiStrCmp (Suffix, &Name[StrLen - StrLenSuffix]))) {\r
- //\r
- // Not ending with <Suffix>.\r
- //\r
- return EFI_INVALID_PARAMETER; \r
- }\r
- return EfiLibHiiVariableRetrieveFromNv (Name, Guid, Size, &Var);\r
-}\r
-\r
-\r
-\r
-EFI_STATUS\r
-EfiLibHiiVariableOverrideBySuffix (\r
- IN CHAR16 *Suffix,\r
- IN CHAR16 *Name,\r
- IN EFI_GUID *Guid,\r
- IN UINTN Size,\r
- OUT VOID *Var\r
- ) \r
-/*++\r
-\r
-Routine Description:\r
- Overrrides the variable with NV data if found.\r
- But it only does it if the NV contains the same variable with Name is appended with Suffix. \r
- For example, if Suffix="MyOverride" and the Name="XyzSetup",\r
- the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"\r
- will be loaded from NV provided the variable exists and the GUID and Size matches.\r
-\r
-Arguments:\r
- Suffix - Suffix the variable will be appended with.\r
- Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r
- Var - Variable will be retrieved into this buffer.\r
- Caller is responsible for providing storage of exactly Size size in bytes.\r
-\r
-Returns:\r
- EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.\r
- EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r
- EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\r
- CHAR16 *NameSuffixed;\r
- UINTN NameLength;\r
- UINTN SuffixLength;\r
-\r
- //\r
- // enough to concatenate both strings.\r
- //\r
- NameLength = EfiStrLen (Name);\r
- SuffixLength = EfiStrLen (Suffix);\r
- NameSuffixed = EfiLibAllocateZeroPool ((NameLength + SuffixLength + 1) * sizeof (CHAR16)); \r
- \r
- EfiStrCpy (NameSuffixed, Name);\r
- EfiStrCat (NameSuffixed, Suffix);\r
- \r
- Status = EfiLibHiiVariableRetrieveFromNv (NameSuffixed, Guid, Size, &Var);\r
- gBS->FreePool (NameSuffixed);\r
- \r
- return Status;\r
-}\r
-\r