3 Copyright (c) 2006, Intel Corporation
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.
24 EFI_DRIVER_BINDING_PROTOCOL gUdp4DriverBinding
= {
25 Udp4DriverBindingSupported
,
26 Udp4DriverBindingStart
,
27 Udp4DriverBindingStop
,
33 EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
= {
34 Udp4ServiceBindingCreateChild
,
35 Udp4ServiceBindingDestroyChild
40 Test to see if this driver supports ControllerHandle.
42 @param This Protocol instance pointer.
43 @param ControllerHandle Handle of device to test.
44 @param RemainingDevicePath Optional parameter use to pick a specific child
47 @retval EFI_SUCCES This driver supports this device.
48 @retval EFI_ALREADY_STARTED This driver is already running on this device.
53 Udp4DriverBindingSupported (
54 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
55 IN EFI_HANDLE ControllerHandle
,
56 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
62 // Test for the Udp4ServiceBinding Protocol
64 Status
= gBS
->OpenProtocol (
66 &gEfiUdp4ServiceBindingProtocolGuid
,
68 This
->DriverBindingHandle
,
70 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
72 if (!EFI_ERROR (Status
)) {
73 return EFI_ALREADY_STARTED
;
77 // Test for the Ip4 Protocol
79 Status
= gBS
->OpenProtocol (
81 &gEfiIp4ServiceBindingProtocolGuid
,
83 This
->DriverBindingHandle
,
85 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
93 Start this driver on ControllerHandle.
95 @param This Protocol instance pointer.
96 @param ControllerHandle Handle of device to bind driver to
97 @param RemainingDevicePath Optional parameter use to pick a specific child
100 @retval EFI_SUCCES This driver is added to ControllerHandle
101 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
102 @retval other This driver does not support this device
107 Udp4DriverBindingStart (
108 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
109 IN EFI_HANDLE ControllerHandle
,
110 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
114 UDP4_SERVICE_DATA
*Udp4Service
;
117 // Allocate Private Context Data Structure.
119 Udp4Service
= NetAllocatePool (sizeof (UDP4_SERVICE_DATA
));
120 if (Udp4Service
== NULL
) {
121 return EFI_OUT_OF_RESOURCES
;
124 Status
= Udp4CreateService (Udp4Service
, This
->DriverBindingHandle
, ControllerHandle
);
125 if (EFI_ERROR (Status
)) {
130 // Install the Udp4ServiceBindingProtocol on the ControllerHandle.
132 Status
= gBS
->InstallMultipleProtocolInterfaces (
134 &gEfiUdp4ServiceBindingProtocolGuid
,
135 &Udp4Service
->ServiceBinding
,
138 if (EFI_ERROR (Status
)) {
142 Udp4SetVariableData (Udp4Service
);
148 Udp4CleanService (Udp4Service
);
152 NetFreePool (Udp4Service
);
159 Stop this driver on ControllerHandle.
161 @param This Protocol instance pointer.
162 @param ControllerHandle Handle of device to stop driver on
163 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
164 of children is zero stop the entire bus driver.
165 @param ChildHandleBuffer List of Child Handles to Stop.
167 @retval EFI_SUCCES This driver is removed ControllerHandle.
168 @retval other This driver was not removed from this device.
173 Udp4DriverBindingStop (
174 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
175 IN EFI_HANDLE ControllerHandle
,
176 IN UINTN NumberOfChildren
,
177 IN EFI_HANDLE
*ChildHandleBuffer
181 EFI_HANDLE NicHandle
;
182 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
183 UDP4_SERVICE_DATA
*Udp4Service
;
184 UDP4_INSTANCE_DATA
*Instance
;
187 // Find the NicHandle where UDP4 ServiceBinding Protocol is installed.
189 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
190 if (NicHandle
== NULL
) {
195 // Retrieve the UDP4 ServiceBinding Protocol.
197 Status
= gBS
->OpenProtocol (
199 &gEfiUdp4ServiceBindingProtocolGuid
,
200 (VOID
**) &ServiceBinding
,
201 This
->DriverBindingHandle
,
203 EFI_OPEN_PROTOCOL_GET_PROTOCOL
205 if (EFI_ERROR (Status
)) {
206 return EFI_DEVICE_ERROR
;
209 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding
);
212 // Uninstall the UDP4 ServiceBinding Protocol.
214 Status
= gBS
->UninstallMultipleProtocolInterfaces (
216 &gEfiUdp4ServiceBindingProtocolGuid
,
217 &Udp4Service
->ServiceBinding
,
220 if (EFI_ERROR (Status
)) {
221 return EFI_DEVICE_ERROR
;
224 while (!NetListIsEmpty (&Udp4Service
->ChildrenList
)) {
226 // Destroy all instances.
228 Instance
= NET_LIST_HEAD (&Udp4Service
->ChildrenList
, UDP4_INSTANCE_DATA
, Link
);
230 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->ChildHandle
);
233 Udp4ClearVariableData (Udp4Service
);
235 Udp4CleanService (Udp4Service
);
237 NetFreePool (Udp4Service
);
244 Creates a child handle with a set of I/O services.
246 @param This Protocol instance pointer.
247 @param ChildHandle Pointer to the handle of the child to create. If
248 it is NULL, then a new handle is created. If it
249 is not NULL, then the I/O services are added to
250 the existing child handle.
252 @retval EFI_SUCCES The child handle was created with the I/O services
253 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
255 @retval other The child handle was not created
260 Udp4ServiceBindingCreateChild (
261 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
262 IN EFI_HANDLE
*ChildHandle
266 UDP4_SERVICE_DATA
*Udp4Service
;
267 UDP4_INSTANCE_DATA
*Instance
;
271 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
272 return EFI_INVALID_PARAMETER
;
275 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
278 // Allocate the instance private data structure.
280 Instance
= NetAllocatePool (sizeof (UDP4_INSTANCE_DATA
));
281 if (Instance
== NULL
) {
282 return EFI_OUT_OF_RESOURCES
;
285 Udp4InitInstance (Udp4Service
, Instance
);
288 // Add an IpInfo for this instance.
290 Instance
->IpInfo
= IpIoAddIp (Udp4Service
->IpIo
);
291 if (Instance
->IpInfo
== NULL
) {
292 Status
= EFI_OUT_OF_RESOURCES
;
297 // Install the Udp4Protocol for this instance.
299 Status
= gBS
->InstallMultipleProtocolInterfaces (
301 &gEfiUdp4ProtocolGuid
,
302 &Instance
->Udp4Proto
,
305 if (EFI_ERROR (Status
)) {
309 Instance
->ChildHandle
= *ChildHandle
;
312 // Open the default Ip4 protocol in the IP_IO BY_CHILD.
314 Status
= gBS
->OpenProtocol (
315 Udp4Service
->IpIo
->ChildHandle
,
316 &gEfiIp4ProtocolGuid
,
318 gUdp4DriverBinding
.DriverBindingHandle
,
319 Instance
->ChildHandle
,
320 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
322 if (EFI_ERROR (Status
)) {
323 goto UNINSTALL_PROTOCOL
;
326 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
329 // Link this instance into the service context data and increase the ChildrenNumber.
331 NetListInsertTail (&Udp4Service
->ChildrenList
, &Instance
->Link
);
332 Udp4Service
->ChildrenNumber
++;
334 NET_RESTORE_TPL (OldTpl
);
340 gBS
->UninstallMultipleProtocolInterfaces (
341 Instance
->ChildHandle
,
342 &gEfiUdp4ProtocolGuid
,
343 &Instance
->Udp4Proto
,
349 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
353 Udp4CleanInstance (Instance
);
355 NetFreePool (Instance
);
362 Destroys a child handle with a set of I/O services.
364 @param This Protocol instance pointer.
365 @param ChildHandle Handle of the child to destroy
367 @retval EFI_SUCCES The I/O services were removed from the child
369 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
370 that are being removed
371 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
372 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
373 its I/O services are being used.
374 @retval other The child handle was not destroyed
379 Udp4ServiceBindingDestroyChild (
380 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
381 IN EFI_HANDLE ChildHandle
385 UDP4_SERVICE_DATA
*Udp4Service
;
386 EFI_UDP4_PROTOCOL
*Udp4Proto
;
387 UDP4_INSTANCE_DATA
*Instance
;
390 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
391 return EFI_INVALID_PARAMETER
;
394 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
397 // Try to get the Udp4 protocol from the ChildHandle.
399 Status
= gBS
->OpenProtocol (
401 &gEfiUdp4ProtocolGuid
,
402 (VOID
**) &Udp4Proto
,
403 gUdp4DriverBinding
.DriverBindingHandle
,
405 EFI_OPEN_PROTOCOL_GET_PROTOCOL
407 if (EFI_ERROR (Status
)) {
408 return EFI_UNSUPPORTED
;
411 Instance
= UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto
);
413 if (Instance
->Destroyed
) {
418 // Use the Destroyed flag to avoid the re-entering of the following code.
420 Instance
->Destroyed
= TRUE
;
423 // Close the Ip4 protocol.
426 Udp4Service
->IpIo
->ChildHandle
,
427 &gEfiIp4ProtocolGuid
,
428 gUdp4DriverBinding
.DriverBindingHandle
,
429 Instance
->ChildHandle
433 // Uninstall the Udp4Protocol previously installed on the ChildHandle.
435 Status
= gBS
->UninstallMultipleProtocolInterfaces (
437 &gEfiUdp4ProtocolGuid
,
438 (VOID
*) &Instance
->Udp4Proto
,
441 if (EFI_ERROR (Status
)) {
442 Instance
->Destroyed
= FALSE
;
447 // Reset the configuration in case the instance's consumer forgets to do this.
449 Udp4Proto
->Configure (Udp4Proto
, NULL
);
452 // Remove the IpInfo this instance consumes.
454 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
456 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
459 // Remove this instance from the service context data's ChildrenList.
461 NetListRemoveEntry (&Instance
->Link
);
462 Udp4Service
->ChildrenNumber
--;
465 // Clean the instance.
467 Udp4CleanInstance (Instance
);
469 NET_RESTORE_TPL (OldTpl
);
471 NetFreePool (Instance
);
479 Udp4DriverEntryPoint (
480 IN EFI_HANDLE ImageHandle
,
481 IN EFI_SYSTEM_TABLE
*SystemTable
487 The entry point for Udp4 driver which installs the driver binding
488 and component name protocol on its ImageHandle.
492 ImageHandle - The image handle of the driver.
493 SystemTable - The system table.
497 EFI_SUCCESS - if the driver binding and component name protocols are
498 successfully installed, otherwise if failed.
505 // Install the Udp4DriverBinding and Udp4ComponentName protocols.
507 Status
= NetLibInstallAllDriverProtocols (
516 if (!EFI_ERROR (Status
)) {
518 // Initialize the UDP random port.
520 mUdp4RandomPort
= (UINT16
) (((UINT16
) NetRandomInitSeed ()) % UDP4_PORT_KNOWN
+ UDP4_PORT_KNOWN
);