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