3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Support routines for Mtftp
23 #include "Mtftp4Impl.h"
27 Allocate a MTFTP4 block range, then init it to the
30 @param Start The start block number
31 @param End The last block number in the range
33 @return NULL if failed to allocate memory, otherwise the created block range.
43 MTFTP4_BLOCK_RANGE
*Range
;
45 Range
= NetAllocatePool (sizeof (MTFTP4_BLOCK_RANGE
));
51 NetListInit (&Range
->Link
);
60 Initialize the block range for either RRQ or WRQ. RRQ and WRQ have
61 different requirements for Start and End. For example, during start
62 up, WRQ initializes its whole valid block range to [0, 0xffff]. This
63 is bacause the server will send us a ACK0 to inform us to start the
64 upload. When the client received ACK0, it will remove 0 from the range,
65 get the next block number, which is 1, then upload the BLOCK1. For RRQ
66 without option negotiation, the server will directly send us the BLOCK1
67 in response to the client's RRQ. When received BLOCK1, the client will
68 remove it from the block range and send an ACK. It also works if there
69 is option negotiation.
71 @param Head The block range head to initialize
72 @param Start The Start block number.
73 @param End The last block number.
75 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range
76 @retval EFI_SUCCESS The initial block range is created.
80 Mtftp4InitBlockRange (
81 IN NET_LIST_ENTRY
*Head
,
86 MTFTP4_BLOCK_RANGE
*Range
;
88 Range
= Mtftp4AllocateRange (Start
, End
);
91 return EFI_OUT_OF_RESOURCES
;
94 NetListInsertTail (Head
, &Range
->Link
);
100 Get the first valid block number on the range list.
102 @param Head The block range head
104 @return -1: if the block range is empty. Otherwise the first valid block number.
108 Mtftp4GetNextBlockNum (
109 IN NET_LIST_ENTRY
*Head
112 MTFTP4_BLOCK_RANGE
*Range
;
114 if (NetListIsEmpty (Head
)) {
118 Range
= NET_LIST_HEAD (Head
, MTFTP4_BLOCK_RANGE
, Link
);
124 Set the last block number of the block range list. It will
125 remove all the blocks after the Last. MTFTP initialize the
126 block range to the maximum possible range, such as [0, 0xffff]
127 for WRQ. When it gets the last block number, it will call
128 this function to set the last block number.
130 @param Head The block range list
131 @param Last The last block number
137 Mtftp4SetLastBlockNum (
138 IN NET_LIST_ENTRY
*Head
,
142 MTFTP4_BLOCK_RANGE
*Range
;
145 // Iterate from the tail to head to remove the block number
148 while (!NetListIsEmpty (Head
)) {
149 Range
= NET_LIST_TAIL (Head
, MTFTP4_BLOCK_RANGE
, Link
);
151 if (Range
->Start
> Last
) {
152 NetListRemoveEntry (&Range
->Link
);
157 if (Range
->End
> Last
) {
167 Remove the block number from the block range list.
169 @param Head The block range list to remove from
170 @param Num The block number to remove
172 @retval EFI_NOT_FOUND The block number isn't in the block range list
173 @retval EFI_SUCCESS The block number has been removed from the list
174 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource
178 Mtftp4RemoveBlockNum (
179 IN NET_LIST_ENTRY
*Head
,
183 MTFTP4_BLOCK_RANGE
*Range
;
184 MTFTP4_BLOCK_RANGE
*NewRange
;
185 NET_LIST_ENTRY
*Entry
;
187 NET_LIST_FOR_EACH (Entry
, Head
) {
190 // Each block represents a hole [Start, End] in the file,
191 // skip to the first range with End >= Num
193 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_BLOCK_RANGE
, Link
);
195 if (Range
->End
< Num
) {
200 // There are three different cases for Start
201 // 1. (Start > Num) && (End >= Num):
202 // because all the holes before this one has the condition of
203 // End < Num, so this block number has been removed.
205 // 2. (Start == Num) && (End >= Num):
206 // Need to increase the Start by one, and if End == Num, this
207 // hole has been removed completely, remove it.
209 // 3. (Start < Num) && (End >= Num):
210 // if End == Num, only need to decrease the End by one because
211 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
212 // if (End > Num), the hold is splited into two holes, with
213 // [Start, Num - 1] and [Num + 1, End].
215 if (Range
->Start
> Num
) {
216 return EFI_NOT_FOUND
;
218 } else if (Range
->Start
== Num
) {
221 if (Range
->Start
> Range
->End
) {
222 NetListRemoveEntry (&Range
->Link
);
229 if (Range
->End
== Num
) {
232 NewRange
= Mtftp4AllocateRange (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
;
251 Build then transmit the request packet for the MTFTP session.
253 @param Instance The Mtftp session
255 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
256 @retval EFI_SUCCESS The request is built and sent
257 @retval Others Failed to transmit the packet.
262 IN MTFTP4_PROTOCOL
*Instance
265 EFI_MTFTP4_PACKET
*Packet
;
266 EFI_MTFTP4_OPTION
*Options
;
267 EFI_MTFTP4_TOKEN
*Token
;
274 Token
= Instance
->Token
;
275 Options
= Token
->OptionList
;
276 Mode
= Instance
->Token
->ModeStr
;
283 // Compute the packet length
285 Len
= (UINT32
) (AsciiStrLen (Token
->Filename
) + AsciiStrLen (Mode
) + 4);
287 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
288 Len
+= (UINT32
) (AsciiStrLen (Options
[Index
].OptionStr
) +
289 AsciiStrLen (Options
[Index
].ValueStr
) + 2);
293 // Allocate a packet then copy the data over
295 if ((Nbuf
= NetbufAlloc (Len
)) == NULL
) {
296 return EFI_OUT_OF_RESOURCES
;
299 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Nbuf
, Len
, FALSE
);
300 Packet
->OpCode
= HTONS (Instance
->Operation
);
301 Cur
= Packet
->Rrq
.Filename
;
302 Cur
= AsciiStrCpy (Cur
, Token
->Filename
);
303 Cur
= AsciiStrCpy (Cur
, Mode
);
305 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
306 Cur
= AsciiStrCpy (Cur
, Options
[Index
].OptionStr
);
307 Cur
= AsciiStrCpy (Cur
, Options
[Index
].ValueStr
);
310 return Mtftp4SendPacket (Instance
, Nbuf
);
315 Build then send an error message
317 @param Instance The MTFTP session
318 @param ErrInfo The error code and message
320 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
321 @retval EFI_SUCCESS The error packet is transmitted.
322 @retval Others Failed to transmit the packet.
327 IN MTFTP4_PROTOCOL
*Instance
,
333 EFI_MTFTP4_PACKET
*TftpError
;
336 Len
= (UINT32
) (AsciiStrLen (ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
337 Packet
= NetbufAlloc (Len
);
339 if (Packet
== NULL
) {
340 return EFI_OUT_OF_RESOURCES
;
343 TftpError
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
344 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
345 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
347 AsciiStrCpy (TftpError
->Error
.ErrorMessage
, ErrInfo
);
349 return Mtftp4SendPacket (Instance
, Packet
);
354 The callback function called when the packet is transmitted.
355 It simply frees the packet.
357 @param Packet The transmitted (or failed to) packet
358 @param Points The local and remote UDP access point
359 @param IoStatus The result of the transmission
360 @param Context Opaque parameter to the callback
379 Set the timeout for the instance. User a longer time for
382 @param Instance The Mtftp session to set time out
389 IN MTFTP4_PROTOCOL
*Instance
392 if (Instance
->Master
) {
393 Instance
->PacketToLive
= Instance
->Timeout
;
395 Instance
->PacketToLive
= Instance
->Timeout
* 2;
401 Send the packet for the instance. It will first save a reference to
402 the packet for later retransmission. then determine the destination
403 port, listen port for requests, and connected port for others. At last,
406 @param Instance The Mtftp instance
407 @param Packet The packet to send
409 @retval EFI_SUCCESS The packet is sent out
410 @retval Others Failed to transmit the packet.
415 IN MTFTP4_PROTOCOL
*Instance
,
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 UdpPoint
.LocalAddr
= 0;
436 UdpPoint
.LocalPort
= 0;
437 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
440 // Send the requests to the listening port, other packets
441 // to the connected port
443 OpCode
= NTOHS (*((UINT16
*) NetbufGetByte (Packet
, 0, NULL
)));
445 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
446 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
447 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
449 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
452 NET_GET_REF (Packet
);
454 Status
= UdpIoSendDatagram (
455 Instance
->UnicastPort
,
463 if (EFI_ERROR (Status
)) {
464 NET_PUT_REF (Packet
);
472 Retransmit the last packet for the instance
474 @param Instance The Mtftp instance
476 @retval EFI_SUCCESS The last packet is retransmitted.
477 @retval Others Failed to retransmit.
482 IN MTFTP4_PROTOCOL
*Instance
489 ASSERT (Instance
->LastPacket
!= NULL
);
491 UdpPoint
.LocalAddr
= 0;
492 UdpPoint
.LocalPort
= 0;
493 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
496 // Set the requests to the listening port, other packets to the connected port
498 OpCode
= NTOHS (*(UINT16
*) NetbufGetByte (Instance
->LastPacket
, 0, NULL
));
500 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
501 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
502 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
504 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
507 NET_GET_REF (Instance
->LastPacket
);
509 Status
= UdpIoSendDatagram (
510 Instance
->UnicastPort
,
511 Instance
->LastPacket
,
518 if (EFI_ERROR (Status
)) {
519 NET_PUT_REF (Instance
->LastPacket
);
527 The timer ticking function for the Mtftp service instance.
529 @param Event The ticking event
530 @param Context The Mtftp service instance
542 MTFTP4_SERVICE
*MtftpSb
;
543 NET_LIST_ENTRY
*Entry
;
544 NET_LIST_ENTRY
*Next
;
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 "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
);