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