blob: 8fcf31c4e9bb612e900bbcf20a9d9641afb27568 [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
Joe Hershberger45f4a232012-07-31 06:09:17 +000031#ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
32#define CONFIG_NETCONSOLE_BUFFER_SIZE 512
33#endif
34
35static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
Joe Hershberger107b3d02012-05-15 08:59:13 +000036static int input_size; /* char count in input buffer */
37static int input_offset; /* offset to valid chars in input buffer */
38static int input_recursion;
39static int output_recursion;
wdenkb8fb6192004-08-02 21:11:11 +000040static int net_timeout;
Joe Hershberger107b3d02012-05-15 08:59:13 +000041static uchar nc_ether[6]; /* server enet address */
42static IPaddr_t nc_ip; /* server ip */
Joe Hershbergerd0f53962012-07-31 06:06:41 +000043static short nc_out_port; /* target output port */
44static short nc_in_port; /* source input port */
Joe Hershberger107b3d02012-05-15 08:59:13 +000045static const char *output_packet; /* used by first send udp */
46static int output_packet_len;
wdenkb8fb6192004-08-02 21:11:11 +000047
Luca Ceresoli428ab362011-04-18 06:19:50 +000048static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
49 IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000050 unsigned len)
51{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000052 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000053}
54
Luca Ceresoli428ab362011-04-18 06:19:50 +000055static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000056 unsigned len)
57{
wdenka0ebde52004-09-08 22:03:11 +000058 if (input_size)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000059 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000060}
61
Joe Hershberger107b3d02012-05-15 08:59:13 +000062static void nc_timeout(void)
wdenkb8fb6192004-08-02 21:11:11 +000063{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000064 net_set_state(NETLOOP_SUCCESS);
wdenkb8fb6192004-08-02 21:11:11 +000065}
66
Joe Hershberger107b3d02012-05-15 08:59:13 +000067void NcStart(void)
wdenkb8fb6192004-08-02 21:11:11 +000068{
Joe Hershberger107b3d02012-05-15 08:59:13 +000069 if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +000070 /* going to check for input packet */
Joe Hershbergerf50357b2012-05-23 07:59:15 +000071 net_set_udp_handler(nc_handler);
Joe Hershberger107b3d02012-05-15 08:59:13 +000072 NetSetTimeout(net_timeout, nc_timeout);
wdenkb8fb6192004-08-02 21:11:11 +000073 } else {
74 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +000075 uchar *pkt;
Joe Hershbergerf50357b2012-05-23 07:59:15 +000076 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershberger6fe8b452012-05-23 07:58:04 +000077 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +000078 memcpy(pkt, output_packet, output_packet_len);
Joe Hershbergerd0f53962012-07-31 06:06:41 +000079 NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
Joe Hershberger107b3d02012-05-15 08:59:13 +000080 output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +000081 }
82}
83
Joe Hershberger107b3d02012-05-15 08:59:13 +000084int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +000085{
wdenka0ebde52004-09-08 22:03:11 +000086 int end, chunk;
87
Joe Hershbergerd0f53962012-07-31 06:06:41 +000088 if (dest != nc_in_port || !len)
Joe Hershberger107b3d02012-05-15 08:59:13 +000089 return 0; /* not for us */
wdenkb8fb6192004-08-02 21:11:11 +000090
Joe Hershberger05a377b2012-05-23 08:01:04 +000091 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
92
Joe Hershberger107b3d02012-05-15 08:59:13 +000093 if (input_size == sizeof(input_buffer))
94 return 1; /* no space */
95 if (len > sizeof(input_buffer) - input_size)
96 len = sizeof(input_buffer) - input_size;
wdenka0ebde52004-09-08 22:03:11 +000097
98 end = input_offset + input_size;
Joe Hershberger107b3d02012-05-15 08:59:13 +000099 if (end > sizeof(input_buffer))
100 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000101
102 chunk = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000103 if (end + len > sizeof(input_buffer)) {
104 chunk = sizeof(input_buffer) - end;
wdenka0ebde52004-09-08 22:03:11 +0000105 memcpy(input_buffer, pkt + chunk, len - chunk);
106 }
Joe Hershberger107b3d02012-05-15 08:59:13 +0000107 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000108
109 input_size += len;
110
wdenkb8fb6192004-08-02 21:11:11 +0000111 return 1;
112}
113
Joe Hershberger107b3d02012-05-15 08:59:13 +0000114static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000115{
wdenkb8fb6192004-08-02 21:11:11 +0000116 struct eth_device *eth;
117 int inited = 0;
118 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000119 uchar *ether;
120 IPaddr_t ip;
wdenkb8fb6192004-08-02 21:11:11 +0000121
Joe Hershberger05a377b2012-05-23 08:01:04 +0000122 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
123
Joe Hershberger107b3d02012-05-15 08:59:13 +0000124 eth = eth_get_dev();
125 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000126 return;
127
Joe Hershberger107b3d02012-05-15 08:59:13 +0000128 if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenka0ebde52004-09-08 22:03:11 +0000129 if (eth->state == ETH_STATE_ACTIVE)
130 return; /* inside net loop */
131 output_packet = buf;
132 output_packet_len = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000133 NetLoop(NETCONS); /* wait for arp reply and send packet */
wdenka0ebde52004-09-08 22:03:11 +0000134 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000135 return;
136 }
137
138 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershberger107b3d02012-05-15 08:59:13 +0000139 if (eth_init(gd->bd) < 0)
wdenkb8fb6192004-08-02 21:11:11 +0000140 return;
141 inited = 1;
142 }
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000143 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000144 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000145 ether = nc_ether;
146 ip = nc_ip;
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000147 NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000148
149 if (inited)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000150 eth_halt();
wdenkb8fb6192004-08-02 21:11:11 +0000151}
152
Mike Frysinger31da77e2009-12-21 14:19:12 -0500153static int nc_start(void)
wdenkb8fb6192004-08-02 21:11:11 +0000154{
wdenka0ebde52004-09-08 22:03:11 +0000155 int netmask, our_ip;
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000156 char *p;
wdenka0ebde52004-09-08 22:03:11 +0000157
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000158 nc_out_port = 6666; /* default port */
159 nc_in_port = nc_out_port;
wdenkb8fb6192004-08-02 21:11:11 +0000160
Joe Hershberger107b3d02012-05-15 08:59:13 +0000161 if (getenv("ncip")) {
wdenkb21608e2005-04-20 09:28:54 +0000162
Joe Hershberger107b3d02012-05-15 08:59:13 +0000163 nc_ip = getenv_IPaddr("ncip");
wdenka0ebde52004-09-08 22:03:11 +0000164 if (!nc_ip)
165 return -1; /* ncip is 0.0.0.0 */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000166 p = strchr(getenv("ncip"), ':');
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000167 if (p != NULL) {
168 nc_out_port = simple_strtoul(p + 1, NULL, 10);
169 nc_in_port = nc_out_port;
170 }
wdenka0ebde52004-09-08 22:03:11 +0000171 } else
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000172 nc_ip = ~0; /* ncip is not set, so broadcast */
173
174 p = getenv("ncoutport");
175 if (p != NULL)
176 nc_out_port = simple_strtoul(p, NULL, 10);
177 p = getenv("ncinport");
178 if (p != NULL)
179 nc_in_port = simple_strtoul(p, NULL, 10);
wdenka0ebde52004-09-08 22:03:11 +0000180
Joe Hershberger107b3d02012-05-15 08:59:13 +0000181 our_ip = getenv_IPaddr("ipaddr");
182 netmask = getenv_IPaddr("netmask");
wdenka0ebde52004-09-08 22:03:11 +0000183
184 if (nc_ip == ~0 || /* 255.255.255.255 */
185 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000186 (netmask | nc_ip) == ~0)) /* broadcast to our net */
187 memset(nc_ether, 0xff, sizeof(nc_ether));
wdenka0ebde52004-09-08 22:03:11 +0000188 else
Joe Hershberger107b3d02012-05-15 08:59:13 +0000189 memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
wdenka0ebde52004-09-08 22:03:11 +0000190
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000191 /*
192 * Initialize the static IP settings and buffer pointers
193 * incase we call NetSendUDPPacket before NetLoop
194 */
195 net_init();
196
wdenka0ebde52004-09-08 22:03:11 +0000197 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000198}
199
Mike Frysinger31da77e2009-12-21 14:19:12 -0500200static void nc_putc(char c)
wdenkb8fb6192004-08-02 21:11:11 +0000201{
202 if (output_recursion)
203 return;
204 output_recursion = 1;
205
Joe Hershberger107b3d02012-05-15 08:59:13 +0000206 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000207
208 output_recursion = 0;
209}
210
Mike Frysinger31da77e2009-12-21 14:19:12 -0500211static void nc_puts(const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000212{
wdenkb21608e2005-04-20 09:28:54 +0000213 int len;
214
wdenkb8fb6192004-08-02 21:11:11 +0000215 if (output_recursion)
216 return;
217 output_recursion = 1;
218
Michael Walle9227aed2011-10-07 12:27:50 +0000219 len = strlen(s);
220 while (len) {
Joe Hershberger45f4a232012-07-31 06:09:17 +0000221 int send_len = min(len, sizeof(input_buffer));
Michael Walle9227aed2011-10-07 12:27:50 +0000222 nc_send_packet(s, send_len);
223 len -= send_len;
224 s += send_len;
225 }
wdenkb8fb6192004-08-02 21:11:11 +0000226
227 output_recursion = 0;
228}
229
Mike Frysinger31da77e2009-12-21 14:19:12 -0500230static int nc_getc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000231{
wdenkb21608e2005-04-20 09:28:54 +0000232 uchar c;
233
wdenkb8fb6192004-08-02 21:11:11 +0000234 input_recursion = 1;
235
236 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000237 while (!input_size)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000238 NetLoop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000239
240 input_recursion = 0;
241
wdenkb21608e2005-04-20 09:28:54 +0000242 c = input_buffer[input_offset++];
243
Joe Hershberger107b3d02012-05-15 08:59:13 +0000244 if (input_offset >= sizeof(input_buffer))
245 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000246 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000247
wdenka0ebde52004-09-08 22:03:11 +0000248 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000249}
250
Mike Frysinger31da77e2009-12-21 14:19:12 -0500251static int nc_tstc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000252{
253 struct eth_device *eth;
254
255 if (input_recursion)
256 return 0;
257
wdenka0ebde52004-09-08 22:03:11 +0000258 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000259 return 1;
260
Joe Hershberger107b3d02012-05-15 08:59:13 +0000261 eth = eth_get_dev();
wdenkb8fb6192004-08-02 21:11:11 +0000262 if (eth && eth->state == ETH_STATE_ACTIVE)
263 return 0; /* inside net loop */
264
265 input_recursion = 1;
266
267 net_timeout = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000268 NetLoop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000269
270 input_recursion = 0;
271
wdenka0ebde52004-09-08 22:03:11 +0000272 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000273}
274
Joe Hershberger107b3d02012-05-15 08:59:13 +0000275int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000276{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200277 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000278 int rc;
279
Joe Hershberger107b3d02012-05-15 08:59:13 +0000280 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000281
Joe Hershberger107b3d02012-05-15 08:59:13 +0000282 strcpy(dev.name, "nc");
wdenkb8fb6192004-08-02 21:11:11 +0000283 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
284 dev.start = nc_start;
285 dev.putc = nc_putc;
286 dev.puts = nc_puts;
287 dev.getc = nc_getc;
288 dev.tstc = nc_tstc;
289
Joe Hershberger107b3d02012-05-15 08:59:13 +0000290 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000291
292 return (rc == 0) ? 1 : rc;
293}