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.
42 Udp4DestroyChildEntryInHandleBuffer (
47 UDP4_INSTANCE_DATA
*Instance
;
48 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
49 UINTN NumberOfChildren
;
50 EFI_HANDLE
*ChildHandleBuffer
;
52 if (Entry
== NULL
|| Context
== NULL
) {
53 return EFI_INVALID_PARAMETER
;
56 Instance
= NET_LIST_USER_STRUCT_S (Entry
, UDP4_INSTANCE_DATA
, Link
, UDP4_INSTANCE_DATA_SIGNATURE
);
57 ServiceBinding
= ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ServiceBinding
;
58 NumberOfChildren
= ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->NumberOfChildren
;
59 ChildHandleBuffer
= ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ChildHandleBuffer
;
61 if (!NetIsInHandleBuffer (Instance
->ChildHandle
, NumberOfChildren
, ChildHandleBuffer
)) {
65 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->ChildHandle
);
70 Test to see if this driver supports ControllerHandle. This service
71 is called by the EFI boot service ConnectController(). In
72 order to make drivers as small as possible, there are a few calling
73 restrictions for this service. ConnectController() must
74 follow these calling restrictions. If any other agent wishes to call
75 Supported() it must also follow these calling restrictions.
77 @param[in] This Protocol instance pointer.
78 @param[in] ControllerHandle Handle of device to test
79 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
82 @retval EFI_SUCCESS This driver supports this device
83 @retval EFI_ALREADY_STARTED This driver is already running on this device
84 @retval other This driver does not support this device
89 Udp4DriverBindingSupported (
90 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
91 IN EFI_HANDLE ControllerHandle
,
92 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
98 // Test for the Udp4ServiceBinding Protocol
100 Status
= gBS
->OpenProtocol (
102 &gEfiUdp4ServiceBindingProtocolGuid
,
104 This
->DriverBindingHandle
,
106 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
108 if (!EFI_ERROR (Status
)) {
109 return EFI_ALREADY_STARTED
;
113 // Test for the Ip4 Protocol
115 Status
= gBS
->OpenProtocol (
117 &gEfiIp4ServiceBindingProtocolGuid
,
119 This
->DriverBindingHandle
,
121 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
129 Start this driver on ControllerHandle. This service is called by the
130 EFI boot service ConnectController(). In order to make
131 drivers as small as possible, there are a few calling restrictions for
132 this service. ConnectController() must follow these
133 calling restrictions. If any other agent wishes to call Start() it
134 must also follow these calling restrictions.
136 @param[in] This Protocol instance pointer.
137 @param[in] ControllerHandle Handle of device to bind driver to
138 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
141 @retval EFI_SUCCESS This driver is added to ControllerHandle
142 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
143 @retval other This driver does not support this device
148 Udp4DriverBindingStart (
149 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
150 IN EFI_HANDLE ControllerHandle
,
151 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
155 UDP4_SERVICE_DATA
*Udp4Service
;
158 // Allocate Private Context Data Structure.
160 Udp4Service
= AllocatePool (sizeof (UDP4_SERVICE_DATA
));
161 if (Udp4Service
== NULL
) {
162 return EFI_OUT_OF_RESOURCES
;
165 Status
= Udp4CreateService (Udp4Service
, This
->DriverBindingHandle
, ControllerHandle
);
166 if (EFI_ERROR (Status
)) {
167 FreePool (Udp4Service
);
172 // Install the Udp4ServiceBindingProtocol on the ControllerHandle.
174 Status
= gBS
->InstallMultipleProtocolInterfaces (
176 &gEfiUdp4ServiceBindingProtocolGuid
,
177 &Udp4Service
->ServiceBinding
,
180 if (EFI_ERROR (Status
)) {
181 Udp4CleanService (Udp4Service
);
182 FreePool (Udp4Service
);
184 Udp4SetVariableData (Udp4Service
);
192 Stop this driver on ControllerHandle. This service is called by the
193 EFI boot service DisconnectController(). In order to
194 make drivers as small as possible, there are a few calling
195 restrictions for this service. DisconnectController()
196 must follow these calling restrictions. If any other agent wishes
197 to call Stop() it must also follow these calling restrictions.
199 @param[in] This Protocol instance pointer.
200 @param[in] ControllerHandle Handle of device to stop driver on
201 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
202 children is zero stop the entire bus driver.
203 @param[in] ChildHandleBuffer List of Child Handles to Stop.
205 @retval EFI_SUCCESS This driver is removed ControllerHandle
206 @retval other This driver was not removed from this device
211 Udp4DriverBindingStop (
212 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
213 IN EFI_HANDLE ControllerHandle
,
214 IN UINTN NumberOfChildren
,
215 IN EFI_HANDLE
*ChildHandleBuffer
219 EFI_HANDLE NicHandle
;
220 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
221 UDP4_SERVICE_DATA
*Udp4Service
;
222 UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
226 // Find the NicHandle where UDP4 ServiceBinding Protocol is installed.
228 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
229 if (NicHandle
== NULL
) {
234 // Retrieve the UDP4 ServiceBinding Protocol.
236 Status
= gBS
->OpenProtocol (
238 &gEfiUdp4ServiceBindingProtocolGuid
,
239 (VOID
**) &ServiceBinding
,
240 This
->DriverBindingHandle
,
242 EFI_OPEN_PROTOCOL_GET_PROTOCOL
244 if (EFI_ERROR (Status
)) {
245 return EFI_DEVICE_ERROR
;
248 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding
);
249 if (NumberOfChildren
!= 0) {
251 // NumberOfChildren is not zero, destroy the children instances in ChildHandleBuffer.
253 List
= &Udp4Service
->ChildrenList
;
254 Context
.ServiceBinding
= ServiceBinding
;
255 Context
.NumberOfChildren
= NumberOfChildren
;
256 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
257 Status
= NetDestroyLinkList (
259 Udp4DestroyChildEntryInHandleBuffer
,
264 gBS
->UninstallMultipleProtocolInterfaces (
266 &gEfiUdp4ServiceBindingProtocolGuid
,
267 &Udp4Service
->ServiceBinding
,
271 Udp4ClearVariableData (Udp4Service
);
273 Udp4CleanService (Udp4Service
);
275 if (gUdpControllerNameTable
!= NULL
) {
276 FreeUnicodeStringTable (gUdpControllerNameTable
);
277 gUdpControllerNameTable
= NULL
;
279 FreePool (Udp4Service
);
287 Creates a child handle and installs a protocol.
289 The CreateChild() function installs a protocol on ChildHandle.
290 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
291 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
293 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
294 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
295 then a new handle is created. If it is a pointer to an existing UEFI handle,
296 then the protocol is added to the existing UEFI handle.
298 @retval EFI_SUCCES The protocol was added to ChildHandle.
299 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
300 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
302 @retval other The child handle was not created
307 Udp4ServiceBindingCreateChild (
308 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
309 IN EFI_HANDLE
*ChildHandle
313 UDP4_SERVICE_DATA
*Udp4Service
;
314 UDP4_INSTANCE_DATA
*Instance
;
318 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
319 return EFI_INVALID_PARAMETER
;
322 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
325 // Allocate the instance private data structure.
327 Instance
= AllocateZeroPool (sizeof (UDP4_INSTANCE_DATA
));
328 if (Instance
== NULL
) {
329 return EFI_OUT_OF_RESOURCES
;
332 Udp4InitInstance (Udp4Service
, Instance
);
335 // Add an IpInfo for this instance.
337 Instance
->IpInfo
= IpIoAddIp (Udp4Service
->IpIo
);
338 if (Instance
->IpInfo
== NULL
) {
339 Status
= EFI_OUT_OF_RESOURCES
;
344 // Install the Udp4Protocol for this instance.
346 Status
= gBS
->InstallMultipleProtocolInterfaces (
348 &gEfiUdp4ProtocolGuid
,
349 &Instance
->Udp4Proto
,
352 if (EFI_ERROR (Status
)) {
356 Instance
->ChildHandle
= *ChildHandle
;
359 // Open the default Ip4 protocol in the IP_IO BY_CHILD.
361 Status
= gBS
->OpenProtocol (
362 Udp4Service
->IpIo
->ChildHandle
,
363 &gEfiIp4ProtocolGuid
,
365 gUdp4DriverBinding
.DriverBindingHandle
,
366 Instance
->ChildHandle
,
367 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
369 if (EFI_ERROR (Status
)) {
374 // Open this instance's Ip4 protocol in the IpInfo BY_CHILD.
376 Status
= gBS
->OpenProtocol (
377 Instance
->IpInfo
->ChildHandle
,
378 &gEfiIp4ProtocolGuid
,
380 gUdp4DriverBinding
.DriverBindingHandle
,
381 Instance
->ChildHandle
,
382 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
384 if (EFI_ERROR (Status
)) {
388 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
391 // Link this instance into the service context data and increase the ChildrenNumber.
393 InsertTailList (&Udp4Service
->ChildrenList
, &Instance
->Link
);
394 Udp4Service
->ChildrenNumber
++;
396 gBS
->RestoreTPL (OldTpl
);
402 if (Instance
->ChildHandle
!= NULL
) {
403 gBS
->UninstallMultipleProtocolInterfaces (
404 Instance
->ChildHandle
,
405 &gEfiUdp4ProtocolGuid
,
406 &Instance
->Udp4Proto
,
411 if (Instance
->IpInfo
!= NULL
) {
412 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
415 Udp4CleanInstance (Instance
);
424 Destroys a child handle with a protocol installed on it.
426 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
427 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
428 last protocol on ChildHandle, then ChildHandle is destroyed.
430 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
431 @param[in] ChildHandle Handle of the child to destroy
433 @retval EFI_SUCCES The protocol was removed from ChildHandle.
434 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
435 @retval EFI_INVALID_PARAMETER Child handle is NULL.
436 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
437 because its services are being used.
438 @retval other The child handle was not destroyed
443 Udp4ServiceBindingDestroyChild (
444 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
445 IN EFI_HANDLE ChildHandle
449 UDP4_SERVICE_DATA
*Udp4Service
;
450 EFI_UDP4_PROTOCOL
*Udp4Proto
;
451 UDP4_INSTANCE_DATA
*Instance
;
454 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
455 return EFI_INVALID_PARAMETER
;
458 Udp4Service
= UDP4_SERVICE_DATA_FROM_THIS (This
);
461 // Try to get the Udp4 protocol from the ChildHandle.
463 Status
= gBS
->OpenProtocol (
465 &gEfiUdp4ProtocolGuid
,
466 (VOID
**) &Udp4Proto
,
467 gUdp4DriverBinding
.DriverBindingHandle
,
469 EFI_OPEN_PROTOCOL_GET_PROTOCOL
471 if (EFI_ERROR (Status
)) {
472 return EFI_UNSUPPORTED
;
475 Instance
= UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto
);
477 if (Instance
->InDestroy
) {
482 // Use the Destroyed flag to avoid the re-entering of the following code.
484 Instance
->InDestroy
= TRUE
;
487 // Close the Ip4 protocol.
490 Udp4Service
->IpIo
->ChildHandle
,
491 &gEfiIp4ProtocolGuid
,
492 gUdp4DriverBinding
.DriverBindingHandle
,
493 Instance
->ChildHandle
496 // Close the Ip4 protocol on this instance's IpInfo.
499 Instance
->IpInfo
->ChildHandle
,
500 &gEfiIp4ProtocolGuid
,
501 gUdp4DriverBinding
.DriverBindingHandle
,
502 Instance
->ChildHandle
506 // Uninstall the Udp4Protocol previously installed on the ChildHandle.
508 Status
= gBS
->UninstallMultipleProtocolInterfaces (
510 &gEfiUdp4ProtocolGuid
,
511 (VOID
*) &Instance
->Udp4Proto
,
514 if (EFI_ERROR (Status
)) {
515 Instance
->InDestroy
= FALSE
;
520 // Reset the configuration in case the instance's consumer forgets to do this.
522 Udp4Proto
->Configure (Udp4Proto
, NULL
);
525 // Remove the IpInfo this instance consumes.
527 IpIoRemoveIp (Udp4Service
->IpIo
, Instance
->IpInfo
);
529 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
532 // Remove this instance from the service context data's ChildrenList.
534 RemoveEntryList (&Instance
->Link
);
535 Udp4Service
->ChildrenNumber
--;
538 // Clean the instance.
540 Udp4CleanInstance (Instance
);
542 gBS
->RestoreTPL (OldTpl
);
550 This is the declaration of an EFI image entry point. This entry point is
551 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
552 both device drivers and bus drivers.
554 The entry point for Udp4 driver which installs the driver binding
555 and component name protocol on its ImageHandle.
557 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
558 @param[in] SystemTable A pointer to the EFI System Table.
560 @retval EFI_SUCCESS The operation completed successfully.
561 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
566 Udp4DriverEntryPoint (
567 IN EFI_HANDLE ImageHandle
,
568 IN EFI_SYSTEM_TABLE
*SystemTable
574 // Install the Udp4DriverBinding and Udp4ComponentName protocols.
576 Status
= EfiLibInstallDriverBindingComponentName2 (
584 if (!EFI_ERROR (Status
)) {
586 // Initialize the UDP random port.
588 mUdp4RandomPort
= (UINT16
) (((UINT16
) NetRandomInitSeed ()) % UDP4_PORT_KNOWN
+ UDP4_PORT_KNOWN
);