3 Copyright (c) 2006 - 2008, 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 port to configure
110 @param[in] Context The context to the function
112 @retval EFI_SUCCESS The UDP IO port is successfully configured.
113 @retval Others Failed to configure the UDP child.
118 IN UDP_IO_PORT
*UdpIo
,
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
->Udp
->Configure (UdpIo
->Udp
, &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 UdpIoFreePort (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
= UdpIoCreatePort (Controller
, ImageHandle
, DhcpConfigUdpIo
, NULL
);
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
)) {
313 Status
= gBS
->SetTimer (DhcpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
315 if (EFI_ERROR (Status
)) {
320 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
322 Status
= gBS
->InstallMultipleProtocolInterfaces (
324 &gEfiDhcp4ServiceBindingProtocolGuid
,
325 &DhcpSb
->ServiceBinding
,
329 if (EFI_ERROR (Status
)) {
336 Dhcp4CloseService (DhcpSb
);
343 Stop this driver on ControllerHandle. This service is called by the
344 EFI boot service DisconnectController(). In order to
345 make drivers as small as possible, there are a few calling
346 restrictions for this service. DisconnectController()
347 must follow these calling restrictions. If any other agent wishes
348 to call Stop() it must also follow these calling restrictions.
350 @param[in] This Protocol instance pointer.
351 @param[in] ControllerHandle Handle of device to stop driver on
352 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
353 children is zero stop the entire bus driver.
354 @param[in] ChildHandleBuffer List of Child Handles to Stop.
356 @retval EFI_SUCCESS This driver is removed ControllerHandle
357 @retval other This driver was not removed from this device
362 Dhcp4DriverBindingStop (
363 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
364 IN EFI_HANDLE ControllerHandle
,
365 IN UINTN NumberOfChildren
,
366 IN EFI_HANDLE
*ChildHandleBuffer
369 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
370 DHCP_SERVICE
*DhcpSb
;
371 DHCP_PROTOCOL
*Instance
;
372 EFI_HANDLE NicHandle
;
377 // DHCP driver opens UDP child, So, the ControllerHandle is the
378 // UDP child handle. locate the Nic handle first.
380 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp4ProtocolGuid
);
382 if (NicHandle
== NULL
) {
383 return EFI_DEVICE_ERROR
;
386 Status
= gBS
->OpenProtocol (
388 &gEfiDhcp4ServiceBindingProtocolGuid
,
389 (VOID
**) &ServiceBinding
,
390 This
->DriverBindingHandle
,
392 EFI_OPEN_PROTOCOL_GET_PROTOCOL
395 if (EFI_ERROR (Status
)) {
396 return EFI_DEVICE_ERROR
;
399 DhcpSb
= DHCP_SERVICE_FROM_THIS (ServiceBinding
);
401 if (DhcpSb
->InDestory
) {
405 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
407 if (NumberOfChildren
== 0) {
409 DhcpSb
->InDestory
= TRUE
;
410 DhcpSb
->ServiceState
= DHCP_DESTORY
;
412 gBS
->UninstallProtocolInterface (
414 &gEfiDhcp4ServiceBindingProtocolGuid
,
418 Dhcp4CloseService (DhcpSb
);
423 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild
424 // may cause other child to be deleted.
426 while (!IsListEmpty (&DhcpSb
->Children
)) {
427 Instance
= NET_LIST_HEAD (&DhcpSb
->Children
, DHCP_PROTOCOL
, Link
);
428 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
431 if (DhcpSb
->NumChildren
!= 0) {
432 Status
= EFI_DEVICE_ERROR
;
436 gBS
->RestoreTPL (OldTpl
);
443 Initialize a new DHCP instance.
445 @param DhcpSb The dhcp service instance
446 @param Instance The dhcp instance to initialize
451 IN DHCP_SERVICE
*DhcpSb
,
452 IN OUT DHCP_PROTOCOL
*Instance
455 Instance
->Signature
= DHCP_PROTOCOL_SIGNATURE
;
456 CopyMem (&Instance
->Dhcp4Protocol
, &mDhcp4ProtocolTemplate
, sizeof (Instance
->Dhcp4Protocol
));
457 InitializeListHead (&Instance
->Link
);
458 Instance
->Handle
= NULL
;
459 Instance
->Service
= DhcpSb
;
460 Instance
->InDestory
= FALSE
;
461 Instance
->CompletionEvent
= NULL
;
462 Instance
->RenewRebindEvent
= NULL
;
463 Instance
->Token
= NULL
;
464 Instance
->UdpIo
= NULL
;
465 NetbufQueInit (&Instance
->ResponseQueue
);
470 Creates a child handle and installs a protocol.
472 The CreateChild() function installs a protocol on ChildHandle.
473 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
474 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
476 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
477 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
478 then a new handle is created. If it is a pointer to an existing UEFI handle,
479 then the protocol is added to the existing UEFI handle.
481 @retval EFI_SUCCES The protocol was added to ChildHandle.
482 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
483 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
485 @retval other The child handle was not created
490 Dhcp4ServiceBindingCreateChild (
491 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
492 IN EFI_HANDLE
*ChildHandle
495 DHCP_SERVICE
*DhcpSb
;
496 DHCP_PROTOCOL
*Instance
;
501 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
502 return EFI_INVALID_PARAMETER
;
505 Instance
= AllocatePool (sizeof (*Instance
));
507 if (Instance
== NULL
) {
508 return EFI_OUT_OF_RESOURCES
;
511 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
512 DhcpInitProtocol (DhcpSb
, Instance
);
515 // Install DHCP4 onto ChildHandle
517 Status
= gBS
->InstallMultipleProtocolInterfaces (
519 &gEfiDhcp4ProtocolGuid
,
520 &Instance
->Dhcp4Protocol
,
524 if (EFI_ERROR (Status
)) {
529 Instance
->Handle
= *ChildHandle
;
532 // Open the Udp4 protocol BY_CHILD.
534 Status
= gBS
->OpenProtocol (
535 DhcpSb
->UdpIo
->UdpHandle
,
536 &gEfiUdp4ProtocolGuid
,
538 gDhcp4DriverBinding
.DriverBindingHandle
,
540 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
542 if (EFI_ERROR (Status
)) {
543 gBS
->UninstallMultipleProtocolInterfaces (
545 &gEfiDhcp4ProtocolGuid
,
546 &Instance
->Dhcp4Protocol
,
554 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
556 InsertTailList (&DhcpSb
->Children
, &Instance
->Link
);
557 DhcpSb
->NumChildren
++;
559 gBS
->RestoreTPL (OldTpl
);
566 Destroys a child handle with a protocol installed on it.
568 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
569 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
570 last protocol on ChildHandle, then ChildHandle is destroyed.
572 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
573 @param ChildHandle Handle of the child to destroy
575 @retval EFI_SUCCES The protocol was removed from ChildHandle.
576 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
577 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
578 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
579 because its services are being used.
580 @retval other The child handle was not destroyed
585 Dhcp4ServiceBindingDestroyChild (
586 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
587 IN EFI_HANDLE ChildHandle
590 DHCP_SERVICE
*DhcpSb
;
591 DHCP_PROTOCOL
*Instance
;
592 EFI_DHCP4_PROTOCOL
*Dhcp
;
596 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
597 return EFI_INVALID_PARAMETER
;
601 // Retrieve the private context data structures
603 Status
= gBS
->OpenProtocol (
605 &gEfiDhcp4ProtocolGuid
,
607 gDhcp4DriverBinding
.DriverBindingHandle
,
609 EFI_OPEN_PROTOCOL_GET_PROTOCOL
612 if (EFI_ERROR (Status
)) {
613 return EFI_UNSUPPORTED
;
616 Instance
= DHCP_INSTANCE_FROM_THIS (Dhcp
);
617 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
619 if (Instance
->Service
!= DhcpSb
) {
620 return EFI_INVALID_PARAMETER
;
624 // A child can be destroyed more than once. For example,
625 // Dhcp4DriverBindingStop will destroy all of its children.
626 // when caller driver is being stopped, it will destory the
627 // dhcp child it opens.
629 if (Instance
->InDestory
) {
633 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
634 Instance
->InDestory
= TRUE
;
637 // Close the Udp4 protocol.
640 DhcpSb
->UdpIo
->UdpHandle
,
641 &gEfiUdp4ProtocolGuid
,
642 gDhcp4DriverBinding
.DriverBindingHandle
,
647 // Uninstall the DHCP4 protocol first to enable a top down destruction.
649 Status
= gBS
->UninstallProtocolInterface (
651 &gEfiDhcp4ProtocolGuid
,
655 if (EFI_ERROR (Status
)) {
656 Instance
->InDestory
= FALSE
;
658 gBS
->RestoreTPL (OldTpl
);
662 if (DhcpSb
->ActiveChild
== Instance
) {
663 DhcpYieldControl (DhcpSb
);
666 RemoveEntryList (&Instance
->Link
);
667 DhcpSb
->NumChildren
--;
669 gBS
->RestoreTPL (OldTpl
);