]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Driver.c
Code clean up in NetLib:
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Udp4Dxe / Udp4Driver.c
CommitLineData
8a67d61d 1/** @file\r
2\r
766c7483 3Copyright (c) 2006 - 2009, Intel Corporation.<BR>\r
8a67d61d 4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
8a67d61d 12**/\r
13\r
14\r
15#include "Udp4Impl.h"\r
16\r
17EFI_DRIVER_BINDING_PROTOCOL gUdp4DriverBinding = {\r
18 Udp4DriverBindingSupported,\r
19 Udp4DriverBindingStart,\r
20 Udp4DriverBindingStop,\r
21 0xa,\r
22 NULL,\r
23 NULL\r
24};\r
25\r
26EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding = {\r
27 Udp4ServiceBindingCreateChild,\r
28 Udp4ServiceBindingDestroyChild\r
29};\r
30\r
31\r
32/**\r
bab52709 33 Test to see if this driver supports ControllerHandle. This service\r
34 is called by the EFI boot service ConnectController(). In\r
35 order to make drivers as small as possible, there are a few calling\r
36 restrictions for this service. ConnectController() must\r
37 follow these calling restrictions. If any other agent wishes to call\r
38 Supported() it must also follow these calling restrictions.\r
39\r
3e8c18da 40 @param[in] This Protocol instance pointer.\r
41 @param[in] ControllerHandle Handle of device to test\r
42 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
43 device to start.\r
bab52709 44\r
45 @retval EFI_SUCCESS This driver supports this device\r
46 @retval EFI_ALREADY_STARTED This driver is already running on this device\r
47 @retval other This driver does not support this device\r
8a67d61d 48\r
49**/\r
50EFI_STATUS\r
51EFIAPI\r
52Udp4DriverBindingSupported (\r
53 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
54 IN EFI_HANDLE ControllerHandle,\r
55 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
56 )\r
57{\r
58 EFI_STATUS Status;\r
59\r
60 //\r
61 // Test for the Udp4ServiceBinding Protocol\r
62 //\r
63 Status = gBS->OpenProtocol (\r
64 ControllerHandle,\r
65 &gEfiUdp4ServiceBindingProtocolGuid,\r
66 NULL,\r
67 This->DriverBindingHandle,\r
68 ControllerHandle,\r
69 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
70 );\r
71 if (!EFI_ERROR (Status)) {\r
72 return EFI_ALREADY_STARTED;\r
73 }\r
74\r
75 //\r
76 // Test for the Ip4 Protocol\r
77 //\r
78 Status = gBS->OpenProtocol (\r
79 ControllerHandle,\r
80 &gEfiIp4ServiceBindingProtocolGuid,\r
81 NULL,\r
82 This->DriverBindingHandle,\r
83 ControllerHandle,\r
84 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
85 );\r
86\r
87 return Status;\r
88}\r
89\r
90\r
91/**\r
bab52709 92 Start this driver on ControllerHandle. This service is called by the\r
93 EFI boot service ConnectController(). In order to make\r
94 drivers as small as possible, there are a few calling restrictions for\r
95 this service. ConnectController() must follow these\r
96 calling restrictions. If any other agent wishes to call Start() it\r
97 must also follow these calling restrictions.\r
98\r
3e8c18da 99 @param[in] This Protocol instance pointer.\r
100 @param[in] ControllerHandle Handle of device to bind driver to\r
101 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
102 device to start.\r
bab52709 103\r
104 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
105 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
106 @retval other This driver does not support this device\r
8a67d61d 107\r
108**/\r
109EFI_STATUS\r
110EFIAPI\r
111Udp4DriverBindingStart (\r
112 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
113 IN EFI_HANDLE ControllerHandle,\r
114 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
115 )\r
116{\r
117 EFI_STATUS Status;\r
118 UDP4_SERVICE_DATA *Udp4Service;\r
119\r
120 //\r
121 // Allocate Private Context Data Structure.\r
122 //\r
e48e37fc 123 Udp4Service = AllocatePool (sizeof (UDP4_SERVICE_DATA));\r
8a67d61d 124 if (Udp4Service == NULL) {\r
125 return EFI_OUT_OF_RESOURCES;\r
126 }\r
127\r
128 Status = Udp4CreateService (Udp4Service, This->DriverBindingHandle, ControllerHandle);\r
129 if (EFI_ERROR (Status)) {\r
766c7483 130 FreePool (Udp4Service);\r
c4a62a12 131 return Status;\r
8a67d61d 132 }\r
133\r
134 //\r
135 // Install the Udp4ServiceBindingProtocol on the ControllerHandle.\r
136 //\r
137 Status = gBS->InstallMultipleProtocolInterfaces (\r
138 &ControllerHandle,\r
139 &gEfiUdp4ServiceBindingProtocolGuid,\r
140 &Udp4Service->ServiceBinding,\r
141 NULL\r
142 );\r
143 if (EFI_ERROR (Status)) {\r
c4a62a12 144 Udp4CleanService (Udp4Service);\r
766c7483 145 FreePool (Udp4Service);\r
c4a62a12 146 } else {\r
147 Udp4SetVariableData (Udp4Service);\r
8a67d61d 148 }\r
149\r
8a67d61d 150 return Status;\r
151}\r
152\r
153\r
154/**\r
bab52709 155 Stop this driver on ControllerHandle. This service is called by the\r
156 EFI boot service DisconnectController(). In order to\r
157 make drivers as small as possible, there are a few calling\r
158 restrictions for this service. DisconnectController()\r
159 must follow these calling restrictions. If any other agent wishes\r
160 to call Stop() it must also follow these calling restrictions.\r
161 \r
3e8c18da 162 @param[in] This Protocol instance pointer.\r
163 @param[in] ControllerHandle Handle of device to stop driver on\r
164 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
165 children is zero stop the entire bus driver.\r
166 @param[in] ChildHandleBuffer List of Child Handles to Stop.\r
bab52709 167\r
168 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
169 @retval other This driver was not removed from this device\r
8a67d61d 170\r
171**/\r
172EFI_STATUS\r
173EFIAPI\r
174Udp4DriverBindingStop (\r
175 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
176 IN EFI_HANDLE ControllerHandle,\r
177 IN UINTN NumberOfChildren,\r
178 IN EFI_HANDLE *ChildHandleBuffer\r
179 )\r
180{\r
181 EFI_STATUS Status;\r
182 EFI_HANDLE NicHandle;\r
183 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
184 UDP4_SERVICE_DATA *Udp4Service;\r
185 UDP4_INSTANCE_DATA *Instance;\r
186\r
187 //\r
188 // Find the NicHandle where UDP4 ServiceBinding Protocol is installed.\r
189 //\r
190 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp4ProtocolGuid);\r
191 if (NicHandle == NULL) {\r
c4a62a12 192 return EFI_DEVICE_ERROR;\r
8a67d61d 193 }\r
194\r
195 //\r
196 // Retrieve the UDP4 ServiceBinding Protocol.\r
197 //\r
198 Status = gBS->OpenProtocol (\r
199 NicHandle,\r
200 &gEfiUdp4ServiceBindingProtocolGuid,\r
201 (VOID **) &ServiceBinding,\r
202 This->DriverBindingHandle,\r
203 NicHandle,\r
204 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
205 );\r
206 if (EFI_ERROR (Status)) {\r
207 return EFI_DEVICE_ERROR;\r
208 }\r
209\r
210 Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding);\r
211\r
c4a62a12 212 if (NumberOfChildren == 0) {\r
8a67d61d 213\r
c4a62a12 214 gBS->UninstallMultipleProtocolInterfaces (\r
215 NicHandle,\r
216 &gEfiUdp4ServiceBindingProtocolGuid,\r
217 &Udp4Service->ServiceBinding,\r
218 NULL\r
219 );\r
8a67d61d 220\r
c4a62a12 221 Udp4ClearVariableData (Udp4Service);\r
8a67d61d 222\r
c4a62a12 223 Udp4CleanService (Udp4Service);\r
8a67d61d 224\r
766c7483 225 FreePool (Udp4Service);\r
c4a62a12 226 } else {\r
8a67d61d 227\r
e48e37fc 228 while (!IsListEmpty (&Udp4Service->ChildrenList)) {\r
c4a62a12 229 Instance = NET_LIST_HEAD (&Udp4Service->ChildrenList, UDP4_INSTANCE_DATA, Link);\r
8a67d61d 230\r
c4a62a12 231 ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle);\r
232 }\r
233 }\r
234\r
235 return Status;\r
8a67d61d 236}\r
237\r
238\r
239/**\r
3e8c18da 240 Creates a child handle and installs a protocol.\r
241 \r
242 The CreateChild() function installs a protocol on ChildHandle. \r
243 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. \r
244 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
8a67d61d 245\r
47c75f64 246 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
247 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
248 then a new handle is created. If it is a pointer to an existing UEFI handle, \r
249 then the protocol is added to the existing UEFI handle.\r
8a67d61d 250\r
3e8c18da 251 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
bab52709 252 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
253 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
254 the child\r
255 @retval other The child handle was not created\r
8a67d61d 256\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260Udp4ServiceBindingCreateChild (\r
261 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
262 IN EFI_HANDLE *ChildHandle\r
263 )\r
264{\r
265 EFI_STATUS Status;\r
266 UDP4_SERVICE_DATA *Udp4Service;\r
267 UDP4_INSTANCE_DATA *Instance;\r
268 EFI_TPL OldTpl;\r
269 VOID *Ip4;\r
270\r
271 if ((This == NULL) || (ChildHandle == NULL)) {\r
272 return EFI_INVALID_PARAMETER;\r
273 }\r
274\r
275 Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (This);\r
276\r
277 //\r
278 // Allocate the instance private data structure.\r
279 //\r
e48e37fc 280 Instance = AllocateZeroPool (sizeof (UDP4_INSTANCE_DATA));\r
8a67d61d 281 if (Instance == NULL) {\r
282 return EFI_OUT_OF_RESOURCES;\r
283 }\r
284\r
285 Udp4InitInstance (Udp4Service, Instance);\r
286\r
287 //\r
288 // Add an IpInfo for this instance.\r
289 //\r
290 Instance->IpInfo = IpIoAddIp (Udp4Service->IpIo);\r
291 if (Instance->IpInfo == NULL) {\r
292 Status = EFI_OUT_OF_RESOURCES;\r
c4a62a12 293 goto ON_ERROR;\r
8a67d61d 294 }\r
295\r
296 //\r
297 // Install the Udp4Protocol for this instance.\r
298 //\r
299 Status = gBS->InstallMultipleProtocolInterfaces (\r
300 ChildHandle,\r
301 &gEfiUdp4ProtocolGuid,\r
302 &Instance->Udp4Proto,\r
303 NULL\r
304 );\r
305 if (EFI_ERROR (Status)) {\r
c4a62a12 306 goto ON_ERROR;\r
8a67d61d 307 }\r
308\r
309 Instance->ChildHandle = *ChildHandle;\r
310\r
311 //\r
312 // Open the default Ip4 protocol in the IP_IO BY_CHILD.\r
313 //\r
314 Status = gBS->OpenProtocol (\r
315 Udp4Service->IpIo->ChildHandle,\r
316 &gEfiIp4ProtocolGuid,\r
317 (VOID **) &Ip4,\r
318 gUdp4DriverBinding.DriverBindingHandle,\r
319 Instance->ChildHandle,\r
320 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
321 );\r
322 if (EFI_ERROR (Status)) {\r
c4a62a12 323 goto ON_ERROR;\r
8a67d61d 324 }\r
325\r
e48e37fc 326 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 327\r
328 //\r
329 // Link this instance into the service context data and increase the ChildrenNumber.\r
330 //\r
e48e37fc 331 InsertTailList (&Udp4Service->ChildrenList, &Instance->Link);\r
8a67d61d 332 Udp4Service->ChildrenNumber++;\r
333\r
e48e37fc 334 gBS->RestoreTPL (OldTpl);\r
8a67d61d 335\r
c4a62a12 336 return EFI_SUCCESS;\r
8a67d61d 337\r
c4a62a12 338ON_ERROR:\r
8a67d61d 339\r
c4a62a12 340 if (Instance->ChildHandle != NULL) {\r
341 gBS->UninstallMultipleProtocolInterfaces (\r
342 Instance->ChildHandle,\r
343 &gEfiUdp4ProtocolGuid,\r
344 &Instance->Udp4Proto,\r
345 NULL\r
346 );\r
347 }\r
8a67d61d 348\r
c4a62a12 349 if (Instance->IpInfo != NULL) {\r
350 IpIoRemoveIp (Udp4Service->IpIo, Instance->IpInfo);\r
351 }\r
8a67d61d 352\r
353 Udp4CleanInstance (Instance);\r
354\r
766c7483 355 FreePool (Instance);\r
8a67d61d 356\r
357 return Status;\r
358}\r
359\r
360\r
361/**\r
3e8c18da 362 Destroys a child handle with a protocol installed on it.\r
363 \r
364 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol \r
365 that was installed by CreateChild() from ChildHandle. If the removed protocol is the \r
366 last protocol on ChildHandle, then ChildHandle is destroyed.\r
8a67d61d 367\r
47c75f64 368 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
369 @param[in] ChildHandle Handle of the child to destroy\r
8a67d61d 370\r
3e8c18da 371 @retval EFI_SUCCES The protocol was removed from ChildHandle.\r
372 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r
373 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.\r
374 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
375 because its services are being used.\r
bab52709 376 @retval other The child handle was not destroyed\r
8a67d61d 377\r
378**/\r
379EFI_STATUS\r
380EFIAPI\r
381Udp4ServiceBindingDestroyChild (\r
382 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
383 IN EFI_HANDLE ChildHandle\r
384 )\r
385{\r
386 EFI_STATUS Status;\r
387 UDP4_SERVICE_DATA *Udp4Service;\r
388 EFI_UDP4_PROTOCOL *Udp4Proto;\r
389 UDP4_INSTANCE_DATA *Instance;\r
390 EFI_TPL OldTpl;\r
391\r
392 if ((This == NULL) || (ChildHandle == NULL)) {\r
393 return EFI_INVALID_PARAMETER;\r
394 }\r
395\r
396 Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (This);\r
397\r
398 //\r
399 // Try to get the Udp4 protocol from the ChildHandle.\r
400 //\r
401 Status = gBS->OpenProtocol (\r
402 ChildHandle,\r
403 &gEfiUdp4ProtocolGuid,\r
404 (VOID **) &Udp4Proto,\r
405 gUdp4DriverBinding.DriverBindingHandle,\r
406 ChildHandle,\r
407 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
408 );\r
409 if (EFI_ERROR (Status)) {\r
410 return EFI_UNSUPPORTED;\r
411 }\r
412\r
413 Instance = UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto);\r
414\r
415 if (Instance->Destroyed) {\r
416 return EFI_SUCCESS;\r
417 }\r
418\r
419 //\r
420 // Use the Destroyed flag to avoid the re-entering of the following code.\r
421 //\r
422 Instance->Destroyed = TRUE;\r
423\r
424 //\r
425 // Close the Ip4 protocol.\r
426 //\r
427 gBS->CloseProtocol (\r
428 Udp4Service->IpIo->ChildHandle,\r
429 &gEfiIp4ProtocolGuid,\r
430 gUdp4DriverBinding.DriverBindingHandle,\r
431 Instance->ChildHandle\r
432 );\r
433\r
434 //\r
435 // Uninstall the Udp4Protocol previously installed on the ChildHandle.\r
436 //\r
437 Status = gBS->UninstallMultipleProtocolInterfaces (\r
438 ChildHandle,\r
439 &gEfiUdp4ProtocolGuid,\r
440 (VOID *) &Instance->Udp4Proto,\r
441 NULL\r
442 );\r
443 if (EFI_ERROR (Status)) {\r
444 Instance->Destroyed = FALSE;\r
445 return Status;\r
446 }\r
447\r
448 //\r
449 // Reset the configuration in case the instance's consumer forgets to do this.\r
450 //\r
451 Udp4Proto->Configure (Udp4Proto, NULL);\r
452\r
453 //\r
454 // Remove the IpInfo this instance consumes.\r
455 //\r
456 IpIoRemoveIp (Udp4Service->IpIo, Instance->IpInfo);\r
457\r
e48e37fc 458 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 459\r
460 //\r
461 // Remove this instance from the service context data's ChildrenList.\r
462 //\r
e48e37fc 463 RemoveEntryList (&Instance->Link);\r
8a67d61d 464 Udp4Service->ChildrenNumber--;\r
465\r
466 //\r
467 // Clean the instance.\r
468 //\r
469 Udp4CleanInstance (Instance);\r
470\r
e48e37fc 471 gBS->RestoreTPL (OldTpl);\r
8a67d61d 472\r
766c7483 473 FreePool (Instance);\r
8a67d61d 474\r
475 return EFI_SUCCESS;\r
476}\r
477\r
bab52709 478/**\r
479 This is the declaration of an EFI image entry point. This entry point is\r
480 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
481 both device drivers and bus drivers.\r
482 \r
483 The entry point for Udp4 driver which installs the driver binding\r
484 and component name protocol on its ImageHandle.\r
485\r
3e8c18da 486 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
487 @param[in] SystemTable A pointer to the EFI System Table.\r
bab52709 488\r
489 @retval EFI_SUCCESS The operation completed successfully.\r
490 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
8a67d61d 491\r
bab52709 492**/\r
8a67d61d 493EFI_STATUS\r
494EFIAPI\r
495Udp4DriverEntryPoint (\r
496 IN EFI_HANDLE ImageHandle,\r
497 IN EFI_SYSTEM_TABLE *SystemTable\r
498 )\r
8a67d61d 499{\r
500 EFI_STATUS Status;\r
501\r
502 //\r
503 // Install the Udp4DriverBinding and Udp4ComponentName protocols.\r
504 //\r
83cbd279 505 Status = EfiLibInstallDriverBindingComponentName2 (\r
8a67d61d 506 ImageHandle,\r
507 SystemTable,\r
508 &gUdp4DriverBinding,\r
509 ImageHandle,\r
510 &gUdp4ComponentName,\r
83cbd279 511 &gUdp4ComponentName2\r
8a67d61d 512 );\r
513 if (!EFI_ERROR (Status)) {\r
514 //\r
515 // Initialize the UDP random port.\r
516 //\r
687a2e5f 517 mUdp4RandomPort = (UINT16) (((UINT16) NetRandomInitSeed ()) % UDP4_PORT_KNOWN + UDP4_PORT_KNOWN);\r
8a67d61d 518 }\r
519\r
520 return Status;\r
521}\r
522\r