2 Mtftp6 support functions implementation.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "Mtftp6Impl.h"
14 Allocate a MTFTP block range, then init it to the range of [Start, End].
16 @param[in] Start The start block number.
17 @param[in] End The last block number in the range.
19 @return Range The range of the allocated block buffer.
28 MTFTP6_BLOCK_RANGE
*Range
;
30 Range
= AllocateZeroPool (sizeof (MTFTP6_BLOCK_RANGE
));
36 InitializeListHead (&Range
->Link
);
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.
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.
61 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range.
62 @retval EFI_SUCCESS The initial block range is created.
66 Mtftp6InitBlockRange (
72 MTFTP6_BLOCK_RANGE
*Range
;
74 Range
= Mtftp6AllocateRange (Start
, End
);
77 return EFI_OUT_OF_RESOURCES
;
80 InsertTailList (Head
, &Range
->Link
);
86 Get the first valid block number on the range list.
88 @param[in] Head The block range head.
90 @retval ==-1 If the block range is empty.
91 @retval >-1 The first valid block number.
95 Mtftp6GetNextBlockNum (
99 MTFTP6_BLOCK_RANGE
*Range
;
101 if (IsListEmpty (Head
)) {
105 Range
= NET_LIST_HEAD (Head
, MTFTP6_BLOCK_RANGE
, Link
);
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.
117 @param[in] Head The block range list.
118 @param[in] Last The last block number.
122 Mtftp6SetLastBlockNum (
127 MTFTP6_BLOCK_RANGE
*Range
;
130 // Iterate from the tail to head to remove the block number
133 while (!IsListEmpty (Head
)) {
134 Range
= NET_LIST_TAIL (Head
, MTFTP6_BLOCK_RANGE
, Link
);
136 if (Range
->Start
> Last
) {
137 RemoveEntryList (&Range
->Link
);
142 if (Range
->End
> Last
) {
151 Remove the block number from the block range list.
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.
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.
164 Mtftp6RemoveBlockNum (
167 IN BOOLEAN Completed
,
168 OUT UINT64
*BlockCounter
171 MTFTP6_BLOCK_RANGE
*Range
;
172 MTFTP6_BLOCK_RANGE
*NewRange
;
175 NET_LIST_FOR_EACH (Entry
, Head
) {
178 // Each block represents a hole [Start, End] in the file,
179 // skip to the first range with End >= Num
181 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_BLOCK_RANGE
, Link
);
183 if (Range
->End
< Num
) {
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.
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.
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].
203 if (Range
->Start
> Num
) {
204 return EFI_NOT_FOUND
;
206 } else if (Range
->Start
== Num
) {
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
219 if (Range
->Round
> 0) {
220 *BlockCounter
+= Range
->Bound
+ MultU64x32 (Range
->Round
- 1, (UINT32
)(Range
->Bound
+ 1)) + 1;
223 if (Range
->Start
> Range
->Bound
) {
228 if ((Range
->Start
> Range
->End
) || Completed
) {
229 RemoveEntryList (&Range
->Link
);
236 if (Range
->End
== Num
) {
239 NewRange
= Mtftp6AllocateRange ((UINT16
) (Num
+ 1), (UINT16
) Range
->End
);
241 if (NewRange
== NULL
) {
242 return EFI_OUT_OF_RESOURCES
;
245 Range
->End
= Num
- 1;
246 NetListInsertAfter (&Range
->Link
, &NewRange
->Link
);
253 return EFI_NOT_FOUND
;
258 Configure the opened Udp6 instance until the corresponding Ip6 instance
261 @param[in] UdpIo The pointer to the Udp6 Io.
262 @param[in] UdpCfgData The pointer to the Udp6 configure data.
264 @retval EFI_SUCCESS Configure the Udp6 instance successfully.
265 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not
272 IN EFI_UDP6_CONFIG_DATA
*UdpCfgData
275 EFI_IP6_MODE_DATA Ip6Mode
;
276 EFI_UDP6_PROTOCOL
*Udp6
;
281 Udp6
= UdpIo
->Protocol
.Udp6
;
284 // Create a timer to check whether the Ip6 instance configured or not.
286 Status
= gBS
->CreateEvent (
293 if (EFI_ERROR (Status
)) {
297 Status
= gBS
->SetTimer (
300 MTFTP6_GET_MAPPING_TIMEOUT
* MTFTP6_TICK_PER_SECOND
302 if (EFI_ERROR (Status
)) {
307 // Check the Ip6 mode data till timeout.
309 while (EFI_ERROR (gBS
->CheckEvent (Event
))) {
313 Status
= Udp6
->GetModeData (Udp6
, NULL
, &Ip6Mode
, NULL
, NULL
);
315 if (!EFI_ERROR (Status
)) {
316 if (Ip6Mode
.AddressList
!= NULL
) {
317 FreePool (Ip6Mode
.AddressList
);
320 if (Ip6Mode
.GroupTable
!= NULL
) {
321 FreePool (Ip6Mode
.GroupTable
);
324 if (Ip6Mode
.RouteTable
!= NULL
) {
325 FreePool (Ip6Mode
.RouteTable
);
328 if (Ip6Mode
.NeighborCache
!= NULL
) {
329 FreePool (Ip6Mode
.NeighborCache
);
332 if (Ip6Mode
.PrefixTable
!= NULL
) {
333 FreePool (Ip6Mode
.PrefixTable
);
336 if (Ip6Mode
.IcmpTypeList
!= NULL
) {
337 FreePool (Ip6Mode
.IcmpTypeList
);
340 if (Ip6Mode
.IsConfigured
) {
342 // Continue to configure the Udp6 instance.
344 Status
= Udp6
->Configure (Udp6
, UdpCfgData
);
346 Status
= EFI_NO_MAPPING
;
354 gBS
->CloseEvent (Event
);
362 The dummy configure routine for create a new Udp6 Io.
364 @param[in] UdpIo The pointer to the Udp6 Io.
365 @param[in] Context The pointer to the context.
367 @retval EFI_SUCCESS This value is always returned.
372 Mtftp6ConfigDummyUdpIo (
382 The configure routine for Mtftp6 instance to transmit/receive.
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.
390 @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.
391 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been
398 IN EFI_IPv6_ADDRESS
*ServerIp
,
399 IN UINT16 ServerPort
,
400 IN EFI_IPv6_ADDRESS
*LocalIp
,
405 EFI_UDP6_PROTOCOL
*Udp6
;
406 EFI_UDP6_CONFIG_DATA
*Udp6Cfg
;
408 Udp6
= UdpIo
->Protocol
.Udp6
;
409 Udp6Cfg
= &(UdpIo
->Config
.Udp6
);
411 ZeroMem (Udp6Cfg
, sizeof (EFI_UDP6_CONFIG_DATA
));
414 // Set the Udp6 Io configure data.
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
;
427 &Udp6Cfg
->StationAddress
,
429 sizeof (EFI_IPv6_ADDRESS
)
433 &Udp6Cfg
->RemoteAddress
,
435 sizeof (EFI_IPv6_ADDRESS
)
439 // Configure the Udp6 instance with current configure data.
441 Status
= Udp6
->Configure (Udp6
, Udp6Cfg
);
443 if (Status
== EFI_NO_MAPPING
) {
445 return Mtftp6GetMapping (UdpIo
, Udp6Cfg
);
453 Build and transmit the request packet for the Mtftp6 instance.
455 @param[in] Instance The pointer to the Mtftp6 instance.
456 @param[in] Operation The operation code of this packet.
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.
465 IN MTFTP6_INSTANCE
*Instance
,
469 EFI_MTFTP6_PACKET
*Packet
;
470 EFI_MTFTP6_OPTION
*Options
;
471 EFI_MTFTP6_TOKEN
*Token
;
472 RETURN_STATUS Status
;
478 UINTN FileNameLength
;
480 UINTN OptionStrLength
;
481 UINTN ValueStrLength
;
483 Token
= Instance
->Token
;
484 Options
= Token
->OptionList
;
485 Mode
= Token
->ModeStr
;
488 Mode
= (UINT8
*) "octet";
492 // The header format of RRQ/WRQ packet is:
494 // 2 bytes string 1 byte string 1 byte
495 // ------------------------------------------------
496 // | Opcode | Filename | 0 | Mode | 0 |
497 // ------------------------------------------------
499 // The common option format is:
501 // string 1 byte string 1 byte
502 // ---------------------------------------
503 // | OptionStr | 0 | ValueStr | 0 |
504 // ---------------------------------------
508 // Compute the size of new Mtftp6 packet.
510 FileNameLength
= AsciiStrLen ((CHAR8
*) Token
->Filename
);
511 ModeLength
= AsciiStrLen ((CHAR8
*) Mode
);
512 BufferLength
= (UINT32
) FileNameLength
+ (UINT32
) ModeLength
+ 4;
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;
521 // Allocate a packet then copy the data.
523 if ((Nbuf
= NetbufAlloc (BufferLength
)) == NULL
) {
524 return EFI_OUT_OF_RESOURCES
;
528 // Copy the opcode, filename and mode into packet.
530 Packet
= (EFI_MTFTP6_PACKET
*) NetbufAllocSpace (Nbuf
, BufferLength
, FALSE
);
531 ASSERT (Packet
!= NULL
);
533 Packet
->OpCode
= HTONS (Operation
);
534 BufferLength
-= sizeof (Packet
->OpCode
);
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;
547 // Copy all the extension options into the packet.
549 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
550 OptionStrLength
= AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
);
551 ValueStrLength
= AsciiStrLen ((CHAR8
*) Options
[Index
].ValueStr
);
553 Status
= AsciiStrCpyS ((CHAR8
*) Cur
, BufferLength
, (CHAR8
*) Options
[Index
].OptionStr
);
554 ASSERT_EFI_ERROR (Status
);
555 BufferLength
-= (UINT32
) (OptionStrLength
+ 1);
556 Cur
+= OptionStrLength
+ 1;
558 Status
= AsciiStrCpyS ((CHAR8
*) Cur
, BufferLength
, (CHAR8
*) Options
[Index
].ValueStr
);
559 ASSERT_EFI_ERROR (Status
);
560 BufferLength
-= (UINT32
) (ValueStrLength
+ 1);
561 Cur
+= ValueStrLength
+ 1;
566 // Save the packet buf for retransmit
568 if (Instance
->LastPacket
!= NULL
) {
569 NetbufFree (Instance
->LastPacket
);
572 Instance
->LastPacket
= Nbuf
;
573 Instance
->CurRetry
= 0;
575 return Mtftp6TransmitPacket (Instance
, Nbuf
);
580 Build and send an error packet.
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.
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.
593 IN MTFTP6_INSTANCE
*Instance
,
599 EFI_MTFTP6_PACKET
*TftpError
;
603 // Allocate a packet then copy the data.
605 Len
= (UINT32
) (AsciiStrLen ((CHAR8
*) ErrInfo
) + sizeof (EFI_MTFTP6_ERROR_HEADER
));
606 Nbuf
= NetbufAlloc (Len
);
609 return EFI_OUT_OF_RESOURCES
;
612 TftpError
= (EFI_MTFTP6_PACKET
*) NetbufAllocSpace (Nbuf
, Len
, FALSE
);
614 if (TftpError
== NULL
) {
616 return EFI_OUT_OF_RESOURCES
;
619 TftpError
->OpCode
= HTONS (EFI_MTFTP6_OPCODE_ERROR
);
620 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
622 AsciiStrCpyS ((CHAR8
*) TftpError
->Error
.ErrorMessage
, AsciiStrLen ((CHAR8
*) ErrInfo
) + 1 , (CHAR8
*) ErrInfo
);
625 // Save the packet buf for retransmit
627 if (Instance
->LastPacket
!= NULL
) {
628 NetbufFree (Instance
->LastPacket
);
631 Instance
->LastPacket
= Nbuf
;
632 Instance
->CurRetry
= 0;
634 return Mtftp6TransmitPacket (Instance
, Nbuf
);
639 The callback function called when the packet is transmitted.
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.
651 IN UDP_END_POINT
*UdpEpt
,
652 IN EFI_STATUS IoStatus
,
657 *(BOOLEAN
*) Context
= TRUE
;
662 Send the packet for the Mtftp6 instance.
664 @param[in] Instance The pointer to the Mtftp6 instance.
665 @param[in] Packet The pointer to the packet to be sent.
667 @retval EFI_SUCCESS The packet was sent out
668 @retval Others Failed to transmit the packet.
672 Mtftp6TransmitPacket (
673 IN MTFTP6_INSTANCE
*Instance
,
677 EFI_UDP6_PROTOCOL
*Udp6
;
678 EFI_UDP6_CONFIG_DATA Udp6CfgData
;
684 ZeroMem (&Udp6CfgData
, sizeof(EFI_UDP6_CONFIG_DATA
));
685 Udp6
= Instance
->UdpIo
->Protocol
.Udp6
;
688 // Set the live time of the packet.
690 Instance
->PacketToLive
= Instance
->IsMaster
? Instance
->Timeout
: (Instance
->Timeout
* 2);
692 Temp
= (UINT16
*) NetbufGetByte (Packet
, 0, NULL
);
693 ASSERT (Temp
!= NULL
);
696 OpCode
= NTOHS (Value
);
698 if (OpCode
== EFI_MTFTP6_OPCODE_RRQ
|| OpCode
== EFI_MTFTP6_OPCODE_DIR
|| OpCode
== EFI_MTFTP6_OPCODE_WRQ
) {
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.
704 Status
= Udp6
->Configure (Udp6
, NULL
);
706 if (EFI_ERROR (Status
)) {
710 Status
= Mtftp6ConfigUdpIo (
713 Instance
->ServerCmdPort
,
714 &Instance
->Config
->StationIp
,
715 Instance
->Config
->LocalPort
718 if (EFI_ERROR (Status
)) {
723 // Get the current local address and port by get Udp6 mode data.
725 Status
= Udp6
->GetModeData (Udp6
, &Udp6CfgData
, NULL
, NULL
, NULL
);
726 if (EFI_ERROR (Status
)) {
730 NET_GET_REF (Packet
);
732 Instance
->IsTransmitted
= FALSE
;
734 Status
= UdpIoSendDatagram (
740 &Instance
->IsTransmitted
743 if (EFI_ERROR (Status
)) {
744 NET_PUT_REF (Packet
);
749 // Poll till the packet sent out from the ip6 queue.
751 gBS
->RestoreTPL (Instance
->OldTpl
);
753 while (!Instance
->IsTransmitted
) {
757 Instance
->OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
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.
764 Status
= Udp6
->Configure (Udp6
, NULL
);
766 if (EFI_ERROR (Status
)) {
770 Status
= Mtftp6ConfigUdpIo (
773 Instance
->ServerDataPort
,
774 &Udp6CfgData
.StationAddress
,
775 Udp6CfgData
.StationPort
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.
783 Status
= Udp6
->GetModeData (Udp6
, &Udp6CfgData
, NULL
, NULL
, NULL
);
784 if (EFI_ERROR (Status
)) {
788 if (Udp6CfgData
.RemotePort
!= Instance
->ServerDataPort
) {
790 Status
= Udp6
->Configure (Udp6
, NULL
);
792 if (EFI_ERROR (Status
)) {
796 Status
= Mtftp6ConfigUdpIo (
799 Instance
->ServerDataPort
,
800 &Udp6CfgData
.StationAddress
,
801 Udp6CfgData
.StationPort
804 if (EFI_ERROR (Status
)) {
809 NET_GET_REF (Packet
);
811 Instance
->IsTransmitted
= FALSE
;
813 Status
= UdpIoSendDatagram (
819 &Instance
->IsTransmitted
822 if (EFI_ERROR (Status
)) {
823 NET_PUT_REF (Packet
);
827 // Poll till the packet sent out from the ip6 queue.
829 gBS
->RestoreTPL (Instance
->OldTpl
);
831 while (!Instance
->IsTransmitted
) {
835 Instance
->OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
843 Check packet for GetInfo callback routine.
845 GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect
846 the first packet from server, then abort the session.
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.
853 @retval EFI_ABORTED Abort the Mtftp6 operation.
859 IN EFI_MTFTP6_PROTOCOL
*This
,
860 IN EFI_MTFTP6_TOKEN
*Token
,
862 IN EFI_MTFTP6_PACKET
*Packet
865 MTFTP6_GETINFO_CONTEXT
*Context
;
868 Context
= (MTFTP6_GETINFO_CONTEXT
*) Token
->Context
;
869 OpCode
= NTOHS (Packet
->OpCode
);
872 // Set the GetInfo's return status according to the OpCode.
875 case EFI_MTFTP6_OPCODE_ERROR
:
876 Context
->Status
= EFI_TFTP_ERROR
;
879 case EFI_MTFTP6_OPCODE_OACK
:
880 Context
->Status
= EFI_SUCCESS
;
884 Context
->Status
= EFI_PROTOCOL_ERROR
;
888 // Allocate buffer then copy the packet over. Use gBS->AllocatePool
889 // in case NetAllocatePool will implements something tricky.
891 *(Context
->Packet
) = AllocateZeroPool (PacketLen
);
893 if (*(Context
->Packet
) == NULL
) {
894 Context
->Status
= EFI_OUT_OF_RESOURCES
;
898 *(Context
->PacketLen
) = PacketLen
;
899 CopyMem (*(Context
->Packet
), Packet
, PacketLen
);
906 Clean up the current Mtftp6 operation.
908 @param[in] Instance The pointer to the Mtftp6 instance.
909 @param[in] Result The result to be returned to the user.
913 Mtftp6OperationClean (
914 IN MTFTP6_INSTANCE
*Instance
,
920 MTFTP6_BLOCK_RANGE
*Block
;
923 // Clean up the current token and event.
925 if (Instance
->Token
!= NULL
) {
926 Instance
->Token
->Status
= Result
;
927 if (Instance
->Token
->Event
!= NULL
) {
928 gBS
->SignalEvent (Instance
->Token
->Event
);
930 Instance
->Token
= NULL
;
934 // Clean up the corresponding Udp6Io.
936 if (Instance
->UdpIo
!= NULL
) {
937 UdpIoCleanIo (Instance
->UdpIo
);
940 if (Instance
->McastUdpIo
!= NULL
) {
942 Instance
->McastUdpIo
->UdpHandle
,
943 &gEfiUdp6ProtocolGuid
,
944 Instance
->McastUdpIo
->Image
,
947 UdpIoFreeIo (Instance
->McastUdpIo
);
948 Instance
->McastUdpIo
= NULL
;
952 // Clean up the stored last packet.
954 if (Instance
->LastPacket
!= NULL
) {
955 NetbufFree (Instance
->LastPacket
);
956 Instance
->LastPacket
= NULL
;
959 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Instance
->BlkList
) {
960 Block
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_BLOCK_RANGE
, Link
);
961 RemoveEntryList (Entry
);
966 // Reinitialize the corresponding fields of the Mtftp6 operation.
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
));
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
;
990 Start the Mtftp6 instance to perform the operation, such as read file,
991 write file, and read directory.
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.
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.
1004 Mtftp6OperationStart (
1005 IN EFI_MTFTP6_PROTOCOL
*This
,
1006 IN EFI_MTFTP6_TOKEN
*Token
,
1010 MTFTP6_INSTANCE
*Instance
;
1015 Token
->Filename
== NULL
||
1016 (Token
->OptionCount
!= 0 && Token
->OptionList
== NULL
) ||
1017 (Token
->OverrideData
!= NULL
&& !NetIp6IsValidUnicast (&Token
->OverrideData
->ServerIp
))
1019 return EFI_INVALID_PARAMETER
;
1023 // At least define one method to collect the data for download.
1025 if ((OpCode
== EFI_MTFTP6_OPCODE_RRQ
|| OpCode
== EFI_MTFTP6_OPCODE_DIR
) &&
1026 Token
->Buffer
== NULL
&&
1027 Token
->CheckPacket
== NULL
1029 return EFI_INVALID_PARAMETER
;
1033 // At least define one method to provide the data for upload.
1035 if (OpCode
== EFI_MTFTP6_OPCODE_WRQ
&& Token
->Buffer
== NULL
&& Token
->PacketNeeded
== NULL
) {
1036 return EFI_INVALID_PARAMETER
;
1039 Instance
= MTFTP6_INSTANCE_FROM_THIS (This
);
1041 if (Instance
->Config
== NULL
) {
1042 return EFI_NOT_STARTED
;
1045 if (Instance
->Token
!= NULL
) {
1046 return EFI_ACCESS_DENIED
;
1049 Status
= EFI_SUCCESS
;
1050 Instance
->OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1052 Instance
->Operation
= OpCode
;
1055 // Parse the extension options in the request packet.
1057 if (Token
->OptionCount
!= 0) {
1059 Status
= Mtftp6ParseExtensionOption (
1063 Instance
->Operation
,
1067 if (EFI_ERROR (Status
)) {
1073 // Initialize runtime data from config data or override data.
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
;
1083 &Instance
->ServerIp
,
1084 &Instance
->Config
->ServerIp
,
1085 sizeof (EFI_IPv6_ADDRESS
)
1088 if (Token
->OverrideData
!= NULL
) {
1089 Instance
->ServerCmdPort
= Token
->OverrideData
->ServerPort
;
1090 Instance
->MaxRetry
= Token
->OverrideData
->TryCount
;
1091 Instance
->Timeout
= Token
->OverrideData
->TimeoutValue
;
1094 &Instance
->ServerIp
,
1095 &Token
->OverrideData
->ServerIp
,
1096 sizeof (EFI_IPv6_ADDRESS
)
1101 // Set default value for undefined parameters.
1103 if (Instance
->ServerCmdPort
== 0) {
1104 Instance
->ServerCmdPort
= MTFTP6_DEFAULT_SERVER_CMD_PORT
;
1106 if (Instance
->BlkSize
== 0) {
1107 Instance
->BlkSize
= MTFTP6_DEFAULT_BLK_SIZE
;
1109 if (Instance
->WindowSize
== 0) {
1110 Instance
->WindowSize
= MTFTP6_DEFAULT_WINDOWSIZE
;
1112 if (Instance
->MaxRetry
== 0) {
1113 Instance
->MaxRetry
= MTFTP6_DEFAULT_MAX_RETRY
;
1115 if (Instance
->Timeout
== 0) {
1116 Instance
->Timeout
= MTFTP6_DEFAULT_TIMEOUT
;
1119 Token
->Status
= EFI_NOT_READY
;
1122 // Switch the routines by the operation code.
1125 case EFI_MTFTP6_OPCODE_RRQ
:
1126 Status
= Mtftp6RrqStart (Instance
, OpCode
);
1129 case EFI_MTFTP6_OPCODE_DIR
:
1130 Status
= Mtftp6RrqStart (Instance
, OpCode
);
1133 case EFI_MTFTP6_OPCODE_WRQ
:
1134 Status
= Mtftp6WrqStart (Instance
, OpCode
);
1138 Status
= EFI_DEVICE_ERROR
;
1142 if (EFI_ERROR (Status
)) {
1147 // Return immediately for asynchronous or poll the instance for synchronous.
1149 gBS
->RestoreTPL (Instance
->OldTpl
);
1151 if (Token
->Event
== NULL
) {
1152 while (Token
->Status
== EFI_NOT_READY
) {
1155 return Token
->Status
;
1162 Mtftp6OperationClean (Instance
, Status
);
1163 gBS
->RestoreTPL (Instance
->OldTpl
);
1170 The timer ticking routine for the Mtftp6 instance.
1172 @param[in] Event The pointer to the ticking event.
1173 @param[in] Context The pointer to the context.
1183 MTFTP6_SERVICE
*Service
;
1184 MTFTP6_INSTANCE
*Instance
;
1187 EFI_MTFTP6_TOKEN
*Token
;
1190 Service
= (MTFTP6_SERVICE
*) Context
;
1193 // Iterate through all the children of the Mtftp service instance. Time
1194 // out the packet. If maximum retries reached, clean the session up.
1196 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Service
->Children
) {
1198 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_INSTANCE
, Link
);
1200 if (Instance
->Token
== NULL
) {
1204 if (Instance
->PacketToLive
> 0) {
1205 Instance
->PacketToLive
--;
1209 Instance
->CurRetry
++;
1210 Token
= Instance
->Token
;
1212 if (Token
->TimeoutCallback
!= NULL
) {
1214 // Call the timeout callback routine if has.
1216 Status
= Token
->TimeoutCallback (&Instance
->Mtftp6
, Token
);
1218 if (EFI_ERROR (Status
)) {
1221 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
1222 (UINT8
*) "User aborted the transfer in time out"
1224 Mtftp6OperationClean (Instance
, EFI_ABORTED
);
1230 // Retransmit the packet if haven't reach the maxmium retry count,
1231 // otherwise exit the transfer.
1233 if (Instance
->CurRetry
< Instance
->MaxRetry
) {
1234 Mtftp6TransmitPacket (Instance
, Instance
->LastPacket
);
1236 Mtftp6OperationClean (Instance
, EFI_TIMEOUT
);