]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
Import Ip4Config module
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4ConfigDxe / NicIp4Variable.c
diff --git a/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c b/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
new file mode 100644 (file)
index 0000000..ac5283f
--- /dev/null
@@ -0,0 +1,383 @@
+/** @file\r
+\r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. 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
+\r
+  NicIp4Variable.c\r
+\r
+Abstract:\r
+\r
+  Routines used to operate the Ip4 configure variable\r
+\r
+\r
+**/\r
+\r
+\r
+#include <Library/NetLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+\r
+#include "NicIp4Variable.h"\r
+\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) || !Ip4IsUnicast (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
+          !Ip4IsUnicast (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
+  None\r
+\r
+  @return The IP4 configure read if it is there and is valid, otherwise NULL\r
+\r
+**/\r
+IP4_CONFIG_VARIABLE *\r
+Ip4ConfigReadVariable (\r
+  VOID\r
+  )\r
+{\r
+  IP4_CONFIG_VARIABLE       *Variable;\r
+  EFI_STATUS                Status;\r
+  UINTN                     Size;\r
+  UINT16                    CheckSum;\r
+\r
+  //\r
+  // Get the size of variable, then allocate a buffer to read the variable.\r
+  //\r
+  Size     = 0;\r
+  Variable = NULL;\r
+  Status   = gRT->GetVariable (\r
+                    EFI_NIC_IP4_CONFIG_VARIABLE,\r
+                    &gEfiNicIp4ConfigVariableGuid,\r
+                    NULL,\r
+                    &Size,\r
+                    NULL\r
+                    );\r
+\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return NULL;\r
+  }\r
+\r
+  if (Size < sizeof (IP4_CONFIG_VARIABLE)) {\r
+    goto REMOVE_VARIABLE;\r
+  }\r
+\r
+  Variable = NetAllocatePool (Size);\r
+\r
+  if (Variable == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  Status = gRT->GetVariable (\r
+                  EFI_NIC_IP4_CONFIG_VARIABLE,\r
+                  &gEfiNicIp4ConfigVariableGuid,\r
+                  NULL,\r
+                  &Size,\r
+                  Variable\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+\r
+  //\r
+  // Verify the checksum, variable size and count\r
+  //\r
+  CheckSum = ~NetblockChecksum ((UINT8 *) Variable, (UINT32)Size);\r
+\r
+  if ((CheckSum != 0) || (Size != Variable->Len)) {\r
+    goto REMOVE_VARIABLE;\r
+  }\r
+\r
+  if ((Variable->Count < 1) || (Variable->Count > MAX_IP4_CONFIG_IN_VARIABLE)) {\r
+    goto REMOVE_VARIABLE;\r
+  }\r
+\r
+  return Variable;\r
+\r
+REMOVE_VARIABLE:\r
+  Ip4ConfigWriteVariable (NULL);\r
+\r
+ON_ERROR:\r
+  if (Variable != NULL) {\r
+    NetFreePool (Variable);\r
+  }\r
+\r
+  return NULL;\r
+}\r
+\r
+\r
+/**\r
+  Write the IP4 configure variable to the NVRAM. If Config\r
+  is NULL, remove the variable.\r
+\r
+  @param  Config       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_VARIABLE    * Config        OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+\r
+  Status = gRT->SetVariable (\r
+                  EFI_NIC_IP4_CONFIG_VARIABLE,\r
+                  &gEfiNicIp4ConfigVariableGuid,\r
+                  IP4_CONFIG_VARIABLE_ATTRIBUTES,\r
+                  (Config == NULL) ? 0 : Config->Len,\r
+                  Config\r
+                  );\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Locate the IP4 configure parameters from the variable.If a\r
+  configuration is found, copy it to a newly allocated block\r
+  of memory to avoid the alignment problem. Caller should\r
+  release the memory after use.\r
+\r
+  @param  Variable     The IP4 configure variable to search in\r
+  @param  NicAddr      The interface address to check\r
+\r
+  @return The point to the NIC's IP4 configure info if it is found\r
+  @return in the IP4 variable, otherwise NULL.\r
+\r
+**/\r
+NIC_IP4_CONFIG_INFO *\r
+Ip4ConfigFindNicVariable (\r
+  IN IP4_CONFIG_VARIABLE    *Variable,\r
+  IN NIC_ADDR               *NicAddr\r
+  )\r
+{\r
+  NIC_IP4_CONFIG_INFO       Temp;\r
+  NIC_IP4_CONFIG_INFO       *Config;\r
+  UINT32                    Index;\r
+  UINT8                     *Cur;\r
+  UINT32                    Len;\r
+\r
+  Cur = (UINT8*)&Variable->ConfigInfo;\r
+\r
+  for (Index = 0; Index < Variable->Count; Index++) {\r
+    //\r
+    // Copy the data to Temp to avoid the alignment problems\r
+    //\r
+    NetCopyMem (&Temp, Cur, sizeof (NIC_IP4_CONFIG_INFO));\r
+    Len = SIZEOF_NIC_IP4_CONFIG_INFO (&Temp);\r
+\r
+    //\r
+    // Found the matching configuration parameters, allocate\r
+    // a block of memory then copy it out.\r
+    //\r
+    if (NIC_ADDR_EQUAL (&Temp.NicAddr, NicAddr)) {\r
+      Config = NetAllocatePool (Len);\r
+\r
+      if (Config == NULL) {\r
+        return NULL;\r
+      }\r
+\r
+      NetCopyMem (Config, Cur, Len);\r
+      return Config;\r
+    }\r
+\r
+    Cur += Len;\r
+  }\r
+\r
+  return NULL;\r
+}\r
+\r
+\r
+/**\r
+  Modify the configuration parameter for the NIC in the variable.\r
+  If Config is NULL, old configuration will be remove from the new\r
+  variable. Otherwise, append it or replace the old one.\r
+\r
+  @param  Variable     The IP4 variable to change\r
+  @param  NicAddr      The interface to search\r
+  @param  Config       The new configuration parameter (NULL to remove the old)\r
+\r
+  @return The new IP4_CONFIG_VARIABLE variable if the new variable has at\r
+  @return least one NIC configure and no EFI_OUT_OF_RESOURCES failure.\r
+  @return Return NULL either because failed to locate memory for new variable\r
+  @return or the only NIC configure is removed from the Variable.\r
+\r
+**/\r
+IP4_CONFIG_VARIABLE *\r
+Ip4ConfigModifyVariable (\r
+  IN IP4_CONFIG_VARIABLE    *Variable,    OPTIONAL\r
+  IN NIC_ADDR               *NicAddr,\r
+  IN NIC_IP4_CONFIG_INFO    *Config       OPTIONAL\r
+  )\r
+{\r
+  NIC_IP4_CONFIG_INFO       Temp;\r
+  NIC_IP4_CONFIG_INFO       *Old;\r
+  IP4_CONFIG_VARIABLE       *NewVar;\r
+  UINT32                    Len;\r
+  UINT32                    TotalLen;\r
+  UINT32                    Count;\r
+  UINT8                     *Next;\r
+  UINT8                     *Cur;\r
+  UINT32                    Index;\r
+\r
+  ASSERT ((Variable != NULL) || (Config != NULL));\r
+\r
+  //\r
+  // Compute the total length\r
+  //\r
+  if (Variable != NULL) {\r
+    //\r
+    // Variable != NULL, then Config can be NULL or not.  and so is\r
+    // the Old. If old configure exists, it is removed from the\r
+    // Variable. New configure is append to the variable.\r
+    //\r
+    //\r
+    Count     = Variable->Count;\r
+    Cur       = (UINT8 *)&Variable->ConfigInfo;\r
+    TotalLen  = Variable->Len;\r
+\r
+    Old       = Ip4ConfigFindNicVariable (Variable, NicAddr);\r
+\r
+    if (Old != NULL) {\r
+      TotalLen -= SIZEOF_NIC_IP4_CONFIG_INFO (Old);\r
+      NetFreePool (Old);\r
+    }\r
+\r
+    if (Config != NULL) {\r
+      TotalLen += SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
+    }\r
+\r
+    //\r
+    // Return NULL if the only NIC_IP4_CONFIG_INFO is being removed.\r
+    //\r
+    if (TotalLen < sizeof (IP4_CONFIG_VARIABLE)) {\r
+      return NULL;\r
+    }\r
+\r
+  } else {\r
+    //\r
+    // Variable == NULL and Config != NULL, Create a new variable with\r
+    // this NIC configure.\r
+    //\r
+    Count     = 0;\r
+    Cur       = NULL;\r
+    TotalLen  = sizeof (IP4_CONFIG_VARIABLE) - sizeof (NIC_IP4_CONFIG_INFO)\r
+                + SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
+  }\r
+\r
+  ASSERT (TotalLen >= sizeof (IP4_CONFIG_VARIABLE));\r
+\r
+  NewVar = NetAllocateZeroPool (TotalLen);\r
+\r
+  if (NewVar == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  NewVar->Len = TotalLen;\r
+\r
+  //\r
+  // Copy the other configure parameters from the old variable\r
+  //\r
+  Next = (UINT8 *)&NewVar->ConfigInfo;\r
+\r
+  for (Index = 0; Index < Count; Index++) {\r
+    NetCopyMem (&Temp, Cur, sizeof (NIC_IP4_CONFIG_INFO));\r
+    Len = SIZEOF_NIC_IP4_CONFIG_INFO (&Temp);\r
+\r
+    if (!NIC_ADDR_EQUAL (&Temp.NicAddr, NicAddr)) {\r
+      NetCopyMem (Next, Cur, Len);\r
+      Next += Len;\r
+      NewVar->Count++;\r
+    }\r
+\r
+    Cur += Len;\r
+  }\r
+\r
+  //\r
+  // Append the new configure if it isn't NULL.\r
+  //\r
+  Len = 0;\r
+\r
+  if (Config != NULL) {\r
+    Len = SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
+\r
+    NetCopyMem (Next, Config, Len);\r
+    NewVar->Count++;\r
+  }\r
+\r
+  ASSERT (Next + Len == (UINT8 *) NewVar + TotalLen);\r
+\r
+  NewVar->CheckSum = ~NetblockChecksum ((UINT8 *) NewVar, TotalLen);\r
+  return NewVar;\r
+}\r