blob: 63ce73c81891004594e4b1ff42e3ef935b8f671d [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;
Joe Hershberger9f374062012-08-03 10:59:08 +000047/*
48 * Start with a default last protocol.
49 * We are only interested in NETCONS or not.
50 */
51enum proto_t net_loop_last_protocol = BOOTP;
wdenkb8fb6192004-08-02 21:11:11 +000052
Luca Ceresoli428ab362011-04-18 06:19:50 +000053static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
54 IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000055 unsigned len)
56{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000057 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000058}
59
Luca Ceresoli428ab362011-04-18 06:19:50 +000060static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000061 unsigned len)
62{
wdenka0ebde52004-09-08 22:03:11 +000063 if (input_size)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000064 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000065}
66
Joe Hershberger107b3d02012-05-15 08:59:13 +000067static void nc_timeout(void)
wdenkb8fb6192004-08-02 21:11:11 +000068{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000069 net_set_state(NETLOOP_SUCCESS);
wdenkb8fb6192004-08-02 21:11:11 +000070}
71
Joe Hershberger66257802012-09-18 10:01:31 +000072static int is_broadcast(IPaddr_t ip)
73{
74 static IPaddr_t netmask;
75 static IPaddr_t our_ip;
76 static int env_changed_id;
77 int env_id = get_env_id();
78
79 /* update only when the environment has changed */
80 if (env_changed_id != env_id) {
81 netmask = getenv_IPaddr("netmask");
82 our_ip = getenv_IPaddr("ipaddr");
83
84 env_changed_id = env_id;
85 }
86
87 return (ip == ~0 || /* 255.255.255.255 */
88 ((netmask & our_ip) == (netmask & ip) && /* on the same net */
89 (netmask | ip) == ~0)); /* broadcast to our net */
90}
91
92static int refresh_settings_from_env(void)
93{
94 const char *p;
95 static int env_changed_id;
96 int env_id = get_env_id();
97
98 /* update only when the environment has changed */
99 if (env_changed_id != env_id) {
100 if (getenv("ncip")) {
101 nc_ip = getenv_IPaddr("ncip");
102 if (!nc_ip)
103 return -1; /* ncip is 0.0.0.0 */
104 p = strchr(getenv("ncip"), ':');
105 if (p != NULL) {
106 nc_out_port = simple_strtoul(p + 1, NULL, 10);
107 nc_in_port = nc_out_port;
108 }
109 } else
110 nc_ip = ~0; /* ncip is not set, so broadcast */
111
112 p = getenv("ncoutport");
113 if (p != NULL)
114 nc_out_port = simple_strtoul(p, NULL, 10);
115 p = getenv("ncinport");
116 if (p != NULL)
117 nc_in_port = simple_strtoul(p, NULL, 10);
118
119 if (is_broadcast(nc_ip))
120 /* broadcast MAC address */
121 memset(nc_ether, 0xff, sizeof(nc_ether));
122 else
123 /* force arp request */
124 memset(nc_ether, 0, sizeof(nc_ether));
125 }
126 return 0;
127}
128
129/**
130 * Called from NetLoop in net/net.c before each packet
131 */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000132void NcStart(void)
wdenkb8fb6192004-08-02 21:11:11 +0000133{
Joe Hershberger66257802012-09-18 10:01:31 +0000134 refresh_settings_from_env();
Joe Hershberger107b3d02012-05-15 08:59:13 +0000135 if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +0000136 /* going to check for input packet */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000137 net_set_udp_handler(nc_handler);
Joe Hershberger107b3d02012-05-15 08:59:13 +0000138 NetSetTimeout(net_timeout, nc_timeout);
wdenkb8fb6192004-08-02 21:11:11 +0000139 } else {
140 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +0000141 uchar *pkt;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000142 net_set_arp_handler(nc_wait_arp_handler);
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, output_packet, output_packet_len);
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000145 NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
Joe Hershberger107b3d02012-05-15 08:59:13 +0000146 output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +0000147 }
148}
149
Joe Hershberger107b3d02012-05-15 08:59:13 +0000150int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +0000151{
wdenka0ebde52004-09-08 22:03:11 +0000152 int end, chunk;
153
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000154 if (dest != nc_in_port || !len)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000155 return 0; /* not for us */
wdenkb8fb6192004-08-02 21:11:11 +0000156
Joe Hershberger05a377b2012-05-23 08:01:04 +0000157 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
158
Joe Hershberger107b3d02012-05-15 08:59:13 +0000159 if (input_size == sizeof(input_buffer))
160 return 1; /* no space */
161 if (len > sizeof(input_buffer) - input_size)
162 len = sizeof(input_buffer) - input_size;
wdenka0ebde52004-09-08 22:03:11 +0000163
164 end = input_offset + input_size;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000165 if (end > sizeof(input_buffer))
166 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000167
168 chunk = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000169 if (end + len > sizeof(input_buffer)) {
170 chunk = sizeof(input_buffer) - end;
wdenka0ebde52004-09-08 22:03:11 +0000171 memcpy(input_buffer, pkt + chunk, len - chunk);
172 }
Joe Hershberger107b3d02012-05-15 08:59:13 +0000173 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000174
175 input_size += len;
176
wdenkb8fb6192004-08-02 21:11:11 +0000177 return 1;
178}
179
Joe Hershberger107b3d02012-05-15 08:59:13 +0000180static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000181{
wdenkb8fb6192004-08-02 21:11:11 +0000182 struct eth_device *eth;
183 int inited = 0;
184 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000185 uchar *ether;
186 IPaddr_t ip;
wdenkb8fb6192004-08-02 21:11:11 +0000187
Joe Hershberger05a377b2012-05-23 08:01:04 +0000188 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
189
Joe Hershberger107b3d02012-05-15 08:59:13 +0000190 eth = eth_get_dev();
191 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000192 return;
193
Joe Hershberger107b3d02012-05-15 08:59:13 +0000194 if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenka0ebde52004-09-08 22:03:11 +0000195 if (eth->state == ETH_STATE_ACTIVE)
196 return; /* inside net loop */
197 output_packet = buf;
198 output_packet_len = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000199 NetLoop(NETCONS); /* wait for arp reply and send packet */
wdenka0ebde52004-09-08 22:03:11 +0000200 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000201 return;
202 }
203
204 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000205 if (eth_is_on_demand_init()) {
206 if (eth_init(gd->bd) < 0)
207 return;
208 eth_set_last_protocol(NETCONS);
209 } else
210 eth_init_state_only(gd->bd);
211
wdenkb8fb6192004-08-02 21:11:11 +0000212 inited = 1;
213 }
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000214 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000215 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000216 ether = nc_ether;
217 ip = nc_ip;
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000218 NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000219
Joe Hershberger9f374062012-08-03 10:59:08 +0000220 if (inited) {
221 if (eth_is_on_demand_init())
222 eth_halt();
223 else
224 eth_halt_state_only();
225 }
wdenkb8fb6192004-08-02 21:11:11 +0000226}
227
Mike Frysinger31da77e2009-12-21 14:19:12 -0500228static int nc_start(void)
wdenkb8fb6192004-08-02 21:11:11 +0000229{
Joe Hershberger66257802012-09-18 10:01:31 +0000230 int retval;
wdenka0ebde52004-09-08 22:03:11 +0000231
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000232 nc_out_port = 6666; /* default port */
233 nc_in_port = nc_out_port;
wdenkb8fb6192004-08-02 21:11:11 +0000234
Joe Hershberger66257802012-09-18 10:01:31 +0000235 retval = refresh_settings_from_env();
236 if (retval != 0)
237 return retval;
wdenka0ebde52004-09-08 22:03:11 +0000238
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000239 /*
240 * Initialize the static IP settings and buffer pointers
241 * incase we call NetSendUDPPacket before NetLoop
242 */
243 net_init();
244
wdenka0ebde52004-09-08 22:03:11 +0000245 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000246}
247
Mike Frysinger31da77e2009-12-21 14:19:12 -0500248static void nc_putc(char c)
wdenkb8fb6192004-08-02 21:11:11 +0000249{
250 if (output_recursion)
251 return;
252 output_recursion = 1;
253
Joe Hershberger107b3d02012-05-15 08:59:13 +0000254 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000255
256 output_recursion = 0;
257}
258
Mike Frysinger31da77e2009-12-21 14:19:12 -0500259static void nc_puts(const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000260{
wdenkb21608e2005-04-20 09:28:54 +0000261 int len;
262
wdenkb8fb6192004-08-02 21:11:11 +0000263 if (output_recursion)
264 return;
265 output_recursion = 1;
266
Michael Walle9227aed2011-10-07 12:27:50 +0000267 len = strlen(s);
268 while (len) {
Joe Hershberger45f4a232012-07-31 06:09:17 +0000269 int send_len = min(len, sizeof(input_buffer));
Michael Walle9227aed2011-10-07 12:27:50 +0000270 nc_send_packet(s, send_len);
271 len -= send_len;
272 s += send_len;
273 }
wdenkb8fb6192004-08-02 21:11:11 +0000274
275 output_recursion = 0;
276}
277
Mike Frysinger31da77e2009-12-21 14:19:12 -0500278static int nc_getc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000279{
wdenkb21608e2005-04-20 09:28:54 +0000280 uchar c;
281
wdenkb8fb6192004-08-02 21:11:11 +0000282 input_recursion = 1;
283
284 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000285 while (!input_size)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000286 NetLoop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000287
288 input_recursion = 0;
289
wdenkb21608e2005-04-20 09:28:54 +0000290 c = input_buffer[input_offset++];
291
Joe Hershberger107b3d02012-05-15 08:59:13 +0000292 if (input_offset >= sizeof(input_buffer))
293 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000294 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000295
wdenka0ebde52004-09-08 22:03:11 +0000296 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000297}
298
Mike Frysinger31da77e2009-12-21 14:19:12 -0500299static int nc_tstc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000300{
301 struct eth_device *eth;
302
303 if (input_recursion)
304 return 0;
305
wdenka0ebde52004-09-08 22:03:11 +0000306 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000307 return 1;
308
Joe Hershberger107b3d02012-05-15 08:59:13 +0000309 eth = eth_get_dev();
wdenkb8fb6192004-08-02 21:11:11 +0000310 if (eth && eth->state == ETH_STATE_ACTIVE)
311 return 0; /* inside net loop */
312
313 input_recursion = 1;
314
315 net_timeout = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000316 NetLoop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000317
318 input_recursion = 0;
319
wdenka0ebde52004-09-08 22:03:11 +0000320 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000321}
322
Joe Hershberger107b3d02012-05-15 08:59:13 +0000323int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000324{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200325 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000326 int rc;
327
Joe Hershberger107b3d02012-05-15 08:59:13 +0000328 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000329
Joe Hershberger107b3d02012-05-15 08:59:13 +0000330 strcpy(dev.name, "nc");
wdenkb8fb6192004-08-02 21:11:11 +0000331 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
332 dev.start = nc_start;
333 dev.putc = nc_putc;
334 dev.puts = nc_puts;
335 dev.getc = nc_getc;
336 dev.tstc = nc_tstc;
337
Joe Hershberger107b3d02012-05-15 08:59:13 +0000338 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000339
340 return (rc == 0) ? 1 : rc;
341}