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