blob: 9aba0c5e3c025704afb71785be7f06180a0a60bc [file] [log] [blame]
wdenkb8fb6192004-08-02 21:11:11 +00001/*
2 * (C) Copyright 2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkb8fb6192004-08-02 21:11:11 +00006 */
7
8#include <common.h>
wdenkb8fb6192004-08-02 21:11:11 +00009#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020010#include <stdio_dev.h>
wdenkb8fb6192004-08-02 21:11:11 +000011#include <net.h>
12
Wolfgang Denk6405a152006-03-31 18:32:53 +020013DECLARE_GLOBAL_DATA_PTR;
14
Joe Hershberger45f4a232012-07-31 06:09:17 +000015#ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
16#define CONFIG_NETCONSOLE_BUFFER_SIZE 512
17#endif
18
19static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
Joe Hershberger107b3d02012-05-15 08:59:13 +000020static int input_size; /* char count in input buffer */
21static int input_offset; /* offset to valid chars in input buffer */
22static int input_recursion;
23static int output_recursion;
wdenkb8fb6192004-08-02 21:11:11 +000024static int net_timeout;
Joe Hershberger107b3d02012-05-15 08:59:13 +000025static uchar nc_ether[6]; /* server enet address */
Joe Hershberger5874dec2015-04-08 01:41:01 -050026static struct in_addr nc_ip; /* server ip */
Joe Hershbergerd0f53962012-07-31 06:06:41 +000027static short nc_out_port; /* target output port */
28static short nc_in_port; /* source input port */
Joe Hershberger107b3d02012-05-15 08:59:13 +000029static const char *output_packet; /* used by first send udp */
30static int output_packet_len;
Joe Hershberger9f374062012-08-03 10:59:08 +000031/*
32 * Start with a default last protocol.
33 * We are only interested in NETCONS or not.
34 */
35enum proto_t net_loop_last_protocol = BOOTP;
wdenkb8fb6192004-08-02 21:11:11 +000036
Luca Ceresoli428ab362011-04-18 06:19:50 +000037static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
Joe Hershberger5874dec2015-04-08 01:41:01 -050038 struct in_addr sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000039 unsigned len)
40{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000041 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000042}
43
Joe Hershberger5874dec2015-04-08 01:41:01 -050044static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
45 unsigned src, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +000046{
wdenka0ebde52004-09-08 22:03:11 +000047 if (input_size)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000048 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000049}
50
Joe Hershberger107b3d02012-05-15 08:59:13 +000051static void nc_timeout(void)
wdenkb8fb6192004-08-02 21:11:11 +000052{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000053 net_set_state(NETLOOP_SUCCESS);
wdenkb8fb6192004-08-02 21:11:11 +000054}
55
Joe Hershberger5874dec2015-04-08 01:41:01 -050056static int is_broadcast(struct in_addr ip)
Joe Hershberger66257802012-09-18 10:01:31 +000057{
Joe Hershberger5874dec2015-04-08 01:41:01 -050058 static struct in_addr netmask;
59 static struct in_addr our_ip;
Joe Hershberger66257802012-09-18 10:01:31 +000060 static int env_changed_id;
61 int env_id = get_env_id();
62
63 /* update only when the environment has changed */
64 if (env_changed_id != env_id) {
Joe Hershberger5874dec2015-04-08 01:41:01 -050065 netmask = getenv_ip("netmask");
66 our_ip = getenv_ip("ipaddr");
Joe Hershberger66257802012-09-18 10:01:31 +000067
68 env_changed_id = env_id;
69 }
70
Joe Hershberger5874dec2015-04-08 01:41:01 -050071 return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */
72 ((netmask.s_addr & our_ip.s_addr) ==
73 (netmask.s_addr & ip.s_addr) && /* on the same net and */
74 (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */
Joe Hershberger66257802012-09-18 10:01:31 +000075}
76
77static int refresh_settings_from_env(void)
78{
79 const char *p;
80 static int env_changed_id;
81 int env_id = get_env_id();
82
83 /* update only when the environment has changed */
84 if (env_changed_id != env_id) {
85 if (getenv("ncip")) {
Joe Hershberger5874dec2015-04-08 01:41:01 -050086 nc_ip = getenv_ip("ncip");
87 if (!nc_ip.s_addr)
Joe Hershberger66257802012-09-18 10:01:31 +000088 return -1; /* ncip is 0.0.0.0 */
89 p = strchr(getenv("ncip"), ':');
90 if (p != NULL) {
91 nc_out_port = simple_strtoul(p + 1, NULL, 10);
92 nc_in_port = nc_out_port;
93 }
94 } else
Joe Hershberger5874dec2015-04-08 01:41:01 -050095 nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
Joe Hershberger66257802012-09-18 10:01:31 +000096
97 p = getenv("ncoutport");
98 if (p != NULL)
99 nc_out_port = simple_strtoul(p, NULL, 10);
100 p = getenv("ncinport");
101 if (p != NULL)
102 nc_in_port = simple_strtoul(p, NULL, 10);
103
104 if (is_broadcast(nc_ip))
105 /* broadcast MAC address */
106 memset(nc_ether, 0xff, sizeof(nc_ether));
107 else
108 /* force arp request */
109 memset(nc_ether, 0, sizeof(nc_ether));
110 }
111 return 0;
112}
113
114/**
115 * Called from NetLoop in net/net.c before each packet
116 */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000117void NcStart(void)
wdenkb8fb6192004-08-02 21:11:11 +0000118{
Joe Hershberger66257802012-09-18 10:01:31 +0000119 refresh_settings_from_env();
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500120 if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +0000121 /* going to check for input packet */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000122 net_set_udp_handler(nc_handler);
Joe Hershberger107b3d02012-05-15 08:59:13 +0000123 NetSetTimeout(net_timeout, nc_timeout);
wdenkb8fb6192004-08-02 21:11:11 +0000124 } else {
125 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +0000126 uchar *pkt;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000127 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500128 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
129 IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000130 memcpy(pkt, output_packet, output_packet_len);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500131 net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
132 output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +0000133 }
134}
135
Joe Hershberger5874dec2015-04-08 01:41:01 -0500136int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
Joe Hershberger58cd2182012-09-18 10:01:32 +0000137 unsigned src_port, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +0000138{
wdenka0ebde52004-09-08 22:03:11 +0000139 int end, chunk;
140
Joe Hershberger58cd2182012-09-18 10:01:32 +0000141 if (dest_port != nc_in_port || !len)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000142 return 0; /* not for us */
wdenkb8fb6192004-08-02 21:11:11 +0000143
Joe Hershberger5874dec2015-04-08 01:41:01 -0500144 if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
Joe Hershberger58cd2182012-09-18 10:01:32 +0000145 return 0; /* not from our client */
146
Joe Hershberger05a377b2012-05-23 08:01:04 +0000147 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
148
Joe Hershberger107b3d02012-05-15 08:59:13 +0000149 if (input_size == sizeof(input_buffer))
150 return 1; /* no space */
151 if (len > sizeof(input_buffer) - input_size)
152 len = sizeof(input_buffer) - input_size;
wdenka0ebde52004-09-08 22:03:11 +0000153
154 end = input_offset + input_size;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000155 if (end > sizeof(input_buffer))
156 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000157
158 chunk = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000159 if (end + len > sizeof(input_buffer)) {
160 chunk = sizeof(input_buffer) - end;
wdenka0ebde52004-09-08 22:03:11 +0000161 memcpy(input_buffer, pkt + chunk, len - chunk);
162 }
Joe Hershberger107b3d02012-05-15 08:59:13 +0000163 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000164
165 input_size += len;
166
wdenkb8fb6192004-08-02 21:11:11 +0000167 return 1;
168}
169
Joe Hershberger107b3d02012-05-15 08:59:13 +0000170static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000171{
wdenkb8fb6192004-08-02 21:11:11 +0000172 struct eth_device *eth;
173 int inited = 0;
174 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000175 uchar *ether;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500176 struct in_addr ip;
wdenkb8fb6192004-08-02 21:11:11 +0000177
Joe Hershberger05a377b2012-05-23 08:01:04 +0000178 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
179
Joe Hershberger107b3d02012-05-15 08:59:13 +0000180 eth = eth_get_dev();
181 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000182 return;
183
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500184 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
wdenka0ebde52004-09-08 22:03:11 +0000185 if (eth->state == ETH_STATE_ACTIVE)
186 return; /* inside net loop */
187 output_packet = buf;
188 output_packet_len = len;
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700189 input_recursion = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000190 NetLoop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700191 input_recursion = 0;
wdenka0ebde52004-09-08 22:03:11 +0000192 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000193 return;
194 }
195
196 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000197 if (eth_is_on_demand_init()) {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500198 if (eth_init() < 0)
Joe Hershberger9f374062012-08-03 10:59:08 +0000199 return;
200 eth_set_last_protocol(NETCONS);
201 } else
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500202 eth_init_state_only();
Joe Hershberger9f374062012-08-03 10:59:08 +0000203
wdenkb8fb6192004-08-02 21:11:11 +0000204 inited = 1;
205 }
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500206 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000207 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000208 ether = nc_ether;
209 ip = nc_ip;
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500210 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000211
Joe Hershberger9f374062012-08-03 10:59:08 +0000212 if (inited) {
213 if (eth_is_on_demand_init())
214 eth_halt();
215 else
216 eth_halt_state_only();
217 }
wdenkb8fb6192004-08-02 21:11:11 +0000218}
219
Simon Glass0d1e1f72014-07-23 06:54:59 -0600220static int nc_start(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000221{
Joe Hershberger66257802012-09-18 10:01:31 +0000222 int retval;
wdenka0ebde52004-09-08 22:03:11 +0000223
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000224 nc_out_port = 6666; /* default port */
225 nc_in_port = nc_out_port;
wdenkb8fb6192004-08-02 21:11:11 +0000226
Joe Hershberger66257802012-09-18 10:01:31 +0000227 retval = refresh_settings_from_env();
228 if (retval != 0)
229 return retval;
wdenka0ebde52004-09-08 22:03:11 +0000230
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000231 /*
232 * Initialize the static IP settings and buffer pointers
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500233 * incase we call net_send_udp_packet before NetLoop
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000234 */
235 net_init();
236
wdenka0ebde52004-09-08 22:03:11 +0000237 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000238}
239
Simon Glass0d1e1f72014-07-23 06:54:59 -0600240static void nc_putc(struct stdio_dev *dev, char c)
wdenkb8fb6192004-08-02 21:11:11 +0000241{
242 if (output_recursion)
243 return;
244 output_recursion = 1;
245
Joe Hershberger107b3d02012-05-15 08:59:13 +0000246 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000247
248 output_recursion = 0;
249}
250
Simon Glass0d1e1f72014-07-23 06:54:59 -0600251static void nc_puts(struct stdio_dev *dev, const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000252{
wdenkb21608e2005-04-20 09:28:54 +0000253 int len;
254
wdenkb8fb6192004-08-02 21:11:11 +0000255 if (output_recursion)
256 return;
257 output_recursion = 1;
258
Michael Walle9227aed2011-10-07 12:27:50 +0000259 len = strlen(s);
260 while (len) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900261 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle9227aed2011-10-07 12:27:50 +0000262 nc_send_packet(s, send_len);
263 len -= send_len;
264 s += send_len;
265 }
wdenkb8fb6192004-08-02 21:11:11 +0000266
267 output_recursion = 0;
268}
269
Simon Glass0d1e1f72014-07-23 06:54:59 -0600270static int nc_getc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000271{
wdenkb21608e2005-04-20 09:28:54 +0000272 uchar c;
273
wdenkb8fb6192004-08-02 21:11:11 +0000274 input_recursion = 1;
275
276 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000277 while (!input_size)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000278 NetLoop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000279
280 input_recursion = 0;
281
wdenkb21608e2005-04-20 09:28:54 +0000282 c = input_buffer[input_offset++];
283
Joe Hershberger107b3d02012-05-15 08:59:13 +0000284 if (input_offset >= sizeof(input_buffer))
285 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000286 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000287
wdenka0ebde52004-09-08 22:03:11 +0000288 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000289}
290
Simon Glass0d1e1f72014-07-23 06:54:59 -0600291static int nc_tstc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000292{
293 struct eth_device *eth;
294
295 if (input_recursion)
296 return 0;
297
wdenka0ebde52004-09-08 22:03:11 +0000298 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000299 return 1;
300
Joe Hershberger107b3d02012-05-15 08:59:13 +0000301 eth = eth_get_dev();
wdenkb8fb6192004-08-02 21:11:11 +0000302 if (eth && eth->state == ETH_STATE_ACTIVE)
303 return 0; /* inside net loop */
304
305 input_recursion = 1;
306
307 net_timeout = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000308 NetLoop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000309
310 input_recursion = 0;
311
wdenka0ebde52004-09-08 22:03:11 +0000312 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000313}
314
Joe Hershberger107b3d02012-05-15 08:59:13 +0000315int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000316{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200317 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000318 int rc;
319
Joe Hershberger107b3d02012-05-15 08:59:13 +0000320 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000321
Joe Hershberger107b3d02012-05-15 08:59:13 +0000322 strcpy(dev.name, "nc");
wdenkb8fb6192004-08-02 21:11:11 +0000323 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
324 dev.start = nc_start;
325 dev.putc = nc_putc;
326 dev.puts = nc_puts;
327 dev.getc = nc_getc;
328 dev.tstc = nc_tstc;
329
Joe Hershberger107b3d02012-05-15 08:59:13 +0000330 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000331
332 return (rc == 0) ? 1 : rc;
333}