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"
13 Allocate a MTFTP block range, then init it to the range of [Start, End].
15 @param[in] Start The start block number.
16 @param[in] End The last block number in the range.
18 @return Range The range of the allocated block buffer.
27 MTFTP6_BLOCK_RANGE
*Range
;
29 Range
= AllocateZeroPool (sizeof (MTFTP6_BLOCK_RANGE
));
35 InitializeListHead (&Range
->Link
);
44 Initialize the block range for either RRQ or WRQ. RRQ and WRQ have
45 different requirements for Start and End. For example, during startup,
46 WRQ initializes its whole valid block range to [0, 0xffff]. This
47 is because the server will send an ACK0 to inform the user to start the
48 upload. When the client receives an ACK0, it will remove 0 from the range,
49 get the next block number, which is 1, then upload the BLOCK1. For RRQ
50 without option negotiation, the server will directly send the BLOCK1
51 in response to the client's RRQ. When received BLOCK1, the client will
52 remove it from the block range and send an ACK. It also works if there
53 is option negotiation.
55 @param[in] Head The block range head to initialize.
56 @param[in] Start The Start block number.
57 @param[in] End The last block number.
59 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range.
60 @retval EFI_SUCCESS The initial block range is created.
64 Mtftp6InitBlockRange (
70 MTFTP6_BLOCK_RANGE
*Range
;
72 Range
= Mtftp6AllocateRange (Start
, End
);
75 return EFI_OUT_OF_RESOURCES
;
78 InsertTailList (Head
, &Range
->Link
);
83 Get the first valid block number on the range list.
85 @param[in] Head The block range head.
87 @retval ==-1 If the block range is empty.
88 @retval >-1 The first valid block number.
92 Mtftp6GetNextBlockNum (
96 MTFTP6_BLOCK_RANGE
*Range
;
98 if (IsListEmpty (Head
)) {
102 Range
= NET_LIST_HEAD (Head
, MTFTP6_BLOCK_RANGE
, Link
);
107 Set the last block number of the block range list. It
108 removes all the blocks after the Last. MTFTP initialize the
109 block range to the maximum possible range, such as [0, 0xffff]
110 for WRQ. When it gets the last block number, it calls
111 this function to set the last block number.
113 @param[in] Head The block range list.
114 @param[in] Last The last block number.
118 Mtftp6SetLastBlockNum (
123 MTFTP6_BLOCK_RANGE
*Range
;
126 // Iterate from the tail to head to remove the block number
129 while (!IsListEmpty (Head
)) {
130 Range
= NET_LIST_TAIL (Head
, MTFTP6_BLOCK_RANGE
, Link
);
132 if (Range
->Start
> Last
) {
133 RemoveEntryList (&Range
->Link
);
138 if (Range
->End
> Last
) {
147 Remove the block number from the block range list.
149 @param[in] Head The block range list to remove from.
150 @param[in] Num The block number to remove.
151 @param[in] Completed Whether Num is the last block number.
152 @param[out] BlockCounter The continuous block counter instead of the value after roll-over.
154 @retval EFI_NOT_FOUND The block number isn't in the block range list.
155 @retval EFI_SUCCESS The block number has been removed from the list.
156 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
160 Mtftp6RemoveBlockNum (
163 IN BOOLEAN Completed
,
164 OUT UINT64
*BlockCounter
167 MTFTP6_BLOCK_RANGE
*Range
;
168 MTFTP6_BLOCK_RANGE
*NewRange
;
171 NET_LIST_FOR_EACH (Entry
, Head
) {
173 // Each block represents a hole [Start, End] in the file,
174 // skip to the first range with End >= Num
176 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_BLOCK_RANGE
, Link
);
178 if (Range
->End
< Num
) {
183 // There are three different cases for Start
184 // 1. (Start > Num) && (End >= Num):
185 // because all the holes before this one has the condition of
186 // End < Num, so this block number has been removed.
188 // 2. (Start == Num) && (End >= Num):
189 // Need to increase the Start by one, and if End == Num, this
190 // hole has been removed completely, remove it.
192 // 3. (Start < Num) && (End >= Num):
193 // if End == Num, only need to decrease the End by one because
194 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
195 // if (End > Num), the hold is split into two holes, with
196 // [Start, Num - 1] and [Num + 1, End].
198 if (Range
->Start
> Num
) {
199 return EFI_NOT_FOUND
;
200 } else if (Range
->Start
== Num
) {
204 // Note that: RFC 1350 does not mention block counter roll-over,
205 // but several TFTP hosts implement the roll-over be able to accept
206 // transfers of unlimited size. There is no consensus, however, whether
207 // the counter should wrap around to zero or to one. Many implementations
208 // wrap to zero, because this is the simplest to implement. Here we choose
213 if (Range
->Round
> 0) {
214 *BlockCounter
+= Range
->Bound
+ MultU64x32 (Range
->Round
- 1, (UINT32
)(Range
->Bound
+ 1)) + 1;
217 if (Range
->Start
> Range
->Bound
) {
222 if ((Range
->Start
> Range
->End
) || Completed
) {
223 RemoveEntryList (&Range
->Link
);
229 if (Range
->End
== Num
) {
232 NewRange
= Mtftp6AllocateRange ((UINT16
)(Num
+ 1), (UINT16
)Range
->End
);
234 if (NewRange
== NULL
) {
235 return EFI_OUT_OF_RESOURCES
;
238 Range
->End
= Num
- 1;
239 NetListInsertAfter (&Range
->Link
, &NewRange
->Link
);
246 return EFI_NOT_FOUND
;
250 Configure the opened Udp6 instance until the corresponding Ip6 instance
253 @param[in] UdpIo The pointer to the Udp6 Io.
254 @param[in] UdpCfgData The pointer to the Udp6 configure data.
256 @retval EFI_SUCCESS Configure the Udp6 instance successfully.
257 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not
264 IN EFI_UDP6_CONFIG_DATA
*UdpCfgData
267 EFI_IP6_MODE_DATA Ip6Mode
;
268 EFI_UDP6_PROTOCOL
*Udp6
;
273 Udp6
= UdpIo
->Protocol
.Udp6
;
276 // Create a timer to check whether the Ip6 instance configured or not.
278 Status
= gBS
->CreateEvent (
285 if (EFI_ERROR (Status
)) {
289 Status
= gBS
->SetTimer (
292 MTFTP6_GET_MAPPING_TIMEOUT
* MTFTP6_TICK_PER_SECOND
294 if (EFI_ERROR (Status
)) {
299 // Check the Ip6 mode data till timeout.
301 while (EFI_ERROR (gBS
->CheckEvent (Event
))) {
304 Status
= Udp6
->GetModeData (Udp6
, NULL
, &Ip6Mode
, NULL
, NULL
);
306 if (!EFI_ERROR (Status
)) {
307 if (Ip6Mode
.AddressList
!= NULL
) {
308 FreePool (Ip6Mode
.AddressList
);
311 if (Ip6Mode
.GroupTable
!= NULL
) {
312 FreePool (Ip6Mode
.GroupTable
);
315 if (Ip6Mode
.RouteTable
!= NULL
) {
316 FreePool (Ip6Mode
.RouteTable
);
319 if (Ip6Mode
.NeighborCache
!= NULL
) {
320 FreePool (Ip6Mode
.NeighborCache
);
323 if (Ip6Mode
.PrefixTable
!= NULL
) {
324 FreePool (Ip6Mode
.PrefixTable
);
327 if (Ip6Mode
.IcmpTypeList
!= NULL
) {
328 FreePool (Ip6Mode
.IcmpTypeList
);
331 if (Ip6Mode
.IsConfigured
) {
333 // Continue to configure the Udp6 instance.
335 Status
= Udp6
->Configure (Udp6
, UdpCfgData
);
337 Status
= EFI_NO_MAPPING
;
345 gBS
->CloseEvent (Event
);
352 The dummy configure routine for create a new Udp6 Io.
354 @param[in] UdpIo The pointer to the Udp6 Io.
355 @param[in] Context The pointer to the context.
357 @retval EFI_SUCCESS This value is always returned.
362 Mtftp6ConfigDummyUdpIo (
371 The configure routine for Mtftp6 instance to transmit/receive.
373 @param[in] UdpIo The pointer to the Udp6 Io.
374 @param[in] ServerIp The pointer to the server address.
375 @param[in] ServerPort The pointer to the server port.
376 @param[in] LocalIp The pointer to the local address.
377 @param[in] LocalPort The pointer to the local port.
379 @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.
380 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been
387 IN EFI_IPv6_ADDRESS
*ServerIp
,
388 IN UINT16 ServerPort
,
389 IN EFI_IPv6_ADDRESS
*LocalIp
,
394 EFI_UDP6_PROTOCOL
*Udp6
;
395 EFI_UDP6_CONFIG_DATA
*Udp6Cfg
;
397 Udp6
= UdpIo
->Protocol
.Udp6
;
398 Udp6Cfg
= &(UdpIo
->Config
.Udp6
);
400 ZeroMem (Udp6Cfg
, sizeof (EFI_UDP6_CONFIG_DATA
));
403 // Set the Udp6 Io configure data.
405 Udp6Cfg
->AcceptPromiscuous
= FALSE
;
406 Udp6Cfg
->AcceptAnyPort
= FALSE
;
407 Udp6Cfg
->AllowDuplicatePort
= FALSE
;
408 Udp6Cfg
->TrafficClass
= 0;
409 Udp6Cfg
->HopLimit
= 128;
410 Udp6Cfg
->ReceiveTimeout
= 0;
411 Udp6Cfg
->TransmitTimeout
= 0;
412 Udp6Cfg
->StationPort
= LocalPort
;
413 Udp6Cfg
->RemotePort
= ServerPort
;
416 &Udp6Cfg
->StationAddress
,
418 sizeof (EFI_IPv6_ADDRESS
)
422 &Udp6Cfg
->RemoteAddress
,
424 sizeof (EFI_IPv6_ADDRESS
)
428 // Configure the Udp6 instance with current configure data.
430 Status
= Udp6
->Configure (Udp6
, Udp6Cfg
);
432 if (Status
== EFI_NO_MAPPING
) {
433 return Mtftp6GetMapping (UdpIo
, Udp6Cfg
);
440 Build and transmit the request packet for the Mtftp6 instance.
442 @param[in] Instance The pointer to the Mtftp6 instance.
443 @param[in] Operation The operation code of this packet.
445 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request.
446 @retval EFI_SUCCESS The request is built and sent.
447 @retval Others Failed to transmit the packet.
452 IN MTFTP6_INSTANCE
*Instance
,
456 EFI_MTFTP6_PACKET
*Packet
;
457 EFI_MTFTP6_OPTION
*Options
;
458 EFI_MTFTP6_TOKEN
*Token
;
459 RETURN_STATUS Status
;
465 UINTN FileNameLength
;
467 UINTN OptionStrLength
;
468 UINTN ValueStrLength
;
470 Token
= Instance
->Token
;
471 Options
= Token
->OptionList
;
472 Mode
= Token
->ModeStr
;
475 Mode
= (UINT8
*)"octet";
479 // The header format of RRQ/WRQ packet is:
481 // 2 bytes string 1 byte string 1 byte
482 // ------------------------------------------------
483 // | Opcode | Filename | 0 | Mode | 0 |
484 // ------------------------------------------------
486 // The common option format is:
488 // string 1 byte string 1 byte
489 // ---------------------------------------
490 // | OptionStr | 0 | ValueStr | 0 |
491 // ---------------------------------------
495 // Compute the size of new Mtftp6 packet.
497 FileNameLength
= AsciiStrLen ((CHAR8
*)Token
->Filename
);
498 ModeLength
= AsciiStrLen ((CHAR8
*)Mode
);
499 BufferLength
= (UINT32
)FileNameLength
+ (UINT32
)ModeLength
+ 4;
501 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
502 OptionStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].OptionStr
);
503 ValueStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].ValueStr
);
504 BufferLength
+= (UINT32
)OptionStrLength
+ (UINT32
)ValueStrLength
+ 2;
508 // Allocate a packet then copy the data.
510 if ((Nbuf
= NetbufAlloc (BufferLength
)) == NULL
) {
511 return EFI_OUT_OF_RESOURCES
;
515 // Copy the opcode, filename and mode into packet.
517 Packet
= (EFI_MTFTP6_PACKET
*)NetbufAllocSpace (Nbuf
, BufferLength
, FALSE
);
518 ASSERT (Packet
!= NULL
);
520 Packet
->OpCode
= HTONS (Operation
);
521 BufferLength
-= sizeof (Packet
->OpCode
);
523 Cur
= Packet
->Rrq
.Filename
;
524 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Token
->Filename
);
525 ASSERT_EFI_ERROR (Status
);
526 BufferLength
-= (UINT32
)(FileNameLength
+ 1);
527 Cur
+= FileNameLength
+ 1;
528 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Mode
);
529 ASSERT_EFI_ERROR (Status
);
530 BufferLength
-= (UINT32
)(ModeLength
+ 1);
531 Cur
+= ModeLength
+ 1;
534 // Copy all the extension options into the packet.
536 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
537 OptionStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].OptionStr
);
538 ValueStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].ValueStr
);
540 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Options
[Index
].OptionStr
);
541 ASSERT_EFI_ERROR (Status
);
542 BufferLength
-= (UINT32
)(OptionStrLength
+ 1);
543 Cur
+= OptionStrLength
+ 1;
545 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Options
[Index
].ValueStr
);
546 ASSERT_EFI_ERROR (Status
);
547 BufferLength
-= (UINT32
)(ValueStrLength
+ 1);
548 Cur
+= ValueStrLength
+ 1;
552 // Save the packet buf for retransmit
554 if (Instance
->LastPacket
!= NULL
) {
555 NetbufFree (Instance
->LastPacket
);
558 Instance
->LastPacket
= Nbuf
;
559 Instance
->CurRetry
= 0;
561 return Mtftp6TransmitPacket (Instance
, Nbuf
);
565 Build and send an error packet.
567 @param[in] Instance The pointer to the Mtftp6 instance.
568 @param[in] ErrCode The error code in the packet.
569 @param[in] ErrInfo The error message in the packet.
571 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet.
572 @retval EFI_SUCCESS The error packet is transmitted.
573 @retval Others Failed to transmit the packet.
578 IN MTFTP6_INSTANCE
*Instance
,
584 EFI_MTFTP6_PACKET
*TftpError
;
588 // Allocate a packet then copy the data.
590 Len
= (UINT32
)(AsciiStrLen ((CHAR8
*)ErrInfo
) + sizeof (EFI_MTFTP6_ERROR_HEADER
));
591 Nbuf
= NetbufAlloc (Len
);
594 return EFI_OUT_OF_RESOURCES
;
597 TftpError
= (EFI_MTFTP6_PACKET
*)NetbufAllocSpace (Nbuf
, Len
, FALSE
);
599 if (TftpError
== NULL
) {
601 return EFI_OUT_OF_RESOURCES
;
604 TftpError
->OpCode
= HTONS (EFI_MTFTP6_OPCODE_ERROR
);
605 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
607 AsciiStrCpyS ((CHAR8
*)TftpError
->Error
.ErrorMessage
, AsciiStrLen ((CHAR8
*)ErrInfo
) + 1, (CHAR8
*)ErrInfo
);
610 // Save the packet buf for retransmit
612 if (Instance
->LastPacket
!= NULL
) {
613 NetbufFree (Instance
->LastPacket
);
616 Instance
->LastPacket
= Nbuf
;
617 Instance
->CurRetry
= 0;
619 return Mtftp6TransmitPacket (Instance
, Nbuf
);
623 The callback function called when the packet is transmitted.
625 @param[in] Packet The pointer to the packet.
626 @param[in] UdpEpt The pointer to the Udp6 access point.
627 @param[in] IoStatus The result of the transmission.
628 @param[in] Context The pointer to the context.
635 IN UDP_END_POINT
*UdpEpt
,
636 IN EFI_STATUS IoStatus
,
641 *(BOOLEAN
*)Context
= TRUE
;
645 Send the packet for the Mtftp6 instance.
647 @param[in] Instance The pointer to the Mtftp6 instance.
648 @param[in] Packet The pointer to the packet to be sent.
650 @retval EFI_SUCCESS The packet was sent out
651 @retval Others Failed to transmit the packet.
655 Mtftp6TransmitPacket (
656 IN MTFTP6_INSTANCE
*Instance
,
660 EFI_UDP6_PROTOCOL
*Udp6
;
661 EFI_UDP6_CONFIG_DATA Udp6CfgData
;
667 ZeroMem (&Udp6CfgData
, sizeof (EFI_UDP6_CONFIG_DATA
));
668 Udp6
= Instance
->UdpIo
->Protocol
.Udp6
;
671 // Set the live time of the packet.
673 Instance
->PacketToLive
= Instance
->IsMaster
? Instance
->Timeout
: (Instance
->Timeout
* 2);
675 Temp
= (UINT16
*)NetbufGetByte (Packet
, 0, NULL
);
676 ASSERT (Temp
!= NULL
);
679 OpCode
= NTOHS (Value
);
681 if ((OpCode
== EFI_MTFTP6_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP6_OPCODE_DIR
) || (OpCode
== EFI_MTFTP6_OPCODE_WRQ
)) {
683 // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as
684 // (serverip, 69, localip, localport) to send.
685 // Usually local address and local port are both default as zero.
687 Status
= Udp6
->Configure (Udp6
, NULL
);
689 if (EFI_ERROR (Status
)) {
693 Status
= Mtftp6ConfigUdpIo (
696 Instance
->ServerCmdPort
,
697 &Instance
->Config
->StationIp
,
698 Instance
->Config
->LocalPort
701 if (EFI_ERROR (Status
)) {
706 // Get the current local address and port by get Udp6 mode data.
708 Status
= Udp6
->GetModeData (Udp6
, &Udp6CfgData
, NULL
, NULL
, NULL
);
709 if (EFI_ERROR (Status
)) {
713 NET_GET_REF (Packet
);
715 Instance
->IsTransmitted
= FALSE
;
717 Status
= UdpIoSendDatagram (
723 &Instance
->IsTransmitted
726 if (EFI_ERROR (Status
)) {
727 NET_PUT_REF (Packet
);
732 // Poll till the packet sent out from the ip6 queue.
734 gBS
->RestoreTPL (Instance
->OldTpl
);
736 while (!Instance
->IsTransmitted
) {
740 Instance
->OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
743 // For the subsequent exchange of such requests, reconfigure the Udp6Io as
744 // (serverip, 0, localip, localport) to receive.
745 // Currently local address and local port are specified by Udp6 mode data.
747 Status
= Udp6
->Configure (Udp6
, NULL
);
749 if (EFI_ERROR (Status
)) {
753 Status
= Mtftp6ConfigUdpIo (
756 Instance
->ServerDataPort
,
757 &Udp6CfgData
.StationAddress
,
758 Udp6CfgData
.StationPort
762 // For the data exchange, configure the Udp6Io as (serverip, dataport,
763 // localip, localport) to send/receive.
764 // Currently local address and local port are specified by Udp6 mode data.
766 Status
= Udp6
->GetModeData (Udp6
, &Udp6CfgData
, NULL
, NULL
, NULL
);
767 if (EFI_ERROR (Status
)) {
771 if (Udp6CfgData
.RemotePort
!= Instance
->ServerDataPort
) {
772 Status
= Udp6
->Configure (Udp6
, NULL
);
774 if (EFI_ERROR (Status
)) {
778 Status
= Mtftp6ConfigUdpIo (
781 Instance
->ServerDataPort
,
782 &Udp6CfgData
.StationAddress
,
783 Udp6CfgData
.StationPort
786 if (EFI_ERROR (Status
)) {
791 NET_GET_REF (Packet
);
793 Instance
->IsTransmitted
= FALSE
;
795 Status
= UdpIoSendDatagram (
801 &Instance
->IsTransmitted
804 if (EFI_ERROR (Status
)) {
805 NET_PUT_REF (Packet
);
809 // Poll till the packet sent out from the ip6 queue.
811 gBS
->RestoreTPL (Instance
->OldTpl
);
813 while (!Instance
->IsTransmitted
) {
817 Instance
->OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
824 Check packet for GetInfo callback routine.
826 GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect
827 the first packet from server, then abort the session.
829 @param[in] This The pointer to the Mtftp6 protocol.
830 @param[in] Token The pointer to the Mtftp6 token.
831 @param[in] PacketLen The length of the packet.
832 @param[in] Packet The pointer to the received packet.
834 @retval EFI_ABORTED Abort the Mtftp6 operation.
840 IN EFI_MTFTP6_PROTOCOL
*This
,
841 IN EFI_MTFTP6_TOKEN
*Token
,
843 IN EFI_MTFTP6_PACKET
*Packet
846 MTFTP6_GETINFO_CONTEXT
*Context
;
849 Context
= (MTFTP6_GETINFO_CONTEXT
*)Token
->Context
;
850 OpCode
= NTOHS (Packet
->OpCode
);
853 // Set the GetInfo's return status according to the OpCode.
856 case EFI_MTFTP6_OPCODE_ERROR
:
857 Context
->Status
= EFI_TFTP_ERROR
;
860 case EFI_MTFTP6_OPCODE_OACK
:
861 Context
->Status
= EFI_SUCCESS
;
865 Context
->Status
= EFI_PROTOCOL_ERROR
;
869 // Allocate buffer then copy the packet over. Use gBS->AllocatePool
870 // in case NetAllocatePool will implements something tricky.
872 *(Context
->Packet
) = AllocateZeroPool (PacketLen
);
874 if (*(Context
->Packet
) == NULL
) {
875 Context
->Status
= EFI_OUT_OF_RESOURCES
;
879 *(Context
->PacketLen
) = PacketLen
;
880 CopyMem (*(Context
->Packet
), Packet
, PacketLen
);
886 Clean up the current Mtftp6 operation.
888 @param[in] Instance The pointer to the Mtftp6 instance.
889 @param[in] Result The result to be returned to the user.
893 Mtftp6OperationClean (
894 IN MTFTP6_INSTANCE
*Instance
,
900 MTFTP6_BLOCK_RANGE
*Block
;
903 // Clean up the current token and event.
905 if (Instance
->Token
!= NULL
) {
906 Instance
->Token
->Status
= Result
;
907 if (Instance
->Token
->Event
!= NULL
) {
908 gBS
->SignalEvent (Instance
->Token
->Event
);
911 Instance
->Token
= NULL
;
915 // Clean up the corresponding Udp6Io.
917 if (Instance
->UdpIo
!= NULL
) {
918 UdpIoCleanIo (Instance
->UdpIo
);
921 if (Instance
->McastUdpIo
!= NULL
) {
923 Instance
->McastUdpIo
->UdpHandle
,
924 &gEfiUdp6ProtocolGuid
,
925 Instance
->McastUdpIo
->Image
,
928 UdpIoFreeIo (Instance
->McastUdpIo
);
929 Instance
->McastUdpIo
= NULL
;
933 // Clean up the stored last packet.
935 if (Instance
->LastPacket
!= NULL
) {
936 NetbufFree (Instance
->LastPacket
);
937 Instance
->LastPacket
= NULL
;
940 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Instance
->BlkList
) {
941 Block
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_BLOCK_RANGE
, Link
);
942 RemoveEntryList (Entry
);
947 // Reinitialize the corresponding fields of the Mtftp6 operation.
949 ZeroMem (&Instance
->ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
950 ZeroMem (&Instance
->ServerIp
, sizeof (EFI_IPv6_ADDRESS
));
951 ZeroMem (&Instance
->McastIp
, sizeof (EFI_IPv6_ADDRESS
));
953 Instance
->ServerCmdPort
= 0;
954 Instance
->ServerDataPort
= 0;
955 Instance
->McastPort
= 0;
956 Instance
->BlkSize
= 0;
957 Instance
->Operation
= 0;
958 Instance
->WindowSize
= 1;
959 Instance
->TotalBlock
= 0;
960 Instance
->AckedBlock
= 0;
961 Instance
->LastBlk
= 0;
962 Instance
->PacketToLive
= 0;
963 Instance
->MaxRetry
= 0;
964 Instance
->CurRetry
= 0;
965 Instance
->Timeout
= 0;
966 Instance
->IsMaster
= TRUE
;
970 Start the Mtftp6 instance to perform the operation, such as read file,
971 write file, and read directory.
973 @param[in] This The MTFTP session.
974 @param[in] Token The token than encapsules the user's request.
975 @param[in] OpCode The operation to perform.
977 @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.
978 @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.
979 @retval EFI_ALREADY_STARTED There is pending operation for the session.
980 @retval EFI_SUCCESS The operation is successfully started.
984 Mtftp6OperationStart (
985 IN EFI_MTFTP6_PROTOCOL
*This
,
986 IN EFI_MTFTP6_TOKEN
*Token
,
990 MTFTP6_INSTANCE
*Instance
;
993 if ((This
== NULL
) ||
995 (Token
->Filename
== NULL
) ||
996 ((Token
->OptionCount
!= 0) && (Token
->OptionList
== NULL
)) ||
997 ((Token
->OverrideData
!= NULL
) && !NetIp6IsValidUnicast (&Token
->OverrideData
->ServerIp
))
1000 return EFI_INVALID_PARAMETER
;
1004 // At least define one method to collect the data for download.
1006 if (((OpCode
== EFI_MTFTP6_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP6_OPCODE_DIR
)) &&
1007 (Token
->Buffer
== NULL
) &&
1008 (Token
->CheckPacket
== NULL
)
1011 return EFI_INVALID_PARAMETER
;
1015 // At least define one method to provide the data for upload.
1017 if ((OpCode
== EFI_MTFTP6_OPCODE_WRQ
) && (Token
->Buffer
== NULL
) && (Token
->PacketNeeded
== NULL
)) {
1018 return EFI_INVALID_PARAMETER
;
1021 Instance
= MTFTP6_INSTANCE_FROM_THIS (This
);
1023 if (Instance
->Config
== NULL
) {
1024 return EFI_NOT_STARTED
;
1027 if (Instance
->Token
!= NULL
) {
1028 return EFI_ACCESS_DENIED
;
1031 Status
= EFI_SUCCESS
;
1032 Instance
->OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1034 Instance
->Operation
= OpCode
;
1037 // Parse the extension options in the request packet.
1039 if (Token
->OptionCount
!= 0) {
1040 Status
= Mtftp6ParseExtensionOption (
1044 Instance
->Operation
,
1048 if (EFI_ERROR (Status
)) {
1054 // Initialize runtime data from config data or override data.
1056 Instance
->Token
= Token
;
1057 Instance
->ServerCmdPort
= Instance
->Config
->InitialServerPort
;
1058 Instance
->ServerDataPort
= 0;
1059 Instance
->MaxRetry
= Instance
->Config
->TryCount
;
1060 Instance
->Timeout
= Instance
->Config
->TimeoutValue
;
1061 Instance
->IsMaster
= TRUE
;
1064 &Instance
->ServerIp
,
1065 &Instance
->Config
->ServerIp
,
1066 sizeof (EFI_IPv6_ADDRESS
)
1069 if (Token
->OverrideData
!= NULL
) {
1070 Instance
->ServerCmdPort
= Token
->OverrideData
->ServerPort
;
1071 Instance
->MaxRetry
= Token
->OverrideData
->TryCount
;
1072 Instance
->Timeout
= Token
->OverrideData
->TimeoutValue
;
1075 &Instance
->ServerIp
,
1076 &Token
->OverrideData
->ServerIp
,
1077 sizeof (EFI_IPv6_ADDRESS
)
1082 // Set default value for undefined parameters.
1084 if (Instance
->ServerCmdPort
== 0) {
1085 Instance
->ServerCmdPort
= MTFTP6_DEFAULT_SERVER_CMD_PORT
;
1088 if (Instance
->BlkSize
== 0) {
1089 Instance
->BlkSize
= MTFTP6_DEFAULT_BLK_SIZE
;
1092 if (Instance
->WindowSize
== 0) {
1093 Instance
->WindowSize
= MTFTP6_DEFAULT_WINDOWSIZE
;
1096 if (Instance
->MaxRetry
== 0) {
1097 Instance
->MaxRetry
= MTFTP6_DEFAULT_MAX_RETRY
;
1100 if (Instance
->Timeout
== 0) {
1101 Instance
->Timeout
= MTFTP6_DEFAULT_TIMEOUT
;
1104 Token
->Status
= EFI_NOT_READY
;
1107 // Switch the routines by the operation code.
1110 case EFI_MTFTP6_OPCODE_RRQ
:
1111 Status
= Mtftp6RrqStart (Instance
, OpCode
);
1114 case EFI_MTFTP6_OPCODE_DIR
:
1115 Status
= Mtftp6RrqStart (Instance
, OpCode
);
1118 case EFI_MTFTP6_OPCODE_WRQ
:
1119 Status
= Mtftp6WrqStart (Instance
, OpCode
);
1123 Status
= EFI_DEVICE_ERROR
;
1127 if (EFI_ERROR (Status
)) {
1132 // Return immediately for asynchronous or poll the instance for synchronous.
1134 gBS
->RestoreTPL (Instance
->OldTpl
);
1136 if (Token
->Event
== NULL
) {
1137 while (Token
->Status
== EFI_NOT_READY
) {
1141 return Token
->Status
;
1148 Mtftp6OperationClean (Instance
, Status
);
1149 gBS
->RestoreTPL (Instance
->OldTpl
);
1155 The timer ticking routine for the Mtftp6 instance.
1157 @param[in] Event The pointer to the ticking event.
1158 @param[in] Context The pointer to the context.
1168 MTFTP6_SERVICE
*Service
;
1169 MTFTP6_INSTANCE
*Instance
;
1172 EFI_MTFTP6_TOKEN
*Token
;
1175 Service
= (MTFTP6_SERVICE
*)Context
;
1178 // Iterate through all the children of the Mtftp service instance. Time
1179 // out the packet. If maximum retries reached, clean the session up.
1181 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Service
->Children
) {
1182 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP6_INSTANCE
, Link
);
1184 if (Instance
->Token
== NULL
) {
1188 if (Instance
->PacketToLive
> 0) {
1189 Instance
->PacketToLive
--;
1193 Instance
->CurRetry
++;
1194 Token
= Instance
->Token
;
1196 if (Token
->TimeoutCallback
!= NULL
) {
1198 // Call the timeout callback routine if has.
1200 Status
= Token
->TimeoutCallback (&Instance
->Mtftp6
, Token
);
1202 if (EFI_ERROR (Status
)) {
1205 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
1206 (UINT8
*)"User aborted the transfer in time out"
1208 Mtftp6OperationClean (Instance
, EFI_ABORTED
);
1214 // Retransmit the packet if haven't reach the maximum retry count,
1215 // otherwise exit the transfer.
1217 if (Instance
->CurRetry
< Instance
->MaxRetry
) {
1218 Mtftp6TransmitPacket (Instance
, Instance
->LastPacket
);
1220 Mtftp6OperationClean (Instance
, EFI_TIMEOUT
);