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