]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Support.c
1 /** @file
2 Mtftp6 support functions implementation.
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 "Mtftp6Impl.h"
17
18
19 /**
20 Allocate a MTFTP block range, then init it to the range of [Start, End].
21
22 @param[in] Start The start block number.
23 @param[in] End The last block number in the range.
24
25 @return Range The range of the allocated block buffer.
26
27 **/
28 MTFTP6_BLOCK_RANGE *
29 Mtftp6AllocateRange (
30 IN UINT16 Start,
31 IN UINT16 End
32 )
33 {
34 MTFTP6_BLOCK_RANGE *Range;
35
36 Range = AllocateZeroPool (sizeof (MTFTP6_BLOCK_RANGE));
37
38 if (Range == NULL) {
39 return NULL;
40 }
41
42 InitializeListHead (&Range->Link);
43 Range->Start = Start;
44 Range->End = End;
45 Range->Bound = End;
46
47 return Range;
48 }
49
50
51 /**
52 Initialize the block range for either RRQ or WRQ. RRQ and WRQ have
53 different requirements for Start and End. For example, during startup,
54 WRQ initializes its whole valid block range to [0, 0xffff]. This
55 is bacause the server will send an ACK0 to inform the user to start the
56 upload. When the client receives an ACK0, it will remove 0 from the range,
57 get the next block number, which is 1, then upload the BLOCK1. For RRQ
58 without option negotiation, the server will directly send the BLOCK1
59 in response to the client's RRQ. When received BLOCK1, the client will
60 remove it from the block range and send an ACK. It also works if there
61 is option negotiation.
62
63 @param[in] Head The block range head to initialize.
64 @param[in] Start The Start block number.
65 @param[in] End The last block number.
66
67 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range.
68 @retval EFI_SUCCESS The initial block range is created.
69
70 **/
71 EFI_STATUS
72 Mtftp6InitBlockRange (
73 IN LIST_ENTRY *Head,
74 IN UINT16 Start,
75 IN UINT16 End
76 )
77 {
78 MTFTP6_BLOCK_RANGE *Range;
79
80 Range = Mtftp6AllocateRange (Start, End);
81
82 if (Range == NULL) {
83 return EFI_OUT_OF_RESOURCES;
84 }
85
86 InsertTailList (Head, &Range->Link);
87 return EFI_SUCCESS;
88 }
89
90
91 /**
92 Get the first valid block number on the range list.
93
94 @param[in] Head The block range head.
95
96 @retval ==-1 If the block range is empty.
97 @retval >-1 The first valid block number.
98
99 **/
100 INTN
101 Mtftp6GetNextBlockNum (
102 IN LIST_ENTRY *Head
103 )
104 {
105 MTFTP6_BLOCK_RANGE *Range;
106
107 if (IsListEmpty (Head)) {
108 return -1;
109 }
110
111 Range = NET_LIST_HEAD (Head, MTFTP6_BLOCK_RANGE, Link);
112 return Range->Start;
113 }
114
115
116 /**
117 Set the last block number of the block range list. It
118 removes all the blocks after the Last. MTFTP initialize the
119 block range to the maximum possible range, such as [0, 0xffff]
120 for WRQ. When it gets the last block number, it calls
121 this function to set the last block number.
122
123 @param[in] Head The block range list.
124 @param[in] Last The last block number.
125
126 **/
127 VOID
128 Mtftp6SetLastBlockNum (
129 IN LIST_ENTRY *Head,
130 IN UINT16 Last
131 )
132 {
133 MTFTP6_BLOCK_RANGE *Range;
134
135 //
136 // Iterate from the tail to head to remove the block number
137 // after the last.
138 //
139 while (!IsListEmpty (Head)) {
140 Range = NET_LIST_TAIL (Head, MTFTP6_BLOCK_RANGE, Link);
141
142 if (Range->Start > Last) {
143 RemoveEntryList (&Range->Link);
144 FreePool (Range);
145 continue;
146 }
147
148 if (Range->End > Last) {
149 Range->End = Last;
150 }
151 return ;
152 }
153 }
154
155
156 /**
157 Remove the block number from the block range list.
158
159 @param[in] Head The block range list to remove from.
160 @param[in] Num The block number to remove.
161 @param[in] Completed Whether Num is the last block number
162 @param[out] TotalBlock The continuous block number in all
163
164 @retval EFI_NOT_FOUND The block number isn't in the block range list.
165 @retval EFI_SUCCESS The block number has been removed from the list.
166 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
167
168 **/
169 EFI_STATUS
170 Mtftp6RemoveBlockNum (
171 IN LIST_ENTRY *Head,
172 IN UINT16 Num,
173 IN BOOLEAN Completed,
174 OUT UINT64 *TotalBlock
175 )
176 {
177 MTFTP6_BLOCK_RANGE *Range;
178 MTFTP6_BLOCK_RANGE *NewRange;
179 LIST_ENTRY *Entry;
180
181 NET_LIST_FOR_EACH (Entry, Head) {
182
183 //
184 // Each block represents a hole [Start, End] in the file,
185 // skip to the first range with End >= Num
186 //
187 Range = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);
188
189 if (Range->End < Num) {
190 continue;
191 }
192
193 //
194 // There are three different cases for Start
195 // 1. (Start > Num) && (End >= Num):
196 // because all the holes before this one has the condition of
197 // End < Num, so this block number has been removed.
198 //
199 // 2. (Start == Num) && (End >= Num):
200 // Need to increase the Start by one, and if End == Num, this
201 // hole has been removed completely, remove it.
202 //
203 // 3. (Start < Num) && (End >= Num):
204 // if End == Num, only need to decrease the End by one because
205 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
206 // if (End > Num), the hold is splited into two holes, with
207 // [Start, Num - 1] and [Num + 1, End].
208 //
209 if (Range->Start > Num) {
210 return EFI_NOT_FOUND;
211
212 } else if (Range->Start == Num) {
213 Range->Start++;
214
215 //
216 // Note that: RFC 1350 does not mention block counter roll-over,
217 // but several TFTP hosts implement the roll-over be able to accept
218 // transfers of unlimited size. There is no consensus, however, whether
219 // the counter should wrap around to zero or to one. Many implementations
220 // wrap to zero, because this is the simplest to implement. Here we choose
221 // this solution.
222 //
223 *TotalBlock = Num;
224
225 if (Range->Round > 0) {
226 *TotalBlock += Range->Bound + MultU64x32 ((UINT64) (Range->Round -1), (UINT32)(Range->Bound + 1)) + 1;
227 }
228
229 if (Range->Start > Range->Bound) {
230 Range->Start = 0;
231 Range->Round ++;
232 }
233
234 if ((Range->Start > Range->End) || Completed) {
235 RemoveEntryList (&Range->Link);
236 FreePool (Range);
237 }
238
239 return EFI_SUCCESS;
240
241 } else {
242 if (Range->End == Num) {
243 Range->End--;
244 } else {
245 NewRange = Mtftp6AllocateRange ((UINT16) (Num + 1), (UINT16) Range->End);
246
247 if (NewRange == NULL) {
248 return EFI_OUT_OF_RESOURCES;
249 }
250
251 Range->End = Num - 1;
252 NetListInsertAfter (&Range->Link, &NewRange->Link);
253 }
254
255 return EFI_SUCCESS;
256 }
257 }
258
259 return EFI_NOT_FOUND;
260 }
261
262
263 /**
264 Configure the opened Udp6 instance until the corresponding Ip6 instance
265 has been configured.
266
267 @param[in] UdpIo The pointer to the Udp6 Io.
268 @param[in] UdpCfgData The pointer to the Udp6 configure data.
269
270 @retval EFI_SUCCESS Configure the Udp6 instance successfully.
271 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not
272 been configured yet.
273
274 **/
275 EFI_STATUS
276 Mtftp6GetMapping (
277 IN UDP_IO *UdpIo,
278 IN EFI_UDP6_CONFIG_DATA *UdpCfgData
279 )
280 {
281 EFI_IP6_MODE_DATA Ip6Mode;
282 EFI_UDP6_PROTOCOL *Udp6;
283 EFI_STATUS Status;
284 EFI_EVENT Event;
285
286 Event = NULL;
287 Udp6 = UdpIo->Protocol.Udp6;
288
289 //
290 // Create a timer to check whether the Ip6 instance configured or not.
291 //
292 Status = gBS->CreateEvent (
293 EVT_TIMER,
294 TPL_CALLBACK,
295 NULL,
296 NULL,
297 &Event
298 );
299 if (EFI_ERROR (Status)) {
300 goto ON_EXIT;
301 }
302
303 Status = gBS->SetTimer (
304 Event,
305 TimerRelative,
306 MTFTP6_GET_MAPPING_TIMEOUT * MTFTP6_TICK_PER_SECOND
307 );
308 if (EFI_ERROR (Status)) {
309 goto ON_EXIT;
310 }
311
312 //
313 // Check the Ip6 mode data till timeout.
314 //
315 while (EFI_ERROR (gBS->CheckEvent (Event))) {
316
317 Udp6->Poll (Udp6);
318
319 Status = Udp6->GetModeData (Udp6, NULL, &Ip6Mode, NULL, NULL);
320
321 if (!EFI_ERROR (Status)) {
322
323 if (Ip6Mode.IsConfigured) {
324 //
325 // Continue to configure the Udp6 instance.
326 //
327 Status = Udp6->Configure (Udp6, UdpCfgData);
328 } else {
329 Status = EFI_NO_MAPPING;
330 }
331 }
332 }
333
334 ON_EXIT:
335
336 if (Event != NULL) {
337 gBS->CloseEvent (Event);
338 }
339
340 return Status;
341 }
342
343
344 /**
345 The dummy configure routine for create a new Udp6 Io.
346
347 @param[in] UdpIo The pointer to the Udp6 Io.
348 @param[in] Context The pointer to the context.
349
350 @retval EFI_SUCCESS This value is always returned.
351
352 **/
353 EFI_STATUS
354 EFIAPI
355 Mtftp6ConfigDummyUdpIo (
356 IN UDP_IO *UdpIo,
357 IN VOID *Context
358 )
359 {
360 return EFI_SUCCESS;
361 }
362
363
364 /**
365 The configure routine for Mtftp6 instance to transmit/receive.
366
367 @param[in] UdpIo The pointer to the Udp6 Io.
368 @param[in] ServerIp The pointer to the server address.
369 @param[in] ServerPort The pointer to the server port.
370 @param[in] LocalIp The pointer to the local address.
371 @param[in] LocalPort The pointer to the local port.
372
373 @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.
374 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been
375 configured yet.
376
377 **/
378 EFI_STATUS
379 Mtftp6ConfigUdpIo (
380 IN UDP_IO *UdpIo,
381 IN EFI_IPv6_ADDRESS *ServerIp,
382 IN UINT16 ServerPort,
383 IN EFI_IPv6_ADDRESS *LocalIp,
384 IN UINT16 LocalPort
385 )
386 {
387 EFI_STATUS Status;
388 EFI_UDP6_PROTOCOL *Udp6;
389 EFI_UDP6_CONFIG_DATA *Udp6Cfg;
390
391 Udp6 = UdpIo->Protocol.Udp6;
392 Udp6Cfg = &(UdpIo->Config.Udp6);
393
394 ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA));
395
396 //
397 // Set the Udp6 Io configure data.
398 //
399 Udp6Cfg->AcceptPromiscuous = FALSE;
400 Udp6Cfg->AcceptAnyPort = FALSE;
401 Udp6Cfg->AllowDuplicatePort = FALSE;
402 Udp6Cfg->TrafficClass = 0;
403 Udp6Cfg->HopLimit = 128;
404 Udp6Cfg->ReceiveTimeout = 0;
405 Udp6Cfg->TransmitTimeout = 0;
406 Udp6Cfg->StationPort = LocalPort;
407 Udp6Cfg->RemotePort = ServerPort;
408
409 CopyMem (
410 &Udp6Cfg->StationAddress,
411 LocalIp,
412 sizeof (EFI_IPv6_ADDRESS)
413 );
414
415 CopyMem (
416 &Udp6Cfg->RemoteAddress,
417 ServerIp,
418 sizeof (EFI_IPv6_ADDRESS)
419 );
420
421 //
422 // Configure the Udp6 instance with current configure data.
423 //
424 Status = Udp6->Configure (Udp6, Udp6Cfg);
425
426 if (Status == EFI_NO_MAPPING) {
427
428 return Mtftp6GetMapping (UdpIo, Udp6Cfg);
429 }
430
431 return Status;
432 }
433
434
435 /**
436 Build and transmit the request packet for the Mtftp6 instance.
437
438 @param[in] Instance The pointer to the Mtftp6 instance.
439 @param[in] Operation The operation code of this packet.
440
441 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request.
442 @retval EFI_SUCCESS The request is built and sent.
443 @retval Others Failed to transmit the packet.
444
445 **/
446 EFI_STATUS
447 Mtftp6SendRequest (
448 IN MTFTP6_INSTANCE *Instance,
449 IN UINT16 Operation
450 )
451 {
452 EFI_MTFTP6_PACKET *Packet;
453 EFI_MTFTP6_OPTION *Options;
454 EFI_MTFTP6_TOKEN *Token;
455 NET_BUF *Nbuf;
456 UINT8 *Mode;
457 UINT8 *Cur;
458 UINT32 Len1;
459 UINT32 Len2;
460 UINT32 Len;
461 UINTN Index;
462
463 Token = Instance->Token;
464 Options = Token->OptionList;
465 Mode = Token->ModeStr;
466
467 if (Mode == NULL) {
468 Mode = (UINT8 *) "octet";
469 }
470
471 //
472 // The header format of RRQ/WRQ packet is:
473 //
474 // 2 bytes string 1 byte string 1 byte
475 // ------------------------------------------------
476 // | Opcode | Filename | 0 | Mode | 0 |
477 // ------------------------------------------------
478 //
479 // The common option format is:
480 //
481 // string 1 byte string 1 byte
482 // ---------------------------------------
483 // | OptionStr | 0 | ValueStr | 0 |
484 // ---------------------------------------
485 //
486
487 //
488 // Compute the size of new Mtftp6 packet.
489 //
490 Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Token->Filename);
491 Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Mode);
492 Len = Len1 + Len2 + 4;
493
494 for (Index = 0; Index < Token->OptionCount; Index++) {
495 Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
496 Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
497 Len += Len1 + Len2 + 2;
498 }
499
500 //
501 // Allocate a packet then copy the data.
502 //
503 if ((Nbuf = NetbufAlloc (Len)) == NULL) {
504 return EFI_OUT_OF_RESOURCES;
505 }
506
507 //
508 // Copy the opcode, filename and mode into packet.
509 //
510 Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
511 ASSERT (Packet != NULL);
512
513 Packet->OpCode = HTONS (Operation);
514 Cur = Packet->Rrq.Filename;
515 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Token->Filename);
516 Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;
517 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Mode);
518 Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;
519
520 //
521 // Copy all the extension options into the packet.
522 //
523 for (Index = 0; Index < Token->OptionCount; ++Index) {
524 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].OptionStr);
525 Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;
526 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].ValueStr);
527 Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;
528 }
529
530 //
531 // Save the packet buf for retransmit
532 //
533 if (Instance->LastPacket != NULL) {
534 NetbufFree (Instance->LastPacket);
535 }
536
537 Instance->LastPacket = Nbuf;
538 Instance->CurRetry = 0;
539
540 return Mtftp6TransmitPacket (Instance, Nbuf);
541 }
542
543
544 /**
545 Build and send an error packet.
546
547 @param[in] Instance The pointer to the Mtftp6 instance.
548 @param[in] ErrCode The error code in the packet.
549 @param[in] ErrInfo The error message in the packet.
550
551 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet.
552 @retval EFI_SUCCESS The error packet is transmitted.
553 @retval Others Failed to transmit the packet.
554
555 **/
556 EFI_STATUS
557 Mtftp6SendError (
558 IN MTFTP6_INSTANCE *Instance,
559 IN UINT16 ErrCode,
560 IN UINT8* ErrInfo
561 )
562 {
563 NET_BUF *Nbuf;
564 EFI_MTFTP6_PACKET *TftpError;
565 UINT32 Len;
566
567 //
568 // Allocate a packet then copy the data.
569 //
570 Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP6_ERROR_HEADER));
571 Nbuf = NetbufAlloc (Len);
572
573 if (Nbuf == NULL) {
574 return EFI_OUT_OF_RESOURCES;
575 }
576
577 TftpError = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
578
579 if (TftpError == NULL) {
580 NetbufFree (Nbuf);
581 return EFI_OUT_OF_RESOURCES;
582 }
583
584 TftpError->OpCode = HTONS (EFI_MTFTP6_OPCODE_ERROR);
585 TftpError->Error.ErrorCode = HTONS (ErrCode);
586
587 AsciiStrCpy ((CHAR8 *) TftpError->Error.ErrorMessage, (CHAR8 *) ErrInfo);
588
589 //
590 // Save the packet buf for retransmit
591 //
592 if (Instance->LastPacket != NULL) {
593 NetbufFree (Instance->LastPacket);
594 }
595
596 Instance->LastPacket = Nbuf;
597 Instance->CurRetry = 0;
598
599 return Mtftp6TransmitPacket (Instance, Nbuf);
600 }
601
602
603 /**
604 The callback function called when the packet is transmitted.
605
606 @param[in] Packet The pointer to the packet.
607 @param[in] UdpEpt The pointer to the Udp6 access point.
608 @param[in] IoStatus The result of the transmission.
609 @param[in] Context The pointer to the context.
610
611 **/
612 VOID
613 EFIAPI
614 Mtftp6OnPacketSent (
615 IN NET_BUF *Packet,
616 IN UDP_END_POINT *UdpEpt,
617 IN EFI_STATUS IoStatus,
618 IN VOID *Context
619 )
620 {
621 NetbufFree (Packet);
622 *(BOOLEAN *) Context = TRUE;
623 }
624
625
626 /**
627 Send the packet for the Mtftp6 instance.
628
629 @param[in] Instance The pointer to the Mtftp6 instance.
630 @param[in] Packet The pointer to the packet to be sent.
631
632 @retval EFI_SUCCESS The packet was sent out
633 @retval Others Failed to transmit the packet.
634
635 **/
636 EFI_STATUS
637 Mtftp6TransmitPacket (
638 IN MTFTP6_INSTANCE *Instance,
639 IN NET_BUF *Packet
640 )
641 {
642 EFI_UDP6_PROTOCOL *Udp6;
643 EFI_UDP6_CONFIG_DATA Udp6CfgData;
644 EFI_STATUS Status;
645 UINT16 *Temp;
646 UINT16 Value;
647 UINT16 OpCode;
648
649 ZeroMem (&Udp6CfgData, sizeof(EFI_UDP6_CONFIG_DATA));
650 Udp6 = Instance->UdpIo->Protocol.Udp6;
651
652 //
653 // Set the live time of the packet.
654 //
655 Instance->PacketToLive = Instance->IsMaster ? Instance->Timeout : (Instance->Timeout * 2);
656
657 Temp = (UINT16 *) NetbufGetByte (Packet, 0, NULL);
658 ASSERT (Temp != NULL);
659
660 Value = *Temp;
661 OpCode = NTOHS (Value);
662
663 if (OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR || OpCode == EFI_MTFTP6_OPCODE_WRQ) {
664 //
665 // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as
666 // (serverip, 69, localip, localport) to send.
667 // Usually local address and local port are both default as zero.
668 //
669 Status = Udp6->Configure (Udp6, NULL);
670
671 if (EFI_ERROR (Status)) {
672 return Status;
673 }
674
675 Status = Mtftp6ConfigUdpIo (
676 Instance->UdpIo,
677 &Instance->ServerIp,
678 Instance->ServerCmdPort,
679 &Instance->Config->StationIp,
680 Instance->Config->LocalPort
681 );
682
683 if (EFI_ERROR (Status)) {
684 return Status;
685 }
686
687 //
688 // Get the current local address and port by get Udp6 mode data.
689 //
690 Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);
691 if (EFI_ERROR (Status)) {
692 return Status;
693 }
694
695 NET_GET_REF (Packet);
696
697 Instance->IsTransmitted = FALSE;
698
699 Status = UdpIoSendDatagram (
700 Instance->UdpIo,
701 Packet,
702 NULL,
703 NULL,
704 Mtftp6OnPacketSent,
705 &Instance->IsTransmitted
706 );
707
708 if (EFI_ERROR (Status)) {
709 NET_PUT_REF (Packet);
710 return Status;
711 }
712
713 //
714 // Poll till the packet sent out from the ip6 queue.
715 //
716 gBS->RestoreTPL (Instance->OldTpl);
717
718 while (!Instance->IsTransmitted) {
719 Udp6->Poll (Udp6);
720 }
721
722 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
723
724 //
725 // For the subsequent exchange of such requests, reconfigure the Udp6Io as
726 // (serverip, 0, localip, localport) to receive.
727 // Currently local address and local port are specified by Udp6 mode data.
728 //
729 Status = Udp6->Configure (Udp6, NULL);
730
731 if (EFI_ERROR (Status)) {
732 return Status;
733 }
734
735 Status = Mtftp6ConfigUdpIo (
736 Instance->UdpIo,
737 &Instance->ServerIp,
738 Instance->ServerDataPort,
739 &Udp6CfgData.StationAddress,
740 Udp6CfgData.StationPort
741 );
742 } else {
743 //
744 // For the data exchange, configure the Udp6Io as (serverip, dataport,
745 // localip, localport) to send/receive.
746 // Currently local address and local port are specified by Udp6 mode data.
747 //
748 Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);
749 if (EFI_ERROR (Status)) {
750 return Status;
751 }
752
753 if (Udp6CfgData.RemotePort != Instance->ServerDataPort) {
754
755 Status = Udp6->Configure (Udp6, NULL);
756
757 if (EFI_ERROR (Status)) {
758 return Status;
759 }
760
761 Status = Mtftp6ConfigUdpIo (
762 Instance->UdpIo,
763 &Instance->ServerIp,
764 Instance->ServerDataPort,
765 &Udp6CfgData.StationAddress,
766 Udp6CfgData.StationPort
767 );
768
769 if (EFI_ERROR (Status)) {
770 return Status;
771 }
772 }
773
774 NET_GET_REF (Packet);
775
776 Instance->IsTransmitted = FALSE;
777
778 Status = UdpIoSendDatagram (
779 Instance->UdpIo,
780 Packet,
781 NULL,
782 NULL,
783 Mtftp6OnPacketSent,
784 &Instance->IsTransmitted
785 );
786
787 if (EFI_ERROR (Status)) {
788 NET_PUT_REF (Packet);
789 }
790
791 //
792 // Poll till the packet sent out from the ip6 queue.
793 //
794 gBS->RestoreTPL (Instance->OldTpl);
795
796 while (!Instance->IsTransmitted) {
797 Udp6->Poll (Udp6);
798 }
799
800 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
801 }
802
803 return Status;
804 }
805
806
807 /**
808 Check packet for GetInfo callback routine.
809
810 GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect
811 the first packet from server, then abort the session.
812
813 @param[in] This The pointer to the Mtftp6 protocol.
814 @param[in] Token The pointer to the Mtftp6 token.
815 @param[in] PacketLen The length of the packet.
816 @param[in] Packet The pointer to the received packet.
817
818 @retval EFI_ABORTED Abort the Mtftp6 operation.
819
820 **/
821 EFI_STATUS
822 EFIAPI
823 Mtftp6CheckPacket (
824 IN EFI_MTFTP6_PROTOCOL *This,
825 IN EFI_MTFTP6_TOKEN *Token,
826 IN UINT16 PacketLen,
827 IN EFI_MTFTP6_PACKET *Packet
828 )
829 {
830 MTFTP6_GETINFO_CONTEXT *Context;
831 UINT16 OpCode;
832
833 Context = (MTFTP6_GETINFO_CONTEXT *) Token->Context;
834 OpCode = NTOHS (Packet->OpCode);
835
836 //
837 // Set the GetInfo's return status according to the OpCode.
838 //
839 switch (OpCode) {
840 case EFI_MTFTP6_OPCODE_ERROR:
841 Context->Status = EFI_TFTP_ERROR;
842 break;
843
844 case EFI_MTFTP6_OPCODE_OACK:
845 Context->Status = EFI_SUCCESS;
846 break;
847
848 default:
849 Context->Status = EFI_PROTOCOL_ERROR;
850 }
851
852 //
853 // Allocate buffer then copy the packet over. Use gBS->AllocatePool
854 // in case NetAllocatePool will implements something tricky.
855 //
856 *(Context->Packet) = AllocateZeroPool (PacketLen);
857
858 if (*(Context->Packet) == NULL) {
859 Context->Status = EFI_OUT_OF_RESOURCES;
860 return EFI_ABORTED;
861 }
862
863 *(Context->PacketLen) = PacketLen;
864 CopyMem (*(Context->Packet), Packet, PacketLen);
865
866 return EFI_ABORTED;
867 }
868
869
870 /**
871 Clean up the current Mtftp6 operation.
872
873 @param[in] Instance The pointer to the Mtftp6 instance.
874 @param[in] Result The result to be returned to the user.
875
876 **/
877 VOID
878 Mtftp6OperationClean (
879 IN MTFTP6_INSTANCE *Instance,
880 IN EFI_STATUS Result
881 )
882 {
883 LIST_ENTRY *Entry;
884 LIST_ENTRY *Next;
885 MTFTP6_BLOCK_RANGE *Block;
886
887 //
888 // Clean up the current token and event.
889 //
890 if (Instance->Token != NULL) {
891 Instance->Token->Status = Result;
892 if (Instance->Token->Event != NULL) {
893 gBS->SignalEvent (Instance->Token->Event);
894 }
895 Instance->Token = NULL;
896 }
897
898 //
899 // Clean up the corresponding Udp6Io.
900 //
901 if (Instance->UdpIo != NULL) {
902 UdpIoCleanIo (Instance->UdpIo);
903 }
904
905 if (Instance->McastUdpIo != NULL) {
906 gBS->CloseProtocol (
907 Instance->McastUdpIo->UdpHandle,
908 &gEfiUdp6ProtocolGuid,
909 Instance->McastUdpIo->Image,
910 Instance->Handle
911 );
912 UdpIoFreeIo (Instance->McastUdpIo);
913 Instance->McastUdpIo = NULL;
914 }
915
916 //
917 // Clean up the stored last packet.
918 //
919 if (Instance->LastPacket != NULL) {
920 NetbufFree (Instance->LastPacket);
921 Instance->LastPacket = NULL;
922 }
923
924 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) {
925 Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);
926 RemoveEntryList (Entry);
927 FreePool (Block);
928 }
929
930 //
931 // Reinitialize the corresponding fields of the Mtftp6 operation.
932 //
933 ZeroMem (&Instance->ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));
934 ZeroMem (&Instance->ServerIp, sizeof (EFI_IPv6_ADDRESS));
935 ZeroMem (&Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));
936
937 Instance->ServerCmdPort = 0;
938 Instance->ServerDataPort = 0;
939 Instance->McastPort = 0;
940 Instance->BlkSize = 0;
941 Instance->LastBlk = 0;
942 Instance->PacketToLive = 0;
943 Instance->MaxRetry = 0;
944 Instance->CurRetry = 0;
945 Instance->Timeout = 0;
946 Instance->IsMaster = TRUE;
947 }
948
949
950 /**
951 Start the Mtftp6 instance to perform the operation, such as read file,
952 write file, and read directory.
953
954 @param[in] This The MTFTP session.
955 @param[in] Token The token than encapsues the user's request.
956 @param[in] OpCode The operation to perform.
957
958 @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.
959 @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.
960 @retval EFI_ALREADY_STARTED There is pending operation for the session.
961 @retval EFI_SUCCESS The operation is successfully started.
962
963 **/
964 EFI_STATUS
965 Mtftp6OperationStart (
966 IN EFI_MTFTP6_PROTOCOL *This,
967 IN EFI_MTFTP6_TOKEN *Token,
968 IN UINT16 OpCode
969 )
970 {
971 MTFTP6_INSTANCE *Instance;
972 EFI_STATUS Status;
973
974 if (This == NULL ||
975 Token == NULL ||
976 Token->Filename == NULL ||
977 (Token->OptionCount != 0 && Token->OptionList == NULL) ||
978 (Token->OverrideData != NULL && !NetIp6IsValidUnicast (&Token->OverrideData->ServerIp))
979 ) {
980 return EFI_INVALID_PARAMETER;
981 }
982
983 //
984 // At least define one method to collect the data for download.
985 //
986 if ((OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR) &&
987 Token->Buffer == NULL &&
988 Token->CheckPacket == NULL
989 ) {
990 return EFI_INVALID_PARAMETER;
991 }
992
993 //
994 // At least define one method to provide the data for upload.
995 //
996 if (OpCode == EFI_MTFTP6_OPCODE_WRQ && Token->Buffer == NULL && Token->PacketNeeded == NULL) {
997 return EFI_INVALID_PARAMETER;
998 }
999
1000 Instance = MTFTP6_INSTANCE_FROM_THIS (This);
1001
1002 if (Instance->Config == NULL) {
1003 return EFI_NOT_STARTED;
1004 }
1005
1006 if (Instance->Token != NULL) {
1007 return EFI_ACCESS_DENIED;
1008 }
1009
1010 Status = EFI_SUCCESS;
1011 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1012
1013 //
1014 // Parse the extension options in the request packet.
1015 //
1016 if (Token->OptionCount != 0) {
1017
1018 Status = Mtftp6ParseExtensionOption (
1019 Token->OptionList,
1020 Token->OptionCount,
1021 TRUE,
1022 &Instance->ExtInfo
1023 );
1024
1025 if (EFI_ERROR (Status)) {
1026 goto ON_ERROR;
1027 }
1028 }
1029
1030 //
1031 // Initialize runtime data from config data or override data.
1032 //
1033 Instance->Token = Token;
1034 Instance->ServerCmdPort = Instance->Config->InitialServerPort;
1035 Instance->ServerDataPort = 0;
1036 Instance->MaxRetry = Instance->Config->TryCount;
1037 Instance->Timeout = Instance->Config->TimeoutValue;
1038 Instance->IsMaster = TRUE;
1039
1040 CopyMem (
1041 &Instance->ServerIp,
1042 &Instance->Config->ServerIp,
1043 sizeof (EFI_IPv6_ADDRESS)
1044 );
1045
1046 if (Token->OverrideData != NULL) {
1047 Instance->ServerCmdPort = Token->OverrideData->ServerPort;
1048 Instance->MaxRetry = Token->OverrideData->TryCount;
1049 Instance->Timeout = Token->OverrideData->TimeoutValue;
1050
1051 CopyMem (
1052 &Instance->ServerIp,
1053 &Token->OverrideData->ServerIp,
1054 sizeof (EFI_IPv6_ADDRESS)
1055 );
1056 }
1057
1058 //
1059 // Set default value for undefined parameters.
1060 //
1061 if (Instance->ServerCmdPort == 0) {
1062 Instance->ServerCmdPort = MTFTP6_DEFAULT_SERVER_CMD_PORT;
1063 }
1064 if (Instance->BlkSize == 0) {
1065 Instance->BlkSize = MTFTP6_DEFAULT_BLK_SIZE;
1066 }
1067 if (Instance->MaxRetry == 0) {
1068 Instance->MaxRetry = MTFTP6_DEFAULT_MAX_RETRY;
1069 }
1070 if (Instance->Timeout == 0) {
1071 Instance->Timeout = MTFTP6_DEFAULT_TIMEOUT;
1072 }
1073
1074 Token->Status = EFI_NOT_READY;
1075
1076 //
1077 // Switch the routines by the operation code.
1078 //
1079 switch (OpCode) {
1080 case EFI_MTFTP6_OPCODE_RRQ:
1081 Status = Mtftp6RrqStart (Instance, OpCode);
1082 break;
1083
1084 case EFI_MTFTP6_OPCODE_DIR:
1085 Status = Mtftp6RrqStart (Instance, OpCode);
1086 break;
1087
1088 case EFI_MTFTP6_OPCODE_WRQ:
1089 Status = Mtftp6WrqStart (Instance, OpCode);
1090 break;
1091
1092 default:
1093 Status = EFI_DEVICE_ERROR;
1094 goto ON_ERROR;
1095 }
1096
1097 if (EFI_ERROR (Status)) {
1098 goto ON_ERROR;
1099 }
1100
1101 //
1102 // Return immediately for asynchronous or poll the instance for synchronous.
1103 //
1104 gBS->RestoreTPL (Instance->OldTpl);
1105
1106 if (Token->Event == NULL) {
1107 while (Token->Status == EFI_NOT_READY) {
1108 This->Poll (This);
1109 }
1110 return Token->Status;
1111 }
1112
1113 return EFI_SUCCESS;
1114
1115 ON_ERROR:
1116
1117 Mtftp6OperationClean (Instance, Status);
1118 gBS->RestoreTPL (Instance->OldTpl);
1119
1120 return Status;
1121 }
1122
1123
1124 /**
1125 The timer ticking routine for the Mtftp6 instance.
1126
1127 @param[in] Event The pointer to the ticking event.
1128 @param[in] Context The pointer to the context.
1129
1130 **/
1131 VOID
1132 EFIAPI
1133 Mtftp6OnTimerTick (
1134 IN EFI_EVENT Event,
1135 IN VOID *Context
1136 )
1137 {
1138 MTFTP6_SERVICE *Service;
1139 MTFTP6_INSTANCE *Instance;
1140 LIST_ENTRY *Entry;
1141 LIST_ENTRY *Next;
1142 EFI_MTFTP6_TOKEN *Token;
1143 EFI_STATUS Status;
1144
1145 Service = (MTFTP6_SERVICE *) Context;
1146
1147 //
1148 // Iterate through all the children of the Mtftp service instance. Time
1149 // out the packet. If maximum retries reached, clean the session up.
1150 //
1151 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Children) {
1152
1153 Instance = NET_LIST_USER_STRUCT (Entry, MTFTP6_INSTANCE, Link);
1154
1155 if (Instance->Token == NULL) {
1156 continue;
1157 }
1158
1159 if (Instance->PacketToLive > 0) {
1160 Instance->PacketToLive--;
1161 continue;
1162 }
1163
1164 Instance->CurRetry++;
1165 Token = Instance->Token;
1166
1167 if (Token->TimeoutCallback != NULL) {
1168 //
1169 // Call the timeout callback routine if has.
1170 //
1171 Status = Token->TimeoutCallback (&Instance->Mtftp6, Token);
1172
1173 if (EFI_ERROR (Status)) {
1174 Mtftp6SendError (
1175 Instance,
1176 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,
1177 (UINT8 *) "User aborted the transfer in time out"
1178 );
1179 Mtftp6OperationClean (Instance, EFI_ABORTED);
1180 continue;
1181 }
1182 }
1183
1184 //
1185 // Retransmit the packet if haven't reach the maxmium retry count,
1186 // otherwise exit the transfer.
1187 //
1188 if (Instance->CurRetry < Instance->MaxRetry) {
1189 Mtftp6TransmitPacket (Instance, Instance->LastPacket);
1190 } else {
1191 Mtftp6OperationClean (Instance, EFI_TIMEOUT);
1192 continue;
1193 }
1194 }
1195 }