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