blob: 14243b8a92c05f22efc26504b80fc0bd338671b5 [file] [log] [blame]
wdenkb8fb6192004-08-02 21:11:11 +00001/*
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>
wdenkb8fb6192004-08-02 21:11:11 +000025#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020026#include <stdio_dev.h>
wdenkb8fb6192004-08-02 21:11:11 +000027#include <net.h>
28
Wolfgang Denk6405a152006-03-31 18:32:53 +020029DECLARE_GLOBAL_DATA_PTR;
30
wdenka0ebde52004-09-08 22:03:11 +000031static char input_buffer[512];
Joe Hershberger107b3d02012-05-15 08:59:13 +000032static int input_size; /* char count in input buffer */
33static int input_offset; /* offset to valid chars in input buffer */
34static int input_recursion;
35static int output_recursion;
wdenkb8fb6192004-08-02 21:11:11 +000036static int net_timeout;
Joe Hershberger107b3d02012-05-15 08:59:13 +000037static uchar nc_ether[6]; /* server enet address */
38static IPaddr_t nc_ip; /* server ip */
39static short nc_port; /* source/target port */
40static const char *output_packet; /* used by first send udp */
41static int output_packet_len;
wdenkb8fb6192004-08-02 21:11:11 +000042
Luca Ceresoli428ab362011-04-18 06:19:50 +000043static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
44 IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000045 unsigned len)
46{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000047 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000048}
49
Luca Ceresoli428ab362011-04-18 06:19:50 +000050static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000051 unsigned len)
52{
wdenka0ebde52004-09-08 22:03:11 +000053 if (input_size)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000054 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000055}
56
Joe Hershberger107b3d02012-05-15 08:59:13 +000057static void nc_timeout(void)
wdenkb8fb6192004-08-02 21:11:11 +000058{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000059 net_set_state(NETLOOP_SUCCESS);
wdenkb8fb6192004-08-02 21:11:11 +000060}
61
Joe Hershberger107b3d02012-05-15 08:59:13 +000062void NcStart(void)
wdenkb8fb6192004-08-02 21:11:11 +000063{
Joe Hershberger107b3d02012-05-15 08:59:13 +000064 if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +000065 /* going to check for input packet */
Joe Hershbergerf50357b2012-05-23 07:59:15 +000066 net_set_udp_handler(nc_handler);
Joe Hershberger107b3d02012-05-15 08:59:13 +000067 NetSetTimeout(net_timeout, nc_timeout);
wdenkb8fb6192004-08-02 21:11:11 +000068 } else {
69 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +000070 uchar *pkt;
Joe Hershbergerf50357b2012-05-23 07:59:15 +000071 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershberger6fe8b452012-05-23 07:58:04 +000072 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +000073 memcpy(pkt, output_packet, output_packet_len);
74 NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port,
75 output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +000076 }
77}
78
Joe Hershberger107b3d02012-05-15 08:59:13 +000079int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +000080{
wdenka0ebde52004-09-08 22:03:11 +000081 int end, chunk;
82
83 if (dest != nc_port || !len)
Joe Hershberger107b3d02012-05-15 08:59:13 +000084 return 0; /* not for us */
wdenkb8fb6192004-08-02 21:11:11 +000085
Joe Hershberger05a377b2012-05-23 08:01:04 +000086 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
87
Joe Hershberger107b3d02012-05-15 08:59:13 +000088 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;
wdenka0ebde52004-09-08 22:03:11 +000092
93 end = input_offset + input_size;
Joe Hershberger107b3d02012-05-15 08:59:13 +000094 if (end > sizeof(input_buffer))
95 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +000096
97 chunk = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +000098 if (end + len > sizeof(input_buffer)) {
99 chunk = sizeof(input_buffer) - end;
wdenka0ebde52004-09-08 22:03:11 +0000100 memcpy(input_buffer, pkt + chunk, len - chunk);
101 }
Joe Hershberger107b3d02012-05-15 08:59:13 +0000102 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000103
104 input_size += len;
105
wdenkb8fb6192004-08-02 21:11:11 +0000106 return 1;
107}
108
Joe Hershberger107b3d02012-05-15 08:59:13 +0000109static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000110{
wdenkb8fb6192004-08-02 21:11:11 +0000111 struct eth_device *eth;
112 int inited = 0;
113 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000114 uchar *ether;
115 IPaddr_t ip;
wdenkb8fb6192004-08-02 21:11:11 +0000116
Joe Hershberger05a377b2012-05-23 08:01:04 +0000117 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
118
Joe Hershberger107b3d02012-05-15 08:59:13 +0000119 eth = eth_get_dev();
120 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000121 return;
122
Joe Hershberger107b3d02012-05-15 08:59:13 +0000123 if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenka0ebde52004-09-08 22:03:11 +0000124 if (eth->state == ETH_STATE_ACTIVE)
125 return; /* inside net loop */
126 output_packet = buf;
127 output_packet_len = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000128 NetLoop(NETCONS); /* wait for arp reply and send packet */
wdenka0ebde52004-09-08 22:03:11 +0000129 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000130 return;
131 }
132
133 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershberger107b3d02012-05-15 08:59:13 +0000134 if (eth_init(gd->bd) < 0)
wdenkb8fb6192004-08-02 21:11:11 +0000135 return;
136 inited = 1;
137 }
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000138 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000139 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000140 ether = nc_ether;
141 ip = nc_ip;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000142 NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000143
144 if (inited)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000145 eth_halt();
wdenkb8fb6192004-08-02 21:11:11 +0000146}
147
Mike Frysinger31da77e2009-12-21 14:19:12 -0500148static int nc_start(void)
wdenkb8fb6192004-08-02 21:11:11 +0000149{
wdenka0ebde52004-09-08 22:03:11 +0000150 int netmask, our_ip;
151
152 nc_port = 6666; /* default port */
wdenkb8fb6192004-08-02 21:11:11 +0000153
Joe Hershberger107b3d02012-05-15 08:59:13 +0000154 if (getenv("ncip")) {
wdenkb21608e2005-04-20 09:28:54 +0000155 char *p;
156
Joe Hershberger107b3d02012-05-15 08:59:13 +0000157 nc_ip = getenv_IPaddr("ncip");
wdenka0ebde52004-09-08 22:03:11 +0000158 if (!nc_ip)
159 return -1; /* ncip is 0.0.0.0 */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000160 p = strchr(getenv("ncip"), ':');
161 if (p != NULL)
162 nc_port = simple_strtoul(p + 1, NULL, 10);
wdenka0ebde52004-09-08 22:03:11 +0000163 } else
164 nc_ip = ~0; /* ncip is not set */
165
Joe Hershberger107b3d02012-05-15 08:59:13 +0000166 our_ip = getenv_IPaddr("ipaddr");
167 netmask = getenv_IPaddr("netmask");
wdenka0ebde52004-09-08 22:03:11 +0000168
169 if (nc_ip == ~0 || /* 255.255.255.255 */
170 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000171 (netmask | nc_ip) == ~0)) /* broadcast to our net */
172 memset(nc_ether, 0xff, sizeof(nc_ether));
wdenka0ebde52004-09-08 22:03:11 +0000173 else
Joe Hershberger107b3d02012-05-15 08:59:13 +0000174 memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
wdenka0ebde52004-09-08 22:03:11 +0000175
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000176 /*
177 * Initialize the static IP settings and buffer pointers
178 * incase we call NetSendUDPPacket before NetLoop
179 */
180 net_init();
181
wdenka0ebde52004-09-08 22:03:11 +0000182 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000183}
184
Mike Frysinger31da77e2009-12-21 14:19:12 -0500185static void nc_putc(char c)
wdenkb8fb6192004-08-02 21:11:11 +0000186{
187 if (output_recursion)
188 return;
189 output_recursion = 1;
190
Joe Hershberger107b3d02012-05-15 08:59:13 +0000191 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000192
193 output_recursion = 0;
194}
195
Mike Frysinger31da77e2009-12-21 14:19:12 -0500196static void nc_puts(const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000197{
wdenkb21608e2005-04-20 09:28:54 +0000198 int len;
199
wdenkb8fb6192004-08-02 21:11:11 +0000200 if (output_recursion)
201 return;
202 output_recursion = 1;
203
Michael Walle9227aed2011-10-07 12:27:50 +0000204 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 }
wdenkb8fb6192004-08-02 21:11:11 +0000211
212 output_recursion = 0;
213}
214
Mike Frysinger31da77e2009-12-21 14:19:12 -0500215static int nc_getc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000216{
wdenkb21608e2005-04-20 09:28:54 +0000217 uchar c;
218
wdenkb8fb6192004-08-02 21:11:11 +0000219 input_recursion = 1;
220
221 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000222 while (!input_size)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000223 NetLoop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000224
225 input_recursion = 0;
226
wdenkb21608e2005-04-20 09:28:54 +0000227 c = input_buffer[input_offset++];
228
Joe Hershberger107b3d02012-05-15 08:59:13 +0000229 if (input_offset >= sizeof(input_buffer))
230 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000231 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000232
wdenka0ebde52004-09-08 22:03:11 +0000233 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000234}
235
Mike Frysinger31da77e2009-12-21 14:19:12 -0500236static int nc_tstc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000237{
238 struct eth_device *eth;
239
240 if (input_recursion)
241 return 0;
242
wdenka0ebde52004-09-08 22:03:11 +0000243 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000244 return 1;
245
Joe Hershberger107b3d02012-05-15 08:59:13 +0000246 eth = eth_get_dev();
wdenkb8fb6192004-08-02 21:11:11 +0000247 if (eth && eth->state == ETH_STATE_ACTIVE)
248 return 0; /* inside net loop */
249
250 input_recursion = 1;
251
252 net_timeout = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000253 NetLoop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000254
255 input_recursion = 0;
256
wdenka0ebde52004-09-08 22:03:11 +0000257 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000258}
259
Joe Hershberger107b3d02012-05-15 08:59:13 +0000260int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000261{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200262 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000263 int rc;
264
Joe Hershberger107b3d02012-05-15 08:59:13 +0000265 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000266
Joe Hershberger107b3d02012-05-15 08:59:13 +0000267 strcpy(dev.name, "nc");
wdenkb8fb6192004-08-02 21:11:11 +0000268 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 Hershberger107b3d02012-05-15 08:59:13 +0000275 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000276
277 return (rc == 0) ? 1 : rc;
278}