2 The driver binding and service binding protocol for IP4 driver.
4 Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding
= {
18 Ip4DriverBindingSupported
,
19 Ip4DriverBindingStart
,
27 This is the declaration of an EFI image entry point. This entry point is
28 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
29 both device drivers and bus drivers.
31 The entry point for IP4 driver which install the driver
32 binding and component name protocol on its image.
34 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
35 @param[in] SystemTable A pointer to the EFI System Table.
37 @retval EFI_SUCCESS The operation completed successfully.
38 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
44 IN EFI_HANDLE ImageHandle
,
45 IN EFI_SYSTEM_TABLE
*SystemTable
48 return EfiLibInstallDriverBindingComponentName2 (
59 Test to see if this driver supports ControllerHandle. This service
60 is called by the EFI boot service ConnectController(). In
61 order to make drivers as small as possible, there are a few calling
62 restrictions for this service. ConnectController() must
63 follow these calling restrictions. If any other agent wishes to call
64 Supported() it must also follow these calling restrictions.
66 @param[in] This Protocol instance pointer.
67 @param[in] ControllerHandle Handle of device to test
68 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
71 @retval EFI_SUCCESS This driver supports this device
72 @retval EFI_ALREADY_STARTED This driver is already running on this device
73 @retval other This driver does not support this device
78 Ip4DriverBindingSupported (
79 IN EFI_DRIVER_BINDING_PROTOCOL
* This
,
80 IN EFI_HANDLE ControllerHandle
,
81 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
87 // Test for the MNP service binding Protocol
89 Status
= gBS
->OpenProtocol (
91 &gEfiManagedNetworkServiceBindingProtocolGuid
,
93 This
->DriverBindingHandle
,
95 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
98 if (EFI_ERROR (Status
)) {
103 // Test for the Arp service binding Protocol
105 Status
= gBS
->OpenProtocol (
107 &gEfiArpServiceBindingProtocolGuid
,
109 This
->DriverBindingHandle
,
111 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
118 Clean up a IP4 service binding instance. It will release all
119 the resource allocated by the instance. The instance may be
120 partly initialized, or partly destroyed. If a resource is
121 destroyed, it is marked as that in case the destroy failed and
122 being called again later.
124 @param[in] IpSb The IP4 service binding instance to clean up
126 @retval EFI_SUCCESS The resource used by the instance are cleaned up
127 @retval other Failed to clean up some of the resources.
137 Create a new IP4 driver service binding private instance.
139 @param Controller The controller that has MNP service binding
141 @param ImageHandle The IP4 driver's image handle
142 @param Service The variable to receive the newly created IP4
145 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
146 @retval EFI_SUCCESS A new IP4 service binding private is created.
147 @retval other Other error occurs.
152 IN EFI_HANDLE Controller
,
153 IN EFI_HANDLE ImageHandle
,
154 OUT IP4_SERVICE
**Service
160 ASSERT (Service
!= NULL
);
165 // allocate a service private data then initialize all the filed to
166 // empty resources, so if any thing goes wrong when allocating
167 // resources, Ip4CleanService can be called to clean it up.
169 IpSb
= AllocateZeroPool (sizeof (IP4_SERVICE
));
172 return EFI_OUT_OF_RESOURCES
;
175 IpSb
->Signature
= IP4_SERVICE_SIGNATURE
;
176 IpSb
->ServiceBinding
.CreateChild
= Ip4ServiceBindingCreateChild
;
177 IpSb
->ServiceBinding
.DestroyChild
= Ip4ServiceBindingDestroyChild
;
178 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
180 IpSb
->NumChildren
= 0;
181 InitializeListHead (&IpSb
->Children
);
183 InitializeListHead (&IpSb
->Interfaces
);
184 IpSb
->DefaultInterface
= NULL
;
185 IpSb
->DefaultRouteTable
= NULL
;
187 Ip4InitAssembleTable (&IpSb
->Assemble
);
189 IpSb
->IgmpCtrl
.Igmpv1QuerySeen
= 0;
190 InitializeListHead (&IpSb
->IgmpCtrl
.Groups
);
192 IpSb
->Image
= ImageHandle
;
193 IpSb
->Controller
= Controller
;
195 IpSb
->MnpChildHandle
= NULL
;
198 IpSb
->MnpConfigData
.ReceivedQueueTimeoutValue
= 0;
199 IpSb
->MnpConfigData
.TransmitQueueTimeoutValue
= 0;
200 IpSb
->MnpConfigData
.ProtocolTypeFilter
= IP4_ETHER_PROTO
;
201 IpSb
->MnpConfigData
.EnableUnicastReceive
= TRUE
;
202 IpSb
->MnpConfigData
.EnableMulticastReceive
= TRUE
;
203 IpSb
->MnpConfigData
.EnableBroadcastReceive
= TRUE
;
204 IpSb
->MnpConfigData
.EnablePromiscuousReceive
= FALSE
;
205 IpSb
->MnpConfigData
.FlushQueuesOnReset
= TRUE
;
206 IpSb
->MnpConfigData
.EnableReceiveTimestamps
= FALSE
;
207 IpSb
->MnpConfigData
.DisableBackgroundPolling
= FALSE
;
209 ZeroMem (&IpSb
->SnpMode
, sizeof (EFI_SIMPLE_NETWORK_MODE
));
214 // Create various resources. First create the route table, timer
215 // event and MNP child. IGMP, interface's initialization depend
218 IpSb
->DefaultRouteTable
= Ip4CreateRouteTable ();
220 if (IpSb
->DefaultRouteTable
== NULL
) {
221 Status
= EFI_OUT_OF_RESOURCES
;
225 Status
= gBS
->CreateEvent (
226 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
233 if (EFI_ERROR (Status
)) {
237 Status
= NetLibCreateServiceChild (
240 &gEfiManagedNetworkServiceBindingProtocolGuid
,
241 &IpSb
->MnpChildHandle
244 if (EFI_ERROR (Status
)) {
248 Status
= gBS
->OpenProtocol (
249 IpSb
->MnpChildHandle
,
250 &gEfiManagedNetworkProtocolGuid
,
251 (VOID
**) &IpSb
->Mnp
,
254 EFI_OPEN_PROTOCOL_BY_DRIVER
257 if (EFI_ERROR (Status
)) {
261 Status
= Ip4ServiceConfigMnp (IpSb
, TRUE
);
263 if (EFI_ERROR (Status
)) {
267 Status
= IpSb
->Mnp
->GetModeData (IpSb
->Mnp
, NULL
, &IpSb
->SnpMode
);
269 if (EFI_ERROR (Status
)) {
273 Status
= Ip4InitIgmp (IpSb
);
275 if (EFI_ERROR (Status
)) {
279 IpSb
->MacString
= NULL
;
280 Status
= NetLibGetMacString (IpSb
->Controller
, IpSb
->Image
, &IpSb
->MacString
);
282 if (EFI_ERROR (Status
)) {
286 IpSb
->DefaultInterface
= Ip4CreateInterface (IpSb
->Mnp
, Controller
, ImageHandle
);
288 if (IpSb
->DefaultInterface
== NULL
) {
289 Status
= EFI_OUT_OF_RESOURCES
;
293 InsertHeadList (&IpSb
->Interfaces
, &IpSb
->DefaultInterface
->Link
);
295 ZeroMem (&IpSb
->Ip4Config2Instance
, sizeof (IP4_CONFIG2_INSTANCE
));
297 Status
= Ip4Config2InitInstance (&IpSb
->Ip4Config2Instance
);
299 if (EFI_ERROR (Status
)) {
303 IpSb
->MaxPacketSize
= IpSb
->SnpMode
.MaxPacketSize
- sizeof (IP4_HEAD
);
304 if (NetLibGetVlanId (IpSb
->Controller
) != 0) {
306 // This is a VLAN device, reduce MTU by VLAN tag length
308 IpSb
->MaxPacketSize
-= NET_VLAN_TAG_LEN
;
310 IpSb
->OldMaxPacketSize
= IpSb
->MaxPacketSize
;
316 Ip4CleanService (IpSb
);
324 Clean up a IP4 service binding instance. It will release all
325 the resource allocated by the instance. The instance may be
326 partly initialized, or partly destroyed. If a resource is
327 destroyed, it is marked as that in case the destroy failed and
328 being called again later.
330 @param[in] IpSb The IP4 service binding instance to clean up
332 @retval EFI_SUCCESS The resource used by the instance are cleaned up
333 @retval other Failed to clean up some of the resources.
343 if (IpSb
->DefaultInterface
!= NULL
) {
344 Status
= Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
346 if (EFI_ERROR (Status
)) {
350 IpSb
->DefaultInterface
= NULL
;
353 if (IpSb
->DefaultRouteTable
!= NULL
) {
354 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
355 IpSb
->DefaultRouteTable
= NULL
;
358 Ip4CleanAssembleTable (&IpSb
->Assemble
);
360 if (IpSb
->MnpChildHandle
!= NULL
) {
361 if (IpSb
->Mnp
!= NULL
) {
363 IpSb
->MnpChildHandle
,
364 &gEfiManagedNetworkProtocolGuid
,
372 NetLibDestroyServiceChild (
375 &gEfiManagedNetworkServiceBindingProtocolGuid
,
379 IpSb
->MnpChildHandle
= NULL
;
382 if (IpSb
->Timer
!= NULL
) {
383 gBS
->SetTimer (IpSb
->Timer
, TimerCancel
, 0);
384 gBS
->CloseEvent (IpSb
->Timer
);
389 if (IpSb
->MacString
!= NULL
) {
390 FreePool (IpSb
->MacString
);
393 Ip4Config2CleanInstance (&IpSb
->Ip4Config2Instance
);
399 Callback function which provided by user to remove one node in NetDestroyLinkList process.
401 @param[in] Entry The entry to be removed.
402 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
404 @retval EFI_SUCCESS The entry has been removed successfully.
405 @retval Others Fail to remove the entry.
410 Ip4DestroyChildEntryInHandleBuffer (
411 IN LIST_ENTRY
*Entry
,
415 IP4_PROTOCOL
*IpInstance
;
416 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
417 UINTN NumberOfChildren
;
418 EFI_HANDLE
*ChildHandleBuffer
;
420 if (Entry
== NULL
|| Context
== NULL
) {
421 return EFI_INVALID_PARAMETER
;
424 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP4_PROTOCOL
, Link
, IP4_PROTOCOL_SIGNATURE
);
425 ServiceBinding
= ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ServiceBinding
;
426 NumberOfChildren
= ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->NumberOfChildren
;
427 ChildHandleBuffer
= ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ChildHandleBuffer
;
429 if (!NetIsInHandleBuffer (IpInstance
->Handle
, NumberOfChildren
, ChildHandleBuffer
)) {
433 return ServiceBinding
->DestroyChild (ServiceBinding
, IpInstance
->Handle
);
437 Start this driver on ControllerHandle. This service is called by the
438 EFI boot service ConnectController(). In order to make
439 drivers as small as possible, there are a few calling restrictions for
440 this service. ConnectController() must follow these
441 calling restrictions. If any other agent wishes to call Start() it
442 must also follow these calling restrictions.
444 @param[in] This Protocol instance pointer.
445 @param[in] ControllerHandle Handle of device to bind driver to
446 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
449 @retval EFI_SUCCESS This driver is added to ControllerHandle
450 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
451 @retval other This driver does not support this device
456 Ip4DriverBindingStart (
457 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
458 IN EFI_HANDLE ControllerHandle
,
459 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
466 // Test for the Ip4 service binding protocol
468 Status
= gBS
->OpenProtocol (
470 &gEfiIp4ServiceBindingProtocolGuid
,
472 This
->DriverBindingHandle
,
474 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
477 if (Status
== EFI_SUCCESS
) {
478 return EFI_ALREADY_STARTED
;
481 Status
= Ip4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &IpSb
);
483 if (EFI_ERROR (Status
)) {
487 ASSERT (IpSb
!= NULL
);
490 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
492 Status
= gBS
->InstallMultipleProtocolInterfaces (
494 &gEfiIp4ServiceBindingProtocolGuid
,
495 &IpSb
->ServiceBinding
,
496 &gEfiIp4Config2ProtocolGuid
,
497 &IpSb
->Ip4Config2Instance
.Ip4Config2
,
501 if (EFI_ERROR (Status
)) {
506 // Ready to go: start the receiving and timer.
507 // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after
508 // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status.
510 Status
= Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
512 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
513 goto UNINSTALL_PROTOCOL
;
516 Status
= gBS
->SetTimer (IpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
518 if (EFI_ERROR (Status
)) {
519 goto UNINSTALL_PROTOCOL
;
523 // Initialize the IP4 ID
525 mIp4Id
= (UINT16
)NET_RANDOM (NetRandomInitSeed ());
530 gBS
->UninstallProtocolInterface (
532 &gEfiIp4ServiceBindingProtocolGuid
,
533 &IpSb
->ServiceBinding
537 Ip4CleanService (IpSb
);
544 Stop this driver on ControllerHandle. This service is called by the
545 EFI boot service DisconnectController(). In order to
546 make drivers as small as possible, there are a few calling
547 restrictions for this service. DisconnectController()
548 must follow these calling restrictions. If any other agent wishes
549 to call Stop() it must also follow these calling restrictions.
551 @param[in] This Protocol instance pointer.
552 @param[in] ControllerHandle Handle of device to stop driver on
553 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
554 of children is zero stop the entire bus driver.
555 @param[in] ChildHandleBuffer List of Child Handles to Stop.
557 @retval EFI_SUCCESS This driver is removed ControllerHandle
558 @retval other This driver was not removed from this device
563 Ip4DriverBindingStop (
564 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
565 IN EFI_HANDLE ControllerHandle
,
566 IN UINTN NumberOfChildren
,
567 IN EFI_HANDLE
*ChildHandleBuffer
570 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
572 EFI_HANDLE NicHandle
;
576 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
578 IP4_ROUTE_TABLE
*RouteTable
;
584 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
585 if (NicHandle
== NULL
) {
586 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiArpProtocolGuid
);
587 if (NicHandle
== NULL
) {
588 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
589 if (NicHandle
!= NULL
) {
597 Status
= gBS
->OpenProtocol (
599 &gEfiIp4ServiceBindingProtocolGuid
,
600 (VOID
**) &ServiceBinding
,
601 This
->DriverBindingHandle
,
603 EFI_OPEN_PROTOCOL_GET_PROTOCOL
605 if (EFI_ERROR (Status
)) {
606 return EFI_DEVICE_ERROR
;
609 IpSb
= IP4_SERVICE_FROM_PROTOCOL (ServiceBinding
);
612 Status
= Ip4Config2DestroyDhcp4 (&IpSb
->Ip4Config2Instance
);
613 gBS
->CloseEvent (IpSb
->Ip4Config2Instance
.Dhcp4Event
);
614 IpSb
->Ip4Config2Instance
.Dhcp4Event
= NULL
;
615 } else if (NumberOfChildren
!= 0) {
616 List
= &IpSb
->Children
;
617 Context
.ServiceBinding
= ServiceBinding
;
618 Context
.NumberOfChildren
= NumberOfChildren
;
619 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
620 Status
= NetDestroyLinkList (
622 Ip4DestroyChildEntryInHandleBuffer
,
626 } else if (IpSb
->DefaultInterface
->ArpHandle
== ControllerHandle
) {
629 // The ARP protocol for the default interface is being uninstalled and all
630 // its IP child handles should have been destroyed before. So, release the
631 // default interface and route table, create a new one and mark it as not started.
633 Ip4CancelReceive (IpSb
->DefaultInterface
);
634 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
635 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
637 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
641 RouteTable
= Ip4CreateRouteTable ();
642 if (RouteTable
== NULL
) {
643 Ip4FreeInterface (IpIf
, NULL
);
647 IpSb
->DefaultInterface
= IpIf
;
648 InsertHeadList (&IpSb
->Interfaces
, &IpIf
->Link
);
649 IpSb
->DefaultRouteTable
= RouteTable
;
650 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
652 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
654 } else if (IsListEmpty (&IpSb
->Children
)) {
656 IpSb
->State
= IP4_SERVICE_DESTROY
;
659 // OK, clean other resources then uninstall the service binding protocol.
661 Status
= Ip4CleanService (IpSb
);
662 if (EFI_ERROR (Status
)) {
667 gBS
->UninstallMultipleProtocolInterfaces (
669 &gEfiIp4ServiceBindingProtocolGuid
,
671 &gEfiIp4Config2ProtocolGuid
,
672 &IpSb
->Ip4Config2Instance
.Ip4Config2
,
676 if (gIp4ControllerNameTable
!= NULL
) {
677 FreeUnicodeStringTable (gIp4ControllerNameTable
);
678 gIp4ControllerNameTable
= NULL
;
689 Creates a child handle and installs a protocol.
691 The CreateChild() function installs a protocol on ChildHandle.
692 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
693 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
695 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
696 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
697 then a new handle is created. If it is a pointer to an existing UEFI handle,
698 then the protocol is added to the existing UEFI handle.
700 @retval EFI_SUCCES The protocol was added to ChildHandle.
701 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
702 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
704 @retval other The child handle was not created
709 Ip4ServiceBindingCreateChild (
710 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
711 IN OUT EFI_HANDLE
*ChildHandle
715 IP4_PROTOCOL
*IpInstance
;
720 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
721 return EFI_INVALID_PARAMETER
;
724 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
725 IpInstance
= AllocatePool (sizeof (IP4_PROTOCOL
));
727 if (IpInstance
== NULL
) {
728 return EFI_OUT_OF_RESOURCES
;
731 Ip4InitProtocol (IpSb
, IpInstance
);
734 // Install Ip4 onto ChildHandle
736 Status
= gBS
->InstallMultipleProtocolInterfaces (
738 &gEfiIp4ProtocolGuid
,
739 &IpInstance
->Ip4Proto
,
743 if (EFI_ERROR (Status
)) {
747 IpInstance
->Handle
= *ChildHandle
;
750 // Open the Managed Network protocol BY_CHILD.
752 Status
= gBS
->OpenProtocol (
753 IpSb
->MnpChildHandle
,
754 &gEfiManagedNetworkProtocolGuid
,
756 gIp4DriverBinding
.DriverBindingHandle
,
758 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
760 if (EFI_ERROR (Status
)) {
761 gBS
->UninstallMultipleProtocolInterfaces (
763 &gEfiIp4ProtocolGuid
,
764 &IpInstance
->Ip4Proto
,
772 // Insert it into the service binding instance.
774 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
776 InsertTailList (&IpSb
->Children
, &IpInstance
->Link
);
779 gBS
->RestoreTPL (OldTpl
);
783 if (EFI_ERROR (Status
)) {
785 Ip4CleanProtocol (IpInstance
);
787 FreePool (IpInstance
);
795 Destroys a child handle with a protocol installed on it.
797 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
798 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
799 last protocol on ChildHandle, then ChildHandle is destroyed.
801 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
802 @param ChildHandle Handle of the child to destroy
804 @retval EFI_SUCCES The protocol was removed from ChildHandle.
805 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
806 @retval EFI_INVALID_PARAMETER Child handle is NULL.
807 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
808 because its services are being used.
809 @retval other The child handle was not destroyed
814 Ip4ServiceBindingDestroyChild (
815 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
816 IN EFI_HANDLE ChildHandle
821 IP4_PROTOCOL
*IpInstance
;
822 EFI_IP4_PROTOCOL
*Ip4
;
826 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
827 return EFI_INVALID_PARAMETER
;
831 // Retrieve the private context data structures
833 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
835 Status
= gBS
->OpenProtocol (
837 &gEfiIp4ProtocolGuid
,
839 gIp4DriverBinding
.DriverBindingHandle
,
841 EFI_OPEN_PROTOCOL_GET_PROTOCOL
844 if (EFI_ERROR (Status
)) {
845 return EFI_UNSUPPORTED
;
848 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (Ip4
);
850 if (IpInstance
->Service
!= IpSb
) {
851 return EFI_INVALID_PARAMETER
;
854 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
857 // A child can be destroyed more than once. For example,
858 // Ip4DriverBindingStop will destroy all of its children.
859 // when UDP driver is being stopped, it will destroy all
860 // the IP child it opens.
862 if (IpInstance
->State
== IP4_STATE_DESTROY
) {
863 gBS
->RestoreTPL (OldTpl
);
867 State
= IpInstance
->State
;
868 IpInstance
->State
= IP4_STATE_DESTROY
;
871 // Close the Managed Network protocol.
874 IpSb
->MnpChildHandle
,
875 &gEfiManagedNetworkProtocolGuid
,
876 gIp4DriverBinding
.DriverBindingHandle
,
880 if (IpInstance
->Interface
!= NULL
&& IpInstance
->Interface
->Arp
!= NULL
) {
882 IpInstance
->Interface
->ArpHandle
,
883 &gEfiArpProtocolGuid
,
884 gIp4DriverBinding
.DriverBindingHandle
,
890 // Uninstall the IP4 protocol first. Many thing happens during
892 // 1. The consumer of the IP4 protocol will be stopped if it
893 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
894 // stopped, IP driver's stop function will be called, and uninstall
895 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
896 // makes it possible to create the network stack bottom up, and
898 // 2. the upper layer will recycle the received packet. The recycle
899 // event's TPL is higher than this function. The recycle events
900 // will be called back before preceeding. If any packets not recycled,
901 // that means there is a resource leak.
903 gBS
->RestoreTPL (OldTpl
);
904 Status
= gBS
->UninstallProtocolInterface (
906 &gEfiIp4ProtocolGuid
,
907 &IpInstance
->Ip4Proto
909 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
910 if (EFI_ERROR (Status
)) {
914 Status
= Ip4CleanProtocol (IpInstance
);
915 if (EFI_ERROR (Status
)) {
916 gBS
->InstallMultipleProtocolInterfaces (
918 &gEfiIp4ProtocolGuid
,
926 RemoveEntryList (&IpInstance
->Link
);
929 gBS
->RestoreTPL (OldTpl
);
931 FreePool (IpInstance
);
935 IpInstance
->State
= State
;
936 gBS
->RestoreTPL (OldTpl
);