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