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
;
281 Mode
= (UINT8
*) "octet";
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
+= AsciiStrLen ((CHAR8
*) Token
->Filename
) + 1;
309 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Mode
);
310 Cur
+= AsciiStrLen ((CHAR8
*) Mode
) + 1;
312 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
313 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].OptionStr
);
314 Cur
+= AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
) + 1;
316 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].ValueStr
);
317 Cur
+= AsciiStrLen ((CHAR8
*) (CHAR8
*) Options
[Index
].ValueStr
) + 1;
320 return Mtftp4SendPacket (Instance
, Nbuf
);
325 Build then send an error message
327 @param Instance The MTFTP session
328 @param ErrInfo The error code and message
330 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
331 @retval EFI_SUCCESS The error packet is transmitted.
332 @retval Others Failed to transmit the packet.
337 IN MTFTP4_PROTOCOL
*Instance
,
343 EFI_MTFTP4_PACKET
*TftpError
;
346 Len
= (UINT32
) (AsciiStrLen ((CHAR8
*) ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
347 Packet
= NetbufAlloc (Len
);
349 if (Packet
== NULL
) {
350 return EFI_OUT_OF_RESOURCES
;
353 TftpError
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
354 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
355 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
357 AsciiStrCpy ((CHAR8
*) TftpError
->Error
.ErrorMessage
, (CHAR8
*) ErrInfo
);
359 return Mtftp4SendPacket (Instance
, Packet
);
364 The callback function called when the packet is transmitted.
365 It simply frees the packet.
367 @param Packet The transmitted (or failed to) packet
368 @param Points The local and remote UDP access point
369 @param IoStatus The result of the transmission
370 @param Context Opaque parameter to the callback
389 Set the timeout for the instance. User a longer time for
392 @param Instance The Mtftp session to set time out
399 IN MTFTP4_PROTOCOL
*Instance
402 if (Instance
->Master
) {
403 Instance
->PacketToLive
= Instance
->Timeout
;
405 Instance
->PacketToLive
= Instance
->Timeout
* 2;
411 Send the packet for the instance. It will first save a reference to
412 the packet for later retransmission. then determine the destination
413 port, listen port for requests, and connected port for others. At last,
416 @param Instance The Mtftp instance
417 @param Packet The packet to send
419 @retval EFI_SUCCESS The packet is sent out
420 @retval Others Failed to transmit the packet.
425 IN MTFTP4_PROTOCOL
*Instance
,
435 // Save the packet for retransmission
437 if (Instance
->LastPacket
!= NULL
) {
438 NetbufFree (Instance
->LastPacket
);
441 Instance
->LastPacket
= Packet
;
443 Instance
->CurRetry
= 0;
444 Mtftp4SetTimeout (Instance
);
446 UdpPoint
.LocalAddr
= 0;
447 UdpPoint
.LocalPort
= 0;
448 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
451 // Send the requests to the listening port, other packets
452 // to the connected port
454 Value
= *((UINT16
*) NetbufGetByte (Packet
, 0, NULL
));
455 OpCode
= NTOHS (Value
);
457 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
458 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
459 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
461 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
464 NET_GET_REF (Packet
);
466 Status
= UdpIoSendDatagram (
467 Instance
->UnicastPort
,
475 if (EFI_ERROR (Status
)) {
476 NET_PUT_REF (Packet
);
484 Retransmit the last packet for the instance
486 @param Instance The Mtftp instance
488 @retval EFI_SUCCESS The last packet is retransmitted.
489 @retval Others Failed to retransmit.
494 IN MTFTP4_PROTOCOL
*Instance
502 ASSERT (Instance
->LastPacket
!= NULL
);
504 UdpPoint
.LocalAddr
= 0;
505 UdpPoint
.LocalPort
= 0;
506 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
509 // Set the requests to the listening port, other packets to the connected port
511 Value
= *(UINT16
*) NetbufGetByte (Instance
->LastPacket
, 0, NULL
);
512 OpCode
= NTOHS (Value
);
514 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
515 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
516 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
518 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
521 NET_GET_REF (Instance
->LastPacket
);
523 Status
= UdpIoSendDatagram (
524 Instance
->UnicastPort
,
525 Instance
->LastPacket
,
532 if (EFI_ERROR (Status
)) {
533 NET_PUT_REF (Instance
->LastPacket
);
541 The timer ticking function for the Mtftp service instance.
543 @param Event The ticking event
544 @param Context The Mtftp service instance
556 MTFTP4_SERVICE
*MtftpSb
;
557 NET_LIST_ENTRY
*Entry
;
558 NET_LIST_ENTRY
*Next
;
559 MTFTP4_PROTOCOL
*Instance
;
560 EFI_MTFTP4_TOKEN
*Token
;
562 MtftpSb
= (MTFTP4_SERVICE
*) Context
;
565 // Iterate through all the children of the Mtftp service instance. Time
566 // out the packet. If maximum retries reached, clean the session up.
568 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
569 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
571 if ((Instance
->PacketToLive
== 0) || (--Instance
->PacketToLive
> 0)) {
576 // Call the user's time out handler
578 Token
= Instance
->Token
;
580 if ((Token
->TimeoutCallback
!= NULL
) &&
581 EFI_ERROR (Token
->TimeoutCallback (&Instance
->Mtftp4
, Token
))) {
585 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
586 (UINT8
*) "User aborted the transfer in time out"
589 Mtftp4CleanOperation (Instance
, EFI_ABORTED
);
594 // Retransmit the packet if haven't reach the maxmium retry count,
595 // otherwise exit the transfer.
597 if (++Instance
->CurRetry
< Instance
->MaxRetry
) {
598 Mtftp4Retransmit (Instance
);
599 Mtftp4SetTimeout (Instance
);
601 Mtftp4CleanOperation (Instance
, EFI_TIMEOUT
);