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.
17 EFI_DRIVER_BINDING_PROTOCOL gUdp4DriverBinding
= {
18 Udp4DriverBindingSupported
,
19 Udp4DriverBindingStart
,
20 Udp4DriverBindingStop
,
26 EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
= {
27 Udp4ServiceBindingCreateChild
,
28 Udp4ServiceBindingDestroyChild
32 Callback function which provided by user to remove one node in NetDestroyLinkList process.
34 @param[in] Entry The entry to be removed.
35 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
37 @retval EFI_SUCCESS The entry has been removed successfully.
38 @retval Others Fail to remove the entry.
43 Udp4DestroyChildEntryInHandleBuffer (
48 UDP4_INSTANCE_DATA
*Instance
;
49 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
50 UINTN NumberOfChildren
;
51 EFI_HANDLE
*ChildHandleBuffer
;
53 if (Entry
== NULL
|| Context
== NULL
) {
54 return EFI_INVALID_PARAMETER
;
57 Instance
= NET_LIST_USER_STRUCT_S (Entry
, UDP4_INSTANCE_DATA
, Link
, UDP4_INSTANCE_DATA_SIGNATURE
);
58 ServiceBinding
= ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ServiceBinding
;
59 NumberOfChildren
= ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->NumberOfChildren
;
60 ChildHandleBuffer
= ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ChildHandleBuffer
;
62 if (!NetIsInHandleBuffer (Instance
->ChildHandle
, NumberOfChildren
, ChildHandleBuffer
)) {
66 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->ChildHandle
);
71 Test to see if this driver supports ControllerHandle. This service
72 is called by the EFI boot service ConnectController(). In
73 order to make drivers as small as possible, there are a few calling
74 restrictions for this service. ConnectController() must
75 follow these calling restrictions. If any other agent wishes to call
76 Supported() it must also follow these calling restrictions.
78 @param[in] This Protocol instance pointer.
79 @param[in] ControllerHandle Handle of device to test
80 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
83 @retval EFI_SUCCESS This driver supports this device
84 @retval EFI_ALREADY_STARTED This driver is already running on this device
85 @retval other This driver does not support this device
90 Udp4DriverBindingSupported (
91 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
92 IN EFI_HANDLE ControllerHandle
,
93 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
99 // Test for the Udp4ServiceBinding Protocol
101 Status
= gBS
->OpenProtocol (
103 &gEfiUdp4ServiceBindingProtocolGuid
,
105 This
->DriverBindingHandle
,
107 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
109 if (!EFI_ERROR (Status
)) {
110 return EFI_ALREADY_STARTED
;
114 // Test for the Ip4 Protocol
116 Status
= gBS
->OpenProtocol (
118 &gEfiIp4ServiceBindingProtocolGuid
,
120 This
->DriverBindingHandle
,
122 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
130 Start this driver on ControllerHandle. This service is called by the
131 EFI boot service ConnectController(). In order to make
132 drivers as small as possible, there are a few calling restrictions for
133 this service. ConnectController() must follow these
134 calling restrictions. If any other agent wishes to call Start() it
135 must also follow these calling restrictions.
137 @param[in] This Protocol instance pointer.
138 @param[in] ControllerHandle Handle of device to bind driver to
139 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
142 @retval EFI_SUCCESS This driver is added to ControllerHandle
143 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
144 @retval other This driver does not support this device
149 Udp4DriverBindingStart (
150 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
151 IN EFI_HANDLE ControllerHandle
,
152 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
156 UDP4_SERVICE_DATA
*Udp4Service
;
159 // Allocate Private Context Data Structure.
161 Udp4Service
= AllocatePool (sizeof (UDP4_SERVICE_DATA
));
162 if (Udp4Service
== NULL
) {
163 return EFI_OUT_OF_RESOURCES
;
166 Status
= Udp4CreateService (Udp4Service
, This
->DriverBindingHandle
, ControllerHandle
);
167 if (EFI_ERROR (Status
)) {
168 FreePool (Udp4Service
);
173 // Install the Udp4ServiceBindingProtocol on the ControllerHandle.
175 Status
= gBS
->InstallMultipleProtocolInterfaces (
177 &gEfiUdp4ServiceBindingProtocolGuid
,
178 &Udp4Service
->ServiceBinding
,
181 if (EFI_ERROR (Status
)) {
182 Udp4CleanService (Udp4Service
);
183 FreePool (Udp4Service
);
185 Udp4SetVariableData (Udp4Service
);
193 Stop this driver on ControllerHandle. This service is called by the
194 EFI boot service DisconnectController(). In order to
195 make drivers as small as possible, there are a few calling
196 restrictions for this service. DisconnectController()
197 must follow these calling restrictions. If any other agent wishes
198 to call Stop() it must also follow these calling restrictions.
200 @param[in] This Protocol instance pointer.
201 @param[in] ControllerHandle Handle of device to stop driver on
202 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
203 children is zero stop the entire bus driver.
204 @param[in] ChildHandleBuffer List of Child Handles to Stop.
206 @retval EFI_SUCCESS This driver is removed ControllerHandle
207 @retval other This driver was not removed from this device
212 Udp4DriverBindingStop (
213 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
214 IN EFI_HANDLE ControllerHandle
,
215 IN UINTN NumberOfChildren
,
216 IN EFI_HANDLE
*ChildHandleBuffer
220 EFI_HANDLE NicHandle
;
221 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
222 UDP4_SERVICE_DATA
*Udp4Service
;
223 UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
227 // Find the NicHandle where UDP4 ServiceBinding Protocol is installed.
229 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
230 if (NicHandle
== NULL
) {
235 // Retrieve the UDP4 ServiceBinding Protocol.
237 Status
= gBS
->OpenProtocol (
239 &gEfiUdp4ServiceBindingProtocolGuid
,
240 (VOID
**) &ServiceBinding
,
241 This
->DriverBindingHandle
,
243 EFI_OPEN_PROTOCOL_GET_PROTOCOL
245 if (EFI_ERROR (Status
)) {
246 return EFI_DEVICE_ERROR
;
249 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding
);
250 if (NumberOfChildren
!= 0) {
252 // NumberOfChildren is not zero, destroy the children instances in ChildHandleBuffer.
254 List
= &Udp4Service
->ChildrenList
;
255 Context
.ServiceBinding
= ServiceBinding
;
256 Context
.NumberOfChildren
= NumberOfChildren
;
257 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
258 Status
= NetDestroyLinkList (
260 Udp4DestroyChildEntryInHandleBuffer
,
265 gBS
->UninstallMultipleProtocolInterfaces (
267 &gEfiUdp4ServiceBindingProtocolGuid
,
268 &Udp4Service
->ServiceBinding
,
272 Udp4ClearVariableData (Udp4Service
);
274 Udp4CleanService (Udp4Service
);
276 if (gUdpControllerNameTable
!= NULL
) {
277 FreeUnicodeStringTable (gUdpControllerNameTable
);
278 gUdpControllerNameTable
= NULL
;
280 FreePool (Udp4Service
);
288 Creates a child handle and installs a protocol.
290 The CreateChild() function installs a protocol on ChildHandle.
291 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
292 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
294 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
295 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
296 then a new handle is created. If it is a pointer to an existing UEFI handle,
297 then the protocol is added to the existing UEFI handle.
299 @retval EFI_SUCCES The protocol was added to ChildHandle.
300 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
301 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
303 @retval other The child handle was not created
308 Udp4ServiceBindingCreateChild (
309 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
310 IN EFI_HANDLE
*ChildHandle
314 UDP4_SERVICE_DATA
*Udp4Service
;
315 UDP4_INSTANCE_DATA
*Instance
;
319 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
320 return EFI_INVALID_PARAMETER
;
323 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
326 // Allocate the instance private data structure.
328 Instance
= AllocateZeroPool (sizeof (UDP4_INSTANCE_DATA
));
329 if (Instance
== NULL
) {
330 return EFI_OUT_OF_RESOURCES
;
333 Udp4InitInstance (Udp4Service
, Instance
);
336 // Add an IpInfo for this instance.
338 Instance
->IpInfo
= IpIoAddIp (Udp4Service
->IpIo
);
339 if (Instance
->IpInfo
== NULL
) {
340 Status
= EFI_OUT_OF_RESOURCES
;
345 // Install the Udp4Protocol for this instance.
347 Status
= gBS
->InstallMultipleProtocolInterfaces (
349 &gEfiUdp4ProtocolGuid
,
350 &Instance
->Udp4Proto
,
353 if (EFI_ERROR (Status
)) {
357 Instance
->ChildHandle
= *ChildHandle
;
360 // Open the default Ip4 protocol in the IP_IO BY_CHILD.
362 Status
= gBS
->OpenProtocol (
363 Udp4Service
->IpIo
->ChildHandle
,
364 &gEfiIp4ProtocolGuid
,
366 gUdp4DriverBinding
.DriverBindingHandle
,
367 Instance
->ChildHandle
,
368 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
370 if (EFI_ERROR (Status
)) {
375 // Open this instance's Ip4 protocol in the IpInfo BY_CHILD.
377 Status
= gBS
->OpenProtocol (
378 Instance
->IpInfo
->ChildHandle
,
379 &gEfiIp4ProtocolGuid
,
381 gUdp4DriverBinding
.DriverBindingHandle
,
382 Instance
->ChildHandle
,
383 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
385 if (EFI_ERROR (Status
)) {
389 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
392 // Link this instance into the service context data and increase the ChildrenNumber.
394 InsertTailList (&Udp4Service
->ChildrenList
, &Instance
->Link
);
395 Udp4Service
->ChildrenNumber
++;
397 gBS
->RestoreTPL (OldTpl
);
403 if (Instance
->ChildHandle
!= NULL
) {
404 gBS
->UninstallMultipleProtocolInterfaces (
405 Instance
->ChildHandle
,
406 &gEfiUdp4ProtocolGuid
,
407 &Instance
->Udp4Proto
,
412 if (Instance
->IpInfo
!= NULL
) {
413 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
416 Udp4CleanInstance (Instance
);
425 Destroys a child handle with a protocol installed on it.
427 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
428 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
429 last protocol on ChildHandle, then ChildHandle is destroyed.
431 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
432 @param[in] ChildHandle Handle of the child to destroy
434 @retval EFI_SUCCES The protocol was removed from ChildHandle.
435 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
436 @retval EFI_INVALID_PARAMETER Child handle is NULL.
437 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
438 because its services are being used.
439 @retval other The child handle was not destroyed
444 Udp4ServiceBindingDestroyChild (
445 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
446 IN EFI_HANDLE ChildHandle
450 UDP4_SERVICE_DATA
*Udp4Service
;
451 EFI_UDP4_PROTOCOL
*Udp4Proto
;
452 UDP4_INSTANCE_DATA
*Instance
;
455 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
456 return EFI_INVALID_PARAMETER
;
459 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
462 // Try to get the Udp4 protocol from the ChildHandle.
464 Status
= gBS
->OpenProtocol (
466 &gEfiUdp4ProtocolGuid
,
467 (VOID
**) &Udp4Proto
,
468 gUdp4DriverBinding
.DriverBindingHandle
,
470 EFI_OPEN_PROTOCOL_GET_PROTOCOL
472 if (EFI_ERROR (Status
)) {
473 return EFI_UNSUPPORTED
;
476 Instance
= UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto
);
478 if (Instance
->InDestroy
) {
483 // Use the Destroyed flag to avoid the re-entering of the following code.
485 Instance
->InDestroy
= TRUE
;
488 // Close the Ip4 protocol.
491 Udp4Service
->IpIo
->ChildHandle
,
492 &gEfiIp4ProtocolGuid
,
493 gUdp4DriverBinding
.DriverBindingHandle
,
494 Instance
->ChildHandle
497 // Close the Ip4 protocol on this instance's IpInfo.
500 Instance
->IpInfo
->ChildHandle
,
501 &gEfiIp4ProtocolGuid
,
502 gUdp4DriverBinding
.DriverBindingHandle
,
503 Instance
->ChildHandle
507 // Uninstall the Udp4Protocol previously installed on the ChildHandle.
509 Status
= gBS
->UninstallMultipleProtocolInterfaces (
511 &gEfiUdp4ProtocolGuid
,
512 (VOID
*) &Instance
->Udp4Proto
,
515 if (EFI_ERROR (Status
)) {
516 Instance
->InDestroy
= FALSE
;
521 // Reset the configuration in case the instance's consumer forgets to do this.
523 Udp4Proto
->Configure (Udp4Proto
, NULL
);
526 // Remove the IpInfo this instance consumes.
528 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
530 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
533 // Remove this instance from the service context data's ChildrenList.
535 RemoveEntryList (&Instance
->Link
);
536 Udp4Service
->ChildrenNumber
--;
539 // Clean the instance.
541 Udp4CleanInstance (Instance
);
543 gBS
->RestoreTPL (OldTpl
);
551 This is the declaration of an EFI image entry point. This entry point is
552 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
553 both device drivers and bus drivers.
555 The entry point for Udp4 driver which installs the driver binding
556 and component name protocol on its ImageHandle.
558 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
559 @param[in] SystemTable A pointer to the EFI System Table.
561 @retval EFI_SUCCESS The operation completed successfully.
562 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
567 Udp4DriverEntryPoint (
568 IN EFI_HANDLE ImageHandle
,
569 IN EFI_SYSTEM_TABLE
*SystemTable
575 // Install the Udp4DriverBinding and Udp4ComponentName protocols.
577 Status
= EfiLibInstallDriverBindingComponentName2 (
585 if (!EFI_ERROR (Status
)) {
587 // Initialize the UDP random port.
589 mUdp4RandomPort
= (UINT16
) (((UINT16
) NetRandomInitSeed ()) % UDP4_PORT_KNOWN
+ UDP4_PORT_KNOWN
);