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