2 Support routines for Mtftp.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Mtftp4Impl.h"
19 Allocate a MTFTP4 block range, then init it to the range of [Start, End]
21 @param Start The start block number
22 @param End The last block number in the range
24 @return Pointer to the created block range, NULL if failed to allocate memory.
33 MTFTP4_BLOCK_RANGE
*Range
;
35 Range
= AllocateZeroPool (sizeof (MTFTP4_BLOCK_RANGE
));
41 InitializeListHead (&Range
->Link
);
51 Initialize the block range for either RRQ or WRQ.
53 RRQ and WRQ have different requirements for Start and End.
54 For example, during start up, WRQ initializes its whole valid block range
55 to [0, 0xffff]. This is bacause the server will send us a ACK0 to inform us
56 to start the upload. When the client received ACK0, it will remove 0 from the
57 range, get the next block number, which is 1, then upload the BLOCK1. For RRQ
58 without option negotiation, the server will directly send us the BLOCK1 in
59 response to the client's RRQ. When received BLOCK1, the client will remove
60 it from the block range and send an ACK. It also works if there is option
63 @param Head The block range head to initialize
64 @param Start The Start block number.
65 @param End The last block number.
67 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range
68 @retval EFI_SUCCESS The initial block range is created.
72 Mtftp4InitBlockRange (
78 MTFTP4_BLOCK_RANGE
*Range
;
80 Range
= Mtftp4AllocateRange (Start
, End
);
83 return EFI_OUT_OF_RESOURCES
;
86 InsertTailList (Head
, &Range
->Link
);
92 Get the first valid block number on the range list.
94 @param Head The block range head
96 @return The first valid block number, -1 if the block range is empty.
100 Mtftp4GetNextBlockNum (
104 MTFTP4_BLOCK_RANGE
*Range
;
106 if (IsListEmpty (Head
)) {
110 Range
= NET_LIST_HEAD (Head
, MTFTP4_BLOCK_RANGE
, Link
);
116 Set the last block number of the block range list.
118 It will remove all the blocks after the Last. MTFTP initialize the block range
119 to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the
120 last block number, it will call this function to set the last block number.
122 @param Head The block range list
123 @param Last The last block number
127 Mtftp4SetLastBlockNum (
132 MTFTP4_BLOCK_RANGE
*Range
;
135 // Iterate from the tail to head to remove the block number
138 while (!IsListEmpty (Head
)) {
139 Range
= NET_LIST_TAIL (Head
, MTFTP4_BLOCK_RANGE
, Link
);
141 if (Range
->Start
> Last
) {
142 RemoveEntryList (&Range
->Link
);
147 if (Range
->End
> Last
) {
157 Remove the block number from the block range list.
159 @param Head The block range list to remove from
160 @param Num The block number to remove
161 @param Completed Whether Num is the last block number
162 @param TotalBlock The continuous block number in all
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 resource
170 Mtftp4RemoveBlockNum (
173 IN BOOLEAN Completed
,
174 OUT UINT64
*TotalBlock
177 MTFTP4_BLOCK_RANGE
*Range
;
178 MTFTP4_BLOCK_RANGE
*NewRange
;
181 NET_LIST_FOR_EACH (Entry
, Head
) {
184 // Each block represents a hole [Start, End] in the file,
185 // skip to the first range with End >= Num
187 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_BLOCK_RANGE
, Link
);
189 if (Range
->End
< Num
) {
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.
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.
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].
209 if (Range
->Start
> Num
) {
210 return EFI_NOT_FOUND
;
212 } else if (Range
->Start
== Num
) {
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
225 if (Range
->Round
> 0) {
226 *TotalBlock
+= Range
->Bound
+ MultU64x32 ((UINTN
) (Range
->Round
-1), (UINT32
) (Range
->Bound
+ 1)) + 1;
229 if (Range
->Start
> Range
->Bound
) {
234 if ((Range
->Start
> Range
->End
) || Completed
) {
235 RemoveEntryList (&Range
->Link
);
242 if (Range
->End
== Num
) {
245 NewRange
= Mtftp4AllocateRange ((UINT16
) (Num
+ 1), (UINT16
) Range
->End
);
247 if (NewRange
== NULL
) {
248 return EFI_OUT_OF_RESOURCES
;
251 Range
->End
= Num
- 1;
252 NetListInsertAfter (&Range
->Link
, &NewRange
->Link
);
259 return EFI_NOT_FOUND
;
264 Build then transmit the request packet for the MTFTP session.
266 @param Instance The Mtftp session
268 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
269 @retval EFI_SUCCESS The request is built and sent
270 @retval Others Failed to transmit the packet.
275 IN MTFTP4_PROTOCOL
*Instance
278 EFI_MTFTP4_PACKET
*Packet
;
279 EFI_MTFTP4_OPTION
*Options
;
280 EFI_MTFTP4_TOKEN
*Token
;
281 RETURN_STATUS Status
;
287 UINTN FileNameLength
;
289 UINTN OptionStrLength
;
290 UINTN ValueStrLength
;
292 Token
= Instance
->Token
;
293 Options
= Token
->OptionList
;
294 Mode
= Instance
->Token
->ModeStr
;
297 Mode
= (UINT8
*) "octet";
301 // Compute the packet length
303 FileNameLength
= AsciiStrLen ((CHAR8
*) Token
->Filename
);
304 ModeLength
= AsciiStrLen ((CHAR8
*) Mode
);
305 BufferLength
= (UINT32
) FileNameLength
+ (UINT32
) ModeLength
+ 4;
307 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
308 OptionStrLength
= AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
);
309 ValueStrLength
= AsciiStrLen ((CHAR8
*) Options
[Index
].ValueStr
);
310 BufferLength
+= (UINT32
) OptionStrLength
+ (UINT32
) ValueStrLength
+ 2;
313 // Allocate a packet then copy the data over
315 if ((Nbuf
= NetbufAlloc (BufferLength
)) == NULL
) {
316 return EFI_OUT_OF_RESOURCES
;
319 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Nbuf
, BufferLength
, FALSE
);
320 ASSERT (Packet
!= NULL
);
322 Packet
->OpCode
= HTONS (Instance
->Operation
);
323 BufferLength
-= sizeof (Packet
->OpCode
);
325 Cur
= Packet
->Rrq
.Filename
;
326 Status
= AsciiStrCpyS ((CHAR8
*) Cur
, BufferLength
, (CHAR8
*) Token
->Filename
);
327 ASSERT_EFI_ERROR (Status
);
328 BufferLength
-= (UINT32
) (FileNameLength
+ 1);
329 Cur
+= FileNameLength
+ 1;
330 Status
= AsciiStrCpyS ((CHAR8
*) Cur
, BufferLength
, (CHAR8
*) Mode
);
331 ASSERT_EFI_ERROR (Status
);
332 BufferLength
-= (UINT32
) (ModeLength
+ 1);
333 Cur
+= ModeLength
+ 1;
335 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
336 OptionStrLength
= AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
);
337 ValueStrLength
= AsciiStrLen ((CHAR8
*) Options
[Index
].ValueStr
);
339 Status
= AsciiStrCpyS ((CHAR8
*) Cur
, BufferLength
, (CHAR8
*) Options
[Index
].OptionStr
);
340 ASSERT_EFI_ERROR (Status
);
341 BufferLength
-= (UINT32
) (OptionStrLength
+ 1);
342 Cur
+= OptionStrLength
+ 1;
344 Status
= AsciiStrCpyS ((CHAR8
*) Cur
, BufferLength
, (CHAR8
*) Options
[Index
].ValueStr
);
345 ASSERT_EFI_ERROR (Status
);
346 BufferLength
-= (UINT32
) (ValueStrLength
+ 1);
347 Cur
+= ValueStrLength
+ 1;
351 return Mtftp4SendPacket (Instance
, Nbuf
);
356 Build then send an error message.
358 @param Instance The MTFTP session
359 @param ErrCode The error code
360 @param ErrInfo The error message
362 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
363 @retval EFI_SUCCESS The error packet is transmitted.
364 @retval Others Failed to transmit the packet.
369 IN MTFTP4_PROTOCOL
*Instance
,
375 EFI_MTFTP4_PACKET
*TftpError
;
378 Len
= (UINT32
) (AsciiStrLen ((CHAR8
*) ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
379 Packet
= NetbufAlloc (Len
);
380 if (Packet
== NULL
) {
381 return EFI_OUT_OF_RESOURCES
;
384 TftpError
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
385 ASSERT (TftpError
!= NULL
);
387 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
388 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
390 AsciiStrCpyS ((CHAR8
*) TftpError
->Error
.ErrorMessage
, Len
, (CHAR8
*) ErrInfo
);
392 return Mtftp4SendPacket (Instance
, Packet
);
397 The callback function called when the packet is transmitted.
399 It simply frees the packet.
401 @param Packet The transmitted (or failed to) packet
402 @param EndPoint The local and remote UDP access point
403 @param IoStatus The result of the transmission
404 @param Context Opaque parameter to the callback
411 IN UDP_END_POINT
*EndPoint
,
412 IN EFI_STATUS IoStatus
,
421 Set the timeout for the instance. User a longer time for passive instances.
423 @param Instance The Mtftp session to set time out
428 IN OUT MTFTP4_PROTOCOL
*Instance
431 if (Instance
->Master
) {
432 Instance
->PacketToLive
= Instance
->Timeout
;
434 Instance
->PacketToLive
= Instance
->Timeout
* 2;
440 Send the packet for the instance.
442 It will first save a reference to the packet for later retransmission.
443 Then determine the destination port, listen port for requests, and connected
444 port for others. At last, send the packet out.
446 @param Instance The Mtftp instance
447 @param Packet The packet to send
449 @retval EFI_SUCCESS The packet is sent out
450 @retval Others Failed to transmit the packet.
455 IN OUT MTFTP4_PROTOCOL
*Instance
,
456 IN OUT NET_BUF
*Packet
459 UDP_END_POINT UdpPoint
;
465 // Save the packet for retransmission
467 if (Instance
->LastPacket
!= NULL
) {
468 NetbufFree (Instance
->LastPacket
);
471 Instance
->LastPacket
= Packet
;
473 Instance
->CurRetry
= 0;
474 Mtftp4SetTimeout (Instance
);
476 ZeroMem (&UdpPoint
, sizeof (UdpPoint
));
477 UdpPoint
.RemoteAddr
.Addr
[0] = Instance
->ServerIp
;
480 // Send the requests to the listening port, other packets
481 // to the connected port
483 Buffer
= NetbufGetByte (Packet
, 0, NULL
);
484 ASSERT (Buffer
!= NULL
);
485 OpCode
= NTOHS (*(UINT16
*)Buffer
);
487 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) ||
488 (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
489 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
490 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
492 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
495 NET_GET_REF (Packet
);
497 Status
= UdpIoSendDatagram (
498 Instance
->UnicastPort
,
506 if (EFI_ERROR (Status
)) {
507 NET_PUT_REF (Packet
);
515 Retransmit the last packet for the instance.
517 @param Instance The Mtftp instance
519 @retval EFI_SUCCESS The last packet is retransmitted.
520 @retval Others Failed to retransmit.
525 IN MTFTP4_PROTOCOL
*Instance
528 UDP_END_POINT UdpPoint
;
533 ASSERT (Instance
->LastPacket
!= NULL
);
535 ZeroMem (&UdpPoint
, sizeof (UdpPoint
));
536 UdpPoint
.RemoteAddr
.Addr
[0] = Instance
->ServerIp
;
539 // Set the requests to the listening port, other packets to the connected port
541 Buffer
= NetbufGetByte (Instance
->LastPacket
, 0, NULL
);
542 ASSERT (Buffer
!= NULL
);
543 OpCode
= NTOHS (*(UINT16
*) Buffer
);
545 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
546 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
547 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
549 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
552 NET_GET_REF (Instance
->LastPacket
);
554 Status
= UdpIoSendDatagram (
555 Instance
->UnicastPort
,
556 Instance
->LastPacket
,
563 if (EFI_ERROR (Status
)) {
564 NET_PUT_REF (Instance
->LastPacket
);
572 The timer ticking function in TPL_NOTIFY level for the Mtftp service instance.
574 @param Event The ticking event
575 @param Context The Mtftp service instance
580 Mtftp4OnTimerTickNotifyLevel (
585 MTFTP4_SERVICE
*MtftpSb
;
588 MTFTP4_PROTOCOL
*Instance
;
590 MtftpSb
= (MTFTP4_SERVICE
*) Context
;
593 // Iterate through all the children of the Mtftp service instance. Time
594 // out the current packet transmit.
596 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
597 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
598 if ((Instance
->PacketToLive
== 0) || (--Instance
->PacketToLive
> 0)) {
599 Instance
->HasTimeout
= FALSE
;
601 Instance
->HasTimeout
= TRUE
;
608 The timer ticking function for the Mtftp service instance.
610 @param Event The ticking event
611 @param Context The Mtftp service instance
621 MTFTP4_SERVICE
*MtftpSb
;
624 MTFTP4_PROTOCOL
*Instance
;
625 EFI_MTFTP4_TOKEN
*Token
;
627 MtftpSb
= (MTFTP4_SERVICE
*) Context
;
630 // Iterate through all the children of the Mtftp service instance.
632 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
633 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
634 if (!Instance
->HasTimeout
) {
638 Instance
->HasTimeout
= FALSE
;
641 // Call the user's time out handler
643 Token
= Instance
->Token
;
645 if (Token
!= NULL
&& Token
->TimeoutCallback
!= NULL
&&
646 EFI_ERROR (Token
->TimeoutCallback (&Instance
->Mtftp4
, Token
))) {
649 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
650 (UINT8
*) "User aborted the transfer in time out"
653 Mtftp4CleanOperation (Instance
, EFI_ABORTED
);
658 // Retransmit the packet if haven't reach the maxmium retry count,
659 // otherwise exit the transfer.
661 if (++Instance
->CurRetry
< Instance
->MaxRetry
) {
662 Mtftp4Retransmit (Instance
);
663 Mtftp4SetTimeout (Instance
);
665 Mtftp4CleanOperation (Instance
, EFI_TIMEOUT
);