blob: 19e563ea820a25eb1193c91b8f5a3b95bf581619 [file] [log] [blame]
wdenk68ceb292004-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>
wdenk68ceb292004-08-02 21:11:11 +000025#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020026#include <stdio_dev.h>
wdenk68ceb292004-08-02 21:11:11 +000027#include <net.h>
28
Wolfgang Denkd87080b2006-03-31 18:32:53 +020029DECLARE_GLOBAL_DATA_PTR;
30
wdenkeedcd072004-09-08 22:03:11 +000031static char input_buffer[512];
Joe Hershbergere1902ac2012-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;
wdenk68ceb292004-08-02 21:11:11 +000036static int net_timeout;
Joe Hershbergere1902ac2012-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;
wdenk68ceb292004-08-02 21:11:11 +000042
Luca Ceresoli03eb1292011-04-18 06:19:50 +000043static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
44 IPaddr_t sip, unsigned src,
wdenk68ceb292004-08-02 21:11:11 +000045 unsigned len)
46{
Joe Hershberger22f6e992012-05-23 07:59:14 +000047 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenk68ceb292004-08-02 21:11:11 +000048}
49
Luca Ceresoli03eb1292011-04-18 06:19:50 +000050static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
wdenk68ceb292004-08-02 21:11:11 +000051 unsigned len)
52{
wdenkeedcd072004-09-08 22:03:11 +000053 if (input_size)
Joe Hershberger22f6e992012-05-23 07:59:14 +000054 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenk68ceb292004-08-02 21:11:11 +000055}
56
Joe Hershbergere1902ac2012-05-15 08:59:13 +000057static void nc_timeout(void)
wdenk68ceb292004-08-02 21:11:11 +000058{
Joe Hershberger22f6e992012-05-23 07:59:14 +000059 net_set_state(NETLOOP_SUCCESS);
wdenk68ceb292004-08-02 21:11:11 +000060}
61
Joe Hershbergere1902ac2012-05-15 08:59:13 +000062void NcStart(void)
wdenk68ceb292004-08-02 21:11:11 +000063{
Joe Hershbergere1902ac2012-05-15 08:59:13 +000064 if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenk68ceb292004-08-02 21:11:11 +000065 /* going to check for input packet */
Joe Hershbergerece223b2012-05-23 07:59:15 +000066 net_set_udp_handler(nc_handler);
Joe Hershbergere1902ac2012-05-15 08:59:13 +000067 NetSetTimeout(net_timeout, nc_timeout);
wdenk68ceb292004-08-02 21:11:11 +000068 } else {
69 /* send arp request */
wdenkeedcd072004-09-08 22:03:11 +000070 uchar *pkt;
Joe Hershbergerece223b2012-05-23 07:59:15 +000071 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershberger594c26f2012-05-23 07:58:04 +000072 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-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);
wdenk68ceb292004-08-02 21:11:11 +000076 }
77}
78
Joe Hershbergere1902ac2012-05-15 08:59:13 +000079int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
wdenk68ceb292004-08-02 21:11:11 +000080{
wdenkeedcd072004-09-08 22:03:11 +000081 int end, chunk;
82
83 if (dest != nc_port || !len)
Joe Hershbergere1902ac2012-05-15 08:59:13 +000084 return 0; /* not for us */
wdenk68ceb292004-08-02 21:11:11 +000085
Joe Hershbergere1902ac2012-05-15 08:59:13 +000086 if (input_size == sizeof(input_buffer))
87 return 1; /* no space */
88 if (len > sizeof(input_buffer) - input_size)
89 len = sizeof(input_buffer) - input_size;
wdenkeedcd072004-09-08 22:03:11 +000090
91 end = input_offset + input_size;
Joe Hershbergere1902ac2012-05-15 08:59:13 +000092 if (end > sizeof(input_buffer))
93 end -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +000094
95 chunk = len;
Joe Hershbergere1902ac2012-05-15 08:59:13 +000096 if (end + len > sizeof(input_buffer)) {
97 chunk = sizeof(input_buffer) - end;
wdenkeedcd072004-09-08 22:03:11 +000098 memcpy(input_buffer, pkt + chunk, len - chunk);
99 }
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000100 memcpy(input_buffer + end, pkt, chunk);
wdenkeedcd072004-09-08 22:03:11 +0000101
102 input_size += len;
103
wdenk68ceb292004-08-02 21:11:11 +0000104 return 1;
105}
106
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000107static void nc_send_packet(const char *buf, int len)
wdenk68ceb292004-08-02 21:11:11 +0000108{
wdenk68ceb292004-08-02 21:11:11 +0000109 struct eth_device *eth;
110 int inited = 0;
111 uchar *pkt;
wdenkeedcd072004-09-08 22:03:11 +0000112 uchar *ether;
113 IPaddr_t ip;
wdenk68ceb292004-08-02 21:11:11 +0000114
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000115 eth = eth_get_dev();
116 if (eth == NULL)
wdenk68ceb292004-08-02 21:11:11 +0000117 return;
wdenk68ceb292004-08-02 21:11:11 +0000118
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000119 if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenkeedcd072004-09-08 22:03:11 +0000120 if (eth->state == ETH_STATE_ACTIVE)
121 return; /* inside net loop */
122 output_packet = buf;
123 output_packet_len = len;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000124 NetLoop(NETCONS); /* wait for arp reply and send packet */
wdenkeedcd072004-09-08 22:03:11 +0000125 output_packet_len = 0;
126 return;
127 }
128
wdenk68ceb292004-08-02 21:11:11 +0000129 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000130 if (eth_init(gd->bd) < 0)
wdenk68ceb292004-08-02 21:11:11 +0000131 return;
132 inited = 1;
133 }
Joe Hershberger594c26f2012-05-23 07:58:04 +0000134 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000135 memcpy(pkt, buf, len);
wdenkeedcd072004-09-08 22:03:11 +0000136 ether = nc_ether;
137 ip = nc_ip;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000138 NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
wdenk68ceb292004-08-02 21:11:11 +0000139
140 if (inited)
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000141 eth_halt();
wdenk68ceb292004-08-02 21:11:11 +0000142}
143
Mike Frysingerfa2744d2009-12-21 14:19:12 -0500144static int nc_start(void)
wdenk68ceb292004-08-02 21:11:11 +0000145{
wdenkeedcd072004-09-08 22:03:11 +0000146 int netmask, our_ip;
wdenk68ceb292004-08-02 21:11:11 +0000147
wdenkeedcd072004-09-08 22:03:11 +0000148 nc_port = 6666; /* default port */
149
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000150 if (getenv("ncip")) {
wdenkb2323ea2005-04-20 09:28:54 +0000151 char *p;
152
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000153 nc_ip = getenv_IPaddr("ncip");
wdenkeedcd072004-09-08 22:03:11 +0000154 if (!nc_ip)
155 return -1; /* ncip is 0.0.0.0 */
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000156 p = strchr(getenv("ncip"), ':');
157 if (p != NULL)
158 nc_port = simple_strtoul(p + 1, NULL, 10);
wdenkeedcd072004-09-08 22:03:11 +0000159 } else
160 nc_ip = ~0; /* ncip is not set */
161
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000162 our_ip = getenv_IPaddr("ipaddr");
163 netmask = getenv_IPaddr("netmask");
wdenkeedcd072004-09-08 22:03:11 +0000164
165 if (nc_ip == ~0 || /* 255.255.255.255 */
166 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000167 (netmask | nc_ip) == ~0)) /* broadcast to our net */
168 memset(nc_ether, 0xff, sizeof(nc_ether));
wdenkeedcd072004-09-08 22:03:11 +0000169 else
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000170 memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
wdenkeedcd072004-09-08 22:03:11 +0000171
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000172 /*
173 * Initialize the static IP settings and buffer pointers
174 * incase we call NetSendUDPPacket before NetLoop
175 */
176 net_init();
177
wdenkeedcd072004-09-08 22:03:11 +0000178 return 0;
wdenk68ceb292004-08-02 21:11:11 +0000179}
180
Mike Frysingerfa2744d2009-12-21 14:19:12 -0500181static void nc_putc(char c)
wdenk68ceb292004-08-02 21:11:11 +0000182{
183 if (output_recursion)
184 return;
185 output_recursion = 1;
186
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000187 nc_send_packet(&c, 1);
wdenk68ceb292004-08-02 21:11:11 +0000188
189 output_recursion = 0;
190}
191
Mike Frysingerfa2744d2009-12-21 14:19:12 -0500192static void nc_puts(const char *s)
wdenk68ceb292004-08-02 21:11:11 +0000193{
wdenkb2323ea2005-04-20 09:28:54 +0000194 int len;
195
wdenk68ceb292004-08-02 21:11:11 +0000196 if (output_recursion)
197 return;
198 output_recursion = 1;
199
Michael Walle1a9845b2011-10-07 12:27:50 +0000200 len = strlen(s);
201 while (len) {
202 int send_len = min(len, 512);
203 nc_send_packet(s, send_len);
204 len -= send_len;
205 s += send_len;
206 }
wdenk68ceb292004-08-02 21:11:11 +0000207
208 output_recursion = 0;
209}
210
Mike Frysingerfa2744d2009-12-21 14:19:12 -0500211static int nc_getc(void)
wdenk68ceb292004-08-02 21:11:11 +0000212{
wdenkb2323ea2005-04-20 09:28:54 +0000213 uchar c;
214
wdenk68ceb292004-08-02 21:11:11 +0000215 input_recursion = 1;
216
217 net_timeout = 0; /* no timeout */
wdenkeedcd072004-09-08 22:03:11 +0000218 while (!input_size)
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000219 NetLoop(NETCONS);
wdenk68ceb292004-08-02 21:11:11 +0000220
221 input_recursion = 0;
222
wdenkb2323ea2005-04-20 09:28:54 +0000223 c = input_buffer[input_offset++];
224
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000225 if (input_offset >= sizeof(input_buffer))
226 input_offset -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +0000227 input_size--;
wdenk68ceb292004-08-02 21:11:11 +0000228
wdenkeedcd072004-09-08 22:03:11 +0000229 return c;
wdenk68ceb292004-08-02 21:11:11 +0000230}
231
Mike Frysingerfa2744d2009-12-21 14:19:12 -0500232static int nc_tstc(void)
wdenk68ceb292004-08-02 21:11:11 +0000233{
234 struct eth_device *eth;
235
236 if (input_recursion)
237 return 0;
238
wdenkeedcd072004-09-08 22:03:11 +0000239 if (input_size)
wdenk68ceb292004-08-02 21:11:11 +0000240 return 1;
241
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000242 eth = eth_get_dev();
wdenk68ceb292004-08-02 21:11:11 +0000243 if (eth && eth->state == ETH_STATE_ACTIVE)
244 return 0; /* inside net loop */
245
246 input_recursion = 1;
247
248 net_timeout = 1;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000249 NetLoop(NETCONS); /* kind of poll */
wdenk68ceb292004-08-02 21:11:11 +0000250
251 input_recursion = 0;
252
wdenkeedcd072004-09-08 22:03:11 +0000253 return input_size != 0;
wdenk68ceb292004-08-02 21:11:11 +0000254}
255
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000256int drv_nc_init(void)
wdenk68ceb292004-08-02 21:11:11 +0000257{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200258 struct stdio_dev dev;
wdenk68ceb292004-08-02 21:11:11 +0000259 int rc;
260
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000261 memset(&dev, 0, sizeof(dev));
wdenk68ceb292004-08-02 21:11:11 +0000262
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000263 strcpy(dev.name, "nc");
wdenk68ceb292004-08-02 21:11:11 +0000264 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
265 dev.start = nc_start;
266 dev.putc = nc_putc;
267 dev.puts = nc_puts;
268 dev.getc = nc_getc;
269 dev.tstc = nc_tstc;
270
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000271 rc = stdio_register(&dev);
wdenk68ceb292004-08-02 21:11:11 +0000272
273 return (rc == 0) ? 1 : rc;
274}