2 The driver binding and service binding protocol for IP4 driver.
4 Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
13 EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding
= {
14 Ip4DriverBindingSupported
,
15 Ip4DriverBindingStart
,
22 BOOLEAN mIpSec2Installed
= FALSE
;
25 Callback function for IpSec2 Protocol install.
27 @param[in] Event Event whose notification function is being invoked
28 @param[in] Context Pointer to the notification function's context
33 IpSec2InstalledCallback (
41 // Test if protocol was even found.
42 // Notification function will be called at least once.
44 Status
= gBS
->LocateProtocol (&gEfiIpSec2ProtocolGuid
, NULL
, (VOID
**)&mIpSec
);
45 if ((Status
== EFI_SUCCESS
) && (mIpSec
!= NULL
)) {
47 // Close the event so it does not get called again.
49 gBS
->CloseEvent (Event
);
51 mIpSec2Installed
= TRUE
;
56 This is the declaration of an EFI image entry point. This entry point is
57 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
58 both device drivers and bus drivers.
60 The entry point for IP4 driver which install the driver
61 binding and component name protocol on its image.
63 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
64 @param[in] SystemTable A pointer to the EFI System Table.
66 @retval EFI_SUCCESS The operation completed successfully.
67 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
73 IN EFI_HANDLE ImageHandle
,
74 IN EFI_SYSTEM_TABLE
*SystemTable
79 EfiCreateProtocolNotifyEvent (
80 &gEfiIpSec2ProtocolGuid
,
82 IpSec2InstalledCallback
,
87 return EfiLibInstallDriverBindingComponentName2 (
98 Test to see if this driver supports ControllerHandle. This service
99 is called by the EFI boot service ConnectController(). In
100 order to make drivers as small as possible, there are a few calling
101 restrictions for this service. ConnectController() must
102 follow these calling restrictions. If any other agent wishes to call
103 Supported() it must also follow these calling restrictions.
105 @param[in] This Protocol instance pointer.
106 @param[in] ControllerHandle Handle of device to test
107 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
110 @retval EFI_SUCCESS This driver supports this device
111 @retval EFI_ALREADY_STARTED This driver is already running on this device
112 @retval other This driver does not support this device
117 Ip4DriverBindingSupported (
118 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
119 IN EFI_HANDLE ControllerHandle
,
120 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
126 // Test for the MNP service binding Protocol
128 Status
= gBS
->OpenProtocol (
130 &gEfiManagedNetworkServiceBindingProtocolGuid
,
132 This
->DriverBindingHandle
,
134 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
137 if (EFI_ERROR (Status
)) {
142 // Test for the Arp service binding Protocol
144 Status
= gBS
->OpenProtocol (
146 &gEfiArpServiceBindingProtocolGuid
,
148 This
->DriverBindingHandle
,
150 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
157 Clean up a IP4 service binding instance. It will release all
158 the resource allocated by the instance. The instance may be
159 partly initialized, or partly destroyed. If a resource is
160 destroyed, it is marked as that in case the destroy failed and
161 being called again later.
163 @param[in] IpSb The IP4 service binding instance to clean up
165 @retval EFI_SUCCESS The resource used by the instance are cleaned up
166 @retval other Failed to clean up some of the resources.
175 Create a new IP4 driver service binding private instance.
177 @param Controller The controller that has MNP service binding
179 @param ImageHandle The IP4 driver's image handle
180 @param Service The variable to receive the newly created IP4
183 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
184 @retval EFI_SUCCESS A new IP4 service binding private is created.
185 @retval other Other error occurs.
190 IN EFI_HANDLE Controller
,
191 IN EFI_HANDLE ImageHandle
,
192 OUT IP4_SERVICE
**Service
198 ASSERT (Service
!= NULL
);
203 // allocate a service private data then initialize all the filed to
204 // empty resources, so if any thing goes wrong when allocating
205 // resources, Ip4CleanService can be called to clean it up.
207 IpSb
= AllocateZeroPool (sizeof (IP4_SERVICE
));
210 return EFI_OUT_OF_RESOURCES
;
213 IpSb
->Signature
= IP4_SERVICE_SIGNATURE
;
214 IpSb
->ServiceBinding
.CreateChild
= Ip4ServiceBindingCreateChild
;
215 IpSb
->ServiceBinding
.DestroyChild
= Ip4ServiceBindingDestroyChild
;
216 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
218 IpSb
->NumChildren
= 0;
219 InitializeListHead (&IpSb
->Children
);
221 InitializeListHead (&IpSb
->Interfaces
);
222 IpSb
->DefaultInterface
= NULL
;
223 IpSb
->DefaultRouteTable
= NULL
;
225 Ip4InitAssembleTable (&IpSb
->Assemble
);
227 IpSb
->IgmpCtrl
.Igmpv1QuerySeen
= 0;
228 InitializeListHead (&IpSb
->IgmpCtrl
.Groups
);
230 IpSb
->Image
= ImageHandle
;
231 IpSb
->Controller
= Controller
;
233 IpSb
->MnpChildHandle
= NULL
;
236 IpSb
->MnpConfigData
.ReceivedQueueTimeoutValue
= 0;
237 IpSb
->MnpConfigData
.TransmitQueueTimeoutValue
= 0;
238 IpSb
->MnpConfigData
.ProtocolTypeFilter
= IP4_ETHER_PROTO
;
239 IpSb
->MnpConfigData
.EnableUnicastReceive
= TRUE
;
240 IpSb
->MnpConfigData
.EnableMulticastReceive
= TRUE
;
241 IpSb
->MnpConfigData
.EnableBroadcastReceive
= TRUE
;
242 IpSb
->MnpConfigData
.EnablePromiscuousReceive
= FALSE
;
243 IpSb
->MnpConfigData
.FlushQueuesOnReset
= TRUE
;
244 IpSb
->MnpConfigData
.EnableReceiveTimestamps
= FALSE
;
245 IpSb
->MnpConfigData
.DisableBackgroundPolling
= FALSE
;
247 ZeroMem (&IpSb
->SnpMode
, sizeof (EFI_SIMPLE_NETWORK_MODE
));
250 IpSb
->ReconfigCheckTimer
= NULL
;
252 IpSb
->ReconfigEvent
= NULL
;
254 IpSb
->Reconfig
= FALSE
;
256 IpSb
->MediaPresent
= TRUE
;
259 // Create various resources. First create the route table, timer
260 // event, ReconfigEvent and MNP child. IGMP, interface's initialization depend
263 IpSb
->DefaultRouteTable
= Ip4CreateRouteTable ();
265 if (IpSb
->DefaultRouteTable
== NULL
) {
266 Status
= EFI_OUT_OF_RESOURCES
;
270 Status
= gBS
->CreateEvent (
271 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
278 if (EFI_ERROR (Status
)) {
282 Status
= gBS
->CreateEvent (
283 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
285 Ip4TimerReconfigChecking
,
287 &IpSb
->ReconfigCheckTimer
290 if (EFI_ERROR (Status
)) {
294 Status
= gBS
->CreateEvent (
297 Ip4AutoReconfigCallBack
,
301 if (EFI_ERROR (Status
)) {
305 Status
= NetLibCreateServiceChild (
308 &gEfiManagedNetworkServiceBindingProtocolGuid
,
309 &IpSb
->MnpChildHandle
312 if (EFI_ERROR (Status
)) {
316 Status
= gBS
->OpenProtocol (
317 IpSb
->MnpChildHandle
,
318 &gEfiManagedNetworkProtocolGuid
,
322 EFI_OPEN_PROTOCOL_BY_DRIVER
325 if (EFI_ERROR (Status
)) {
329 Status
= Ip4ServiceConfigMnp (IpSb
, TRUE
);
331 if (EFI_ERROR (Status
)) {
335 Status
= IpSb
->Mnp
->GetModeData (IpSb
->Mnp
, NULL
, &IpSb
->SnpMode
);
337 if (EFI_ERROR (Status
)) {
341 Status
= Ip4InitIgmp (IpSb
);
343 if (EFI_ERROR (Status
)) {
347 IpSb
->MacString
= NULL
;
348 Status
= NetLibGetMacString (IpSb
->Controller
, IpSb
->Image
, &IpSb
->MacString
);
350 if (EFI_ERROR (Status
)) {
354 IpSb
->DefaultInterface
= Ip4CreateInterface (IpSb
->Mnp
, Controller
, ImageHandle
);
356 if (IpSb
->DefaultInterface
== NULL
) {
357 Status
= EFI_OUT_OF_RESOURCES
;
361 InsertHeadList (&IpSb
->Interfaces
, &IpSb
->DefaultInterface
->Link
);
363 ZeroMem (&IpSb
->Ip4Config2Instance
, sizeof (IP4_CONFIG2_INSTANCE
));
365 Status
= Ip4Config2InitInstance (&IpSb
->Ip4Config2Instance
);
367 if (EFI_ERROR (Status
)) {
371 IpSb
->MaxPacketSize
= IpSb
->SnpMode
.MaxPacketSize
- sizeof (IP4_HEAD
);
372 if (NetLibGetVlanId (IpSb
->Controller
) != 0) {
374 // This is a VLAN device, reduce MTU by VLAN tag length
376 IpSb
->MaxPacketSize
-= NET_VLAN_TAG_LEN
;
379 IpSb
->OldMaxPacketSize
= IpSb
->MaxPacketSize
;
385 Ip4CleanService (IpSb
);
392 Clean up a IP4 service binding instance. It will release all
393 the resource allocated by the instance. The instance may be
394 partly initialized, or partly destroyed. If a resource is
395 destroyed, it is marked as that in case the destroy failed and
396 being called again later.
398 @param[in] IpSb The IP4 service binding instance to clean up
400 @retval EFI_SUCCESS The resource used by the instance are cleaned up
401 @retval other Failed to clean up some of the resources.
411 IpSb
->State
= IP4_SERVICE_DESTROY
;
413 if (IpSb
->Timer
!= NULL
) {
414 gBS
->SetTimer (IpSb
->Timer
, TimerCancel
, 0);
415 gBS
->CloseEvent (IpSb
->Timer
);
420 if (IpSb
->ReconfigCheckTimer
!= NULL
) {
421 gBS
->SetTimer (IpSb
->ReconfigCheckTimer
, TimerCancel
, 0);
422 gBS
->CloseEvent (IpSb
->ReconfigCheckTimer
);
424 IpSb
->ReconfigCheckTimer
= NULL
;
427 if (IpSb
->DefaultInterface
!= NULL
) {
428 Status
= Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
430 if (EFI_ERROR (Status
)) {
434 IpSb
->DefaultInterface
= NULL
;
437 if (IpSb
->DefaultRouteTable
!= NULL
) {
438 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
439 IpSb
->DefaultRouteTable
= NULL
;
442 Ip4CleanAssembleTable (&IpSb
->Assemble
);
444 if (IpSb
->MnpChildHandle
!= NULL
) {
445 if (IpSb
->Mnp
!= NULL
) {
447 IpSb
->MnpChildHandle
,
448 &gEfiManagedNetworkProtocolGuid
,
456 NetLibDestroyServiceChild (
459 &gEfiManagedNetworkServiceBindingProtocolGuid
,
463 IpSb
->MnpChildHandle
= NULL
;
466 if (IpSb
->ReconfigEvent
!= NULL
) {
467 gBS
->CloseEvent (IpSb
->ReconfigEvent
);
469 IpSb
->ReconfigEvent
= NULL
;
472 IpSb
->Reconfig
= FALSE
;
474 if (IpSb
->MacString
!= NULL
) {
475 FreePool (IpSb
->MacString
);
478 Ip4Config2CleanInstance (&IpSb
->Ip4Config2Instance
);
484 Callback function which provided by user to remove one node in NetDestroyLinkList process.
486 @param[in] Entry The entry to be removed.
487 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
489 @retval EFI_SUCCESS The entry has been removed successfully.
490 @retval Others Fail to remove the entry.
495 Ip4DestroyChildEntryInHandleBuffer (
496 IN LIST_ENTRY
*Entry
,
500 IP4_PROTOCOL
*IpInstance
;
501 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
502 UINTN NumberOfChildren
;
503 EFI_HANDLE
*ChildHandleBuffer
;
505 if ((Entry
== NULL
) || (Context
== NULL
)) {
506 return EFI_INVALID_PARAMETER
;
509 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP4_PROTOCOL
, Link
, IP4_PROTOCOL_SIGNATURE
);
510 ServiceBinding
= ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*)Context
)->ServiceBinding
;
511 NumberOfChildren
= ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*)Context
)->NumberOfChildren
;
512 ChildHandleBuffer
= ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*)Context
)->ChildHandleBuffer
;
514 if (!NetIsInHandleBuffer (IpInstance
->Handle
, NumberOfChildren
, ChildHandleBuffer
)) {
518 return ServiceBinding
->DestroyChild (ServiceBinding
, IpInstance
->Handle
);
522 Start this driver on ControllerHandle. This service is called by the
523 EFI boot service ConnectController(). In order to make
524 drivers as small as possible, there are a few calling restrictions for
525 this service. ConnectController() must follow these
526 calling restrictions. If any other agent wishes to call Start() it
527 must also follow these calling restrictions.
529 @param[in] This Protocol instance pointer.
530 @param[in] ControllerHandle Handle of device to bind driver to
531 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
534 @retval EFI_SUCCESS This driver is added to ControllerHandle
535 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
536 @retval other This driver does not support this device
541 Ip4DriverBindingStart (
542 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
543 IN EFI_HANDLE ControllerHandle
,
544 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
549 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Cfg2
;
551 IP4_CONFIG2_DATA_ITEM
*DataItem
;
558 // Test for the Ip4 service binding protocol
560 Status
= gBS
->OpenProtocol (
562 &gEfiIp4ServiceBindingProtocolGuid
,
564 This
->DriverBindingHandle
,
566 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
569 if (Status
== EFI_SUCCESS
) {
570 return EFI_ALREADY_STARTED
;
573 Status
= Ip4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &IpSb
);
575 if (EFI_ERROR (Status
)) {
579 ASSERT (IpSb
!= NULL
);
581 Ip4Cfg2
= &IpSb
->Ip4Config2Instance
.Ip4Config2
;
584 // Install the Ip4ServiceBinding Protocol onto ControllerHandle
586 Status
= gBS
->InstallMultipleProtocolInterfaces (
588 &gEfiIp4ServiceBindingProtocolGuid
,
589 &IpSb
->ServiceBinding
,
590 &gEfiIp4Config2ProtocolGuid
,
595 if (EFI_ERROR (Status
)) {
600 // Read the config data from NV variable again.
601 // The default data can be changed by other drivers.
603 Status
= Ip4Config2ReadConfigData (IpSb
->MacString
, &IpSb
->Ip4Config2Instance
);
604 if (EFI_ERROR (Status
)) {
605 goto UNINSTALL_PROTOCOL
;
609 // Consume the installed EFI_IP4_CONFIG2_PROTOCOL to set the default data items.
611 for (Index
= Ip4Config2DataTypePolicy
; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
612 DataItem
= &IpSb
->Ip4Config2Instance
.DataItem
[Index
];
613 if (DataItem
->Data
.Ptr
!= NULL
) {
614 Status
= Ip4Cfg2
->SetData (
620 if (EFI_ERROR (Status
)) {
621 goto UNINSTALL_PROTOCOL
;
624 if ((Index
== Ip4Config2DataTypePolicy
) && (*(DataItem
->Data
.Policy
) == Ip4Config2PolicyDhcp
)) {
631 // Ready to go: start the receiving and timer.
632 // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after
633 // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status.
635 Status
= Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
637 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
638 goto UNINSTALL_PROTOCOL
;
641 Status
= gBS
->SetTimer (IpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
643 if (EFI_ERROR (Status
)) {
644 goto UNINSTALL_PROTOCOL
;
647 Status
= gBS
->SetTimer (IpSb
->ReconfigCheckTimer
, TimerPeriodic
, 500 * TICKS_PER_MS
);
649 if (EFI_ERROR (Status
)) {
650 goto UNINSTALL_PROTOCOL
;
654 // Initialize the IP4 ID
656 mIp4Id
= (UINT16
)NET_RANDOM (NetRandomInitSeed ());
661 gBS
->UninstallMultipleProtocolInterfaces (
663 &gEfiIp4ServiceBindingProtocolGuid
,
664 &IpSb
->ServiceBinding
,
665 &gEfiIp4Config2ProtocolGuid
,
671 Ip4CleanService (IpSb
);
677 Stop this driver on ControllerHandle. This service is called by the
678 EFI boot service DisconnectController(). In order to
679 make drivers as small as possible, there are a few calling
680 restrictions for this service. DisconnectController()
681 must follow these calling restrictions. If any other agent wishes
682 to call Stop() it must also follow these calling restrictions.
684 @param[in] This Protocol instance pointer.
685 @param[in] ControllerHandle Handle of device to stop driver on
686 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
687 of children is zero stop the entire bus driver.
688 @param[in] ChildHandleBuffer List of Child Handles to Stop.
690 @retval EFI_SUCCESS This driver is removed ControllerHandle
691 @retval other This driver was not removed from this device
696 Ip4DriverBindingStop (
697 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
698 IN EFI_HANDLE ControllerHandle
,
699 IN UINTN NumberOfChildren
,
700 IN EFI_HANDLE
*ChildHandleBuffer
703 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
705 EFI_HANDLE NicHandle
;
709 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
711 IP4_ROUTE_TABLE
*RouteTable
;
717 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
718 if (NicHandle
== NULL
) {
719 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiArpProtocolGuid
);
720 if (NicHandle
== NULL
) {
721 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
722 if (NicHandle
!= NULL
) {
730 Status
= gBS
->OpenProtocol (
732 &gEfiIp4ServiceBindingProtocolGuid
,
733 (VOID
**)&ServiceBinding
,
734 This
->DriverBindingHandle
,
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL
738 if (EFI_ERROR (Status
)) {
739 return EFI_DEVICE_ERROR
;
742 IpSb
= IP4_SERVICE_FROM_PROTOCOL (ServiceBinding
);
745 Status
= Ip4Config2DestroyDhcp4 (&IpSb
->Ip4Config2Instance
);
746 gBS
->CloseEvent (IpSb
->Ip4Config2Instance
.Dhcp4Event
);
747 IpSb
->Ip4Config2Instance
.Dhcp4Event
= NULL
;
748 } else if (NumberOfChildren
!= 0) {
749 List
= &IpSb
->Children
;
750 Context
.ServiceBinding
= ServiceBinding
;
751 Context
.NumberOfChildren
= NumberOfChildren
;
752 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
753 Status
= NetDestroyLinkList (
755 Ip4DestroyChildEntryInHandleBuffer
,
759 } else if (IpSb
->DefaultInterface
->ArpHandle
== ControllerHandle
) {
761 // The ARP protocol for the default interface is being uninstalled and all
762 // its IP child handles should have been destroyed before. So, release the
763 // default interface and route table, create a new one and mark it as not started.
765 Ip4CancelReceive (IpSb
->DefaultInterface
);
766 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
767 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
769 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
774 RouteTable
= Ip4CreateRouteTable ();
775 if (RouteTable
== NULL
) {
776 Ip4FreeInterface (IpIf
, NULL
);
780 IpSb
->DefaultInterface
= IpIf
;
781 InsertHeadList (&IpSb
->Interfaces
, &IpIf
->Link
);
782 IpSb
->DefaultRouteTable
= RouteTable
;
783 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
785 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
786 } else if (IsListEmpty (&IpSb
->Children
)) {
789 // OK, clean other resources then uninstall the service binding protocol.
791 Status
= Ip4CleanService (IpSb
);
792 if (EFI_ERROR (Status
)) {
797 gBS
->UninstallMultipleProtocolInterfaces (
799 &gEfiIp4ServiceBindingProtocolGuid
,
801 &gEfiIp4Config2ProtocolGuid
,
802 &IpSb
->Ip4Config2Instance
.Ip4Config2
,
806 if (gIp4ControllerNameTable
!= NULL
) {
807 FreeUnicodeStringTable (gIp4ControllerNameTable
);
808 gIp4ControllerNameTable
= NULL
;
819 Creates a child handle and installs a protocol.
821 The CreateChild() function installs a protocol on ChildHandle.
822 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
823 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
825 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
826 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
827 then a new handle is created. If it is a pointer to an existing UEFI handle,
828 then the protocol is added to the existing UEFI handle.
830 @retval EFI_SUCCESS The protocol was added to ChildHandle.
831 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
832 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
834 @retval other The child handle was not created
839 Ip4ServiceBindingCreateChild (
840 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
841 IN OUT EFI_HANDLE
*ChildHandle
845 IP4_PROTOCOL
*IpInstance
;
850 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
851 return EFI_INVALID_PARAMETER
;
854 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
855 IpInstance
= AllocatePool (sizeof (IP4_PROTOCOL
));
857 if (IpInstance
== NULL
) {
858 return EFI_OUT_OF_RESOURCES
;
861 Ip4InitProtocol (IpSb
, IpInstance
);
864 // Install Ip4 onto ChildHandle
866 Status
= gBS
->InstallMultipleProtocolInterfaces (
868 &gEfiIp4ProtocolGuid
,
869 &IpInstance
->Ip4Proto
,
873 if (EFI_ERROR (Status
)) {
877 IpInstance
->Handle
= *ChildHandle
;
880 // Open the Managed Network protocol BY_CHILD.
882 Status
= gBS
->OpenProtocol (
883 IpSb
->MnpChildHandle
,
884 &gEfiManagedNetworkProtocolGuid
,
886 gIp4DriverBinding
.DriverBindingHandle
,
888 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
890 if (EFI_ERROR (Status
)) {
891 gBS
->UninstallMultipleProtocolInterfaces (
893 &gEfiIp4ProtocolGuid
,
894 &IpInstance
->Ip4Proto
,
902 // Insert it into the service binding instance.
904 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
906 InsertTailList (&IpSb
->Children
, &IpInstance
->Link
);
909 gBS
->RestoreTPL (OldTpl
);
913 if (EFI_ERROR (Status
)) {
914 Ip4CleanProtocol (IpInstance
);
916 FreePool (IpInstance
);
923 Destroys a child handle with a protocol installed on it.
925 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
926 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
927 last protocol on ChildHandle, then ChildHandle is destroyed.
929 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
930 @param ChildHandle Handle of the child to destroy
932 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
933 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
934 @retval EFI_INVALID_PARAMETER Child handle is NULL.
935 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
936 because its services are being used.
937 @retval other The child handle was not destroyed
942 Ip4ServiceBindingDestroyChild (
943 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
944 IN EFI_HANDLE ChildHandle
949 IP4_PROTOCOL
*IpInstance
;
950 EFI_IP4_PROTOCOL
*Ip4
;
953 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
954 return EFI_INVALID_PARAMETER
;
958 // Retrieve the private context data structures
960 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
962 Status
= gBS
->OpenProtocol (
964 &gEfiIp4ProtocolGuid
,
966 gIp4DriverBinding
.DriverBindingHandle
,
968 EFI_OPEN_PROTOCOL_GET_PROTOCOL
971 if (EFI_ERROR (Status
)) {
972 return EFI_UNSUPPORTED
;
975 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (Ip4
);
977 if (IpInstance
->Service
!= IpSb
) {
978 return EFI_INVALID_PARAMETER
;
981 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
984 // A child can be destroyed more than once. For example,
985 // Ip4DriverBindingStop will destroy all of its children.
986 // when UDP driver is being stopped, it will destroy all
987 // the IP child it opens.
989 if (IpInstance
->InDestroy
) {
990 gBS
->RestoreTPL (OldTpl
);
994 IpInstance
->InDestroy
= TRUE
;
997 // Close the Managed Network protocol.
1000 IpSb
->MnpChildHandle
,
1001 &gEfiManagedNetworkProtocolGuid
,
1002 gIp4DriverBinding
.DriverBindingHandle
,
1006 if ((IpInstance
->Interface
!= NULL
) && (IpInstance
->Interface
->Arp
!= NULL
)) {
1007 gBS
->CloseProtocol (
1008 IpInstance
->Interface
->ArpHandle
,
1009 &gEfiArpProtocolGuid
,
1010 gIp4DriverBinding
.DriverBindingHandle
,
1016 // Uninstall the IP4 protocol first. Many thing happens during
1018 // 1. The consumer of the IP4 protocol will be stopped if it
1019 // opens the protocol BY_DRIVER. For example, if MNP driver is
1020 // stopped, IP driver's stop function will be called, and uninstall
1021 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
1022 // makes it possible to create the network stack bottom up, and
1023 // stop it top down.
1024 // 2. the upper layer will recycle the received packet. The recycle
1025 // event's TPL is higher than this function. The recycle events
1026 // will be called back before preceding. If any packets not recycled,
1027 // that means there is a resource leak.
1029 gBS
->RestoreTPL (OldTpl
);
1030 Status
= gBS
->UninstallProtocolInterface (
1032 &gEfiIp4ProtocolGuid
,
1033 &IpInstance
->Ip4Proto
1035 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1036 if (EFI_ERROR (Status
)) {
1037 IpInstance
->InDestroy
= FALSE
;
1041 Status
= Ip4CleanProtocol (IpInstance
);
1042 if (EFI_ERROR (Status
)) {
1043 gBS
->InstallMultipleProtocolInterfaces (
1045 &gEfiIp4ProtocolGuid
,
1053 RemoveEntryList (&IpInstance
->Link
);
1054 IpSb
->NumChildren
--;
1056 gBS
->RestoreTPL (OldTpl
);
1058 FreePool (IpInstance
);
1062 gBS
->RestoreTPL (OldTpl
);