blob: cec96c571504b13128536be0d287135967e1779d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk68ceb292004-08-02 21:11:11 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk68ceb292004-08-02 21:11:11 +00005 */
6
7#include <common.h>
wdenk68ceb292004-08-02 21:11:11 +00008#include <command.h>
Simon Glassf1f0ae62019-08-01 09:46:41 -06009#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020011#include <stdio_dev.h>
wdenk68ceb292004-08-02 21:11:11 +000012#include <net.h>
13
Joe Hershberger2c8fe512012-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 Hershbergere1902ac2012-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;
wdenk68ceb292004-08-02 21:11:11 +000023static int net_timeout;
Joe Hershbergere1902ac2012-05-15 08:59:13 +000024static uchar nc_ether[6]; /* server enet address */
Joe Hershberger049a95a2015-04-08 01:41:01 -050025static struct in_addr nc_ip; /* server ip */
Joe Hershberger7f518982012-07-31 06:06:41 +000026static short nc_out_port; /* target output port */
27static short nc_in_port; /* source input port */
Joe Hershbergere1902ac2012-05-15 08:59:13 +000028static const char *output_packet; /* used by first send udp */
29static int output_packet_len;
Joe Hershbergerf8be7d62012-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;
wdenk68ceb292004-08-02 21:11:11 +000035
Luca Ceresoli03eb1292011-04-18 06:19:50 +000036static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
Joe Hershberger049a95a2015-04-08 01:41:01 -050037 struct in_addr sip, unsigned src,
wdenk68ceb292004-08-02 21:11:11 +000038 unsigned len)
39{
Joe Hershberger22f6e992012-05-23 07:59:14 +000040 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenk68ceb292004-08-02 21:11:11 +000041}
42
Joe Hershberger049a95a2015-04-08 01:41:01 -050043static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
44 unsigned src, unsigned len)
wdenk68ceb292004-08-02 21:11:11 +000045{
wdenkeedcd072004-09-08 22:03:11 +000046 if (input_size)
Joe Hershberger22f6e992012-05-23 07:59:14 +000047 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenk68ceb292004-08-02 21:11:11 +000048}
49
Joe Hershberger6a38a5f2015-04-08 01:41:16 -050050static void nc_timeout_handler(void)
wdenk68ceb292004-08-02 21:11:11 +000051{
Joe Hershberger22f6e992012-05-23 07:59:14 +000052 net_set_state(NETLOOP_SUCCESS);
wdenk68ceb292004-08-02 21:11:11 +000053}
54
Joe Hershberger049a95a2015-04-08 01:41:01 -050055static int is_broadcast(struct in_addr ip)
Joe Hershbergere827fec2012-09-18 10:01:31 +000056{
Joe Hershberger049a95a2015-04-08 01:41:01 -050057 static struct in_addr netmask;
58 static struct in_addr our_ip;
Joe Hershbergere827fec2012-09-18 10:01:31 +000059 static int env_changed_id;
Simon Glassf1f0ae62019-08-01 09:46:41 -060060 int env_id = env_get_id();
Joe Hershbergere827fec2012-09-18 10:01:31 +000061
62 /* update only when the environment has changed */
63 if (env_changed_id != env_id) {
Simon Glass723806c2017-08-03 12:22:15 -060064 netmask = env_get_ip("netmask");
65 our_ip = env_get_ip("ipaddr");
Joe Hershbergere827fec2012-09-18 10:01:31 +000066
67 env_changed_id = env_id;
68 }
69
Joe Hershberger049a95a2015-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 Hershbergere827fec2012-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 Glassf1f0ae62019-08-01 09:46:41 -060080 int env_id = env_get_id();
Joe Hershbergere827fec2012-09-18 10:01:31 +000081
82 /* update only when the environment has changed */
83 if (env_changed_id != env_id) {
Simon Glass00caae62017-08-03 12:22:12 -060084 if (env_get("ncip")) {
Simon Glass723806c2017-08-03 12:22:15 -060085 nc_ip = env_get_ip("ncip");
Joe Hershberger049a95a2015-04-08 01:41:01 -050086 if (!nc_ip.s_addr)
Joe Hershbergere827fec2012-09-18 10:01:31 +000087 return -1; /* ncip is 0.0.0.0 */
Simon Glass00caae62017-08-03 12:22:12 -060088 p = strchr(env_get("ncip"), ':');
Joe Hershbergere827fec2012-09-18 10:01:31 +000089 if (p != NULL) {
Simon Glass0b1284e2021-07-24 09:03:30 -060090 nc_out_port = dectoul(p + 1, NULL);
Joe Hershbergere827fec2012-09-18 10:01:31 +000091 nc_in_port = nc_out_port;
92 }
Joe Hershberger6a38a5f2015-04-08 01:41:16 -050093 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050094 nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
Joe Hershberger6a38a5f2015-04-08 01:41:16 -050095 }
Joe Hershbergere827fec2012-09-18 10:01:31 +000096
Simon Glass00caae62017-08-03 12:22:12 -060097 p = env_get("ncoutport");
Joe Hershbergere827fec2012-09-18 10:01:31 +000098 if (p != NULL)
Simon Glass0b1284e2021-07-24 09:03:30 -060099 nc_out_port = dectoul(p, NULL);
Simon Glass00caae62017-08-03 12:22:12 -0600100 p = env_get("ncinport");
Joe Hershbergere827fec2012-09-18 10:01:31 +0000101 if (p != NULL)
Simon Glass0b1284e2021-07-24 09:03:30 -0600102 nc_in_port = dectoul(p, NULL);
Joe Hershbergere827fec2012-09-18 10:01:31 +0000103
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 Hershbergerbc0571f2015-04-08 01:41:21 -0500115 * Called from net_loop in net/net.c before each packet
Joe Hershbergere827fec2012-09-18 10:01:31 +0000116 */
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500117void nc_start(void)
wdenk68ceb292004-08-02 21:11:11 +0000118{
Joe Hershbergere827fec2012-09-18 10:01:31 +0000119 refresh_settings_from_env();
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500120 if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
wdenk68ceb292004-08-02 21:11:11 +0000121 /* going to check for input packet */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000122 net_set_udp_handler(nc_handler);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500123 net_set_timeout_handler(net_timeout, nc_timeout_handler);
wdenk68ceb292004-08-02 21:11:11 +0000124 } else {
125 /* send arp request */
wdenkeedcd072004-09-08 22:03:11 +0000126 uchar *pkt;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000127 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500128 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
129 IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000130 memcpy(pkt, output_packet, output_packet_len);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500131 net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
132 output_packet_len);
wdenk68ceb292004-08-02 21:11:11 +0000133 }
134}
135
Joe Hershberger049a95a2015-04-08 01:41:01 -0500136int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
Joe Hershberger8a0eccb2012-09-18 10:01:32 +0000137 unsigned src_port, unsigned len)
wdenk68ceb292004-08-02 21:11:11 +0000138{
wdenkeedcd072004-09-08 22:03:11 +0000139 int end, chunk;
140
Joe Hershberger8a0eccb2012-09-18 10:01:32 +0000141 if (dest_port != nc_in_port || !len)
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000142 return 0; /* not for us */
wdenk68ceb292004-08-02 21:11:11 +0000143
Joe Hershberger049a95a2015-04-08 01:41:01 -0500144 if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
Joe Hershberger8a0eccb2012-09-18 10:01:32 +0000145 return 0; /* not from our client */
146
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000147 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
148
Joe Hershbergere1902ac2012-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;
wdenkeedcd072004-09-08 22:03:11 +0000153
154 end = input_offset + input_size;
Joe Hershberger3cacc6a2017-08-30 17:32:31 -0500155 if (end >= sizeof(input_buffer))
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000156 end -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +0000157
158 chunk = len;
Joe Hershberger3cacc6a2017-08-30 17:32:31 -0500159 /* Check if packet will wrap in input_buffer */
160 if (end + len >= sizeof(input_buffer)) {
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000161 chunk = sizeof(input_buffer) - end;
Joe Hershberger3cacc6a2017-08-30 17:32:31 -0500162 /* Copy the second part of the pkt to start of input_buffer */
wdenkeedcd072004-09-08 22:03:11 +0000163 memcpy(input_buffer, pkt + chunk, len - chunk);
164 }
Joe Hershberger3cacc6a2017-08-30 17:32:31 -0500165 /* Copy first (or only) part of pkt after end of current valid input*/
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000166 memcpy(input_buffer + end, pkt, chunk);
wdenkeedcd072004-09-08 22:03:11 +0000167
168 input_size += len;
169
wdenk68ceb292004-08-02 21:11:11 +0000170 return 1;
171}
172
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000173static void nc_send_packet(const char *buf, int len)
wdenk68ceb292004-08-02 21:11:11 +0000174{
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200175#ifdef CONFIG_DM_ETH
176 struct udevice *eth;
177#else
wdenk68ceb292004-08-02 21:11:11 +0000178 struct eth_device *eth;
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200179#endif
wdenk68ceb292004-08-02 21:11:11 +0000180 int inited = 0;
181 uchar *pkt;
wdenkeedcd072004-09-08 22:03:11 +0000182 uchar *ether;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500183 struct in_addr ip;
wdenk68ceb292004-08-02 21:11:11 +0000184
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000185 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
186
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000187 eth = eth_get_dev();
188 if (eth == NULL)
wdenk68ceb292004-08-02 21:11:11 +0000189 return;
wdenk68ceb292004-08-02 21:11:11 +0000190
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500191 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200192 if (eth_is_active(eth))
wdenkeedcd072004-09-08 22:03:11 +0000193 return; /* inside net loop */
194 output_packet = buf;
195 output_packet_len = len;
Suriyan Ramasamiefd9bb92013-10-16 09:54:24 -0700196 input_recursion = 1;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500197 net_loop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasamiefd9bb92013-10-16 09:54:24 -0700198 input_recursion = 0;
wdenkeedcd072004-09-08 22:03:11 +0000199 output_packet_len = 0;
200 return;
201 }
202
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200203 if (!eth_is_active(eth)) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000204 if (eth_is_on_demand_init()) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500205 if (eth_init() < 0)
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000206 return;
207 eth_set_last_protocol(NETCONS);
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500208 } else {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500209 eth_init_state_only();
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500210 }
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000211
wdenk68ceb292004-08-02 21:11:11 +0000212 inited = 1;
213 }
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500214 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000215 memcpy(pkt, buf, len);
wdenkeedcd072004-09-08 22:03:11 +0000216 ether = nc_ether;
217 ip = nc_ip;
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500218 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
wdenk68ceb292004-08-02 21:11:11 +0000219
Joe Hershbergerf8be7d62012-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 }
wdenk68ceb292004-08-02 21:11:11 +0000226}
227
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500228static int nc_stdio_start(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000229{
Joe Hershbergere827fec2012-09-18 10:01:31 +0000230 int retval;
wdenk68ceb292004-08-02 21:11:11 +0000231
Joe Hershberger7f518982012-07-31 06:06:41 +0000232 nc_out_port = 6666; /* default port */
233 nc_in_port = nc_out_port;
wdenkeedcd072004-09-08 22:03:11 +0000234
Joe Hershbergere827fec2012-09-18 10:01:31 +0000235 retval = refresh_settings_from_env();
236 if (retval != 0)
237 return retval;
wdenkeedcd072004-09-08 22:03:11 +0000238
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000239 /*
240 * Initialize the static IP settings and buffer pointers
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500241 * incase we call net_send_udp_packet before net_loop
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000242 */
243 net_init();
244
wdenkeedcd072004-09-08 22:03:11 +0000245 return 0;
wdenk68ceb292004-08-02 21:11:11 +0000246}
247
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500248static void nc_stdio_putc(struct stdio_dev *dev, char c)
wdenk68ceb292004-08-02 21:11:11 +0000249{
250 if (output_recursion)
251 return;
252 output_recursion = 1;
253
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000254 nc_send_packet(&c, 1);
wdenk68ceb292004-08-02 21:11:11 +0000255
256 output_recursion = 0;
257}
258
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500259static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
wdenk68ceb292004-08-02 21:11:11 +0000260{
wdenkb2323ea2005-04-20 09:28:54 +0000261 int len;
262
wdenk68ceb292004-08-02 21:11:11 +0000263 if (output_recursion)
264 return;
265 output_recursion = 1;
266
Michael Walle1a9845b2011-10-07 12:27:50 +0000267 len = strlen(s);
268 while (len) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900269 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle1a9845b2011-10-07 12:27:50 +0000270 nc_send_packet(s, send_len);
271 len -= send_len;
272 s += send_len;
273 }
wdenk68ceb292004-08-02 21:11:11 +0000274
275 output_recursion = 0;
276}
277
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500278static int nc_stdio_getc(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000279{
wdenkb2323ea2005-04-20 09:28:54 +0000280 uchar c;
281
wdenk68ceb292004-08-02 21:11:11 +0000282 input_recursion = 1;
283
284 net_timeout = 0; /* no timeout */
wdenkeedcd072004-09-08 22:03:11 +0000285 while (!input_size)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500286 net_loop(NETCONS);
wdenk68ceb292004-08-02 21:11:11 +0000287
288 input_recursion = 0;
289
wdenkb2323ea2005-04-20 09:28:54 +0000290 c = input_buffer[input_offset++];
291
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000292 if (input_offset >= sizeof(input_buffer))
293 input_offset -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +0000294 input_size--;
wdenk68ceb292004-08-02 21:11:11 +0000295
wdenkeedcd072004-09-08 22:03:11 +0000296 return c;
wdenk68ceb292004-08-02 21:11:11 +0000297}
298
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500299static int nc_stdio_tstc(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000300{
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200301#ifdef CONFIG_DM_ETH
302 struct udevice *eth;
303#else
wdenk68ceb292004-08-02 21:11:11 +0000304 struct eth_device *eth;
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200305#endif
wdenk68ceb292004-08-02 21:11:11 +0000306
307 if (input_recursion)
308 return 0;
309
wdenkeedcd072004-09-08 22:03:11 +0000310 if (input_size)
wdenk68ceb292004-08-02 21:11:11 +0000311 return 1;
312
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000313 eth = eth_get_dev();
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200314 if (eth_is_active(eth))
wdenk68ceb292004-08-02 21:11:11 +0000315 return 0; /* inside net loop */
316
317 input_recursion = 1;
318
319 net_timeout = 1;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500320 net_loop(NETCONS); /* kind of poll */
wdenk68ceb292004-08-02 21:11:11 +0000321
322 input_recursion = 0;
323
wdenkeedcd072004-09-08 22:03:11 +0000324 return input_size != 0;
wdenk68ceb292004-08-02 21:11:11 +0000325}
326
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000327int drv_nc_init(void)
wdenk68ceb292004-08-02 21:11:11 +0000328{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200329 struct stdio_dev dev;
wdenk68ceb292004-08-02 21:11:11 +0000330 int rc;
331
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000332 memset(&dev, 0, sizeof(dev));
wdenk68ceb292004-08-02 21:11:11 +0000333
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000334 strcpy(dev.name, "nc");
Bin Meng1caf9342015-11-03 23:23:37 -0800335 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Joe Hershberger6a38a5f2015-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;
wdenk68ceb292004-08-02 21:11:11 +0000341
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000342 rc = stdio_register(&dev);
wdenk68ceb292004-08-02 21:11:11 +0000343
344 return (rc == 0) ? 1 : rc;
345}