]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
NetworkPkg: Move Network library and drivers from MdeModulePkg to NetworkPkg
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Driver.c
diff --git a/MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c b/MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
deleted file mode 100644 (file)
index ebd4dec..0000000
+++ /dev/null
@@ -1,1069 +0,0 @@
-/** @file\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
-#include "Ip4Impl.h"\r
-\r
-EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {\r
-  Ip4DriverBindingSupported,\r
-  Ip4DriverBindingStart,\r
-  Ip4DriverBindingStop,\r
-  0xa,\r
-  NULL,\r
-  NULL\r
-};\r
-\r
-BOOLEAN  mIpSec2Installed = FALSE;\r
-\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
-IpSec2InstalledCallback (\r
-  IN EFI_EVENT  Event,\r
-  IN VOID       *Context\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
-    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
-  @param[in]  ImageHandle           The firmware allocated handle for the UEFI image.\r
-  @param[in]  SystemTable           A pointer to the EFI System Table.\r
-\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
-**/\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
-  EfiCreateProtocolNotifyEvent (\r
-    &gEfiIpSec2ProtocolGuid,\r
-    TPL_CALLBACK,\r
-    IpSec2InstalledCallback,\r
-    NULL,\r
-    &Registration\r
-    );\r
-\r
-  return EfiLibInstallDriverBindingComponentName2 (\r
-           ImageHandle,\r
-           SystemTable,\r
-           &gIp4DriverBinding,\r
-           ImageHandle,\r
-           &gIp4ComponentName,\r
-           &gIp4ComponentName2\r
-           );\r
-}\r
-\r
-/**\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
-EFIAPI\r
-Ip4DriverBindingSupported (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL  * This,\r
-  IN EFI_HANDLE                   ControllerHandle,\r
-  IN EFI_DEVICE_PATH_PROTOCOL     * RemainingDevicePath OPTIONAL\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-\r
-  //\r
-  // Test for the MNP service binding Protocol\r
-  //\r
-  Status = gBS->OpenProtocol (\r
-                  ControllerHandle,\r
-                  &gEfiManagedNetworkServiceBindingProtocolGuid,\r
-                  NULL,\r
-                  This->DriverBindingHandle,\r
-                  ControllerHandle,\r
-                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Test for the Arp service binding Protocol\r
-  //\r
-  Status = gBS->OpenProtocol (\r
-                  ControllerHandle,\r
-                  &gEfiArpServiceBindingProtocolGuid,\r
-                  NULL,\r
-                  This->DriverBindingHandle,\r
-                  ControllerHandle,\r
-                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
-                  );\r
-\r
-  return Status;\r
-}\r
-\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
-  );\r
-\r
-\r
-/**\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
-\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
-EFI_STATUS\r
-Ip4CreateService (\r
-  IN  EFI_HANDLE            Controller,\r
-  IN  EFI_HANDLE            ImageHandle,\r
-  OUT IP4_SERVICE           **Service\r
-  )\r
-{\r
-  IP4_SERVICE               *IpSb;\r
-  EFI_STATUS                Status;\r
-\r
-  ASSERT (Service != NULL);\r
-\r
-  *Service = NULL;\r
-\r
-  //\r
-  // allocate a service private data then initialize all the filed to\r
-  // empty resources, so if any thing goes wrong when allocating\r
-  // resources, Ip4CleanService can be called to clean it up.\r
-  //\r
-  IpSb = AllocateZeroPool (sizeof (IP4_SERVICE));\r
-\r
-  if (IpSb == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  IpSb->Signature                   = IP4_SERVICE_SIGNATURE;\r
-  IpSb->ServiceBinding.CreateChild  = Ip4ServiceBindingCreateChild;\r
-  IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;\r
-  IpSb->State                       = IP4_SERVICE_UNSTARTED;\r
-\r
-  IpSb->NumChildren                 = 0;\r
-  InitializeListHead (&IpSb->Children);\r
-\r
-  InitializeListHead (&IpSb->Interfaces);\r
-  IpSb->DefaultInterface            = NULL;\r
-  IpSb->DefaultRouteTable           = NULL;\r
-\r
-  Ip4InitAssembleTable (&IpSb->Assemble);\r
-\r
-  IpSb->IgmpCtrl.Igmpv1QuerySeen    = 0;\r
-  InitializeListHead (&IpSb->IgmpCtrl.Groups);\r
-\r
-  IpSb->Image                       = ImageHandle;\r
-  IpSb->Controller                  = Controller;\r
-\r
-  IpSb->MnpChildHandle              = NULL;\r
-  IpSb->Mnp                         = NULL;\r
-\r
-  IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0;\r
-  IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0;\r
-  IpSb->MnpConfigData.ProtocolTypeFilter        = IP4_ETHER_PROTO;\r
-  IpSb->MnpConfigData.EnableUnicastReceive      = TRUE;\r
-  IpSb->MnpConfigData.EnableMulticastReceive    = TRUE;\r
-  IpSb->MnpConfigData.EnableBroadcastReceive    = TRUE;\r
-  IpSb->MnpConfigData.EnablePromiscuousReceive  = FALSE;\r
-  IpSb->MnpConfigData.FlushQueuesOnReset        = TRUE;\r
-  IpSb->MnpConfigData.EnableReceiveTimestamps   = FALSE;\r
-  IpSb->MnpConfigData.DisableBackgroundPolling  = FALSE;\r
-\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->Reconfig = FALSE;\r
-\r
-  IpSb->MediaPresent = TRUE;\r
-\r
-  //\r
-  // Create various resources. First create the route table, timer\r
-  // event, ReconfigEvent and MNP child. IGMP, interface's initialization depend\r
-  // on the MNP child.\r
-  //\r
-  IpSb->DefaultRouteTable = Ip4CreateRouteTable ();\r
-\r
-  if (IpSb->DefaultRouteTable == NULL) {\r
-    Status = EFI_OUT_OF_RESOURCES;\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  Status = gBS->CreateEvent (\r
-                  EVT_NOTIFY_SIGNAL | EVT_TIMER,\r
-                  TPL_CALLBACK,\r
-                  Ip4TimerTicking,\r
-                  IpSb,\r
-                  &IpSb->Timer\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    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
-             &gEfiManagedNetworkServiceBindingProtocolGuid,\r
-             &IpSb->MnpChildHandle\r
-             );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  Status = gBS->OpenProtocol (\r
-                  IpSb->MnpChildHandle,\r
-                  &gEfiManagedNetworkProtocolGuid,\r
-                  (VOID **) &IpSb->Mnp,\r
-                  ImageHandle,\r
-                  Controller,\r
-                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  Status = Ip4ServiceConfigMnp (IpSb, TRUE);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  Status = Ip4InitIgmp (IpSb);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    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
-    Status = EFI_OUT_OF_RESOURCES;\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);\r
-\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
-  FreePool (IpSb);\r
-\r
-  return Status;\r
-}\r
-\r
-\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
-  )\r
-{\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
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    IpSb->DefaultInterface = NULL;\r
-  }\r
-\r
-  if (IpSb->DefaultRouteTable != NULL) {\r
-    Ip4FreeRouteTable (IpSb->DefaultRouteTable);\r
-    IpSb->DefaultRouteTable = NULL;\r
-  }\r
-\r
-  Ip4CleanAssembleTable (&IpSb->Assemble);\r
-\r
-  if (IpSb->MnpChildHandle != NULL) {\r
-    if (IpSb->Mnp != NULL) {\r
-      gBS->CloseProtocol (\r
-             IpSb->MnpChildHandle,\r
-             &gEfiManagedNetworkProtocolGuid,\r
-             IpSb->Image,\r
-             IpSb->Controller\r
-             );\r
-\r
-      IpSb->Mnp = NULL;\r
-    }\r
-\r
-    NetLibDestroyServiceChild (\r
-      IpSb->Controller,\r
-      IpSb->Image,\r
-      &gEfiManagedNetworkServiceBindingProtocolGuid,\r
-      IpSb->MnpChildHandle\r
-      );\r
-\r
-    IpSb->MnpChildHandle = NULL;\r
-  }\r
-\r
-  if (IpSb->ReconfigEvent != NULL) {\r
-    gBS->CloseEvent (IpSb->ReconfigEvent);\r
-\r
-    IpSb->ReconfigEvent = NULL;\r
-  }\r
-\r
-  IpSb->Reconfig = FALSE;\r
-\r
-  if (IpSb->MacString != NULL) {\r
-    FreePool (IpSb->MacString);\r
-  }\r
-\r
-  Ip4Config2CleanInstance (&IpSb->Ip4Config2Instance);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Callback function which provided by user to remove one node in NetDestroyLinkList process.\r
-\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_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_HANDLE                   ControllerHandle,\r
-  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL\r
-  )\r
-{\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
-  //\r
-  Status = gBS->OpenProtocol (\r
-                  ControllerHandle,\r
-                  &gEfiIp4ServiceBindingProtocolGuid,\r
-                  NULL,\r
-                  This->DriverBindingHandle,\r
-                  ControllerHandle,\r
-                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
-                  );\r
-\r
-  if (Status == EFI_SUCCESS) {\r
-    return EFI_ALREADY_STARTED;\r
-  }\r
-\r
-  Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    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
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &ControllerHandle,\r
-                  &gEfiIp4ServiceBindingProtocolGuid,\r
-                  &IpSb->ServiceBinding,\r
-                  &gEfiIp4Config2ProtocolGuid,\r
-                  Ip4Cfg2,\r
-                  NULL\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto FREE_SERVICE;\r
-  }\r
-\r
-  //\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) && Status != EFI_ALREADY_STARTED) {\r
-    goto UNINSTALL_PROTOCOL;\r
-  }\r
-\r
-  Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    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
-  return Status;\r
-\r
-UNINSTALL_PROTOCOL:\r
-  gBS->UninstallMultipleProtocolInterfaces (\r
-         ControllerHandle,\r
-         &gEfiIp4ServiceBindingProtocolGuid,\r
-         &IpSb->ServiceBinding,\r
-         &gEfiIp4Config2ProtocolGuid,\r
-         Ip4Cfg2,\r
-         NULL\r
-         );\r
-\r
-FREE_SERVICE:\r
-  Ip4CleanService (IpSb);\r
-  FreePool (IpSb);\r
-  return Status;\r
-}\r
-\r
-\r
-/**\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
-EFIAPI\r
-Ip4DriverBindingStop (\r
-  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
-  IN  EFI_HANDLE                   ControllerHandle,\r
-  IN  UINTN                        NumberOfChildren,\r
-  IN  EFI_HANDLE                   *ChildHandleBuffer\r
-  )\r
-{\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
-  BOOLEAN                                  IsDhcp4;\r
-\r
-  IsDhcp4   = FALSE;\r
-\r
-  NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);\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
-  Status = gBS->OpenProtocol (\r
-                  NicHandle,\r
-                  &gEfiIp4ServiceBindingProtocolGuid,\r
-                  (VOID **) &ServiceBinding,\r
-                  This->DriverBindingHandle,\r
-                  NicHandle,\r
-                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\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
-    //\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
-    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
-    IpSb->DefaultInterface  = IpIf;\r
-    InsertHeadList (&IpSb->Interfaces, &IpIf->Link);\r
-    IpSb->DefaultRouteTable = RouteTable;\r
-    Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);\r
-\r
-    IpSb->State = IP4_SERVICE_UNSTARTED;\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
-    gBS->UninstallMultipleProtocolInterfaces (\r
-           NicHandle,\r
-           &gEfiIp4ServiceBindingProtocolGuid,\r
-           ServiceBinding,\r
-           &gEfiIp4Config2ProtocolGuid,\r
-           &IpSb->Ip4Config2Instance.Ip4Config2,\r
-           NULL\r
-           );\r
-\r
-    if (gIp4ControllerNameTable != NULL) {\r
-      FreeUnicodeStringTable (gIp4ControllerNameTable);\r
-      gIp4ControllerNameTable = NULL;\r
-    }\r
-    FreePool (IpSb);\r
-  }\r
-\r
-ON_ERROR:\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Creates a child handle and installs a protocol.\r
-\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
-  @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 OUT EFI_HANDLE                *ChildHandle\r
-  )\r
-{\r
-  IP4_SERVICE               *IpSb;\r
-  IP4_PROTOCOL              *IpInstance;\r
-  EFI_TPL                   OldTpl;\r
-  EFI_STATUS                Status;\r
-  VOID                      *Mnp;\r
-\r
-  if ((This == NULL) || (ChildHandle == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  IpSb       = IP4_SERVICE_FROM_PROTOCOL (This);\r
-  IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));\r
-\r
-  if (IpInstance == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  Ip4InitProtocol (IpSb, IpInstance);\r
-\r
-  //\r
-  // Install Ip4 onto ChildHandle\r
-  //\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  ChildHandle,\r
-                  &gEfiIp4ProtocolGuid,\r
-                  &IpInstance->Ip4Proto,\r
-                  NULL\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  IpInstance->Handle = *ChildHandle;\r
-\r
-  //\r
-  // Open the Managed Network protocol BY_CHILD.\r
-  //\r
-  Status = gBS->OpenProtocol (\r
-                  IpSb->MnpChildHandle,\r
-                  &gEfiManagedNetworkProtocolGuid,\r
-                  (VOID **) &Mnp,\r
-                  gIp4DriverBinding.DriverBindingHandle,\r
-                  IpInstance->Handle,\r
-                  EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    gBS->UninstallMultipleProtocolInterfaces (\r
-           ChildHandle,\r
-           &gEfiIp4ProtocolGuid,\r
-           &IpInstance->Ip4Proto,\r
-           NULL\r
-           );\r
-\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  //\r
-  // Insert it into the service binding instance.\r
-  //\r
-  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
-\r
-  InsertTailList (&IpSb->Children, &IpInstance->Link);\r
-  IpSb->NumChildren++;\r
-\r
-  gBS->RestoreTPL (OldTpl);\r
-\r
-ON_ERROR:\r
-\r
-  if (EFI_ERROR (Status)) {\r
-\r
-    Ip4CleanProtocol (IpInstance);\r
-\r
-    FreePool (IpInstance);\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\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        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
-  @param  ChildHandle Handle of the child to destroy\r
-\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
-EFIAPI\r
-Ip4ServiceBindingDestroyChild (\r
-  IN EFI_SERVICE_BINDING_PROTOCOL  *This,\r
-  IN EFI_HANDLE                    ChildHandle\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  IP4_SERVICE               *IpSb;\r
-  IP4_PROTOCOL              *IpInstance;\r
-  EFI_IP4_PROTOCOL          *Ip4;\r
-  EFI_TPL                   OldTpl;\r
-\r
-  if ((This == NULL) || (ChildHandle == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Retrieve the private context data structures\r
-  //\r
-  IpSb   = IP4_SERVICE_FROM_PROTOCOL (This);\r
-\r
-  Status = gBS->OpenProtocol (\r
-                  ChildHandle,\r
-                  &gEfiIp4ProtocolGuid,\r
-                  (VOID **) &Ip4,\r
-                  gIp4DriverBinding.DriverBindingHandle,\r
-                  ChildHandle,\r
-                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
-  IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);\r
-\r
-  if (IpInstance->Service != IpSb) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
-\r
-  //\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->InDestroy) {\r
-    gBS->RestoreTPL (OldTpl);\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  IpInstance->InDestroy = TRUE;\r
-\r
-  //\r
-  // Close the Managed Network protocol.\r
-  //\r
-  gBS->CloseProtocol (\r
-         IpSb->MnpChildHandle,\r
-         &gEfiManagedNetworkProtocolGuid,\r
-         gIp4DriverBinding.DriverBindingHandle,\r
-         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
-  // 1. The consumer of the IP4 protocol will be stopped if it\r
-  // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is\r
-  // stopped, IP driver's stop function will be called, and uninstall\r
-  // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This\r
-  // makes it possible to create the network stack bottom up, and\r
-  // stop it top down.\r
-  // 2. the upper layer will recycle the received packet. The recycle\r
-  // event's TPL is higher than this function. The recycle events\r
-  // 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
-  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
-  if (EFI_ERROR (Status)) {\r
-    gBS->InstallMultipleProtocolInterfaces (\r
-           &ChildHandle,\r
-           &gEfiIp4ProtocolGuid,\r
-           Ip4,\r
-           NULL\r
-           );\r
-\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  RemoveEntryList (&IpInstance->Link);\r
-  IpSb->NumChildren--;\r
-\r
-  gBS->RestoreTPL (OldTpl);\r
-\r
-  FreePool (IpInstance);\r
-  return EFI_SUCCESS;\r
-\r
-ON_ERROR:\r
-  gBS->RestoreTPL (OldTpl);\r
-\r
-  return Status;\r
-}\r