3 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "Dhcp4Driver.h"
11 EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding
= {
12 Dhcp4DriverBindingSupported
,
13 Dhcp4DriverBindingStart
,
14 Dhcp4DriverBindingStop
,
20 EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate
= {
21 Dhcp4ServiceBindingCreateChild
,
22 Dhcp4ServiceBindingDestroyChild
26 This is the declaration of an EFI image entry point. This entry point is
27 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
28 both device drivers and bus drivers.
30 Entry point of the DHCP driver to install various protocols.
32 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
33 @param[in] SystemTable A pointer to the EFI System Table.
35 @retval EFI_SUCCESS The operation completed successfully.
36 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
41 Dhcp4DriverEntryPoint (
42 IN EFI_HANDLE ImageHandle
,
43 IN EFI_SYSTEM_TABLE
*SystemTable
46 return EfiLibInstallDriverBindingComponentName2 (
57 Test to see if this driver supports ControllerHandle. This service
58 is called by the EFI boot service ConnectController(). In
59 order to make drivers as small as possible, there are a few calling
60 restrictions for this service. ConnectController() must
61 follow these calling restrictions. If any other agent wishes to call
62 Supported() it must also follow these calling restrictions.
64 @param[in] This Protocol instance pointer.
65 @param[in] ControllerHandle Handle of device to test
66 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
69 @retval EFI_SUCCESS This driver supports this device
70 @retval EFI_ALREADY_STARTED This driver is already running on this device
71 @retval other This driver does not support this device
76 Dhcp4DriverBindingSupported (
77 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
78 IN EFI_HANDLE ControllerHandle
,
79 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
84 Status
= gBS
->OpenProtocol (
86 &gEfiUdp4ServiceBindingProtocolGuid
,
88 This
->DriverBindingHandle
,
90 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
97 Configure the default UDP child to receive all the DHCP traffics
98 on this network interface.
100 @param[in] UdpIo The UDP IO to configure
101 @param[in] Context The context to the function
103 @retval EFI_SUCCESS The UDP IO is successfully configured.
104 @retval Others Failed to configure the UDP child.
114 EFI_UDP4_CONFIG_DATA UdpConfigData
;
116 UdpConfigData
.AcceptBroadcast
= TRUE
;
117 UdpConfigData
.AcceptPromiscuous
= FALSE
;
118 UdpConfigData
.AcceptAnyPort
= FALSE
;
119 UdpConfigData
.AllowDuplicatePort
= TRUE
;
120 UdpConfigData
.TypeOfService
= 0;
121 UdpConfigData
.TimeToLive
= 64;
122 UdpConfigData
.DoNotFragment
= FALSE
;
123 UdpConfigData
.ReceiveTimeout
= 0;
124 UdpConfigData
.TransmitTimeout
= 0;
126 UdpConfigData
.UseDefaultAddress
= FALSE
;
127 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
128 UdpConfigData
.RemotePort
= DHCP_SERVER_PORT
;
130 ZeroMem (&UdpConfigData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
131 ZeroMem (&UdpConfigData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
132 ZeroMem (&UdpConfigData
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
134 return UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfigData
);
138 Destroy the DHCP service. The Dhcp4 service may be partly initialized,
139 or partly destroyed. If a resource is destroyed, it is marked as so in
140 case the destroy failed and being called again later.
142 @param[in] DhcpSb The DHCP service instance to destroy.
144 @retval EFI_SUCCESS Always return success.
149 IN DHCP_SERVICE
*DhcpSb
152 DhcpCleanLease (DhcpSb
);
154 if (DhcpSb
->UdpIo
!= NULL
) {
155 UdpIoFreeIo (DhcpSb
->UdpIo
);
156 DhcpSb
->UdpIo
= NULL
;
159 if (DhcpSb
->Timer
!= NULL
) {
160 gBS
->SetTimer (DhcpSb
->Timer
, TimerCancel
, 0);
161 gBS
->CloseEvent (DhcpSb
->Timer
);
163 DhcpSb
->Timer
= NULL
;
170 Create a new DHCP service binding instance for the controller.
172 @param[in] Controller The controller to install DHCP service binding
174 @param[in] ImageHandle The driver's image handle
175 @param[out] Service The variable to receive the created DHCP service
178 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .
179 @retval EFI_SUCCESS The DHCP service instance is created.
180 @retval other Other error occurs.
185 IN EFI_HANDLE Controller
,
186 IN EFI_HANDLE ImageHandle
,
187 OUT DHCP_SERVICE
**Service
190 DHCP_SERVICE
*DhcpSb
;
194 DhcpSb
= AllocateZeroPool (sizeof (DHCP_SERVICE
));
196 if (DhcpSb
== NULL
) {
197 return EFI_OUT_OF_RESOURCES
;
200 DhcpSb
->Signature
= DHCP_SERVICE_SIGNATURE
;
201 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
202 DhcpSb
->Controller
= Controller
;
203 DhcpSb
->Image
= ImageHandle
;
204 InitializeListHead (&DhcpSb
->Children
);
205 DhcpSb
->DhcpState
= Dhcp4Stopped
;
206 DhcpSb
->Xid
= NET_RANDOM (NetRandomInitSeed ());
208 &DhcpSb
->ServiceBinding
,
209 &mDhcp4ServiceBindingTemplate
,
210 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
213 // Create various resources, UdpIo, Timer, and get Mac address
215 Status
= gBS
->CreateEvent (
216 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
223 if (EFI_ERROR (Status
)) {
227 DhcpSb
->UdpIo
= UdpIoCreateIo (
235 if (DhcpSb
->UdpIo
== NULL
) {
236 Status
= EFI_OUT_OF_RESOURCES
;
240 DhcpSb
->HwLen
= (UINT8
)DhcpSb
->UdpIo
->SnpMode
.HwAddressSize
;
241 DhcpSb
->HwType
= DhcpSb
->UdpIo
->SnpMode
.IfType
;
242 CopyMem (&DhcpSb
->Mac
, &DhcpSb
->UdpIo
->SnpMode
.CurrentAddress
, sizeof (DhcpSb
->Mac
));
248 Dhcp4CloseService (DhcpSb
);
255 Start this driver on ControllerHandle. This service is called by the
256 EFI boot service ConnectController(). In order to make
257 drivers as small as possible, there are a few calling restrictions for
258 this service. ConnectController() must follow these
259 calling restrictions. If any other agent wishes to call Start() it
260 must also follow these calling restrictions.
262 @param[in] This Protocol instance pointer.
263 @param[in] ControllerHandle Handle of device to bind driver to
264 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
267 @retval EFI_SUCCESS This driver is added to ControllerHandle
268 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
269 @retval other This driver does not support this device
274 Dhcp4DriverBindingStart (
275 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
276 IN EFI_HANDLE ControllerHandle
,
277 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
280 DHCP_SERVICE
*DhcpSb
;
284 // First: test for the DHCP4 Protocol
286 Status
= gBS
->OpenProtocol (
288 &gEfiDhcp4ServiceBindingProtocolGuid
,
290 This
->DriverBindingHandle
,
292 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
295 if (Status
== EFI_SUCCESS
) {
296 return EFI_ALREADY_STARTED
;
299 Status
= Dhcp4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &DhcpSb
);
301 if (EFI_ERROR (Status
)) {
305 ASSERT (DhcpSb
!= NULL
);
308 // Start the receiving
310 Status
= UdpIoRecvDatagram (DhcpSb
->UdpIo
, DhcpInput
, DhcpSb
, 0);
312 if (EFI_ERROR (Status
)) {
316 Status
= gBS
->SetTimer (DhcpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
318 if (EFI_ERROR (Status
)) {
323 // Install the Dhcp4ServiceBinding Protocol onto ControllerHandle
325 Status
= gBS
->InstallMultipleProtocolInterfaces (
327 &gEfiDhcp4ServiceBindingProtocolGuid
,
328 &DhcpSb
->ServiceBinding
,
332 if (EFI_ERROR (Status
)) {
339 Dhcp4CloseService (DhcpSb
);
345 Callback function which provided by user to remove one node in NetDestroyLinkList process.
347 @param[in] Entry The entry to be removed.
348 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
350 @retval EFI_SUCCESS The entry has been removed successfully.
351 @retval Others Fail to remove the entry.
356 Dhcp4DestroyChildEntry (
357 IN LIST_ENTRY
*Entry
,
361 DHCP_PROTOCOL
*Instance
;
362 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
364 if ((Entry
== NULL
) || (Context
== NULL
)) {
365 return EFI_INVALID_PARAMETER
;
368 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP_PROTOCOL
, Link
, DHCP_PROTOCOL_SIGNATURE
);
369 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*)Context
;
371 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
375 Stop this driver on ControllerHandle. This service is called by the
376 EFI boot service DisconnectController(). In order to
377 make drivers as small as possible, there are a few calling
378 restrictions for this service. DisconnectController()
379 must follow these calling restrictions. If any other agent wishes
380 to call Stop() it must also follow these calling restrictions.
382 @param[in] This Protocol instance pointer.
383 @param[in] ControllerHandle Handle of device to stop driver on
384 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
385 children is zero stop the entire bus driver.
386 @param[in] ChildHandleBuffer List of Child Handles to Stop.
388 @retval EFI_SUCCESS This driver is removed ControllerHandle
389 @retval other This driver was not removed from this device
394 Dhcp4DriverBindingStop (
395 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
396 IN EFI_HANDLE ControllerHandle
,
397 IN UINTN NumberOfChildren
,
398 IN EFI_HANDLE
*ChildHandleBuffer
401 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
402 DHCP_SERVICE
*DhcpSb
;
403 EFI_HANDLE NicHandle
;
409 // DHCP driver opens UDP child, So, the ControllerHandle is the
410 // UDP child handle. locate the Nic handle first.
412 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp4ProtocolGuid
);
414 if (NicHandle
== NULL
) {
418 Status
= gBS
->OpenProtocol (
420 &gEfiDhcp4ServiceBindingProtocolGuid
,
421 (VOID
**)&ServiceBinding
,
422 This
->DriverBindingHandle
,
424 EFI_OPEN_PROTOCOL_GET_PROTOCOL
427 if (EFI_ERROR (Status
)) {
428 return EFI_DEVICE_ERROR
;
431 DhcpSb
= DHCP_SERVICE_FROM_THIS (ServiceBinding
);
432 if (!IsListEmpty (&DhcpSb
->Children
)) {
434 // Destroy all the children instances before destroy the service.
436 List
= &DhcpSb
->Children
;
437 Status
= NetDestroyLinkList (
439 Dhcp4DestroyChildEntry
,
443 if (EFI_ERROR (Status
) || (ListLength
!= 0)) {
444 Status
= EFI_DEVICE_ERROR
;
448 if ((NumberOfChildren
== 0) && !IsListEmpty (&DhcpSb
->Children
)) {
449 Status
= EFI_DEVICE_ERROR
;
452 if ((NumberOfChildren
== 0) && IsListEmpty (&DhcpSb
->Children
)) {
454 // Destroy the service itself if no child instance left.
456 DhcpSb
->ServiceState
= DHCP_DESTROY
;
458 gBS
->UninstallProtocolInterface (
460 &gEfiDhcp4ServiceBindingProtocolGuid
,
464 Dhcp4CloseService (DhcpSb
);
466 if (gDhcpControllerNameTable
!= NULL
) {
467 FreeUnicodeStringTable (gDhcpControllerNameTable
);
468 gDhcpControllerNameTable
= NULL
;
473 Status
= EFI_SUCCESS
;
480 Initialize a new DHCP instance.
482 @param DhcpSb The dhcp service instance
483 @param Instance The dhcp instance to initialize
488 IN DHCP_SERVICE
*DhcpSb
,
489 IN OUT DHCP_PROTOCOL
*Instance
492 Instance
->Signature
= DHCP_PROTOCOL_SIGNATURE
;
493 CopyMem (&Instance
->Dhcp4Protocol
, &mDhcp4ProtocolTemplate
, sizeof (Instance
->Dhcp4Protocol
));
494 InitializeListHead (&Instance
->Link
);
495 Instance
->Handle
= NULL
;
496 Instance
->Service
= DhcpSb
;
497 Instance
->InDestroy
= FALSE
;
498 Instance
->CompletionEvent
= NULL
;
499 Instance
->RenewRebindEvent
= NULL
;
500 Instance
->Token
= NULL
;
501 Instance
->UdpIo
= NULL
;
502 Instance
->ElaspedTime
= 0;
503 NetbufQueInit (&Instance
->ResponseQueue
);
507 Creates a child handle and installs a protocol.
509 The CreateChild() function installs a protocol on ChildHandle.
510 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
511 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
513 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
514 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
515 then a new handle is created. If it is a pointer to an existing UEFI handle,
516 then the protocol is added to the existing UEFI handle.
518 @retval EFI_SUCCESS The protocol was added to ChildHandle.
519 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
520 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
522 @retval other The child handle was not created
527 Dhcp4ServiceBindingCreateChild (
528 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
529 IN EFI_HANDLE
*ChildHandle
532 DHCP_SERVICE
*DhcpSb
;
533 DHCP_PROTOCOL
*Instance
;
538 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
539 return EFI_INVALID_PARAMETER
;
542 Instance
= AllocatePool (sizeof (*Instance
));
544 if (Instance
== NULL
) {
545 return EFI_OUT_OF_RESOURCES
;
548 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
549 DhcpInitProtocol (DhcpSb
, Instance
);
552 // Install DHCP4 onto ChildHandle
554 Status
= gBS
->InstallMultipleProtocolInterfaces (
556 &gEfiDhcp4ProtocolGuid
,
557 &Instance
->Dhcp4Protocol
,
561 if (EFI_ERROR (Status
)) {
566 Instance
->Handle
= *ChildHandle
;
569 // Open the Udp4 protocol BY_CHILD.
571 Status
= gBS
->OpenProtocol (
572 DhcpSb
->UdpIo
->UdpHandle
,
573 &gEfiUdp4ProtocolGuid
,
575 gDhcp4DriverBinding
.DriverBindingHandle
,
577 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
579 if (EFI_ERROR (Status
)) {
580 gBS
->UninstallMultipleProtocolInterfaces (
582 &gEfiDhcp4ProtocolGuid
,
583 &Instance
->Dhcp4Protocol
,
591 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
593 InsertTailList (&DhcpSb
->Children
, &Instance
->Link
);
594 DhcpSb
->NumChildren
++;
596 gBS
->RestoreTPL (OldTpl
);
602 Destroys a child handle with a protocol installed on it.
604 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
605 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
606 last protocol on ChildHandle, then ChildHandle is destroyed.
608 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
609 @param ChildHandle Handle of the child to destroy
611 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
612 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
613 @retval EFI_INVALID_PARAMETER Child handle is NULL.
614 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
615 because its services are being used.
616 @retval other The child handle was not destroyed
621 Dhcp4ServiceBindingDestroyChild (
622 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
623 IN EFI_HANDLE ChildHandle
626 DHCP_SERVICE
*DhcpSb
;
627 DHCP_PROTOCOL
*Instance
;
628 EFI_DHCP4_PROTOCOL
*Dhcp
;
632 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
633 return EFI_INVALID_PARAMETER
;
637 // Retrieve the private context data structures
639 Status
= gBS
->OpenProtocol (
641 &gEfiDhcp4ProtocolGuid
,
643 gDhcp4DriverBinding
.DriverBindingHandle
,
645 EFI_OPEN_PROTOCOL_GET_PROTOCOL
648 if (EFI_ERROR (Status
)) {
649 return EFI_UNSUPPORTED
;
652 Instance
= DHCP_INSTANCE_FROM_THIS (Dhcp
);
653 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
655 if (Instance
->Service
!= DhcpSb
) {
656 return EFI_INVALID_PARAMETER
;
660 // A child can be destroyed more than once. For example,
661 // Dhcp4DriverBindingStop will destroy all of its children.
662 // when caller driver is being stopped, it will destroy the
663 // dhcp child it opens.
665 if (Instance
->InDestroy
) {
669 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
670 Instance
->InDestroy
= TRUE
;
673 // Close the Udp4 protocol.
676 DhcpSb
->UdpIo
->UdpHandle
,
677 &gEfiUdp4ProtocolGuid
,
678 gDhcp4DriverBinding
.DriverBindingHandle
,
683 // Uninstall the DHCP4 protocol first to enable a top down destruction.
685 gBS
->RestoreTPL (OldTpl
);
686 Status
= gBS
->UninstallProtocolInterface (
688 &gEfiDhcp4ProtocolGuid
,
691 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
692 if (EFI_ERROR (Status
)) {
693 Instance
->InDestroy
= FALSE
;
695 gBS
->RestoreTPL (OldTpl
);
699 if (DhcpSb
->ActiveChild
== Instance
) {
700 DhcpYieldControl (DhcpSb
);
703 RemoveEntryList (&Instance
->Link
);
704 DhcpSb
->NumChildren
--;
706 if (Instance
->UdpIo
!= NULL
) {
707 UdpIoCleanIo (Instance
->UdpIo
);
709 Instance
->UdpIo
->UdpHandle
,
710 &gEfiUdp4ProtocolGuid
,
711 Instance
->Service
->Image
,
714 UdpIoFreeIo (Instance
->UdpIo
);
715 Instance
->UdpIo
= NULL
;
716 Instance
->Token
= NULL
;
719 gBS
->RestoreTPL (OldTpl
);