]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
BaseTools:Change the path of the file that Binary Cache
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Driver.c
index eec6d69bd384c140c438b39efd5e2e3b5ba4055f..ebd4dec1dfe4290452b7e4041c626323679a587c 100644 (file)
@@ -1,22 +1,10 @@
 /** @file\r
-\r
-Copyright (c) 2005 - 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
-  Ip4Driver.c\r
-\r
-Abstract:\r
-\r
   The driver binding and service binding protocol for IP4 driver.\r
 \r
+Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
+\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -31,56 +19,96 @@ EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {
   NULL\r
 };\r
 \r
-//@MT: EFI_DRIVER_ENTRY_POINT (Ip4DriverEntryPoint)\r
+BOOLEAN  mIpSec2Installed = FALSE;\r
 \r
-EFI_STATUS\r
+/**\r
+   Callback function for IpSec2 Protocol install.\r
+\r
+   @param[in] Event           Event whose notification function is being invoked\r
+   @param[in] Context         Pointer to the notification function's context\r
+\r
+**/\r
+VOID\r
 EFIAPI\r
-Ip4DriverEntryPoint (\r
-  IN EFI_HANDLE             ImageHandle,\r
-  IN EFI_SYSTEM_TABLE       *SystemTable\r
+IpSec2InstalledCallback (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
   )\r
-/*++\r
+{\r
+  EFI_STATUS    Status;\r
+  //\r
+  // Test if protocol was even found.\r
+  // Notification function will be called at least once.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **)&mIpSec);\r
+  if (Status == EFI_SUCCESS && mIpSec != NULL) {\r
+    //\r
+    // Close the event so it does not get called again.\r
+    //\r
+    gBS->CloseEvent (Event);\r
 \r
-Routine Description:\r
+    mIpSec2Installed = TRUE;\r
+  }\r
+}\r
+\r
+/**\r
+  This is the declaration of an EFI image entry point. This entry point is\r
+  the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
+  both device drivers and bus drivers.\r
 \r
   The entry point for IP4 driver which install the driver\r
   binding and component name protocol on its image.\r
 \r
-Arguments:\r
+  @param[in]  ImageHandle           The firmware allocated handle for the UEFI image.\r
+  @param[in]  SystemTable           A pointer to the EFI System Table.\r
 \r
-  ImageHandle - The image handle of the driver\r
-  SystemTable - The system table\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a lack of resources.\r
 \r
-Returns:\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Ip4DriverEntryPoint (\r
+  IN EFI_HANDLE             ImageHandle,\r
+  IN EFI_SYSTEM_TABLE       *SystemTable\r
+  )\r
+{\r
+  VOID            *Registration;\r
 \r
-  EFI_SUCCESS if the driver binding and component name protocols\r
-  are successfully installed, otherwise if failed.\r
+  EfiCreateProtocolNotifyEvent (\r
+    &gEfiIpSec2ProtocolGuid,\r
+    TPL_CALLBACK,\r
+    IpSec2InstalledCallback,\r
+    NULL,\r
+    &Registration\r
+    );\r
 \r
---*/\r
-{\r
-  return NetLibInstallAllDriverProtocols (\r
+  return EfiLibInstallDriverBindingComponentName2 (\r
            ImageHandle,\r
            SystemTable,\r
            &gIp4DriverBinding,\r
            ImageHandle,\r
            &gIp4ComponentName,\r
-           NULL,\r
-           NULL\r
+           &gIp4ComponentName2\r
            );\r
 }\r
 \r
-\r
 /**\r
-  Test to see if this driver supports ControllerHandle.\r
-\r
-  @param  This                   Protocol instance pointer.\r
-  @param  ControllerHandle       Handle of device to test\r
-  @param  RemainingDevicePath    Optional parameter use to pick a specific child\r
-                                 device to start.\r
-\r
-  @retval EFI_SUCCES             This driver supports this device\r
-  @retval EFI_ALREADY_STARTED    This driver is already running on this device\r
-  @retval other                  This driver does not support this device\r
+  Test to see if this driver supports ControllerHandle. This service\r
+  is called by the EFI boot service ConnectController(). In\r
+  order to make drivers as small as possible, there are a few calling\r
+  restrictions for this service. ConnectController() must\r
+  follow these calling restrictions. If any other agent wishes to call\r
+  Supported() it must also follow these calling restrictions.\r
+\r
+  @param[in]  This                Protocol instance pointer.\r
+  @param[in]  ControllerHandle    Handle of device to test\r
+  @param[in]  RemainingDevicePath Optional parameter use to pick a specific child\r
+                                  device to start.\r
+\r
+  @retval EFI_SUCCESS         This driver supports this device\r
+  @retval EFI_ALREADY_STARTED This driver is already running on this device\r
+  @retval other               This driver does not support this device\r
 \r
 **/\r
 EFI_STATUS\r
@@ -124,7 +152,19 @@ Ip4DriverBindingSupported (
   return Status;\r
 }\r
 \r
-STATIC\r
+/**\r
+  Clean up a IP4 service binding instance. It will release all\r
+  the resource allocated by the instance. The instance may be\r
+  partly initialized, or partly destroyed. If a resource is\r
+  destroyed, it is marked as that in case the destroy failed and\r
+  being called again later.\r
+\r
+  @param[in]  IpSb               The IP4 service binding instance to clean up\r
+\r
+  @retval EFI_SUCCESS            The resource used by the instance are cleaned up\r
+  @retval other                  Failed to clean up some of the resources.\r
+\r
+**/\r
 EFI_STATUS\r
 Ip4CleanService (\r
   IN IP4_SERVICE            *IpSb\r
@@ -132,19 +172,19 @@ Ip4CleanService (
 \r
 \r
 /**\r
-  Create a new IP4 driver service binding protocol\r
+  Create a new IP4 driver service binding private instance.\r
 \r
-  @param  Controller             The controller that has MNP service binding\r
-                                 installed\r
-  @param  ImageHandle            The IP4 driver's image handle\r
-  @param  Service                The variable to receive the newly created IP4\r
-                                 service.\r
+  @param  Controller         The controller that has MNP service binding\r
+                             installed\r
+  @param  ImageHandle        The IP4 driver's image handle\r
+  @param  Service            The variable to receive the newly created IP4\r
+                             service.\r
 \r
   @retval EFI_OUT_OF_RESOURCES   Failed to allocate some resource\r
   @retval EFI_SUCCESS            A new IP4 service binding private is created.\r
+  @retval other                  Other error occurs.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 Ip4CreateService (\r
   IN  EFI_HANDLE            Controller,\r
@@ -164,7 +204,7 @@ Ip4CreateService (
   // empty resources, so if any thing goes wrong when allocating\r
   // resources, Ip4CleanService can be called to clean it up.\r
   //\r
-  IpSb = NetAllocatePool (sizeof (IP4_SERVICE));\r
+  IpSb = AllocateZeroPool (sizeof (IP4_SERVICE));\r
 \r
   if (IpSb == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -174,19 +214,18 @@ Ip4CreateService (
   IpSb->ServiceBinding.CreateChild  = Ip4ServiceBindingCreateChild;\r
   IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;\r
   IpSb->State                       = IP4_SERVICE_UNSTARTED;\r
-  IpSb->InDestory                   = FALSE;\r
 \r
   IpSb->NumChildren                 = 0;\r
-  NetListInit (&IpSb->Children);\r
+  InitializeListHead (&IpSb->Children);\r
 \r
-  NetListInit (&IpSb->Interfaces);\r
+  InitializeListHead (&IpSb->Interfaces);\r
   IpSb->DefaultInterface            = NULL;\r
   IpSb->DefaultRouteTable           = NULL;\r
 \r
   Ip4InitAssembleTable (&IpSb->Assemble);\r
 \r
   IpSb->IgmpCtrl.Igmpv1QuerySeen    = 0;\r
-  NetListInit (&IpSb->IgmpCtrl.Groups);\r
+  InitializeListHead (&IpSb->IgmpCtrl.Groups);\r
 \r
   IpSb->Image                       = ImageHandle;\r
   IpSb->Controller                  = Controller;\r
@@ -205,16 +244,20 @@ Ip4CreateService (
   IpSb->MnpConfigData.EnableReceiveTimestamps   = FALSE;\r
   IpSb->MnpConfigData.DisableBackgroundPolling  = FALSE;\r
 \r
-  NetZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));\r
+  ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));\r
+\r
+  IpSb->Timer = NULL;\r
+  IpSb->ReconfigCheckTimer = NULL;\r
+\r
+  IpSb->ReconfigEvent = NULL;\r
 \r
-  IpSb->Timer                       = NULL;\r
-  IpSb->Ip4Config                   = NULL;\r
-  IpSb->DoneEvent                   = NULL;\r
-  IpSb->ReconfigEvent               = NULL;\r
+  IpSb->Reconfig = FALSE;\r
+\r
+  IpSb->MediaPresent = TRUE;\r
 \r
   //\r
   // Create various resources. First create the route table, timer\r
-  // event and MNP child. IGMP, interface's initialization depend\r
+  // event, ReconfigEvent and MNP child. IGMP, interface's initialization depend\r
   // on the MNP child.\r
   //\r
   IpSb->DefaultRouteTable = Ip4CreateRouteTable ();\r
@@ -236,6 +279,29 @@ Ip4CreateService (
     goto ON_ERROR;\r
   }\r
 \r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL | EVT_TIMER,\r
+                  TPL_CALLBACK,\r
+                  Ip4TimerReconfigChecking,\r
+                  IpSb,\r
+                  &IpSb->ReconfigCheckTimer\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  Ip4AutoReconfigCallBack,\r
+                  IpSb,\r
+                  &IpSb->ReconfigEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+\r
   Status = NetLibCreateServiceChild (\r
              Controller,\r
              ImageHandle,\r
@@ -278,6 +344,13 @@ Ip4CreateService (
     goto ON_ERROR;\r
   }\r
 \r
+  IpSb->MacString = NULL;\r
+  Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+\r
   IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);\r
 \r
   if (IpSb->DefaultInterface == NULL) {\r
@@ -285,16 +358,31 @@ Ip4CreateService (
     goto ON_ERROR;\r
   }\r
 \r
-  NetListInsertHead (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);\r
+  InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);\r
 \r
-  IpSb->MacString = NULL;\r
+  ZeroMem (&IpSb->Ip4Config2Instance, sizeof (IP4_CONFIG2_INSTANCE));\r
+\r
+  Status = Ip4Config2InitInstance (&IpSb->Ip4Config2Instance);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
 \r
+  IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);\r
+  if (NetLibGetVlanId (IpSb->Controller) != 0) {\r
+    //\r
+    // This is a VLAN device, reduce MTU by VLAN tag length\r
+    //\r
+    IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;\r
+  }\r
+  IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;\r
   *Service = IpSb;\r
+\r
   return EFI_SUCCESS;\r
 \r
 ON_ERROR:\r
   Ip4CleanService (IpSb);\r
-  NetFreePool (IpSb);\r
+  FreePool (IpSb);\r
 \r
   return Status;\r
 }\r
@@ -303,14 +391,14 @@ ON_ERROR:
 /**\r
   Clean up a IP4 service binding instance. It will release all\r
   the resource allocated by the instance. The instance may be\r
-  partly initialized, or partly destoried. If a resource is\r
-  destoried, it is marked as that in case the destory failed and\r
+  partly initialized, or partly destroyed. If a resource is\r
+  destroyed, it is marked as that in case the destroy failed and\r
   being called again later.\r
 \r
-  @param  IpSb                   The IP4 serviceing binding instance to clean up\r
+  @param[in]  IpSb               The IP4 service binding instance to clean up\r
 \r
   @retval EFI_SUCCESS            The resource used by the instance are cleaned up\r
-  @retval Others                 Failed to clean up some of the resources.\r
+  @retval other                  Failed to clean up some of the resources.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -320,6 +408,22 @@ Ip4CleanService (
 {\r
   EFI_STATUS                Status;\r
 \r
+  IpSb->State     = IP4_SERVICE_DESTROY;\r
+\r
+  if (IpSb->Timer != NULL) {\r
+    gBS->SetTimer (IpSb->Timer, TimerCancel, 0);\r
+    gBS->CloseEvent (IpSb->Timer);\r
+\r
+    IpSb->Timer = NULL;\r
+  }\r
+\r
+  if (IpSb->ReconfigCheckTimer != NULL) {\r
+    gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerCancel, 0);\r
+    gBS->CloseEvent (IpSb->ReconfigCheckTimer);\r
+\r
+    IpSb->ReconfigCheckTimer = NULL;\r
+  }\r
+\r
   if (IpSb->DefaultInterface != NULL) {\r
     Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);\r
 \r
@@ -338,13 +442,13 @@ Ip4CleanService (
   Ip4CleanAssembleTable (&IpSb->Assemble);\r
 \r
   if (IpSb->MnpChildHandle != NULL) {\r
-    if (IpSb->Mnp) {\r
+    if (IpSb->Mnp != NULL) {\r
       gBS->CloseProtocol (\r
-            IpSb->MnpChildHandle,\r
-            &gEfiManagedNetworkProtocolGuid,\r
-            IpSb->Image,\r
-            IpSb->Controller\r
-            );\r
+             IpSb->MnpChildHandle,\r
+             &gEfiManagedNetworkProtocolGuid,\r
+             IpSb->Image,\r
+             IpSb->Controller\r
+             );\r
 \r
       IpSb->Mnp = NULL;\r
     }\r
@@ -359,55 +463,96 @@ Ip4CleanService (
     IpSb->MnpChildHandle = NULL;\r
   }\r
 \r
-  if (IpSb->Timer != NULL) {\r
-    gBS->SetTimer (IpSb->Timer, TimerCancel, 0);\r
-    gBS->CloseEvent (IpSb->Timer);\r
+  if (IpSb->ReconfigEvent != NULL) {\r
+    gBS->CloseEvent (IpSb->ReconfigEvent);\r
 \r
-    IpSb->Timer = NULL;\r
+    IpSb->ReconfigEvent = NULL;\r
   }\r
 \r
-  if (IpSb->Ip4Config != NULL) {\r
-    IpSb->Ip4Config->Stop (IpSb->Ip4Config);\r
+  IpSb->Reconfig = FALSE;\r
 \r
-    gBS->CloseProtocol (\r
-          IpSb->Controller,\r
-          &gEfiIp4ConfigProtocolGuid,\r
-          IpSb->Image,\r
-          IpSb->Controller\r
-          );\r
-\r
-    gBS->CloseEvent (IpSb->DoneEvent);\r
-    gBS->CloseEvent (IpSb->ReconfigEvent);\r
-    IpSb->Ip4Config = NULL;\r
+  if (IpSb->MacString != NULL) {\r
+    FreePool (IpSb->MacString);\r
   }\r
 \r
+  Ip4Config2CleanInstance (&IpSb->Ip4Config2Instance);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
-  Start this driver on ControllerHandle.\r
+  Callback function which provided by user to remove one node in NetDestroyLinkList process.\r
 \r
-  @param  This                   Protocol instance pointer.\r
-  @param  ControllerHandle       Handle of device to bind driver to\r
-  @param  RemainingDevicePath    Optional parameter use to pick a specific child\r
-                                 device to start.\r
+  @param[in]    Entry           The entry to be removed.\r
+  @param[in]    Context         Pointer to the callback context corresponds to the Context in NetDestroyLinkList.\r
 \r
-  @retval EFI_SUCCES             This driver is added to ControllerHandle\r
-  @retval EFI_ALREADY_STARTED    This driver is already running on ControllerHandle\r
-  @retval other                  This driver does not support this device\r
+  @retval EFI_SUCCESS           The entry has been removed successfully.\r
+  @retval Others                Fail to remove the entry.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Ip4DestroyChildEntryInHandleBuffer (\r
+  IN LIST_ENTRY         *Entry,\r
+  IN VOID               *Context\r
+  )\r
+{\r
+  IP4_PROTOCOL                  *IpInstance;\r
+  EFI_SERVICE_BINDING_PROTOCOL  *ServiceBinding;\r
+  UINTN                         NumberOfChildren;\r
+  EFI_HANDLE                    *ChildHandleBuffer;\r
+\r
+  if (Entry == NULL || Context == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
+  ServiceBinding    = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;\r
+  NumberOfChildren  = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;\r
+  ChildHandleBuffer = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;\r
+\r
+  if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);\r
+}\r
+\r
+/**\r
+  Start this driver on ControllerHandle. This service is called by the\r
+  EFI boot service ConnectController(). In order to make\r
+  drivers as small as possible, there are a few calling restrictions for\r
+  this service. ConnectController() must follow these\r
+  calling restrictions. If any other agent wishes to call Start() it\r
+  must also follow these calling restrictions.\r
+\r
+  @param[in]  This                 Protocol instance pointer.\r
+  @param[in]  ControllerHandle     Handle of device to bind driver to\r
+  @param[in]  RemainingDevicePath  Optional parameter use to pick a specific child\r
+                                   device to start.\r
+\r
+  @retval EFI_SUCCESS          This driver is added to ControllerHandle\r
+  @retval EFI_ALREADY_STARTED  This driver is already running on ControllerHandle\r
+  @retval other                This driver does not support this device\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 Ip4DriverBindingStart (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL  * This,\r
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,\r
   IN EFI_HANDLE                   ControllerHandle,\r
-  IN EFI_DEVICE_PATH_PROTOCOL     * RemainingDevicePath OPTIONAL\r
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL\r
   )\r
 {\r
-  IP4_SERVICE               *IpSb;\r
-  EFI_STATUS                Status;\r
+  EFI_STATUS                    Status;\r
+  IP4_SERVICE                   *IpSb;\r
+  EFI_IP4_CONFIG2_PROTOCOL      *Ip4Cfg2;\r
+  UINTN                         Index;\r
+  IP4_CONFIG2_DATA_ITEM         *DataItem;\r
+\r
+  IpSb     = NULL;\r
+  Ip4Cfg2  = NULL;\r
+  DataItem = NULL;\r
 \r
   //\r
   // Test for the Ip4 service binding protocol\r
@@ -431,6 +576,10 @@ Ip4DriverBindingStart (
     return Status;\r
   }\r
 \r
+  ASSERT (IpSb != NULL);\r
+\r
+  Ip4Cfg2  = &IpSb->Ip4Config2Instance.Ip4Config2;\r
+\r
   //\r
   // Install the Ip4ServiceBinding Protocol onto ControlerHandle\r
   //\r
@@ -438,6 +587,8 @@ Ip4DriverBindingStart (
                   &ControllerHandle,\r
                   &gEfiIp4ServiceBindingProtocolGuid,\r
                   &IpSb->ServiceBinding,\r
+                  &gEfiIp4Config2ProtocolGuid,\r
+                  Ip4Cfg2,\r
                   NULL\r
                   );\r
 \r
@@ -446,11 +597,44 @@ Ip4DriverBindingStart (
   }\r
 \r
   //\r
-  // ready to go: start the receiving and timer\r
+  // Read the config data from NV variable again.\r
+  // The default data can be changed by other drivers.\r
+  //\r
+  Status = Ip4Config2ReadConfigData (IpSb->MacString, &IpSb->Ip4Config2Instance);\r
+  if (EFI_ERROR (Status)) {\r
+    goto UNINSTALL_PROTOCOL;\r
+  }\r
+\r
+  //\r
+  // Consume the installed EFI_IP4_CONFIG2_PROTOCOL to set the default data items.\r
+  //\r
+  for (Index = Ip4Config2DataTypePolicy; Index < Ip4Config2DataTypeMaximum; Index++) {\r
+    DataItem = &IpSb->Ip4Config2Instance.DataItem[Index];\r
+    if (DataItem->Data.Ptr != NULL) {\r
+      Status = Ip4Cfg2->SetData (\r
+                          Ip4Cfg2,\r
+                          Index,\r
+                          DataItem->DataSize,\r
+                          DataItem->Data.Ptr\r
+                          );\r
+      if (EFI_ERROR(Status)) {\r
+        goto UNINSTALL_PROTOCOL;\r
+      }\r
+\r
+      if (Index == Ip4Config2DataTypePolicy && (*(DataItem->Data.Policy) == Ip4Config2PolicyDhcp)) {\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Ready to go: start the receiving and timer.\r
+  // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after\r
+  // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status.\r
   //\r
   Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r
 \r
-  if (EFI_ERROR (Status)) {\r
+  if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
     goto UNINSTALL_PROTOCOL;\r
   }\r
 \r
@@ -460,41 +644,52 @@ Ip4DriverBindingStart (
     goto UNINSTALL_PROTOCOL;\r
   }\r
 \r
+  Status = gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerPeriodic, 500 * TICKS_PER_MS);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto UNINSTALL_PROTOCOL;\r
+  }\r
+\r
   //\r
   // Initialize the IP4 ID\r
   //\r
   mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());\r
 \r
-  Ip4SetVariableData (IpSb);\r
-\r
   return Status;\r
 \r
 UNINSTALL_PROTOCOL:\r
-  gBS->UninstallProtocolInterface (\r
+  gBS->UninstallMultipleProtocolInterfaces (\r
          ControllerHandle,\r
          &gEfiIp4ServiceBindingProtocolGuid,\r
-         &IpSb->ServiceBinding\r
+         &IpSb->ServiceBinding,\r
+         &gEfiIp4Config2ProtocolGuid,\r
+         Ip4Cfg2,\r
+         NULL\r
          );\r
 \r
 FREE_SERVICE:\r
   Ip4CleanService (IpSb);\r
-  NetFreePool (IpSb);\r
-\r
+  FreePool (IpSb);\r
   return Status;\r
 }\r
 \r
 \r
 /**\r
-  Stop this driver on ControllerHandle.\r
-\r
-  @param  This                   Protocol instance pointer.\r
-  @param  ControllerHandle       Handle of device to stop driver on\r
-  @param  NumberOfChildren       Number of Handles in ChildHandleBuffer. If  number\r
-                                 of children is zero stop the entire  bus driver.\r
-  @param  ChildHandleBuffer      List of Child Handles to Stop.\r
-\r
-  @retval EFI_SUCCES             This driver is removed ControllerHandle\r
-  @retval other                  This driver was not removed from this device\r
+  Stop this driver on ControllerHandle. This service is called by the\r
+  EFI boot service DisconnectController(). In order to\r
+  make drivers as small as possible, there are a few calling\r
+  restrictions for this service. DisconnectController()\r
+  must follow these calling restrictions. If any other agent wishes\r
+  to call Stop() it must also follow these calling restrictions.\r
+\r
+  @param[in]  This              Protocol instance pointer.\r
+  @param[in]  ControllerHandle  Handle of device to stop driver on\r
+  @param[in]  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number\r
+                                of children is zero stop the entire bus driver.\r
+  @param[in]  ChildHandleBuffer List of Child Handles to Stop.\r
+\r
+  @retval EFI_SUCCESS           This driver is removed ControllerHandle\r
+  @retval other                 This driver was not removed from this device\r
 \r
 **/\r
 EFI_STATUS\r
@@ -506,108 +701,33 @@ Ip4DriverBindingStop (
   IN  EFI_HANDLE                   *ChildHandleBuffer\r
   )\r
 {\r
-  EFI_SERVICE_BINDING_PROTOCOL  *ServiceBinding;\r
-  IP4_SERVICE                   *IpSb;\r
-  IP4_PROTOCOL                  *IpInstance;\r
-  EFI_HANDLE                    NicHandle;\r
-  EFI_STATUS                    Status;\r
-  EFI_TPL                       OldTpl;\r
-  INTN                          State;\r
+  EFI_SERVICE_BINDING_PROTOCOL             *ServiceBinding;\r
+  IP4_SERVICE                              *IpSb;\r
+  EFI_HANDLE                               NicHandle;\r
+  EFI_STATUS                               Status;\r
+  INTN                                     State;\r
+  LIST_ENTRY                               *List;\r
+  IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT  Context;\r
+  IP4_INTERFACE                            *IpIf;\r
+  IP4_ROUTE_TABLE                          *RouteTable;\r
 \r
-  //\r
-  // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol\r
-  // by driver. So the ControllerHandle may be the MNP child handle, ARP child\r
-  // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed\r
-  // in the NIC handle.\r
-  //\r
-  //\r
-  // First, check whether it is the IP4_CONFIG protocol being uninstalled.\r
-  // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary\r
-  // to clean up the default configuration if IP4_CONFIG is being stopped.\r
-  //\r
-  Status = gBS->OpenProtocol (\r
-                  ControllerHandle,\r
-                  &gEfiIp4ConfigProtocolGuid,\r
-                  NULL,\r
-                  This->DriverBindingHandle,\r
-                  ControllerHandle,\r
-                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
-                  );\r
+  BOOLEAN                                  IsDhcp4;\r
 \r
-  if (Status == EFI_SUCCESS) {\r
-    //\r
-    // Retrieve the IP4 service binding protocol. If failed, it is\r
-    // likely that Ip4 ServiceBinding is uninstalled already. In this\r
-    // case, return immediately.\r
-    //\r
-    Status = gBS->OpenProtocol (\r
-                    ControllerHandle,\r
-                    &gEfiIp4ServiceBindingProtocolGuid,\r
-                    (VOID **) &ServiceBinding,\r
-                    This->DriverBindingHandle,\r
-                    ControllerHandle,\r
-                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-                    );\r
+  IsDhcp4   = FALSE;\r
 \r
-    if (EFI_ERROR (Status)) {\r
-      return EFI_SUCCESS;\r
-    }\r
-\r
-    IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);\r
-\r
-    OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
-\r
-    if (IpSb->Ip4Config && (IpSb->State != IP4_SERVICE_DESTORY)) {\r
-\r
-      IpSb->Ip4Config->Stop (IpSb->Ip4Config);\r
-\r
-      Status = gBS->CloseProtocol (\r
-                      ControllerHandle,\r
-                      &gEfiIp4ConfigProtocolGuid,\r
-                      IpSb->Image,\r
-                      ControllerHandle\r
-                      );\r
-\r
-      if (EFI_ERROR (Status)) {\r
-        NET_RESTORE_TPL (OldTpl);\r
-        return Status;\r
-      }\r
-\r
-      //\r
-      // If the auto configure hasn't complete, mark it as not started.\r
-      //\r
-      if (IpSb->State == IP4_SERVICE_STARTED) {\r
-        IpSb->State = IP4_SERVICE_UNSTARTED;\r
-      }\r
-\r
-      IpSb->Ip4Config = NULL;\r
-      gBS->CloseEvent (IpSb->DoneEvent);\r
-      gBS->CloseEvent (IpSb->ReconfigEvent);\r
-    }\r
-\r
-    NET_RESTORE_TPL (OldTpl);\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Either MNP or ARP protocol is being uninstalled. The controller\r
-  // handle is either the MNP child or ARP child. But, the IP4's\r
-  // service binding is installed on the NIC handle. So, need to open\r
-  // the protocol info to find the NIC handle.\r
-  //\r
   NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);\r
-\r
   if (NicHandle == NULL) {\r
     NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);\r
+    if (NicHandle == NULL) {\r
+      NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);\r
+      if (NicHandle != NULL) {\r
+        IsDhcp4 = TRUE;\r
+      } else {\r
+        return EFI_SUCCESS;\r
+      }\r
+    }\r
   }\r
 \r
-  if (NicHandle == NULL) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Retrieve the IP4 service binding protocol\r
-  //\r
   Status = gBS->OpenProtocol (\r
                   NicHandle,\r
                   &gEfiIp4ServiceBindingProtocolGuid,\r
@@ -616,101 +736,111 @@ Ip4DriverBindingStop (
                   NicHandle,\r
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                   );\r
-\r
   if (EFI_ERROR (Status)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  IpSb   = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);\r
+  IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);\r
+\r
+  if (IsDhcp4) {\r
+    Status = Ip4Config2DestroyDhcp4 (&IpSb->Ip4Config2Instance);\r
+    gBS->CloseEvent (IpSb->Ip4Config2Instance.Dhcp4Event);\r
+    IpSb->Ip4Config2Instance.Dhcp4Event = NULL;\r
+  } else if (NumberOfChildren != 0) {\r
+    List = &IpSb->Children;\r
+    Context.ServiceBinding    = ServiceBinding;\r
+    Context.NumberOfChildren  = NumberOfChildren;\r
+    Context.ChildHandleBuffer = ChildHandleBuffer;\r
+    Status = NetDestroyLinkList (\r
+               List,\r
+               Ip4DestroyChildEntryInHandleBuffer,\r
+               &Context,\r
+               NULL\r
+               );\r
+  } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {\r
 \r
-  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
-\r
-  if (IpSb->InDestory) {\r
-    NET_RESTORE_TPL (OldTpl);\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  IpSb->InDestory = TRUE;\r
-\r
-  State           = IpSb->State;\r
-  IpSb->State     = IP4_SERVICE_DESTORY;\r
-\r
-  //\r
-  // Destory all the children first. If not all children are destoried,\r
-  // the IP driver can operate correctly, so restore it state. Don't\r
-  // use NET_LIST_FOR_EACH_SAFE here, because it will cache the next\r
-  // pointer, which may point to the child that has already been destoried.\r
-  // For example, if there are two child in the list, the first is UDP\r
-  // listen child, the send is the MTFTP's child. When Udp child is\r
-  // destoried, it will destory the MTFTP's child. Then Next point to\r
-  // a invalid child.\r
-  //\r
-  while (!NetListIsEmpty (&IpSb->Children)) {\r
-    IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);\r
-    Ip4ServiceBindingDestroyChild (ServiceBinding, IpInstance->Handle);\r
-  }\r
+    //\r
+    // The ARP protocol for the default interface is being uninstalled and all\r
+    // its IP child handles should have been destroyed before. So, release the\r
+    // default interface and route table, create a new one and mark it as not started.\r
+    //\r
+    Ip4CancelReceive (IpSb->DefaultInterface);\r
+    Ip4FreeInterface (IpSb->DefaultInterface, NULL);\r
+    Ip4FreeRouteTable (IpSb->DefaultRouteTable);\r
 \r
-  if (IpSb->NumChildren != 0) {\r
-    IpSb->State = State;\r
-    Status      = EFI_DEVICE_ERROR;\r
-    goto ON_ERROR;\r
-  }\r
+    IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
+    if (IpIf == NULL) {\r
+      goto ON_ERROR;\r
+    }\r
+    RouteTable = Ip4CreateRouteTable ();\r
+    if (RouteTable == NULL) {\r
+      Ip4FreeInterface (IpIf, NULL);\r
+      goto ON_ERROR;;\r
+    }\r
 \r
-  //\r
-  // Clear the variable data.\r
-  //\r
-  Ip4ClearVariableData (IpSb);\r
+    IpSb->DefaultInterface  = IpIf;\r
+    InsertHeadList (&IpSb->Interfaces, &IpIf->Link);\r
+    IpSb->DefaultRouteTable = RouteTable;\r
+    Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);\r
 \r
-  //\r
-  // OK, clean other resources then uninstall the service binding protocol.\r
-  //\r
-  Status = Ip4CleanService (IpSb);\r
+    IpSb->State = IP4_SERVICE_UNSTARTED;\r
 \r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
-  }\r
+  } else if (IsListEmpty (&IpSb->Children)) {\r
+    State           = IpSb->State;\r
+    //\r
+    // OK, clean other resources then uninstall the service binding protocol.\r
+    //\r
+    Status = Ip4CleanService (IpSb);\r
+    if (EFI_ERROR (Status)) {\r
+      IpSb->State = State;\r
+      goto ON_ERROR;\r
+    }\r
 \r
-  Status = gBS->UninstallProtocolInterface (\r
-                  NicHandle,\r
-                  &gEfiIp4ServiceBindingProtocolGuid,\r
-                  ServiceBinding\r
-                  );\r
+    gBS->UninstallMultipleProtocolInterfaces (\r
+           NicHandle,\r
+           &gEfiIp4ServiceBindingProtocolGuid,\r
+           ServiceBinding,\r
+           &gEfiIp4Config2ProtocolGuid,\r
+           &IpSb->Ip4Config2Instance.Ip4Config2,\r
+           NULL\r
+           );\r
 \r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
+    if (gIp4ControllerNameTable != NULL) {\r
+      FreeUnicodeStringTable (gIp4ControllerNameTable);\r
+      gIp4ControllerNameTable = NULL;\r
+    }\r
+    FreePool (IpSb);\r
   }\r
 \r
-  NET_RESTORE_TPL (OldTpl);\r
-  NetFreePool (IpSb);\r
-  return EFI_SUCCESS;\r
-\r
 ON_ERROR:\r
-  IpSb->InDestory = FALSE;\r
-  NET_RESTORE_TPL (OldTpl);\r
   return Status;\r
 }\r
 \r
 \r
 /**\r
-  Creates a child handle with a set of I/O services.\r
+  Creates a child handle and installs a protocol.\r
 \r
-  @param  This                   Protocol instance pointer.\r
-  @param  ChildHandle            Pointer to the handle of the child to create.   If\r
-                                 it is NULL, then a new handle is created.   If it\r
-                                 is not NULL, then the I/O services are  added to\r
-                                 the existing child handle.\r
+  The CreateChild() function installs a protocol on ChildHandle.\r
+  If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r
+  If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
 \r
-  @retval EFI_SUCCES             The child handle was created with the I/O services\r
-  @retval EFI_OUT_OF_RESOURCES   There are not enough resources availabe to create\r
-                                 the child\r
-  @retval other                  The child handle was not created\r
+  @param  This        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
+  @param  ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
+                      then a new handle is created. If it is a pointer to an existing UEFI handle,\r
+                      then the protocol is added to the existing UEFI handle.\r
+\r
+  @retval EFI_SUCCES            The protocol was added to ChildHandle.\r
+  @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES  There are not enough resources available to create\r
+                                the child\r
+  @retval other                 The child handle was not created\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 Ip4ServiceBindingCreateChild (\r
   IN EFI_SERVICE_BINDING_PROTOCOL  *This,\r
-  IN EFI_HANDLE                    *ChildHandle\r
+  IN OUT EFI_HANDLE                *ChildHandle\r
   )\r
 {\r
   IP4_SERVICE               *IpSb;\r
@@ -724,7 +854,7 @@ Ip4ServiceBindingCreateChild (
   }\r
 \r
   IpSb       = IP4_SERVICE_FROM_PROTOCOL (This);\r
-  IpInstance = NetAllocatePool (sizeof (IP4_PROTOCOL));\r
+  IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));\r
 \r
   if (IpInstance == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -773,12 +903,12 @@ Ip4ServiceBindingCreateChild (
   //\r
   // Insert it into the service binding instance.\r
   //\r
-  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
-  NetListInsertTail (&IpSb->Children, &IpInstance->Link);\r
+  InsertTailList (&IpSb->Children, &IpInstance->Link);\r
   IpSb->NumChildren++;\r
 \r
-  NET_RESTORE_TPL (OldTpl);\r
+  gBS->RestoreTPL (OldTpl);\r
 \r
 ON_ERROR:\r
 \r
@@ -786,7 +916,7 @@ ON_ERROR:
 \r
     Ip4CleanProtocol (IpInstance);\r
 \r
-    NetFreePool (IpInstance);\r
+    FreePool (IpInstance);\r
   }\r
 \r
   return Status;\r
@@ -794,19 +924,21 @@ ON_ERROR:
 \r
 \r
 /**\r
-  Destroys a child handle with a set of I/O services.\r
+  Destroys a child handle with a protocol installed on it.\r
+\r
+  The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r
+  that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r
+  last protocol on ChildHandle, then ChildHandle is destroyed.\r
 \r
-  @param  This                   Protocol instance pointer.\r
-  @param  ChildHandle            Handle of the child to destroy\r
+  @param  This        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
+  @param  ChildHandle Handle of the child to destroy\r
 \r
-  @retval EFI_SUCCES             The I/O services were removed from the child\r
-                                 handle\r
-  @retval EFI_UNSUPPORTED        The child handle does not support the I/O services\r
-                                  that are being removed\r
-  @retval EFI_INVALID_PARAMETER  Child handle is not a valid EFI Handle.\r
-  @retval EFI_ACCESS_DENIED      The child handle could not be destroyed because\r
-                                 its  I/O services are being used.\r
-  @retval other                  The child handle was not destroyed\r
+  @retval EFI_SUCCES            The protocol was removed from ChildHandle.\r
+  @retval EFI_UNSUPPORTED       ChildHandle does not support the protocol that is being removed.\r
+  @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
+  @retval EFI_ACCESS_DENIED     The protocol could not be removed from the ChildHandle\r
+                                because its services are being used.\r
+  @retval other                 The child handle was not destroyed\r
 \r
 **/\r
 EFI_STATUS\r
@@ -821,7 +953,6 @@ Ip4ServiceBindingDestroyChild (
   IP4_PROTOCOL              *IpInstance;\r
   EFI_IP4_PROTOCOL          *Ip4;\r
   EFI_TPL                   OldTpl;\r
-  INTN                      State;\r
 \r
   if ((This == NULL) || (ChildHandle == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -851,21 +982,20 @@ Ip4ServiceBindingDestroyChild (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
   //\r
-  // A child can be destoried more than once. For example,\r
-  // Ip4DriverBindingStop will destory all of its children.\r
-  // when UDP driver is being stopped, it will destory all\r
+  // A child can be destroyed more than once. For example,\r
+  // Ip4DriverBindingStop will destroy all of its children.\r
+  // when UDP driver is being stopped, it will destroy all\r
   // the IP child it opens.\r
   //\r
-  if (IpInstance->State == IP4_STATE_DESTORY) {\r
-    NET_RESTORE_TPL (OldTpl);\r
+  if (IpInstance->InDestroy) {\r
+    gBS->RestoreTPL (OldTpl);\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  State             = IpInstance->State;\r
-  IpInstance->State = IP4_STATE_DESTORY;\r
+  IpInstance->InDestroy = TRUE;\r
 \r
   //\r
   // Close the Managed Network protocol.\r
@@ -877,6 +1007,15 @@ Ip4ServiceBindingDestroyChild (
          ChildHandle\r
          );\r
 \r
+  if (IpInstance->Interface != NULL && IpInstance->Interface->Arp != NULL) {\r
+    gBS->CloseProtocol (\r
+           IpInstance->Interface->ArpHandle,\r
+           &gEfiArpProtocolGuid,\r
+           gIp4DriverBinding.DriverBindingHandle,\r
+           ChildHandle\r
+           );\r
+  }\r
+\r
   //\r
   // Uninstall the IP4 protocol first. Many thing happens during\r
   // this:\r
@@ -891,20 +1030,19 @@ Ip4ServiceBindingDestroyChild (
   // will be called back before preceeding. If any packets not recycled,\r
   // that means there is a resource leak.\r
   //\r
+  gBS->RestoreTPL (OldTpl);\r
   Status = gBS->UninstallProtocolInterface (\r
                   ChildHandle,\r
                   &gEfiIp4ProtocolGuid,\r
                   &IpInstance->Ip4Proto\r
                   );\r
-\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
   if (EFI_ERROR (Status)) {\r
+    IpInstance->InDestroy = FALSE;\r
     goto ON_ERROR;\r
   }\r
 \r
   Status = Ip4CleanProtocol (IpInstance);\r
-\r
-  Ip4SetVariableData (IpSb);\r
-\r
   if (EFI_ERROR (Status)) {\r
     gBS->InstallMultipleProtocolInterfaces (\r
            &ChildHandle,\r
@@ -916,17 +1054,16 @@ Ip4ServiceBindingDestroyChild (
     goto ON_ERROR;\r
   }\r
 \r
-  NetListRemoveEntry (&IpInstance->Link);\r
+  RemoveEntryList (&IpInstance->Link);\r
   IpSb->NumChildren--;\r
 \r
-  NET_RESTORE_TPL (OldTpl);\r
+  gBS->RestoreTPL (OldTpl);\r
 \r
-  NetFreePool (IpInstance);\r
+  FreePool (IpInstance);\r
   return EFI_SUCCESS;\r
 \r
 ON_ERROR:\r
-  IpInstance->State = State;\r
-  NET_RESTORE_TPL (OldTpl);\r
+  gBS->RestoreTPL (OldTpl);\r
 \r
   return Status;\r
 }\r