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