blob: 31042a6b6ba76566a0c74bd99918b710e8f516af [file] [log] [blame]
wdenk68ceb292004-08-02 21:11:11 +00001/*
2 * (C) Copyright 2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk68ceb292004-08-02 21:11:11 +00006 */
7
8#include <common.h>
wdenk68ceb292004-08-02 21:11:11 +00009#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020010#include <stdio_dev.h>
wdenk68ceb292004-08-02 21:11:11 +000011#include <net.h>
12
Wolfgang Denkd87080b2006-03-31 18:32:53 +020013DECLARE_GLOBAL_DATA_PTR;
14
Joe Hershberger2c8fe512012-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 Hershbergere1902ac2012-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;
wdenk68ceb292004-08-02 21:11:11 +000024static int net_timeout;
Joe Hershbergere1902ac2012-05-15 08:59:13 +000025static uchar nc_ether[6]; /* server enet address */
Joe Hershberger049a95a2015-04-08 01:41:01 -050026static struct in_addr nc_ip; /* server ip */
Joe Hershberger7f518982012-07-31 06:06:41 +000027static short nc_out_port; /* target output port */
28static short nc_in_port; /* source input port */
Joe Hershbergere1902ac2012-05-15 08:59:13 +000029static const char *output_packet; /* used by first send udp */
30static int output_packet_len;
Joe Hershbergerf8be7d62012-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;
wdenk68ceb292004-08-02 21:11:11 +000036
Luca Ceresoli03eb1292011-04-18 06:19:50 +000037static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
Joe Hershberger049a95a2015-04-08 01:41:01 -050038 struct in_addr sip, unsigned src,
wdenk68ceb292004-08-02 21:11:11 +000039 unsigned len)
40{
Joe Hershberger22f6e992012-05-23 07:59:14 +000041 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
wdenk68ceb292004-08-02 21:11:11 +000042}
43
Joe Hershberger049a95a2015-04-08 01:41:01 -050044static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
45 unsigned src, unsigned len)
wdenk68ceb292004-08-02 21:11:11 +000046{
wdenkeedcd072004-09-08 22:03:11 +000047 if (input_size)
Joe Hershberger22f6e992012-05-23 07:59:14 +000048 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
wdenk68ceb292004-08-02 21:11:11 +000049}
50
Joe Hershberger6a38a5f2015-04-08 01:41:16 -050051static void nc_timeout_handler(void)
wdenk68ceb292004-08-02 21:11:11 +000052{
Joe Hershberger22f6e992012-05-23 07:59:14 +000053 net_set_state(NETLOOP_SUCCESS);
wdenk68ceb292004-08-02 21:11:11 +000054}
55
Joe Hershberger049a95a2015-04-08 01:41:01 -050056static int is_broadcast(struct in_addr ip)
Joe Hershbergere827fec2012-09-18 10:01:31 +000057{
Joe Hershberger049a95a2015-04-08 01:41:01 -050058 static struct in_addr netmask;
59 static struct in_addr our_ip;
Joe Hershbergere827fec2012-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) {
Joe Hershberger049a95a2015-04-08 01:41:01 -050065 netmask = getenv_ip("netmask");
66 our_ip = getenv_ip("ipaddr");
Joe Hershbergere827fec2012-09-18 10:01:31 +000067
68 env_changed_id = env_id;
69 }
70
Joe Hershberger049a95a2015-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 Hershbergere827fec2012-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) {
85 if (getenv("ncip")) {
Joe Hershberger049a95a2015-04-08 01:41:01 -050086 nc_ip = getenv_ip("ncip");
87 if (!nc_ip.s_addr)
Joe Hershbergere827fec2012-09-18 10:01:31 +000088 return -1; /* ncip is 0.0.0.0 */
89 p = strchr(getenv("ncip"), ':');
90 if (p != NULL) {
91 nc_out_port = simple_strtoul(p + 1, NULL, 10);
92 nc_in_port = nc_out_port;
93 }
Joe Hershberger6a38a5f2015-04-08 01:41:16 -050094 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050095 nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
Joe Hershberger6a38a5f2015-04-08 01:41:16 -050096 }
Joe Hershbergere827fec2012-09-18 10:01:31 +000097
98 p = getenv("ncoutport");
99 if (p != NULL)
100 nc_out_port = simple_strtoul(p, NULL, 10);
101 p = getenv("ncinport");
102 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 Hershbergerbc0571f2015-04-08 01:41:21 -0500116 * Called from net_loop in net/net.c before each packet
Joe Hershbergere827fec2012-09-18 10:01:31 +0000117 */
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500118void nc_start(void)
wdenk68ceb292004-08-02 21:11:11 +0000119{
Joe Hershbergere827fec2012-09-18 10:01:31 +0000120 refresh_settings_from_env();
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500121 if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
wdenk68ceb292004-08-02 21:11:11 +0000122 /* going to check for input packet */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000123 net_set_udp_handler(nc_handler);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500124 net_set_timeout_handler(net_timeout, nc_timeout_handler);
wdenk68ceb292004-08-02 21:11:11 +0000125 } else {
126 /* send arp request */
wdenkeedcd072004-09-08 22:03:11 +0000127 uchar *pkt;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000128 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500129 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
130 IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000131 memcpy(pkt, output_packet, output_packet_len);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500132 net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
133 output_packet_len);
wdenk68ceb292004-08-02 21:11:11 +0000134 }
135}
136
Joe Hershberger049a95a2015-04-08 01:41:01 -0500137int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
Joe Hershberger8a0eccb2012-09-18 10:01:32 +0000138 unsigned src_port, unsigned len)
wdenk68ceb292004-08-02 21:11:11 +0000139{
wdenkeedcd072004-09-08 22:03:11 +0000140 int end, chunk;
141
Joe Hershberger8a0eccb2012-09-18 10:01:32 +0000142 if (dest_port != nc_in_port || !len)
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000143 return 0; /* not for us */
wdenk68ceb292004-08-02 21:11:11 +0000144
Joe Hershberger049a95a2015-04-08 01:41:01 -0500145 if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
Joe Hershberger8a0eccb2012-09-18 10:01:32 +0000146 return 0; /* not from our client */
147
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000148 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
149
Joe Hershbergere1902ac2012-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;
wdenkeedcd072004-09-08 22:03:11 +0000154
155 end = input_offset + input_size;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000156 if (end > sizeof(input_buffer))
157 end -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +0000158
159 chunk = len;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000160 if (end + len > sizeof(input_buffer)) {
161 chunk = sizeof(input_buffer) - end;
wdenkeedcd072004-09-08 22:03:11 +0000162 memcpy(input_buffer, pkt + chunk, len - chunk);
163 }
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000164 memcpy(input_buffer + end, pkt, chunk);
wdenkeedcd072004-09-08 22:03:11 +0000165
166 input_size += len;
167
wdenk68ceb292004-08-02 21:11:11 +0000168 return 1;
169}
170
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000171static void nc_send_packet(const char *buf, int len)
wdenk68ceb292004-08-02 21:11:11 +0000172{
wdenk68ceb292004-08-02 21:11:11 +0000173 struct eth_device *eth;
174 int inited = 0;
175 uchar *pkt;
wdenkeedcd072004-09-08 22:03:11 +0000176 uchar *ether;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500177 struct in_addr ip;
wdenk68ceb292004-08-02 21:11:11 +0000178
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000179 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
180
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000181 eth = eth_get_dev();
182 if (eth == NULL)
wdenk68ceb292004-08-02 21:11:11 +0000183 return;
wdenk68ceb292004-08-02 21:11:11 +0000184
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500185 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
wdenkeedcd072004-09-08 22:03:11 +0000186 if (eth->state == ETH_STATE_ACTIVE)
187 return; /* inside net loop */
188 output_packet = buf;
189 output_packet_len = len;
Suriyan Ramasamiefd9bb92013-10-16 09:54:24 -0700190 input_recursion = 1;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500191 net_loop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasamiefd9bb92013-10-16 09:54:24 -0700192 input_recursion = 0;
wdenkeedcd072004-09-08 22:03:11 +0000193 output_packet_len = 0;
194 return;
195 }
196
wdenk68ceb292004-08-02 21:11:11 +0000197 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000198 if (eth_is_on_demand_init()) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500199 if (eth_init() < 0)
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000200 return;
201 eth_set_last_protocol(NETCONS);
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500202 } else {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500203 eth_init_state_only();
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500204 }
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000205
wdenk68ceb292004-08-02 21:11:11 +0000206 inited = 1;
207 }
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500208 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000209 memcpy(pkt, buf, len);
wdenkeedcd072004-09-08 22:03:11 +0000210 ether = nc_ether;
211 ip = nc_ip;
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500212 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
wdenk68ceb292004-08-02 21:11:11 +0000213
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000214 if (inited) {
215 if (eth_is_on_demand_init())
216 eth_halt();
217 else
218 eth_halt_state_only();
219 }
wdenk68ceb292004-08-02 21:11:11 +0000220}
221
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500222static int nc_stdio_start(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000223{
Joe Hershbergere827fec2012-09-18 10:01:31 +0000224 int retval;
wdenk68ceb292004-08-02 21:11:11 +0000225
Joe Hershberger7f518982012-07-31 06:06:41 +0000226 nc_out_port = 6666; /* default port */
227 nc_in_port = nc_out_port;
wdenkeedcd072004-09-08 22:03:11 +0000228
Joe Hershbergere827fec2012-09-18 10:01:31 +0000229 retval = refresh_settings_from_env();
230 if (retval != 0)
231 return retval;
wdenkeedcd072004-09-08 22:03:11 +0000232
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000233 /*
234 * Initialize the static IP settings and buffer pointers
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500235 * incase we call net_send_udp_packet before net_loop
Joe Hershbergerd7310c72012-05-23 07:59:23 +0000236 */
237 net_init();
238
wdenkeedcd072004-09-08 22:03:11 +0000239 return 0;
wdenk68ceb292004-08-02 21:11:11 +0000240}
241
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500242static void nc_stdio_putc(struct stdio_dev *dev, char c)
wdenk68ceb292004-08-02 21:11:11 +0000243{
244 if (output_recursion)
245 return;
246 output_recursion = 1;
247
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000248 nc_send_packet(&c, 1);
wdenk68ceb292004-08-02 21:11:11 +0000249
250 output_recursion = 0;
251}
252
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500253static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
wdenk68ceb292004-08-02 21:11:11 +0000254{
wdenkb2323ea2005-04-20 09:28:54 +0000255 int len;
256
wdenk68ceb292004-08-02 21:11:11 +0000257 if (output_recursion)
258 return;
259 output_recursion = 1;
260
Michael Walle1a9845b2011-10-07 12:27:50 +0000261 len = strlen(s);
262 while (len) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900263 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle1a9845b2011-10-07 12:27:50 +0000264 nc_send_packet(s, send_len);
265 len -= send_len;
266 s += send_len;
267 }
wdenk68ceb292004-08-02 21:11:11 +0000268
269 output_recursion = 0;
270}
271
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500272static int nc_stdio_getc(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000273{
wdenkb2323ea2005-04-20 09:28:54 +0000274 uchar c;
275
wdenk68ceb292004-08-02 21:11:11 +0000276 input_recursion = 1;
277
278 net_timeout = 0; /* no timeout */
wdenkeedcd072004-09-08 22:03:11 +0000279 while (!input_size)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500280 net_loop(NETCONS);
wdenk68ceb292004-08-02 21:11:11 +0000281
282 input_recursion = 0;
283
wdenkb2323ea2005-04-20 09:28:54 +0000284 c = input_buffer[input_offset++];
285
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000286 if (input_offset >= sizeof(input_buffer))
287 input_offset -= sizeof(input_buffer);
wdenkeedcd072004-09-08 22:03:11 +0000288 input_size--;
wdenk68ceb292004-08-02 21:11:11 +0000289
wdenkeedcd072004-09-08 22:03:11 +0000290 return c;
wdenk68ceb292004-08-02 21:11:11 +0000291}
292
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500293static int nc_stdio_tstc(struct stdio_dev *dev)
wdenk68ceb292004-08-02 21:11:11 +0000294{
295 struct eth_device *eth;
296
297 if (input_recursion)
298 return 0;
299
wdenkeedcd072004-09-08 22:03:11 +0000300 if (input_size)
wdenk68ceb292004-08-02 21:11:11 +0000301 return 1;
302
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000303 eth = eth_get_dev();
wdenk68ceb292004-08-02 21:11:11 +0000304 if (eth && eth->state == ETH_STATE_ACTIVE)
305 return 0; /* inside net loop */
306
307 input_recursion = 1;
308
309 net_timeout = 1;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500310 net_loop(NETCONS); /* kind of poll */
wdenk68ceb292004-08-02 21:11:11 +0000311
312 input_recursion = 0;
313
wdenkeedcd072004-09-08 22:03:11 +0000314 return input_size != 0;
wdenk68ceb292004-08-02 21:11:11 +0000315}
316
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000317int drv_nc_init(void)
wdenk68ceb292004-08-02 21:11:11 +0000318{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200319 struct stdio_dev dev;
wdenk68ceb292004-08-02 21:11:11 +0000320 int rc;
321
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000322 memset(&dev, 0, sizeof(dev));
wdenk68ceb292004-08-02 21:11:11 +0000323
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000324 strcpy(dev.name, "nc");
wdenk68ceb292004-08-02 21:11:11 +0000325 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500326 dev.start = nc_stdio_start;
327 dev.putc = nc_stdio_putc;
328 dev.puts = nc_stdio_puts;
329 dev.getc = nc_stdio_getc;
330 dev.tstc = nc_stdio_tstc;
wdenk68ceb292004-08-02 21:11:11 +0000331
Joe Hershbergere1902ac2012-05-15 08:59:13 +0000332 rc = stdio_register(&dev);
wdenk68ceb292004-08-02 21:11:11 +0000333
334 return (rc == 0) ? 1 : rc;
335}