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 (
58 Test to see if this driver supports ControllerHandle. This service
59 is called by the EFI boot service ConnectController(). In
60 order to make drivers as small as possible, there are a few calling
61 restrictions for this service. ConnectController() must
62 follow these calling restrictions. If any other agent wishes to call
63 Supported() it must also follow these calling restrictions.
65 @param[in] This Protocol instance pointer.
66 @param[in] ControllerHandle Handle of device to test
67 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
70 @retval EFI_SUCCESS This driver supports this device
71 @retval EFI_ALREADY_STARTED This driver is already running on this device
72 @retval other This driver does not support this device
77 Dhcp4DriverBindingSupported (
78 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
79 IN EFI_HANDLE ControllerHandle
,
80 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
85 Status
= gBS
->OpenProtocol (
87 &gEfiUdp4ServiceBindingProtocolGuid
,
89 This
->DriverBindingHandle
,
91 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
100 Configure the default UDP child to receive all the DHCP traffics
101 on this network interface.
103 @param[in] UdpIo The UDP IO to configure
104 @param[in] Context The context to the function
106 @retval EFI_SUCCESS The UDP IO is successfully configured.
107 @retval Others Failed to configure the UDP child.
117 EFI_UDP4_CONFIG_DATA UdpConfigData
;
119 UdpConfigData
.AcceptBroadcast
= TRUE
;
120 UdpConfigData
.AcceptPromiscuous
= FALSE
;
121 UdpConfigData
.AcceptAnyPort
= FALSE
;
122 UdpConfigData
.AllowDuplicatePort
= TRUE
;
123 UdpConfigData
.TypeOfService
= 0;
124 UdpConfigData
.TimeToLive
= 64;
125 UdpConfigData
.DoNotFragment
= FALSE
;
126 UdpConfigData
.ReceiveTimeout
= 0;
127 UdpConfigData
.TransmitTimeout
= 0;
129 UdpConfigData
.UseDefaultAddress
= FALSE
;
130 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
131 UdpConfigData
.RemotePort
= DHCP_SERVER_PORT
;
133 ZeroMem (&UdpConfigData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
134 ZeroMem (&UdpConfigData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
135 ZeroMem (&UdpConfigData
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
137 return UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfigData
);;
143 Destroy the DHCP service. The Dhcp4 service may be partly initialized,
144 or partly destroyed. If a resource is destroyed, it is marked as so in
145 case the destroy failed and being called again later.
147 @param[in] DhcpSb The DHCP service instance to destroy.
149 @retval EFI_SUCCESS Always return success.
154 IN DHCP_SERVICE
*DhcpSb
157 DhcpCleanLease (DhcpSb
);
159 if (DhcpSb
->UdpIo
!= NULL
) {
160 UdpIoFreeIo (DhcpSb
->UdpIo
);
161 DhcpSb
->UdpIo
= NULL
;
164 if (DhcpSb
->Timer
!= NULL
) {
165 gBS
->SetTimer (DhcpSb
->Timer
, TimerCancel
, 0);
166 gBS
->CloseEvent (DhcpSb
->Timer
);
168 DhcpSb
->Timer
= NULL
;
177 Create a new DHCP service binding instance for the controller.
179 @param[in] Controller The controller to install DHCP service binding
181 @param[in] ImageHandle The driver's image handle
182 @param[out] Service The variable to receive the created DHCP service
185 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .
186 @retval EFI_SUCCESS The DHCP service instance is created.
187 @retval other Other error occurs.
192 IN EFI_HANDLE Controller
,
193 IN EFI_HANDLE ImageHandle
,
194 OUT DHCP_SERVICE
**Service
197 DHCP_SERVICE
*DhcpSb
;
201 DhcpSb
= AllocateZeroPool (sizeof (DHCP_SERVICE
));
203 if (DhcpSb
== NULL
) {
204 return EFI_OUT_OF_RESOURCES
;
207 DhcpSb
->Signature
= DHCP_SERVICE_SIGNATURE
;
208 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
209 DhcpSb
->Controller
= Controller
;
210 DhcpSb
->Image
= ImageHandle
;
211 InitializeListHead (&DhcpSb
->Children
);
212 DhcpSb
->DhcpState
= Dhcp4Stopped
;
213 DhcpSb
->Xid
= NET_RANDOM (NetRandomInitSeed ());
215 &DhcpSb
->ServiceBinding
,
216 &mDhcp4ServiceBindingTemplate
,
217 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
220 // Create various resources, UdpIo, Timer, and get Mac address
222 Status
= gBS
->CreateEvent (
223 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
230 if (EFI_ERROR (Status
)) {
234 DhcpSb
->UdpIo
= UdpIoCreateIo (
242 if (DhcpSb
->UdpIo
== NULL
) {
243 Status
= EFI_OUT_OF_RESOURCES
;
247 DhcpSb
->HwLen
= (UINT8
) DhcpSb
->UdpIo
->SnpMode
.HwAddressSize
;
248 DhcpSb
->HwType
= DhcpSb
->UdpIo
->SnpMode
.IfType
;
249 CopyMem (&DhcpSb
->Mac
, &DhcpSb
->UdpIo
->SnpMode
.CurrentAddress
, sizeof (DhcpSb
->Mac
));
255 Dhcp4CloseService (DhcpSb
);
263 Start this driver on ControllerHandle. This service is called by the
264 EFI boot service ConnectController(). In order to make
265 drivers as small as possible, there are a few calling restrictions for
266 this service. ConnectController() must follow these
267 calling restrictions. If any other agent wishes to call Start() it
268 must also follow these calling restrictions.
270 @param[in] This Protocol instance pointer.
271 @param[in] ControllerHandle Handle of device to bind driver to
272 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
275 @retval EFI_SUCCESS This driver is added to ControllerHandle
276 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
277 @retval other This driver does not support this device
282 Dhcp4DriverBindingStart (
283 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
284 IN EFI_HANDLE ControllerHandle
,
285 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
288 DHCP_SERVICE
*DhcpSb
;
292 // First: test for the DHCP4 Protocol
294 Status
= gBS
->OpenProtocol (
296 &gEfiDhcp4ServiceBindingProtocolGuid
,
298 This
->DriverBindingHandle
,
300 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
303 if (Status
== EFI_SUCCESS
) {
304 return EFI_ALREADY_STARTED
;
307 Status
= Dhcp4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &DhcpSb
);
309 if (EFI_ERROR (Status
)) {
312 ASSERT (DhcpSb
!= NULL
);
315 // Start the receiving
317 Status
= UdpIoRecvDatagram (DhcpSb
->UdpIo
, DhcpInput
, DhcpSb
, 0);
319 if (EFI_ERROR (Status
)) {
322 Status
= gBS
->SetTimer (DhcpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
324 if (EFI_ERROR (Status
)) {
329 // Install the Dhcp4ServiceBinding Protocol onto ControllerHandle
331 Status
= gBS
->InstallMultipleProtocolInterfaces (
333 &gEfiDhcp4ServiceBindingProtocolGuid
,
334 &DhcpSb
->ServiceBinding
,
338 if (EFI_ERROR (Status
)) {
345 Dhcp4CloseService (DhcpSb
);
351 Callback function which provided by user to remove one node in NetDestroyLinkList process.
353 @param[in] Entry The entry to be removed.
354 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
356 @retval EFI_SUCCESS The entry has been removed successfully.
357 @retval Others Fail to remove the entry.
362 Dhcp4DestroyChildEntry (
363 IN LIST_ENTRY
*Entry
,
367 DHCP_PROTOCOL
*Instance
;
368 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
370 if (Entry
== NULL
|| Context
== NULL
) {
371 return EFI_INVALID_PARAMETER
;
374 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP_PROTOCOL
, Link
, DHCP_PROTOCOL_SIGNATURE
);
375 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*) Context
;
377 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
382 Stop this driver on ControllerHandle. This service is called by the
383 EFI boot service DisconnectController(). In order to
384 make drivers as small as possible, there are a few calling
385 restrictions for this service. DisconnectController()
386 must follow these calling restrictions. If any other agent wishes
387 to call Stop() it must also follow these calling restrictions.
389 @param[in] This Protocol instance pointer.
390 @param[in] ControllerHandle Handle of device to stop driver on
391 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
392 children is zero stop the entire bus driver.
393 @param[in] ChildHandleBuffer List of Child Handles to Stop.
395 @retval EFI_SUCCESS This driver is removed ControllerHandle
396 @retval other This driver was not removed from this device
401 Dhcp4DriverBindingStop (
402 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
403 IN EFI_HANDLE ControllerHandle
,
404 IN UINTN NumberOfChildren
,
405 IN EFI_HANDLE
*ChildHandleBuffer
408 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
409 DHCP_SERVICE
*DhcpSb
;
410 EFI_HANDLE NicHandle
;
416 // DHCP driver opens UDP child, So, the ControllerHandle is the
417 // UDP child handle. locate the Nic handle first.
419 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp4ProtocolGuid
);
421 if (NicHandle
== NULL
) {
425 Status
= gBS
->OpenProtocol (
427 &gEfiDhcp4ServiceBindingProtocolGuid
,
428 (VOID
**) &ServiceBinding
,
429 This
->DriverBindingHandle
,
431 EFI_OPEN_PROTOCOL_GET_PROTOCOL
434 if (EFI_ERROR (Status
)) {
435 return EFI_DEVICE_ERROR
;
438 DhcpSb
= DHCP_SERVICE_FROM_THIS (ServiceBinding
);
439 if (!IsListEmpty (&DhcpSb
->Children
)) {
441 // Destroy all the children instances before destroy the service.
443 List
= &DhcpSb
->Children
;
444 Status
= NetDestroyLinkList (
446 Dhcp4DestroyChildEntry
,
450 if (EFI_ERROR (Status
) || ListLength
!= 0) {
451 Status
= EFI_DEVICE_ERROR
;
455 if (NumberOfChildren
== 0 && !IsListEmpty (&DhcpSb
->Children
)) {
456 Status
= EFI_DEVICE_ERROR
;
459 if (NumberOfChildren
== 0 && IsListEmpty (&DhcpSb
->Children
)) {
461 // Destroy the service itself if no child instance left.
463 DhcpSb
->ServiceState
= DHCP_DESTROY
;
465 gBS
->UninstallProtocolInterface (
467 &gEfiDhcp4ServiceBindingProtocolGuid
,
471 Dhcp4CloseService (DhcpSb
);
473 if (gDhcpControllerNameTable
!= NULL
) {
474 FreeUnicodeStringTable (gDhcpControllerNameTable
);
475 gDhcpControllerNameTable
= NULL
;
479 Status
= EFI_SUCCESS
;
487 Initialize a new DHCP instance.
489 @param DhcpSb The dhcp service instance
490 @param Instance The dhcp instance to initialize
495 IN DHCP_SERVICE
*DhcpSb
,
496 IN OUT DHCP_PROTOCOL
*Instance
499 Instance
->Signature
= DHCP_PROTOCOL_SIGNATURE
;
500 CopyMem (&Instance
->Dhcp4Protocol
, &mDhcp4ProtocolTemplate
, sizeof (Instance
->Dhcp4Protocol
));
501 InitializeListHead (&Instance
->Link
);
502 Instance
->Handle
= NULL
;
503 Instance
->Service
= DhcpSb
;
504 Instance
->InDestroy
= FALSE
;
505 Instance
->CompletionEvent
= NULL
;
506 Instance
->RenewRebindEvent
= NULL
;
507 Instance
->Token
= NULL
;
508 Instance
->UdpIo
= NULL
;
509 Instance
->ElaspedTime
= 0;
510 NetbufQueInit (&Instance
->ResponseQueue
);
515 Creates a child handle and installs a protocol.
517 The CreateChild() function installs a protocol on ChildHandle.
518 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
519 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
521 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
522 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
523 then a new handle is created. If it is a pointer to an existing UEFI handle,
524 then the protocol is added to the existing UEFI handle.
526 @retval EFI_SUCCESS The protocol was added to ChildHandle.
527 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
528 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
530 @retval other The child handle was not created
535 Dhcp4ServiceBindingCreateChild (
536 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
537 IN EFI_HANDLE
*ChildHandle
540 DHCP_SERVICE
*DhcpSb
;
541 DHCP_PROTOCOL
*Instance
;
546 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
547 return EFI_INVALID_PARAMETER
;
550 Instance
= AllocatePool (sizeof (*Instance
));
552 if (Instance
== NULL
) {
553 return EFI_OUT_OF_RESOURCES
;
556 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
557 DhcpInitProtocol (DhcpSb
, Instance
);
560 // Install DHCP4 onto ChildHandle
562 Status
= gBS
->InstallMultipleProtocolInterfaces (
564 &gEfiDhcp4ProtocolGuid
,
565 &Instance
->Dhcp4Protocol
,
569 if (EFI_ERROR (Status
)) {
574 Instance
->Handle
= *ChildHandle
;
577 // Open the Udp4 protocol BY_CHILD.
579 Status
= gBS
->OpenProtocol (
580 DhcpSb
->UdpIo
->UdpHandle
,
581 &gEfiUdp4ProtocolGuid
,
583 gDhcp4DriverBinding
.DriverBindingHandle
,
585 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
587 if (EFI_ERROR (Status
)) {
588 gBS
->UninstallMultipleProtocolInterfaces (
590 &gEfiDhcp4ProtocolGuid
,
591 &Instance
->Dhcp4Protocol
,
599 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
601 InsertTailList (&DhcpSb
->Children
, &Instance
->Link
);
602 DhcpSb
->NumChildren
++;
604 gBS
->RestoreTPL (OldTpl
);
611 Destroys a child handle with a protocol installed on it.
613 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
614 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
615 last protocol on ChildHandle, then ChildHandle is destroyed.
617 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
618 @param ChildHandle Handle of the child to destroy
620 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
621 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
622 @retval EFI_INVALID_PARAMETER Child handle is NULL.
623 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
624 because its services are being used.
625 @retval other The child handle was not destroyed
630 Dhcp4ServiceBindingDestroyChild (
631 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
632 IN EFI_HANDLE ChildHandle
635 DHCP_SERVICE
*DhcpSb
;
636 DHCP_PROTOCOL
*Instance
;
637 EFI_DHCP4_PROTOCOL
*Dhcp
;
641 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
642 return EFI_INVALID_PARAMETER
;
646 // Retrieve the private context data structures
648 Status
= gBS
->OpenProtocol (
650 &gEfiDhcp4ProtocolGuid
,
652 gDhcp4DriverBinding
.DriverBindingHandle
,
654 EFI_OPEN_PROTOCOL_GET_PROTOCOL
657 if (EFI_ERROR (Status
)) {
658 return EFI_UNSUPPORTED
;
661 Instance
= DHCP_INSTANCE_FROM_THIS (Dhcp
);
662 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
664 if (Instance
->Service
!= DhcpSb
) {
665 return EFI_INVALID_PARAMETER
;
669 // A child can be destroyed more than once. For example,
670 // Dhcp4DriverBindingStop will destroy all of its children.
671 // when caller driver is being stopped, it will destroy the
672 // dhcp child it opens.
674 if (Instance
->InDestroy
) {
678 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
679 Instance
->InDestroy
= TRUE
;
682 // Close the Udp4 protocol.
685 DhcpSb
->UdpIo
->UdpHandle
,
686 &gEfiUdp4ProtocolGuid
,
687 gDhcp4DriverBinding
.DriverBindingHandle
,
692 // Uninstall the DHCP4 protocol first to enable a top down destruction.
694 gBS
->RestoreTPL (OldTpl
);
695 Status
= gBS
->UninstallProtocolInterface (
697 &gEfiDhcp4ProtocolGuid
,
700 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
701 if (EFI_ERROR (Status
)) {
702 Instance
->InDestroy
= FALSE
;
704 gBS
->RestoreTPL (OldTpl
);
708 if (DhcpSb
->ActiveChild
== Instance
) {
709 DhcpYieldControl (DhcpSb
);
712 RemoveEntryList (&Instance
->Link
);
713 DhcpSb
->NumChildren
--;
715 if (Instance
->UdpIo
!= NULL
) {
716 UdpIoCleanIo (Instance
->UdpIo
);
718 Instance
->UdpIo
->UdpHandle
,
719 &gEfiUdp4ProtocolGuid
,
720 Instance
->Service
->Image
,
723 UdpIoFreeIo (Instance
->UdpIo
);
724 Instance
->UdpIo
= NULL
;
725 Instance
->Token
= NULL
;
728 gBS
->RestoreTPL (OldTpl
);