2 Driver Binding functions and Service Binding functions
3 implementation for Mtftp6 Driver.
5 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "Mtftp6Impl.h"
20 EFI_DRIVER_BINDING_PROTOCOL gMtftp6DriverBinding
= {
21 Mtftp6DriverBindingSupported
,
22 Mtftp6DriverBindingStart
,
23 Mtftp6DriverBindingStop
,
29 EFI_SERVICE_BINDING_PROTOCOL gMtftp6ServiceBindingTemplate
= {
30 Mtftp6ServiceBindingCreateChild
,
31 Mtftp6ServiceBindingDestroyChild
36 Destory the MTFTP6 service. The MTFTP6 service may be partly initialized,
37 or partly destroyed. If a resource is destroyed, it is marked as such in
38 case the destroy failed and is called again later.
40 @param[in] Service The MTFTP6 service to be destroyed.
44 Mtftp6DestroyService (
45 IN MTFTP6_SERVICE
*Service
49 // Make sure all children instances have been already destoryed.
51 ASSERT (Service
->ChildrenNum
== 0);
53 if (Service
->DummyUdpIo
!= NULL
) {
54 UdpIoFreeIo (Service
->DummyUdpIo
);
57 if (Service
->Timer
!= NULL
) {
58 gBS
->CloseEvent (Service
->Timer
);
66 Create then initialize a MTFTP6 service binding instance.
68 @param[in] Controller The controller to install the MTFTP6 service
70 @param[in] Image The driver binding image of the MTFTP6 driver.
71 @param[out] Service The variable to receive the created service
74 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to create the instance
75 @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep connection with UDP.
76 @retval EFI_SUCCESS The service instance is created for the controller.
81 IN EFI_HANDLE Controller
,
83 OUT MTFTP6_SERVICE
**Service
86 MTFTP6_SERVICE
*Mtftp6Srv
;
89 ASSERT (Service
!= NULL
);
92 Mtftp6Srv
= AllocateZeroPool (sizeof (MTFTP6_SERVICE
));
94 if (Mtftp6Srv
== NULL
) {
95 return EFI_OUT_OF_RESOURCES
;
98 Mtftp6Srv
->Signature
= MTFTP6_SERVICE_SIGNATURE
;
99 Mtftp6Srv
->Controller
= Controller
;
100 Mtftp6Srv
->Image
= Image
;
101 Mtftp6Srv
->InDestory
= FALSE
;
102 Mtftp6Srv
->ChildrenNum
= 0;
105 &Mtftp6Srv
->ServiceBinding
,
106 &gMtftp6ServiceBindingTemplate
,
107 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
110 InitializeListHead (&Mtftp6Srv
->Children
);
113 // Create a internal timer for all instances.
115 Status
= gBS
->CreateEvent (
116 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
123 if (EFI_ERROR (Status
)) {
124 FreePool (Mtftp6Srv
);
129 // Create a dummy Udp6Io to build parent-child relationship between Udp6 driver
130 // and Mtftp6 driver.
132 Mtftp6Srv
->DummyUdpIo
= UdpIoCreateIo (
135 Mtftp6ConfigDummyUdpIo
,
140 if (Mtftp6Srv
->DummyUdpIo
== NULL
) {
141 gBS
->CloseEvent (Mtftp6Srv
->Timer
);
142 FreePool (Mtftp6Srv
);
143 return EFI_DEVICE_ERROR
;
146 *Service
= Mtftp6Srv
;
152 Destroy the MTFTP6 instance and recycle the resources.
154 @param[in] Instance The pointer to the MTFTP6 instance.
158 Mtftp6DestroyInstance (
159 IN MTFTP6_INSTANCE
*Instance
164 MTFTP6_BLOCK_RANGE
*Block
;
166 if (Instance
->Config
!= NULL
) {
167 FreePool (Instance
->Config
);
170 if (Instance
->Token
!= NULL
&& Instance
->Token
->Event
!= NULL
) {
171 gBS
->SignalEvent (Instance
->Token
->Event
);
174 if (Instance
->LastPacket
!= NULL
) {
175 NetbufFree (Instance
->LastPacket
);
178 if (Instance
->UdpIo
!= NULL
) {
179 UdpIoFreeIo (Instance
->UdpIo
);
182 if (Instance
->McastUdpIo
!= NULL
) {
183 UdpIoFreeIo (Instance
->McastUdpIo
);
186 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Instance
->BlkList
) {
187 Block
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_BLOCK_RANGE
, Link
);
188 RemoveEntryList (Entry
);
197 Create the MTFTP6 instance and initialize it.
199 @param[in] Service The pointer to the MTFTP6 service.
200 @param[out] Instance The pointer to the MTFTP6 instance.
202 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
203 @retval EFI_SUCCESS The MTFTP6 instance is created.
207 Mtftp6CreateInstance (
208 IN MTFTP6_SERVICE
*Service
,
209 OUT MTFTP6_INSTANCE
**Instance
212 MTFTP6_INSTANCE
*Mtftp6Ins
;
215 Mtftp6Ins
= AllocateZeroPool (sizeof (MTFTP6_INSTANCE
));
217 if (Mtftp6Ins
== NULL
) {
218 return EFI_OUT_OF_RESOURCES
;
221 Mtftp6Ins
->Signature
= MTFTP6_INSTANCE_SIGNATURE
;
222 Mtftp6Ins
->InDestory
= FALSE
;
223 Mtftp6Ins
->Service
= Service
;
227 &gMtftp6ProtocolTemplate
,
228 sizeof (EFI_MTFTP6_PROTOCOL
)
231 InitializeListHead (&Mtftp6Ins
->Link
);
232 InitializeListHead (&Mtftp6Ins
->BlkList
);
234 *Instance
= Mtftp6Ins
;
241 This is the declaration of an EFI image entry point. This entry point is
242 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers, including
243 both device drivers and bus drivers.
245 Entry point of the MTFTP6 driver to install various protocols.
247 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
248 @param[in] SystemTable The pointer to the EFI System Table.
250 @retval EFI_SUCCESS The operation completed successfully.
251 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
256 Mtftp6DriverEntryPoint (
257 IN EFI_HANDLE ImageHandle
,
258 IN EFI_SYSTEM_TABLE
*SystemTable
261 return EfiLibInstallDriverBindingComponentName2 (
264 &gMtftp6DriverBinding
,
266 &gMtftp6ComponentName
,
267 &gMtftp6ComponentName2
273 Test to see if this driver supports Controller. This service
274 is called by the EFI boot service ConnectController(). In
275 order to make drivers as small as possible, there are calling
276 restrictions for this service. ConnectController() must
277 follow these calling restrictions. If any other agent wishes to call
278 Supported(), it must also follow these calling restrictions.
280 @param[in] This Protocol instance pointer.
281 @param[in] Controller Handle of device to test
282 @param[in] RemainingDevicePath Optional parameter use to pick a specific child.
285 @retval EFI_SUCCESS This driver supports this device.
286 @retval Others This driver does not support this device.
291 Mtftp6DriverBindingSupported (
292 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
293 IN EFI_HANDLE Controller
,
294 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
297 return gBS
->OpenProtocol (
299 &gEfiUdp6ServiceBindingProtocolGuid
,
301 This
->DriverBindingHandle
,
303 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
309 Start this driver on Controller. This service is called by the
310 EFI boot service ConnectController(). In order to make
311 drivers as small as possible, there are calling restrictions for
312 this service. ConnectController() must follow these
313 calling restrictions. If any other agent wishes to call Start() it
314 must also follow these calling restrictions.
316 @param[in] This Protocol instance pointer.
317 @param[in] Controller Handle of device to bind driver to.
318 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
321 @retval EFI_SUCCESS This driver is added to Controller.
322 @retval EFI_ALREADY_STARTED This driver is already running on Controller.
323 @retval Others This driver does not support this device.
328 Mtftp6DriverBindingStart (
329 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
330 IN EFI_HANDLE Controller
,
331 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
334 MTFTP6_SERVICE
*Service
;
338 // Directly return if driver is already running on this Nic handle.
340 Status
= gBS
->OpenProtocol (
342 &gEfiMtftp6ServiceBindingProtocolGuid
,
344 This
->DriverBindingHandle
,
346 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
349 if (!EFI_ERROR (Status
)) {
350 return EFI_ALREADY_STARTED
;
354 // Create Mtftp6 service for this Nic handle
356 Status
= Mtftp6CreateService (
358 This
->DriverBindingHandle
,
362 if (EFI_ERROR (Status
)) {
366 ASSERT (Service
!= NULL
);
369 // Start the internal timer to track the packet retransmission.
371 Status
= gBS
->SetTimer (
377 if (EFI_ERROR (Status
)) {
382 // Install the Mtftp6 service on the Nic handle.
384 Status
= gBS
->InstallMultipleProtocolInterfaces (
386 &gEfiMtftp6ServiceBindingProtocolGuid
,
387 &Service
->ServiceBinding
,
391 if (EFI_ERROR (Status
)) {
399 Mtftp6DestroyService (Service
);
405 Stop this driver on Controller. This service is called by the
406 EFI boot service DisconnectController(). In order to
407 make drivers as small as possible, there are calling
408 restrictions for this service. DisconnectController()
409 must follow these calling restrictions. If any other agent wishes
410 to call Stop(), it must also follow these calling restrictions.
412 @param[in] This Protocol instance pointer.
413 @param[in] Controller Handle of device to stop driver on
414 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
415 children is zero, stop the entire bus driver.
416 @param[in] ChildHandleBuffer List of Child Handles to Stop.
418 @retval EFI_SUCCESS This driver is removed Controller.
419 @retval EFI_DEVICE_ERROR An unexpected error.
420 @retval Others This driver was not removed from this device.
425 Mtftp6DriverBindingStop (
426 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
427 IN EFI_HANDLE Controller
,
428 IN UINTN NumberOfChildren
,
429 IN EFI_HANDLE
*ChildHandleBuffer
432 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
433 MTFTP6_SERVICE
*Service
;
434 MTFTP6_INSTANCE
*Instance
;
435 EFI_HANDLE NicHandle
;
440 // Locate the Nic handle to retrieve the Mtftp6 private data.
442 NicHandle
= NetLibGetNicHandle (Controller
, &gEfiUdp6ProtocolGuid
);
444 if (NicHandle
== NULL
) {
445 return EFI_DEVICE_ERROR
;
448 Status
= gBS
->OpenProtocol (
450 &gEfiMtftp6ServiceBindingProtocolGuid
,
451 (VOID
**) &ServiceBinding
,
452 This
->DriverBindingHandle
,
454 EFI_OPEN_PROTOCOL_GET_PROTOCOL
457 if (EFI_ERROR (Status
)) {
458 return EFI_DEVICE_ERROR
;
461 Service
= MTFTP6_SERVICE_FROM_THIS (ServiceBinding
);
463 if (Service
->InDestory
) {
467 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
469 if (NumberOfChildren
== 0) {
471 // Destory the Mtftp6 service if there is no Mtftp6 child instance left.
473 Service
->InDestory
= TRUE
;
475 gBS
->UninstallProtocolInterface (
477 &gEfiMtftp6ServiceBindingProtocolGuid
,
481 Mtftp6DestroyService (Service
);
485 // Destory the Mtftp6 child instance one by one.
487 while (!IsListEmpty (&Service
->Children
)) {
488 Instance
= NET_LIST_HEAD (&Service
->Children
, MTFTP6_INSTANCE
, Link
);
489 Mtftp6ServiceBindingDestroyChild (ServiceBinding
, Instance
->Handle
);
492 if (Service
->ChildrenNum
!= 0) {
493 Status
= EFI_DEVICE_ERROR
;
497 gBS
->RestoreTPL (OldTpl
);
503 Creates a child handle and installs a protocol.
505 The CreateChild() function installs a protocol on ChildHandle.
506 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
507 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
509 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
510 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
511 then a new handle is created. If it is a pointer to an existing
512 UEFI handle, then the protocol is added to the existing UEFI handle.
514 @retval EFI_SUCCES The protocol was added to ChildHandle.
515 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
516 @retval Others The child handle was not created.
521 Mtftp6ServiceBindingCreateChild (
522 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
523 IN OUT EFI_HANDLE
*ChildHandle
526 MTFTP6_SERVICE
*Service
;
527 MTFTP6_INSTANCE
*Instance
;
532 if (This
== NULL
|| ChildHandle
== NULL
) {
533 return EFI_INVALID_PARAMETER
;
536 Service
= MTFTP6_SERVICE_FROM_THIS (This
);
538 Status
= Mtftp6CreateInstance (Service
, &Instance
);
540 if (EFI_ERROR (Status
)) {
544 ASSERT (Instance
!= NULL
);
547 // Install the Mtftp6 protocol on the new child handle.
549 Status
= gBS
->InstallMultipleProtocolInterfaces (
551 &gEfiMtftp6ProtocolGuid
,
556 if (EFI_ERROR (Status
)) {
560 Instance
->Handle
= *ChildHandle
;
563 // Open the Udp6 protocol by child.
565 Status
= gBS
->OpenProtocol (
566 Service
->DummyUdpIo
->UdpHandle
,
567 &gEfiUdp6ProtocolGuid
,
569 gMtftp6DriverBinding
.DriverBindingHandle
,
571 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
574 if (EFI_ERROR (Status
)) {
575 gBS
->UninstallMultipleProtocolInterfaces (
577 &gEfiMtftp6ProtocolGuid
,
586 // Add the new Mtftp6 instance into the children list of Mtftp6 service.
588 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
590 InsertTailList (&Service
->Children
, &Instance
->Link
);
591 Service
->ChildrenNum
++;
593 gBS
->RestoreTPL (OldTpl
);
598 Mtftp6DestroyInstance (Instance
);
604 Destroys a child handle with a protocol installed on it.
606 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
607 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
608 last protocol on ChildHandle, then ChildHandle is destroyed.
610 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
611 @param[in] ChildHandle Handle of the child to destroy.
613 @retval EFI_SUCCES The protocol was removed from ChildHandle.
614 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
615 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
616 @retval Others The child handle was not destroyed
621 Mtftp6ServiceBindingDestroyChild (
622 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
623 IN EFI_HANDLE ChildHandle
626 MTFTP6_SERVICE
*Service
;
627 MTFTP6_INSTANCE
*Instance
;
628 EFI_MTFTP6_PROTOCOL
*Mtftp6
;
632 if (This
== NULL
|| ChildHandle
== NULL
) {
633 return EFI_INVALID_PARAMETER
;
637 // Locate the Nic handle to retrieve the Mtftp6 private data.
639 Status
= gBS
->OpenProtocol (
641 &gEfiMtftp6ProtocolGuid
,
643 gMtftp6DriverBinding
.DriverBindingHandle
,
645 EFI_OPEN_PROTOCOL_GET_PROTOCOL
648 if (EFI_ERROR (Status
)) {
649 return EFI_UNSUPPORTED
;
652 Instance
= MTFTP6_INSTANCE_FROM_THIS (Mtftp6
);
653 Service
= MTFTP6_SERVICE_FROM_THIS (This
);
655 if (Instance
->Service
!= Service
) {
656 return EFI_INVALID_PARAMETER
;
660 // Check whether the instance already in destory state.
662 if (Instance
->InDestory
) {
666 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
668 Instance
->InDestory
= TRUE
;
671 Service
->DummyUdpIo
->UdpHandle
,
672 &gEfiUdp6ProtocolGuid
,
673 gMtftp6DriverBinding
.DriverBindingHandle
,
678 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
680 Status
= gBS
->UninstallProtocolInterface (
682 &gEfiMtftp6ProtocolGuid
,
686 if (EFI_ERROR (Status
)) {
687 Instance
->InDestory
= FALSE
;
688 gBS
->RestoreTPL (OldTpl
);
693 // Remove the Mtftp6 instance from the children list of Mtftp6 service.
695 RemoveEntryList (&Instance
->Link
);
696 Service
->ChildrenNum
--;
698 Mtftp6DestroyInstance (Instance
);
700 gBS
->RestoreTPL (OldTpl
);