3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <stdio_dev.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
16 #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
19 static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
20 static int input_size; /* char count in input buffer */
21 static int input_offset; /* offset to valid chars in input buffer */
22 static int input_recursion;
23 static int output_recursion;
24 static int net_timeout;
25 static uchar nc_ether[6]; /* server enet address */
26 static struct in_addr nc_ip; /* server ip */
27 static short nc_out_port; /* target output port */
28 static short nc_in_port; /* source input port */
29 static const char *output_packet; /* used by first send udp */
30 static int output_packet_len;
32 * Start with a default last protocol.
33 * We are only interested in NETCONS or not.
35 enum proto_t net_loop_last_protocol = BOOTP;
37 static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
38 struct in_addr sip, unsigned src,
41 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
44 static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
45 unsigned src, unsigned len)
48 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
51 static void nc_timeout_handler(void)
53 net_set_state(NETLOOP_SUCCESS);
56 static int is_broadcast(struct in_addr ip)
58 static struct in_addr netmask;
59 static struct in_addr our_ip;
60 static int env_changed_id;
61 int env_id = get_env_id();
63 /* update only when the environment has changed */
64 if (env_changed_id != env_id) {
65 netmask = env_get_ip("netmask");
66 our_ip = env_get_ip("ipaddr");
68 env_changed_id = env_id;
71 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 */
77 static int refresh_settings_from_env(void)
80 static int env_changed_id;
81 int env_id = get_env_id();
83 /* update only when the environment has changed */
84 if (env_changed_id != env_id) {
85 if (env_get("ncip")) {
86 nc_ip = env_get_ip("ncip");
88 return -1; /* ncip is 0.0.0.0 */
89 p = strchr(env_get("ncip"), ':');
91 nc_out_port = simple_strtoul(p + 1, NULL, 10);
92 nc_in_port = nc_out_port;
95 nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
98 p = env_get("ncoutport");
100 nc_out_port = simple_strtoul(p, NULL, 10);
101 p = env_get("ncinport");
103 nc_in_port = simple_strtoul(p, NULL, 10);
105 if (is_broadcast(nc_ip))
106 /* broadcast MAC address */
107 memset(nc_ether, 0xff, sizeof(nc_ether));
109 /* force arp request */
110 memset(nc_ether, 0, sizeof(nc_ether));
116 * Called from net_loop in net/net.c before each packet
120 refresh_settings_from_env();
121 if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
122 /* going to check for input packet */
123 net_set_udp_handler(nc_handler);
124 net_set_timeout_handler(net_timeout, nc_timeout_handler);
126 /* send arp request */
128 net_set_arp_handler(nc_wait_arp_handler);
129 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
131 memcpy(pkt, output_packet, output_packet_len);
132 net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
137 int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
138 unsigned src_port, unsigned len)
142 if (dest_port != nc_in_port || !len)
143 return 0; /* not for us */
145 if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
146 return 0; /* not from our client */
148 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
150 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;
155 end = input_offset + input_size;
156 if (end >= sizeof(input_buffer))
157 end -= sizeof(input_buffer);
160 /* Check if packet will wrap in input_buffer */
161 if (end + len >= sizeof(input_buffer)) {
162 chunk = sizeof(input_buffer) - end;
163 /* Copy the second part of the pkt to start of input_buffer */
164 memcpy(input_buffer, pkt + chunk, len - chunk);
166 /* Copy first (or only) part of pkt after end of current valid input*/
167 memcpy(input_buffer + end, pkt, chunk);
174 static void nc_send_packet(const char *buf, int len)
179 struct eth_device *eth;
186 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
192 if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
193 if (eth_is_active(eth))
194 return; /* inside net loop */
196 output_packet_len = len;
198 net_loop(NETCONS); /* wait for arp reply and send packet */
200 output_packet_len = 0;
204 if (!eth_is_active(eth)) {
205 if (eth_is_on_demand_init()) {
208 eth_set_last_protocol(NETCONS);
210 eth_init_state_only();
215 pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
216 memcpy(pkt, buf, len);
219 net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
222 if (eth_is_on_demand_init())
225 eth_halt_state_only();
229 static int nc_stdio_start(struct stdio_dev *dev)
233 nc_out_port = 6666; /* default port */
234 nc_in_port = nc_out_port;
236 retval = refresh_settings_from_env();
241 * Initialize the static IP settings and buffer pointers
242 * incase we call net_send_udp_packet before net_loop
249 static void nc_stdio_putc(struct stdio_dev *dev, char c)
251 if (output_recursion)
253 output_recursion = 1;
255 nc_send_packet(&c, 1);
257 output_recursion = 0;
260 static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
264 if (output_recursion)
266 output_recursion = 1;
270 int send_len = min(len, (int)sizeof(input_buffer));
271 nc_send_packet(s, send_len);
276 output_recursion = 0;
279 static int nc_stdio_getc(struct stdio_dev *dev)
285 net_timeout = 0; /* no timeout */
291 c = input_buffer[input_offset++];
293 if (input_offset >= sizeof(input_buffer))
294 input_offset -= sizeof(input_buffer);
300 static int nc_stdio_tstc(struct stdio_dev *dev)
305 struct eth_device *eth;
315 if (eth_is_active(eth))
316 return 0; /* inside net loop */
321 net_loop(NETCONS); /* kind of poll */
325 return input_size != 0;
328 int drv_nc_init(void)
330 struct stdio_dev dev;
333 memset(&dev, 0, sizeof(dev));
335 strcpy(dev.name, "nc");
336 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
337 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;
343 rc = stdio_register(&dev);
345 return (rc == 0) ? 1 : rc;