]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4Config.c
SourceLevelDebugPkg/SecPeiDebugAgentLib: Restore CPU interrupt state
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4ConfigDxe / Ip4Config.c
index 8f26b6f79b7678bc8d0a6d753d8bec053c6c141f..3d2f0a4f9b240e443f4991790029f7cdefd95ac0 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   This code implements the IP4Config and NicIp4Config protocols.\r
 \r
-Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>\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
@@ -13,43 +14,40 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "Ip4Config.h"\r
+#include "NicIp4Variable.h"\r
 \r
-IP4_CONFIG_INSTANCE *mIp4ConfigNicList[MAX_IP4_CONFIG_IN_VARIABLE];\r
-\r
+//\r
+// Ip4 Config Protocol\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP4_CONFIG_PROTOCOL     mIp4ConfigProtocolTemplate = {\r
+  EfiIp4ConfigStart,\r
+  EfiIp4ConfigStop,\r
+  EfiIp4ConfigGetData\r
+};\r
 \r
 /**\r
   Get the NIC's configure information from the IP4 configure variable.\r
   It will remove the invalid variable.\r
 \r
-  @param  NicAddr                The NIC to check\r
+  @param  Instance               The IP4 CONFIG instance.\r
 \r
   @return NULL if no configure for the NIC in the variable, or it is invalid.\r
           Otherwise the pointer to the NIC's IP configure parameter will be returned.\r
 \r
 **/\r
 NIC_IP4_CONFIG_INFO *\r
-Ip4ConfigGetNicInfo (\r
-  IN  NIC_ADDR              *NicAddr\r
+EfiNicIp4ConfigGetInfo (\r
+  IN  IP4_CONFIG_INSTANCE   *Instance\r
   )\r
 {\r
-  IP4_CONFIG_VARIABLE       *Variable;\r
-  IP4_CONFIG_VARIABLE       *NewVariable;\r
-  NIC_IP4_CONFIG_INFO       *Config;\r
+  NIC_IP4_CONFIG_INFO *NicConfig;\r
 \r
   //\r
-  // Read the configuration parameter for this NicAddr from\r
+  // Read the configuration parameter for this NIC from\r
   // the EFI variable\r
   //\r
-  Variable = Ip4ConfigReadVariable ();\r
-\r
-  if (Variable == NULL) {\r
-    return NULL;\r
-  }\r
-\r
-  Config = Ip4ConfigFindNicVariable (Variable, NicAddr);\r
-\r
-  if (Config == NULL) {\r
-    FreePool (Variable);\r
+  NicConfig = Ip4ConfigReadVariable (Instance);\r
+  if (NicConfig == NULL) {\r
     return NULL;\r
   }\r
 \r
@@ -57,85 +55,16 @@ Ip4ConfigGetNicInfo (
   // Validate the configuration, if the configuration is invalid,\r
   // remove it from the variable.\r
   //\r
-  if (!Ip4ConfigIsValid (Config)) {\r
-    NewVariable = Ip4ConfigModifyVariable (Variable, &Config->NicAddr, NULL);\r
-    Ip4ConfigWriteVariable (NewVariable);\r
-\r
-    if (NewVariable != NULL) {\r
-      FreePool (NewVariable);\r
-    };\r
-\r
-    FreePool (Config);\r
-    Config = NULL;\r
-  }\r
-\r
-  FreePool (Variable);\r
-  return Config;\r
-}\r
-\r
-\r
-/**\r
-  Get the configure parameter for this NIC.\r
-\r
-  @param  Instance               The IP4 CONFIG Instance.\r
-  @param  ConfigLen              The length of the NicConfig buffer.\r
-  @param  NicConfig              The buffer to receive the NIC's configure\r
-                                 parameter.\r
-\r
-  @retval EFI_SUCCESS            The configure parameter for this NIC was\r
-                                 obtained successfully .\r
-  @retval EFI_INVALID_PARAMETER  This or ConfigLen is NULL.\r
-  @retval EFI_NOT_FOUND          There is no configure parameter for the NIC in\r
-                                 NVRam.\r
-  @retval EFI_BUFFER_TOO_SMALL   The ConfigLen is too small or the NicConfig is\r
-                                 NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-EfiNicIp4ConfigGetInfo (\r
-  IN  IP4_CONFIG_INSTANCE         *Instance,\r
-  IN OUT  UINTN                   *ConfigLen,\r
-  OUT NIC_IP4_CONFIG_INFO         *NicConfig\r
-  )\r
-{\r
-  NIC_IP4_CONFIG_INFO *Config;\r
-  EFI_STATUS          Status;\r
-  UINTN               Len;\r
-\r
-  if ((Instance == NULL) || (ConfigLen == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Read the Nic's configuration parameter from variable\r
-  //\r
-  Config    = Ip4ConfigGetNicInfo (&Instance->NicAddr);\r
+  if (!Ip4ConfigIsValid (NicConfig)) {\r
+    Ip4ConfigWriteVariable (Instance, NULL);\r
 \r
-  if (Config == NULL) {\r
-    return EFI_NOT_FOUND;\r
+    FreePool (NicConfig);\r
+    NicConfig = NULL;\r
   }\r
 \r
-  //\r
-  // Copy the data to user's buffer\r
-  //\r
-  Len = SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
-\r
-  if ((*ConfigLen < Len) || (NicConfig == NULL)) {\r
-    Status = EFI_BUFFER_TOO_SMALL;\r
-  } else {\r
-    Status = EFI_SUCCESS;\r
-    CopyMem (NicConfig, Config, Len);\r
-    Ip4ConfigFixRouteTablePointer (&NicConfig->Ip4Info);\r
-  }\r
-\r
-  *ConfigLen = Len;\r
-\r
-  FreePool (Config);\r
-  return Status;\r
+  return NicConfig;\r
 }\r
 \r
-\r
 /**\r
   Set the IP configure parameters for this NIC.\r
 \r
@@ -167,9 +96,7 @@ EfiNicIp4ConfigSetInfo (
   IN BOOLEAN                      Reconfig\r
   )\r
 {\r
-  IP4_CONFIG_VARIABLE *Variable;\r
-  IP4_CONFIG_VARIABLE *NewVariable;\r
-  EFI_STATUS          Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // Validate the parameters\r
@@ -190,26 +117,7 @@ EfiNicIp4ConfigSetInfo (
   //\r
   // Update the parameter in the configure variable\r
   //\r
-  Variable = Ip4ConfigReadVariable ();\r
-\r
-  if ((Variable == NULL) && (NicConfig == NULL)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  NewVariable = Ip4ConfigModifyVariable (Variable, &Instance->NicAddr, NicConfig);\r
-  Status      = Ip4ConfigWriteVariable (NewVariable);\r
-\r
-  if (NewVariable != NULL) {\r
-    FreePool (NewVariable);\r
-  }\r
-\r
-  //\r
-  // Variable is NULL when saving the first configure parameter\r
-  //\r
-  if (Variable != NULL) {\r
-    FreePool (Variable);\r
-  }\r
-\r
+  Status = Ip4ConfigWriteVariable (Instance, NicConfig);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -218,10 +126,33 @@ EfiNicIp4ConfigSetInfo (
   // Signal the IP4 to run the auto configuration again\r
   //\r
   if (Reconfig && (Instance->ReconfigEvent != NULL)) {\r
+    //\r
+    // When NicConfig is NULL, NIC IP4 configuration parameter is removed,\r
+    // the auto configuration process should stop running the configuration\r
+    // policy for the EFI IPv4 Protocol driver.\r
+    //\r
+    if (NicConfig == NULL) {\r
+      Instance->DoNotStart = TRUE;\r
+    }\r
+\r
     Status = gBS->SignalEvent (Instance->ReconfigEvent);\r
     DispatchDpc ();\r
   }\r
 \r
+  if (NicConfig == NULL) {\r
+    return Status;\r
+  }\r
+  //\r
+  // A dedicated timer is used to poll underlying media status.In case of\r
+  // cable swap, a new round auto configuration will be initiated. The timer\r
+  // starts in DHCP policy only. STATIC policy stops the timer.\r
+  // \r
+  if (NicConfig->Source == IP4_CONFIG_SOURCE_DHCP) {\r
+    gBS->SetTimer (Instance->Timer, TimerPeriodic, TICKS_PER_SECOND);\r
+  } else if (NicConfig->Source == IP4_CONFIG_SOURCE_STATIC) {\r
+    gBS->SetTimer (Instance->Timer, TimerCancel, 0);\r
+  }\r
+  \r
   return Status;\r
 }\r
 \r
@@ -246,7 +177,7 @@ Ip4ConfigOnDhcp4Complete (
   EFI_DHCP4_MODE_DATA       Dhcp4Mode;\r
   EFI_IP4_IPCONFIG_DATA     *Ip4Config;\r
   EFI_STATUS                Status;\r
-  BOOLEAN                   Perment;\r
+  BOOLEAN                   Permanent;\r
   IP4_ADDR                  Subnet;\r
   IP4_ADDR                  Ip1;\r
   IP4_ADDR                  Ip2;\r
@@ -272,11 +203,11 @@ Ip4ConfigOnDhcp4Complete (
     // the instance and to NVRam. So, both the IP4 driver and\r
     // other user can get that address.\r
     //\r
-    Perment = FALSE;\r
+    Permanent = FALSE;\r
 \r
     if (Instance->NicConfig != NULL) {\r
       ASSERT (Instance->NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);\r
-      Perment = Instance->NicConfig->Perment;\r
+      Permanent = Instance->NicConfig->Permanent;\r
       FreePool (Instance->NicConfig);\r
     }\r
 \r
@@ -291,7 +222,7 @@ Ip4ConfigOnDhcp4Complete (
 \r
     CopyMem (&Instance->NicConfig->NicAddr, &Instance->NicAddr, sizeof (Instance->NicConfig->NicAddr));\r
     Instance->NicConfig->Source  = IP4_CONFIG_SOURCE_DHCP;\r
-    Instance->NicConfig->Perment = Perment;\r
+    Instance->NicConfig->Permanent = Permanent;\r
 \r
     Ip4Config                    = &Instance->NicConfig->Ip4Info;\r
     Ip4Config->StationAddress    = Dhcp4Mode.ClientAddress;\r
@@ -420,9 +351,15 @@ EfiIp4ConfigStart (
   Instance->DoneEvent     = DoneEvent;\r
   Instance->ReconfigEvent = ReconfigEvent;\r
 \r
-  Instance->NicConfig     = Ip4ConfigGetNicInfo (&Instance->NicAddr);\r
+  Instance->NicConfig     = EfiNicIp4ConfigGetInfo (Instance);\r
 \r
   if (Instance->NicConfig == NULL) {\r
+    if (Instance->DoNotStart) {\r
+      Instance->DoNotStart = FALSE;\r
+      Status = EFI_SUCCESS;\r
+      goto ON_EXIT;\r
+    }\r
+\r
     Source = IP4_CONFIG_SOURCE_DHCP;\r
   } else {\r
     Source = Instance->NicConfig->Source;\r
@@ -720,7 +657,7 @@ Ip4ConfigCleanDhcp4 (
     This->Dhcp4Handle = NULL;\r
   }\r
 \r
-  if (This->Dhcp4Event == NULL) {\r
+  if (This->Dhcp4Event != NULL) {\r
     gBS->CloseEvent (This->Dhcp4Event);\r
     This->Dhcp4Event = NULL;\r
   }\r
@@ -752,9 +689,57 @@ Ip4ConfigCleanConfig (
   Ip4ConfigCleanDhcp4 (Instance);\r
 }\r
 \r
-EFI_IP4_CONFIG_PROTOCOL     mIp4ConfigProtocolTemplate = {\r
-  EfiIp4ConfigStart,\r
-  EfiIp4ConfigStop,\r
-  EfiIp4ConfigGetData\r
-};\r
 \r
+/**\r
+  A dedicated timer is used to poll underlying media status. In case of\r
+  cable swap, a new round auto configuration will be initiated. The timer \r
+  will signal the IP4 to run the auto configuration again. IP4 driver will free\r
+  old IP address related resource, such as route table and Interface, then\r
+  initiate a DHCP process by IP4Config->Start to acquire new IP, eventually\r
+  create route table for new IP address.\r
+\r
+  @param[in]  Event                  The IP4 service instance's heart beat timer.\r
+  @param[in]  Context                The IP4 service instance.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MediaChangeDetect (\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
+  )\r
+{\r
+  BOOLEAN                      OldMediaPresent;\r
+  EFI_STATUS                   Status;\r
+  EFI_SIMPLE_NETWORK_MODE      SnpModeData;\r
+  IP4_CONFIG_INSTANCE         *Instance;  \r
+\r
+  Instance = (IP4_CONFIG_INSTANCE *) Context;\r
+\r
+  OldMediaPresent = Instance->MediaPresent;\r
+  \r
+  //\r
+  // Get fresh mode data from MNP, since underlying media status may change\r
+  //\r
+  Status = Instance->Mnp->GetModeData (Instance->Mnp, NULL, &SnpModeData);\r
+  if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
+    return;\r
+  }\r
+\r
+  Instance->MediaPresent = SnpModeData.MediaPresent;\r
+  //\r
+  // Media transimit Unpresent to Present means new link movement is detected.\r
+  //\r
+  if (!OldMediaPresent && Instance->MediaPresent) {\r
+    //\r
+    // Signal the IP4 to run the auto configuration again. IP4 driver will free\r
+    // old IP address related resource, such as route table and Interface, then \r
+    // initiate a DHCP round by IP4Config->Start to acquire new IP, eventually \r
+    // create route table for new IP address.\r
+    //\r
+    if (Instance->ReconfigEvent != NULL) {\r
+      Status = gBS->SignalEvent (Instance->ReconfigEvent);\r
+      DispatchDpc ();\r
+    }\r
+  }\r
+}\r