blob: 028fca966391c7d8b6839bdc653b0f40c7495522 [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 Hershbergerd02aa6b2015-04-08 01:41:16 -050051static void nc_timeout_handler(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) {
Simon Glassda1a1342017-08-03 12:22:15 -060065 netmask = env_get_ip("netmask");
66 our_ip = env_get_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) {
Simon Glass64b723f2017-08-03 12:22:12 -060085 if (env_get("ncip")) {
Simon Glassda1a1342017-08-03 12:22:15 -060086 nc_ip = env_get_ip("ncip");
Joe Hershberger5874dec2015-04-08 01:41:01 -050087 if (!nc_ip.s_addr)
Joe Hershberger66257802012-09-18 10:01:31 +000088 return -1; /* ncip is 0.0.0.0 */
Simon Glass64b723f2017-08-03 12:22:12 -060089 p = strchr(env_get("ncip"), ':');
Joe Hershberger66257802012-09-18 10:01:31 +000090 if (p != NULL) {
91 nc_out_port = simple_strtoul(p + 1, NULL, 10);
92 nc_in_port = nc_out_port;
93 }
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -050094 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050095 nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -050096 }
Joe Hershberger66257802012-09-18 10:01:31 +000097
Simon Glass64b723f2017-08-03 12:22:12 -060098 p = env_get("ncoutport");
Joe Hershberger66257802012-09-18 10:01:31 +000099 if (p != NULL)
100 nc_out_port = simple_strtoul(p, NULL, 10);
Simon Glass64b723f2017-08-03 12:22:12 -0600101 p = env_get("ncinport");
Joe Hershberger66257802012-09-18 10:01:31 +0000102 if (p != NULL)
103 nc_in_port = simple_strtoul(p, NULL, 10);
104
105 if (is_broadcast(nc_ip))
106 /* broadcast MAC address */
107 memset(nc_ether, 0xff, sizeof(nc_ether));
108 else
109 /* force arp request */
110 memset(nc_ether, 0, sizeof(nc_ether));
111 }
112 return 0;
113}
114
115/**
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500116 * Called from net_loop in net/net.c before each packet
Joe Hershberger66257802012-09-18 10:01:31 +0000117 */
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500118void nc_start(void)
wdenkb8fb6192004-08-02 21:11:11 +0000119{
Joe Hershberger66257802012-09-18 10:01:31 +0000120 refresh_settings_from_env();
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500121 if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +0000122 /* going to check for input packet */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000123 net_set_udp_handler(nc_handler);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500124 net_set_timeout_handler(net_timeout, nc_timeout_handler);
wdenkb8fb6192004-08-02 21:11:11 +0000125 } else {
126 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +0000127 uchar *pkt;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000128 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500129 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
130 IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000131 memcpy(pkt, output_packet, output_packet_len);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500132 net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
133 output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +0000134 }
135}
136
Joe Hershberger5874dec2015-04-08 01:41:01 -0500137int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
Joe Hershberger58cd2182012-09-18 10:01:32 +0000138 unsigned src_port, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +0000139{
wdenka0ebde52004-09-08 22:03:11 +0000140 int end, chunk;
141
Joe Hershberger58cd2182012-09-18 10:01:32 +0000142 if (dest_port != nc_in_port || !len)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000143 return 0; /* not for us */
wdenkb8fb6192004-08-02 21:11:11 +0000144
Joe Hershberger5874dec2015-04-08 01:41:01 -0500145 if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
Joe Hershberger58cd2182012-09-18 10:01:32 +0000146 return 0; /* not from our client */
147
Joe Hershberger05a377b2012-05-23 08:01:04 +0000148 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
149
Joe Hershberger107b3d02012-05-15 08:59:13 +0000150 if (input_size == sizeof(input_buffer))
151 return 1; /* no space */
152 if (len > sizeof(input_buffer) - input_size)
153 len = sizeof(input_buffer) - input_size;
wdenka0ebde52004-09-08 22:03:11 +0000154
155 end = input_offset + input_size;
Joe Hershberger83c48c52017-08-30 17:32:31 -0500156 if (end >= sizeof(input_buffer))
Joe Hershberger107b3d02012-05-15 08:59:13 +0000157 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000158
159 chunk = len;
Joe Hershberger83c48c52017-08-30 17:32:31 -0500160 /* Check if packet will wrap in input_buffer */
161 if (end + len >= sizeof(input_buffer)) {
Joe Hershberger107b3d02012-05-15 08:59:13 +0000162 chunk = sizeof(input_buffer) - end;
Joe Hershberger83c48c52017-08-30 17:32:31 -0500163 /* Copy the second part of the pkt to start of input_buffer */
wdenka0ebde52004-09-08 22:03:11 +0000164 memcpy(input_buffer, pkt + chunk, len - chunk);
165 }
Joe Hershberger83c48c52017-08-30 17:32:31 -0500166 /* Copy first (or only) part of pkt after end of current valid input*/
Joe Hershberger107b3d02012-05-15 08:59:13 +0000167 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000168
169 input_size += len;
170
wdenkb8fb6192004-08-02 21:11:11 +0000171 return 1;
172}
173
Joe Hershberger107b3d02012-05-15 08:59:13 +0000174static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000175{
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200176#ifdef CONFIG_DM_ETH
177 struct udevice *eth;
178#else
wdenkb8fb6192004-08-02 21:11:11 +0000179 struct eth_device *eth;
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200180#endif
wdenkb8fb6192004-08-02 21:11:11 +0000181 int inited = 0;
182 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000183 uchar *ether;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500184 struct in_addr ip;
wdenkb8fb6192004-08-02 21:11:11 +0000185
Joe Hershberger05a377b2012-05-23 08:01:04 +0000186 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
187
Joe Hershberger107b3d02012-05-15 08:59:13 +0000188 eth = eth_get_dev();
189 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000190 return;
191
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500192 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200193 if (eth_is_active(eth))
wdenka0ebde52004-09-08 22:03:11 +0000194 return; /* inside net loop */
195 output_packet = buf;
196 output_packet_len = len;
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700197 input_recursion = 1;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500198 net_loop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700199 input_recursion = 0;
wdenka0ebde52004-09-08 22:03:11 +0000200 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000201 return;
202 }
203
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200204 if (!eth_is_active(eth)) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000205 if (eth_is_on_demand_init()) {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500206 if (eth_init() < 0)
Joe Hershberger9f374062012-08-03 10:59:08 +0000207 return;
208 eth_set_last_protocol(NETCONS);
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500209 } else {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500210 eth_init_state_only();
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500211 }
Joe Hershberger9f374062012-08-03 10:59:08 +0000212
wdenkb8fb6192004-08-02 21:11:11 +0000213 inited = 1;
214 }
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500215 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000216 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000217 ether = nc_ether;
218 ip = nc_ip;
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500219 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000220
Joe Hershberger9f374062012-08-03 10:59:08 +0000221 if (inited) {
222 if (eth_is_on_demand_init())
223 eth_halt();
224 else
225 eth_halt_state_only();
226 }
wdenkb8fb6192004-08-02 21:11:11 +0000227}
228
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500229static int nc_stdio_start(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000230{
Joe Hershberger66257802012-09-18 10:01:31 +0000231 int retval;
wdenka0ebde52004-09-08 22:03:11 +0000232
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000233 nc_out_port = 6666; /* default port */
234 nc_in_port = nc_out_port;
wdenkb8fb6192004-08-02 21:11:11 +0000235
Joe Hershberger66257802012-09-18 10:01:31 +0000236 retval = refresh_settings_from_env();
237 if (retval != 0)
238 return retval;
wdenka0ebde52004-09-08 22:03:11 +0000239
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000240 /*
241 * Initialize the static IP settings and buffer pointers
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500242 * incase we call net_send_udp_packet before net_loop
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000243 */
244 net_init();
245
wdenka0ebde52004-09-08 22:03:11 +0000246 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000247}
248
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500249static void nc_stdio_putc(struct stdio_dev *dev, char c)
wdenkb8fb6192004-08-02 21:11:11 +0000250{
251 if (output_recursion)
252 return;
253 output_recursion = 1;
254
Joe Hershberger107b3d02012-05-15 08:59:13 +0000255 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000256
257 output_recursion = 0;
258}
259
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500260static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000261{
wdenkb21608e2005-04-20 09:28:54 +0000262 int len;
263
wdenkb8fb6192004-08-02 21:11:11 +0000264 if (output_recursion)
265 return;
266 output_recursion = 1;
267
Michael Walle9227aed2011-10-07 12:27:50 +0000268 len = strlen(s);
269 while (len) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900270 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle9227aed2011-10-07 12:27:50 +0000271 nc_send_packet(s, send_len);
272 len -= send_len;
273 s += send_len;
274 }
wdenkb8fb6192004-08-02 21:11:11 +0000275
276 output_recursion = 0;
277}
278
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500279static int nc_stdio_getc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000280{
wdenkb21608e2005-04-20 09:28:54 +0000281 uchar c;
282
wdenkb8fb6192004-08-02 21:11:11 +0000283 input_recursion = 1;
284
285 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000286 while (!input_size)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500287 net_loop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000288
289 input_recursion = 0;
290
wdenkb21608e2005-04-20 09:28:54 +0000291 c = input_buffer[input_offset++];
292
Joe Hershberger107b3d02012-05-15 08:59:13 +0000293 if (input_offset >= sizeof(input_buffer))
294 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000295 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000296
wdenka0ebde52004-09-08 22:03:11 +0000297 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000298}
299
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500300static int nc_stdio_tstc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000301{
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200302#ifdef CONFIG_DM_ETH
303 struct udevice *eth;
304#else
wdenkb8fb6192004-08-02 21:11:11 +0000305 struct eth_device *eth;
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200306#endif
wdenkb8fb6192004-08-02 21:11:11 +0000307
308 if (input_recursion)
309 return 0;
310
wdenka0ebde52004-09-08 22:03:11 +0000311 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000312 return 1;
313
Joe Hershberger107b3d02012-05-15 08:59:13 +0000314 eth = eth_get_dev();
Bernhard Nortmann752abb72015-09-14 15:29:44 +0200315 if (eth_is_active(eth))
wdenkb8fb6192004-08-02 21:11:11 +0000316 return 0; /* inside net loop */
317
318 input_recursion = 1;
319
320 net_timeout = 1;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500321 net_loop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000322
323 input_recursion = 0;
324
wdenka0ebde52004-09-08 22:03:11 +0000325 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000326}
327
Joe Hershberger107b3d02012-05-15 08:59:13 +0000328int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000329{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200330 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000331 int rc;
332
Joe Hershberger107b3d02012-05-15 08:59:13 +0000333 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000334
Joe Hershberger107b3d02012-05-15 08:59:13 +0000335 strcpy(dev.name, "nc");
Bin Meng6abe4b62015-11-03 23:23:37 -0800336 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500337 dev.start = nc_stdio_start;
338 dev.putc = nc_stdio_putc;
339 dev.puts = nc_stdio_puts;
340 dev.getc = nc_stdio_getc;
341 dev.tstc = nc_stdio_tstc;
wdenkb8fb6192004-08-02 21:11:11 +0000342
Joe Hershberger107b3d02012-05-15 08:59:13 +0000343 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000344
345 return (rc == 0) ? 1 : rc;
346}