2 Support routines for Mtftp.
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. 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
= AllocatePool (sizeof (MTFTP4_BLOCK_RANGE
));
41 InitializeListHead (&Range
->Link
);
50 Initialize the block range for either RRQ or WRQ.
52 RRQ and WRQ have different requirements for Start and End.
53 For example, during start up, WRQ initializes its whole valid block range
54 to [0, 0xffff]. This is bacause the server will send us a ACK0 to inform us
55 to start the upload. When the client received ACK0, it will remove 0 from the
56 range, get the next block number, which is 1, then upload the BLOCK1. For RRQ
57 without option negotiation, the server will directly send us the BLOCK1 in
58 response to the client's RRQ. When received BLOCK1, the client will remove
59 it from the block range and send an ACK. It also works if there is option
62 @param Head The block range head to initialize
63 @param Start The Start block number.
64 @param End The last block number.
66 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range
67 @retval EFI_SUCCESS The initial block range is created.
71 Mtftp4InitBlockRange (
77 MTFTP4_BLOCK_RANGE
*Range
;
79 Range
= Mtftp4AllocateRange (Start
, End
);
82 return EFI_OUT_OF_RESOURCES
;
85 InsertTailList (Head
, &Range
->Link
);
91 Get the first valid block number on the range list.
93 @param Head The block range head
95 @return The first valid block number, -1 if the block range is empty.
99 Mtftp4GetNextBlockNum (
103 MTFTP4_BLOCK_RANGE
*Range
;
105 if (IsListEmpty (Head
)) {
109 Range
= NET_LIST_HEAD (Head
, MTFTP4_BLOCK_RANGE
, Link
);
115 Set the last block number of the block range list.
117 It will remove all the blocks after the Last. MTFTP initialize the block range
118 to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the
119 last block number, it will call this function to set the last block number.
121 @param Head The block range list
122 @param Last The last block number
126 Mtftp4SetLastBlockNum (
131 MTFTP4_BLOCK_RANGE
*Range
;
134 // Iterate from the tail to head to remove the block number
137 while (!IsListEmpty (Head
)) {
138 Range
= NET_LIST_TAIL (Head
, MTFTP4_BLOCK_RANGE
, Link
);
140 if (Range
->Start
> Last
) {
141 RemoveEntryList (&Range
->Link
);
146 if (Range
->End
> Last
) {
156 Remove the block number from the block range list.
158 @param Head The block range list to remove from
159 @param Num The block number to remove
161 @retval EFI_NOT_FOUND The block number isn't in the block range list
162 @retval EFI_SUCCESS The block number has been removed from the list
163 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource
167 Mtftp4RemoveBlockNum (
172 MTFTP4_BLOCK_RANGE
*Range
;
173 MTFTP4_BLOCK_RANGE
*NewRange
;
176 NET_LIST_FOR_EACH (Entry
, Head
) {
179 // Each block represents a hole [Start, End] in the file,
180 // skip to the first range with End >= Num
182 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_BLOCK_RANGE
, Link
);
184 if (Range
->End
< Num
) {
189 // There are three different cases for Start
190 // 1. (Start > Num) && (End >= Num):
191 // because all the holes before this one has the condition of
192 // End < Num, so this block number has been removed.
194 // 2. (Start == Num) && (End >= Num):
195 // Need to increase the Start by one, and if End == Num, this
196 // hole has been removed completely, remove it.
198 // 3. (Start < Num) && (End >= Num):
199 // if End == Num, only need to decrease the End by one because
200 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
201 // if (End > Num), the hold is splited into two holes, with
202 // [Start, Num - 1] and [Num + 1, End].
204 if (Range
->Start
> Num
) {
205 return EFI_NOT_FOUND
;
207 } else if (Range
->Start
== Num
) {
210 if (Range
->Start
> Range
->End
) {
211 RemoveEntryList (&Range
->Link
);
218 if (Range
->End
== Num
) {
221 NewRange
= Mtftp4AllocateRange ((UINT16
) (Num
+ 1), (UINT16
) Range
->End
);
223 if (NewRange
== NULL
) {
224 return EFI_OUT_OF_RESOURCES
;
227 Range
->End
= Num
- 1;
228 NetListInsertAfter (&Range
->Link
, &NewRange
->Link
);
235 return EFI_NOT_FOUND
;
240 Build then transmit the request packet for the MTFTP session.
242 @param Instance The Mtftp session
244 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
245 @retval EFI_SUCCESS The request is built and sent
246 @retval Others Failed to transmit the packet.
251 IN MTFTP4_PROTOCOL
*Instance
254 EFI_MTFTP4_PACKET
*Packet
;
255 EFI_MTFTP4_OPTION
*Options
;
256 EFI_MTFTP4_TOKEN
*Token
;
265 Token
= Instance
->Token
;
266 Options
= Token
->OptionList
;
267 Mode
= Instance
->Token
->ModeStr
;
270 Mode
= (UINT8
*) "octet";
274 // Compute the packet length
276 Len1
= (UINT32
) AsciiStrLen ((CHAR8
*) Token
->Filename
);
277 Len2
= (UINT32
) AsciiStrLen ((CHAR8
*) Mode
);
278 Len
= (Len1
+ Len2
+ 4);
280 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
281 Len1
= (UINT32
) AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
);
282 Len2
= (UINT32
) AsciiStrLen ((CHAR8
*) Options
[Index
].ValueStr
);
283 Len
+= Len1
+ Len2
+ 2;
287 // Allocate a packet then copy the data over
289 if ((Nbuf
= NetbufAlloc (Len
)) == NULL
) {
290 return EFI_OUT_OF_RESOURCES
;
293 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Nbuf
, Len
, FALSE
);
294 ASSERT (Packet
!= NULL
);
296 Packet
->OpCode
= HTONS (Instance
->Operation
);
297 Cur
= Packet
->Rrq
.Filename
;
298 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Token
->Filename
);
299 Cur
+= AsciiStrLen ((CHAR8
*) Token
->Filename
) + 1;
300 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Mode
);
301 Cur
+= AsciiStrLen ((CHAR8
*) Mode
) + 1;
303 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
304 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].OptionStr
);
305 Cur
+= AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
) + 1;
307 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].ValueStr
);
308 Cur
+= AsciiStrLen ((CHAR8
*) (CHAR8
*) Options
[Index
].ValueStr
) + 1;
311 return Mtftp4SendPacket (Instance
, Nbuf
);
316 Build then send an error message.
318 @param Instance The MTFTP session
319 @param ErrCode The error code
320 @param ErrInfo The error message
322 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
323 @retval EFI_SUCCESS The error packet is transmitted.
324 @retval Others Failed to transmit the packet.
329 IN MTFTP4_PROTOCOL
*Instance
,
335 EFI_MTFTP4_PACKET
*TftpError
;
338 Len
= (UINT32
) (AsciiStrLen ((CHAR8
*) ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
339 Packet
= NetbufAlloc (Len
);
340 if (Packet
== NULL
) {
341 return EFI_OUT_OF_RESOURCES
;
344 TftpError
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
345 ASSERT (TftpError
!= NULL
);
347 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
348 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
350 AsciiStrCpy ((CHAR8
*) TftpError
->Error
.ErrorMessage
, (CHAR8
*) ErrInfo
);
352 return Mtftp4SendPacket (Instance
, Packet
);
357 The callback function called when the packet is transmitted.
359 It simply frees the packet.
361 @param Packet The transmitted (or failed to) packet
362 @param EndPoint The local and remote UDP access point
363 @param IoStatus The result of the transmission
364 @param Context Opaque parameter to the callback
370 IN UDP_END_POINT
*EndPoint
,
371 IN EFI_STATUS IoStatus
,
380 Set the timeout for the instance. User a longer time for passive instances.
382 @param Instance The Mtftp session to set time out
387 IN OUT MTFTP4_PROTOCOL
*Instance
390 if (Instance
->Master
) {
391 Instance
->PacketToLive
= Instance
->Timeout
;
393 Instance
->PacketToLive
= Instance
->Timeout
* 2;
399 Send the packet for the instance.
401 It will first save a reference to the packet for later retransmission.
402 Then determine the destination port, listen port for requests, and connected
403 port for others. At last, send the packet out.
405 @param Instance The Mtftp instance
406 @param Packet The packet to send
408 @retval EFI_SUCCESS The packet is sent out
409 @retval Others Failed to transmit the packet.
414 IN OUT MTFTP4_PROTOCOL
*Instance
,
415 IN OUT NET_BUF
*Packet
418 UDP_END_POINT UdpPoint
;
424 // Save the packet for retransmission
426 if (Instance
->LastPacket
!= NULL
) {
427 NetbufFree (Instance
->LastPacket
);
430 Instance
->LastPacket
= Packet
;
432 Instance
->CurRetry
= 0;
433 Mtftp4SetTimeout (Instance
);
435 ZeroMem (&UdpPoint
, sizeof (UdpPoint
));
436 UdpPoint
.RemoteAddr
.Addr
[0] = Instance
->ServerIp
;
439 // Send the requests to the listening port, other packets
440 // to the connected port
442 Value
= *((UINT16
*) NetbufGetByte (Packet
, 0, NULL
));
443 OpCode
= NTOHS (Value
);
445 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) ||
446 (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
447 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
448 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
450 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
453 NET_GET_REF (Packet
);
455 Status
= UdpIoSendDatagram (
456 Instance
->UnicastPort
,
464 if (EFI_ERROR (Status
)) {
465 NET_PUT_REF (Packet
);
473 Retransmit the last packet for the instance.
475 @param Instance The Mtftp instance
477 @retval EFI_SUCCESS The last packet is retransmitted.
478 @retval Others Failed to retransmit.
483 IN MTFTP4_PROTOCOL
*Instance
486 UDP_END_POINT UdpPoint
;
491 ASSERT (Instance
->LastPacket
!= NULL
);
493 ZeroMem (&UdpPoint
, sizeof (UdpPoint
));
494 UdpPoint
.RemoteAddr
.Addr
[0] = Instance
->ServerIp
;
497 // Set the requests to the listening port, other packets to the connected port
499 Value
= *(UINT16
*) NetbufGetByte (Instance
->LastPacket
, 0, NULL
);
500 OpCode
= NTOHS (Value
);
502 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
503 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
504 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
506 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
509 NET_GET_REF (Instance
->LastPacket
);
511 Status
= UdpIoSendDatagram (
512 Instance
->UnicastPort
,
513 Instance
->LastPacket
,
520 if (EFI_ERROR (Status
)) {
521 NET_PUT_REF (Instance
->LastPacket
);
529 The timer ticking function for the Mtftp service instance.
531 @param Event The ticking event
532 @param Context The Mtftp service instance
542 MTFTP4_SERVICE
*MtftpSb
;
545 MTFTP4_PROTOCOL
*Instance
;
546 EFI_MTFTP4_TOKEN
*Token
;
548 MtftpSb
= (MTFTP4_SERVICE
*) Context
;
551 // Iterate through all the children of the Mtftp service instance. Time
552 // out the packet. If maximum retries reached, clean the session up.
554 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
555 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
557 if ((Instance
->PacketToLive
== 0) || (--Instance
->PacketToLive
> 0)) {
562 // Call the user's time out handler
564 Token
= Instance
->Token
;
566 if ((Token
->TimeoutCallback
!= NULL
) &&
567 EFI_ERROR (Token
->TimeoutCallback (&Instance
->Mtftp4
, Token
))) {
571 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
572 (UINT8
*) "User aborted the transfer in time out"
575 Mtftp4CleanOperation (Instance
, EFI_ABORTED
);
580 // Retransmit the packet if haven't reach the maxmium retry count,
581 // otherwise exit the transfer.
583 if (++Instance
->CurRetry
< Instance
->MaxRetry
) {
584 Mtftp4Retransmit (Instance
);
585 Mtftp4SetTimeout (Instance
);
587 Mtftp4CleanOperation (Instance
, EFI_TIMEOUT
);