wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 2 | * Copyright 1994, 1995, 2000 Neil Russell. |
| 3 | * (See License) |
| 4 | * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 5 | * Copyright 2011 Comelit Group SpA, |
| 6 | * Luca Ceresoli <luca.ceresoli@comelit.it> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <net.h> |
| 12 | #include "tftp.h" |
| 13 | #include "bootp.h" |
| 14 | |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 15 | /* Well known TFTP port # */ |
| 16 | #define WELL_KNOWN_PORT 69 |
| 17 | /* Millisecs to timeout for lost pkt */ |
| 18 | #define TIMEOUT 5000UL |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 19 | #ifndef CONFIG_NET_RETRY_COUNT |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 20 | /* # of timeouts before giving up */ |
| 21 | # define TIMEOUT_COUNT 10 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 22 | #else |
| 23 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) |
| 24 | #endif |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 25 | /* Number of "loading" hashes per line (for checking the image size) */ |
| 26 | #define HASHES_PER_LINE 65 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * TFTP operations. |
| 30 | */ |
| 31 | #define TFTP_RRQ 1 |
| 32 | #define TFTP_WRQ 2 |
| 33 | #define TFTP_DATA 3 |
| 34 | #define TFTP_ACK 4 |
| 35 | #define TFTP_ERROR 5 |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 36 | #define TFTP_OACK 6 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 37 | |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 38 | static ulong TftpTimeoutMSecs = TIMEOUT; |
| 39 | static int TftpTimeoutCountMax = TIMEOUT_COUNT; |
| 40 | |
| 41 | /* |
| 42 | * These globals govern the timeout behavior when attempting a connection to a |
| 43 | * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to |
| 44 | * wait for the server to respond to initial connection. Second global, |
| 45 | * TftpRRQTimeoutCountMax, gives the number of such connection retries. |
| 46 | * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be |
| 47 | * positive. The globals are meant to be set (and restored) by code needing |
| 48 | * non-standard timeout behavior when initiating a TFTP transfer. |
| 49 | */ |
| 50 | ulong TftpRRQTimeoutMSecs = TIMEOUT; |
| 51 | int TftpRRQTimeoutCountMax = TIMEOUT_COUNT; |
| 52 | |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 53 | enum { |
| 54 | TFTP_ERR_UNDEFINED = 0, |
| 55 | TFTP_ERR_FILE_NOT_FOUND = 1, |
| 56 | TFTP_ERR_ACCESS_DENIED = 2, |
| 57 | TFTP_ERR_DISK_FULL = 3, |
| 58 | TFTP_ERR_UNEXPECTED_OPCODE = 4, |
| 59 | TFTP_ERR_UNKNOWN_TRANSFER_ID = 5, |
| 60 | TFTP_ERR_FILE_ALREADY_EXISTS = 6, |
| 61 | }; |
| 62 | |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 63 | static IPaddr_t TftpRemoteIP; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 64 | /* The UDP port at their end */ |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 65 | static int TftpRemotePort; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 66 | /* The UDP port at our end */ |
| 67 | static int TftpOurPort; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 68 | static int TftpTimeoutCount; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 69 | /* packet sequence number */ |
| 70 | static ulong TftpBlock; |
| 71 | /* last packet sequence number received */ |
| 72 | static ulong TftpLastBlock; |
| 73 | /* count of sequence number wraparounds */ |
| 74 | static ulong TftpBlockWrap; |
| 75 | /* memory offset due to wrapping */ |
| 76 | static ulong TftpBlockWrapOffset; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 77 | static int TftpState; |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 78 | #ifdef CONFIG_TFTP_TSIZE |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 79 | /* The file size reported by the server */ |
| 80 | static int TftpTsize; |
| 81 | /* The number of hashes we printed */ |
| 82 | static short TftpNumchars; |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 83 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_CMD_TFTPPUT |
| 85 | static int TftpWriting; /* 1 if writing, else 0 */ |
| 86 | static int TftpFinalBlock; /* 1 if we have sent the last block */ |
| 87 | #else |
| 88 | #define TftpWriting 0 |
| 89 | #endif |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 90 | |
Luca Ceresoli | 562dfe5 | 2011-05-17 00:03:38 +0000 | [diff] [blame] | 91 | #define STATE_SEND_RRQ 1 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 92 | #define STATE_DATA 2 |
| 93 | #define STATE_TOO_LARGE 3 |
| 94 | #define STATE_BAD_MAGIC 4 |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 95 | #define STATE_OACK 5 |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 96 | #define STATE_RECV_WRQ 6 |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 97 | #define STATE_SEND_WRQ 7 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 98 | |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 99 | /* default TFTP block size */ |
| 100 | #define TFTP_BLOCK_SIZE 512 |
| 101 | /* sequence number is 16 bit */ |
| 102 | #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 103 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 104 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
| 105 | static char default_filename[DEFAULT_NAME_LEN]; |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 106 | |
| 107 | #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN |
| 108 | #define MAX_LEN 128 |
| 109 | #else |
| 110 | #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN |
| 111 | #endif |
| 112 | |
| 113 | static char tftp_filename[MAX_LEN]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 114 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 115 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 116 | extern flash_info_t flash_info[]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 119 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
| 120 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 121 | * 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] | 122 | * (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] | 123 | */ |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 124 | #ifdef CONFIG_TFTP_BLOCKSIZE |
| 125 | #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE |
| 126 | #else |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 127 | #define TFTP_MTU_BLOCKSIZE 1468 |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 128 | #endif |
| 129 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 130 | static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE; |
| 131 | static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 132 | |
| 133 | #ifdef CONFIG_MCAST_TFTP |
| 134 | #include <malloc.h> |
| 135 | #define MTFTP_BITMAPSIZE 0x1000 |
| 136 | static unsigned *Bitmap; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 137 | static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE; |
Luca Ceresoli | 4e7a02b | 2011-05-14 05:50:03 +0000 | [diff] [blame] | 138 | static uchar ProhibitMcast, MasterClient; |
| 139 | static uchar Multicast; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 140 | extern IPaddr_t Mcast_addr; |
| 141 | static int Mcast_port; |
| 142 | static ulong TftpEndingBlock; /* can get 'last' block before done..*/ |
| 143 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 144 | static void parse_multicast_oack(char *pkt, int len); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 145 | |
| 146 | static void |
| 147 | mcast_cleanup(void) |
| 148 | { |
Luca Ceresoli | 6072fcd | 2011-05-14 05:50:01 +0000 | [diff] [blame] | 149 | if (Mcast_addr) |
| 150 | eth_mcast_join(Mcast_addr, 0); |
| 151 | if (Bitmap) |
| 152 | free(Bitmap); |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 153 | Bitmap = NULL; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 154 | Mcast_addr = Multicast = Mcast_port = 0; |
| 155 | TftpEndingBlock = -1; |
| 156 | } |
| 157 | |
| 158 | #endif /* CONFIG_MCAST_TFTP */ |
| 159 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 160 | static __inline__ void |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 161 | store_block(unsigned block, uchar *src, unsigned len) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 162 | { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 163 | ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 164 | ulong newsize = offset + len; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 165 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 166 | int i, rc = 0; |
| 167 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 168 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 169 | /* start address in flash? */ |
Jochen Friedrich | d31a59b | 2008-09-02 11:24:59 +0200 | [diff] [blame] | 170 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
| 171 | continue; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 172 | if (load_addr + offset >= flash_info[i].start[0]) { |
| 173 | rc = 1; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (rc) { /* Flash is destination for this packet */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 179 | rc = flash_write((char *)src, (ulong)(load_addr+offset), len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 180 | if (rc) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 181 | flash_perror(rc); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 182 | NetState = NETLOOP_FAIL; |
| 183 | return; |
| 184 | } |
| 185 | } |
| 186 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 187 | #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 188 | { |
| 189 | (void)memcpy((void *)(load_addr + offset), src, len); |
| 190 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 191 | #ifdef CONFIG_MCAST_TFTP |
| 192 | if (Multicast) |
| 193 | ext2_set_bit(block, Bitmap); |
| 194 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 195 | |
| 196 | if (NetBootFileXferSize < newsize) |
| 197 | NetBootFileXferSize = newsize; |
| 198 | } |
| 199 | |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 200 | /* Clear our state ready for a new transfer */ |
| 201 | void new_transfer(void) |
| 202 | { |
| 203 | TftpLastBlock = 0; |
| 204 | TftpBlockWrap = 0; |
| 205 | TftpBlockWrapOffset = 0; |
| 206 | #ifdef CONFIG_CMD_TFTPPUT |
| 207 | TftpFinalBlock = 0; |
| 208 | #endif |
| 209 | } |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 210 | |
| 211 | #ifdef CONFIG_CMD_TFTPPUT |
| 212 | /** |
| 213 | * Load the next block from memory to be sent over tftp. |
| 214 | * |
| 215 | * @param block Block number to send |
| 216 | * @param dst Destination buffer for data |
| 217 | * @param len Number of bytes in block (this one and every other) |
| 218 | * @return number of bytes loaded |
| 219 | */ |
| 220 | static int load_block(unsigned block, uchar *dst, unsigned len) |
| 221 | { |
| 222 | /* We may want to get the final block from the previous set */ |
| 223 | ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset; |
| 224 | ulong tosend = len; |
| 225 | |
| 226 | tosend = min(NetBootFileXferSize - offset, tosend); |
| 227 | (void)memcpy(dst, (void *)(save_addr + offset), tosend); |
| 228 | debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__, |
| 229 | block, offset, len, tosend); |
| 230 | return tosend; |
| 231 | } |
| 232 | #endif |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 233 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 234 | static void TftpSend(void); |
| 235 | static void TftpTimeout(void); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 236 | |
| 237 | /**********************************************************************/ |
| 238 | |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 239 | static void show_block_marker(void) |
| 240 | { |
| 241 | #ifdef CONFIG_TFTP_TSIZE |
| 242 | if (TftpTsize) { |
| 243 | ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset; |
| 244 | |
| 245 | while (TftpNumchars < pos * 50 / TftpTsize) { |
| 246 | putc('#'); |
| 247 | TftpNumchars++; |
| 248 | } |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 249 | } else |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 250 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 251 | { |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 252 | if (((TftpBlock - 1) % 10) == 0) |
| 253 | putc('#'); |
| 254 | else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) |
| 255 | puts("\n\t "); |
| 256 | } |
| 257 | } |
| 258 | |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 259 | /** |
| 260 | * restart the current transfer due to an error |
| 261 | * |
| 262 | * @param msg Message to print for user |
| 263 | */ |
| 264 | static void restart(const char *msg) |
| 265 | { |
| 266 | printf("\n%s; starting again\n", msg); |
| 267 | #ifdef CONFIG_MCAST_TFTP |
| 268 | mcast_cleanup(); |
| 269 | #endif |
| 270 | NetStartAgain(); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Check if the block number has wrapped, and update progress |
| 275 | * |
| 276 | * TODO: The egregious use of global variables in this file should be tidied. |
| 277 | */ |
| 278 | static void update_block_number(void) |
| 279 | { |
| 280 | /* |
| 281 | * RFC1350 specifies that the first data packet will |
| 282 | * have sequence number 1. If we receive a sequence |
| 283 | * number of 0 this means that there was a wrap |
| 284 | * around of the (16 bit) counter. |
| 285 | */ |
| 286 | if (TftpBlock == 0) { |
| 287 | TftpBlockWrap++; |
| 288 | TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE; |
| 289 | TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */ |
| 290 | } else { |
| 291 | show_block_marker(); |
| 292 | } |
| 293 | } |
| 294 | |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 295 | /* The TFTP get or put is complete */ |
| 296 | static void tftp_complete(void) |
| 297 | { |
| 298 | #ifdef CONFIG_TFTP_TSIZE |
| 299 | /* Print hash marks for the last packet received */ |
| 300 | while (TftpTsize && TftpNumchars < 49) { |
| 301 | putc('#'); |
| 302 | TftpNumchars++; |
| 303 | } |
| 304 | #endif |
| 305 | puts("\ndone\n"); |
| 306 | NetState = NETLOOP_SUCCESS; |
| 307 | } |
| 308 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 309 | static void |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 310 | TftpSend(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 311 | { |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 312 | uchar *pkt; |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 313 | volatile uchar *xp; |
| 314 | int len = 0; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 315 | volatile ushort *s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 316 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 317 | #ifdef CONFIG_MCAST_TFTP |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 318 | /* Multicast TFTP.. non-MasterClients do not ACK data. */ |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 319 | if (Multicast |
| 320 | && (TftpState == STATE_DATA) |
| 321 | && (MasterClient == 0)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 322 | return; |
| 323 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 324 | /* |
| 325 | * We will always be sending some sort of packet, so |
| 326 | * cobble together the packet headers now. |
| 327 | */ |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 328 | pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 329 | |
| 330 | switch (TftpState) { |
Luca Ceresoli | 562dfe5 | 2011-05-17 00:03:38 +0000 | [diff] [blame] | 331 | case STATE_SEND_RRQ: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 332 | case STATE_SEND_WRQ: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 333 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 334 | s = (ushort *)pkt; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 335 | *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ : |
| 336 | TFTP_WRQ); |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 337 | pkt = (uchar *)s; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 338 | strcpy((char *)pkt, tftp_filename); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 339 | pkt += strlen(tftp_filename) + 1; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 340 | strcpy((char *)pkt, "octet"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 341 | pkt += 5 /*strlen("octet")*/ + 1; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 342 | strcpy((char *)pkt, "timeout"); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 343 | pkt += 7 /*strlen("timeout")*/ + 1; |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 344 | sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 345 | debug("send option \"timeout %s\"\n", (char *)pkt); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 346 | pkt += strlen((char *)pkt) + 1; |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 347 | #ifdef CONFIG_TFTP_TSIZE |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 348 | pkt += sprintf((char *)pkt, "tsize%c%lu%c", |
| 349 | 0, NetBootFileXferSize, 0); |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 350 | #endif |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 351 | /* try for more effic. blk size */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 352 | pkt += sprintf((char *)pkt, "blksize%c%d%c", |
| 353 | 0, TftpBlkSizeOption, 0); |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 354 | #ifdef CONFIG_MCAST_TFTP |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 355 | /* Check all preconditions before even trying the option */ |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 356 | if (!ProhibitMcast |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 357 | && (Bitmap = malloc(Mapsize)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 358 | && eth_get_dev()->mcast) { |
| 359 | free(Bitmap); |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 360 | Bitmap = NULL; |
| 361 | pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 362 | } |
| 363 | #endif /* CONFIG_MCAST_TFTP */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 364 | len = pkt - xp; |
| 365 | break; |
| 366 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 367 | case STATE_OACK: |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 368 | #ifdef CONFIG_MCAST_TFTP |
| 369 | /* My turn! Start at where I need blocks I missed.*/ |
| 370 | if (Multicast) |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 371 | TftpBlock = ext2_find_next_zero_bit(Bitmap, |
| 372 | (Mapsize*8), 0); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 373 | /*..falling..*/ |
| 374 | #endif |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 375 | |
| 376 | case STATE_RECV_WRQ: |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 377 | case STATE_DATA: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 378 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 379 | s = (ushort *)pkt; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 380 | s[0] = htons(TFTP_ACK); |
| 381 | s[1] = htons(TftpBlock); |
| 382 | pkt = (uchar *)(s + 2); |
| 383 | #ifdef CONFIG_CMD_TFTPPUT |
| 384 | if (TftpWriting) { |
| 385 | int toload = TftpBlkSize; |
| 386 | int loaded = load_block(TftpBlock, pkt, toload); |
| 387 | |
| 388 | s[0] = htons(TFTP_DATA); |
| 389 | pkt += loaded; |
| 390 | TftpFinalBlock = (loaded < toload); |
| 391 | } |
| 392 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 393 | len = pkt - xp; |
| 394 | break; |
| 395 | |
| 396 | case STATE_TOO_LARGE: |
| 397 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 398 | s = (ushort *)pkt; |
| 399 | *s++ = htons(TFTP_ERROR); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 400 | *s++ = htons(3); |
| 401 | |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 402 | pkt = (uchar *)s; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 403 | strcpy((char *)pkt, "File too large"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 404 | pkt += 14 /*strlen("File too large")*/ + 1; |
| 405 | len = pkt - xp; |
| 406 | break; |
| 407 | |
| 408 | case STATE_BAD_MAGIC: |
| 409 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 410 | s = (ushort *)pkt; |
| 411 | *s++ = htons(TFTP_ERROR); |
| 412 | *s++ = htons(2); |
| 413 | pkt = (uchar *)s; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 414 | strcpy((char *)pkt, "File has bad magic"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 415 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
| 416 | len = pkt - xp; |
| 417 | break; |
| 418 | } |
| 419 | |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 420 | NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort, |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 421 | TftpOurPort, len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 424 | |
| 425 | static void icmp_handler(unsigned type, unsigned code, unsigned dest, |
| 426 | IPaddr_t sip, unsigned src, uchar *pkt, unsigned len) |
| 427 | { |
| 428 | if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) { |
| 429 | /* Oh dear the other end has gone away */ |
| 430 | restart("TFTP server died"); |
| 431 | } |
| 432 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 433 | |
| 434 | static void |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 435 | TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 436 | unsigned len) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 437 | { |
| 438 | ushort proto; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 439 | ushort *s; |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 440 | int i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 441 | |
| 442 | if (dest != TftpOurPort) { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 443 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 444 | if (Multicast |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 445 | && (!Mcast_port || (dest != Mcast_port))) |
| 446 | #endif |
Luca Ceresoli | 09b1823 | 2011-05-14 05:50:02 +0000 | [diff] [blame] | 447 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 448 | } |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 449 | if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort && |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 450 | TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 451 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 452 | |
Luca Ceresoli | 35dfb18 | 2011-05-14 05:50:00 +0000 | [diff] [blame] | 453 | if (len < 2) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 454 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 455 | len -= 2; |
| 456 | /* warning: don't use increment (++) in ntohs() macros!! */ |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 457 | s = (ushort *)pkt; |
| 458 | proto = *s++; |
| 459 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 460 | switch (ntohs(proto)) { |
| 461 | |
| 462 | case TFTP_RRQ: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 463 | break; |
| 464 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 465 | case TFTP_ACK: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 466 | #ifdef CONFIG_CMD_TFTPPUT |
| 467 | if (TftpWriting) { |
| 468 | if (TftpFinalBlock) { |
| 469 | tftp_complete(); |
| 470 | } else { |
| 471 | /* |
| 472 | * Move to the next block. We want our block |
| 473 | * count to wrap just like the other end! |
| 474 | */ |
| 475 | int block = ntohs(*s); |
| 476 | int ack_ok = (TftpBlock == block); |
| 477 | |
| 478 | TftpBlock = (unsigned short)(block + 1); |
| 479 | update_block_number(); |
| 480 | if (ack_ok) |
| 481 | TftpSend(); /* Send next data block */ |
| 482 | } |
| 483 | } |
| 484 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 485 | break; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 486 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 487 | default: |
| 488 | break; |
| 489 | |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 490 | #ifdef CONFIG_CMD_TFTPSRV |
| 491 | case TFTP_WRQ: |
| 492 | debug("Got WRQ\n"); |
| 493 | TftpRemoteIP = sip; |
| 494 | TftpRemotePort = src; |
| 495 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 496 | new_transfer(); |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 497 | TftpSend(); /* Send ACK(0) */ |
| 498 | break; |
| 499 | #endif |
| 500 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 501 | case TFTP_OACK: |
Wolfgang Denk | 12730c7 | 2009-08-10 09:59:10 +0200 | [diff] [blame] | 502 | debug("Got OACK: %s %s\n", |
| 503 | pkt, |
| 504 | pkt + strlen((char *)pkt) + 1); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 505 | TftpState = STATE_OACK; |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 506 | TftpRemotePort = src; |
Wolfgang Denk | 8b70f34 | 2007-08-31 10:01:51 +0200 | [diff] [blame] | 507 | /* |
| 508 | * Check for 'blksize' option. |
| 509 | * Careful: "i" is signed, "len" is unsigned, thus |
| 510 | * something like "len-8" may give a *huge* number |
| 511 | */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 512 | for (i = 0; i+8 < len; i++) { |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 513 | if (strcmp((char *)pkt+i, "blksize") == 0) { |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 514 | TftpBlkSize = (unsigned short) |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 515 | simple_strtoul((char *)pkt+i+8, NULL, |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 516 | 10); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 517 | debug("Blocksize ack: %s, %d\n", |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 518 | (char *)pkt+i+8, TftpBlkSize); |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 519 | } |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 520 | #ifdef CONFIG_TFTP_TSIZE |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 521 | if (strcmp((char *)pkt+i, "tsize") == 0) { |
| 522 | TftpTsize = simple_strtoul((char *)pkt+i+6, |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 523 | NULL, 10); |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 524 | debug("size = %s, %d\n", |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 525 | (char *)pkt+i+6, TftpTsize); |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 526 | } |
| 527 | #endif |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 528 | } |
| 529 | #ifdef CONFIG_MCAST_TFTP |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 530 | parse_multicast_oack((char *)pkt, len-1); |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 531 | if ((Multicast) && (!MasterClient)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 532 | TftpState = STATE_DATA; /* passive.. */ |
| 533 | else |
| 534 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 535 | #ifdef CONFIG_CMD_TFTPPUT |
| 536 | if (TftpWriting) { |
| 537 | /* Get ready to send the first block */ |
| 538 | TftpState = STATE_DATA; |
| 539 | TftpBlock++; |
| 540 | } |
| 541 | #endif |
| 542 | TftpSend(); /* Send ACK or first data block */ |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 543 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 544 | case TFTP_DATA: |
| 545 | if (len < 2) |
| 546 | return; |
| 547 | len -= 2; |
| 548 | TftpBlock = ntohs(*(ushort *)pkt); |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 549 | |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 550 | update_block_number(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 551 | |
Luca Ceresoli | 562dfe5 | 2011-05-17 00:03:38 +0000 | [diff] [blame] | 552 | if (TftpState == STATE_SEND_RRQ) |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 553 | debug("Server did not acknowledge timeout option!\n"); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 554 | |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 555 | if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK || |
| 556 | TftpState == STATE_RECV_WRQ) { |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 557 | /* first block received */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 558 | TftpState = STATE_DATA; |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 559 | TftpRemotePort = src; |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 560 | new_transfer(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 561 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 562 | #ifdef CONFIG_MCAST_TFTP |
| 563 | if (Multicast) { /* start!=1 common if mcast */ |
| 564 | TftpLastBlock = TftpBlock - 1; |
| 565 | } else |
| 566 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 567 | if (TftpBlock != 1) { /* Assertion */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 568 | printf("\nTFTP error: " |
| 569 | "First block is not block 1 (%ld)\n" |
| 570 | "Starting again\n\n", |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 571 | TftpBlock); |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 572 | NetStartAgain(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (TftpBlock == TftpLastBlock) { |
| 578 | /* |
| 579 | * Same block again; ignore it. |
| 580 | */ |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | TftpLastBlock = TftpBlock; |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 585 | TftpTimeoutCountMax = TIMEOUT_COUNT; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 586 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 587 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 588 | store_block(TftpBlock - 1, pkt + 2, len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 589 | |
| 590 | /* |
Luca Ceresoli | 2f3dc0a | 2011-05-17 00:03:41 +0000 | [diff] [blame] | 591 | * Acknowledge the block just received, which will prompt |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 592 | * the remote for the next one. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 593 | */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 594 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 595 | /* if I am the MasterClient, actively calculate what my next |
| 596 | * needed block is; else I'm passive; not ACKING |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 597 | */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 598 | if (Multicast) { |
| 599 | if (len < TftpBlkSize) { |
| 600 | TftpEndingBlock = TftpBlock; |
| 601 | } else if (MasterClient) { |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 602 | TftpBlock = PrevBitmapHole = |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 603 | ext2_find_next_zero_bit( |
| 604 | Bitmap, |
| 605 | (Mapsize*8), |
| 606 | PrevBitmapHole); |
| 607 | if (TftpBlock > ((Mapsize*8) - 1)) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 608 | printf("tftpfile too big\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 609 | /* try to double it and retry */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 610 | Mapsize <<= 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 611 | mcast_cleanup(); |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 612 | NetStartAgain(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | TftpLastBlock = TftpBlock; |
| 616 | } |
| 617 | } |
| 618 | #endif |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 619 | TftpSend(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 620 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 621 | #ifdef CONFIG_MCAST_TFTP |
| 622 | if (Multicast) { |
| 623 | if (MasterClient && (TftpBlock >= TftpEndingBlock)) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 624 | puts("\nMulticast tftp done\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 625 | mcast_cleanup(); |
| 626 | NetState = NETLOOP_SUCCESS; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 627 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 628 | } |
| 629 | else |
| 630 | #endif |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 631 | if (len < TftpBlkSize) |
| 632 | tftp_complete(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 633 | break; |
| 634 | |
| 635 | case TFTP_ERROR: |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 636 | printf("\nTFTP error: '%s' (%d)\n", |
| 637 | pkt + 2, ntohs(*(ushort *)pkt)); |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 638 | |
| 639 | switch (ntohs(*(ushort *)pkt)) { |
| 640 | case TFTP_ERR_FILE_NOT_FOUND: |
| 641 | case TFTP_ERR_ACCESS_DENIED: |
| 642 | puts("Not retrying...\n"); |
| 643 | eth_halt(); |
| 644 | NetState = NETLOOP_FAIL; |
| 645 | break; |
| 646 | case TFTP_ERR_UNDEFINED: |
| 647 | case TFTP_ERR_DISK_FULL: |
| 648 | case TFTP_ERR_UNEXPECTED_OPCODE: |
| 649 | case TFTP_ERR_UNKNOWN_TRANSFER_ID: |
| 650 | case TFTP_ERR_FILE_ALREADY_EXISTS: |
| 651 | default: |
| 652 | puts("Starting again\n\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 653 | #ifdef CONFIG_MCAST_TFTP |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 654 | mcast_cleanup(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 655 | #endif |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 656 | NetStartAgain(); |
| 657 | break; |
| 658 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | |
| 664 | static void |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 665 | TftpTimeout(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 666 | { |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 667 | if (++TftpTimeoutCount > TftpTimeoutCountMax) { |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 668 | restart("Retry count exceeded"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 669 | } else { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 670 | puts("T "); |
| 671 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 672 | if (TftpState != STATE_RECV_WRQ) |
| 673 | TftpSend(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
| 677 | |
Simon Glass | f59675a | 2011-10-24 18:00:05 +0000 | [diff] [blame] | 678 | void TftpStart(enum proto_t protocol) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 679 | { |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 680 | char *ep; /* Environment pointer */ |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 681 | |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 682 | /* |
| 683 | * Allow the user to choose TFTP blocksize and timeout. |
| 684 | * TFTP protocol has a minimal timeout of 1 second. |
| 685 | */ |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 686 | ep = getenv("tftpblocksize"); |
| 687 | if (ep != NULL) |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 688 | TftpBlkSizeOption = simple_strtol(ep, NULL, 10); |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 689 | |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 690 | ep = getenv("tftptimeout"); |
| 691 | if (ep != NULL) |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 692 | TftpTimeoutMSecs = simple_strtol(ep, NULL, 10); |
| 693 | |
| 694 | if (TftpTimeoutMSecs < 1000) { |
| 695 | printf("TFTP timeout (%ld ms) too low, " |
| 696 | "set minimum = 1000 ms\n", |
| 697 | TftpTimeoutMSecs); |
| 698 | TftpTimeoutMSecs = 1000; |
| 699 | } |
| 700 | |
| 701 | debug("TFTP blocksize = %i, timeout = %ld ms\n", |
| 702 | TftpBlkSizeOption, TftpTimeoutMSecs); |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 703 | |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 704 | TftpRemoteIP = NetServerIP; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 705 | if (BootFile[0] == '\0') { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 706 | sprintf(default_filename, "%02lX%02lX%02lX%02lX.img", |
Wolfgang Denk | dec9732 | 2005-08-04 01:09:44 +0200 | [diff] [blame] | 707 | NetOurIP & 0xFF, |
| 708 | (NetOurIP >> 8) & 0xFF, |
| 709 | (NetOurIP >> 16) & 0xFF, |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 710 | (NetOurIP >> 24) & 0xFF); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 711 | |
| 712 | strncpy(tftp_filename, default_filename, MAX_LEN); |
| 713 | tftp_filename[MAX_LEN-1] = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 714 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 715 | printf("*** Warning: no boot file name; using '%s'\n", |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 716 | tftp_filename); |
| 717 | } else { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 718 | char *p = strchr(BootFile, ':'); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 719 | |
| 720 | if (p == NULL) { |
| 721 | strncpy(tftp_filename, BootFile, MAX_LEN); |
| 722 | tftp_filename[MAX_LEN-1] = 0; |
| 723 | } else { |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 724 | TftpRemoteIP = string_to_ip(BootFile); |
Peter Tyser | 5e58efa | 2008-12-01 16:29:38 -0600 | [diff] [blame] | 725 | strncpy(tftp_filename, p + 1, MAX_LEN); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 726 | tftp_filename[MAX_LEN-1] = 0; |
| 727 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 730 | printf("Using %s device\n", eth_get_name()); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 731 | printf("TFTP %s server %pI4; our IP address is %pI4", |
| 732 | protocol == TFTPPUT ? "to" : "from", &TftpRemoteIP, &NetOurIP); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 733 | |
| 734 | /* Check if we need to send across this subnet */ |
| 735 | if (NetOurGatewayIP && NetOurSubnetMask) { |
Luca Ceresoli | 09b1823 | 2011-05-14 05:50:02 +0000 | [diff] [blame] | 736 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 737 | IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 738 | |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 739 | if (OurNet != RemoteNet) |
Luca Ceresoli | 09b1823 | 2011-05-14 05:50:02 +0000 | [diff] [blame] | 740 | printf("; sending through gateway %pI4", |
| 741 | &NetOurGatewayIP); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 742 | } |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 743 | putc('\n'); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 744 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 745 | printf("Filename '%s'.", tftp_filename); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 746 | |
| 747 | if (NetBootFileSize) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 748 | printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
| 749 | print_size(NetBootFileSize<<9, ""); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 752 | putc('\n'); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 753 | #ifdef CONFIG_CMD_TFTPPUT |
| 754 | TftpWriting = (protocol == TFTPPUT); |
| 755 | if (TftpWriting) { |
| 756 | printf("Save address: 0x%lx\n", save_addr); |
| 757 | printf("Save size: 0x%lx\n", save_size); |
| 758 | NetBootFileXferSize = save_size; |
| 759 | puts("Saving: *\b"); |
| 760 | TftpState = STATE_SEND_WRQ; |
| 761 | new_transfer(); |
| 762 | } else |
| 763 | #endif |
| 764 | { |
| 765 | printf("Load address: 0x%lx\n", load_addr); |
| 766 | puts("Loading: *\b"); |
| 767 | TftpState = STATE_SEND_RRQ; |
| 768 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 769 | |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 770 | TftpTimeoutCountMax = TftpRRQTimeoutCountMax; |
| 771 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 772 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
| 773 | NetSetHandler(TftpHandler); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 774 | net_set_icmp_handler(icmp_handler); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 775 | |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 776 | TftpRemotePort = WELL_KNOWN_PORT; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 777 | TftpTimeoutCount = 0; |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 778 | /* Use a pseudo-random port unless a specific port is set */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 779 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 780 | |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 781 | #ifdef CONFIG_TFTP_PORT |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 782 | ep = getenv("tftpdstp"); |
Luca Ceresoli | 35dfb18 | 2011-05-14 05:50:00 +0000 | [diff] [blame] | 783 | if (ep != NULL) |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 784 | TftpRemotePort = simple_strtol(ep, NULL, 10); |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 785 | ep = getenv("tftpsrcp"); |
Luca Ceresoli | 35dfb18 | 2011-05-14 05:50:00 +0000 | [diff] [blame] | 786 | if (ep != NULL) |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 787 | TftpOurPort = simple_strtol(ep, NULL, 10); |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 788 | #endif |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 789 | TftpBlock = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 790 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 791 | /* zero out server ether in case the server ip has changed */ |
| 792 | memset(NetServerEther, 0, 6); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 793 | /* Revert TftpBlkSize to dflt */ |
| 794 | TftpBlkSize = TFTP_BLOCK_SIZE; |
| 795 | #ifdef CONFIG_MCAST_TFTP |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 796 | mcast_cleanup(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 797 | #endif |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 798 | #ifdef CONFIG_TFTP_TSIZE |
| 799 | TftpTsize = 0; |
| 800 | TftpNumchars = 0; |
| 801 | #endif |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 802 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 803 | TftpSend(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 804 | } |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 805 | |
| 806 | #ifdef CONFIG_CMD_TFTPSRV |
| 807 | void |
| 808 | TftpStartServer(void) |
| 809 | { |
| 810 | tftp_filename[0] = 0; |
| 811 | |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 812 | printf("Using %s device\n", eth_get_name()); |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 813 | printf("Listening for TFTP transfer on %pI4\n", &NetOurIP); |
| 814 | printf("Load address: 0x%lx\n", load_addr); |
| 815 | |
| 816 | puts("Loading: *\b"); |
| 817 | |
| 818 | TftpTimeoutCountMax = TIMEOUT_COUNT; |
| 819 | TftpTimeoutCount = 0; |
| 820 | TftpTimeoutMSecs = TIMEOUT; |
| 821 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
| 822 | |
| 823 | /* Revert TftpBlkSize to dflt */ |
| 824 | TftpBlkSize = TFTP_BLOCK_SIZE; |
| 825 | TftpBlock = 0; |
| 826 | TftpOurPort = WELL_KNOWN_PORT; |
| 827 | |
| 828 | #ifdef CONFIG_TFTP_TSIZE |
| 829 | TftpTsize = 0; |
| 830 | TftpNumchars = 0; |
| 831 | #endif |
| 832 | |
| 833 | TftpState = STATE_RECV_WRQ; |
| 834 | NetSetHandler(TftpHandler); |
| 835 | } |
| 836 | #endif /* CONFIG_CMD_TFTPSRV */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 837 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 838 | #ifdef CONFIG_MCAST_TFTP |
| 839 | /* Credits: atftp project. |
| 840 | */ |
| 841 | |
| 842 | /* pick up BcastAddr, Port, and whether I am [now] the master-client. * |
| 843 | * Frame: |
| 844 | * +-------+-----------+---+-------~~-------+---+ |
| 845 | * | opc | multicast | 0 | addr, port, mc | 0 | |
| 846 | * +-------+-----------+---+-------~~-------+---+ |
| 847 | * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then |
| 848 | * I am the new master-client so must send ACKs to DataBlocks. If I am not |
| 849 | * master-client, I'm a passive client, gathering what DataBlocks I may and |
| 850 | * making note of which ones I got in my bitmask. |
| 851 | * In theory, I never go from master->passive.. |
| 852 | * .. this comes in with pkt already pointing just past opc |
| 853 | */ |
| 854 | static void parse_multicast_oack(char *pkt, int len) |
| 855 | { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 856 | int i; |
| 857 | IPaddr_t addr; |
| 858 | char *mc_adr, *port, *mc; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 859 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 860 | mc_adr = port = mc = NULL; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 861 | /* march along looking for 'multicast\0', which has to start at least |
| 862 | * 14 bytes back from the end. |
| 863 | */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 864 | for (i = 0; i < len-14; i++) |
| 865 | if (strcmp(pkt+i, "multicast") == 0) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 866 | break; |
| 867 | if (i >= (len-14)) /* non-Multicast OACK, ign. */ |
| 868 | return; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 869 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 870 | i += 10; /* strlen multicast */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 871 | mc_adr = pkt+i; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 872 | for (; i < len; i++) { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 873 | if (*(pkt+i) == ',') { |
| 874 | *(pkt+i) = '\0'; |
| 875 | if (port) { |
| 876 | mc = pkt+i+1; |
| 877 | break; |
| 878 | } else { |
| 879 | port = pkt+i+1; |
| 880 | } |
| 881 | } |
| 882 | } |
Luca Ceresoli | 6072fcd | 2011-05-14 05:50:01 +0000 | [diff] [blame] | 883 | if (!port || !mc_adr || !mc) |
| 884 | return; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 885 | if (Multicast && MasterClient) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 886 | printf("I got a OACK as master Client, WRONG!\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 887 | return; |
| 888 | } |
| 889 | /* ..I now accept packets destined for this MCAST addr, port */ |
| 890 | if (!Multicast) { |
| 891 | if (Bitmap) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 892 | printf("Internal failure! no mcast.\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 893 | free(Bitmap); |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 894 | Bitmap = NULL; |
| 895 | ProhibitMcast = 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 896 | return ; |
| 897 | } |
| 898 | /* I malloc instead of pre-declare; so that if the file ends |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 899 | * up being too big for this bitmap I can retry |
| 900 | */ |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 901 | Bitmap = malloc(Mapsize); |
| 902 | if (!Bitmap) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 903 | printf("No Bitmap, no multicast. Sorry.\n"); |
| 904 | ProhibitMcast = 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 905 | return; |
| 906 | } |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 907 | memset(Bitmap, 0, Mapsize); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 908 | PrevBitmapHole = 0; |
| 909 | Multicast = 1; |
| 910 | } |
| 911 | addr = string_to_ip(mc_adr); |
| 912 | if (Mcast_addr != addr) { |
| 913 | if (Mcast_addr) |
| 914 | eth_mcast_join(Mcast_addr, 0); |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 915 | Mcast_addr = addr; |
| 916 | if (eth_mcast_join(Mcast_addr, 1)) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 917 | printf("Fail to set mcast, revert to TFTP\n"); |
| 918 | ProhibitMcast = 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 919 | mcast_cleanup(); |
| 920 | NetStartAgain(); |
| 921 | } |
| 922 | } |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 923 | MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10); |
| 924 | Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10); |
| 925 | printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
| 929 | #endif /* Multicast TFTP */ |