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 | |
| 15 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 16 | |
| 17 | #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */ |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 18 | #define TIMEOUT 5 /* 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 | |
| 37 | |
| 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]; |
| 58 | static char *tftp_filename; |
| 59 | |
| 60 | #ifdef CFG_DIRECT_FLASH_TFTP |
| 61 | extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; |
| 62 | #endif |
| 63 | |
| 64 | static __inline__ void |
| 65 | store_block (unsigned block, uchar * src, unsigned len) |
| 66 | { |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 67 | ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset; |
| 68 | ulong newsize = offset + len; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 69 | #ifdef CFG_DIRECT_FLASH_TFTP |
| 70 | int i, rc = 0; |
| 71 | |
| 72 | for (i=0; i<CFG_MAX_FLASH_BANKS; i++) { |
| 73 | /* start address in flash? */ |
| 74 | if (load_addr + offset >= flash_info[i].start[0]) { |
| 75 | rc = 1; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (rc) { /* Flash is destination for this packet */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame^] | 81 | rc = flash_write ((char *)src, (ulong)(load_addr+offset), len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 82 | if (rc) { |
| 83 | flash_perror (rc); |
| 84 | NetState = NETLOOP_FAIL; |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | else |
| 89 | #endif /* CFG_DIRECT_FLASH_TFTP */ |
| 90 | { |
| 91 | (void)memcpy((void *)(load_addr + offset), src, len); |
| 92 | } |
| 93 | |
| 94 | if (NetBootFileXferSize < newsize) |
| 95 | NetBootFileXferSize = newsize; |
| 96 | } |
| 97 | |
| 98 | static void TftpSend (void); |
| 99 | static void TftpTimeout (void); |
| 100 | |
| 101 | /**********************************************************************/ |
| 102 | |
| 103 | static void |
| 104 | TftpSend (void) |
| 105 | { |
| 106 | volatile uchar * pkt; |
| 107 | volatile uchar * xp; |
| 108 | int len = 0; |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 109 | volatile ushort *s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * We will always be sending some sort of packet, so |
| 113 | * cobble together the packet headers now. |
| 114 | */ |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 115 | pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 116 | |
| 117 | switch (TftpState) { |
| 118 | |
| 119 | case STATE_RRQ: |
| 120 | xp = pkt; |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 121 | s = (ushort *)pkt; |
| 122 | *s++ = htons(TFTP_RRQ); |
| 123 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 124 | strcpy ((char *)pkt, tftp_filename); |
| 125 | pkt += strlen(tftp_filename) + 1; |
| 126 | strcpy ((char *)pkt, "octet"); |
| 127 | pkt += 5 /*strlen("octet")*/ + 1; |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 128 | strcpy ((char *)pkt, "timeout"); |
| 129 | pkt += 7 /*strlen("timeout")*/ + 1; |
| 130 | sprintf((char *)pkt, "%d", TIMEOUT); |
| 131 | #ifdef ET_DEBUG |
| 132 | printf("send option \"timeout %s\"\n", (char *)pkt); |
| 133 | #endif |
| 134 | pkt += strlen((char *)pkt) + 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 135 | len = pkt - xp; |
| 136 | break; |
| 137 | |
| 138 | case STATE_DATA: |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 139 | case STATE_OACK: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 140 | xp = pkt; |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 141 | s = (ushort *)pkt; |
| 142 | *s++ = htons(TFTP_ACK); |
| 143 | *s++ = htons(TftpBlock); |
| 144 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 145 | len = pkt - xp; |
| 146 | break; |
| 147 | |
| 148 | case STATE_TOO_LARGE: |
| 149 | xp = pkt; |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 150 | s = (ushort *)pkt; |
| 151 | *s++ = htons(TFTP_ERROR); |
| 152 | *s++ = htons(3); |
| 153 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 154 | strcpy ((char *)pkt, "File too large"); |
| 155 | pkt += 14 /*strlen("File too large")*/ + 1; |
| 156 | len = pkt - xp; |
| 157 | break; |
| 158 | |
| 159 | case STATE_BAD_MAGIC: |
| 160 | xp = pkt; |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 161 | s = (ushort *)pkt; |
| 162 | *s++ = htons(TFTP_ERROR); |
| 163 | *s++ = htons(2); |
| 164 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 165 | strcpy ((char *)pkt, "File has bad magic"); |
| 166 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
| 167 | len = pkt - xp; |
| 168 | break; |
| 169 | } |
| 170 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 171 | NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | static void |
| 176 | TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 177 | { |
| 178 | ushort proto; |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 179 | ushort *s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 180 | |
| 181 | if (dest != TftpOurPort) { |
| 182 | return; |
| 183 | } |
| 184 | if (TftpState != STATE_RRQ && src != TftpServerPort) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (len < 2) { |
| 189 | return; |
| 190 | } |
| 191 | len -= 2; |
| 192 | /* warning: don't use increment (++) in ntohs() macros!! */ |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 193 | s = (ushort *)pkt; |
| 194 | proto = *s++; |
| 195 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 196 | switch (ntohs(proto)) { |
| 197 | |
| 198 | case TFTP_RRQ: |
| 199 | case TFTP_WRQ: |
| 200 | case TFTP_ACK: |
| 201 | break; |
| 202 | default: |
| 203 | break; |
| 204 | |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 205 | case TFTP_OACK: |
| 206 | #ifdef ET_DEBUG |
| 207 | printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1); |
| 208 | #endif |
| 209 | TftpState = STATE_OACK; |
| 210 | TftpServerPort = src; |
| 211 | TftpSend (); /* Send ACK */ |
| 212 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 213 | case TFTP_DATA: |
| 214 | if (len < 2) |
| 215 | return; |
| 216 | len -= 2; |
| 217 | TftpBlock = ntohs(*(ushort *)pkt); |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 218 | |
| 219 | /* |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 220 | * RFC1350 specifies that the first data packet will |
| 221 | * have sequence number 1. If we receive a sequence |
| 222 | * number of 0 this means that there was a wrap |
| 223 | * around of the (16 bit) counter. |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 224 | */ |
| 225 | if (TftpBlock == 0) { |
| 226 | TftpBlockWrap++; |
| 227 | TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE; |
| 228 | printf ("\n\t %lu MB reveived\n\t ", TftpBlockWrapOffset>>20); |
| 229 | } else { |
| 230 | if (((TftpBlock - 1) % 10) == 0) { |
| 231 | putc ('#'); |
| 232 | } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) { |
| 233 | puts ("\n\t "); |
| 234 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 235 | } |
| 236 | |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 237 | #ifdef ET_DEBUG |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 238 | if (TftpState == STATE_RRQ) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 239 | puts ("Server did not acknowledge timeout option!\n"); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 240 | } |
| 241 | #endif |
| 242 | |
| 243 | if (TftpState == STATE_RRQ || TftpState == STATE_OACK) { |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 244 | /* first block received */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 245 | TftpState = STATE_DATA; |
| 246 | TftpServerPort = src; |
| 247 | TftpLastBlock = 0; |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 248 | TftpBlockWrap = 0; |
| 249 | TftpBlockWrapOffset = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 250 | |
| 251 | if (TftpBlock != 1) { /* Assertion */ |
| 252 | printf ("\nTFTP error: " |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 253 | "First block is not block 1 (%ld)\n" |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 254 | "Starting again\n\n", |
| 255 | TftpBlock); |
| 256 | NetStartAgain (); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (TftpBlock == TftpLastBlock) { |
| 262 | /* |
| 263 | * Same block again; ignore it. |
| 264 | */ |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | TftpLastBlock = TftpBlock; |
| 269 | NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout); |
| 270 | |
| 271 | store_block (TftpBlock - 1, pkt + 2, len); |
| 272 | |
| 273 | /* |
| 274 | * Acknoledge the block just received, which will prompt |
| 275 | * the server for the next one. |
| 276 | */ |
| 277 | TftpSend (); |
| 278 | |
wdenk | 3f85ce2 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 279 | if (len < TFTP_BLOCK_SIZE) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 280 | /* |
| 281 | * We received the whole thing. Try to |
| 282 | * run it. |
| 283 | */ |
| 284 | puts ("\ndone\n"); |
| 285 | NetState = NETLOOP_SUCCESS; |
| 286 | } |
| 287 | break; |
| 288 | |
| 289 | case TFTP_ERROR: |
| 290 | printf ("\nTFTP error: '%s' (%d)\n", |
| 291 | pkt + 2, ntohs(*(ushort *)pkt)); |
| 292 | puts ("Starting again\n\n"); |
| 293 | NetStartAgain (); |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
| 299 | static void |
| 300 | TftpTimeout (void) |
| 301 | { |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 302 | if (++TftpTimeoutCount > TIMEOUT_COUNT) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 303 | puts ("\nRetry count exceeded; starting again\n"); |
| 304 | NetStartAgain (); |
| 305 | } else { |
| 306 | puts ("T "); |
| 307 | NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout); |
| 308 | TftpSend (); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | |
| 313 | void |
| 314 | TftpStart (void) |
| 315 | { |
Wolfgang Denk | ecb0ccd | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 316 | #ifdef CONFIG_TFTP_PORT |
| 317 | char *ep; /* Environment pointer */ |
| 318 | #endif |
| 319 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 320 | if (BootFile[0] == '\0') { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 321 | sprintf(default_filename, "%02lX%02lX%02lX%02lX.img", |
Wolfgang Denk | c43352c | 2005-08-04 01:09:44 +0200 | [diff] [blame] | 322 | NetOurIP & 0xFF, |
| 323 | (NetOurIP >> 8) & 0xFF, |
| 324 | (NetOurIP >> 16) & 0xFF, |
| 325 | (NetOurIP >> 24) & 0xFF ); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 326 | tftp_filename = default_filename; |
| 327 | |
| 328 | printf ("*** Warning: no boot file name; using '%s'\n", |
| 329 | tftp_filename); |
| 330 | } else { |
| 331 | tftp_filename = BootFile; |
| 332 | } |
| 333 | |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 334 | #if defined(CONFIG_NET_MULTI) |
| 335 | printf ("Using %s device\n", eth_get_name()); |
| 336 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 337 | puts ("TFTP from server "); print_IPaddr (NetServerIP); |
| 338 | puts ("; our IP address is "); print_IPaddr (NetOurIP); |
| 339 | |
| 340 | /* Check if we need to send across this subnet */ |
| 341 | if (NetOurGatewayIP && NetOurSubnetMask) { |
| 342 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
| 343 | IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; |
| 344 | |
| 345 | if (OurNet != ServerNet) { |
| 346 | puts ("; sending through gateway "); |
| 347 | print_IPaddr (NetOurGatewayIP) ; |
| 348 | } |
| 349 | } |
| 350 | putc ('\n'); |
| 351 | |
| 352 | printf ("Filename '%s'.", tftp_filename); |
| 353 | |
| 354 | if (NetBootFileSize) { |
| 355 | printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
| 356 | print_size (NetBootFileSize<<9, ""); |
| 357 | } |
| 358 | |
| 359 | putc ('\n'); |
| 360 | |
| 361 | printf ("Load address: 0x%lx\n", load_addr); |
| 362 | |
| 363 | puts ("Loading: *\b"); |
| 364 | |
| 365 | NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout); |
| 366 | NetSetHandler (TftpHandler); |
| 367 | |
| 368 | TftpServerPort = WELL_KNOWN_PORT; |
| 369 | TftpTimeoutCount = 0; |
| 370 | TftpState = STATE_RRQ; |
Wolfgang Denk | ecb0ccd | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 371 | /* Use a pseudo-random port unless a specific port is set */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 372 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
Wolfgang Denk | ecb0ccd | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 373 | #ifdef CONFIG_TFTP_PORT |
Wolfgang Denk | 28cb937 | 2005-09-24 23:25:46 +0200 | [diff] [blame] | 374 | if ((ep = getenv("tftpdstp")) != NULL) { |
| 375 | TftpServerPort = simple_strtol(ep, NULL, 10); |
| 376 | } |
| 377 | if ((ep = getenv("tftpsrcp")) != NULL) { |
Wolfgang Denk | ecb0ccd | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 378 | TftpOurPort= simple_strtol(ep, NULL, 10); |
| 379 | } |
| 380 | #endif |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 381 | TftpBlock = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 382 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 383 | /* zero out server ether in case the server ip has changed */ |
| 384 | memset(NetServerEther, 0, 6); |
| 385 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 386 | TftpSend (); |
| 387 | } |
| 388 | |
| 389 | #endif /* CFG_CMD_NET */ |