3 Copyright (c) 2006 - 2012, 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 Destroy 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 destroy.
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
->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
)) {
318 ASSERT (DhcpSb
!= NULL
);
321 // Start the receiving
323 Status
= UdpIoRecvDatagram (DhcpSb
->UdpIo
, DhcpInput
, DhcpSb
, 0);
325 if (EFI_ERROR (Status
)) {
328 Status
= gBS
->SetTimer (DhcpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
330 if (EFI_ERROR (Status
)) {
335 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
337 Status
= gBS
->InstallMultipleProtocolInterfaces (
339 &gEfiDhcp4ServiceBindingProtocolGuid
,
340 &DhcpSb
->ServiceBinding
,
344 if (EFI_ERROR (Status
)) {
351 Dhcp4CloseService (DhcpSb
);
357 Callback function which provided by user to remove one node in NetDestroyLinkList process.
359 @param[in] Entry The entry to be removed.
360 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
362 @retval EFI_SUCCESS The entry has been removed successfully.
363 @retval Others Fail to remove the entry.
367 Dhcp4DestroyChildEntry (
368 IN LIST_ENTRY
*Entry
,
372 DHCP_PROTOCOL
*Instance
;
373 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
375 if (Entry
== NULL
|| Context
== NULL
) {
376 return EFI_INVALID_PARAMETER
;
379 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP_PROTOCOL
, Link
, DHCP_PROTOCOL_SIGNATURE
);
380 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*) Context
;
382 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
387 Stop this driver on ControllerHandle. This service is called by the
388 EFI boot service DisconnectController(). In order to
389 make drivers as small as possible, there are a few calling
390 restrictions for this service. DisconnectController()
391 must follow these calling restrictions. If any other agent wishes
392 to call Stop() it must also follow these calling restrictions.
394 @param[in] This Protocol instance pointer.
395 @param[in] ControllerHandle Handle of device to stop driver on
396 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
397 children is zero stop the entire bus driver.
398 @param[in] ChildHandleBuffer List of Child Handles to Stop.
400 @retval EFI_SUCCESS This driver is removed ControllerHandle
401 @retval other This driver was not removed from this device
406 Dhcp4DriverBindingStop (
407 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
408 IN EFI_HANDLE ControllerHandle
,
409 IN UINTN NumberOfChildren
,
410 IN EFI_HANDLE
*ChildHandleBuffer
413 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
414 DHCP_SERVICE
*DhcpSb
;
415 EFI_HANDLE NicHandle
;
421 // DHCP driver opens UDP child, So, the ControllerHandle is the
422 // UDP child handle. locate the Nic handle first.
424 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp4ProtocolGuid
);
426 if (NicHandle
== NULL
) {
430 Status
= gBS
->OpenProtocol (
432 &gEfiDhcp4ServiceBindingProtocolGuid
,
433 (VOID
**) &ServiceBinding
,
434 This
->DriverBindingHandle
,
436 EFI_OPEN_PROTOCOL_GET_PROTOCOL
439 if (EFI_ERROR (Status
)) {
440 return EFI_DEVICE_ERROR
;
443 DhcpSb
= DHCP_SERVICE_FROM_THIS (ServiceBinding
);
444 if (!IsListEmpty (&DhcpSb
->Children
)) {
446 // Destroy all the children instances before destory the service.
448 List
= &DhcpSb
->Children
;
449 Status
= NetDestroyLinkList (
451 Dhcp4DestroyChildEntry
,
455 if (EFI_ERROR (Status
) || ListLength
!= 0) {
456 Status
= EFI_DEVICE_ERROR
;
460 if (NumberOfChildren
== 0 && !IsListEmpty (&DhcpSb
->Children
)) {
461 Status
= EFI_DEVICE_ERROR
;
464 if (NumberOfChildren
== 0 && IsListEmpty (&DhcpSb
->Children
)) {
466 // Destroy the service itself if no child instance left.
468 DhcpSb
->ServiceState
= DHCP_DESTROY
;
470 gBS
->UninstallProtocolInterface (
472 &gEfiDhcp4ServiceBindingProtocolGuid
,
476 Dhcp4CloseService (DhcpSb
);
478 if (gDhcpControllerNameTable
!= NULL
) {
479 FreeUnicodeStringTable (gDhcpControllerNameTable
);
480 gDhcpControllerNameTable
= NULL
;
484 Status
= EFI_SUCCESS
;
492 Initialize a new DHCP instance.
494 @param DhcpSb The dhcp service instance
495 @param Instance The dhcp instance to initialize
500 IN DHCP_SERVICE
*DhcpSb
,
501 IN OUT DHCP_PROTOCOL
*Instance
504 Instance
->Signature
= DHCP_PROTOCOL_SIGNATURE
;
505 CopyMem (&Instance
->Dhcp4Protocol
, &mDhcp4ProtocolTemplate
, sizeof (Instance
->Dhcp4Protocol
));
506 InitializeListHead (&Instance
->Link
);
507 Instance
->Handle
= NULL
;
508 Instance
->Service
= DhcpSb
;
509 Instance
->InDestroy
= FALSE
;
510 Instance
->CompletionEvent
= NULL
;
511 Instance
->RenewRebindEvent
= NULL
;
512 Instance
->Token
= NULL
;
513 Instance
->UdpIo
= NULL
;
514 Instance
->ElaspedTime
= 0;
515 NetbufQueInit (&Instance
->ResponseQueue
);
520 Creates a child handle and installs a protocol.
522 The CreateChild() function installs a protocol on ChildHandle.
523 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
524 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
526 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
527 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
528 then a new handle is created. If it is a pointer to an existing UEFI handle,
529 then the protocol is added to the existing UEFI handle.
531 @retval EFI_SUCCES The protocol was added to ChildHandle.
532 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
533 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
535 @retval other The child handle was not created
540 Dhcp4ServiceBindingCreateChild (
541 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
542 IN EFI_HANDLE
*ChildHandle
545 DHCP_SERVICE
*DhcpSb
;
546 DHCP_PROTOCOL
*Instance
;
551 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
552 return EFI_INVALID_PARAMETER
;
555 Instance
= AllocatePool (sizeof (*Instance
));
557 if (Instance
== NULL
) {
558 return EFI_OUT_OF_RESOURCES
;
561 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
562 DhcpInitProtocol (DhcpSb
, Instance
);
565 // Install DHCP4 onto ChildHandle
567 Status
= gBS
->InstallMultipleProtocolInterfaces (
569 &gEfiDhcp4ProtocolGuid
,
570 &Instance
->Dhcp4Protocol
,
574 if (EFI_ERROR (Status
)) {
579 Instance
->Handle
= *ChildHandle
;
582 // Open the Udp4 protocol BY_CHILD.
584 Status
= gBS
->OpenProtocol (
585 DhcpSb
->UdpIo
->UdpHandle
,
586 &gEfiUdp4ProtocolGuid
,
588 gDhcp4DriverBinding
.DriverBindingHandle
,
590 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
592 if (EFI_ERROR (Status
)) {
593 gBS
->UninstallMultipleProtocolInterfaces (
595 &gEfiDhcp4ProtocolGuid
,
596 &Instance
->Dhcp4Protocol
,
604 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
606 InsertTailList (&DhcpSb
->Children
, &Instance
->Link
);
607 DhcpSb
->NumChildren
++;
609 gBS
->RestoreTPL (OldTpl
);
616 Destroys a child handle with a protocol installed on it.
618 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
619 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
620 last protocol on ChildHandle, then ChildHandle is destroyed.
622 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
623 @param ChildHandle Handle of the child to destroy
625 @retval EFI_SUCCES The protocol was removed from ChildHandle.
626 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
627 @retval EFI_INVALID_PARAMETER Child handle is NULL.
628 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
629 because its services are being used.
630 @retval other The child handle was not destroyed
635 Dhcp4ServiceBindingDestroyChild (
636 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
637 IN EFI_HANDLE ChildHandle
640 DHCP_SERVICE
*DhcpSb
;
641 DHCP_PROTOCOL
*Instance
;
642 EFI_DHCP4_PROTOCOL
*Dhcp
;
646 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
647 return EFI_INVALID_PARAMETER
;
651 // Retrieve the private context data structures
653 Status
= gBS
->OpenProtocol (
655 &gEfiDhcp4ProtocolGuid
,
657 gDhcp4DriverBinding
.DriverBindingHandle
,
659 EFI_OPEN_PROTOCOL_GET_PROTOCOL
662 if (EFI_ERROR (Status
)) {
663 return EFI_UNSUPPORTED
;
666 Instance
= DHCP_INSTANCE_FROM_THIS (Dhcp
);
667 DhcpSb
= DHCP_SERVICE_FROM_THIS (This
);
669 if (Instance
->Service
!= DhcpSb
) {
670 return EFI_INVALID_PARAMETER
;
674 // A child can be destroyed more than once. For example,
675 // Dhcp4DriverBindingStop will destroy all of its children.
676 // when caller driver is being stopped, it will destroy the
677 // dhcp child it opens.
679 if (Instance
->InDestroy
) {
683 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
684 Instance
->InDestroy
= TRUE
;
687 // Close the Udp4 protocol.
690 DhcpSb
->UdpIo
->UdpHandle
,
691 &gEfiUdp4ProtocolGuid
,
692 gDhcp4DriverBinding
.DriverBindingHandle
,
697 // Uninstall the DHCP4 protocol first to enable a top down destruction.
699 gBS
->RestoreTPL (OldTpl
);
700 Status
= gBS
->UninstallProtocolInterface (
702 &gEfiDhcp4ProtocolGuid
,
705 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
706 if (EFI_ERROR (Status
)) {
707 Instance
->InDestroy
= FALSE
;
709 gBS
->RestoreTPL (OldTpl
);
713 if (DhcpSb
->ActiveChild
== Instance
) {
714 DhcpYieldControl (DhcpSb
);
717 RemoveEntryList (&Instance
->Link
);
718 DhcpSb
->NumChildren
--;
720 gBS
->RestoreTPL (OldTpl
);