wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1994, 1995, 2000 Neil Russell. |
| 3 | * (See License) |
| 4 | * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <net.h> |
| 10 | #include "tftp.h" |
| 11 | #include "bootp.h" |
| 12 | |
| 13 | #undef ET_DEBUG |
| 14 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 15 | #if defined(CONFIG_CMD_NET) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 16 | |
| 17 | #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */ |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 18 | #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 19 | #ifndef CONFIG_NET_RETRY_COUNT |
| 20 | # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */ |
| 21 | #else |
| 22 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) |
| 23 | #endif |
| 24 | /* (for checking the image size) */ |
| 25 | #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ |
| 26 | |
| 27 | /* |
| 28 | * TFTP operations. |
| 29 | */ |
| 30 | #define TFTP_RRQ 1 |
| 31 | #define TFTP_WRQ 2 |
| 32 | #define TFTP_DATA 3 |
| 33 | #define TFTP_ACK 4 |
| 34 | #define TFTP_ERROR 5 |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 35 | #define TFTP_OACK 6 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 36 | |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 37 | static ulong TftpTimeoutMSecs = TIMEOUT; |
| 38 | static int TftpTimeoutCountMax = TIMEOUT_COUNT; |
| 39 | |
| 40 | /* |
| 41 | * These globals govern the timeout behavior when attempting a connection to a |
| 42 | * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to |
| 43 | * wait for the server to respond to initial connection. Second global, |
| 44 | * TftpRRQTimeoutCountMax, gives the number of such connection retries. |
| 45 | * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be |
| 46 | * positive. The globals are meant to be set (and restored) by code needing |
| 47 | * non-standard timeout behavior when initiating a TFTP transfer. |
| 48 | */ |
| 49 | ulong TftpRRQTimeoutMSecs = TIMEOUT; |
| 50 | int TftpRRQTimeoutCountMax = TIMEOUT_COUNT; |
| 51 | |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 52 | static IPaddr_t TftpServerIP; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 53 | static int TftpServerPort; /* The UDP port at their end */ |
| 54 | static int TftpOurPort; /* The UDP port at our end */ |
| 55 | static int TftpTimeoutCount; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 56 | static ulong TftpBlock; /* packet sequence number */ |
| 57 | static ulong TftpLastBlock; /* last packet sequence number received */ |
| 58 | static ulong TftpBlockWrap; /* count of sequence number wraparounds */ |
| 59 | static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 60 | static int TftpState; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 61 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 62 | #define STATE_RRQ 1 |
| 63 | #define STATE_DATA 2 |
| 64 | #define STATE_TOO_LARGE 3 |
| 65 | #define STATE_BAD_MAGIC 4 |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 66 | #define STATE_OACK 5 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 67 | |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 68 | #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */ |
| 69 | #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */ |
| 70 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 71 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
| 72 | static char default_filename[DEFAULT_NAME_LEN]; |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 73 | |
| 74 | #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN |
| 75 | #define MAX_LEN 128 |
| 76 | #else |
| 77 | #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN |
| 78 | #endif |
| 79 | |
| 80 | static char tftp_filename[MAX_LEN]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 81 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 83 | extern flash_info_t flash_info[]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 86 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
| 87 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 88 | * almost-MTU block sizes. At least try... fall back to 512 if need be. |
| 89 | */ |
| 90 | #define TFTP_MTU_BLOCKSIZE 1468 |
| 91 | static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE; |
| 92 | static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE; |
| 93 | |
| 94 | #ifdef CONFIG_MCAST_TFTP |
| 95 | #include <malloc.h> |
| 96 | #define MTFTP_BITMAPSIZE 0x1000 |
| 97 | static unsigned *Bitmap; |
| 98 | static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE; |
| 99 | static uchar ProhibitMcast=0, MasterClient=0; |
| 100 | static uchar Multicast=0; |
| 101 | extern IPaddr_t Mcast_addr; |
| 102 | static int Mcast_port; |
| 103 | static ulong TftpEndingBlock; /* can get 'last' block before done..*/ |
| 104 | |
| 105 | static void parse_multicast_oack(char *pkt,int len); |
| 106 | |
| 107 | static void |
| 108 | mcast_cleanup(void) |
| 109 | { |
| 110 | if (Mcast_addr) eth_mcast_join(Mcast_addr, 0); |
| 111 | if (Bitmap) free(Bitmap); |
| 112 | Bitmap=NULL; |
| 113 | Mcast_addr = Multicast = Mcast_port = 0; |
| 114 | TftpEndingBlock = -1; |
| 115 | } |
| 116 | |
| 117 | #endif /* CONFIG_MCAST_TFTP */ |
| 118 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 119 | static __inline__ void |
| 120 | store_block (unsigned block, uchar * src, unsigned len) |
| 121 | { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 122 | ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 123 | ulong newsize = offset + len; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 124 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 125 | int i, rc = 0; |
| 126 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 127 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 128 | /* start address in flash? */ |
Jochen Friedrich | d31a59b | 2008-09-02 11:24:59 +0200 | [diff] [blame] | 129 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
| 130 | continue; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 131 | if (load_addr + offset >= flash_info[i].start[0]) { |
| 132 | rc = 1; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (rc) { /* Flash is destination for this packet */ |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 138 | rc = flash_write ((char *)src, (ulong)(load_addr+offset), len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 139 | if (rc) { |
| 140 | flash_perror (rc); |
| 141 | NetState = NETLOOP_FAIL; |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 146 | #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 147 | { |
| 148 | (void)memcpy((void *)(load_addr + offset), src, len); |
| 149 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 150 | #ifdef CONFIG_MCAST_TFTP |
| 151 | if (Multicast) |
| 152 | ext2_set_bit(block, Bitmap); |
| 153 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 154 | |
| 155 | if (NetBootFileXferSize < newsize) |
| 156 | NetBootFileXferSize = newsize; |
| 157 | } |
| 158 | |
| 159 | static void TftpSend (void); |
| 160 | static void TftpTimeout (void); |
| 161 | |
| 162 | /**********************************************************************/ |
| 163 | |
| 164 | static void |
| 165 | TftpSend (void) |
| 166 | { |
| 167 | volatile uchar * pkt; |
| 168 | volatile uchar * xp; |
| 169 | int len = 0; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 170 | volatile ushort *s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 171 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 172 | #ifdef CONFIG_MCAST_TFTP |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 173 | /* Multicast TFTP.. non-MasterClients do not ACK data. */ |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 174 | if (Multicast |
| 175 | && (TftpState == STATE_DATA) |
| 176 | && (MasterClient == 0)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 177 | return; |
| 178 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 179 | /* |
| 180 | * We will always be sending some sort of packet, so |
| 181 | * cobble together the packet headers now. |
| 182 | */ |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 183 | pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 184 | |
| 185 | switch (TftpState) { |
| 186 | |
| 187 | case STATE_RRQ: |
| 188 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 189 | s = (ushort *)pkt; |
| 190 | *s++ = htons(TFTP_RRQ); |
| 191 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 192 | strcpy ((char *)pkt, tftp_filename); |
| 193 | pkt += strlen(tftp_filename) + 1; |
| 194 | strcpy ((char *)pkt, "octet"); |
| 195 | pkt += 5 /*strlen("octet")*/ + 1; |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 196 | strcpy ((char *)pkt, "timeout"); |
| 197 | pkt += 7 /*strlen("timeout")*/ + 1; |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 198 | sprintf((char *)pkt, "%lu", TIMEOUT / 1000); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 199 | #ifdef ET_DEBUG |
| 200 | printf("send option \"timeout %s\"\n", (char *)pkt); |
| 201 | #endif |
| 202 | pkt += strlen((char *)pkt) + 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 203 | /* try for more effic. blk size */ |
| 204 | pkt += sprintf((char *)pkt,"blksize%c%d%c", |
stefano babic | 7192096 | 2007-08-21 15:52:33 +0200 | [diff] [blame] | 205 | 0,TftpBlkSizeOption,0); |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 206 | #ifdef CONFIG_MCAST_TFTP |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 207 | /* Check all preconditions before even trying the option */ |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 208 | if (!ProhibitMcast |
| 209 | && (Bitmap=malloc(Mapsize)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 210 | && eth_get_dev()->mcast) { |
| 211 | free(Bitmap); |
| 212 | Bitmap=NULL; |
| 213 | pkt += sprintf((char *)pkt,"multicast%c%c",0,0); |
| 214 | } |
| 215 | #endif /* CONFIG_MCAST_TFTP */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 216 | len = pkt - xp; |
| 217 | break; |
| 218 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 219 | case STATE_OACK: |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 220 | #ifdef CONFIG_MCAST_TFTP |
| 221 | /* My turn! Start at where I need blocks I missed.*/ |
| 222 | if (Multicast) |
| 223 | TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0); |
| 224 | /*..falling..*/ |
| 225 | #endif |
| 226 | case STATE_DATA: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 227 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 228 | s = (ushort *)pkt; |
| 229 | *s++ = htons(TFTP_ACK); |
| 230 | *s++ = htons(TftpBlock); |
| 231 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 232 | len = pkt - xp; |
| 233 | break; |
| 234 | |
| 235 | case STATE_TOO_LARGE: |
| 236 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 237 | s = (ushort *)pkt; |
| 238 | *s++ = htons(TFTP_ERROR); |
| 239 | *s++ = htons(3); |
| 240 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 241 | strcpy ((char *)pkt, "File too large"); |
| 242 | pkt += 14 /*strlen("File too large")*/ + 1; |
| 243 | len = pkt - xp; |
| 244 | break; |
| 245 | |
| 246 | case STATE_BAD_MAGIC: |
| 247 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 248 | s = (ushort *)pkt; |
| 249 | *s++ = htons(TFTP_ERROR); |
| 250 | *s++ = htons(2); |
| 251 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 252 | strcpy ((char *)pkt, "File has bad magic"); |
| 253 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
| 254 | len = pkt - xp; |
| 255 | break; |
| 256 | } |
| 257 | |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 258 | NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | static void |
| 263 | TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 264 | { |
| 265 | ushort proto; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 266 | ushort *s; |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 267 | int i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 268 | |
| 269 | if (dest != TftpOurPort) { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 270 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 271 | if (Multicast |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 272 | && (!Mcast_port || (dest != Mcast_port))) |
| 273 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | if (TftpState != STATE_RRQ && src != TftpServerPort) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | if (len < 2) { |
| 281 | return; |
| 282 | } |
| 283 | len -= 2; |
| 284 | /* warning: don't use increment (++) in ntohs() macros!! */ |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 285 | s = (ushort *)pkt; |
| 286 | proto = *s++; |
| 287 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 288 | switch (ntohs(proto)) { |
| 289 | |
| 290 | case TFTP_RRQ: |
| 291 | case TFTP_WRQ: |
| 292 | case TFTP_ACK: |
| 293 | break; |
| 294 | default: |
| 295 | break; |
| 296 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 297 | case TFTP_OACK: |
| 298 | #ifdef ET_DEBUG |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 299 | printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 300 | #endif |
| 301 | TftpState = STATE_OACK; |
| 302 | TftpServerPort = src; |
Wolfgang Denk | 8b70f34 | 2007-08-31 10:01:51 +0200 | [diff] [blame] | 303 | /* |
| 304 | * Check for 'blksize' option. |
| 305 | * Careful: "i" is signed, "len" is unsigned, thus |
| 306 | * something like "len-8" may give a *huge* number |
| 307 | */ |
| 308 | for (i=0; i+8<len; i++) { |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 309 | if (strcmp ((char*)pkt+i,"blksize") == 0) { |
| 310 | TftpBlkSize = (unsigned short) |
| 311 | simple_strtoul((char*)pkt+i+8,NULL,10); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 312 | #ifdef ET_DEBUG |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 313 | printf ("Blocksize ack: %s, %d\n", |
| 314 | (char*)pkt+i+8,TftpBlkSize); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 315 | #endif |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 316 | break; |
| 317 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 318 | } |
| 319 | #ifdef CONFIG_MCAST_TFTP |
| 320 | parse_multicast_oack((char *)pkt,len-1); |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 321 | if ((Multicast) && (!MasterClient)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 322 | TftpState = STATE_DATA; /* passive.. */ |
| 323 | else |
| 324 | #endif |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 325 | TftpSend (); /* Send ACK */ |
| 326 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 327 | case TFTP_DATA: |
| 328 | if (len < 2) |
| 329 | return; |
| 330 | len -= 2; |
| 331 | TftpBlock = ntohs(*(ushort *)pkt); |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 332 | |
| 333 | /* |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 334 | * RFC1350 specifies that the first data packet will |
| 335 | * have sequence number 1. If we receive a sequence |
| 336 | * number of 0 this means that there was a wrap |
| 337 | * around of the (16 bit) counter. |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 338 | */ |
| 339 | if (TftpBlock == 0) { |
| 340 | TftpBlockWrap++; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 341 | TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE; |
Wolfgang Denk | 26442ac | 2006-08-14 22:43:13 +0200 | [diff] [blame] | 342 | printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20); |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 343 | } else { |
| 344 | if (((TftpBlock - 1) % 10) == 0) { |
| 345 | putc ('#'); |
| 346 | } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) { |
| 347 | puts ("\n\t "); |
| 348 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 349 | } |
| 350 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 351 | #ifdef ET_DEBUG |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 352 | if (TftpState == STATE_RRQ) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 353 | puts ("Server did not acknowledge timeout option!\n"); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 354 | } |
| 355 | #endif |
| 356 | |
| 357 | if (TftpState == STATE_RRQ || TftpState == STATE_OACK) { |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 358 | /* first block received */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 359 | TftpState = STATE_DATA; |
| 360 | TftpServerPort = src; |
| 361 | TftpLastBlock = 0; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 362 | TftpBlockWrap = 0; |
| 363 | TftpBlockWrapOffset = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 364 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 365 | #ifdef CONFIG_MCAST_TFTP |
| 366 | if (Multicast) { /* start!=1 common if mcast */ |
| 367 | TftpLastBlock = TftpBlock - 1; |
| 368 | } else |
| 369 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 370 | if (TftpBlock != 1) { /* Assertion */ |
| 371 | printf ("\nTFTP error: " |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 372 | "First block is not block 1 (%ld)\n" |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 373 | "Starting again\n\n", |
| 374 | TftpBlock); |
| 375 | NetStartAgain (); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (TftpBlock == TftpLastBlock) { |
| 381 | /* |
| 382 | * Same block again; ignore it. |
| 383 | */ |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | TftpLastBlock = TftpBlock; |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 388 | TftpTimeoutMSecs = TIMEOUT; |
| 389 | TftpTimeoutCountMax = TIMEOUT_COUNT; |
| 390 | NetSetTimeout (TftpTimeoutMSecs, TftpTimeout); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 391 | |
| 392 | store_block (TftpBlock - 1, pkt + 2, len); |
| 393 | |
| 394 | /* |
| 395 | * Acknoledge the block just received, which will prompt |
| 396 | * the server for the next one. |
| 397 | */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 398 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 399 | /* if I am the MasterClient, actively calculate what my next |
| 400 | * needed block is; else I'm passive; not ACKING |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 401 | */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 402 | if (Multicast) { |
| 403 | if (len < TftpBlkSize) { |
| 404 | TftpEndingBlock = TftpBlock; |
| 405 | } else if (MasterClient) { |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 406 | TftpBlock = PrevBitmapHole = |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 407 | ext2_find_next_zero_bit( |
| 408 | Bitmap, |
| 409 | (Mapsize*8), |
| 410 | PrevBitmapHole); |
| 411 | if (TftpBlock > ((Mapsize*8) - 1)) { |
| 412 | printf ("tftpfile too big\n"); |
| 413 | /* try to double it and retry */ |
| 414 | Mapsize<<=1; |
| 415 | mcast_cleanup(); |
| 416 | NetStartAgain (); |
| 417 | return; |
| 418 | } |
| 419 | TftpLastBlock = TftpBlock; |
| 420 | } |
| 421 | } |
| 422 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 423 | TftpSend (); |
| 424 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 425 | #ifdef CONFIG_MCAST_TFTP |
| 426 | if (Multicast) { |
| 427 | if (MasterClient && (TftpBlock >= TftpEndingBlock)) { |
| 428 | puts ("\nMulticast tftp done\n"); |
| 429 | mcast_cleanup(); |
| 430 | NetState = NETLOOP_SUCCESS; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 431 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 432 | } |
| 433 | else |
| 434 | #endif |
| 435 | if (len < TftpBlkSize) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 436 | /* |
| 437 | * We received the whole thing. Try to |
| 438 | * run it. |
| 439 | */ |
| 440 | puts ("\ndone\n"); |
| 441 | NetState = NETLOOP_SUCCESS; |
| 442 | } |
| 443 | break; |
| 444 | |
| 445 | case TFTP_ERROR: |
| 446 | printf ("\nTFTP error: '%s' (%d)\n", |
| 447 | pkt + 2, ntohs(*(ushort *)pkt)); |
| 448 | puts ("Starting again\n\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 449 | #ifdef CONFIG_MCAST_TFTP |
| 450 | mcast_cleanup(); |
| 451 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 452 | NetStartAgain (); |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | static void |
| 459 | TftpTimeout (void) |
| 460 | { |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 461 | if (++TftpTimeoutCount > TftpTimeoutCountMax) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 462 | puts ("\nRetry count exceeded; starting again\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 463 | #ifdef CONFIG_MCAST_TFTP |
| 464 | mcast_cleanup(); |
| 465 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 466 | NetStartAgain (); |
| 467 | } else { |
| 468 | puts ("T "); |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 469 | NetSetTimeout (TftpTimeoutMSecs, TftpTimeout); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 470 | TftpSend (); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void |
| 476 | TftpStart (void) |
| 477 | { |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 478 | #ifdef CONFIG_TFTP_PORT |
| 479 | char *ep; /* Environment pointer */ |
| 480 | #endif |
| 481 | |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 482 | TftpServerIP = NetServerIP; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 483 | if (BootFile[0] == '\0') { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 484 | sprintf(default_filename, "%02lX%02lX%02lX%02lX.img", |
Wolfgang Denk | dec9732 | 2005-08-04 01:09:44 +0200 | [diff] [blame] | 485 | NetOurIP & 0xFF, |
| 486 | (NetOurIP >> 8) & 0xFF, |
| 487 | (NetOurIP >> 16) & 0xFF, |
| 488 | (NetOurIP >> 24) & 0xFF ); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 489 | |
| 490 | strncpy(tftp_filename, default_filename, MAX_LEN); |
| 491 | tftp_filename[MAX_LEN-1] = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 492 | |
| 493 | printf ("*** Warning: no boot file name; using '%s'\n", |
| 494 | tftp_filename); |
| 495 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | fa1dfde | 2008-02-14 08:02:12 +0100 | [diff] [blame] | 496 | char *p = strchr (BootFile, ':'); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 497 | |
| 498 | if (p == NULL) { |
| 499 | strncpy(tftp_filename, BootFile, MAX_LEN); |
| 500 | tftp_filename[MAX_LEN-1] = 0; |
| 501 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 502 | TftpServerIP = string_to_ip (BootFile); |
Peter Tyser | 5e58efa | 2008-12-01 16:29:38 -0600 | [diff] [blame] | 503 | strncpy(tftp_filename, p + 1, MAX_LEN); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 504 | tftp_filename[MAX_LEN-1] = 0; |
| 505 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 506 | } |
| 507 | |
wdenk | c6abb7e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 508 | #if defined(CONFIG_NET_MULTI) |
| 509 | printf ("Using %s device\n", eth_get_name()); |
| 510 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 511 | puts ("TFTP from server "); print_IPaddr (TftpServerIP); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 512 | puts ("; our IP address is "); print_IPaddr (NetOurIP); |
| 513 | |
| 514 | /* Check if we need to send across this subnet */ |
| 515 | if (NetOurGatewayIP && NetOurSubnetMask) { |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 516 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
| 517 | IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 518 | |
| 519 | if (OurNet != ServerNet) { |
| 520 | puts ("; sending through gateway "); |
| 521 | print_IPaddr (NetOurGatewayIP) ; |
| 522 | } |
| 523 | } |
| 524 | putc ('\n'); |
| 525 | |
| 526 | printf ("Filename '%s'.", tftp_filename); |
| 527 | |
| 528 | if (NetBootFileSize) { |
| 529 | printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
| 530 | print_size (NetBootFileSize<<9, ""); |
| 531 | } |
| 532 | |
| 533 | putc ('\n'); |
| 534 | |
| 535 | printf ("Load address: 0x%lx\n", load_addr); |
| 536 | |
| 537 | puts ("Loading: *\b"); |
| 538 | |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 539 | TftpTimeoutMSecs = TftpRRQTimeoutMSecs; |
| 540 | TftpTimeoutCountMax = TftpRRQTimeoutCountMax; |
| 541 | |
| 542 | NetSetTimeout (TftpTimeoutMSecs, TftpTimeout); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 543 | NetSetHandler (TftpHandler); |
| 544 | |
| 545 | TftpServerPort = WELL_KNOWN_PORT; |
| 546 | TftpTimeoutCount = 0; |
| 547 | TftpState = STATE_RRQ; |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 548 | /* Use a pseudo-random port unless a specific port is set */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 549 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 550 | |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 551 | #ifdef CONFIG_TFTP_PORT |
Wolfgang Denk | 227b519 | 2005-09-24 23:25:46 +0200 | [diff] [blame] | 552 | if ((ep = getenv("tftpdstp")) != NULL) { |
| 553 | TftpServerPort = simple_strtol(ep, NULL, 10); |
| 554 | } |
| 555 | if ((ep = getenv("tftpsrcp")) != NULL) { |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 556 | TftpOurPort= simple_strtol(ep, NULL, 10); |
| 557 | } |
| 558 | #endif |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 559 | TftpBlock = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 560 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 561 | /* zero out server ether in case the server ip has changed */ |
| 562 | memset(NetServerEther, 0, 6); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 563 | /* Revert TftpBlkSize to dflt */ |
| 564 | TftpBlkSize = TFTP_BLOCK_SIZE; |
| 565 | #ifdef CONFIG_MCAST_TFTP |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 566 | mcast_cleanup(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 567 | #endif |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 568 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 569 | TftpSend (); |
| 570 | } |
| 571 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 572 | #ifdef CONFIG_MCAST_TFTP |
| 573 | /* Credits: atftp project. |
| 574 | */ |
| 575 | |
| 576 | /* pick up BcastAddr, Port, and whether I am [now] the master-client. * |
| 577 | * Frame: |
| 578 | * +-------+-----------+---+-------~~-------+---+ |
| 579 | * | opc | multicast | 0 | addr, port, mc | 0 | |
| 580 | * +-------+-----------+---+-------~~-------+---+ |
| 581 | * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then |
| 582 | * I am the new master-client so must send ACKs to DataBlocks. If I am not |
| 583 | * master-client, I'm a passive client, gathering what DataBlocks I may and |
| 584 | * making note of which ones I got in my bitmask. |
| 585 | * In theory, I never go from master->passive.. |
| 586 | * .. this comes in with pkt already pointing just past opc |
| 587 | */ |
| 588 | static void parse_multicast_oack(char *pkt, int len) |
| 589 | { |
| 590 | int i; |
| 591 | IPaddr_t addr; |
| 592 | char *mc_adr, *port, *mc; |
| 593 | |
| 594 | mc_adr=port=mc=NULL; |
| 595 | /* march along looking for 'multicast\0', which has to start at least |
| 596 | * 14 bytes back from the end. |
| 597 | */ |
| 598 | for (i=0;i<len-14;i++) |
| 599 | if (strcmp (pkt+i,"multicast") == 0) |
| 600 | break; |
| 601 | if (i >= (len-14)) /* non-Multicast OACK, ign. */ |
| 602 | return; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 603 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 604 | i+=10; /* strlen multicast */ |
| 605 | mc_adr = pkt+i; |
| 606 | for (;i<len;i++) { |
| 607 | if (*(pkt+i) == ',') { |
| 608 | *(pkt+i) = '\0'; |
| 609 | if (port) { |
| 610 | mc = pkt+i+1; |
| 611 | break; |
| 612 | } else { |
| 613 | port = pkt+i+1; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | if (!port || !mc_adr || !mc ) return; |
| 618 | if (Multicast && MasterClient) { |
| 619 | printf ("I got a OACK as master Client, WRONG!\n"); |
| 620 | return; |
| 621 | } |
| 622 | /* ..I now accept packets destined for this MCAST addr, port */ |
| 623 | if (!Multicast) { |
| 624 | if (Bitmap) { |
| 625 | printf ("Internal failure! no mcast.\n"); |
| 626 | free(Bitmap); |
| 627 | Bitmap=NULL; |
| 628 | ProhibitMcast=1; |
| 629 | return ; |
| 630 | } |
| 631 | /* I malloc instead of pre-declare; so that if the file ends |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 632 | * up being too big for this bitmap I can retry |
| 633 | */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 634 | if (!(Bitmap = malloc (Mapsize))) { |
| 635 | printf ("No Bitmap, no multicast. Sorry.\n"); |
| 636 | ProhibitMcast=1; |
| 637 | return; |
| 638 | } |
| 639 | memset (Bitmap,0,Mapsize); |
| 640 | PrevBitmapHole = 0; |
| 641 | Multicast = 1; |
| 642 | } |
| 643 | addr = string_to_ip(mc_adr); |
| 644 | if (Mcast_addr != addr) { |
| 645 | if (Mcast_addr) |
| 646 | eth_mcast_join(Mcast_addr, 0); |
| 647 | if (eth_mcast_join(Mcast_addr=addr, 1)) { |
| 648 | printf ("Fail to set mcast, revert to TFTP\n"); |
| 649 | ProhibitMcast=1; |
| 650 | mcast_cleanup(); |
| 651 | NetStartAgain(); |
| 652 | } |
| 653 | } |
| 654 | MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10); |
| 655 | Mcast_port = (unsigned short)simple_strtoul(port,NULL,10); |
| 656 | printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient); |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | #endif /* Multicast TFTP */ |
| 661 | |
Jon Loeliger | 5615ef2 | 2007-08-15 11:55:35 -0500 | [diff] [blame] | 662 | #endif |