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 ((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
;
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
;
276 Token
= Instance
->Token
;
277 Options
= Token
->OptionList
;
278 Mode
= Instance
->Token
->ModeStr
;
285 // Compute the packet length
287 Len1
= (UINT32
) AsciiStrLen ((CHAR8
*) Token
->Filename
);
288 Len2
= (UINT32
) AsciiStrLen ((CHAR8
*) Mode
);
289 Len
= (Len1
+ Len2
+ 4);
291 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
292 Len1
= (UINT32
) AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
);
293 Len2
= (UINT32
) AsciiStrLen ((CHAR8
*) Options
[Index
].ValueStr
);
294 Len
+= Len1
+ Len2
+ 2;
298 // Allocate a packet then copy the data over
300 if ((Nbuf
= NetbufAlloc (Len
)) == NULL
) {
301 return EFI_OUT_OF_RESOURCES
;
304 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Nbuf
, Len
, FALSE
);
305 Packet
->OpCode
= HTONS (Instance
->Operation
);
306 Cur
= Packet
->Rrq
.Filename
;
307 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Token
->Filename
);
308 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Mode
);
310 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
311 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].OptionStr
);
312 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].ValueStr
);
315 return Mtftp4SendPacket (Instance
, Nbuf
);
320 Build then send an error message
322 @param Instance The MTFTP session
323 @param ErrInfo The error code and message
325 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
326 @retval EFI_SUCCESS The error packet is transmitted.
327 @retval Others Failed to transmit the packet.
332 IN MTFTP4_PROTOCOL
*Instance
,
338 EFI_MTFTP4_PACKET
*TftpError
;
341 Len
= (UINT32
) (AsciiStrLen ((CHAR8
*) ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
342 Packet
= NetbufAlloc (Len
);
344 if (Packet
== NULL
) {
345 return EFI_OUT_OF_RESOURCES
;
348 TftpError
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
349 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
350 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
352 AsciiStrCpy ((CHAR8
*) TftpError
->Error
.ErrorMessage
, (CHAR8
*) ErrInfo
);
354 return Mtftp4SendPacket (Instance
, Packet
);
359 The callback function called when the packet is transmitted.
360 It simply frees the packet.
362 @param Packet The transmitted (or failed to) packet
363 @param Points The local and remote UDP access point
364 @param IoStatus The result of the transmission
365 @param Context Opaque parameter to the callback
384 Set the timeout for the instance. User a longer time for
387 @param Instance The Mtftp session to set time out
394 IN MTFTP4_PROTOCOL
*Instance
397 if (Instance
->Master
) {
398 Instance
->PacketToLive
= Instance
->Timeout
;
400 Instance
->PacketToLive
= Instance
->Timeout
* 2;
406 Send the packet for the instance. It will first save a reference to
407 the packet for later retransmission. then determine the destination
408 port, listen port for requests, and connected port for others. At last,
411 @param Instance The Mtftp instance
412 @param Packet The packet to send
414 @retval EFI_SUCCESS The packet is sent out
415 @retval Others Failed to transmit the packet.
420 IN MTFTP4_PROTOCOL
*Instance
,
430 // Save the packet for retransmission
432 if (Instance
->LastPacket
!= NULL
) {
433 NetbufFree (Instance
->LastPacket
);
436 Instance
->LastPacket
= Packet
;
438 Instance
->CurRetry
= 0;
439 Mtftp4SetTimeout (Instance
);
441 UdpPoint
.LocalAddr
= 0;
442 UdpPoint
.LocalPort
= 0;
443 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
446 // Send the requests to the listening port, other packets
447 // to the connected port
449 Value
= *((UINT16
*) NetbufGetByte (Packet
, 0, NULL
));
450 OpCode
= NTOHS (Value
);
452 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
453 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
454 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
456 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
459 NET_GET_REF (Packet
);
461 Status
= UdpIoSendDatagram (
462 Instance
->UnicastPort
,
470 if (EFI_ERROR (Status
)) {
471 NET_PUT_REF (Packet
);
479 Retransmit the last packet for the instance
481 @param Instance The Mtftp instance
483 @retval EFI_SUCCESS The last packet is retransmitted.
484 @retval Others Failed to retransmit.
489 IN MTFTP4_PROTOCOL
*Instance
497 ASSERT (Instance
->LastPacket
!= NULL
);
499 UdpPoint
.LocalAddr
= 0;
500 UdpPoint
.LocalPort
= 0;
501 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
504 // Set the requests to the listening port, other packets to the connected port
506 Value
= *(UINT16
*) NetbufGetByte (Instance
->LastPacket
, 0, NULL
);
507 OpCode
= NTOHS (Value
);
509 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
510 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
511 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
513 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
516 NET_GET_REF (Instance
->LastPacket
);
518 Status
= UdpIoSendDatagram (
519 Instance
->UnicastPort
,
520 Instance
->LastPacket
,
527 if (EFI_ERROR (Status
)) {
528 NET_PUT_REF (Instance
->LastPacket
);
536 The timer ticking function for the Mtftp service instance.
538 @param Event The ticking event
539 @param Context The Mtftp service instance
551 MTFTP4_SERVICE
*MtftpSb
;
552 NET_LIST_ENTRY
*Entry
;
553 NET_LIST_ENTRY
*Next
;
554 MTFTP4_PROTOCOL
*Instance
;
555 EFI_MTFTP4_TOKEN
*Token
;
557 MtftpSb
= (MTFTP4_SERVICE
*) Context
;
560 // Iterate through all the children of the Mtftp service instance. Time
561 // out the packet. If maximum retries reached, clean the session up.
563 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
564 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
566 if ((Instance
->PacketToLive
== 0) || (--Instance
->PacketToLive
> 0)) {
571 // Call the user's time out handler
573 Token
= Instance
->Token
;
575 if ((Token
->TimeoutCallback
!= NULL
) &&
576 EFI_ERROR (Token
->TimeoutCallback (&Instance
->Mtftp4
, Token
))) {
580 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
581 "User aborted the transfer in time out"
584 Mtftp4CleanOperation (Instance
, EFI_ABORTED
);
589 // Retransmit the packet if haven't reach the maxmium retry count,
590 // otherwise exit the transfer.
592 if (++Instance
->CurRetry
< Instance
->MaxRetry
) {
593 Mtftp4Retransmit (Instance
);
594 Mtftp4SetTimeout (Instance
);
596 Mtftp4CleanOperation (Instance
, EFI_TIMEOUT
);