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