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.
42 MTFTP4_BLOCK_RANGE
*Range
;
44 Range
= AllocatePool (sizeof (MTFTP4_BLOCK_RANGE
));
50 InitializeListHead (&Range
->Link
);
59 Initialize the block range for either RRQ or WRQ. RRQ and WRQ have
60 different requirements for Start and End. For example, during start
61 up, WRQ initializes its whole valid block range to [0, 0xffff]. This
62 is bacause the server will send us a ACK0 to inform us to start the
63 upload. When the client received ACK0, it will remove 0 from the range,
64 get the next block number, which is 1, then upload the BLOCK1. For RRQ
65 without option negotiation, the server will directly send us the BLOCK1
66 in response to the client's RRQ. When received BLOCK1, the client will
67 remove it from the block range and send an ACK. It also works if there
68 is option negotiation.
70 @param Head The block range head to initialize
71 @param Start The Start block number.
72 @param End The last block number.
74 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range
75 @retval EFI_SUCCESS The initial block range is created.
79 Mtftp4InitBlockRange (
85 MTFTP4_BLOCK_RANGE
*Range
;
87 Range
= Mtftp4AllocateRange (Start
, End
);
90 return EFI_OUT_OF_RESOURCES
;
93 InsertTailList (Head
, &Range
->Link
);
99 Get the first valid block number on the range list.
101 @param Head The block range head
103 @return -1: if the block range is empty. Otherwise the first valid block number.
107 Mtftp4GetNextBlockNum (
111 MTFTP4_BLOCK_RANGE
*Range
;
113 if (IsListEmpty (Head
)) {
117 Range
= NET_LIST_HEAD (Head
, MTFTP4_BLOCK_RANGE
, Link
);
123 Set the last block number of the block range list. It will
124 remove all the blocks after the Last. MTFTP initialize the
125 block range to the maximum possible range, such as [0, 0xffff]
126 for WRQ. When it gets the last block number, it will call
127 this function to set the last block number.
129 @param Head The block range list
130 @param Last The last block number
136 Mtftp4SetLastBlockNum (
141 MTFTP4_BLOCK_RANGE
*Range
;
144 // Iterate from the tail to head to remove the block number
147 while (!IsListEmpty (Head
)) {
148 Range
= NET_LIST_TAIL (Head
, MTFTP4_BLOCK_RANGE
, Link
);
150 if (Range
->Start
> Last
) {
151 RemoveEntryList (&Range
->Link
);
152 gBS
->FreePool (Range
);
156 if (Range
->End
> Last
) {
166 Remove the block number from the block range list.
168 @param Head The block range list to remove from
169 @param Num The block number to remove
171 @retval EFI_NOT_FOUND The block number isn't in the block range list
172 @retval EFI_SUCCESS The block number has been removed from the list
173 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource
177 Mtftp4RemoveBlockNum (
182 MTFTP4_BLOCK_RANGE
*Range
;
183 MTFTP4_BLOCK_RANGE
*NewRange
;
186 NET_LIST_FOR_EACH (Entry
, Head
) {
189 // Each block represents a hole [Start, End] in the file,
190 // skip to the first range with End >= Num
192 Range
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_BLOCK_RANGE
, Link
);
194 if (Range
->End
< Num
) {
199 // There are three different cases for Start
200 // 1. (Start > Num) && (End >= Num):
201 // because all the holes before this one has the condition of
202 // End < Num, so this block number has been removed.
204 // 2. (Start == Num) && (End >= Num):
205 // Need to increase the Start by one, and if End == Num, this
206 // hole has been removed completely, remove it.
208 // 3. (Start < Num) && (End >= Num):
209 // if End == Num, only need to decrease the End by one because
210 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
211 // if (End > Num), the hold is splited into two holes, with
212 // [Start, Num - 1] and [Num + 1, End].
214 if (Range
->Start
> Num
) {
215 return EFI_NOT_FOUND
;
217 } else if (Range
->Start
== Num
) {
220 if (Range
->Start
> Range
->End
) {
221 RemoveEntryList (&Range
->Link
);
222 gBS
->FreePool (Range
);
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
;
250 Build then transmit the request packet for the MTFTP session.
252 @param Instance The Mtftp session
254 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
255 @retval EFI_SUCCESS The request is built and sent
256 @retval Others Failed to transmit the packet.
261 IN MTFTP4_PROTOCOL
*Instance
264 EFI_MTFTP4_PACKET
*Packet
;
265 EFI_MTFTP4_OPTION
*Options
;
266 EFI_MTFTP4_TOKEN
*Token
;
275 Token
= Instance
->Token
;
276 Options
= Token
->OptionList
;
277 Mode
= Instance
->Token
->ModeStr
;
280 Mode
= (UINT8
*) "octet";
284 // Compute the packet length
286 Len1
= (UINT32
) AsciiStrLen ((CHAR8
*) Token
->Filename
);
287 Len2
= (UINT32
) AsciiStrLen ((CHAR8
*) Mode
);
288 Len
= (Len1
+ Len2
+ 4);
290 for (Index
= 0; Index
< Token
->OptionCount
; Index
++) {
291 Len1
= (UINT32
) AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
);
292 Len2
= (UINT32
) AsciiStrLen ((CHAR8
*) Options
[Index
].ValueStr
);
293 Len
+= Len1
+ Len2
+ 2;
297 // Allocate a packet then copy the data over
299 if ((Nbuf
= NetbufAlloc (Len
)) == NULL
) {
300 return EFI_OUT_OF_RESOURCES
;
303 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Nbuf
, Len
, FALSE
);
304 Packet
->OpCode
= HTONS (Instance
->Operation
);
305 Cur
= Packet
->Rrq
.Filename
;
306 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Token
->Filename
);
307 Cur
+= AsciiStrLen ((CHAR8
*) Token
->Filename
) + 1;
308 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Mode
);
309 Cur
+= AsciiStrLen ((CHAR8
*) Mode
) + 1;
311 for (Index
= 0; Index
< Token
->OptionCount
; ++Index
) {
312 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].OptionStr
);
313 Cur
+= AsciiStrLen ((CHAR8
*) Options
[Index
].OptionStr
) + 1;
315 Cur
= (UINT8
*) AsciiStrCpy ((CHAR8
*) Cur
, (CHAR8
*) Options
[Index
].ValueStr
);
316 Cur
+= AsciiStrLen ((CHAR8
*) (CHAR8
*) Options
[Index
].ValueStr
) + 1;
319 return Mtftp4SendPacket (Instance
, Nbuf
);
324 Build then send an error message
326 @param Instance The MTFTP session
327 @param ErrInfo The error code and message
329 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
330 @retval EFI_SUCCESS The error packet is transmitted.
331 @retval Others Failed to transmit the packet.
336 IN MTFTP4_PROTOCOL
*Instance
,
342 EFI_MTFTP4_PACKET
*TftpError
;
345 Len
= (UINT32
) (AsciiStrLen ((CHAR8
*) ErrInfo
) + sizeof (EFI_MTFTP4_ERROR_HEADER
));
346 Packet
= NetbufAlloc (Len
);
348 if (Packet
== NULL
) {
349 return EFI_OUT_OF_RESOURCES
;
352 TftpError
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
353 TftpError
->OpCode
= HTONS (EFI_MTFTP4_OPCODE_ERROR
);
354 TftpError
->Error
.ErrorCode
= HTONS (ErrCode
);
356 AsciiStrCpy ((CHAR8
*) TftpError
->Error
.ErrorMessage
, (CHAR8
*) ErrInfo
);
358 return Mtftp4SendPacket (Instance
, Packet
);
363 The callback function called when the packet is transmitted.
364 It simply frees the packet.
366 @param Packet The transmitted (or failed to) packet
367 @param Points The local and remote UDP access point
368 @param IoStatus The result of the transmission
369 @param Context Opaque parameter to the callback
387 Set the timeout for the instance. User a longer time for
390 @param Instance The Mtftp session to set time out
397 IN MTFTP4_PROTOCOL
*Instance
400 if (Instance
->Master
) {
401 Instance
->PacketToLive
= Instance
->Timeout
;
403 Instance
->PacketToLive
= Instance
->Timeout
* 2;
409 Send the packet for the instance. It will first save a reference to
410 the packet for later retransmission. then determine the destination
411 port, listen port for requests, and connected port for others. At last,
414 @param Instance The Mtftp instance
415 @param Packet The packet to send
417 @retval EFI_SUCCESS The packet is sent out
418 @retval Others Failed to transmit the packet.
423 IN MTFTP4_PROTOCOL
*Instance
,
433 // Save the packet for retransmission
435 if (Instance
->LastPacket
!= NULL
) {
436 NetbufFree (Instance
->LastPacket
);
439 Instance
->LastPacket
= Packet
;
441 Instance
->CurRetry
= 0;
442 Mtftp4SetTimeout (Instance
);
444 UdpPoint
.LocalAddr
= 0;
445 UdpPoint
.LocalPort
= 0;
446 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
449 // Send the requests to the listening port, other packets
450 // to the connected port
452 Value
= *((UINT16
*) NetbufGetByte (Packet
, 0, NULL
));
453 OpCode
= NTOHS (Value
);
455 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
456 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
457 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
459 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
462 NET_GET_REF (Packet
);
464 Status
= UdpIoSendDatagram (
465 Instance
->UnicastPort
,
473 if (EFI_ERROR (Status
)) {
474 NET_PUT_REF (Packet
);
482 Retransmit the last packet for the instance
484 @param Instance The Mtftp instance
486 @retval EFI_SUCCESS The last packet is retransmitted.
487 @retval Others Failed to retransmit.
492 IN MTFTP4_PROTOCOL
*Instance
500 ASSERT (Instance
->LastPacket
!= NULL
);
502 UdpPoint
.LocalAddr
= 0;
503 UdpPoint
.LocalPort
= 0;
504 UdpPoint
.RemoteAddr
= Instance
->ServerIp
;
507 // Set the requests to the listening port, other packets to the connected port
509 Value
= *(UINT16
*) NetbufGetByte (Instance
->LastPacket
, 0, NULL
);
510 OpCode
= NTOHS (Value
);
512 if ((OpCode
== EFI_MTFTP4_OPCODE_RRQ
) || (OpCode
== EFI_MTFTP4_OPCODE_DIR
) ||
513 (OpCode
== EFI_MTFTP4_OPCODE_WRQ
)) {
514 UdpPoint
.RemotePort
= Instance
->ListeningPort
;
516 UdpPoint
.RemotePort
= Instance
->ConnectedPort
;
519 NET_GET_REF (Instance
->LastPacket
);
521 Status
= UdpIoSendDatagram (
522 Instance
->UnicastPort
,
523 Instance
->LastPacket
,
530 if (EFI_ERROR (Status
)) {
531 NET_PUT_REF (Instance
->LastPacket
);
539 The timer ticking function for the Mtftp service instance.
541 @param Event The ticking event
542 @param Context The Mtftp service instance
554 MTFTP4_SERVICE
*MtftpSb
;
557 MTFTP4_PROTOCOL
*Instance
;
558 EFI_MTFTP4_TOKEN
*Token
;
560 MtftpSb
= (MTFTP4_SERVICE
*) Context
;
563 // Iterate through all the children of the Mtftp service instance. Time
564 // out the packet. If maximum retries reached, clean the session up.
566 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &MtftpSb
->Children
) {
567 Instance
= NET_LIST_USER_STRUCT (Entry
, MTFTP4_PROTOCOL
, Link
);
569 if ((Instance
->PacketToLive
== 0) || (--Instance
->PacketToLive
> 0)) {
574 // Call the user's time out handler
576 Token
= Instance
->Token
;
578 if ((Token
->TimeoutCallback
!= NULL
) &&
579 EFI_ERROR (Token
->TimeoutCallback (&Instance
->Mtftp4
, Token
))) {
583 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
584 (UINT8
*) "User aborted the transfer in time out"
587 Mtftp4CleanOperation (Instance
, EFI_ABORTED
);
592 // Retransmit the packet if haven't reach the maxmium retry count,
593 // otherwise exit the transfer.
595 if (++Instance
->CurRetry
< Instance
->MaxRetry
) {
596 Mtftp4Retransmit (Instance
);
597 Mtftp4SetTimeout (Instance
);
599 Mtftp4CleanOperation (Instance
, EFI_TIMEOUT
);