xref: /openbmc/u-boot/drivers/net/netconsole.c (revision f9727161)
1 /*
2  * (C) Copyright 2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <command.h>
10 #include <stdio_dev.h>
11 #include <net.h>
12 
13 DECLARE_GLOBAL_DATA_PTR;
14 
15 #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
16 #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
17 #endif
18 
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 IPaddr_t 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;
31 /*
32  * Start with a default last protocol.
33  * We are only interested in NETCONS or not.
34  */
35 enum proto_t net_loop_last_protocol = BOOTP;
36 
37 static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
38 				 IPaddr_t sip, unsigned src,
39 				 unsigned len)
40 {
41 	net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
42 }
43 
44 static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
45 			unsigned len)
46 {
47 	if (input_size)
48 		net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
49 }
50 
51 static void nc_timeout(void)
52 {
53 	net_set_state(NETLOOP_SUCCESS);
54 }
55 
56 static 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 
76 static 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  */
116 void NcStart(void)
117 {
118 	refresh_settings_from_env();
119 	if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
120 		/* going to check for input packet */
121 		net_set_udp_handler(nc_handler);
122 		NetSetTimeout(net_timeout, nc_timeout);
123 	} else {
124 		/* send arp request */
125 		uchar *pkt;
126 		net_set_arp_handler(nc_wait_arp_handler);
127 		pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
128 		memcpy(pkt, output_packet, output_packet_len);
129 		NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
130 			output_packet_len);
131 	}
132 }
133 
134 int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
135 	unsigned src_port, unsigned len)
136 {
137 	int end, chunk;
138 
139 	if (dest_port != nc_in_port || !len)
140 		return 0; /* not for us */
141 
142 	if (src_ip != nc_ip && !is_broadcast(nc_ip))
143 		return 0; /* not from our client */
144 
145 	debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
146 
147 	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;
151 
152 	end = input_offset + input_size;
153 	if (end > sizeof(input_buffer))
154 		end -= sizeof(input_buffer);
155 
156 	chunk = len;
157 	if (end + len > sizeof(input_buffer)) {
158 		chunk = sizeof(input_buffer) - end;
159 		memcpy(input_buffer, pkt + chunk, len - chunk);
160 	}
161 	memcpy(input_buffer + end, pkt, chunk);
162 
163 	input_size += len;
164 
165 	return 1;
166 }
167 
168 static void nc_send_packet(const char *buf, int len)
169 {
170 	struct eth_device *eth;
171 	int inited = 0;
172 	uchar *pkt;
173 	uchar *ether;
174 	IPaddr_t ip;
175 
176 	debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
177 
178 	eth = eth_get_dev();
179 	if (eth == NULL)
180 		return;
181 
182 	if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
183 		if (eth->state == ETH_STATE_ACTIVE)
184 			return;	/* inside net loop */
185 		output_packet = buf;
186 		output_packet_len = len;
187 		NetLoop(NETCONS); /* wait for arp reply and send packet */
188 		output_packet_len = 0;
189 		return;
190 	}
191 
192 	if (eth->state != ETH_STATE_ACTIVE) {
193 		if (eth_is_on_demand_init()) {
194 			if (eth_init(gd->bd) < 0)
195 				return;
196 			eth_set_last_protocol(NETCONS);
197 		} else
198 			eth_init_state_only(gd->bd);
199 
200 		inited = 1;
201 	}
202 	pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
203 	memcpy(pkt, buf, len);
204 	ether = nc_ether;
205 	ip = nc_ip;
206 	NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
207 
208 	if (inited) {
209 		if (eth_is_on_demand_init())
210 			eth_halt();
211 		else
212 			eth_halt_state_only();
213 	}
214 }
215 
216 static int nc_start(void)
217 {
218 	int retval;
219 
220 	nc_out_port = 6666; /* default port */
221 	nc_in_port = nc_out_port;
222 
223 	retval = refresh_settings_from_env();
224 	if (retval != 0)
225 		return retval;
226 
227 	/*
228 	 * Initialize the static IP settings and buffer pointers
229 	 * incase we call NetSendUDPPacket before NetLoop
230 	 */
231 	net_init();
232 
233 	return 0;
234 }
235 
236 static void nc_putc(char c)
237 {
238 	if (output_recursion)
239 		return;
240 	output_recursion = 1;
241 
242 	nc_send_packet(&c, 1);
243 
244 	output_recursion = 0;
245 }
246 
247 static void nc_puts(const char *s)
248 {
249 	int len;
250 
251 	if (output_recursion)
252 		return;
253 	output_recursion = 1;
254 
255 	len = strlen(s);
256 	while (len) {
257 		int send_len = min(len, sizeof(input_buffer));
258 		nc_send_packet(s, send_len);
259 		len -= send_len;
260 		s += send_len;
261 	}
262 
263 	output_recursion = 0;
264 }
265 
266 static int nc_getc(void)
267 {
268 	uchar c;
269 
270 	input_recursion = 1;
271 
272 	net_timeout = 0;	/* no timeout */
273 	while (!input_size)
274 		NetLoop(NETCONS);
275 
276 	input_recursion = 0;
277 
278 	c = input_buffer[input_offset++];
279 
280 	if (input_offset >= sizeof(input_buffer))
281 		input_offset -= sizeof(input_buffer);
282 	input_size--;
283 
284 	return c;
285 }
286 
287 static int nc_tstc(void)
288 {
289 	struct eth_device *eth;
290 
291 	if (input_recursion)
292 		return 0;
293 
294 	if (input_size)
295 		return 1;
296 
297 	eth = eth_get_dev();
298 	if (eth && eth->state == ETH_STATE_ACTIVE)
299 		return 0;	/* inside net loop */
300 
301 	input_recursion = 1;
302 
303 	net_timeout = 1;
304 	NetLoop(NETCONS);	/* kind of poll */
305 
306 	input_recursion = 0;
307 
308 	return input_size != 0;
309 }
310 
311 int drv_nc_init(void)
312 {
313 	struct stdio_dev dev;
314 	int rc;
315 
316 	memset(&dev, 0, sizeof(dev));
317 
318 	strcpy(dev.name, "nc");
319 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
320 	dev.start = nc_start;
321 	dev.putc = nc_putc;
322 	dev.puts = nc_puts;
323 	dev.getc = nc_getc;
324 	dev.tstc = nc_tstc;
325 
326 	rc = stdio_register(&dev);
327 
328 	return (rc == 0) ? 1 : rc;
329 }
330