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