+++ /dev/null
-/** @file\r
- Routines used to operate the Ip4 configure variable.\r
-\r
-Copyright (c) 2006 - 2012, 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<BR>\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
-**/\r
-\r
-#include "Ip4Config.h"\r
-#include "NicIp4Variable.h"\r
-\r
-BOOLEAN mIp4ConfigVariableReclaimed = FALSE;\r
-\r
-/**\r
- Check whether the configure parameter is valid.\r
-\r
- @param NicConfig The configure parameter to check\r
-\r
- @return TRUE if the parameter is valid for the interface, otherwise FALSE.\r
-\r
-**/\r
-BOOLEAN\r
-Ip4ConfigIsValid (\r
- IN NIC_IP4_CONFIG_INFO *NicConfig\r
- )\r
-{\r
- EFI_IP4_IPCONFIG_DATA *IpConfig;\r
- IP4_ADDR Station;\r
- IP4_ADDR Netmask;\r
- IP4_ADDR Gateway;\r
- UINT32 Index;\r
-\r
- IpConfig = &NicConfig->Ip4Info;\r
-\r
- if (NicConfig->Source == IP4_CONFIG_SOURCE_STATIC) {\r
- //\r
- // Validate that the addresses are unicast and mask\r
- // is properly formated\r
- //\r
- Station = EFI_NTOHL (IpConfig->StationAddress);\r
- Netmask = EFI_NTOHL (IpConfig->SubnetMask);\r
-\r
- if ((Netmask == 0) || !IP4_IS_VALID_NETMASK (Netmask) ||\r
- (Station == 0) || !NetIp4IsUnicast (Station, Netmask)) {\r
- return FALSE;\r
- }\r
-\r
- //\r
- // Validate that the next hops are on the connected network\r
- // or that is a direct route (Gateway == 0).\r
- //\r
- for (Index = 0; Index < IpConfig->RouteTableSize; Index++) {\r
- Gateway = EFI_NTOHL (IpConfig->RouteTable[Index].GatewayAddress);\r
-\r
- if ((Gateway != 0) && (!IP4_NET_EQUAL (Station, Gateway, Netmask) ||\r
- !NetIp4IsUnicast (Gateway, Netmask))) {\r
- return FALSE;\r
- }\r
- }\r
-\r
- return TRUE;\r
- }\r
-\r
- //\r
- // return false if it is an unkown configure source. Valid\r
- // sources are static and dhcp.\r
- //\r
- return (BOOLEAN) (NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);\r
-}\r
-\r
-\r
-\r
-/**\r
- Read the ip4 configure variable from the EFI variable.\r
-\r
- @param Instance The IP4 CONFIG instance.\r
-\r
- @return The IP4 configure read if it is there and is valid, otherwise NULL.\r
-\r
-**/\r
-NIC_IP4_CONFIG_INFO *\r
-Ip4ConfigReadVariable (\r
- IN IP4_CONFIG_INSTANCE *Instance\r
- )\r
-{\r
- NIC_IP4_CONFIG_INFO *NicConfig;\r
-\r
- GetVariable2 (Instance->MacString, &gEfiNicIp4ConfigVariableGuid, (VOID**)&NicConfig, NULL);\r
- if (NicConfig != NULL) {\r
- Ip4ConfigFixRouteTablePointer (&NicConfig->Ip4Info);\r
- }\r
-\r
- return NicConfig;\r
-}\r
-\r
-/**\r
- Write the IP4 configure variable to the NVRAM. If Config\r
- is NULL, remove the variable.\r
-\r
- @param Instance The IP4 CONFIG instance.\r
- @param NicConfig The IP4 configure data to write.\r
-\r
- @retval EFI_SUCCESS The variable is written to the NVRam.\r
- @retval Others Failed to write the variable.\r
-\r
-**/\r
-EFI_STATUS\r
-Ip4ConfigWriteVariable (\r
- IN IP4_CONFIG_INSTANCE *Instance,\r
- IN NIC_IP4_CONFIG_INFO *NicConfig OPTIONAL\r
- )\r
-{\r
- EFI_STATUS Status;\r
-\r
- Status = gRT->SetVariable (\r
- Instance->MacString,\r
- &gEfiNicIp4ConfigVariableGuid,\r
- IP4_CONFIG_VARIABLE_ATTRIBUTES,\r
- (NicConfig == NULL) ? 0 : SIZEOF_NIC_IP4_CONFIG_INFO (NicConfig),\r
- NicConfig\r
- );\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
- Check whether a NIC exist in the platform given its MAC address.\r
-\r
- @param NicAddr The MAC address for the NIC to be checked.\r
-\r
- @retval TRUE The NIC exist in the platform.\r
- @retval FALSE The NIC doesn't exist in the platform.\r
-\r
-**/\r
-BOOLEAN\r
-Ip4ConfigIsNicExist (\r
- IN NIC_ADDR *NicAddr\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_HANDLE *HandleBuffer;\r
- UINTN NumberOfHandles;\r
- UINTN Index;\r
- BOOLEAN Found;\r
- UINTN AddrSize;\r
- EFI_MAC_ADDRESS MacAddr;\r
-\r
- //\r
- // Locate Service Binding handles.\r
- //\r
- Status = gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiManagedNetworkServiceBindingProtocolGuid,\r
- NULL,\r
- &NumberOfHandles,\r
- &HandleBuffer\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return FALSE;\r
- }\r
-\r
- Found = FALSE;\r
- for (Index = 0; Index < NumberOfHandles; Index++) {\r
- //\r
- // Get MAC address.\r
- //\r
- AddrSize = 0;\r
- Status = NetLibGetMacAddress (HandleBuffer[Index], &MacAddr, &AddrSize);\r
- if (EFI_ERROR (Status)) {\r
- Found = FALSE;\r
- goto Exit;\r
- }\r
-\r
- if ((NicAddr->Len == AddrSize) && (CompareMem (NicAddr->MacAddr.Addr, MacAddr.Addr, AddrSize) == 0)) {\r
- Found = TRUE;\r
- goto Exit;\r
- }\r
- }\r
-\r
-Exit:\r
- FreePool (HandleBuffer);\r
- return Found;\r
-}\r
-\r
-/**\r
- Reclaim Ip4Config Variables for NIC which has been removed from the platform.\r
-\r
-**/\r
-VOID\r
-Ip4ConfigReclaimVariable (\r
- VOID\r
- )\r
-{\r
- EFI_STATUS Status;\r
- UINTN VariableNameSize;\r
- CHAR16 *VariableName;\r
- CHAR16 *CurrentVariableName;\r
- EFI_GUID VendorGuid;\r
- UINTN VariableNameBufferSize;\r
- NIC_IP4_CONFIG_INFO *NicConfig;\r
-\r
- //\r
- // Check whether we need perform reclaim.\r
- //\r
- if (mIp4ConfigVariableReclaimed) {\r
- return;\r
- }\r
- mIp4ConfigVariableReclaimed = TRUE;\r
-\r
- //\r
- // Get all Ip4Config Variable.\r
- //\r
- VariableNameSize = sizeof (CHAR16);\r
- VariableName = AllocateZeroPool (VariableNameSize);\r
- VariableNameBufferSize = VariableNameSize;\r
-\r
- while (TRUE) {\r
- Status = gRT->GetNextVariableName (\r
- &VariableNameSize,\r
- VariableName,\r
- &VendorGuid\r
- );\r
-\r
-Check:\r
- if (Status == EFI_BUFFER_TOO_SMALL) {\r
- VariableName = ReallocatePool (VariableNameBufferSize, VariableNameSize, VariableName);\r
- VariableNameBufferSize = VariableNameSize;\r
- //\r
- // Try again using the new buffer.\r
- //\r
- Status = gRT->GetNextVariableName (\r
- &VariableNameSize,\r
- VariableName,\r
- &VendorGuid\r
- );\r
- }\r
-\r
- if (EFI_ERROR (Status)) {\r
- //\r
- // No more variable available, finish search.\r
- //\r
- break;\r
- }\r
-\r
- //\r
- // Check variable GUID.\r
- //\r
- if (!CompareGuid (&VendorGuid, &gEfiNicIp4ConfigVariableGuid)) {\r
- continue;\r
- }\r
-\r
- GetVariable2 (VariableName, &gEfiNicIp4ConfigVariableGuid, (VOID**)&NicConfig, NULL);\r
- if (NicConfig == NULL) {\r
- break;\r
- }\r
-\r
- if (!Ip4ConfigIsNicExist (&NicConfig->NicAddr)) {\r
- //\r
- // No NIC found for this Ip4Config variable, remove it.\r
- // Since we are in loop of GetNextVariableName(), we need move on to next\r
- // Variable first and then delete current Variable.\r
- //\r
- CurrentVariableName = AllocateCopyPool (VariableNameSize, VariableName);\r
- Status = gRT->GetNextVariableName (\r
- &VariableNameSize,\r
- VariableName,\r
- &VendorGuid\r
- );\r
-\r
- gRT->SetVariable (\r
- CurrentVariableName,\r
- &gEfiNicIp4ConfigVariableGuid,\r
- IP4_CONFIG_VARIABLE_ATTRIBUTES,\r
- 0,\r
- NULL\r
- );\r
- FreePool (CurrentVariableName);\r
-\r
- //\r
- // We already get next variable, go to check it.\r
- //\r
- goto Check;\r
- }\r
- }\r
-\r
- FreePool (VariableName);\r
-}\r
-\r
-/**\r
- Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure.\r
-\r
- The pointer is set to be immediately follow the ConfigData if there're entries\r
- in the RouteTable. Otherwise it is set to NULL.\r
-\r
- @param ConfigData The IP4 IP configure data.\r
-\r
-**/\r
-VOID\r
-Ip4ConfigFixRouteTablePointer (\r
- IN OUT EFI_IP4_IPCONFIG_DATA *ConfigData\r
- )\r
-{\r
- //\r
- // The memory used for route table entries must immediately follow\r
- // the ConfigData and be not packed.\r
- //\r
- if (ConfigData->RouteTableSize > 0) {\r
- ConfigData->RouteTable = (EFI_IP4_ROUTE_TABLE *) (ConfigData + 1);\r
- } else {\r
- ConfigData->RouteTable = NULL;\r
- }\r
-}\r
-\r