wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 25 | #include <command.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 26 | #include <stdio_dev.h> |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 27 | #include <net.h> |
| 28 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 31 | static char input_buffer[512]; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 32 | static int input_size; /* char count in input buffer */ |
| 33 | static int input_offset; /* offset to valid chars in input buffer */ |
| 34 | static int input_recursion; |
| 35 | static int output_recursion; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 36 | static int net_timeout; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 37 | static uchar nc_ether[6]; /* server enet address */ |
| 38 | static IPaddr_t nc_ip; /* server ip */ |
| 39 | static short nc_port; /* source/target port */ |
| 40 | static const char *output_packet; /* used by first send udp */ |
| 41 | static int output_packet_len; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 42 | |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 43 | static void nc_wait_arp_handler(uchar *pkt, unsigned dest, |
| 44 | IPaddr_t sip, unsigned src, |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 45 | unsigned len) |
| 46 | { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 47 | net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 50 | static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 51 | unsigned len) |
| 52 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 53 | if (input_size) |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 54 | net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 57 | static void nc_timeout(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 58 | { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 59 | net_set_state(NETLOOP_SUCCESS); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 62 | void NcStart(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 63 | { |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 64 | if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 65 | /* going to check for input packet */ |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 66 | net_set_udp_handler(nc_handler); |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 67 | NetSetTimeout(net_timeout, nc_timeout); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 68 | } else { |
| 69 | /* send arp request */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 70 | uchar *pkt; |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 71 | net_set_arp_handler(nc_wait_arp_handler); |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 72 | pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 73 | memcpy(pkt, output_packet, output_packet_len); |
| 74 | NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, |
| 75 | output_packet_len); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 79 | int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 80 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 81 | int end, chunk; |
| 82 | |
| 83 | if (dest != nc_port || !len) |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 84 | return 0; /* not for us */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 85 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 86 | debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt); |
| 87 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 88 | if (input_size == sizeof(input_buffer)) |
| 89 | return 1; /* no space */ |
| 90 | if (len > sizeof(input_buffer) - input_size) |
| 91 | len = sizeof(input_buffer) - input_size; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 92 | |
| 93 | end = input_offset + input_size; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 94 | if (end > sizeof(input_buffer)) |
| 95 | end -= sizeof(input_buffer); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 96 | |
| 97 | chunk = len; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 98 | if (end + len > sizeof(input_buffer)) { |
| 99 | chunk = sizeof(input_buffer) - end; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 100 | memcpy(input_buffer, pkt + chunk, len - chunk); |
| 101 | } |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 102 | memcpy(input_buffer + end, pkt, chunk); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 103 | |
| 104 | input_size += len; |
| 105 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 106 | return 1; |
| 107 | } |
| 108 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 109 | static void nc_send_packet(const char *buf, int len) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 110 | { |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 111 | struct eth_device *eth; |
| 112 | int inited = 0; |
| 113 | uchar *pkt; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 114 | uchar *ether; |
| 115 | IPaddr_t ip; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 116 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 117 | debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf); |
| 118 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 119 | eth = eth_get_dev(); |
| 120 | if (eth == NULL) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 121 | return; |
| 122 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 123 | if (!memcmp(nc_ether, NetEtherNullAddr, 6)) { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 124 | if (eth->state == ETH_STATE_ACTIVE) |
| 125 | return; /* inside net loop */ |
| 126 | output_packet = buf; |
| 127 | output_packet_len = len; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 128 | NetLoop(NETCONS); /* wait for arp reply and send packet */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 129 | output_packet_len = 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | if (eth->state != ETH_STATE_ACTIVE) { |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 134 | if (eth_init(gd->bd) < 0) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 135 | return; |
| 136 | inited = 1; |
| 137 | } |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 138 | pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 139 | memcpy(pkt, buf, len); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 140 | ether = nc_ether; |
| 141 | ip = nc_ip; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 142 | NetSendUDPPacket(ether, ip, nc_port, nc_port, len); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 143 | |
| 144 | if (inited) |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 145 | eth_halt(); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Mike Frysinger | 31da77e | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 148 | static int nc_start(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 149 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 150 | int netmask, our_ip; |
| 151 | |
| 152 | nc_port = 6666; /* default port */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 153 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 154 | if (getenv("ncip")) { |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 155 | char *p; |
| 156 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 157 | nc_ip = getenv_IPaddr("ncip"); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 158 | if (!nc_ip) |
| 159 | return -1; /* ncip is 0.0.0.0 */ |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 160 | p = strchr(getenv("ncip"), ':'); |
| 161 | if (p != NULL) |
| 162 | nc_port = simple_strtoul(p + 1, NULL, 10); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 163 | } else |
| 164 | nc_ip = ~0; /* ncip is not set */ |
| 165 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 166 | our_ip = getenv_IPaddr("ipaddr"); |
| 167 | netmask = getenv_IPaddr("netmask"); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 168 | |
| 169 | if (nc_ip == ~0 || /* 255.255.255.255 */ |
| 170 | ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */ |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 171 | (netmask | nc_ip) == ~0)) /* broadcast to our net */ |
| 172 | memset(nc_ether, 0xff, sizeof(nc_ether)); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 173 | else |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 174 | memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 175 | |
Joe Hershberger | 6a9ed16 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 176 | /* |
| 177 | * Initialize the static IP settings and buffer pointers |
| 178 | * incase we call NetSendUDPPacket before NetLoop |
| 179 | */ |
| 180 | net_init(); |
| 181 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 182 | return 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Mike Frysinger | 31da77e | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 185 | static void nc_putc(char c) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 186 | { |
| 187 | if (output_recursion) |
| 188 | return; |
| 189 | output_recursion = 1; |
| 190 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 191 | nc_send_packet(&c, 1); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 192 | |
| 193 | output_recursion = 0; |
| 194 | } |
| 195 | |
Mike Frysinger | 31da77e | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 196 | static void nc_puts(const char *s) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 197 | { |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 198 | int len; |
| 199 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 200 | if (output_recursion) |
| 201 | return; |
| 202 | output_recursion = 1; |
| 203 | |
Michael Walle | 9227aed | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 204 | len = strlen(s); |
| 205 | while (len) { |
| 206 | int send_len = min(len, 512); |
| 207 | nc_send_packet(s, send_len); |
| 208 | len -= send_len; |
| 209 | s += send_len; |
| 210 | } |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 211 | |
| 212 | output_recursion = 0; |
| 213 | } |
| 214 | |
Mike Frysinger | 31da77e | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 215 | static int nc_getc(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 216 | { |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 217 | uchar c; |
| 218 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 219 | input_recursion = 1; |
| 220 | |
| 221 | net_timeout = 0; /* no timeout */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 222 | while (!input_size) |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 223 | NetLoop(NETCONS); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 224 | |
| 225 | input_recursion = 0; |
| 226 | |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 227 | c = input_buffer[input_offset++]; |
| 228 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 229 | if (input_offset >= sizeof(input_buffer)) |
| 230 | input_offset -= sizeof(input_buffer); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 231 | input_size--; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 232 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 233 | return c; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Mike Frysinger | 31da77e | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 236 | static int nc_tstc(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 237 | { |
| 238 | struct eth_device *eth; |
| 239 | |
| 240 | if (input_recursion) |
| 241 | return 0; |
| 242 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 243 | if (input_size) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 244 | return 1; |
| 245 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 246 | eth = eth_get_dev(); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 247 | if (eth && eth->state == ETH_STATE_ACTIVE) |
| 248 | return 0; /* inside net loop */ |
| 249 | |
| 250 | input_recursion = 1; |
| 251 | |
| 252 | net_timeout = 1; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 253 | NetLoop(NETCONS); /* kind of poll */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 254 | |
| 255 | input_recursion = 0; |
| 256 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 257 | return input_size != 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 260 | int drv_nc_init(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 261 | { |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 262 | struct stdio_dev dev; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 263 | int rc; |
| 264 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 265 | memset(&dev, 0, sizeof(dev)); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 266 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 267 | strcpy(dev.name, "nc"); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 268 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 269 | dev.start = nc_start; |
| 270 | dev.putc = nc_putc; |
| 271 | dev.puts = nc_puts; |
| 272 | dev.getc = nc_getc; |
| 273 | dev.tstc = nc_tstc; |
| 274 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 275 | rc = stdio_register(&dev); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 276 | |
| 277 | return (rc == 0) ? 1 : rc; |
| 278 | } |