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