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