blob: 1e52917ff2c3a9f0dc79dfa45948ae7933476e66 [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 struct udevice *eth;
wdenk68ceb292004-08-02 21:11:11 +0000176 int inited = 0;
177 uchar *pkt;
wdenkeedcd072004-09-08 22:03:11 +0000178 uchar *ether;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500179 struct in_addr ip;
wdenk68ceb292004-08-02 21:11:11 +0000180
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000181 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
182
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000183 eth = eth_get_dev();
184 if (eth == NULL)
wdenk68ceb292004-08-02 21:11:11 +0000185 return;
wdenk68ceb292004-08-02 21:11:11 +0000186
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500187 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200188 if (eth_is_active(eth))
wdenkeedcd072004-09-08 22:03:11 +0000189 return; /* inside net loop */
190 output_packet = buf;
191 output_packet_len = len;
Suriyan Ramasamiefd9bb92013-10-16 09:54:24 -0700192 input_recursion = 1;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500193 net_loop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasamiefd9bb92013-10-16 09:54:24 -0700194 input_recursion = 0;
wdenkeedcd072004-09-08 22:03:11 +0000195 output_packet_len = 0;
196 return;
197 }
198
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200199 if (!eth_is_active(eth)) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000200 if (eth_is_on_demand_init()) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500201 if (eth_init() < 0)
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000202 return;
203 eth_set_last_protocol(NETCONS);
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500204 } else {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500205 eth_init_state_only();
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500206 }
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000207
wdenk68ceb292004-08-02 21:11:11 +0000208 inited = 1;
209 }
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500210 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000211 memcpy(pkt, buf, len);
wdenkeedcd072004-09-08 22:03:11 +0000212 ether = nc_ether;
213 ip = nc_ip;
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500214 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
wdenk68ceb292004-08-02 21:11:11 +0000215
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000216 if (inited) {
217 if (eth_is_on_demand_init())
218 eth_halt();
219 else
220 eth_halt_state_only();
221 }
wdenk68ceb292004-08-02 21:11:11 +0000222}
223
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500224static int nc_stdio_start(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000225{
Joe Hershbergere827fec2012-09-18 10:01:31 +0000226 int retval;
wdenk68ceb292004-08-02 21:11:11 +0000227
Joe Hershberger7f518982012-07-31 06:06:41 +0000228 nc_out_port = 6666; /* default port */
229 nc_in_port = nc_out_port;
wdenkeedcd072004-09-08 22:03:11 +0000230
Joe Hershbergere827fec2012-09-18 10:01:31 +0000231 retval = refresh_settings_from_env();
232 if (retval != 0)
233 return retval;
wdenkeedcd072004-09-08 22:03:11 +0000234
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000235 /*
236 * Initialize the static IP settings and buffer pointers
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500237 * incase we call net_send_udp_packet before net_loop
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000238 */
239 net_init();
240
wdenkeedcd072004-09-08 22:03:11 +0000241 return 0;
wdenk68ceb292004-08-02 21:11:11 +0000242}
243
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500244static void nc_stdio_putc(struct stdio_dev *dev, char c)
wdenk68ceb292004-08-02 21:11:11 +0000245{
246 if (output_recursion)
247 return;
248 output_recursion = 1;
249
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000250 nc_send_packet(&c, 1);
wdenk68ceb292004-08-02 21:11:11 +0000251
252 output_recursion = 0;
253}
254
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500255static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
wdenk68ceb292004-08-02 21:11:11 +0000256{
wdenkb2323ea2005-04-20 09:28:54 +0000257 int len;
258
wdenk68ceb292004-08-02 21:11:11 +0000259 if (output_recursion)
260 return;
261 output_recursion = 1;
262
Michael Walle1a9845b2011-10-07 12:27:50 +0000263 len = strlen(s);
264 while (len) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900265 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle1a9845b2011-10-07 12:27:50 +0000266 nc_send_packet(s, send_len);
267 len -= send_len;
268 s += send_len;
269 }
wdenk68ceb292004-08-02 21:11:11 +0000270
271 output_recursion = 0;
272}
273
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500274static int nc_stdio_getc(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000275{
wdenkb2323ea2005-04-20 09:28:54 +0000276 uchar c;
277
wdenk68ceb292004-08-02 21:11:11 +0000278 input_recursion = 1;
279
280 net_timeout = 0; /* no timeout */
wdenkeedcd072004-09-08 22:03:11 +0000281 while (!input_size)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500282 net_loop(NETCONS);
wdenk68ceb292004-08-02 21:11:11 +0000283
284 input_recursion = 0;
285
wdenkb2323ea2005-04-20 09:28:54 +0000286 c = input_buffer[input_offset++];
287
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000288 if (input_offset >= sizeof(input_buffer))
289 input_offset -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +0000290 input_size--;
wdenk68ceb292004-08-02 21:11:11 +0000291
wdenkeedcd072004-09-08 22:03:11 +0000292 return c;
wdenk68ceb292004-08-02 21:11:11 +0000293}
294
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500295static int nc_stdio_tstc(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000296{
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200297 struct udevice *eth;
wdenk68ceb292004-08-02 21:11:11 +0000298
299 if (input_recursion)
300 return 0;
301
wdenkeedcd072004-09-08 22:03:11 +0000302 if (input_size)
wdenk68ceb292004-08-02 21:11:11 +0000303 return 1;
304
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000305 eth = eth_get_dev();
Bernhard Nortmannc163e432015-09-14 15:29:44 +0200306 if (eth_is_active(eth))
wdenk68ceb292004-08-02 21:11:11 +0000307 return 0; /* inside net loop */
308
309 input_recursion = 1;
310
311 net_timeout = 1;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500312 net_loop(NETCONS); /* kind of poll */
wdenk68ceb292004-08-02 21:11:11 +0000313
314 input_recursion = 0;
315
wdenkeedcd072004-09-08 22:03:11 +0000316 return input_size != 0;
wdenk68ceb292004-08-02 21:11:11 +0000317}
318
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000319int drv_nc_init(void)
wdenk68ceb292004-08-02 21:11:11 +0000320{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200321 struct stdio_dev dev;
wdenk68ceb292004-08-02 21:11:11 +0000322 int rc;
323
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000324 memset(&dev, 0, sizeof(dev));
wdenk68ceb292004-08-02 21:11:11 +0000325
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000326 strcpy(dev.name, "nc");
Bin Meng1caf9342015-11-03 23:23:37 -0800327 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500328 dev.start = nc_stdio_start;
329 dev.putc = nc_stdio_putc;
330 dev.puts = nc_stdio_puts;
331 dev.getc = nc_stdio_getc;
332 dev.tstc = nc_stdio_tstc;
wdenk68ceb292004-08-02 21:11:11 +0000333
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000334 rc = stdio_register(&dev);
wdenk68ceb292004-08-02 21:11:11 +0000335
336 return (rc == 0) ? 1 : rc;
337}