wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
Luca Ceresoli | c42fc27 | 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 | b640ed3 | 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 | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 11 | #include <efi_loader.h> |
Joe Hershberger | ed19185 | 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 | 2118592 | 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 | 3a29e5a | 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 | |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 22 | /* Well known TFTP port # */ |
| 23 | #define WELL_KNOWN_PORT 69 |
| 24 | /* Millisecs to timeout for lost pkt */ |
Bin Meng | e67eaa8 | 2015-08-27 22:25:51 -0700 | [diff] [blame] | 25 | #define TIMEOUT 5000UL |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 26 | #ifndef CONFIG_NET_RETRY_COUNT |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 27 | /* # of timeouts before giving up */ |
Bin Meng | e67eaa8 | 2015-08-27 22:25:51 -0700 | [diff] [blame] | 28 | # define TIMEOUT_COUNT 10 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 29 | #else |
| 30 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) |
| 31 | #endif |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 32 | /* Number of "loading" hashes per line (for checking the image size) */ |
| 33 | #define HASHES_PER_LINE 65 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 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 |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 43 | #define TFTP_OACK 6 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 44 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 45 | static ulong timeout_ms = TIMEOUT; |
| 46 | static int timeout_count_max = TIMEOUT_COUNT; |
Simon Glass | cab9a8d | 2012-10-11 13:57:36 +0000 | [diff] [blame] | 47 | static ulong time_start; /* Record time we started tftp */ |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * These globals govern the timeout behavior when attempting a connection to a |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 51 | * TFTP server. tftp_timeout_ms specifies the number of milliseconds to |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 52 | * wait for the server to respond to initial connection. Second global, |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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 |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 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 | */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 58 | ulong tftp_timeout_ms = TIMEOUT; |
| 59 | int tftp_timeout_count_max = TIMEOUT_COUNT; |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 60 | |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 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 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 71 | static struct in_addr tftp_remote_ip; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 72 | /* The UDP port at their end */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 73 | static int tftp_remote_port; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 74 | /* The UDP port at our end */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 75 | static int tftp_our_port; |
| 76 | static int timeout_count; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 77 | /* packet sequence number */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 78 | static ulong tftp_cur_block; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 79 | /* last packet sequence number received */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 80 | static ulong tftp_prev_block; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 81 | /* count of sequence number wraparounds */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 82 | static ulong tftp_block_wrap; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 83 | /* memory offset due to wrapping */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 84 | static ulong tftp_block_wrap_offset; |
| 85 | static int tftp_state; |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 86 | static ulong tftp_load_addr; |
| 87 | #ifdef CONFIG_LMB |
| 88 | static ulong tftp_load_size; |
| 89 | #endif |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 90 | #ifdef CONFIG_TFTP_TSIZE |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 91 | /* The file size reported by the server */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 92 | static int tftp_tsize; |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 93 | /* The number of hashes we printed */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 94 | static short tftp_tsize_num_hash; |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 95 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 96 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 101 | #else |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 102 | #define tftp_put_active 0 |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 103 | #endif |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 104 | |
Luca Ceresoli | 562dfe5 | 2011-05-17 00:03:38 +0000 | [diff] [blame] | 105 | #define STATE_SEND_RRQ 1 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 106 | #define STATE_DATA 2 |
| 107 | #define STATE_TOO_LARGE 3 |
| 108 | #define STATE_BAD_MAGIC 4 |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 109 | #define STATE_OACK 5 |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 110 | #define STATE_RECV_WRQ 6 |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 111 | #define STATE_SEND_WRQ 7 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 112 | |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 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)) |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 117 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 118 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
| 119 | static char default_filename[DEFAULT_NAME_LEN]; |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 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]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 128 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 129 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
| 130 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 131 | * almost-MTU block sizes. At least try... fall back to 512 if need be. |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 132 | * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 133 | */ |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 134 | #ifdef CONFIG_TFTP_BLOCKSIZE |
| 135 | #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE |
| 136 | #else |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 137 | #define TFTP_MTU_BLOCKSIZE 1468 |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 138 | #endif |
| 139 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 140 | static unsigned short tftp_block_size = TFTP_BLOCK_SIZE; |
| 141 | static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 142 | |
| 143 | #ifdef CONFIG_MCAST_TFTP |
| 144 | #include <malloc.h> |
| 145 | #define MTFTP_BITMAPSIZE 0x1000 |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 155 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 156 | static void parse_multicast_oack(char *pkt, int len); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 157 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 158 | static void mcast_cleanup(void) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 159 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 160 | if (net_mcast_addr) |
| 161 | eth_mcast_join(net_mcast_addr, 0); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 162 | if (tftp_mcast_bitmap) |
| 163 | free(tftp_mcast_bitmap); |
| 164 | tftp_mcast_bitmap = NULL; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 165 | net_mcast_addr.s_addr = 0; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 166 | tftp_mcast_active = 0; |
| 167 | tftp_mcast_port = 0; |
| 168 | tftp_mcast_ending_block = -1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #endif /* CONFIG_MCAST_TFTP */ |
| 172 | |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 173 | static inline int store_block(int block, uchar *src, unsigned int len) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 174 | { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 175 | ulong offset = block * tftp_block_size + tftp_block_wrap_offset; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 176 | ulong newsize = offset + len; |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 177 | ulong store_addr = tftp_load_addr + offset; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 178 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 179 | int i, rc = 0; |
| 180 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 181 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 182 | /* start address in flash? */ |
Jochen Friedrich | d31a59b | 2008-09-02 11:24:59 +0200 | [diff] [blame] | 183 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
| 184 | continue; |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 185 | if (store_addr >= flash_info[i].start[0]) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 186 | rc = 1; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (rc) { /* Flash is destination for this packet */ |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 192 | rc = flash_write((char *)src, store_addr, len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 193 | if (rc) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 194 | flash_perror(rc); |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 195 | return rc; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 196 | } |
Joe Hershberger | 3a29e5a | 2012-05-15 08:59:12 +0000 | [diff] [blame] | 197 | } else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 198 | #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 199 | { |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 200 | void *ptr; |
Joe Hershberger | ed19185 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 201 | |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 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); |
Joe Hershberger | ed19185 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 211 | memcpy(ptr, src, len); |
| 212 | unmap_sysmem(ptr); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 213 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 214 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 215 | if (tftp_mcast_active) |
| 216 | ext2_set_bit(block, tftp_mcast_bitmap); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 217 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 218 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 219 | if (net_boot_file_size < newsize) |
| 220 | net_boot_file_size = newsize; |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 221 | |
| 222 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 225 | /* Clear our state ready for a new transfer */ |
Simon Glass | 8431332 | 2011-10-27 06:24:28 +0000 | [diff] [blame] | 226 | static void new_transfer(void) |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 227 | { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 228 | tftp_prev_block = 0; |
| 229 | tftp_block_wrap = 0; |
| 230 | tftp_block_wrap_offset = 0; |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 231 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 232 | tftp_put_final_block_sent = 0; |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 233 | #endif |
| 234 | } |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 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 */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 248 | ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 249 | ulong tosend = len; |
| 250 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 251 | tosend = min(net_boot_file_size - offset, tosend); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 252 | (void)memcpy(dst, (void *)(save_addr + offset), tosend); |
| 253 | debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__, |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 254 | block, offset, len, tosend); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 255 | return tosend; |
| 256 | } |
| 257 | #endif |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 258 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 259 | static void tftp_send(void); |
| 260 | static void tftp_timeout_handler(void); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 261 | |
| 262 | /**********************************************************************/ |
| 263 | |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 264 | static void show_block_marker(void) |
| 265 | { |
| 266 | #ifdef CONFIG_TFTP_TSIZE |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 267 | if (tftp_tsize) { |
| 268 | ulong pos = tftp_cur_block * tftp_block_size + |
| 269 | tftp_block_wrap_offset; |
Max Krummenacher | dd081cc | 2015-08-05 17:17:05 +0200 | [diff] [blame] | 270 | if (pos > tftp_tsize) |
| 271 | pos = tftp_tsize; |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 272 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 273 | while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) { |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 274 | putc('#'); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 275 | tftp_tsize_num_hash++; |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 276 | } |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 277 | } else |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 278 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 279 | { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 280 | if (((tftp_cur_block - 1) % 10) == 0) |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 281 | putc('#'); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 282 | else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0) |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 283 | puts("\n\t "); |
| 284 | } |
| 285 | } |
| 286 | |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 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 |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 298 | net_start_again(); |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 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 | */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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 */ |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 318 | } else { |
| 319 | show_block_marker(); |
| 320 | } |
| 321 | } |
| 322 | |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 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 */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 328 | while (tftp_tsize && tftp_tsize_num_hash < 49) { |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 329 | putc('#'); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 330 | tftp_tsize_num_hash++; |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 331 | } |
Simon Glass | c8f0a5c | 2014-10-10 07:30:21 -0600 | [diff] [blame] | 332 | puts(" "); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 333 | print_size(tftp_tsize, ""); |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 334 | #endif |
Simon Glass | cab9a8d | 2012-10-11 13:57:36 +0000 | [diff] [blame] | 335 | time_start = get_timer(time_start); |
| 336 | if (time_start > 0) { |
| 337 | puts("\n\t "); /* Line up with "Loading: " */ |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 338 | print_size(net_boot_file_size / |
Simon Glass | cab9a8d | 2012-10-11 13:57:36 +0000 | [diff] [blame] | 339 | time_start * 1000, "/s"); |
| 340 | } |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 341 | puts("\ndone\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 342 | net_set_state(NETLOOP_SUCCESS); |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 345 | static void tftp_send(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 346 | { |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 347 | uchar *pkt; |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 348 | uchar *xp; |
| 349 | int len = 0; |
| 350 | ushort *s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 351 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 352 | #ifdef CONFIG_MCAST_TFTP |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 353 | /* Multicast TFTP.. non-MasterClients do not ACK data. */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 354 | if (tftp_mcast_active && tftp_state == STATE_DATA && |
| 355 | tftp_mcast_master_client == 0) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 356 | return; |
| 357 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 358 | /* |
| 359 | * We will always be sending some sort of packet, so |
| 360 | * cobble together the packet headers now. |
| 361 | */ |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 362 | pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 363 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 364 | switch (tftp_state) { |
Luca Ceresoli | 562dfe5 | 2011-05-17 00:03:38 +0000 | [diff] [blame] | 365 | case STATE_SEND_RRQ: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 366 | case STATE_SEND_WRQ: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 367 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 368 | s = (ushort *)pkt; |
Simon Glass | 4abd0e2 | 2011-10-27 06:24:29 +0000 | [diff] [blame] | 369 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 370 | *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ : |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 371 | TFTP_WRQ); |
Simon Glass | 4abd0e2 | 2011-10-27 06:24:29 +0000 | [diff] [blame] | 372 | #else |
| 373 | *s++ = htons(TFTP_RRQ); |
| 374 | #endif |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 375 | pkt = (uchar *)s; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 376 | strcpy((char *)pkt, tftp_filename); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 377 | pkt += strlen(tftp_filename) + 1; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 378 | strcpy((char *)pkt, "octet"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 379 | pkt += 5 /*strlen("octet")*/ + 1; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 380 | strcpy((char *)pkt, "timeout"); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 381 | pkt += 7 /*strlen("timeout")*/ + 1; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 382 | sprintf((char *)pkt, "%lu", timeout_ms / 1000); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 383 | debug("send option \"timeout %s\"\n", (char *)pkt); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 384 | pkt += strlen((char *)pkt) + 1; |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 385 | #ifdef CONFIG_TFTP_TSIZE |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 386 | pkt += sprintf((char *)pkt, "tsize%c%u%c", |
| 387 | 0, net_boot_file_size, 0); |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 388 | #endif |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 389 | /* try for more effic. blk size */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 390 | pkt += sprintf((char *)pkt, "blksize%c%d%c", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 391 | 0, tftp_block_size_option, 0); |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 392 | #ifdef CONFIG_MCAST_TFTP |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 393 | /* Check all preconditions before even trying the option */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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; |
Joe Hershberger | 3a29e5a | 2012-05-15 08:59:12 +0000 | [diff] [blame] | 399 | pkt += sprintf((char *)pkt, "multicast%c%c", |
| 400 | 0, 0); |
| 401 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 402 | } |
| 403 | #endif /* CONFIG_MCAST_TFTP */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 404 | len = pkt - xp; |
| 405 | break; |
| 406 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 407 | case STATE_OACK: |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 408 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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 */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 415 | #endif |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 416 | |
| 417 | case STATE_RECV_WRQ: |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 418 | case STATE_DATA: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 419 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 420 | s = (ushort *)pkt; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 421 | s[0] = htons(TFTP_ACK); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 422 | s[1] = htons(tftp_cur_block); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 423 | pkt = (uchar *)(s + 2); |
| 424 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 425 | if (tftp_put_active) { |
| 426 | int toload = tftp_block_size; |
| 427 | int loaded = load_block(tftp_cur_block, pkt, toload); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 428 | |
| 429 | s[0] = htons(TFTP_DATA); |
| 430 | pkt += loaded; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 431 | tftp_put_final_block_sent = (loaded < toload); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 432 | } |
| 433 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 434 | len = pkt - xp; |
| 435 | break; |
| 436 | |
| 437 | case STATE_TOO_LARGE: |
| 438 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 439 | s = (ushort *)pkt; |
| 440 | *s++ = htons(TFTP_ERROR); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 441 | *s++ = htons(3); |
| 442 | |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 443 | pkt = (uchar *)s; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 444 | strcpy((char *)pkt, "File too large"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 445 | pkt += 14 /*strlen("File too large")*/ + 1; |
| 446 | len = pkt - xp; |
| 447 | break; |
| 448 | |
| 449 | case STATE_BAD_MAGIC: |
| 450 | xp = pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 451 | s = (ushort *)pkt; |
| 452 | *s++ = htons(TFTP_ERROR); |
| 453 | *s++ = htons(2); |
| 454 | pkt = (uchar *)s; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 455 | strcpy((char *)pkt, "File has bad magic"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 456 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
| 457 | len = pkt - xp; |
| 458 | break; |
| 459 | } |
| 460 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 461 | net_send_udp_packet(net_server_ethaddr, tftp_remote_ip, |
| 462 | tftp_remote_port, tftp_our_port, len); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 465 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 466 | static void icmp_handler(unsigned type, unsigned code, unsigned dest, |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 467 | struct in_addr sip, unsigned src, uchar *pkt, |
| 468 | unsigned len) |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 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 | } |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 475 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 476 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 477 | static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 478 | unsigned src, unsigned len) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 479 | { |
Kim Phillips | 878dad0 | 2013-01-16 18:09:19 -0600 | [diff] [blame] | 480 | __be16 proto; |
| 481 | __be16 *s; |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 482 | int i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 483 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 484 | if (dest != tftp_our_port) { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 485 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 486 | if (tftp_mcast_active && |
| 487 | (!tftp_mcast_port || dest != tftp_mcast_port)) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 488 | #endif |
Luca Ceresoli | 09b1823 | 2011-05-14 05:50:02 +0000 | [diff] [blame] | 489 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 490 | } |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 491 | if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port && |
| 492 | tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 493 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 494 | |
Luca Ceresoli | 35dfb18 | 2011-05-14 05:50:00 +0000 | [diff] [blame] | 495 | if (len < 2) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 496 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 497 | len -= 2; |
| 498 | /* warning: don't use increment (++) in ntohs() macros!! */ |
Kim Phillips | 878dad0 | 2013-01-16 18:09:19 -0600 | [diff] [blame] | 499 | s = (__be16 *)pkt; |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 500 | proto = *s++; |
| 501 | pkt = (uchar *)s; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 502 | switch (ntohs(proto)) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 503 | case TFTP_RRQ: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 504 | break; |
| 505 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 506 | case TFTP_ACK: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 507 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 508 | if (tftp_put_active) { |
| 509 | if (tftp_put_final_block_sent) { |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 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); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 517 | int ack_ok = (tftp_cur_block == block); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 518 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 519 | tftp_cur_block = (unsigned short)(block + 1); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 520 | update_block_number(); |
| 521 | if (ack_ok) |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 522 | tftp_send(); /* Send next data block */ |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 526 | break; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 527 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 528 | default: |
| 529 | break; |
| 530 | |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 531 | #ifdef CONFIG_CMD_TFTPSRV |
| 532 | case TFTP_WRQ: |
| 533 | debug("Got WRQ\n"); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 534 | tftp_remote_ip = sip; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 535 | tftp_remote_port = src; |
| 536 | tftp_our_port = 1024 + (get_timer(0) % 3072); |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 537 | new_transfer(); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 538 | tftp_send(); /* Send ACK(0) */ |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 539 | break; |
| 540 | #endif |
| 541 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 542 | case TFTP_OACK: |
Wolfgang Denk | 12730c7 | 2009-08-10 09:59:10 +0200 | [diff] [blame] | 543 | debug("Got OACK: %s %s\n", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 544 | pkt, pkt + strlen((char *)pkt) + 1); |
| 545 | tftp_state = STATE_OACK; |
| 546 | tftp_remote_port = src; |
Wolfgang Denk | 8b70f34 | 2007-08-31 10:01:51 +0200 | [diff] [blame] | 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 | */ |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 552 | for (i = 0; i+8 < len; i++) { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 553 | if (strcmp((char *)pkt + i, "blksize") == 0) { |
| 554 | tftp_block_size = (unsigned short) |
| 555 | simple_strtoul((char *)pkt + i + 8, |
| 556 | NULL, 10); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 557 | debug("Blocksize ack: %s, %d\n", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 558 | (char *)pkt + i + 8, tftp_block_size); |
Wolfgang Denk | 83e8e47 | 2007-08-30 14:42:15 +0200 | [diff] [blame] | 559 | } |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 560 | #ifdef CONFIG_TFTP_TSIZE |
Luca Ceresoli | bf6ce41 | 2011-05-14 05:49:58 +0000 | [diff] [blame] | 561 | if (strcmp((char *)pkt+i, "tsize") == 0) { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 562 | tftp_tsize = simple_strtoul((char *)pkt + i + 6, |
Luca Ceresoli | c42fc27 | 2011-05-14 05:49:56 +0000 | [diff] [blame] | 563 | NULL, 10); |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 564 | debug("size = %s, %d\n", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 565 | (char *)pkt + i + 6, tftp_tsize); |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 566 | } |
| 567 | #endif |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 568 | } |
| 569 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 570 | parse_multicast_oack((char *)pkt, len - 1); |
| 571 | if ((tftp_mcast_active) && (!tftp_mcast_master_client)) |
| 572 | tftp_state = STATE_DATA; /* passive.. */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 573 | else |
| 574 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 575 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 576 | if (tftp_put_active) { |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 577 | /* Get ready to send the first block */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 578 | tftp_state = STATE_DATA; |
| 579 | tftp_cur_block++; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 580 | } |
| 581 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 582 | tftp_send(); /* Send ACK or first data block */ |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 583 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 584 | case TFTP_DATA: |
| 585 | if (len < 2) |
| 586 | return; |
| 587 | len -= 2; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 588 | tftp_cur_block = ntohs(*(__be16 *)pkt); |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 589 | |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 590 | update_block_number(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 591 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 592 | if (tftp_state == STATE_SEND_RRQ) |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 593 | debug("Server did not acknowledge timeout option!\n"); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 594 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 595 | if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK || |
| 596 | tftp_state == STATE_RECV_WRQ) { |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 597 | /* first block received */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 598 | tftp_state = STATE_DATA; |
| 599 | tftp_remote_port = src; |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 600 | new_transfer(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 601 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 602 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 603 | if (tftp_mcast_active) { /* start!=1 common if mcast */ |
| 604 | tftp_prev_block = tftp_cur_block - 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 605 | } else |
| 606 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 612 | net_start_again(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | } |
| 616 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 617 | if (tftp_cur_block == tftp_prev_block) { |
| 618 | /* Same block again; ignore it. */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 619 | break; |
| 620 | } |
| 621 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 622 | tftp_prev_block = tftp_cur_block; |
Albert ARIBAUD \(3ADEV\) | 8300685 | 2015-10-12 00:02:57 +0200 | [diff] [blame] | 623 | timeout_count_max = tftp_timeout_count_max; |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 624 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 625 | |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 626 | if (store_block(tftp_cur_block - 1, pkt + 2, len)) { |
| 627 | eth_halt(); |
| 628 | net_set_state(NETLOOP_FAIL); |
| 629 | break; |
| 630 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 631 | |
| 632 | /* |
Luca Ceresoli | 2f3dc0a | 2011-05-17 00:03:41 +0000 | [diff] [blame] | 633 | * Acknowledge the block just received, which will prompt |
Luca Ceresoli | 5079c76 | 2011-05-17 00:03:37 +0000 | [diff] [blame] | 634 | * the remote for the next one. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 635 | */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 636 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 637 | /* if I am the MasterClient, actively calculate what my next |
| 638 | * needed block is; else I'm passive; not ACKING |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 639 | */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 652 | /* try to double it and retry */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 653 | tftp_mcast_bitmap_size <<= 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 654 | mcast_cleanup(); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 655 | net_start_again(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 656 | return; |
| 657 | } |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 658 | tftp_prev_block = tftp_cur_block; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 662 | tftp_send(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 663 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 664 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 665 | if (tftp_mcast_active) { |
| 666 | if (tftp_mcast_master_client && |
| 667 | (tftp_cur_block >= tftp_mcast_ending_block)) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 668 | puts("\nMulticast tftp done\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 669 | mcast_cleanup(); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 670 | net_set_state(NETLOOP_SUCCESS); |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 671 | } |
Joe Hershberger | 3a29e5a | 2012-05-15 08:59:12 +0000 | [diff] [blame] | 672 | } else |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 673 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 674 | if (len < tftp_block_size) |
Simon Glass | b437aad | 2011-10-24 18:00:03 +0000 | [diff] [blame] | 675 | tftp_complete(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 676 | break; |
| 677 | |
| 678 | case TFTP_ERROR: |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 679 | printf("\nTFTP error: '%s' (%d)\n", |
Kim Phillips | 878dad0 | 2013-01-16 18:09:19 -0600 | [diff] [blame] | 680 | pkt + 2, ntohs(*(__be16 *)pkt)); |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 681 | |
Kim Phillips | 878dad0 | 2013-01-16 18:09:19 -0600 | [diff] [blame] | 682 | switch (ntohs(*(__be16 *)pkt)) { |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 683 | case TFTP_ERR_FILE_NOT_FOUND: |
| 684 | case TFTP_ERR_ACCESS_DENIED: |
| 685 | puts("Not retrying...\n"); |
| 686 | eth_halt(); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 687 | net_set_state(NETLOOP_FAIL); |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 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"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 696 | #ifdef CONFIG_MCAST_TFTP |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 697 | mcast_cleanup(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 698 | #endif |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 699 | net_start_again(); |
Remy Bohmer | b46293f | 2009-10-28 22:13:40 +0100 | [diff] [blame] | 700 | break; |
| 701 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 702 | break; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 707 | static void tftp_timeout_handler(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 708 | { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 709 | if (++timeout_count > timeout_count_max) { |
Simon Glass | bd8a0e0 | 2011-10-24 18:00:04 +0000 | [diff] [blame] | 710 | restart("Retry count exceeded"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 711 | } else { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 712 | puts("T "); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 713 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 714 | if (tftp_state != STATE_RECV_WRQ) |
| 715 | tftp_send(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 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 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 738 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 739 | void tftp_start(enum proto_t protocol) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 740 | { |
Albert ARIBAUD \(3ADEV\) | 8300685 | 2015-10-12 00:02:57 +0200 | [diff] [blame] | 741 | #if CONFIG_NET_TFTP_VARS |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 742 | char *ep; /* Environment pointer */ |
Alessandro Rubini | 8832f6e | 2009-08-07 13:59:06 +0200 | [diff] [blame] | 743 | |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 744 | /* |
| 745 | * Allow the user to choose TFTP blocksize and timeout. |
| 746 | * TFTP protocol has a minimal timeout of 1 second. |
| 747 | */ |
Albert ARIBAUD \(3ADEV\) | 8300685 | 2015-10-12 00:02:57 +0200 | [diff] [blame] | 748 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 749 | ep = env_get("tftpblocksize"); |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 750 | if (ep != NULL) |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 751 | tftp_block_size_option = simple_strtol(ep, NULL, 10); |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 752 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 753 | ep = env_get("tftptimeout"); |
Luca Ceresoli | c4ffb9f | 2011-05-14 05:49:59 +0000 | [diff] [blame] | 754 | if (ep != NULL) |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 755 | timeout_ms = simple_strtol(ep, NULL, 10); |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 756 | |
Bin Meng | e67eaa8 | 2015-08-27 22:25:51 -0700 | [diff] [blame] | 757 | if (timeout_ms < 1000) { |
| 758 | printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 759 | timeout_ms); |
Bin Meng | e67eaa8 | 2015-08-27 22:25:51 -0700 | [diff] [blame] | 760 | timeout_ms = 1000; |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 763 | ep = env_get("tftptimeoutcountmax"); |
Albert ARIBAUD \(3ADEV\) | 8300685 | 2015-10-12 00:02:57 +0200 | [diff] [blame] | 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 | |
Wolfgang Denk | b233bd7 | 2010-01-17 23:55:53 +0100 | [diff] [blame] | 774 | debug("TFTP blocksize = %i, timeout = %ld ms\n", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 775 | tftp_block_size_option, timeout_ms); |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 776 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 777 | tftp_remote_ip = net_server_ip; |
Joe Hershberger | 9cb7b02 | 2018-07-03 19:36:43 -0500 | [diff] [blame] | 778 | if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) { |
Matthias Weisser | 6cc6326 | 2011-12-03 03:29:44 +0000 | [diff] [blame] | 779 | sprintf(default_filename, "%02X%02X%02X%02X.img", |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 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); |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 784 | |
Vladimir Zapolskiy | 04cc290 | 2017-06-28 22:56:07 +0300 | [diff] [blame] | 785 | strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN); |
| 786 | tftp_filename[DEFAULT_NAME_LEN - 1] = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 787 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 788 | printf("*** Warning: no boot file name; using '%s'\n", |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 789 | tftp_filename); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 792 | printf("Using %s device\n", eth_get_name()); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 793 | printf("TFTP %s server %pI4; our IP address is %pI4", |
Simon Glass | 4abd0e2 | 2011-10-27 06:24:29 +0000 | [diff] [blame] | 794 | #ifdef CONFIG_CMD_TFTPPUT |
| 795 | protocol == TFTPPUT ? "to" : "from", |
| 796 | #else |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 797 | "from", |
Simon Glass | 4abd0e2 | 2011-10-27 06:24:29 +0000 | [diff] [blame] | 798 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 799 | &tftp_remote_ip, &net_ip); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 800 | |
| 801 | /* Check if we need to send across this subnet */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 802 | if (net_gateway.s_addr && net_netmask.s_addr) { |
| 803 | struct in_addr our_net; |
| 804 | struct in_addr remote_net; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 805 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 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); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 810 | } |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 811 | putc('\n'); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 812 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 813 | printf("Filename '%s'.", tftp_filename); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 814 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 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, ""); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 821 | putc('\n'); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 822 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 823 | tftp_put_active = (protocol == TFTPPUT); |
| 824 | if (tftp_put_active) { |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 825 | printf("Save address: 0x%lx\n", save_addr); |
| 826 | printf("Save size: 0x%lx\n", save_size); |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 827 | net_boot_file_size = save_size; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 828 | puts("Saving: *\b"); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 829 | tftp_state = STATE_SEND_WRQ; |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 830 | new_transfer(); |
| 831 | } else |
| 832 | #endif |
| 833 | { |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 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); |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 842 | puts("Loading: *\b"); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 843 | tftp_state = STATE_SEND_RRQ; |
Jörg Krause | 6f341f5 | 2017-09-15 22:16:48 +0200 | [diff] [blame] | 844 | #ifdef CONFIG_CMD_BOOTEFI |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 845 | efi_set_bootdev("Net", "", tftp_filename); |
Jörg Krause | 6f341f5 | 2017-09-15 22:16:48 +0200 | [diff] [blame] | 846 | #endif |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 847 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 848 | |
Simon Glass | cab9a8d | 2012-10-11 13:57:36 +0000 | [diff] [blame] | 849 | time_start = get_timer(0); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 850 | timeout_count_max = tftp_timeout_count_max; |
Bartlomiej Sieka | 371cde7 | 2008-10-01 15:26:29 +0200 | [diff] [blame] | 851 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 852 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 853 | net_set_udp_handler(tftp_handler); |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 854 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 855 | net_set_icmp_handler(icmp_handler); |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 856 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 857 | tftp_remote_port = WELL_KNOWN_PORT; |
| 858 | timeout_count = 0; |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 859 | /* Use a pseudo-random port unless a specific port is set */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 860 | tftp_our_port = 1024 + (get_timer(0) % 3072); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 861 | |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 862 | #ifdef CONFIG_TFTP_PORT |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 863 | ep = env_get("tftpdstp"); |
Luca Ceresoli | 35dfb18 | 2011-05-14 05:50:00 +0000 | [diff] [blame] | 864 | if (ep != NULL) |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 865 | tftp_remote_port = simple_strtol(ep, NULL, 10); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 866 | ep = env_get("tftpsrcp"); |
Luca Ceresoli | 35dfb18 | 2011-05-14 05:50:00 +0000 | [diff] [blame] | 867 | if (ep != NULL) |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 868 | tftp_our_port = simple_strtol(ep, NULL, 10); |
Wolfgang Denk | e3cfce5 | 2005-09-24 22:37:32 +0200 | [diff] [blame] | 869 | #endif |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 870 | tftp_cur_block = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 871 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 872 | /* zero out server ether in case the server ip has changed */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 873 | memset(net_server_ethaddr, 0, 6); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 874 | /* Revert tftp_block_size to dflt */ |
| 875 | tftp_block_size = TFTP_BLOCK_SIZE; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 876 | #ifdef CONFIG_MCAST_TFTP |
Jean-Christophe PLAGNIOL-VILLARD | 5855541 | 2008-01-18 01:14:03 +0100 | [diff] [blame] | 877 | mcast_cleanup(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 878 | #endif |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 879 | #ifdef CONFIG_TFTP_TSIZE |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 880 | tftp_tsize = 0; |
| 881 | tftp_tsize_num_hash = 0; |
Robin Getz | b3b00ed | 2009-08-20 10:50:20 -0400 | [diff] [blame] | 882 | #endif |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 883 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 884 | tftp_send(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 885 | } |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 886 | |
| 887 | #ifdef CONFIG_CMD_TFTPSRV |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 888 | void tftp_start_server(void) |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 889 | { |
| 890 | tftp_filename[0] = 0; |
| 891 | |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 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 | } |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 898 | printf("Using %s device\n", eth_get_name()); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 899 | printf("Listening for TFTP transfer on %pI4\n", &net_ip); |
Simon Goldschmidt | a96cf8f | 2019-01-14 22:38:22 +0100 | [diff] [blame] | 900 | printf("Load address: 0x%lx\n", tftp_load_addr); |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 901 | |
| 902 | puts("Loading: *\b"); |
| 903 | |
Albert ARIBAUD \(3ADEV\) | 8300685 | 2015-10-12 00:02:57 +0200 | [diff] [blame] | 904 | timeout_count_max = tftp_timeout_count_max; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 905 | timeout_count = 0; |
| 906 | timeout_ms = TIMEOUT; |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 907 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 908 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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; |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 913 | |
| 914 | #ifdef CONFIG_TFTP_TSIZE |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 915 | tftp_tsize = 0; |
| 916 | tftp_tsize_num_hash = 0; |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 917 | #endif |
| 918 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 919 | tftp_state = STATE_RECV_WRQ; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 920 | net_set_udp_handler(tftp_handler); |
Andrew Ruder | 91393c5 | 2013-10-22 19:10:28 -0500 | [diff] [blame] | 921 | |
| 922 | /* zero out server ether in case the server ip has changed */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 923 | memset(net_server_ethaddr, 0, 6); |
Luca Ceresoli | b640ed3 | 2011-05-17 00:03:39 +0000 | [diff] [blame] | 924 | } |
| 925 | #endif /* CONFIG_CMD_TFTPSRV */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 926 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 927 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 928 | /* |
| 929 | * Credits: atftp project. |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 930 | */ |
| 931 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 932 | /* |
| 933 | * Pick up BcastAddr, Port, and whether I am [now] the master-client. |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 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 | { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 947 | int i; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 948 | struct in_addr addr; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 949 | char *mc_adr; |
| 950 | char *port; |
| 951 | char *mc; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 952 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 953 | mc_adr = NULL; |
| 954 | port = NULL; |
| 955 | mc = NULL; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 956 | /* march along looking for 'multicast\0', which has to start at least |
| 957 | * 14 bytes back from the end. |
| 958 | */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 959 | for (i = 0; i < len - 14; i++) |
| 960 | if (strcmp(pkt + i, "multicast") == 0) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 961 | break; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 962 | if (i >= (len - 14)) /* non-Multicast OACK, ign. */ |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 963 | return; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 964 | |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 965 | i += 10; /* strlen multicast */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 966 | mc_adr = pkt + i; |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 967 | for (; i < len; i++) { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 968 | if (*(pkt + i) == ',') { |
| 969 | *(pkt + i) = '\0'; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 970 | if (port) { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 971 | mc = pkt + i + 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 972 | break; |
| 973 | } else { |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 974 | port = pkt + i + 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | } |
Luca Ceresoli | 6072fcd | 2011-05-14 05:50:01 +0000 | [diff] [blame] | 978 | if (!port || !mc_adr || !mc) |
| 979 | return; |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 980 | if (tftp_mcast_active && tftp_mcast_master_client) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 981 | printf("I got a OACK as master Client, WRONG!\n"); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 982 | return; |
| 983 | } |
| 984 | /* ..I now accept packets destined for this MCAST addr, port */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 985 | if (!tftp_mcast_active) { |
| 986 | if (tftp_mcast_bitmap) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 987 | printf("Internal failure! no mcast.\n"); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 988 | free(tftp_mcast_bitmap); |
| 989 | tftp_mcast_bitmap = NULL; |
| 990 | tftp_mcast_disabled = 1; |
| 991 | return; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 992 | } |
| 993 | /* I malloc instead of pre-declare; so that if the file ends |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 994 | * up being too big for this bitmap I can retry |
| 995 | */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1000 | return; |
| 1001 | } |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 1002 | memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size); |
| 1003 | tftp_mcast_prev_hole = 0; |
| 1004 | tftp_mcast_active = 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1005 | } |
| 1006 | addr = string_to_ip(mc_adr); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 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)) { |
Luca Ceresoli | f40538a | 2011-05-14 05:49:57 +0000 | [diff] [blame] | 1012 | printf("Fail to set mcast, revert to TFTP\n"); |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 1013 | tftp_mcast_disabled = 1; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1014 | mcast_cleanup(); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 1015 | net_start_again(); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1016 | } |
| 1017 | } |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 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); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | #endif /* Multicast TFTP */ |