2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
15 #if (CONFIG_COMMANDS & CFG_CMD_NET)
17 #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
18 #define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
19 #ifndef CONFIG_NET_RETRY_COUNT
20 # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
22 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
24 /* (for checking the image size) */
25 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
38 static int TftpServerPort
; /* The UDP port at their end */
39 static int TftpOurPort
; /* The UDP port at our end */
40 static int TftpTimeoutCount
;
41 static ulong TftpBlock
; /* packet sequence number */
42 static ulong TftpLastBlock
; /* last packet sequence number received */
43 static ulong TftpBlockWrap
; /* count of sequence number wraparounds */
44 static ulong TftpBlockWrapOffset
; /* memory offset due to wrapping */
49 #define STATE_TOO_LARGE 3
50 #define STATE_BAD_MAGIC 4
53 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
54 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
56 #define DEFAULT_NAME_LEN (8 + 4 + 1)
57 static char default_filename
[DEFAULT_NAME_LEN
];
58 static char *tftp_filename
;
60 #ifdef CFG_DIRECT_FLASH_TFTP
61 extern flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
64 static __inline__
void
65 store_block (unsigned block
, uchar
* src
, unsigned len
)
67 ulong offset
= block
* TFTP_BLOCK_SIZE
+ TftpBlockWrapOffset
;
68 ulong newsize
= offset
+ len
;
69 #ifdef CFG_DIRECT_FLASH_TFTP
72 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; i
++) {
73 /* start address in flash? */
74 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
80 if (rc
) { /* Flash is destination for this packet */
81 rc
= flash_write ((uchar
*)src
, (ulong
)(load_addr
+offset
), len
);
84 NetState
= NETLOOP_FAIL
;
89 #endif /* CFG_DIRECT_FLASH_TFTP */
91 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
94 if (NetBootFileXferSize
< newsize
)
95 NetBootFileXferSize
= newsize
;
98 static void TftpSend (void);
99 static void TftpTimeout (void);
101 /**********************************************************************/
106 volatile uchar
* pkt
;
111 * We will always be sending some sort of packet, so
112 * cobble together the packet headers now.
114 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_HDR_SIZE
;
120 *((ushort
*)pkt
)++ = htons(TFTP_RRQ
);
121 strcpy ((char *)pkt
, tftp_filename
);
122 pkt
+= strlen(tftp_filename
) + 1;
123 strcpy ((char *)pkt
, "octet");
124 pkt
+= 5 /*strlen("octet")*/ + 1;
125 strcpy ((char *)pkt
, "timeout");
126 pkt
+= 7 /*strlen("timeout")*/ + 1;
127 sprintf((char *)pkt
, "%d", TIMEOUT
);
129 printf("send option \"timeout %s\"\n", (char *)pkt
);
131 pkt
+= strlen((char *)pkt
) + 1;
138 *((ushort
*)pkt
)++ = htons(TFTP_ACK
);
139 *((ushort
*)pkt
)++ = htons(TftpBlock
);
143 case STATE_TOO_LARGE
:
145 *((ushort
*)pkt
)++ = htons(TFTP_ERROR
);
146 *((ushort
*)pkt
)++ = htons(3);
147 strcpy ((char *)pkt
, "File too large");
148 pkt
+= 14 /*strlen("File too large")*/ + 1;
152 case STATE_BAD_MAGIC
:
154 *((ushort
*)pkt
)++ = htons(TFTP_ERROR
);
155 *((ushort
*)pkt
)++ = htons(2);
156 strcpy ((char *)pkt
, "File has bad magic");
157 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
162 NetSendUDPPacket(NetServerEther
, NetServerIP
, TftpServerPort
, TftpOurPort
, len
);
167 TftpHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
171 if (dest
!= TftpOurPort
) {
174 if (TftpState
!= STATE_RRQ
&& src
!= TftpServerPort
) {
182 /* warning: don't use increment (++) in ntohs() macros!! */
183 proto
= *((ushort
*)pkt
)++;
184 switch (ntohs(proto
)) {
195 printf("Got OACK: %s %s\n", pkt
, pkt
+strlen(pkt
)+1);
197 TftpState
= STATE_OACK
;
198 TftpServerPort
= src
;
199 TftpSend (); /* Send ACK */
205 TftpBlock
= ntohs(*(ushort
*)pkt
);
208 * RFC1350 specifies that the first data packet will
209 * have sequence number 1. If we receive a sequence
210 * number of 0 this means that there was a wrap
211 * around of the (16 bit) counter.
213 if (TftpBlock
== 0) {
215 TftpBlockWrapOffset
+= TFTP_BLOCK_SIZE
* TFTP_SEQUENCE_SIZE
;
216 printf ("\n\t %lu MB reveived\n\t ", TftpBlockWrapOffset
>>20);
218 if (((TftpBlock
- 1) % 10) == 0) {
220 } else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0) {
226 if (TftpState
== STATE_RRQ
) {
227 puts ("Server did not acknowledge timeout option!\n");
231 if (TftpState
== STATE_RRQ
|| TftpState
== STATE_OACK
) {
232 /* first block received */
233 TftpState
= STATE_DATA
;
234 TftpServerPort
= src
;
237 TftpBlockWrapOffset
= 0;
239 if (TftpBlock
!= 1) { /* Assertion */
240 printf ("\nTFTP error: "
241 "First block is not block 1 (%ld)\n"
242 "Starting again\n\n",
249 if (TftpBlock
== TftpLastBlock
) {
251 * Same block again; ignore it.
256 TftpLastBlock
= TftpBlock
;
257 NetSetTimeout (TIMEOUT
* CFG_HZ
, TftpTimeout
);
259 store_block (TftpBlock
- 1, pkt
+ 2, len
);
262 * Acknoledge the block just received, which will prompt
263 * the server for the next one.
267 if (len
< TFTP_BLOCK_SIZE
) {
269 * We received the whole thing. Try to
273 NetState
= NETLOOP_SUCCESS
;
278 printf ("\nTFTP error: '%s' (%d)\n",
279 pkt
+ 2, ntohs(*(ushort
*)pkt
));
280 puts ("Starting again\n\n");
290 if (++TftpTimeoutCount
> TIMEOUT_COUNT
) {
291 puts ("\nRetry count exceeded; starting again\n");
295 NetSetTimeout (TIMEOUT
* CFG_HZ
, TftpTimeout
);
304 if (BootFile
[0] == '\0') {
305 sprintf(default_filename
, "%02lX%02lX%02lX%02lX.img",
307 (NetOurIP
>> 8) & 0xFF,
308 (NetOurIP
>> 16) & 0xFF,
309 (NetOurIP
>> 24) & 0xFF );
310 tftp_filename
= default_filename
;
312 printf ("*** Warning: no boot file name; using '%s'\n",
315 tftp_filename
= BootFile
;
318 #if defined(CONFIG_NET_MULTI)
319 printf ("Using %s device\n", eth_get_name());
321 puts ("TFTP from server "); print_IPaddr (NetServerIP
);
322 puts ("; our IP address is "); print_IPaddr (NetOurIP
);
324 /* Check if we need to send across this subnet */
325 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
326 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
327 IPaddr_t ServerNet
= NetServerIP
& NetOurSubnetMask
;
329 if (OurNet
!= ServerNet
) {
330 puts ("; sending through gateway ");
331 print_IPaddr (NetOurGatewayIP
) ;
336 printf ("Filename '%s'.", tftp_filename
);
338 if (NetBootFileSize
) {
339 printf (" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
340 print_size (NetBootFileSize
<<9, "");
345 printf ("Load address: 0x%lx\n", load_addr
);
347 puts ("Loading: *\b");
349 NetSetTimeout (TIMEOUT
* CFG_HZ
, TftpTimeout
);
350 NetSetHandler (TftpHandler
);
352 TftpServerPort
= WELL_KNOWN_PORT
;
353 TftpTimeoutCount
= 0;
354 TftpState
= STATE_RRQ
;
355 TftpOurPort
= 1024 + (get_timer(0) % 3072);
358 /* zero out server ether in case the server ip has changed */
359 memset(NetServerEther
, 0, 6);
364 #endif /* CFG_CMD_NET */