3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
4 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.
123 EFI_UDP4_CONFIG_DATA UdpConfigData
;
125 UdpConfigData
.AcceptBroadcast
= TRUE
;
126 UdpConfigData
.AcceptPromiscuous
= FALSE
;
127 UdpConfigData
.AcceptAnyPort
= FALSE
;
128 UdpConfigData
.AllowDuplicatePort
= TRUE
;
129 UdpConfigData
.TypeOfService
= 0;
130 UdpConfigData
.TimeToLive
= 64;
131 UdpConfigData
.DoNotFragment
= FALSE
;
132 UdpConfigData
.ReceiveTimeout
= 0;
133 UdpConfigData
.TransmitTimeout
= 0;
135 UdpConfigData
.UseDefaultAddress
= FALSE
;
136 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
137 UdpConfigData
.RemotePort
= DHCP_SERVER_PORT
;
139 ZeroMem (&UdpConfigData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
140 ZeroMem (&UdpConfigData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
141 ZeroMem (&UdpConfigData
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
143 return UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfigData
);;
149 Destory the DHCP service. The Dhcp4 service may be partly initialized,
150 or partly destroyed. If a resource is destroyed, it is marked as so in
151 case the destroy failed and being called again later.
153 @param[in] DhcpSb The DHCP service instance to destory.
155 @retval EFI_SUCCESS Always return success.
160 IN DHCP_SERVICE
*DhcpSb
163 DhcpCleanLease (DhcpSb
);
165 if (DhcpSb
->UdpIo
!= NULL
) {
166 UdpIoFreeIo (DhcpSb
->UdpIo
);
167 DhcpSb
->UdpIo
= NULL
;
170 if (DhcpSb
->Timer
!= NULL
) {
171 gBS
->SetTimer (DhcpSb
->Timer
, TimerCancel
, 0);
172 gBS
->CloseEvent (DhcpSb
->Timer
);
174 DhcpSb
->Timer
= NULL
;
183 Create a new DHCP service binding instance for the controller.
185 @param[in] Controller The controller to install DHCP service binding
187 @param[in] ImageHandle The driver's image handle
188 @param[out] Service The variable to receive the created DHCP service
191 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .
192 @retval EFI_SUCCESS The DHCP service instance is created.
193 @retval other Other error occurs.
198 IN EFI_HANDLE Controller
,
199 IN EFI_HANDLE ImageHandle
,
200 OUT DHCP_SERVICE
**Service
203 DHCP_SERVICE
*DhcpSb
;
207 DhcpSb
= AllocateZeroPool (sizeof (DHCP_SERVICE
));
209 if (DhcpSb
== NULL
) {
210 return EFI_OUT_OF_RESOURCES
;
213 DhcpSb
->Signature
= DHCP_SERVICE_SIGNATURE
;
214 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
215 DhcpSb
->InDestory
= FALSE
;
216 DhcpSb
->Controller
= Controller
;
217 DhcpSb
->Image
= ImageHandle
;
218 InitializeListHead (&DhcpSb
->Children
);
219 DhcpSb
->DhcpState
= Dhcp4Stopped
;
220 DhcpSb
->Xid
= NET_RANDOM (NetRandomInitSeed ());
222 &DhcpSb
->ServiceBinding
,
223 &mDhcp4ServiceBindingTemplate
,
224 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
227 // Create various resources, UdpIo, Timer, and get Mac address
229 Status
= gBS
->CreateEvent (
230 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
237 if (EFI_ERROR (Status
)) {
241 DhcpSb
->UdpIo
= UdpIoCreateIo (
249 if (DhcpSb
->UdpIo
== NULL
) {
250 Status
= EFI_OUT_OF_RESOURCES
;
254 DhcpSb
->HwLen
= (UINT8
) DhcpSb
->UdpIo
->SnpMode
.HwAddressSize
;
255 DhcpSb
->HwType
= DhcpSb
->UdpIo
->SnpMode
.IfType
;
256 CopyMem (&DhcpSb
->Mac
, &DhcpSb
->UdpIo
->SnpMode
.CurrentAddress
, sizeof (DhcpSb
->Mac
));
262 Dhcp4CloseService (DhcpSb
);
270 Start this driver on ControllerHandle. This service is called by the
271 EFI boot service ConnectController(). In order to make
272 drivers as small as possible, there are a few calling restrictions for
273 this service. ConnectController() must follow these
274 calling restrictions. If any other agent wishes to call Start() it
275 must also follow these calling restrictions.
277 @param[in] This Protocol instance pointer.
278 @param[in] ControllerHandle Handle of device to bind driver to
279 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
282 @retval EFI_SUCCESS This driver is added to ControllerHandle
283 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
284 @retval other This driver does not support this device
289 Dhcp4DriverBindingStart (
290 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
291 IN EFI_HANDLE ControllerHandle
,
292 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
295 DHCP_SERVICE
*DhcpSb
;
299 // First: test for the DHCP4 Protocol
301 Status
= gBS
->OpenProtocol (
303 &gEfiDhcp4ServiceBindingProtocolGuid
,
305 This
->DriverBindingHandle
,
307 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
310 if (Status
== EFI_SUCCESS
) {
311 return EFI_ALREADY_STARTED
;
314 Status
= Dhcp4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &DhcpSb
);
316 if (EFI_ERROR (Status
)) {
319 ASSERT (DhcpSb
!= NULL
);
322 // Start the receiving
324 Status
= UdpIoRecvDatagram (DhcpSb
->UdpIo
, DhcpInput
, DhcpSb
, 0);
326 if (EFI_ERROR (Status
)) {
329 Status
= gBS
->SetTimer (DhcpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
331 if (EFI_ERROR (Status
)) {
336 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
338 Status
= gBS
->InstallMultipleProtocolInterfaces (
340 &gEfiDhcp4ServiceBindingProtocolGuid
,
341 &DhcpSb
->ServiceBinding
,
345 if (EFI_ERROR (Status
)) {
352 Dhcp4CloseService (DhcpSb
);
359 Stop this driver on ControllerHandle. This service is called by the
360 EFI boot service DisconnectController(). In order to
361 make drivers as small as possible, there are a few calling
362 restrictions for this service. DisconnectController()
363 must follow these calling restrictions. If any other agent wishes
364 to call Stop() it must also follow these calling restrictions.
366 @param[in] This Protocol instance pointer.
367 @param[in] ControllerHandle Handle of device to stop driver on
368 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
369 children is zero stop the entire bus driver.
370 @param[in] ChildHandleBuffer List of Child Handles to Stop.
372 @retval EFI_SUCCESS This driver is removed ControllerHandle
373 @retval other This driver was not removed from this device
378 Dhcp4DriverBindingStop (
379 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
380 IN EFI_HANDLE ControllerHandle
,
381 IN UINTN NumberOfChildren
,
382 IN EFI_HANDLE
*ChildHandleBuffer
385 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
386 DHCP_SERVICE
*DhcpSb
;
387 DHCP_PROTOCOL
*Instance
;
388 EFI_HANDLE NicHandle
;
393 // DHCP driver opens UDP child, So, the ControllerHandle is the
394 // UDP child handle. locate the Nic handle first.
396 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp4ProtocolGuid
);
398 if (NicHandle
== NULL
) {
399 return EFI_DEVICE_ERROR
;
402 Status
= gBS
->OpenProtocol (
404 &gEfiDhcp4ServiceBindingProtocolGuid
,
405 (VOID
**) &ServiceBinding
,
406 This
->DriverBindingHandle
,
408 EFI_OPEN_PROTOCOL_GET_PROTOCOL
411 if (EFI_ERROR (Status
)) {
412 return EFI_DEVICE_ERROR
;
415 DhcpSb
= DHCP_SERVICE_FROM_THIS (ServiceBinding
);
417 if (DhcpSb
->InDestory
) {
421 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
423 if (NumberOfChildren
== 0) {
425 DhcpSb
->InDestory
= TRUE
;
426 DhcpSb
->ServiceState
= DHCP_DESTORY
;
428 gBS
->UninstallProtocolInterface (
430 &gEfiDhcp4ServiceBindingProtocolGuid
,
434 Dhcp4CloseService (DhcpSb
);
439 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild
440 // may cause other child to be deleted.
442 while (!IsListEmpty (&DhcpSb
->Children
)) {
443 Instance
= NET_LIST_HEAD (&DhcpSb
->Children
, DHCP_PROTOCOL
, Link
);
444 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
447 if (DhcpSb
->NumChildren
!= 0) {
448 Status
= EFI_DEVICE_ERROR
;
452 gBS
->RestoreTPL (OldTpl
);
459 Initialize a new DHCP instance.
461 @param DhcpSb The dhcp service instance
462 @param Instance The dhcp instance to initialize
467 IN DHCP_SERVICE
*DhcpSb
,
468 IN OUT DHCP_PROTOCOL
*Instance
471 Instance
->Signature
= DHCP_PROTOCOL_SIGNATURE
;
472 CopyMem (&Instance
->Dhcp4Protocol
, &mDhcp4ProtocolTemplate
, sizeof (Instance
->Dhcp4Protocol
));
473 InitializeListHead (&Instance
->Link
);
474 Instance
->Handle
= NULL
;
475 Instance
->Service
= DhcpSb
;
476 Instance
->InDestory
= FALSE
;
477 Instance
->CompletionEvent
= NULL
;
478 Instance
->RenewRebindEvent
= NULL
;
479 Instance
->Token
= NULL
;
480 Instance
->UdpIo
= NULL
;
481 NetbufQueInit (&Instance
->ResponseQueue
);
486 Creates a child handle and installs a protocol.
488 The CreateChild() function installs a protocol on ChildHandle.
489 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
490 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
492 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
493 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
494 then a new handle is created. If it is a pointer to an existing UEFI handle,
495 then the protocol is added to the existing UEFI handle.
497 @retval EFI_SUCCES The protocol was added to ChildHandle.
498 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
499 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
501 @retval other The child handle was not created
506 Dhcp4ServiceBindingCreateChild (
507 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
508 IN EFI_HANDLE
*ChildHandle
511 DHCP_SERVICE
*DhcpSb
;
512 DHCP_PROTOCOL
*Instance
;
517 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
518 return EFI_INVALID_PARAMETER
;
521 Instance
= AllocatePool (sizeof (*Instance
));
523 if (Instance
== NULL
) {
524 return EFI_OUT_OF_RESOURCES
;
527 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
528 DhcpInitProtocol (DhcpSb
, Instance
);
531 // Install DHCP4 onto ChildHandle
533 Status
= gBS
->InstallMultipleProtocolInterfaces (
535 &gEfiDhcp4ProtocolGuid
,
536 &Instance
->Dhcp4Protocol
,
540 if (EFI_ERROR (Status
)) {
545 Instance
->Handle
= *ChildHandle
;
548 // Open the Udp4 protocol BY_CHILD.
550 Status
= gBS
->OpenProtocol (
551 DhcpSb
->UdpIo
->UdpHandle
,
552 &gEfiUdp4ProtocolGuid
,
554 gDhcp4DriverBinding
.DriverBindingHandle
,
556 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
558 if (EFI_ERROR (Status
)) {
559 gBS
->UninstallMultipleProtocolInterfaces (
561 &gEfiDhcp4ProtocolGuid
,
562 &Instance
->Dhcp4Protocol
,
570 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
572 InsertTailList (&DhcpSb
->Children
, &Instance
->Link
);
573 DhcpSb
->NumChildren
++;
575 gBS
->RestoreTPL (OldTpl
);
582 Destroys a child handle with a protocol installed on it.
584 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
585 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
586 last protocol on ChildHandle, then ChildHandle is destroyed.
588 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
589 @param ChildHandle Handle of the child to destroy
591 @retval EFI_SUCCES The protocol was removed from ChildHandle.
592 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
593 @retval EFI_INVALID_PARAMETER Child handle is NULL.
594 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
595 because its services are being used.
596 @retval other The child handle was not destroyed
601 Dhcp4ServiceBindingDestroyChild (
602 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
603 IN EFI_HANDLE ChildHandle
606 DHCP_SERVICE
*DhcpSb
;
607 DHCP_PROTOCOL
*Instance
;
608 EFI_DHCP4_PROTOCOL
*Dhcp
;
612 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
613 return EFI_INVALID_PARAMETER
;
617 // Retrieve the private context data structures
619 Status
= gBS
->OpenProtocol (
621 &gEfiDhcp4ProtocolGuid
,
623 gDhcp4DriverBinding
.DriverBindingHandle
,
625 EFI_OPEN_PROTOCOL_GET_PROTOCOL
628 if (EFI_ERROR (Status
)) {
629 return EFI_UNSUPPORTED
;
632 Instance
= DHCP_INSTANCE_FROM_THIS (Dhcp
);
633 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
635 if (Instance
->Service
!= DhcpSb
) {
636 return EFI_INVALID_PARAMETER
;
640 // A child can be destroyed more than once. For example,
641 // Dhcp4DriverBindingStop will destroy all of its children.
642 // when caller driver is being stopped, it will destory the
643 // dhcp child it opens.
645 if (Instance
->InDestory
) {
649 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
650 Instance
->InDestory
= TRUE
;
653 // Close the Udp4 protocol.
656 DhcpSb
->UdpIo
->UdpHandle
,
657 &gEfiUdp4ProtocolGuid
,
658 gDhcp4DriverBinding
.DriverBindingHandle
,
663 // Uninstall the DHCP4 protocol first to enable a top down destruction.
665 Status
= gBS
->UninstallProtocolInterface (
667 &gEfiDhcp4ProtocolGuid
,
671 if (EFI_ERROR (Status
)) {
672 Instance
->InDestory
= FALSE
;
674 gBS
->RestoreTPL (OldTpl
);
678 if (DhcpSb
->ActiveChild
== Instance
) {
679 DhcpYieldControl (DhcpSb
);
682 RemoveEntryList (&Instance
->Link
);
683 DhcpSb
->NumChildren
--;
685 gBS
->RestoreTPL (OldTpl
);