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