2 Support routines for Mtftp.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "Mtftp4Impl.h"
12 Allocate a MTFTP4 block range, then init it to the range of [Start, End]
14 @param Start The start block number
15 @param End The last block number in the range
17 @return Pointer to the created block range, NULL if failed to allocate memory.
26 MTFTP4_BLOCK_RANGE
*Range
;
28 Range
= AllocateZeroPool (sizeof (MTFTP4_BLOCK_RANGE
));
34 InitializeListHead (&Range
->Link
);
43 Initialize the block range for either RRQ or WRQ.
45 RRQ and WRQ have different requirements for Start and End.
46 For example, during start up, WRQ initializes its whole valid block range
47 to [0, 0xffff]. This is because the server will send us a ACK0 to inform us
48 to start the upload. When the client received ACK0, it will remove 0 from the
49 range, get the next block number, which is 1, then upload the BLOCK1. For RRQ
50 without option negotiation, the server will directly send us the BLOCK1 in
51 response to the client's RRQ. When received BLOCK1, the client will remove
52 it from the block range and send an ACK. It also works if there is option
55 @param Head The block range head to initialize
56 @param Start The Start block number.
57 @param 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 Mtftp4InitBlockRange (
70 MTFTP4_BLOCK_RANGE
*Range
;
72 Range
= Mtftp4AllocateRange (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 Head The block range head
87 @return The first valid block number, -1 if the block range is empty.
91 Mtftp4GetNextBlockNum (
95 MTFTP4_BLOCK_RANGE
*Range
;
97 if (IsListEmpty (Head
)) {
101 Range
= NET_LIST_HEAD (Head
, MTFTP4_BLOCK_RANGE
, Link
);
106 Set the last block number of the block range list.
108 It will remove all the blocks after the Last. MTFTP initialize the block range
109 to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the
110 last block number, it will call this function to set the last block number.
112 @param Head The block range list
113 @param Last The last block number
117 Mtftp4SetLastBlockNum (
122 MTFTP4_BLOCK_RANGE
*Range
;
125 // Iterate from the tail to head to remove the block number
128 while (!IsListEmpty (Head
)) {
129 Range
= NET_LIST_TAIL (Head
, MTFTP4_BLOCK_RANGE
, Link
);
131 if (Range
->Start
> Last
) {
132 RemoveEntryList (&Range
->Link
);
137 if (Range
->End
> Last
) {
146 Remove the block number from the block range list.
148 @param Head The block range list to remove from
149 @param Num The block number to remove
150 @param Completed Whether Num is the last block number.
151 @param BlockCounter The continuous block counter instead of the value after roll-over.
153 @retval EFI_NOT_FOUND The block number isn't in the block range list
154 @retval EFI_SUCCESS The block number has been removed from the list
155 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource
159 Mtftp4RemoveBlockNum (
162 IN BOOLEAN Completed
,
163 OUT UINT64
*BlockCounter
166 MTFTP4_BLOCK_RANGE
*Range
;
167 MTFTP4_BLOCK_RANGE
*NewRange
;
170 NET_LIST_FOR_EACH (Entry
, Head
) {
172 // Each block represents a hole [Start, End] in the file,
173 // skip to the first range with End >= Num
175 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_BLOCK_RANGE
, Link
);
177 if (Range
->End
< Num
) {
182 // There are three different cases for Start
183 // 1. (Start > Num) && (End >= Num):
184 // because all the holes before this one has the condition of
185 // End < Num, so this block number has been removed.
187 // 2. (Start == Num) && (End >= Num):
188 // Need to increase the Start by one, and if End == Num, this
189 // hole has been removed completely, remove it.
191 // 3. (Start < Num) && (End >= Num):
192 // if End == Num, only need to decrease the End by one because
193 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
194 // if (End > Num), the hold is split into two holes, with
195 // [Start, Num - 1] and [Num + 1, End].
197 if (Range
->Start
> Num
) {
198 return EFI_NOT_FOUND
;
199 } else if (Range
->Start
== Num
) {
203 // Note that: RFC 1350 does not mention block counter roll-over,
204 // but several TFTP hosts implement the roll-over be able to accept
205 // transfers of unlimited size. There is no consensus, however, whether
206 // the counter should wrap around to zero or to one. Many implementations
207 // wrap to zero, because this is the simplest to implement. Here we choose
212 if (Range
->Round
> 0) {
213 *BlockCounter
+= Range
->Bound
+ MultU64x32 ((UINTN
)(Range
->Round
-1), (UINT32
)(Range
->Bound
+ 1)) + 1;
216 if (Range
->Start
> Range
->Bound
) {
221 if ((Range
->Start
> Range
->End
) || Completed
) {
222 RemoveEntryList (&Range
->Link
);
228 if (Range
->End
== Num
) {
231 NewRange
= Mtftp4AllocateRange ((UINT16
)(Num
+ 1), (UINT16
)Range
->End
);
233 if (NewRange
== NULL
) {
234 return EFI_OUT_OF_RESOURCES
;
237 Range
->End
= Num
- 1;
238 NetListInsertAfter (&Range
->Link
, &NewRange
->Link
);
245 return EFI_NOT_FOUND
;
249 Build then transmit the request packet for the MTFTP session.
251 @param Instance The Mtftp session
253 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
254 @retval EFI_SUCCESS The request is built and sent
255 @retval Others Failed to transmit the packet.
260 IN MTFTP4_PROTOCOL
*Instance
263 EFI_MTFTP4_PACKET
*Packet
;
264 EFI_MTFTP4_OPTION
*Options
;
265 EFI_MTFTP4_TOKEN
*Token
;
266 RETURN_STATUS Status
;
272 UINTN FileNameLength
;
274 UINTN OptionStrLength
;
275 UINTN ValueStrLength
;
277 Token
= Instance
->Token
;
278 Options
= Token
->OptionList
;
279 Mode
= Instance
->Token
->ModeStr
;
282 Mode
= (UINT8
*)"octet";
286 // Compute the packet length
288 FileNameLength
= AsciiStrLen ((CHAR8
*)Token
->Filename
);
289 ModeLength
= AsciiStrLen ((CHAR8
*)Mode
);
290 BufferLength
= (UINT32
)FileNameLength
+ (UINT32
)ModeLength
+ 4;
292 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
293 OptionStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].OptionStr
);
294 ValueStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].ValueStr
);
295 BufferLength
+= (UINT32
)OptionStrLength
+ (UINT32
)ValueStrLength
+ 2;
299 // Allocate a packet then copy the data over
301 if ((Nbuf
= NetbufAlloc (BufferLength
)) == NULL
) {
302 return EFI_OUT_OF_RESOURCES
;
305 Packet
= (EFI_MTFTP4_PACKET
*)NetbufAllocSpace (Nbuf
, BufferLength
, FALSE
);
306 ASSERT (Packet
!= NULL
);
308 Packet
->OpCode
= HTONS (Instance
->Operation
);
309 BufferLength
-= sizeof (Packet
->OpCode
);
311 Cur
= Packet
->Rrq
.Filename
;
312 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Token
->Filename
);
313 ASSERT_EFI_ERROR (Status
);
314 BufferLength
-= (UINT32
)(FileNameLength
+ 1);
315 Cur
+= FileNameLength
+ 1;
316 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Mode
);
317 ASSERT_EFI_ERROR (Status
);
318 BufferLength
-= (UINT32
)(ModeLength
+ 1);
319 Cur
+= ModeLength
+ 1;
321 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
322 OptionStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].OptionStr
);
323 ValueStrLength
= AsciiStrLen ((CHAR8
*)Options
[Index
].ValueStr
);
325 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Options
[Index
].OptionStr
);
326 ASSERT_EFI_ERROR (Status
);
327 BufferLength
-= (UINT32
)(OptionStrLength
+ 1);
328 Cur
+= OptionStrLength
+ 1;
330 Status
= AsciiStrCpyS ((CHAR8
*)Cur
, BufferLength
, (CHAR8
*)Options
[Index
].ValueStr
);
331 ASSERT_EFI_ERROR (Status
);
332 BufferLength
-= (UINT32
)(ValueStrLength
+ 1);
333 Cur
+= ValueStrLength
+ 1;
336 return Mtftp4SendPacket (Instance
, Nbuf
);
340 Build then send an error message.
342 @param Instance The MTFTP session
343 @param ErrCode The error code
344 @param ErrInfo The error message
346 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
347 @retval EFI_SUCCESS The error packet is transmitted.
348 @retval Others Failed to transmit the packet.
353 IN MTFTP4_PROTOCOL
*Instance
,
359 EFI_MTFTP4_PACKET
*TftpError
;
362 Len
= (UINT32
)(AsciiStrLen ((CHAR8
*)ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
363 Packet
= NetbufAlloc (Len
);
364 if (Packet
== NULL
) {
365 return EFI_OUT_OF_RESOURCES
;
368 TftpError
= (EFI_MTFTP4_PACKET
*)NetbufAllocSpace (Packet
, Len
, FALSE
);
369 ASSERT (TftpError
!= NULL
);
371 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
372 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
374 AsciiStrCpyS ((CHAR8
*)TftpError
->Error
.ErrorMessage
, Len
, (CHAR8
*)ErrInfo
);
376 return Mtftp4SendPacket (Instance
, Packet
);
380 The callback function called when the packet is transmitted.
382 It simply frees the packet.
384 @param Packet The transmitted (or failed to) packet
385 @param EndPoint The local and remote UDP access point
386 @param IoStatus The result of the transmission
387 @param Context Opaque parameter to the callback
394 IN UDP_END_POINT
*EndPoint
,
395 IN EFI_STATUS IoStatus
,
403 Set the timeout for the instance. User a longer time for passive instances.
405 @param Instance The Mtftp session to set time out
410 IN OUT MTFTP4_PROTOCOL
*Instance
413 if (Instance
->Master
) {
414 Instance
->PacketToLive
= Instance
->Timeout
;
416 Instance
->PacketToLive
= Instance
->Timeout
* 2;
421 Send the packet for the instance.
423 It will first save a reference to the packet for later retransmission.
424 Then determine the destination port, listen port for requests, and connected
425 port for others. At last, send the packet out.
427 @param Instance The Mtftp instance
428 @param Packet The packet to send
430 @retval EFI_SUCCESS The packet is sent out
431 @retval Others Failed to transmit the packet.
436 IN OUT MTFTP4_PROTOCOL
*Instance
,
437 IN OUT NET_BUF
*Packet
440 UDP_END_POINT UdpPoint
;
446 // Save the packet for retransmission
448 if (Instance
->LastPacket
!= NULL
) {
449 NetbufFree (Instance
->LastPacket
);
452 Instance
->LastPacket
= Packet
;
454 Instance
->CurRetry
= 0;
455 Mtftp4SetTimeout (Instance
);
457 ZeroMem (&UdpPoint
, sizeof (UdpPoint
));
458 UdpPoint
.RemoteAddr
.Addr
[0] = Instance
->ServerIp
;
461 // Send the requests to the listening port, other packets
462 // to the connected port
464 Buffer
= NetbufGetByte (Packet
, 0, NULL
);
465 ASSERT (Buffer
!= NULL
);
466 OpCode
= NTOHS (*(UINT16
*)Buffer
);
468 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) ||
469 (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
470 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
))
472 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
474 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
477 NET_GET_REF (Packet
);
479 Status
= UdpIoSendDatagram (
480 Instance
->UnicastPort
,
488 if (EFI_ERROR (Status
)) {
489 NET_PUT_REF (Packet
);
496 Retransmit the last packet for the instance.
498 @param Instance The Mtftp instance
500 @retval EFI_SUCCESS The last packet is retransmitted.
501 @retval Others Failed to retransmit.
506 IN MTFTP4_PROTOCOL
*Instance
509 UDP_END_POINT UdpPoint
;
514 ASSERT (Instance
->LastPacket
!= NULL
);
516 ZeroMem (&UdpPoint
, sizeof (UdpPoint
));
517 UdpPoint
.RemoteAddr
.Addr
[0] = Instance
->ServerIp
;
520 // Set the requests to the listening port, other packets to the connected port
522 Buffer
= NetbufGetByte (Instance
->LastPacket
, 0, NULL
);
523 ASSERT (Buffer
!= NULL
);
524 OpCode
= NTOHS (*(UINT16
*)Buffer
);
526 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
527 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
))
529 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
531 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
534 NET_GET_REF (Instance
->LastPacket
);
536 Status
= UdpIoSendDatagram (
537 Instance
->UnicastPort
,
538 Instance
->LastPacket
,
545 if (EFI_ERROR (Status
)) {
546 NET_PUT_REF (Instance
->LastPacket
);
553 The timer ticking function in TPL_NOTIFY level for the Mtftp service instance.
555 @param Event The ticking event
556 @param Context The Mtftp service instance
561 Mtftp4OnTimerTickNotifyLevel (
566 MTFTP4_SERVICE
*MtftpSb
;
569 MTFTP4_PROTOCOL
*Instance
;
571 MtftpSb
= (MTFTP4_SERVICE
*)Context
;
574 // Iterate through all the children of the Mtftp service instance. Time
575 // out the current packet transmit.
577 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
578 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
579 if ((Instance
->PacketToLive
== 0) || (--Instance
->PacketToLive
> 0)) {
580 Instance
->HasTimeout
= FALSE
;
582 Instance
->HasTimeout
= TRUE
;
588 The timer ticking function for the Mtftp service instance.
590 @param Event The ticking event
591 @param Context The Mtftp service instance
601 MTFTP4_SERVICE
*MtftpSb
;
604 MTFTP4_PROTOCOL
*Instance
;
605 EFI_MTFTP4_TOKEN
*Token
;
607 MtftpSb
= (MTFTP4_SERVICE
*)Context
;
610 // Iterate through all the children of the Mtftp service instance.
612 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
613 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
614 if (!Instance
->HasTimeout
) {
618 Instance
->HasTimeout
= FALSE
;
621 // Call the user's time out handler
623 Token
= Instance
->Token
;
625 if ((Token
!= NULL
) && (Token
->TimeoutCallback
!= NULL
) &&
626 EFI_ERROR (Token
->TimeoutCallback (&Instance
->Mtftp4
, Token
)))
630 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
631 (UINT8
*)"User aborted the transfer in time out"
634 Mtftp4CleanOperation (Instance
, EFI_ABORTED
);
639 // Retransmit the packet if haven't reach the maximum retry count,
640 // otherwise exit the transfer.
642 if (++Instance
->CurRetry
< Instance
->MaxRetry
) {
643 Mtftp4Retransmit (Instance
);
644 Mtftp4SetTimeout (Instance
);
646 Mtftp4CleanOperation (Instance
, EFI_TIMEOUT
);