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