2 Implementation of Mtftp drivers.
4 Copyright (c) 2006 - 2010, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Mtftp4Impl.h"
17 EFI_DRIVER_BINDING_PROTOCOL gMtftp4DriverBinding
= {
18 Mtftp4DriverBindingSupported
,
19 Mtftp4DriverBindingStart
,
20 Mtftp4DriverBindingStop
,
26 EFI_SERVICE_BINDING_PROTOCOL gMtftp4ServiceBindingTemplete
= {
27 Mtftp4ServiceBindingCreateChild
,
28 Mtftp4ServiceBindingDestroyChild
33 The driver entry point which installs multiple protocols to the ImageHandle.
35 @param ImageHandle The MTFTP's image handle.
36 @param SystemTable The system table.
38 @retval EFI_SUCCESS The handles are successfully installed on the image.
39 @retval others some EFI_ERROR occured.
44 Mtftp4DriverEntryPoint (
45 IN EFI_HANDLE ImageHandle
,
46 IN EFI_SYSTEM_TABLE
*SystemTable
49 return EfiLibInstallDriverBindingComponentName2 (
52 &gMtftp4DriverBinding
,
54 &gMtftp4ComponentName
,
55 &gMtftp4ComponentName2
61 Test whether MTFTP driver support this controller.
63 @param This The MTFTP driver binding instance
64 @param Controller The controller to test
65 @param RemainingDevicePath The remaining device path
67 @retval EFI_SUCCESS The controller has UDP service binding protocol
68 installed, MTFTP can support it.
69 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
70 RemainingDevicePath is already being managed by
71 the driver specified by This.
72 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
73 RemainingDevicePath is already being managed by a
74 different driver or an application that requires
76 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
77 RemainingDevicePath is not supported by the driver
83 Mtftp4DriverBindingSupported (
84 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
85 IN EFI_HANDLE Controller
,
86 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
91 Status
= gBS
->OpenProtocol (
93 &gEfiUdp4ServiceBindingProtocolGuid
,
95 This
->DriverBindingHandle
,
97 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
105 Config a NULL UDP that is used to keep the connection between UDP and MTFTP.
107 Just leave the Udp child unconfigured. When UDP is unloaded,
108 MTFTP will be informed with DriverBinding Stop.
110 @param UdpIo The UDP_IO to configure
111 @param Context The opaque parameter to the callback
113 @retval EFI_SUCCESS It always return EFI_SUCCESS directly.
117 Mtftp4ConfigNullUdp (
127 Create then initialize a MTFTP service binding instance.
129 @param Controller The controller to install the MTFTP service
131 @param Image The driver binding image of the MTFTP driver
132 @param Service The variable to receive the created service
135 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the instance
136 @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep
138 @retval EFI_SUCCESS The service instance is created for the
143 Mtftp4CreateService (
144 IN EFI_HANDLE Controller
,
146 OUT MTFTP4_SERVICE
**Service
149 MTFTP4_SERVICE
*MtftpSb
;
153 MtftpSb
= AllocatePool (sizeof (MTFTP4_SERVICE
));
155 if (MtftpSb
== NULL
) {
156 return EFI_OUT_OF_RESOURCES
;
159 MtftpSb
->Signature
= MTFTP4_SERVICE_SIGNATURE
;
160 MtftpSb
->ServiceBinding
= gMtftp4ServiceBindingTemplete
;
161 MtftpSb
->InDestory
= FALSE
;
162 MtftpSb
->ChildrenNum
= 0;
163 InitializeListHead (&MtftpSb
->Children
);
165 MtftpSb
->Timer
= NULL
;
166 MtftpSb
->TimerToGetMap
= NULL
;
167 MtftpSb
->Controller
= Controller
;
168 MtftpSb
->Image
= Image
;
169 MtftpSb
->ConnectUdp
= NULL
;
172 // Create the timer and a udp to be notified when UDP is uninstalled
174 Status
= gBS
->CreateEvent (
175 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
182 if (EFI_ERROR (Status
)) {
188 // Create the timer used to time out the procedure which is used to
189 // get the default IP address.
191 Status
= gBS
->CreateEvent (
196 &MtftpSb
->TimerToGetMap
198 if (EFI_ERROR (Status
)) {
199 gBS
->CloseEvent (MtftpSb
->Timer
);
204 MtftpSb
->ConnectUdp
= UdpIoCreateIo (
212 if (MtftpSb
->ConnectUdp
== NULL
) {
213 gBS
->CloseEvent (MtftpSb
->TimerToGetMap
);
214 gBS
->CloseEvent (MtftpSb
->Timer
);
216 return EFI_DEVICE_ERROR
;
225 Release all the resource used the MTFTP service binding instance.
227 @param MtftpSb The MTFTP service binding instance.
232 IN MTFTP4_SERVICE
*MtftpSb
235 UdpIoFreeIo (MtftpSb
->ConnectUdp
);
236 gBS
->CloseEvent (MtftpSb
->TimerToGetMap
);
237 gBS
->CloseEvent (MtftpSb
->Timer
);
242 Start the MTFTP driver on this controller.
244 MTFTP driver will install a MTFTP SERVICE BINDING protocol on the supported
245 controller, which can be used to create/destroy MTFTP children.
247 @param This The MTFTP driver binding protocol.
248 @param Controller The controller to manage.
249 @param RemainingDevicePath Remaining device path.
251 @retval EFI_ALREADY_STARTED The MTFTP service binding protocol has been
252 started on the controller.
253 @retval EFI_SUCCESS The MTFTP service binding is installed on the
259 Mtftp4DriverBindingStart (
260 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
261 IN EFI_HANDLE Controller
,
262 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
265 MTFTP4_SERVICE
*MtftpSb
;
269 // Directly return if driver is already running.
271 Status
= gBS
->OpenProtocol (
273 &gEfiMtftp4ServiceBindingProtocolGuid
,
275 This
->DriverBindingHandle
,
277 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
280 if (Status
== EFI_SUCCESS
) {
281 return EFI_ALREADY_STARTED
;
284 Status
= Mtftp4CreateService (Controller
, This
->DriverBindingHandle
, &MtftpSb
);
286 if (EFI_ERROR (Status
)) {
289 ASSERT (MtftpSb
!= NULL
);
291 Status
= gBS
->SetTimer (MtftpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
293 if (EFI_ERROR (Status
)) {
298 // Install the Mtftp4ServiceBinding Protocol onto Controller
300 Status
= gBS
->InstallMultipleProtocolInterfaces (
302 &gEfiMtftp4ServiceBindingProtocolGuid
,
303 &MtftpSb
->ServiceBinding
,
307 if (EFI_ERROR (Status
)) {
314 Mtftp4CleanService (MtftpSb
);
322 Stop the MTFTP driver on controller. The controller is a UDP
325 @param This The MTFTP driver binding protocol
326 @param Controller The controller to stop
327 @param NumberOfChildren The number of children
328 @param ChildHandleBuffer The array of the child handle.
330 @retval EFI_SUCCESS The driver is stopped on the controller.
331 @retval EFI_DEVICE_ERROR Failed to stop the driver on the controller.
336 Mtftp4DriverBindingStop (
337 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
338 IN EFI_HANDLE Controller
,
339 IN UINTN NumberOfChildren
,
340 IN EFI_HANDLE
*ChildHandleBuffer
343 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
344 MTFTP4_SERVICE
*MtftpSb
;
345 MTFTP4_PROTOCOL
*Instance
;
346 EFI_HANDLE NicHandle
;
351 // MTFTP driver opens UDP child, So, Controller is a UDP
352 // child handle. Locate the Nic handle first. Then get the
353 // MTFTP private data back.
355 NicHandle
= NetLibGetNicHandle (Controller
, &gEfiUdp4ProtocolGuid
);
357 if (NicHandle
== NULL
) {
358 return EFI_DEVICE_ERROR
;
361 Status
= gBS
->OpenProtocol (
363 &gEfiMtftp4ServiceBindingProtocolGuid
,
364 (VOID
**) &ServiceBinding
,
365 This
->DriverBindingHandle
,
367 EFI_OPEN_PROTOCOL_GET_PROTOCOL
370 if (EFI_ERROR (Status
)) {
371 return EFI_DEVICE_ERROR
;
374 MtftpSb
= MTFTP4_SERVICE_FROM_THIS (ServiceBinding
);
376 if (MtftpSb
->InDestory
) {
380 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
382 if (NumberOfChildren
== 0) {
384 MtftpSb
->InDestory
= TRUE
;
386 gBS
->UninstallProtocolInterface (
388 &gEfiMtftp4ServiceBindingProtocolGuid
,
392 Mtftp4CleanService (MtftpSb
);
397 while (!IsListEmpty (&MtftpSb
->Children
)) {
398 Instance
= NET_LIST_HEAD (&MtftpSb
->Children
, MTFTP4_PROTOCOL
, Link
);
399 Mtftp4ServiceBindingDestroyChild (ServiceBinding
, Instance
->Handle
);
402 if (MtftpSb
->ChildrenNum
!= 0) {
403 Status
= EFI_DEVICE_ERROR
;
407 gBS
->RestoreTPL (OldTpl
);
413 Initialize a MTFTP protocol instance which is the child of MtftpSb.
415 @param MtftpSb The MTFTP service binding protocol.
416 @param Instance The MTFTP instance to initialize.
421 IN MTFTP4_SERVICE
*MtftpSb
,
422 OUT MTFTP4_PROTOCOL
*Instance
425 ZeroMem (Instance
, sizeof (MTFTP4_PROTOCOL
));
427 Instance
->Signature
= MTFTP4_PROTOCOL_SIGNATURE
;
428 InitializeListHead (&Instance
->Link
);
429 CopyMem (&Instance
->Mtftp4
, &gMtftp4ProtocolTemplate
, sizeof (Instance
->Mtftp4
));
430 Instance
->State
= MTFTP4_STATE_UNCONFIGED
;
431 Instance
->InDestory
= FALSE
;
432 Instance
->Service
= MtftpSb
;
434 InitializeListHead (&Instance
->Blocks
);
439 Create a MTFTP child for the service binding instance, then
440 install the MTFTP protocol to the ChildHandle.
442 @param This The MTFTP service binding instance.
443 @param ChildHandle The Child handle to install the MTFTP protocol.
445 @retval EFI_INVALID_PARAMETER The parameter is invalid.
446 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the new child.
447 @retval EFI_SUCCESS The child is successfully create.
452 Mtftp4ServiceBindingCreateChild (
453 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
454 IN EFI_HANDLE
*ChildHandle
457 MTFTP4_SERVICE
*MtftpSb
;
458 MTFTP4_PROTOCOL
*Instance
;
463 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
464 return EFI_INVALID_PARAMETER
;
467 Instance
= AllocatePool (sizeof (*Instance
));
469 if (Instance
== NULL
) {
470 return EFI_OUT_OF_RESOURCES
;
473 MtftpSb
= MTFTP4_SERVICE_FROM_THIS (This
);
475 Mtftp4InitProtocol (MtftpSb
, Instance
);
477 Instance
->UnicastPort
= UdpIoCreateIo (
485 if (Instance
->UnicastPort
== NULL
) {
487 return EFI_OUT_OF_RESOURCES
;
491 // Install the MTFTP protocol onto ChildHandle
493 Status
= gBS
->InstallMultipleProtocolInterfaces (
495 &gEfiMtftp4ProtocolGuid
,
500 if (EFI_ERROR (Status
)) {
504 Instance
->Handle
= *ChildHandle
;
507 // Open the Udp4 protocol BY_CHILD.
509 Status
= gBS
->OpenProtocol (
510 MtftpSb
->ConnectUdp
->UdpHandle
,
511 &gEfiUdp4ProtocolGuid
,
513 gMtftp4DriverBinding
.DriverBindingHandle
,
515 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
517 if (EFI_ERROR (Status
)) {
518 gBS
->UninstallMultipleProtocolInterfaces (
520 &gEfiMtftp4ProtocolGuid
,
529 // Add it to the parent's child list.
531 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
533 InsertTailList (&MtftpSb
->Children
, &Instance
->Link
);
534 MtftpSb
->ChildrenNum
++;
536 gBS
->RestoreTPL (OldTpl
);
540 if (EFI_ERROR (Status
)) {
541 UdpIoFreeIo (Instance
->UnicastPort
);
550 Destory one of the service binding's child.
552 @param This The service binding instance
553 @param ChildHandle The child handle to destory
555 @retval EFI_INVALID_PARAMETER The parameter is invaid.
556 @retval EFI_UNSUPPORTED The child may have already been destoried.
557 @retval EFI_SUCCESS The child is destoried and removed from the
563 Mtftp4ServiceBindingDestroyChild (
564 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
565 IN EFI_HANDLE ChildHandle
568 MTFTP4_SERVICE
*MtftpSb
;
569 MTFTP4_PROTOCOL
*Instance
;
570 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
574 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
575 return EFI_INVALID_PARAMETER
;
579 // Retrieve the private context data structures
581 Status
= gBS
->OpenProtocol (
583 &gEfiMtftp4ProtocolGuid
,
585 gMtftp4DriverBinding
.DriverBindingHandle
,
587 EFI_OPEN_PROTOCOL_GET_PROTOCOL
590 if (EFI_ERROR (Status
)) {
591 return EFI_UNSUPPORTED
;
594 Instance
= MTFTP4_PROTOCOL_FROM_THIS (Mtftp4
);
595 MtftpSb
= MTFTP4_SERVICE_FROM_THIS (This
);
597 if (Instance
->Service
!= MtftpSb
) {
598 return EFI_INVALID_PARAMETER
;
601 if (Instance
->InDestory
) {
605 Instance
->InDestory
= TRUE
;
608 // Close the Udp4 protocol.
611 MtftpSb
->ConnectUdp
->UdpHandle
,
612 &gEfiUdp4ProtocolGuid
,
613 gMtftp4DriverBinding
.DriverBindingHandle
,
618 // Uninstall the MTFTP4 protocol first to enable a top down destruction.
620 Status
= gBS
->UninstallProtocolInterface (
622 &gEfiMtftp4ProtocolGuid
,
626 if (EFI_ERROR (Status
)) {
627 Instance
->InDestory
= FALSE
;
631 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
633 Mtftp4CleanOperation (Instance
, EFI_DEVICE_ERROR
);
634 UdpIoFreeIo (Instance
->UnicastPort
);
636 RemoveEntryList (&Instance
->Link
);
637 MtftpSb
->ChildrenNum
--;
639 gBS
->RestoreTPL (OldTpl
);