]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Udp6Dxe/Udp6Driver.c
1. Add EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName() support.
[mirror_edk2.git] / NetworkPkg / Udp6Dxe / Udp6Driver.c
1 /** @file
2 Driver Binding functions and Service Binding functions for the Network driver module.
3
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Udp6Impl.h"
17
18 EFI_DRIVER_BINDING_PROTOCOL gUdp6DriverBinding = {
19 Udp6DriverBindingSupported,
20 Udp6DriverBindingStart,
21 Udp6DriverBindingStop,
22 0xa,
23 NULL,
24 NULL
25 };
26
27 EFI_SERVICE_BINDING_PROTOCOL mUdp6ServiceBinding = {
28 Udp6ServiceBindingCreateChild,
29 Udp6ServiceBindingDestroyChild
30 };
31
32 /**
33 Tests to see if this driver supports a given controller. If a child device is provided,
34 it further tests to see if this driver supports creating a handle for the specified child device.
35
36 This function checks to see if the driver specified by This supports the device specified by
37 ControllerHandle. Drivers will typically use the device path attached to
38 ControllerHandle and/or the services from the bus I/O abstraction attached to
39 ControllerHandle to determine if the driver supports ControllerHandle. This function
40 may be called many times during platform initialization. In order to reduce boot times, the tests
41 performed by this function must be very small, and take as little time as possible to execute. This
42 function must not change the state of any hardware devices, and this function must be aware that the
43 device specified by ControllerHandle may already be managed by the same driver or a
44 different driver. This function must match its calls to AllocatePages() with FreePages(),
45 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
46 Because ControllerHandle may have been previously started by the same driver, if a protocol is
47 already in the opened state, then it must not be closed with CloseProtocol(). This is required
48 to guarantee the state of ControllerHandle is not modified by this function.
49
50 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
51 @param[in] ControllerHandle The handle of the controller to test. This handle
52 must support a protocol interface that supplies
53 an I/O abstraction to the driver.
54 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
55 parameter is ignored by device drivers, and is optional for bus
56 drivers. For bus drivers, if this parameter is not NULL, then
57 the bus driver must determine if the bus controller specified
58 by ControllerHandle and the child controller specified
59 by RemainingDevicePath are both supported by this
60 bus driver.
61
62 @retval EFI_SUCCESS The device specified by ControllerHandle and
63 RemainingDevicePath is supported by the driver specified by This.
64 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
65 RemainingDevicePath is already being managed by the driver
66 specified by This.
67 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
68 RemainingDevicePath is already being managed by a different
69 driver or an application that requires exclusive access.
70 Currently not implemented.
71 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
72 RemainingDevicePath is not supported by the driver specified by This.
73 **/
74 EFI_STATUS
75 EFIAPI
76 Udp6DriverBindingSupported (
77 IN EFI_DRIVER_BINDING_PROTOCOL *This,
78 IN EFI_HANDLE ControllerHandle,
79 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
80 )
81 {
82 EFI_STATUS Status;
83 //
84 // Test for the Udp6ServiceBinding Protocol
85 //
86 Status = gBS->OpenProtocol (
87 ControllerHandle,
88 &gEfiUdp6ServiceBindingProtocolGuid,
89 NULL,
90 This->DriverBindingHandle,
91 ControllerHandle,
92 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
93 );
94 if (!EFI_ERROR (Status)) {
95 return EFI_ALREADY_STARTED;
96 }
97 //
98 // Test for the Ip6ServiceBinding Protocol
99 //
100 Status = gBS->OpenProtocol (
101 ControllerHandle,
102 &gEfiIp6ServiceBindingProtocolGuid,
103 NULL,
104 This->DriverBindingHandle,
105 ControllerHandle,
106 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
107 );
108
109 return Status;
110 }
111
112 /**
113 Start this driver on ControllerHandle.
114
115 This service is called by the EFI boot service ConnectController(). In order to make
116 drivers as small as possible, there are a few calling restrictions for
117 this service. ConnectController() must follow these
118 calling restrictions. If any other agent wishes to call Start() it
119 must also follow these calling restrictions.
120
121 @param[in] This Protocol instance pointer.
122 @param[in] ControllerHandle Handle of device to bind the driver to.
123 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
124 device to start.
125
126 @retval EFI_SUCCES This driver is added to ControllerHandle.
127 @retval EFI_OUT_OF_RESOURCES The required system resource can't be allocated.
128 @retval other This driver does not support this device.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 Udp6DriverBindingStart (
134 IN EFI_DRIVER_BINDING_PROTOCOL *This,
135 IN EFI_HANDLE ControllerHandle,
136 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
137 )
138 {
139 EFI_STATUS Status;
140 UDP6_SERVICE_DATA *Udp6Service;
141
142 //
143 // Allocate Private Context Data Structure.
144 //
145 Udp6Service = AllocateZeroPool (sizeof (UDP6_SERVICE_DATA));
146 if (Udp6Service == NULL) {
147 Status = EFI_OUT_OF_RESOURCES;
148 goto EXIT;
149 }
150
151 Status = Udp6CreateService (Udp6Service, This->DriverBindingHandle, ControllerHandle);
152 if (EFI_ERROR (Status)) {
153 goto EXIT;
154 }
155
156 //
157 // Install the Udp6ServiceBindingProtocol on the ControllerHandle.
158 //
159 Status = gBS->InstallMultipleProtocolInterfaces (
160 &ControllerHandle,
161 &gEfiUdp6ServiceBindingProtocolGuid,
162 &Udp6Service->ServiceBinding,
163 NULL
164 );
165 if (EFI_ERROR (Status)) {
166 Udp6CleanService (Udp6Service);
167 goto EXIT;
168 } else {
169 Status = Udp6SetVariableData (Udp6Service);
170 }
171
172 EXIT:
173 if (EFI_ERROR (Status)) {
174 if (Udp6Service != NULL) {
175 FreePool (Udp6Service);
176 }
177 }
178 return Status;
179 }
180
181 /**
182 Callback function which provided by user to remove one node in NetDestroyLinkList process.
183
184 @param[in] Entry The entry to be removed.
185 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
186
187 @retval EFI_SUCCESS The entry has been removed successfully.
188 @retval Others Fail to remove the entry.
189
190 **/
191 EFI_STATUS
192 Udp6DestroyChildEntryInHandleBuffer (
193 IN LIST_ENTRY *Entry,
194 IN VOID *Context
195 )
196 {
197 UDP6_INSTANCE_DATA *Instance;
198 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
199 UINTN NumberOfChildren;
200 EFI_HANDLE *ChildHandleBuffer;
201
202 if (Entry == NULL || Context == NULL) {
203 return EFI_INVALID_PARAMETER;
204 }
205
206 Instance = NET_LIST_USER_STRUCT_S (Entry, UDP6_INSTANCE_DATA, Link, UDP6_INSTANCE_DATA_SIGNATURE);
207 ServiceBinding = ((UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;
208 NumberOfChildren = ((UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;
209 ChildHandleBuffer = ((UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;
210
211 if (!NetIsInHandleBuffer (Instance->ChildHandle, NumberOfChildren, ChildHandleBuffer)) {
212 return EFI_SUCCESS;
213 }
214
215 return ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle);
216 }
217
218 /**
219 Stop this driver on ControllerHandle.
220
221 This service is called by the EFI boot service DisconnectController(). In order to
222 make drivers as small as possible, there are a few calling
223 restrictions for this service. DisconnectController()
224 must follow these calling restrictions. If any other agent wishes
225 to call Stop(), it must also follow these calling restrictions.
226
227 @param[in] This Protocol instance pointer.
228 @param[in] ControllerHandle Handle of device to stop the driver on.
229 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If the number
230 of children is zero stop the entire bus driver.
231 @param[in] ChildHandleBuffer List of Child Handles to Stop. It is optional.
232
233 @retval EFI_SUCCES This driver is removed ControllerHandle.
234 @retval EFI_DEVICE_ERROR Can't find the NicHandle from the ControllerHandle and specified GUID.
235 @retval other This driver was not removed from this device.
236
237 **/
238 EFI_STATUS
239 EFIAPI
240 Udp6DriverBindingStop (
241 IN EFI_DRIVER_BINDING_PROTOCOL *This,
242 IN EFI_HANDLE ControllerHandle,
243 IN UINTN NumberOfChildren,
244 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
245 )
246 {
247 EFI_STATUS Status;
248 EFI_HANDLE NicHandle;
249 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
250 UDP6_SERVICE_DATA *Udp6Service;
251 LIST_ENTRY *List;
252 UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
253
254 //
255 // Find the NicHandle where UDP6 ServiceBinding Protocol is installed.
256 //
257 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp6ProtocolGuid);
258 if (NicHandle == NULL) {
259 return EFI_SUCCESS;
260 }
261
262 //
263 // Retrieve the UDP6 ServiceBinding Protocol.
264 //
265 Status = gBS->OpenProtocol (
266 NicHandle,
267 &gEfiUdp6ServiceBindingProtocolGuid,
268 (VOID **) &ServiceBinding,
269 This->DriverBindingHandle,
270 NicHandle,
271 EFI_OPEN_PROTOCOL_GET_PROTOCOL
272 );
273 if (EFI_ERROR (Status)) {
274 return EFI_DEVICE_ERROR;
275 }
276
277 Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (ServiceBinding);
278
279 if (NumberOfChildren != 0) {
280 //
281 // NumberOfChildren is not zero, destroy the children instances in ChildHandleBuffer.
282 //
283 List = &Udp6Service->ChildrenList;
284 Context.ServiceBinding = ServiceBinding;
285 Context.NumberOfChildren = NumberOfChildren;
286 Context.ChildHandleBuffer = ChildHandleBuffer;
287 Status = NetDestroyLinkList (
288 List,
289 Udp6DestroyChildEntryInHandleBuffer,
290 &Context,
291 NULL
292 );
293 } else if (IsListEmpty (&Udp6Service->ChildrenList)) {
294 gBS->UninstallMultipleProtocolInterfaces (
295 NicHandle,
296 &gEfiUdp6ServiceBindingProtocolGuid,
297 &Udp6Service->ServiceBinding,
298 NULL
299 );
300
301 Udp6ClearVariableData (Udp6Service);
302
303 Udp6CleanService (Udp6Service);
304
305 FreePool (Udp6Service);
306
307 Status = EFI_SUCCESS;
308 }
309
310 return Status;
311 }
312
313 /**
314 Creates a child handle and installs a protocol.
315
316 The CreateChild() function installs a protocol on ChildHandle.
317 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
318 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
319
320 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
321 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
322 then a new handle is created. If it is a pointer to an existing UEFI handle,
323 then the protocol is added to the existing UEFI handle.
324
325 @retval EFI_SUCCES The protocol was added to ChildHandle.
326 @retval EFI_INVALID_PARAMETER This is NULL or ChildHandle is NULL.
327 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
328 the child.
329 @retval other The child handle was not created.
330
331 **/
332 EFI_STATUS
333 EFIAPI
334 Udp6ServiceBindingCreateChild (
335 IN EFI_SERVICE_BINDING_PROTOCOL *This,
336 IN OUT EFI_HANDLE *ChildHandle
337 )
338 {
339 EFI_STATUS Status;
340 UDP6_SERVICE_DATA *Udp6Service;
341 UDP6_INSTANCE_DATA *Instance;
342 EFI_TPL OldTpl;
343 VOID *Ip6;
344
345 if ((This == NULL) || (ChildHandle == NULL)) {
346 return EFI_INVALID_PARAMETER;
347 }
348
349 Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (This);
350
351 //
352 // Allocate the instance private data structure.
353 //
354 Instance = AllocateZeroPool (sizeof (UDP6_INSTANCE_DATA));
355 if (Instance == NULL) {
356 return EFI_OUT_OF_RESOURCES;
357 }
358
359 Udp6InitInstance (Udp6Service, Instance);
360
361 //
362 // Add an IpInfo for this instance.
363 //
364 Instance->IpInfo = IpIoAddIp (Udp6Service->IpIo);
365 if (Instance->IpInfo == NULL) {
366 Status = EFI_OUT_OF_RESOURCES;
367 goto ON_ERROR;
368 }
369
370 //
371 // Install the Udp6Protocol for this instance.
372 //
373 Status = gBS->InstallMultipleProtocolInterfaces (
374 ChildHandle,
375 &gEfiUdp6ProtocolGuid,
376 &Instance->Udp6Proto,
377 NULL
378 );
379 if (EFI_ERROR (Status)) {
380 goto ON_ERROR;
381 }
382
383 Instance->ChildHandle = *ChildHandle;
384
385 //
386 // Open the default Ip6 protocol in the IP_IO BY_CHILD.
387 //
388 Status = gBS->OpenProtocol (
389 Udp6Service->IpIo->ChildHandle,
390 &gEfiIp6ProtocolGuid,
391 (VOID **) &Ip6,
392 gUdp6DriverBinding.DriverBindingHandle,
393 Instance->ChildHandle,
394 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
395 );
396 if (EFI_ERROR (Status)) {
397 goto ON_ERROR;
398 }
399
400 //
401 // Open this instance's Ip6 protocol in the IpInfo BY_CHILD.
402 //
403 Status = gBS->OpenProtocol (
404 Instance->IpInfo->ChildHandle,
405 &gEfiIp6ProtocolGuid,
406 (VOID **) &Ip6,
407 gUdp6DriverBinding.DriverBindingHandle,
408 Instance->ChildHandle,
409 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
410 );
411 if (EFI_ERROR (Status)) {
412 goto ON_ERROR;
413 }
414
415 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
416
417 //
418 // Link this instance into the service context data and increase the ChildrenNumber.
419 //
420 InsertTailList (&Udp6Service->ChildrenList, &Instance->Link);
421 Udp6Service->ChildrenNumber++;
422
423 gBS->RestoreTPL (OldTpl);
424
425 return EFI_SUCCESS;
426
427 ON_ERROR:
428
429 if (Instance->ChildHandle != NULL) {
430 gBS->UninstallMultipleProtocolInterfaces (
431 Instance->ChildHandle,
432 &gEfiUdp6ProtocolGuid,
433 &Instance->Udp6Proto,
434 NULL
435 );
436 }
437
438 if (Instance->IpInfo != NULL) {
439 IpIoRemoveIp (Udp6Service->IpIo, Instance->IpInfo);
440 }
441
442 Udp6CleanInstance (Instance);
443
444 FreePool (Instance);
445
446 return Status;
447 }
448
449 /**
450 Destroys a child handle with a set of I/O services.
451 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
452 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
453 last protocol on ChildHandle, then ChildHandle is destroyed.
454
455 @param[in] This Protocol instance pointer.
456 @param[in] ChildHandle Handle of the child to destroy.
457
458 @retval EFI_SUCCES The I/O services were removed from the child
459 handle.
460 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
461 that are being removed.
462 @retval EFI_INVALID_PARAMETER Child handle is NULL.
463 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
464 its I/O services are being used.
465 @retval other The child handle was not destroyed.
466
467 **/
468 EFI_STATUS
469 EFIAPI
470 Udp6ServiceBindingDestroyChild (
471 IN EFI_SERVICE_BINDING_PROTOCOL *This,
472 IN EFI_HANDLE ChildHandle
473 )
474 {
475 EFI_STATUS Status;
476 UDP6_SERVICE_DATA *Udp6Service;
477 EFI_UDP6_PROTOCOL *Udp6Proto;
478 UDP6_INSTANCE_DATA *Instance;
479 EFI_TPL OldTpl;
480
481 if ((This == NULL) || (ChildHandle == NULL)) {
482 return EFI_INVALID_PARAMETER;
483 }
484
485 Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (This);
486
487 //
488 // Try to get the Udp6 protocol from the ChildHandle.
489 //
490 Status = gBS->OpenProtocol (
491 ChildHandle,
492 &gEfiUdp6ProtocolGuid,
493 (VOID **) &Udp6Proto,
494 gUdp6DriverBinding.DriverBindingHandle,
495 ChildHandle,
496 EFI_OPEN_PROTOCOL_GET_PROTOCOL
497 );
498 if (EFI_ERROR (Status)) {
499 return EFI_UNSUPPORTED;
500 }
501
502 Instance = UDP6_INSTANCE_DATA_FROM_THIS (Udp6Proto);
503
504 if (Instance->InDestroy) {
505 return EFI_SUCCESS;
506 }
507
508 //
509 // Use the Destroyed flag to avoid the re-entering of the following code.
510 //
511 Instance->InDestroy = TRUE;
512
513 //
514 // Close the Ip6 protocol on the default IpIo.
515 //
516 gBS->CloseProtocol (
517 Udp6Service->IpIo->ChildHandle,
518 &gEfiIp6ProtocolGuid,
519 gUdp6DriverBinding.DriverBindingHandle,
520 Instance->ChildHandle
521 );
522 //
523 // Close the Ip6 protocol on this instance's IpInfo.
524 //
525 gBS->CloseProtocol (
526 Instance->IpInfo->ChildHandle,
527 &gEfiIp6ProtocolGuid,
528 gUdp6DriverBinding.DriverBindingHandle,
529 Instance->ChildHandle
530 );
531
532 //
533 // Uninstall the Udp6Protocol previously installed on the ChildHandle.
534 //
535 Status = gBS->UninstallMultipleProtocolInterfaces (
536 ChildHandle,
537 &gEfiUdp6ProtocolGuid,
538 (VOID *) &Instance->Udp6Proto,
539 NULL
540 );
541 if (EFI_ERROR (Status)) {
542 Instance->InDestroy = FALSE;
543 return Status;
544 }
545
546 //
547 // Reset the configuration in case the instance's consumer forgets to do this.
548 //
549 Udp6Proto->Configure (Udp6Proto, NULL);
550
551 //
552 // Remove the IpInfo this instance consumes.
553 //
554 IpIoRemoveIp (Udp6Service->IpIo, Instance->IpInfo);
555
556 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
557
558 //
559 // Remove this instance from the service context data's ChildrenList.
560 //
561 RemoveEntryList (&Instance->Link);
562 Udp6Service->ChildrenNumber--;
563
564 //
565 // Clean the instance.
566 //
567 Udp6CleanInstance (Instance);
568
569 gBS->RestoreTPL (OldTpl);
570
571 FreePool (Instance);
572
573 return EFI_SUCCESS;
574 }
575
576 /**
577 This is the declaration of an EFI image entry point. This entry point is
578 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers, including
579 both device drivers and bus drivers.
580
581 The entry point for Udp6 driver that installs the driver binding
582 and component name protocol on its ImageHandle.
583
584 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
585 @param[in] SystemTable A pointer to the EFI System Table.
586
587 @retval EFI_SUCCESS The operation completed successfully.
588 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
589
590 **/
591 EFI_STATUS
592 EFIAPI
593 Udp6DriverEntryPoint (
594 IN EFI_HANDLE ImageHandle,
595 IN EFI_SYSTEM_TABLE *SystemTable
596 )
597 {
598 EFI_STATUS Status;
599
600 //
601 // Install the Udp6DriverBinding and Udp6ComponentName protocols.
602 //
603
604 Status = EfiLibInstallDriverBindingComponentName2 (
605 ImageHandle,
606 SystemTable,
607 &gUdp6DriverBinding,
608 ImageHandle,
609 &gUdp6ComponentName,
610 &gUdp6ComponentName2
611 );
612 if (!EFI_ERROR (Status)) {
613 //
614 // Initialize the UDP random port.
615 //
616 mUdp6RandomPort = (UINT16)(
617 ((UINT16) NetRandomInitSeed ()) %
618 UDP6_PORT_KNOWN +
619 UDP6_PORT_KNOWN
620 );
621 }
622
623 return Status;
624 }
625
626