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