blob: 677c89f0486f2a19f334db02b45a58a443b3e2da [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 */
26static IPaddr_t 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,
38 IPaddr_t 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
Luca Ceresoli428ab362011-04-18 06:19:50 +000044static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000045 unsigned len)
46{
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 Hershberger107b3d02012-05-15 08:59:13 +000051static void nc_timeout(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 Hershberger66257802012-09-18 10:01:31 +000056static int is_broadcast(IPaddr_t ip)
57{
58 static IPaddr_t netmask;
59 static IPaddr_t our_ip;
60 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) {
65 netmask = getenv_IPaddr("netmask");
66 our_ip = getenv_IPaddr("ipaddr");
67
68 env_changed_id = env_id;
69 }
70
71 return (ip == ~0 || /* 255.255.255.255 */
72 ((netmask & our_ip) == (netmask & ip) && /* on the same net */
73 (netmask | ip) == ~0)); /* broadcast to our net */
74}
75
76static int refresh_settings_from_env(void)
77{
78 const char *p;
79 static int env_changed_id;
80 int env_id = get_env_id();
81
82 /* update only when the environment has changed */
83 if (env_changed_id != env_id) {
84 if (getenv("ncip")) {
85 nc_ip = getenv_IPaddr("ncip");
86 if (!nc_ip)
87 return -1; /* ncip is 0.0.0.0 */
88 p = strchr(getenv("ncip"), ':');
89 if (p != NULL) {
90 nc_out_port = simple_strtoul(p + 1, NULL, 10);
91 nc_in_port = nc_out_port;
92 }
93 } else
94 nc_ip = ~0; /* ncip is not set, so broadcast */
95
96 p = getenv("ncoutport");
97 if (p != NULL)
98 nc_out_port = simple_strtoul(p, NULL, 10);
99 p = getenv("ncinport");
100 if (p != NULL)
101 nc_in_port = simple_strtoul(p, NULL, 10);
102
103 if (is_broadcast(nc_ip))
104 /* broadcast MAC address */
105 memset(nc_ether, 0xff, sizeof(nc_ether));
106 else
107 /* force arp request */
108 memset(nc_ether, 0, sizeof(nc_ether));
109 }
110 return 0;
111}
112
113/**
114 * Called from NetLoop in net/net.c before each packet
115 */
Joe Hershberger107b3d02012-05-15 08:59:13 +0000116void NcStart(void)
wdenkb8fb6192004-08-02 21:11:11 +0000117{
Joe Hershberger66257802012-09-18 10:01:31 +0000118 refresh_settings_from_env();
Joe Hershberger107b3d02012-05-15 08:59:13 +0000119 if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +0000120 /* going to check for input packet */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000121 net_set_udp_handler(nc_handler);
Joe Hershberger107b3d02012-05-15 08:59:13 +0000122 NetSetTimeout(net_timeout, nc_timeout);
wdenkb8fb6192004-08-02 21:11:11 +0000123 } else {
124 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +0000125 uchar *pkt;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000126 net_set_arp_handler(nc_wait_arp_handler);
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000127 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000128 memcpy(pkt, output_packet, output_packet_len);
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000129 NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
Joe Hershberger107b3d02012-05-15 08:59:13 +0000130 output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +0000131 }
132}
133
Joe Hershberger58cd2182012-09-18 10:01:32 +0000134int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
135 unsigned src_port, unsigned len)
wdenkb8fb6192004-08-02 21:11:11 +0000136{
wdenka0ebde52004-09-08 22:03:11 +0000137 int end, chunk;
138
Joe Hershberger58cd2182012-09-18 10:01:32 +0000139 if (dest_port != nc_in_port || !len)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000140 return 0; /* not for us */
wdenkb8fb6192004-08-02 21:11:11 +0000141
Joe Hershberger58cd2182012-09-18 10:01:32 +0000142 if (src_ip != nc_ip && !is_broadcast(nc_ip))
143 return 0; /* not from our client */
144
Joe Hershberger05a377b2012-05-23 08:01:04 +0000145 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
146
Joe Hershberger107b3d02012-05-15 08:59:13 +0000147 if (input_size == sizeof(input_buffer))
148 return 1; /* no space */
149 if (len > sizeof(input_buffer) - input_size)
150 len = sizeof(input_buffer) - input_size;
wdenka0ebde52004-09-08 22:03:11 +0000151
152 end = input_offset + input_size;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000153 if (end > sizeof(input_buffer))
154 end -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000155
156 chunk = len;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000157 if (end + len > sizeof(input_buffer)) {
158 chunk = sizeof(input_buffer) - end;
wdenka0ebde52004-09-08 22:03:11 +0000159 memcpy(input_buffer, pkt + chunk, len - chunk);
160 }
Joe Hershberger107b3d02012-05-15 08:59:13 +0000161 memcpy(input_buffer + end, pkt, chunk);
wdenka0ebde52004-09-08 22:03:11 +0000162
163 input_size += len;
164
wdenkb8fb6192004-08-02 21:11:11 +0000165 return 1;
166}
167
Joe Hershberger107b3d02012-05-15 08:59:13 +0000168static void nc_send_packet(const char *buf, int len)
wdenkb8fb6192004-08-02 21:11:11 +0000169{
wdenkb8fb6192004-08-02 21:11:11 +0000170 struct eth_device *eth;
171 int inited = 0;
172 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000173 uchar *ether;
174 IPaddr_t ip;
wdenkb8fb6192004-08-02 21:11:11 +0000175
Joe Hershberger05a377b2012-05-23 08:01:04 +0000176 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
177
Joe Hershberger107b3d02012-05-15 08:59:13 +0000178 eth = eth_get_dev();
179 if (eth == NULL)
wdenkb8fb6192004-08-02 21:11:11 +0000180 return;
181
Joe Hershberger107b3d02012-05-15 08:59:13 +0000182 if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
wdenka0ebde52004-09-08 22:03:11 +0000183 if (eth->state == ETH_STATE_ACTIVE)
184 return; /* inside net loop */
185 output_packet = buf;
186 output_packet_len = len;
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700187 input_recursion = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000188 NetLoop(NETCONS); /* wait for arp reply and send packet */
Suriyan Ramasami7f37f222013-10-16 09:54:24 -0700189 input_recursion = 0;
wdenka0ebde52004-09-08 22:03:11 +0000190 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000191 return;
192 }
193
194 if (eth->state != ETH_STATE_ACTIVE) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000195 if (eth_is_on_demand_init()) {
196 if (eth_init(gd->bd) < 0)
197 return;
198 eth_set_last_protocol(NETCONS);
199 } else
200 eth_init_state_only(gd->bd);
201
wdenkb8fb6192004-08-02 21:11:11 +0000202 inited = 1;
203 }
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000204 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000205 memcpy(pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000206 ether = nc_ether;
207 ip = nc_ip;
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000208 NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000209
Joe Hershberger9f374062012-08-03 10:59:08 +0000210 if (inited) {
211 if (eth_is_on_demand_init())
212 eth_halt();
213 else
214 eth_halt_state_only();
215 }
wdenkb8fb6192004-08-02 21:11:11 +0000216}
217
Simon Glass0d1e1f72014-07-23 06:54:59 -0600218static int nc_start(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000219{
Joe Hershberger66257802012-09-18 10:01:31 +0000220 int retval;
wdenka0ebde52004-09-08 22:03:11 +0000221
Joe Hershbergerd0f53962012-07-31 06:06:41 +0000222 nc_out_port = 6666; /* default port */
223 nc_in_port = nc_out_port;
wdenkb8fb6192004-08-02 21:11:11 +0000224
Joe Hershberger66257802012-09-18 10:01:31 +0000225 retval = refresh_settings_from_env();
226 if (retval != 0)
227 return retval;
wdenka0ebde52004-09-08 22:03:11 +0000228
Joe Hershberger6a9ed162012-05-23 07:59:23 +0000229 /*
230 * Initialize the static IP settings and buffer pointers
231 * incase we call NetSendUDPPacket before NetLoop
232 */
233 net_init();
234
wdenka0ebde52004-09-08 22:03:11 +0000235 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000236}
237
Simon Glass0d1e1f72014-07-23 06:54:59 -0600238static void nc_putc(struct stdio_dev *dev, char c)
wdenkb8fb6192004-08-02 21:11:11 +0000239{
240 if (output_recursion)
241 return;
242 output_recursion = 1;
243
Joe Hershberger107b3d02012-05-15 08:59:13 +0000244 nc_send_packet(&c, 1);
wdenkb8fb6192004-08-02 21:11:11 +0000245
246 output_recursion = 0;
247}
248
Simon Glass0d1e1f72014-07-23 06:54:59 -0600249static void nc_puts(struct stdio_dev *dev, const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000250{
wdenkb21608e2005-04-20 09:28:54 +0000251 int len;
252
wdenkb8fb6192004-08-02 21:11:11 +0000253 if (output_recursion)
254 return;
255 output_recursion = 1;
256
Michael Walle9227aed2011-10-07 12:27:50 +0000257 len = strlen(s);
258 while (len) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900259 int send_len = min(len, (int)sizeof(input_buffer));
Michael Walle9227aed2011-10-07 12:27:50 +0000260 nc_send_packet(s, send_len);
261 len -= send_len;
262 s += send_len;
263 }
wdenkb8fb6192004-08-02 21:11:11 +0000264
265 output_recursion = 0;
266}
267
Simon Glass0d1e1f72014-07-23 06:54:59 -0600268static int nc_getc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000269{
wdenkb21608e2005-04-20 09:28:54 +0000270 uchar c;
271
wdenkb8fb6192004-08-02 21:11:11 +0000272 input_recursion = 1;
273
274 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000275 while (!input_size)
Joe Hershberger107b3d02012-05-15 08:59:13 +0000276 NetLoop(NETCONS);
wdenkb8fb6192004-08-02 21:11:11 +0000277
278 input_recursion = 0;
279
wdenkb21608e2005-04-20 09:28:54 +0000280 c = input_buffer[input_offset++];
281
Joe Hershberger107b3d02012-05-15 08:59:13 +0000282 if (input_offset >= sizeof(input_buffer))
283 input_offset -= sizeof(input_buffer);
wdenka0ebde52004-09-08 22:03:11 +0000284 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000285
wdenka0ebde52004-09-08 22:03:11 +0000286 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000287}
288
Simon Glass0d1e1f72014-07-23 06:54:59 -0600289static int nc_tstc(struct stdio_dev *dev)
wdenkb8fb6192004-08-02 21:11:11 +0000290{
291 struct eth_device *eth;
292
293 if (input_recursion)
294 return 0;
295
wdenka0ebde52004-09-08 22:03:11 +0000296 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000297 return 1;
298
Joe Hershberger107b3d02012-05-15 08:59:13 +0000299 eth = eth_get_dev();
wdenkb8fb6192004-08-02 21:11:11 +0000300 if (eth && eth->state == ETH_STATE_ACTIVE)
301 return 0; /* inside net loop */
302
303 input_recursion = 1;
304
305 net_timeout = 1;
Joe Hershberger107b3d02012-05-15 08:59:13 +0000306 NetLoop(NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000307
308 input_recursion = 0;
309
wdenka0ebde52004-09-08 22:03:11 +0000310 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000311}
312
Joe Hershberger107b3d02012-05-15 08:59:13 +0000313int drv_nc_init(void)
wdenkb8fb6192004-08-02 21:11:11 +0000314{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200315 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000316 int rc;
317
Joe Hershberger107b3d02012-05-15 08:59:13 +0000318 memset(&dev, 0, sizeof(dev));
wdenkb8fb6192004-08-02 21:11:11 +0000319
Joe Hershberger107b3d02012-05-15 08:59:13 +0000320 strcpy(dev.name, "nc");
wdenkb8fb6192004-08-02 21:11:11 +0000321 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
322 dev.start = nc_start;
323 dev.putc = nc_putc;
324 dev.puts = nc_puts;
325 dev.getc = nc_getc;
326 dev.tstc = nc_tstc;
327
Joe Hershberger107b3d02012-05-15 08:59:13 +0000328 rc = stdio_register(&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000329
330 return (rc == 0) ? 1 : rc;
331}