]> 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 ee78ceea6c7f11d08a6f8850b43fe4b4e9bc16a0..3d2f0a4f9b240e443f4991790029f7cdefd95ac0 100644 (file)
 /** @file\r
+  This code implements the IP4Config and NicIp4Config protocols.\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\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\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
-Module Name:\r
-\r
-  Ip4Config.c\r
-\r
-Abstract:\r
-\r
-  This code implements the IP4Config and NicIp4Config protocols.\r
-\r
-\r
 **/\r
 \r
 #include "Ip4Config.h"\r
+#include "NicIp4Variable.h"\r
 \r
-IP4_CONFIG_INSTANCE *mIp4ConfigNicList[MAX_IP4_CONFIG_IN_VARIABLE];\r
-\r
-VOID\r
-EFIAPI\r
-Ip4ConfigOnDhcp4Complete (\r
-  IN EFI_EVENT                  Event,\r
-  IN VOID                       *Context\r
-  );\r
-\r
-\r
-/**\r
-  Return the name and MAC address for the NIC. The Name, if not NULL,\r
-  has at least IP4_NIC_NAME_LENGTH bytes.\r
-\r
-  @param  This                   The NIC IP4 CONFIG protocol\r
-  @param  Name                   The buffer to return the name\r
-  @param  NicAddr                The buffer to return the MAC addr\r
-\r
-  @retval EFI_INVALID_PARAMETER  This is NULL\r
-  @retval EFI_SUCCESS            The name or address of the NIC are returned.\r
-\r
-**/\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EfiNicIp4ConfigGetName (\r
-  IN  EFI_NIC_IP4_CONFIG_PROTOCOL *This,\r
-  IN  UINT16                      *Name,          OPTIONAL\r
-  IN  NIC_ADDR                    *NicAddr       OPTIONAL\r
-  )\r
-{\r
-  IP4_CONFIG_INSTANCE       *Instance;\r
-\r
-  if (This == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Instance = IP4_CONFIG_INSTANCE_FROM_NIC_IP4CONFIG (This);\r
-\r
-  if (Name != NULL) {\r
-    NetCopyMem (Name, Instance->NicName, IP4_NIC_NAME_LENGTH);\r
-  }\r
-\r
-  if (NicAddr != NULL) {\r
-    CopyMem (NicAddr, &Instance->NicAddr, sizeof (*NicAddr));\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\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
+  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
-  @return Otherwise the NIC's IP configure parameter.\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
-    NetFreePool (Variable);\r
+  NicConfig = Ip4ConfigReadVariable (Instance);\r
+  if (NicConfig == NULL) {\r
     return NULL;\r
   }\r
 \r
@@ -113,124 +55,56 @@ 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
-      NetFreePool (NewVariable);\r
-    };\r
+  if (!Ip4ConfigIsValid (NicConfig)) {\r
+    Ip4ConfigWriteVariable (Instance, NULL);\r
 \r
-    NetFreePool (Config);\r
-    Config = NULL;\r
+    FreePool (NicConfig);\r
+    NicConfig = NULL;\r
   }\r
 \r
-  NetFreePool (Variable);\r
-  return Config;\r
+  return NicConfig;\r
 }\r
 \r
-\r
 /**\r
-  Get the configure parameter for this NIC.\r
-\r
-  @param  This                   The NIC IP4 CONFIG protocol\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_INVALID_PARAMETER  This or ConfigLen is NULL\r
-  @retval EFI_NOT_FOUND          There is no configure parameter for the NIC in\r
-                                 NVRam.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-EfiNicIp4ConfigGetInfo (\r
-  IN  EFI_NIC_IP4_CONFIG_PROTOCOL *This,\r
-  IN OUT  UINTN                   *ConfigLen,\r
-  OUT NIC_IP4_CONFIG_INFO         *NicConfig\r
-  )\r
-{\r
-  IP4_CONFIG_INSTANCE *Instance;\r
-  NIC_IP4_CONFIG_INFO *Config;\r
-  EFI_STATUS          Status;\r
-  UINTN               Len;\r
-\r
-  if ((This == NULL) || (ConfigLen == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Read the Nic's configuration parameter from variable\r
-  //\r
-  Instance  = IP4_CONFIG_INSTANCE_FROM_NIC_IP4CONFIG (This);\r
-  Config    = Ip4ConfigGetNicInfo (&Instance->NicAddr);\r
-\r
-  if (Config == NULL) {\r
-    return EFI_NOT_FOUND;\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
-    NetCopyMem (NicConfig, Config, Len);\r
-  }\r
+  Set the IP configure parameters for this NIC.\r
 \r
-  *ConfigLen = Len;\r
-\r
-  NetFreePool (Config);\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Set the IP configure parameters for this NIC. If Reconfig is TRUE,\r
-  the IP driver will be informed to discard current auto configure\r
-  parameter and restart the auto configuration process. If current\r
-  there is a pending auto configuration, EFI_ALREADY_STARTED is\r
+  If Reconfig is TRUE, the IP driver will be informed to discard current\r
+  auto configure parameter and restart the auto configuration process.\r
+  If current there is a pending auto configuration, EFI_ALREADY_STARTED is\r
   returned. You can only change the configure setting when either\r
   the configure has finished or not started yet. If NicConfig, the\r
   NIC's configure parameter is removed from the variable.\r
 \r
-  @param  This                   The NIC IP4 CONFIG protocol\r
-  @param  NicConfig              The new NIC IP4 configure parameter\r
+  @param  Instance               The IP4 CONFIG instance.\r
+  @param  NicConfig              The new NIC IP4 configure parameter.\r
   @param  Reconfig               Inform the IP4 driver to restart the auto\r
-                                 configuration\r
+                                 configuration.\r
 \r
+  @retval EFI_SUCCESS            The configure parameter for this NIC was\r
+                                 set successfully.\r
   @retval EFI_INVALID_PARAMETER  This is NULL or the configure parameter is\r
                                  invalid.\r
   @retval EFI_ALREADY_STARTED    There is a pending auto configuration.\r
-  @retval EFI_NOT_FOUND          No auto configure parameter is found\r
+  @retval EFI_NOT_FOUND          No auto configure parameter is found.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 EfiNicIp4ConfigSetInfo (\r
-  IN EFI_NIC_IP4_CONFIG_PROTOCOL  *This,\r
-  IN NIC_IP4_CONFIG_INFO          *NicConfig,     OPTIONAL\r
+  IN IP4_CONFIG_INSTANCE          *Instance,\r
+  IN NIC_IP4_CONFIG_INFO          *NicConfig     OPTIONAL,\r
   IN BOOLEAN                      Reconfig\r
   )\r
 {\r
-  IP4_CONFIG_INSTANCE *Instance;\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
   //\r
-  if (This == NULL) {\r
+  if (Instance == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Instance = IP4_CONFIG_INSTANCE_FROM_NIC_IP4CONFIG (This);\r
-\r
   if ((NicConfig != NULL) && (!Ip4ConfigIsValid (NicConfig) ||\r
       !NIC_ADDR_EQUAL (&NicConfig->NicAddr, &Instance->NicAddr))) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -243,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
-    NetFreePool (NewVariable);\r
-  }\r
-\r
-  //\r
-  // Variable is NULL when saving the first configure parameter\r
-  //\r
-  if (Variable != NULL) {\r
-    NetFreePool (Variable);\r
-  }\r
-\r
+  Status = Ip4ConfigWriteVariable (Instance, NicConfig);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -271,23 +126,195 @@ 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
-\r
 /**\r
-  Start the auto configuration process.\r
+  Callback function when DHCP process finished. It will save the\r
+  retrieved IP configure parameter from DHCP to the NVRam.\r
+\r
+  @param  Event                  The callback event\r
+  @param  Context                Opaque context to the callback\r
+\r
+  @return None\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+Ip4ConfigOnDhcp4Complete (\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
+  )\r
+{\r
+  IP4_CONFIG_INSTANCE       *Instance;\r
+  EFI_DHCP4_MODE_DATA       Dhcp4Mode;\r
+  EFI_IP4_IPCONFIG_DATA     *Ip4Config;\r
+  EFI_STATUS                Status;\r
+  BOOLEAN                   Permanent;\r
+  IP4_ADDR                  Subnet;\r
+  IP4_ADDR                  Ip1;\r
+  IP4_ADDR                  Ip2;\r
+\r
+  Instance = (IP4_CONFIG_INSTANCE *) Context;\r
+  ASSERT (Instance->Dhcp4 != NULL);\r
+\r
+  Instance->State   = IP4_CONFIG_STATE_CONFIGURED;\r
+  Instance->Result  = EFI_TIMEOUT;\r
+\r
+  //\r
+  // Get the DHCP retrieved parameters\r
+  //\r
+  Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (Dhcp4Mode.State == Dhcp4Bound) {\r
+    //\r
+    // Save the new configuration retrieved by DHCP both in\r
+    // the instance and to NVRam. So, both the IP4 driver and\r
+    // other user can get that address.\r
+    //\r
+    Permanent = FALSE;\r
+\r
+    if (Instance->NicConfig != NULL) {\r
+      ASSERT (Instance->NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);\r
+      Permanent = Instance->NicConfig->Permanent;\r
+      FreePool (Instance->NicConfig);\r
+    }\r
+\r
+    Instance->NicConfig = AllocatePool (sizeof (NIC_IP4_CONFIG_INFO) + 2* sizeof (EFI_IP4_ROUTE_TABLE));\r
+\r
+    if (Instance->NicConfig == NULL) {\r
+      Instance->Result = EFI_OUT_OF_RESOURCES;\r
+      goto ON_EXIT;\r
+    }\r
+\r
+    Instance->NicConfig->Ip4Info.RouteTable = (EFI_IP4_ROUTE_TABLE *) (Instance->NicConfig + 1);\r
+\r
+    CopyMem (&Instance->NicConfig->NicAddr, &Instance->NicAddr, sizeof (Instance->NicConfig->NicAddr));\r
+    Instance->NicConfig->Source  = IP4_CONFIG_SOURCE_DHCP;\r
+    Instance->NicConfig->Permanent = Permanent;\r
+\r
+    Ip4Config                    = &Instance->NicConfig->Ip4Info;\r
+    Ip4Config->StationAddress    = Dhcp4Mode.ClientAddress;\r
+    Ip4Config->SubnetMask        = Dhcp4Mode.SubnetMask;\r
+\r
+    //\r
+    // Create a route for the connected network\r
+    //\r
+    Ip4Config->RouteTableSize    = 1;\r
+\r
+    CopyMem (&Ip1, &Dhcp4Mode.ClientAddress, sizeof (IP4_ADDR));\r
+    CopyMem (&Ip2, &Dhcp4Mode.SubnetMask, sizeof (IP4_ADDR));\r
 \r
-  @param  This                   The IP4 configure protocol\r
-  @param  DoneEvent              The event to signal when auto configure is done\r
-  @param  ReconfigEvent          The event to signal when reconfigure is necessary.\r
+    Subnet = Ip1 & Ip2;\r
 \r
-  @retval EFI_INVALID_PARAMETER  One of the function parameters is NULL.\r
-  @retval EFI_ALREADY_STARTED    The auto configuration has already started.\r
-  @retval EFI_SUCCESS            The auto configure is successfully started.\r
+    CopyMem (&Ip4Config->RouteTable[0].SubnetAddress, &Subnet, sizeof (EFI_IPv4_ADDRESS));\r
+    CopyMem (&Ip4Config->RouteTable[0].SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
+    ZeroMem (&Ip4Config->RouteTable[0].GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
+\r
+    //\r
+    // Create a route if there is a default router.\r
+    //\r
+    if (!EFI_IP4_EQUAL (&Dhcp4Mode.RouterAddress, &mZeroIp4Addr)) {\r
+      Ip4Config->RouteTableSize = 2;\r
+\r
+      ZeroMem (&Ip4Config->RouteTable[1].SubnetAddress, sizeof (EFI_IPv4_ADDRESS));\r
+      ZeroMem (&Ip4Config->RouteTable[1].SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
+      CopyMem (&Ip4Config->RouteTable[1].GatewayAddress, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
+    }\r
+\r
+    Instance->Result = EFI_SUCCESS;\r
+\r
+    //\r
+    // ignore the return status of EfiNicIp4ConfigSetInfo. Network\r
+    // stack can operate even that failed.\r
+    //\r
+    EfiNicIp4ConfigSetInfo (Instance, Instance->NicConfig, FALSE);\r
+  }\r
+\r
+ON_EXIT:\r
+  gBS->SignalEvent (Instance->DoneEvent);\r
+  Ip4ConfigCleanDhcp4 (Instance);\r
+\r
+  DispatchDpc ();\r
+\r
+  return ;\r
+}\r
+\r
+/**\r
+  Starts running the configuration policy for the EFI IPv4 Protocol driver.\r
+\r
+  The Start() function is called to determine and to begin the platform\r
+  configuration policy by the EFI IPv4 Protocol driver. This determination may\r
+  be as simple as returning EFI_UNSUPPORTED if there is no EFI IPv4 Protocol\r
+  driver configuration policy. It may be as involved as loading some defaults\r
+  from nonvolatile storage, downloading dynamic data from a DHCP server, and\r
+  checking permissions with a site policy server.\r
+  Starting the configuration policy is just the beginning. It may finish almost\r
+  instantly or it may take several minutes before it fails to retrieve configuration\r
+  information from one or more servers. Once the policy is started, drivers\r
+  should use the DoneEvent parameter to determine when the configuration policy\r
+  has completed. EFI_IP4_CONFIG_PROTOCOL.GetData() must then be called to\r
+  determine if the configuration succeeded or failed.\r
+  Until the configuration completes successfully, EFI IPv4 Protocol driver instances\r
+  that are attempting to use default configurations must return EFI_NO_MAPPING.\r
+  Once the configuration is complete, the EFI IPv4 Configuration Protocol driver\r
+  signals DoneEvent. The configuration may need to be updated in the future,\r
+  however; in this case, the EFI IPv4 Configuration Protocol driver must signal\r
+  ReconfigEvent, and all EFI IPv4 Protocol driver instances that are using default\r
+  configurations must return EFI_NO_MAPPING until the configuration policy has\r
+  been rerun.\r
+\r
+  @param  This                   Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.\r
+  @param  DoneEvent              Event that will be signaled when the EFI IPv4\r
+                                 Protocol driver configuration policy completes\r
+                                 execution. This event must be of type EVT_NOTIFY_SIGNAL.\r
+  @param  ReconfigEvent          Event that will be signaled when the EFI IPv4\r
+                                 Protocol driver configuration needs to be updated.\r
+                                 This event must be of type EVT_NOTIFY_SIGNAL.\r
+\r
+  @retval EFI_SUCCESS            The configuration policy for the EFI IPv4 Protocol\r
+                                 driver is now running.\r
+  @retval EFI_INVALID_PARAMETER  One or more of the following parameters is NULL:\r
+                                  This\r
+                                  DoneEvent\r
+                                  ReconfigEvent\r
+  @retval EFI_OUT_OF_RESOURCES   Required system resources could not be allocated.\r
+  @retval EFI_ALREADY_STARTED    The configuration policy for the EFI IPv4 Protocol\r
+                                 driver was already started.\r
+  @retval EFI_DEVICE_ERROR       An unexpected system error or network error occurred.\r
+  @retval EFI_UNSUPPORTED        This interface does not support the EFI IPv4 Protocol\r
+                                 driver configuration.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -313,7 +340,7 @@ EfiIp4ConfigStart (
 \r
   Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This);\r
 \r
-  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
   if (Instance->State != IP4_CONFIG_STATE_IDLE) {\r
     Status = EFI_ALREADY_STARTED;\r
@@ -324,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
@@ -415,7 +448,7 @@ EfiIp4ConfigStart (
   //\r
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_SIGNAL,\r
-                  NET_TPL_EVENT,\r
+                  TPL_CALLBACK,\r
                   Ip4ConfigOnDhcp4Complete,\r
                   Instance,\r
                   &Instance->Dhcp4Event\r
@@ -440,20 +473,27 @@ ON_ERROR:
   }\r
 \r
 ON_EXIT:\r
-  NET_RESTORE_TPL (OldTpl);\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  DispatchDpc ();\r
 \r
   return Status;\r
 }\r
 \r
 \r
 /**\r
-  Stop the current auto configuration\r
+  Stops running the configuration policy for the EFI IPv4 Protocol driver.\r
+\r
+  The Stop() function stops the configuration policy for the EFI IPv4 Protocol driver.\r
+  All configuration data will be lost after calling Stop().\r
 \r
-  @param  This                   The IP4 CONFIG protocol\r
+  @param  This                   Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.\r
 \r
+  @retval EFI_SUCCESS            The configuration policy for the EFI IPv4 Protocol\r
+                                 driver has been stopped.\r
   @retval EFI_INVALID_PARAMETER  This is NULL.\r
-  @retval EFI_NOT_STARTED        The auto configuration hasn't been started.\r
-  @retval EFI_SUCCESS            The auto configuration has been stopped.\r
+  @retval EFI_NOT_STARTED        The configuration policy for the EFI IPv4 Protocol\r
+                                 driver was not started.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -473,7 +513,7 @@ EfiIp4ConfigStop (
   Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This);\r
 \r
   Status = EFI_SUCCESS;\r
-  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
   if (Instance->State == IP4_CONFIG_STATE_IDLE) {\r
     Status = EFI_NOT_STARTED;\r
@@ -488,23 +528,36 @@ EfiIp4ConfigStop (
   Ip4ConfigCleanConfig (Instance);\r
 \r
 ON_EXIT:\r
-  NET_RESTORE_TPL (OldTpl);\r
+  gBS->RestoreTPL (OldTpl);\r
 \r
   return Status;\r
 }\r
 \r
 \r
 /**\r
-  Get the current outcome of the auto configuration process\r
+  Returns the default configuration data (if any) for the EFI IPv4 Protocol driver.\r
 \r
-  @param  This                   The IP4 CONFIG protocol\r
-  @param  ConfigDataSize         The size of the configure data\r
-  @param  ConfigData             The buffer to save the configure data\r
+  The GetData() function returns the current configuration data for the EFI IPv4\r
+  Protocol driver after the configuration policy has completed.\r
 \r
-  @retval EFI_INVALID_PARAMETER  This or ConfigDataSize is NULL\r
-  @retval EFI_BUFFER_TOO_SMALL   The buffer is too small. The needed size is\r
-                                 returned in the ConfigDataSize.\r
-  @retval EFI_SUCCESS            The configure data is put in the buffer\r
+  @param  This                   Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.\r
+  @param  ConfigDataSize         On input, the size of the ConfigData buffer.\r
+                                 On output, the count of bytes that were written\r
+                                 into the ConfigData buffer.\r
+  @param  ConfigData             Pointer to the EFI IPv4 Configuration Protocol\r
+                                 driver configuration data structure.\r
+                                 Type EFI_IP4_IPCONFIG_DATA is defined in\r
+                                 "Related Definitions" below.\r
+\r
+  @retval EFI_SUCCESS            The EFI IPv4 Protocol driver configuration has been returned.\r
+  @retval EFI_INVALID_PARAMETER  This is NULL.\r
+  @retval EFI_NOT_STARTED        The configuration policy for the EFI IPv4 Protocol\r
+                                 driver is not running.\r
+  @retval EFI_NOT_READY          EFI IPv4 Protocol driver configuration is still running.\r
+  @retval EFI_ABORTED            EFI IPv4 Protocol driver configuration could not complete.\r
+                                 Currently not implemented.\r
+  @retval EFI_BUFFER_TOO_SMALL   *ConfigDataSize is smaller than the configuration\r
+                                 data buffer or ConfigData is NULL.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -528,7 +581,7 @@ EfiIp4ConfigGetData (
   Instance  = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This);\r
 \r
   Status = EFI_SUCCESS;\r
-  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
   if (Instance->State == IP4_CONFIG_STATE_IDLE) {\r
     Status = EFI_NOT_STARTED;\r
@@ -554,131 +607,19 @@ EfiIp4ConfigGetData (
     if ((*ConfigDataSize < Len) || (ConfigData == NULL)) {\r
       Status = EFI_BUFFER_TOO_SMALL;\r
     } else {\r
-      NetCopyMem (ConfigData, &NicConfig->Ip4Info, Len);\r
+      CopyMem (ConfigData, &NicConfig->Ip4Info, Len);\r
+      Ip4ConfigFixRouteTablePointer (ConfigData);\r
     }\r
 \r
     *ConfigDataSize = Len;\r
   }\r
 \r
 ON_EXIT:\r
-  NET_RESTORE_TPL (OldTpl);\r
+  gBS->RestoreTPL (OldTpl);\r
 \r
   return Status;\r
 }\r
 \r
-\r
-/**\r
-  Callback function when DHCP process finished. It will save the\r
-  retrieved IP configure parameter from DHCP to the NVRam.\r
-\r
-  @param  Event                  The callback event\r
-  @param  Context                Opaque context to the callback\r
-\r
-  @return None\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-Ip4ConfigOnDhcp4Complete (\r
-  IN EFI_EVENT              Event,\r
-  IN VOID                   *Context\r
-  )\r
-{\r
-  IP4_CONFIG_INSTANCE       *Instance;\r
-  EFI_DHCP4_MODE_DATA       Dhcp4Mode;\r
-  EFI_IP4_IPCONFIG_DATA     *Ip4Config;\r
-  EFI_STATUS                Status;\r
-  BOOLEAN                   Perment;\r
-  IP4_ADDR                  Subnet;\r
-  IP4_ADDR                  Ip1;\r
-  IP4_ADDR                  Ip2;\r
-\r
-  Instance = (IP4_CONFIG_INSTANCE *) Context;\r
-  ASSERT (Instance->Dhcp4 != NULL);\r
-\r
-  Instance->State   = IP4_CONFIG_STATE_CONFIGURED;\r
-  Instance->Result  = EFI_TIMEOUT;\r
-\r
-  //\r
-  // Get the DHCP retrieved parameters\r
-  //\r
-  Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  if (Dhcp4Mode.State == Dhcp4Bound) {\r
-    //\r
-    // Save the new configuration retrieved by DHCP both in\r
-    // the instance and to NVRam. So, both the IP4 driver and\r
-    // other user can get that address.\r
-    //\r
-    Perment = FALSE;\r
-\r
-    if (Instance->NicConfig != NULL) {\r
-      ASSERT (Instance->NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);\r
-      Perment = Instance->NicConfig->Perment;\r
-      NetFreePool (Instance->NicConfig);\r
-    }\r
-\r
-    Instance->NicConfig = NetAllocatePool (sizeof (NIC_IP4_CONFIG_INFO) +\r
-                                           sizeof (EFI_IP4_ROUTE_TABLE));\r
-\r
-    if (Instance->NicConfig == NULL) {\r
-      Instance->Result = EFI_OUT_OF_RESOURCES;\r
-      goto ON_EXIT;\r
-    }\r
-\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
-\r
-    Ip4Config                    = &Instance->NicConfig->Ip4Info;\r
-    Ip4Config->StationAddress    = Dhcp4Mode.ClientAddress;\r
-    Ip4Config->SubnetMask        = Dhcp4Mode.SubnetMask;\r
-\r
-    //\r
-    // Create a route for the connected network\r
-    //\r
-    Ip4Config->RouteTableSize    = 1;\r
-\r
-    NetCopyMem (&Ip1, &Dhcp4Mode.ClientAddress, sizeof (IP4_ADDR));\r
-    NetCopyMem (&Ip2, &Dhcp4Mode.SubnetMask, sizeof (IP4_ADDR));\r
-    \r
-    Subnet = Ip1 & Ip2;\r
-\r
-    NetCopyMem (&Ip4Config->RouteTable[0].SubnetAddress, &Subnet, sizeof (EFI_IPv4_ADDRESS));\r
-    NetCopyMem (&Ip4Config->RouteTable[0].SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
-    NetZeroMem (&Ip4Config->RouteTable[0].GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
-\r
-    //\r
-    // Create a route if there is a default router.\r
-    //\r
-    if (!EFI_IP4_EQUAL (Dhcp4Mode.RouterAddress, mZeroIp4Addr)) {\r
-      Ip4Config->RouteTableSize = 2;\r
-\r
-      NetZeroMem (&Ip4Config->RouteTable[1].SubnetAddress, sizeof (EFI_IPv4_ADDRESS));\r
-      NetZeroMem (&Ip4Config->RouteTable[1].SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
-      NetCopyMem (&Ip4Config->RouteTable[1].GatewayAddress, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
-    }\r
-\r
-    Instance->Result = EFI_SUCCESS;\r
-\r
-    //\r
-    // ignore the return status of EfiNicIp4ConfigSetInfo. Network\r
-    // stack can operate even that failed.\r
-    //\r
-    EfiNicIp4ConfigSetInfo (&Instance->NicIp4Protocol, Instance->NicConfig, FALSE);\r
-  }\r
-\r
-ON_EXIT:\r
-  gBS->SignalEvent (Instance->DoneEvent);\r
-  Ip4ConfigCleanDhcp4 (Instance);\r
-  return ;\r
-}\r
-\r
-\r
 /**\r
   Release all the DHCP related resources.\r
 \r
@@ -716,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
@@ -724,7 +665,7 @@ Ip4ConfigCleanDhcp4 (
 \r
 \r
 /**\r
-  Clean up all the configuration parameters\r
+  Clean up all the configuration parameters.\r
 \r
   @param  Instance               The IP4 configure instance\r
 \r
@@ -737,7 +678,7 @@ Ip4ConfigCleanConfig (
   )\r
 {\r
   if (Instance->NicConfig != NULL) {\r
-    NetFreePool (Instance->NicConfig);\r
+    FreePool (Instance->NicConfig);\r
     Instance->NicConfig = NULL;\r
   }\r
 \r
@@ -748,14 +689,57 @@ Ip4ConfigCleanConfig (
   Ip4ConfigCleanDhcp4 (Instance);\r
 }\r
 \r
-EFI_IP4_CONFIG_PROTOCOL     mIp4ConfigProtocolTemplate = {\r
-  EfiIp4ConfigStart,\r
-  EfiIp4ConfigStop,\r
-  EfiIp4ConfigGetData\r
-};\r
 \r
-EFI_NIC_IP4_CONFIG_PROTOCOL mNicIp4ConfigProtocolTemplate = {\r
-  EfiNicIp4ConfigGetName,\r
-  EfiNicIp4ConfigGetInfo,\r
-  EfiNicIp4ConfigSetInfo\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