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.
17 EFI_DRIVER_BINDING_PROTOCOL gUdp4DriverBinding
= {
18 Udp4DriverBindingSupported
,
19 Udp4DriverBindingStart
,
20 Udp4DriverBindingStop
,
26 EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
= {
27 Udp4ServiceBindingCreateChild
,
28 Udp4ServiceBindingDestroyChild
33 Test to see if this driver supports ControllerHandle. This service
34 is called by the EFI boot service ConnectController(). In
35 order to make drivers as small as possible, there are a few calling
36 restrictions for this service. ConnectController() must
37 follow these calling restrictions. If any other agent wishes to call
38 Supported() it must also follow these calling restrictions.
40 @param[in] This Protocol instance pointer.
41 @param[in] ControllerHandle Handle of device to test
42 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
45 @retval EFI_SUCCESS This driver supports this device
46 @retval EFI_ALREADY_STARTED This driver is already running on this device
47 @retval other This driver does not support this device
52 Udp4DriverBindingSupported (
53 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
54 IN EFI_HANDLE ControllerHandle
,
55 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
61 // Test for the Udp4ServiceBinding Protocol
63 Status
= gBS
->OpenProtocol (
65 &gEfiUdp4ServiceBindingProtocolGuid
,
67 This
->DriverBindingHandle
,
69 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
71 if (!EFI_ERROR (Status
)) {
72 return EFI_ALREADY_STARTED
;
76 // Test for the Ip4 Protocol
78 Status
= gBS
->OpenProtocol (
80 &gEfiIp4ServiceBindingProtocolGuid
,
82 This
->DriverBindingHandle
,
84 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
92 Start this driver on ControllerHandle. This service is called by the
93 EFI boot service ConnectController(). In order to make
94 drivers as small as possible, there are a few calling restrictions for
95 this service. ConnectController() must follow these
96 calling restrictions. If any other agent wishes to call Start() it
97 must also follow these calling restrictions.
99 @param[in] This Protocol instance pointer.
100 @param[in] ControllerHandle Handle of device to bind driver to
101 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
104 @retval EFI_SUCCESS This driver is added to ControllerHandle
105 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
106 @retval other This driver does not support this device
111 Udp4DriverBindingStart (
112 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
113 IN EFI_HANDLE ControllerHandle
,
114 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
118 UDP4_SERVICE_DATA
*Udp4Service
;
121 // Allocate Private Context Data Structure.
123 Udp4Service
= AllocatePool (sizeof (UDP4_SERVICE_DATA
));
124 if (Udp4Service
== NULL
) {
125 return EFI_OUT_OF_RESOURCES
;
128 Status
= Udp4CreateService (Udp4Service
, This
->DriverBindingHandle
, ControllerHandle
);
129 if (EFI_ERROR (Status
)) {
130 FreePool (Udp4Service
);
135 // Install the Udp4ServiceBindingProtocol on the ControllerHandle.
137 Status
= gBS
->InstallMultipleProtocolInterfaces (
139 &gEfiUdp4ServiceBindingProtocolGuid
,
140 &Udp4Service
->ServiceBinding
,
143 if (EFI_ERROR (Status
)) {
144 Udp4CleanService (Udp4Service
);
145 FreePool (Udp4Service
);
147 Udp4SetVariableData (Udp4Service
);
155 Stop this driver on ControllerHandle. This service is called by the
156 EFI boot service DisconnectController(). In order to
157 make drivers as small as possible, there are a few calling
158 restrictions for this service. DisconnectController()
159 must follow these calling restrictions. If any other agent wishes
160 to call Stop() it must also follow these calling restrictions.
162 @param[in] This Protocol instance pointer.
163 @param[in] ControllerHandle Handle of device to stop driver on
164 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
165 children is zero stop the entire bus driver.
166 @param[in] ChildHandleBuffer List of Child Handles to Stop.
168 @retval EFI_SUCCESS This driver is removed ControllerHandle
169 @retval other This driver was not removed from this device
174 Udp4DriverBindingStop (
175 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
176 IN EFI_HANDLE ControllerHandle
,
177 IN UINTN NumberOfChildren
,
178 IN EFI_HANDLE
*ChildHandleBuffer
182 EFI_HANDLE NicHandle
;
183 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
184 UDP4_SERVICE_DATA
*Udp4Service
;
185 UDP4_INSTANCE_DATA
*Instance
;
188 // Find the NicHandle where UDP4 ServiceBinding Protocol is installed.
190 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
191 if (NicHandle
== NULL
) {
192 return EFI_DEVICE_ERROR
;
196 // Retrieve the UDP4 ServiceBinding Protocol.
198 Status
= gBS
->OpenProtocol (
200 &gEfiUdp4ServiceBindingProtocolGuid
,
201 (VOID
**) &ServiceBinding
,
202 This
->DriverBindingHandle
,
204 EFI_OPEN_PROTOCOL_GET_PROTOCOL
206 if (EFI_ERROR (Status
)) {
207 return EFI_DEVICE_ERROR
;
210 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding
);
212 if (NumberOfChildren
== 0) {
214 gBS
->UninstallMultipleProtocolInterfaces (
216 &gEfiUdp4ServiceBindingProtocolGuid
,
217 &Udp4Service
->ServiceBinding
,
221 Udp4ClearVariableData (Udp4Service
);
223 Udp4CleanService (Udp4Service
);
225 FreePool (Udp4Service
);
228 while (!IsListEmpty (&Udp4Service
->ChildrenList
)) {
229 Instance
= NET_LIST_HEAD (&Udp4Service
->ChildrenList
, UDP4_INSTANCE_DATA
, Link
);
231 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->ChildHandle
);
240 Creates a child handle and installs a protocol.
242 The CreateChild() function installs a protocol on ChildHandle.
243 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
244 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
246 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
247 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
248 then a new handle is created. If it is a pointer to an existing UEFI handle,
249 then the protocol is added to the existing UEFI handle.
251 @retval EFI_SUCCES The protocol was added to ChildHandle.
252 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
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
= AllocateZeroPool (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
)) {
326 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
329 // Link this instance into the service context data and increase the ChildrenNumber.
331 InsertTailList (&Udp4Service
->ChildrenList
, &Instance
->Link
);
332 Udp4Service
->ChildrenNumber
++;
334 gBS
->RestoreTPL (OldTpl
);
340 if (Instance
->ChildHandle
!= NULL
) {
341 gBS
->UninstallMultipleProtocolInterfaces (
342 Instance
->ChildHandle
,
343 &gEfiUdp4ProtocolGuid
,
344 &Instance
->Udp4Proto
,
349 if (Instance
->IpInfo
!= NULL
) {
350 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
353 Udp4CleanInstance (Instance
);
362 Destroys a child handle with a protocol installed on it.
364 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
365 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
366 last protocol on ChildHandle, then ChildHandle is destroyed.
368 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
369 @param[in] ChildHandle Handle of the child to destroy
371 @retval EFI_SUCCES The protocol was removed from ChildHandle.
372 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
373 @retval EFI_INVALID_PARAMETER Child handle is NULL.
374 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
375 because its services are being used.
376 @retval other The child handle was not destroyed
381 Udp4ServiceBindingDestroyChild (
382 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
383 IN EFI_HANDLE ChildHandle
387 UDP4_SERVICE_DATA
*Udp4Service
;
388 EFI_UDP4_PROTOCOL
*Udp4Proto
;
389 UDP4_INSTANCE_DATA
*Instance
;
392 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
393 return EFI_INVALID_PARAMETER
;
396 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
399 // Try to get the Udp4 protocol from the ChildHandle.
401 Status
= gBS
->OpenProtocol (
403 &gEfiUdp4ProtocolGuid
,
404 (VOID
**) &Udp4Proto
,
405 gUdp4DriverBinding
.DriverBindingHandle
,
407 EFI_OPEN_PROTOCOL_GET_PROTOCOL
409 if (EFI_ERROR (Status
)) {
410 return EFI_UNSUPPORTED
;
413 Instance
= UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto
);
415 if (Instance
->Destroyed
) {
420 // Use the Destroyed flag to avoid the re-entering of the following code.
422 Instance
->Destroyed
= TRUE
;
425 // Close the Ip4 protocol.
428 Udp4Service
->IpIo
->ChildHandle
,
429 &gEfiIp4ProtocolGuid
,
430 gUdp4DriverBinding
.DriverBindingHandle
,
431 Instance
->ChildHandle
435 // Uninstall the Udp4Protocol previously installed on the ChildHandle.
437 Status
= gBS
->UninstallMultipleProtocolInterfaces (
439 &gEfiUdp4ProtocolGuid
,
440 (VOID
*) &Instance
->Udp4Proto
,
443 if (EFI_ERROR (Status
)) {
444 Instance
->Destroyed
= FALSE
;
449 // Reset the configuration in case the instance's consumer forgets to do this.
451 Udp4Proto
->Configure (Udp4Proto
, NULL
);
454 // Remove the IpInfo this instance consumes.
456 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
458 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
461 // Remove this instance from the service context data's ChildrenList.
463 RemoveEntryList (&Instance
->Link
);
464 Udp4Service
->ChildrenNumber
--;
467 // Clean the instance.
469 Udp4CleanInstance (Instance
);
471 gBS
->RestoreTPL (OldTpl
);
479 This is the declaration of an EFI image entry point. This entry point is
480 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
481 both device drivers and bus drivers.
483 The entry point for Udp4 driver which installs the driver binding
484 and component name protocol on its ImageHandle.
486 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
487 @param[in] SystemTable A pointer to the EFI System Table.
489 @retval EFI_SUCCESS The operation completed successfully.
490 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
495 Udp4DriverEntryPoint (
496 IN EFI_HANDLE ImageHandle
,
497 IN EFI_SYSTEM_TABLE
*SystemTable
503 // Install the Udp4DriverBinding and Udp4ComponentName protocols.
505 Status
= EfiLibInstallDriverBindingComponentName2 (
513 if (!EFI_ERROR (Status
)) {
515 // Initialize the UDP random port.
517 mUdp4RandomPort
= (UINT16
) (((UINT16
) NetRandomInitSeed ()) % UDP4_PORT_KNOWN
+ UDP4_PORT_KNOWN
);