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