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