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