]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Mtftp6Dxe/Mtftp6Driver.c
NetworkPkg: comments clean up.
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Driver.c
1 /** @file
2 Driver Binding functions and Service Binding functions
3 implementation for Mtftp6 Driver.
4
5 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
6
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.
11
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.
14
15 **/
16
17 #include "Mtftp6Impl.h"
18
19
20 EFI_DRIVER_BINDING_PROTOCOL gMtftp6DriverBinding = {
21 Mtftp6DriverBindingSupported,
22 Mtftp6DriverBindingStart,
23 Mtftp6DriverBindingStop,
24 0xa,
25 NULL,
26 NULL
27 };
28
29 EFI_SERVICE_BINDING_PROTOCOL gMtftp6ServiceBindingTemplate = {
30 Mtftp6ServiceBindingCreateChild,
31 Mtftp6ServiceBindingDestroyChild
32 };
33
34
35 /**
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.
39
40 @param[in] Service The MTFTP6 service to be destroyed.
41
42 **/
43 VOID
44 Mtftp6DestroyService (
45 IN MTFTP6_SERVICE *Service
46 )
47 {
48 //
49 // Make sure all children instances have been already destoryed.
50 //
51 ASSERT (Service->ChildrenNum == 0);
52
53 if (Service->DummyUdpIo != NULL) {
54 UdpIoFreeIo (Service->DummyUdpIo);
55 }
56
57 if (Service->Timer != NULL) {
58 gBS->CloseEvent (Service->Timer);
59 }
60
61 FreePool (Service);
62 }
63
64
65 /**
66 Create then initialize a MTFTP6 service binding instance.
67
68 @param[in] Controller The controller to install the MTFTP6 service
69 binding on.
70 @param[in] Image The driver binding image of the MTFTP6 driver.
71 @param[out] Service The variable to receive the created service
72 binding instance.
73
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.
77
78 **/
79 EFI_STATUS
80 Mtftp6CreateService (
81 IN EFI_HANDLE Controller,
82 IN EFI_HANDLE Image,
83 OUT MTFTP6_SERVICE **Service
84 )
85 {
86 MTFTP6_SERVICE *Mtftp6Srv;
87 EFI_STATUS Status;
88
89 ASSERT (Service != NULL);
90
91 *Service = NULL;
92 Mtftp6Srv = AllocateZeroPool (sizeof (MTFTP6_SERVICE));
93
94 if (Mtftp6Srv == NULL) {
95 return EFI_OUT_OF_RESOURCES;
96 }
97
98 Mtftp6Srv->Signature = MTFTP6_SERVICE_SIGNATURE;
99 Mtftp6Srv->Controller = Controller;
100 Mtftp6Srv->Image = Image;
101 Mtftp6Srv->InDestory = FALSE;
102 Mtftp6Srv->ChildrenNum = 0;
103
104 CopyMem (
105 &Mtftp6Srv->ServiceBinding,
106 &gMtftp6ServiceBindingTemplate,
107 sizeof (EFI_SERVICE_BINDING_PROTOCOL)
108 );
109
110 InitializeListHead (&Mtftp6Srv->Children);
111
112 //
113 // Create a internal timer for all instances.
114 //
115 Status = gBS->CreateEvent (
116 EVT_NOTIFY_SIGNAL | EVT_TIMER,
117 TPL_CALLBACK,
118 Mtftp6OnTimerTick,
119 Mtftp6Srv,
120 &Mtftp6Srv->Timer
121 );
122
123 if (EFI_ERROR (Status)) {
124 FreePool (Mtftp6Srv);
125 return Status;
126 }
127
128 //
129 // Create a dummy Udp6Io to build parent-child relationship between Udp6 driver
130 // and Mtftp6 driver.
131 //
132 Mtftp6Srv->DummyUdpIo = UdpIoCreateIo (
133 Controller,
134 Image,
135 Mtftp6ConfigDummyUdpIo,
136 UDP_IO_UDP6_VERSION,
137 NULL
138 );
139
140 if (Mtftp6Srv->DummyUdpIo == NULL) {
141 gBS->CloseEvent (Mtftp6Srv->Timer);
142 FreePool (Mtftp6Srv);
143 return EFI_DEVICE_ERROR;
144 }
145
146 *Service = Mtftp6Srv;
147 return EFI_SUCCESS;
148 }
149
150
151 /**
152 Destroy the MTFTP6 instance and recycle the resources.
153
154 @param[in] Instance The pointer to the MTFTP6 instance.
155
156 **/
157 VOID
158 Mtftp6DestroyInstance (
159 IN MTFTP6_INSTANCE *Instance
160 )
161 {
162 LIST_ENTRY *Entry;
163 LIST_ENTRY *Next;
164 MTFTP6_BLOCK_RANGE *Block;
165
166 if (Instance->Config != NULL) {
167 FreePool (Instance->Config);
168 }
169
170 if (Instance->Token != NULL && Instance->Token->Event != NULL) {
171 gBS->SignalEvent (Instance->Token->Event);
172 }
173
174 if (Instance->LastPacket != NULL) {
175 NetbufFree (Instance->LastPacket);
176 }
177
178 if (Instance->UdpIo!= NULL) {
179 UdpIoFreeIo (Instance->UdpIo);
180 }
181
182 if (Instance->McastUdpIo != NULL) {
183 UdpIoFreeIo (Instance->McastUdpIo);
184 }
185
186 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) {
187 Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);
188 RemoveEntryList (Entry);
189 FreePool (Block);
190 }
191
192 FreePool (Instance);
193 }
194
195
196 /**
197 Create the MTFTP6 instance and initialize it.
198
199 @param[in] Service The pointer to the MTFTP6 service.
200 @param[out] Instance The pointer to the MTFTP6 instance.
201
202 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
203 @retval EFI_SUCCESS The MTFTP6 instance is created.
204
205 **/
206 EFI_STATUS
207 Mtftp6CreateInstance (
208 IN MTFTP6_SERVICE *Service,
209 OUT MTFTP6_INSTANCE **Instance
210 )
211 {
212 MTFTP6_INSTANCE *Mtftp6Ins;
213
214 *Instance = NULL;
215 Mtftp6Ins = AllocateZeroPool (sizeof (MTFTP6_INSTANCE));
216
217 if (Mtftp6Ins == NULL) {
218 return EFI_OUT_OF_RESOURCES;
219 }
220
221 Mtftp6Ins->Signature = MTFTP6_INSTANCE_SIGNATURE;
222 Mtftp6Ins->InDestory = FALSE;
223 Mtftp6Ins->Service = Service;
224
225 CopyMem (
226 &Mtftp6Ins->Mtftp6,
227 &gMtftp6ProtocolTemplate,
228 sizeof (EFI_MTFTP6_PROTOCOL)
229 );
230
231 InitializeListHead (&Mtftp6Ins->Link);
232 InitializeListHead (&Mtftp6Ins->BlkList);
233
234 *Instance = Mtftp6Ins;
235
236 return EFI_SUCCESS;
237 }
238
239
240 /**
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.
244
245 Entry point of the MTFTP6 driver to install various protocols.
246
247 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
248 @param[in] SystemTable The pointer to the EFI System Table.
249
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.
252
253 **/
254 EFI_STATUS
255 EFIAPI
256 Mtftp6DriverEntryPoint (
257 IN EFI_HANDLE ImageHandle,
258 IN EFI_SYSTEM_TABLE *SystemTable
259 )
260 {
261 return EfiLibInstallDriverBindingComponentName2 (
262 ImageHandle,
263 SystemTable,
264 &gMtftp6DriverBinding,
265 ImageHandle,
266 &gMtftp6ComponentName,
267 &gMtftp6ComponentName2
268 );
269 }
270
271
272 /**
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.
279
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.
283 device to start.
284
285 @retval EFI_SUCCESS This driver supports this device.
286 @retval Others This driver does not support this device.
287
288 **/
289 EFI_STATUS
290 EFIAPI
291 Mtftp6DriverBindingSupported (
292 IN EFI_DRIVER_BINDING_PROTOCOL *This,
293 IN EFI_HANDLE Controller,
294 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
295 )
296 {
297 return gBS->OpenProtocol (
298 Controller,
299 &gEfiUdp6ServiceBindingProtocolGuid,
300 NULL,
301 This->DriverBindingHandle,
302 Controller,
303 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
304 );
305 }
306
307
308 /**
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.
315
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
319 device to start.
320
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.
324
325 **/
326 EFI_STATUS
327 EFIAPI
328 Mtftp6DriverBindingStart (
329 IN EFI_DRIVER_BINDING_PROTOCOL *This,
330 IN EFI_HANDLE Controller,
331 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
332 )
333 {
334 MTFTP6_SERVICE *Service;
335 EFI_STATUS Status;
336
337 //
338 // Directly return if driver is already running on this Nic handle.
339 //
340 Status = gBS->OpenProtocol (
341 Controller,
342 &gEfiMtftp6ServiceBindingProtocolGuid,
343 NULL,
344 This->DriverBindingHandle,
345 Controller,
346 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
347 );
348
349 if (!EFI_ERROR (Status)) {
350 return EFI_ALREADY_STARTED;
351 }
352
353 //
354 // Create Mtftp6 service for this Nic handle
355 //
356 Status = Mtftp6CreateService (
357 Controller,
358 This->DriverBindingHandle,
359 &Service
360 );
361
362 if (EFI_ERROR (Status)) {
363 return Status;
364 }
365
366 ASSERT (Service != NULL);
367
368 //
369 // Start the internal timer to track the packet retransmission.
370 //
371 Status = gBS->SetTimer (
372 Service->Timer,
373 TimerPeriodic,
374 TICKS_PER_SECOND
375 );
376
377 if (EFI_ERROR (Status)) {
378 goto ON_ERROR;
379 }
380
381 //
382 // Install the Mtftp6 service on the Nic handle.
383 //
384 Status = gBS->InstallMultipleProtocolInterfaces (
385 &Controller,
386 &gEfiMtftp6ServiceBindingProtocolGuid,
387 &Service->ServiceBinding,
388 NULL
389 );
390
391 if (EFI_ERROR (Status)) {
392 goto ON_ERROR;
393 }
394
395 return EFI_SUCCESS;
396
397 ON_ERROR:
398
399 Mtftp6DestroyService (Service);
400 return Status;
401 }
402
403
404 /**
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.
411
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.
417
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.
421
422 **/
423 EFI_STATUS
424 EFIAPI
425 Mtftp6DriverBindingStop (
426 IN EFI_DRIVER_BINDING_PROTOCOL *This,
427 IN EFI_HANDLE Controller,
428 IN UINTN NumberOfChildren,
429 IN EFI_HANDLE *ChildHandleBuffer
430 )
431 {
432 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
433 MTFTP6_SERVICE *Service;
434 MTFTP6_INSTANCE *Instance;
435 EFI_HANDLE NicHandle;
436 EFI_STATUS Status;
437 EFI_TPL OldTpl;
438
439 //
440 // Locate the Nic handle to retrieve the Mtftp6 private data.
441 //
442 NicHandle = NetLibGetNicHandle (Controller, &gEfiUdp6ProtocolGuid);
443
444 if (NicHandle == NULL) {
445 return EFI_DEVICE_ERROR;
446 }
447
448 Status = gBS->OpenProtocol (
449 NicHandle,
450 &gEfiMtftp6ServiceBindingProtocolGuid,
451 (VOID **) &ServiceBinding,
452 This->DriverBindingHandle,
453 NicHandle,
454 EFI_OPEN_PROTOCOL_GET_PROTOCOL
455 );
456
457 if (EFI_ERROR (Status)) {
458 return EFI_DEVICE_ERROR;
459 }
460
461 Service = MTFTP6_SERVICE_FROM_THIS (ServiceBinding);
462
463 if (Service->InDestory) {
464 return EFI_SUCCESS;
465 }
466
467 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
468
469 if (NumberOfChildren == 0) {
470 //
471 // Destory the Mtftp6 service if there is no Mtftp6 child instance left.
472 //
473 Service->InDestory = TRUE;
474
475 gBS->UninstallProtocolInterface (
476 NicHandle,
477 &gEfiMtftp6ServiceBindingProtocolGuid,
478 ServiceBinding
479 );
480
481 Mtftp6DestroyService (Service);
482
483 } else {
484 //
485 // Destory the Mtftp6 child instance one by one.
486 //
487 while (!IsListEmpty (&Service->Children)) {
488 Instance = NET_LIST_HEAD (&Service->Children, MTFTP6_INSTANCE, Link);
489 Mtftp6ServiceBindingDestroyChild (ServiceBinding, Instance->Handle);
490 }
491
492 if (Service->ChildrenNum != 0) {
493 Status = EFI_DEVICE_ERROR;
494 }
495 }
496
497 gBS->RestoreTPL (OldTpl);
498 return Status;
499 }
500
501
502 /**
503 Creates a child handle and installs a protocol.
504
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.
508
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.
513
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.
517
518 **/
519 EFI_STATUS
520 EFIAPI
521 Mtftp6ServiceBindingCreateChild (
522 IN EFI_SERVICE_BINDING_PROTOCOL *This,
523 IN OUT EFI_HANDLE *ChildHandle
524 )
525 {
526 MTFTP6_SERVICE *Service;
527 MTFTP6_INSTANCE *Instance;
528 EFI_STATUS Status;
529 EFI_TPL OldTpl;
530 VOID *Udp6;
531
532 if (This == NULL || ChildHandle == NULL) {
533 return EFI_INVALID_PARAMETER;
534 }
535
536 Service = MTFTP6_SERVICE_FROM_THIS (This);
537
538 Status = Mtftp6CreateInstance (Service, &Instance);
539
540 if (EFI_ERROR (Status)) {
541 return Status;
542 }
543
544 ASSERT (Instance != NULL);
545
546 //
547 // Install the Mtftp6 protocol on the new child handle.
548 //
549 Status = gBS->InstallMultipleProtocolInterfaces (
550 ChildHandle,
551 &gEfiMtftp6ProtocolGuid,
552 &Instance->Mtftp6,
553 NULL
554 );
555
556 if (EFI_ERROR (Status)) {
557 goto ON_ERROR;
558 }
559
560 Instance->Handle = *ChildHandle;
561
562 //
563 // Open the Udp6 protocol by child.
564 //
565 Status = gBS->OpenProtocol (
566 Service->DummyUdpIo->UdpHandle,
567 &gEfiUdp6ProtocolGuid,
568 (VOID **) &Udp6,
569 gMtftp6DriverBinding.DriverBindingHandle,
570 Instance->Handle,
571 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
572 );
573
574 if (EFI_ERROR (Status)) {
575 gBS->UninstallMultipleProtocolInterfaces (
576 Instance->Handle,
577 &gEfiMtftp6ProtocolGuid,
578 &Instance->Mtftp6,
579 NULL
580 );
581
582 goto ON_ERROR;
583 }
584
585 //
586 // Add the new Mtftp6 instance into the children list of Mtftp6 service.
587 //
588 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
589
590 InsertTailList (&Service->Children, &Instance->Link);
591 Service->ChildrenNum++;
592
593 gBS->RestoreTPL (OldTpl);
594 return EFI_SUCCESS;
595
596 ON_ERROR:
597
598 Mtftp6DestroyInstance (Instance);
599 return Status;
600 }
601
602
603 /**
604 Destroys a child handle with a protocol installed on it.
605
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.
609
610 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
611 @param[in] ChildHandle Handle of the child to destroy.
612
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
617
618 **/
619 EFI_STATUS
620 EFIAPI
621 Mtftp6ServiceBindingDestroyChild (
622 IN EFI_SERVICE_BINDING_PROTOCOL *This,
623 IN EFI_HANDLE ChildHandle
624 )
625 {
626 MTFTP6_SERVICE *Service;
627 MTFTP6_INSTANCE *Instance;
628 EFI_MTFTP6_PROTOCOL *Mtftp6;
629 EFI_STATUS Status;
630 EFI_TPL OldTpl;
631
632 if (This == NULL || ChildHandle == NULL) {
633 return EFI_INVALID_PARAMETER;
634 }
635
636 //
637 // Locate the Nic handle to retrieve the Mtftp6 private data.
638 //
639 Status = gBS->OpenProtocol (
640 ChildHandle,
641 &gEfiMtftp6ProtocolGuid,
642 (VOID **) &Mtftp6,
643 gMtftp6DriverBinding.DriverBindingHandle,
644 ChildHandle,
645 EFI_OPEN_PROTOCOL_GET_PROTOCOL
646 );
647
648 if (EFI_ERROR (Status)) {
649 return EFI_UNSUPPORTED;
650 }
651
652 Instance = MTFTP6_INSTANCE_FROM_THIS (Mtftp6);
653 Service = MTFTP6_SERVICE_FROM_THIS (This);
654
655 if (Instance->Service != Service) {
656 return EFI_INVALID_PARAMETER;
657 }
658
659 //
660 // Check whether the instance already in destory state.
661 //
662 if (Instance->InDestory) {
663 return EFI_SUCCESS;
664 }
665
666 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
667
668 Instance->InDestory = TRUE;
669
670 gBS->CloseProtocol (
671 Service->DummyUdpIo->UdpHandle,
672 &gEfiUdp6ProtocolGuid,
673 gMtftp6DriverBinding.DriverBindingHandle,
674 ChildHandle
675 );
676
677 //
678 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
679 //
680 Status = gBS->UninstallProtocolInterface (
681 ChildHandle,
682 &gEfiMtftp6ProtocolGuid,
683 Mtftp6
684 );
685
686 if (EFI_ERROR (Status)) {
687 Instance->InDestory = FALSE;
688 gBS->RestoreTPL (OldTpl);
689 return Status;
690 }
691
692 //
693 // Remove the Mtftp6 instance from the children list of Mtftp6 service.
694 //
695 RemoveEntryList (&Instance->Link);
696 Service->ChildrenNum --;
697
698 Mtftp6DestroyInstance (Instance);
699
700 gBS->RestoreTPL (OldTpl);
701
702 return EFI_SUCCESS;
703 }