blob: 151bc55e076b7fd4dc5deae00429f27b2500e5dd [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
Tom Riniabb9a042024-05-18 20:20:43 -06007#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
Tom Rini364d0022023-01-10 11:19:45 -050014#ifndef CFG_NETCONSOLE_BUFFER_SIZE
15#define CFG_NETCONSOLE_BUFFER_SIZE 512
Joe Hershberger45f4a232012-07-31 06:09:17 +000016#endif
17
Tom Rini364d0022023-01-10 11:19:45 -050018static char input_buffer[CFG_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) {
Simon Glassff9b9032021-07-24 09:03:30 -060090 nc_out_port = dectoul(p + 1, NULL);
Joe Hershberger66257802012-09-18 10:01:31 +000091 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)
Simon Glassff9b9032021-07-24 09:03:30 -060099 nc_out_port = dectoul(p, NULL);
Simon Glass64b723f2017-08-03 12:22:12 -0600100 p = env_get("ncinport");
Joe Hershberger66257802012-09-18 10:01:31 +0000101 if (p != NULL)
Simon Glassff9b9032021-07-24 09:03:30 -0600102 nc_in_port = dectoul(p, NULL);
Joe Hershberger66257802012-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 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 struct udevice *eth;
wdenkb8fb6192004-08-02 21:11:11 +0000176 int inited = 0;
177 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000178 uchar *ether;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500179 struct in_addr ip;
wdenkb8fb6192004-08-02 21:11:11 +0000180
Joe Hershberger05a377b2012-05-23 08:01:04 +0000181 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
182
Joe Hershberger107b3d02012-05-15 08:59:13 +0000183 eth = eth_get_dev();
184 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000185 return;
186
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500187 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200188 if (eth_is_active(eth))
wdenka0ebde52004-09-08 22:03:11 +0000189 return; /* inside net loop */
190 output_packet = buf;
191 output_packet_len = len;
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700192 input_recursion = 1;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500193 net_loop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700194 input_recursion = 0;
wdenka0ebde52004-09-08 22:03:11 +0000195 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000196 return;
197 }
198
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200199 if (!eth_is_active(eth)) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000200 if (eth_is_on_demand_init()) {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500201 if (eth_init() < 0)
Joe Hershberger9f374062012-08-03 10:59:08 +0000202 return;
203 eth_set_last_protocol(NETCONS);
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500204 } else {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500205 eth_init_state_only();
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500206 }
Joe Hershberger9f374062012-08-03 10:59:08 +0000207
wdenkb8fb6192004-08-02 21:11:11 +0000208 inited = 1;
209 }
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500210 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000211 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000212 ether = nc_ether;
213 ip = nc_ip;
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500214 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000215
Joe Hershberger9f374062012-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 }
wdenkb8fb6192004-08-02 21:11:11 +0000222}
223
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500224static int nc_stdio_start(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000225{
Joe Hershberger66257802012-09-18 10:01:31 +0000226 int retval;
wdenka0ebde52004-09-08 22:03:11 +0000227
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000228 nc_out_port = 6666; /* default port */
229 nc_in_port = nc_out_port;
wdenkb8fb6192004-08-02 21:11:11 +0000230
Joe Hershberger66257802012-09-18 10:01:31 +0000231 retval = refresh_settings_from_env();
232 if (retval != 0)
233 return retval;
wdenka0ebde52004-09-08 22:03:11 +0000234
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000235 /*
236 * Initialize the static IP settings and buffer pointers
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500237 * incase we call net_send_udp_packet before net_loop
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000238 */
239 net_init();
240
wdenka0ebde52004-09-08 22:03:11 +0000241 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000242}
243
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500244static void nc_stdio_putc(struct stdio_dev *dev, char c)
wdenkb8fb6192004-08-02 21:11:11 +0000245{
246 if (output_recursion)
247 return;
248 output_recursion = 1;
249
Joe Hershberger107b3d02012-05-15 08:59:13 +0000250 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000251
252 output_recursion = 0;
253}
254
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500255static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000256{
wdenkb21608e2005-04-20 09:28:54 +0000257 int len;
258
wdenkb8fb6192004-08-02 21:11:11 +0000259 if (output_recursion)
260 return;
261 output_recursion = 1;
262
Michael Walle9227aed2011-10-07 12:27:50 +0000263 len = strlen(s);
264 while (len) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900265 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle9227aed2011-10-07 12:27:50 +0000266 nc_send_packet(s, send_len);
267 len -= send_len;
268 s += send_len;
269 }
wdenkb8fb6192004-08-02 21:11:11 +0000270
271 output_recursion = 0;
272}
273
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500274static int nc_stdio_getc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000275{
wdenkb21608e2005-04-20 09:28:54 +0000276 uchar c;
277
wdenkb8fb6192004-08-02 21:11:11 +0000278 input_recursion = 1;
279
280 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000281 while (!input_size)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500282 net_loop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000283
284 input_recursion = 0;
285
wdenkb21608e2005-04-20 09:28:54 +0000286 c = input_buffer[input_offset++];
287
Joe Hershberger107b3d02012-05-15 08:59:13 +0000288 if (input_offset >= sizeof(input_buffer))
289 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000290 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000291
wdenka0ebde52004-09-08 22:03:11 +0000292 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000293}
294
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500295static int nc_stdio_tstc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000296{
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200297 struct udevice *eth;
wdenkb8fb6192004-08-02 21:11:11 +0000298
299 if (input_recursion)
300 return 0;
301
wdenka0ebde52004-09-08 22:03:11 +0000302 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000303 return 1;
304
Joe Hershberger107b3d02012-05-15 08:59:13 +0000305 eth = eth_get_dev();
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200306 if (eth_is_active(eth))
wdenkb8fb6192004-08-02 21:11:11 +0000307 return 0; /* inside net loop */
308
309 input_recursion = 1;
310
311 net_timeout = 1;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500312 net_loop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000313
314 input_recursion = 0;
315
wdenka0ebde52004-09-08 22:03:11 +0000316 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000317}
318
Joe Hershberger107b3d02012-05-15 08:59:13 +0000319int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000320{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200321 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000322 int rc;
323
Joe Hershberger107b3d02012-05-15 08:59:13 +0000324 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000325
Joe Hershberger107b3d02012-05-15 08:59:13 +0000326 strcpy(dev.name, "nc");
Bin Meng6abe4b62015-11-03 23:23:37 -0800327 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Joe Hershbergerd02aa6b2015-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;
wdenkb8fb6192004-08-02 21:11:11 +0000333
Joe Hershberger107b3d02012-05-15 08:59:13 +0000334 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000335
336 return (rc == 0) ? 1 : rc;
337}