blob: f1d0630d020489055709476526d74d5d1f302b50 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkb8fb6192004-08-02 21:11:11 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkb8fb6192004-08-02 21:11:11 +00005 */
6
7#include <common.h>
wdenkb8fb6192004-08-02 21:11:11 +00008#include <command.h>
Simon Glass2d85a752019-08-01 09:46:41 -06009#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020011#include <stdio_dev.h>
wdenkb8fb6192004-08-02 21:11:11 +000012#include <net.h>
13
Joe Hershberger45f4a232012-07-31 06:09:17 +000014#ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
15#define CONFIG_NETCONSOLE_BUFFER_SIZE 512
16#endif
17
18static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
Joe Hershberger107b3d02012-05-15 08:59:13 +000019static int input_size; /* char count in input buffer */
20static int input_offset; /* offset to valid chars in input buffer */
21static int input_recursion;
22static int output_recursion;
wdenkb8fb6192004-08-02 21:11:11 +000023static int net_timeout;
Joe Hershberger107b3d02012-05-15 08:59:13 +000024static uchar nc_ether[6]; /* server enet address */
Joe Hershberger5874dec2015-04-08 01:41:01 -050025static struct in_addr nc_ip; /* server ip */
Joe Hershbergerd0f53962012-07-31 06:06:41 +000026static short nc_out_port; /* target output port */
27static short nc_in_port; /* source input port */
Joe Hershberger107b3d02012-05-15 08:59:13 +000028static const char *output_packet; /* used by first send udp */
29static int output_packet_len;
Joe Hershberger9f374062012-08-03 10:59:08 +000030/*
31 * Start with a default last protocol.
32 * We are only interested in NETCONS or not.
33 */
34enum proto_t net_loop_last_protocol = BOOTP;
wdenkb8fb6192004-08-02 21:11:11 +000035
Luca Ceresoli428ab362011-04-18 06:19:50 +000036static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
Joe Hershberger5874dec2015-04-08 01:41:01 -050037 struct in_addr sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000038 unsigned len)
39{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000040 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000041}
42
Joe Hershberger5874dec2015-04-08 01:41:01 -050043static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
44 unsigned src, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +000045{
wdenka0ebde52004-09-08 22:03:11 +000046 if (input_size)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000047 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenkb8fb6192004-08-02 21:11:11 +000048}
49
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -050050static void nc_timeout_handler(void)
wdenkb8fb6192004-08-02 21:11:11 +000051{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000052 net_set_state(NETLOOP_SUCCESS);
wdenkb8fb6192004-08-02 21:11:11 +000053}
54
Joe Hershberger5874dec2015-04-08 01:41:01 -050055static int is_broadcast(struct in_addr ip)
Joe Hershberger66257802012-09-18 10:01:31 +000056{
Joe Hershberger5874dec2015-04-08 01:41:01 -050057 static struct in_addr netmask;
58 static struct in_addr our_ip;
Joe Hershberger66257802012-09-18 10:01:31 +000059 static int env_changed_id;
Simon Glass2d85a752019-08-01 09:46:41 -060060 int env_id = env_get_id();
Joe Hershberger66257802012-09-18 10:01:31 +000061
62 /* update only when the environment has changed */
63 if (env_changed_id != env_id) {
Simon Glassda1a1342017-08-03 12:22:15 -060064 netmask = env_get_ip("netmask");
65 our_ip = env_get_ip("ipaddr");
Joe Hershberger66257802012-09-18 10:01:31 +000066
67 env_changed_id = env_id;
68 }
69
Joe Hershberger5874dec2015-04-08 01:41:01 -050070 return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */
71 ((netmask.s_addr & our_ip.s_addr) ==
72 (netmask.s_addr & ip.s_addr) && /* on the same net and */
73 (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */
Joe Hershberger66257802012-09-18 10:01:31 +000074}
75
76static int refresh_settings_from_env(void)
77{
78 const char *p;
79 static int env_changed_id;
Simon Glass2d85a752019-08-01 09:46:41 -060080 int env_id = env_get_id();
Joe Hershberger66257802012-09-18 10:01:31 +000081
82 /* update only when the environment has changed */
83 if (env_changed_id != env_id) {
Simon Glass64b723f2017-08-03 12:22:12 -060084 if (env_get("ncip")) {
Simon Glassda1a1342017-08-03 12:22:15 -060085 nc_ip = env_get_ip("ncip");
Joe Hershberger5874dec2015-04-08 01:41:01 -050086 if (!nc_ip.s_addr)
Joe Hershberger66257802012-09-18 10:01:31 +000087 return -1; /* ncip is 0.0.0.0 */
Simon Glass64b723f2017-08-03 12:22:12 -060088 p = strchr(env_get("ncip"), ':');
Joe Hershberger66257802012-09-18 10:01:31 +000089 if (p != NULL) {
90 nc_out_port = simple_strtoul(p + 1, NULL, 10);
91 nc_in_port = nc_out_port;
92 }
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -050093 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050094 nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -050095 }
Joe Hershberger66257802012-09-18 10:01:31 +000096
Simon Glass64b723f2017-08-03 12:22:12 -060097 p = env_get("ncoutport");
Joe Hershberger66257802012-09-18 10:01:31 +000098 if (p != NULL)
99 nc_out_port = simple_strtoul(p, NULL, 10);
Simon Glass64b723f2017-08-03 12:22:12 -0600100 p = env_get("ncinport");
Joe Hershberger66257802012-09-18 10:01:31 +0000101 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/**
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500115 * Called from net_loop in net/net.c before each packet
Joe Hershberger66257802012-09-18 10:01:31 +0000116 */
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500117void nc_start(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 Hershbergerc80b41b02015-04-08 01:41:21 -0500123 net_set_timeout_handler(net_timeout, nc_timeout_handler);
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 Hershberger83c48c52017-08-30 17:32:31 -0500155 if (end >= sizeof(input_buffer))
Joe Hershberger107b3d02012-05-15 08:59:13 +0000156 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000157
158 chunk = len;
Joe Hershberger83c48c52017-08-30 17:32:31 -0500159 /* Check if packet will wrap in input_buffer */
160 if (end + len >= sizeof(input_buffer)) {
Joe Hershberger107b3d02012-05-15 08:59:13 +0000161 chunk = sizeof(input_buffer) - end;
Joe Hershberger83c48c52017-08-30 17:32:31 -0500162 /* Copy the second part of the pkt to start of input_buffer */
wdenka0ebde52004-09-08 22:03:11 +0000163 memcpy(input_buffer, pkt + chunk, len - chunk);
164 }
Joe Hershberger83c48c52017-08-30 17:32:31 -0500165 /* Copy first (or only) part of pkt after end of current valid input*/
Joe Hershberger107b3d02012-05-15 08:59:13 +0000166 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000167
168 input_size += len;
169
wdenkb8fb6192004-08-02 21:11:11 +0000170 return 1;
171}
172
Joe Hershberger107b3d02012-05-15 08:59:13 +0000173static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000174{
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200175#ifdef CONFIG_DM_ETH
176 struct udevice *eth;
177#else
wdenkb8fb6192004-08-02 21:11:11 +0000178 struct eth_device *eth;
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200179#endif
wdenkb8fb6192004-08-02 21:11:11 +0000180 int inited = 0;
181 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000182 uchar *ether;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500183 struct in_addr ip;
wdenkb8fb6192004-08-02 21:11:11 +0000184
Joe Hershberger05a377b2012-05-23 08:01:04 +0000185 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
186
Joe Hershberger107b3d02012-05-15 08:59:13 +0000187 eth = eth_get_dev();
188 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000189 return;
190
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500191 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200192 if (eth_is_active(eth))
wdenka0ebde52004-09-08 22:03:11 +0000193 return; /* inside net loop */
194 output_packet = buf;
195 output_packet_len = len;
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700196 input_recursion = 1;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500197 net_loop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700198 input_recursion = 0;
wdenka0ebde52004-09-08 22:03:11 +0000199 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000200 return;
201 }
202
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200203 if (!eth_is_active(eth)) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000204 if (eth_is_on_demand_init()) {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500205 if (eth_init() < 0)
Joe Hershberger9f374062012-08-03 10:59:08 +0000206 return;
207 eth_set_last_protocol(NETCONS);
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500208 } else {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500209 eth_init_state_only();
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500210 }
Joe Hershberger9f374062012-08-03 10:59:08 +0000211
wdenkb8fb6192004-08-02 21:11:11 +0000212 inited = 1;
213 }
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500214 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + 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 Hershbergera8ca4f62015-04-08 01:41:05 -0500218 net_send_udp_packet(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
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500228static int nc_stdio_start(struct stdio_dev *dev)
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
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500241 * incase we call net_send_udp_packet before net_loop
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000242 */
243 net_init();
244
wdenka0ebde52004-09-08 22:03:11 +0000245 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000246}
247
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500248static void nc_stdio_putc(struct stdio_dev *dev, 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
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500259static void nc_stdio_puts(struct stdio_dev *dev, 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) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900269 int send_len = min(len, (int)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
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500278static int nc_stdio_getc(struct stdio_dev *dev)
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 Hershbergerc80b41b02015-04-08 01:41:21 -0500286 net_loop(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
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500299static int nc_stdio_tstc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000300{
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200301#ifdef CONFIG_DM_ETH
302 struct udevice *eth;
303#else
wdenkb8fb6192004-08-02 21:11:11 +0000304 struct eth_device *eth;
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200305#endif
wdenkb8fb6192004-08-02 21:11:11 +0000306
307 if (input_recursion)
308 return 0;
309
wdenka0ebde52004-09-08 22:03:11 +0000310 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000311 return 1;
312
Joe Hershberger107b3d02012-05-15 08:59:13 +0000313 eth = eth_get_dev();
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200314 if (eth_is_active(eth))
wdenkb8fb6192004-08-02 21:11:11 +0000315 return 0; /* inside net loop */
316
317 input_recursion = 1;
318
319 net_timeout = 1;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500320 net_loop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000321
322 input_recursion = 0;
323
wdenka0ebde52004-09-08 22:03:11 +0000324 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000325}
326
Joe Hershberger107b3d02012-05-15 08:59:13 +0000327int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000328{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200329 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000330 int rc;
331
Joe Hershberger107b3d02012-05-15 08:59:13 +0000332 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000333
Joe Hershberger107b3d02012-05-15 08:59:13 +0000334 strcpy(dev.name, "nc");
Bin Meng6abe4b62015-11-03 23:23:37 -0800335 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500336 dev.start = nc_stdio_start;
337 dev.putc = nc_stdio_putc;
338 dev.puts = nc_stdio_puts;
339 dev.getc = nc_stdio_getc;
340 dev.tstc = nc_stdio_tstc;
wdenkb8fb6192004-08-02 21:11:11 +0000341
Joe Hershberger107b3d02012-05-15 08:59:13 +0000342 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000343
344 return (rc == 0) ? 1 : rc;
345}