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