3 Copyright (c) 2006 - 2009, Intel Corporation.<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include "Dhcp4Impl.h"
15 #include "Dhcp4Driver.h"
17 EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding
= {
18 Dhcp4DriverBindingSupported
,
19 Dhcp4DriverBindingStart
,
20 Dhcp4DriverBindingStop
,
26 EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate
= {
27 Dhcp4ServiceBindingCreateChild
,
28 Dhcp4ServiceBindingDestroyChild
32 This is the declaration of an EFI image entry point. This entry point is
33 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
34 both device drivers and bus drivers.
36 Entry point of the DHCP driver to install various protocols.
38 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
39 @param[in] SystemTable A pointer to the EFI System Table.
41 @retval EFI_SUCCESS The operation completed successfully.
42 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
47 Dhcp4DriverEntryPoint (
48 IN EFI_HANDLE ImageHandle
,
49 IN EFI_SYSTEM_TABLE
*SystemTable
52 return EfiLibInstallDriverBindingComponentName2 (
64 Test to see if this driver supports ControllerHandle. This service
65 is called by the EFI boot service ConnectController(). In
66 order to make drivers as small as possible, there are a few calling
67 restrictions for this service. ConnectController() must
68 follow these calling restrictions. If any other agent wishes to call
69 Supported() it must also follow these calling restrictions.
71 @param[in] This Protocol instance pointer.
72 @param[in] ControllerHandle Handle of device to test
73 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
76 @retval EFI_SUCCESS This driver supports this device
77 @retval EFI_ALREADY_STARTED This driver is already running on this device
78 @retval other This driver does not support this device
83 Dhcp4DriverBindingSupported (
84 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
85 IN EFI_HANDLE ControllerHandle
,
86 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
91 Status
= gBS
->OpenProtocol (
93 &gEfiUdp4ServiceBindingProtocolGuid
,
95 This
->DriverBindingHandle
,
97 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
106 Configure the default UDP child to receive all the DHCP traffics
107 on this network interface.
109 @param[in] UdpIo The UDP IO to configure
110 @param[in] Context The context to the function
112 @retval EFI_SUCCESS The UDP IO is successfully configured.
113 @retval Others Failed to configure the UDP child.
122 EFI_UDP4_CONFIG_DATA UdpConfigData
;
124 UdpConfigData
.AcceptBroadcast
= TRUE
;
125 UdpConfigData
.AcceptPromiscuous
= FALSE
;
126 UdpConfigData
.AcceptAnyPort
= FALSE
;
127 UdpConfigData
.AllowDuplicatePort
= TRUE
;
128 UdpConfigData
.TypeOfService
= 0;
129 UdpConfigData
.TimeToLive
= 64;
130 UdpConfigData
.DoNotFragment
= FALSE
;
131 UdpConfigData
.ReceiveTimeout
= 0;
132 UdpConfigData
.TransmitTimeout
= 0;
134 UdpConfigData
.UseDefaultAddress
= FALSE
;
135 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
136 UdpConfigData
.RemotePort
= DHCP_SERVER_PORT
;
138 ZeroMem (&UdpConfigData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
139 ZeroMem (&UdpConfigData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
140 ZeroMem (&UdpConfigData
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
142 return UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfigData
);;
148 Destory the DHCP service. The Dhcp4 service may be partly initialized,
149 or partly destroyed. If a resource is destroyed, it is marked as so in
150 case the destroy failed and being called again later.
152 @param[in] DhcpSb The DHCP service instance to destory.
154 @retval EFI_SUCCESS Always return success.
159 IN DHCP_SERVICE
*DhcpSb
162 DhcpCleanLease (DhcpSb
);
164 if (DhcpSb
->UdpIo
!= NULL
) {
165 UdpIoFreeIo (DhcpSb
->UdpIo
);
166 DhcpSb
->UdpIo
= NULL
;
169 if (DhcpSb
->Timer
!= NULL
) {
170 gBS
->SetTimer (DhcpSb
->Timer
, TimerCancel
, 0);
171 gBS
->CloseEvent (DhcpSb
->Timer
);
173 DhcpSb
->Timer
= NULL
;
182 Create a new DHCP service binding instance for the controller.
184 @param[in] Controller The controller to install DHCP service binding
186 @param[in] ImageHandle The driver's image handle
187 @param[out] Service The variable to receive the created DHCP service
190 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .
191 @retval EFI_SUCCESS The DHCP service instance is created.
192 @retval other Other error occurs.
197 IN EFI_HANDLE Controller
,
198 IN EFI_HANDLE ImageHandle
,
199 OUT DHCP_SERVICE
**Service
202 DHCP_SERVICE
*DhcpSb
;
206 DhcpSb
= AllocateZeroPool (sizeof (DHCP_SERVICE
));
208 if (DhcpSb
== NULL
) {
209 return EFI_OUT_OF_RESOURCES
;
212 DhcpSb
->Signature
= DHCP_SERVICE_SIGNATURE
;
213 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
214 DhcpSb
->InDestory
= FALSE
;
215 DhcpSb
->Controller
= Controller
;
216 DhcpSb
->Image
= ImageHandle
;
217 InitializeListHead (&DhcpSb
->Children
);
218 DhcpSb
->DhcpState
= Dhcp4Stopped
;
219 DhcpSb
->Xid
= NET_RANDOM (NetRandomInitSeed ());
221 &DhcpSb
->ServiceBinding
,
222 &mDhcp4ServiceBindingTemplate
,
223 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
226 // Create various resources, UdpIo, Timer, and get Mac address
228 Status
= gBS
->CreateEvent (
229 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
236 if (EFI_ERROR (Status
)) {
240 DhcpSb
->UdpIo
= UdpIoCreateIo (
248 if (DhcpSb
->UdpIo
== NULL
) {
249 Status
= EFI_OUT_OF_RESOURCES
;
253 DhcpSb
->HwLen
= (UINT8
) DhcpSb
->UdpIo
->SnpMode
.HwAddressSize
;
254 DhcpSb
->HwType
= DhcpSb
->UdpIo
->SnpMode
.IfType
;
255 CopyMem (&DhcpSb
->Mac
, &DhcpSb
->UdpIo
->SnpMode
.CurrentAddress
, sizeof (DhcpSb
->Mac
));
261 Dhcp4CloseService (DhcpSb
);
269 Start this driver on ControllerHandle. This service is called by the
270 EFI boot service ConnectController(). In order to make
271 drivers as small as possible, there are a few calling restrictions for
272 this service. ConnectController() must follow these
273 calling restrictions. If any other agent wishes to call Start() it
274 must also follow these calling restrictions.
276 @param[in] This Protocol instance pointer.
277 @param[in] ControllerHandle Handle of device to bind driver to
278 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
281 @retval EFI_SUCCESS This driver is added to ControllerHandle
282 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
283 @retval other This driver does not support this device
288 Dhcp4DriverBindingStart (
289 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
290 IN EFI_HANDLE ControllerHandle
,
291 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
294 DHCP_SERVICE
*DhcpSb
;
298 // First: test for the DHCP4 Protocol
300 Status
= gBS
->OpenProtocol (
302 &gEfiDhcp4ServiceBindingProtocolGuid
,
304 This
->DriverBindingHandle
,
306 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
309 if (Status
== EFI_SUCCESS
) {
310 return EFI_ALREADY_STARTED
;
313 Status
= Dhcp4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &DhcpSb
);
315 if (EFI_ERROR (Status
)) {
320 // Start the receiving
322 Status
= UdpIoRecvDatagram (DhcpSb
->UdpIo
, DhcpInput
, DhcpSb
, 0);
324 if (EFI_ERROR (Status
)) {
327 Status
= gBS
->SetTimer (DhcpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
329 if (EFI_ERROR (Status
)) {
334 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
336 Status
= gBS
->InstallMultipleProtocolInterfaces (
338 &gEfiDhcp4ServiceBindingProtocolGuid
,
339 &DhcpSb
->ServiceBinding
,
343 if (EFI_ERROR (Status
)) {
350 Dhcp4CloseService (DhcpSb
);
357 Stop this driver on ControllerHandle. This service is called by the
358 EFI boot service DisconnectController(). In order to
359 make drivers as small as possible, there are a few calling
360 restrictions for this service. DisconnectController()
361 must follow these calling restrictions. If any other agent wishes
362 to call Stop() it must also follow these calling restrictions.
364 @param[in] This Protocol instance pointer.
365 @param[in] ControllerHandle Handle of device to stop driver on
366 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
367 children is zero stop the entire bus driver.
368 @param[in] ChildHandleBuffer List of Child Handles to Stop.
370 @retval EFI_SUCCESS This driver is removed ControllerHandle
371 @retval other This driver was not removed from this device
376 Dhcp4DriverBindingStop (
377 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
378 IN EFI_HANDLE ControllerHandle
,
379 IN UINTN NumberOfChildren
,
380 IN EFI_HANDLE
*ChildHandleBuffer
383 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
384 DHCP_SERVICE
*DhcpSb
;
385 DHCP_PROTOCOL
*Instance
;
386 EFI_HANDLE NicHandle
;
391 // DHCP driver opens UDP child, So, the ControllerHandle is the
392 // UDP child handle. locate the Nic handle first.
394 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp4ProtocolGuid
);
396 if (NicHandle
== NULL
) {
397 return EFI_DEVICE_ERROR
;
400 Status
= gBS
->OpenProtocol (
402 &gEfiDhcp4ServiceBindingProtocolGuid
,
403 (VOID
**) &ServiceBinding
,
404 This
->DriverBindingHandle
,
406 EFI_OPEN_PROTOCOL_GET_PROTOCOL
409 if (EFI_ERROR (Status
)) {
410 return EFI_DEVICE_ERROR
;
413 DhcpSb
= DHCP_SERVICE_FROM_THIS (ServiceBinding
);
415 if (DhcpSb
->InDestory
) {
419 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
421 if (NumberOfChildren
== 0) {
423 DhcpSb
->InDestory
= TRUE
;
424 DhcpSb
->ServiceState
= DHCP_DESTORY
;
426 gBS
->UninstallProtocolInterface (
428 &gEfiDhcp4ServiceBindingProtocolGuid
,
432 Dhcp4CloseService (DhcpSb
);
437 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild
438 // may cause other child to be deleted.
440 while (!IsListEmpty (&DhcpSb
->Children
)) {
441 Instance
= NET_LIST_HEAD (&DhcpSb
->Children
, DHCP_PROTOCOL
, Link
);
442 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
445 if (DhcpSb
->NumChildren
!= 0) {
446 Status
= EFI_DEVICE_ERROR
;
450 gBS
->RestoreTPL (OldTpl
);
457 Initialize a new DHCP instance.
459 @param DhcpSb The dhcp service instance
460 @param Instance The dhcp instance to initialize
465 IN DHCP_SERVICE
*DhcpSb
,
466 IN OUT DHCP_PROTOCOL
*Instance
469 Instance
->Signature
= DHCP_PROTOCOL_SIGNATURE
;
470 CopyMem (&Instance
->Dhcp4Protocol
, &mDhcp4ProtocolTemplate
, sizeof (Instance
->Dhcp4Protocol
));
471 InitializeListHead (&Instance
->Link
);
472 Instance
->Handle
= NULL
;
473 Instance
->Service
= DhcpSb
;
474 Instance
->InDestory
= FALSE
;
475 Instance
->CompletionEvent
= NULL
;
476 Instance
->RenewRebindEvent
= NULL
;
477 Instance
->Token
= NULL
;
478 Instance
->UdpIo
= NULL
;
479 NetbufQueInit (&Instance
->ResponseQueue
);
484 Creates a child handle and installs a protocol.
486 The CreateChild() function installs a protocol on ChildHandle.
487 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
488 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
490 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
491 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
492 then a new handle is created. If it is a pointer to an existing UEFI handle,
493 then the protocol is added to the existing UEFI handle.
495 @retval EFI_SUCCES The protocol was added to ChildHandle.
496 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
497 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
499 @retval other The child handle was not created
504 Dhcp4ServiceBindingCreateChild (
505 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
506 IN EFI_HANDLE
*ChildHandle
509 DHCP_SERVICE
*DhcpSb
;
510 DHCP_PROTOCOL
*Instance
;
515 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
516 return EFI_INVALID_PARAMETER
;
519 Instance
= AllocatePool (sizeof (*Instance
));
521 if (Instance
== NULL
) {
522 return EFI_OUT_OF_RESOURCES
;
525 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
526 DhcpInitProtocol (DhcpSb
, Instance
);
529 // Install DHCP4 onto ChildHandle
531 Status
= gBS
->InstallMultipleProtocolInterfaces (
533 &gEfiDhcp4ProtocolGuid
,
534 &Instance
->Dhcp4Protocol
,
538 if (EFI_ERROR (Status
)) {
543 Instance
->Handle
= *ChildHandle
;
546 // Open the Udp4 protocol BY_CHILD.
548 Status
= gBS
->OpenProtocol (
549 DhcpSb
->UdpIo
->UdpHandle
,
550 &gEfiUdp4ProtocolGuid
,
552 gDhcp4DriverBinding
.DriverBindingHandle
,
554 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
556 if (EFI_ERROR (Status
)) {
557 gBS
->UninstallMultipleProtocolInterfaces (
559 &gEfiDhcp4ProtocolGuid
,
560 &Instance
->Dhcp4Protocol
,
568 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
570 InsertTailList (&DhcpSb
->Children
, &Instance
->Link
);
571 DhcpSb
->NumChildren
++;
573 gBS
->RestoreTPL (OldTpl
);
580 Destroys a child handle with a protocol installed on it.
582 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
583 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
584 last protocol on ChildHandle, then ChildHandle is destroyed.
586 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
587 @param ChildHandle Handle of the child to destroy
589 @retval EFI_SUCCES The protocol was removed from ChildHandle.
590 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
591 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
592 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
593 because its services are being used.
594 @retval other The child handle was not destroyed
599 Dhcp4ServiceBindingDestroyChild (
600 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
601 IN EFI_HANDLE ChildHandle
604 DHCP_SERVICE
*DhcpSb
;
605 DHCP_PROTOCOL
*Instance
;
606 EFI_DHCP4_PROTOCOL
*Dhcp
;
610 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
611 return EFI_INVALID_PARAMETER
;
615 // Retrieve the private context data structures
617 Status
= gBS
->OpenProtocol (
619 &gEfiDhcp4ProtocolGuid
,
621 gDhcp4DriverBinding
.DriverBindingHandle
,
623 EFI_OPEN_PROTOCOL_GET_PROTOCOL
626 if (EFI_ERROR (Status
)) {
627 return EFI_UNSUPPORTED
;
630 Instance
= DHCP_INSTANCE_FROM_THIS (Dhcp
);
631 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
633 if (Instance
->Service
!= DhcpSb
) {
634 return EFI_INVALID_PARAMETER
;
638 // A child can be destroyed more than once. For example,
639 // Dhcp4DriverBindingStop will destroy all of its children.
640 // when caller driver is being stopped, it will destory the
641 // dhcp child it opens.
643 if (Instance
->InDestory
) {
647 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
648 Instance
->InDestory
= TRUE
;
651 // Close the Udp4 protocol.
654 DhcpSb
->UdpIo
->UdpHandle
,
655 &gEfiUdp4ProtocolGuid
,
656 gDhcp4DriverBinding
.DriverBindingHandle
,
661 // Uninstall the DHCP4 protocol first to enable a top down destruction.
663 Status
= gBS
->UninstallProtocolInterface (
665 &gEfiDhcp4ProtocolGuid
,
669 if (EFI_ERROR (Status
)) {
670 Instance
->InDestory
= FALSE
;
672 gBS
->RestoreTPL (OldTpl
);
676 if (DhcpSb
->ActiveChild
== Instance
) {
677 DhcpYieldControl (DhcpSb
);
680 RemoveEntryList (&Instance
->Link
);
681 DhcpSb
->NumChildren
--;
683 gBS
->RestoreTPL (OldTpl
);