xref: /rk3399_rockchip-uboot/drivers/net/netconsole.c (revision d7310c7e63ca9ffd42527dc9735cb505cbe908b7)
1 /*
2  * (C) Copyright 2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <command.h>
26 #include <stdio_dev.h>
27 #include <net.h>
28 
29 DECLARE_GLOBAL_DATA_PTR;
30 
31 static char input_buffer[512];
32 static int input_size; /* char count in input buffer */
33 static int input_offset; /* offset to valid chars in input buffer */
34 static int input_recursion;
35 static int output_recursion;
36 static int net_timeout;
37 static uchar nc_ether[6]; /* server enet address */
38 static IPaddr_t nc_ip; /* server ip */
39 static short nc_port; /* source/target port */
40 static const char *output_packet; /* used by first send udp */
41 static int output_packet_len;
42 
43 static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
44 				 IPaddr_t sip, unsigned src,
45 				 unsigned len)
46 {
47 	net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
48 }
49 
50 static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
51 			unsigned len)
52 {
53 	if (input_size)
54 		net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
55 }
56 
57 static void nc_timeout(void)
58 {
59 	net_set_state(NETLOOP_SUCCESS);
60 }
61 
62 void NcStart(void)
63 {
64 	if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
65 		/* going to check for input packet */
66 		net_set_udp_handler(nc_handler);
67 		NetSetTimeout(net_timeout, nc_timeout);
68 	} else {
69 		/* send arp request */
70 		uchar *pkt;
71 		net_set_arp_handler(nc_wait_arp_handler);
72 		pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
73 		memcpy(pkt, output_packet, output_packet_len);
74 		NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port,
75 			output_packet_len);
76 	}
77 }
78 
79 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
80 {
81 	int end, chunk;
82 
83 	if (dest != nc_port || !len)
84 		return 0; /* not for us */
85 
86 	if (input_size == sizeof(input_buffer))
87 		return 1; /* no space */
88 	if (len > sizeof(input_buffer) - input_size)
89 		len = sizeof(input_buffer) - input_size;
90 
91 	end = input_offset + input_size;
92 	if (end > sizeof(input_buffer))
93 		end -= sizeof(input_buffer);
94 
95 	chunk = len;
96 	if (end + len > sizeof(input_buffer)) {
97 		chunk = sizeof(input_buffer) - end;
98 		memcpy(input_buffer, pkt + chunk, len - chunk);
99 	}
100 	memcpy(input_buffer + end, pkt, chunk);
101 
102 	input_size += len;
103 
104 	return 1;
105 }
106 
107 static void nc_send_packet(const char *buf, int len)
108 {
109 	struct eth_device *eth;
110 	int inited = 0;
111 	uchar *pkt;
112 	uchar *ether;
113 	IPaddr_t ip;
114 
115 	eth = eth_get_dev();
116 	if (eth == NULL)
117 		return;
118 
119 	if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
120 		if (eth->state == ETH_STATE_ACTIVE)
121 			return;	/* inside net loop */
122 		output_packet = buf;
123 		output_packet_len = len;
124 		NetLoop(NETCONS); /* wait for arp reply and send packet */
125 		output_packet_len = 0;
126 		return;
127 	}
128 
129 	if (eth->state != ETH_STATE_ACTIVE) {
130 		if (eth_init(gd->bd) < 0)
131 			return;
132 		inited = 1;
133 	}
134 	pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
135 	memcpy(pkt, buf, len);
136 	ether = nc_ether;
137 	ip = nc_ip;
138 	NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
139 
140 	if (inited)
141 		eth_halt();
142 }
143 
144 static int nc_start(void)
145 {
146 	int netmask, our_ip;
147 
148 	nc_port = 6666;		/* default port */
149 
150 	if (getenv("ncip")) {
151 		char *p;
152 
153 		nc_ip = getenv_IPaddr("ncip");
154 		if (!nc_ip)
155 			return -1;	/* ncip is 0.0.0.0 */
156 		p = strchr(getenv("ncip"), ':');
157 		if (p != NULL)
158 			nc_port = simple_strtoul(p + 1, NULL, 10);
159 	} else
160 		nc_ip = ~0;		/* ncip is not set */
161 
162 	our_ip = getenv_IPaddr("ipaddr");
163 	netmask = getenv_IPaddr("netmask");
164 
165 	if (nc_ip == ~0 ||				/* 255.255.255.255 */
166 	    ((netmask & our_ip) == (netmask & nc_ip) &&	/* on the same net */
167 	    (netmask | nc_ip) == ~0))		/* broadcast to our net */
168 		memset(nc_ether, 0xff, sizeof(nc_ether));
169 	else
170 		memset(nc_ether, 0, sizeof(nc_ether));	/* force arp request */
171 
172 	/*
173 	 * Initialize the static IP settings and buffer pointers
174 	 * incase we call NetSendUDPPacket before NetLoop
175 	 */
176 	net_init();
177 
178 	return 0;
179 }
180 
181 static void nc_putc(char c)
182 {
183 	if (output_recursion)
184 		return;
185 	output_recursion = 1;
186 
187 	nc_send_packet(&c, 1);
188 
189 	output_recursion = 0;
190 }
191 
192 static void nc_puts(const char *s)
193 {
194 	int len;
195 
196 	if (output_recursion)
197 		return;
198 	output_recursion = 1;
199 
200 	len = strlen(s);
201 	while (len) {
202 		int send_len = min(len, 512);
203 		nc_send_packet(s, send_len);
204 		len -= send_len;
205 		s += send_len;
206 	}
207 
208 	output_recursion = 0;
209 }
210 
211 static int nc_getc(void)
212 {
213 	uchar c;
214 
215 	input_recursion = 1;
216 
217 	net_timeout = 0;	/* no timeout */
218 	while (!input_size)
219 		NetLoop(NETCONS);
220 
221 	input_recursion = 0;
222 
223 	c = input_buffer[input_offset++];
224 
225 	if (input_offset >= sizeof(input_buffer))
226 		input_offset -= sizeof(input_buffer);
227 	input_size--;
228 
229 	return c;
230 }
231 
232 static int nc_tstc(void)
233 {
234 	struct eth_device *eth;
235 
236 	if (input_recursion)
237 		return 0;
238 
239 	if (input_size)
240 		return 1;
241 
242 	eth = eth_get_dev();
243 	if (eth && eth->state == ETH_STATE_ACTIVE)
244 		return 0;	/* inside net loop */
245 
246 	input_recursion = 1;
247 
248 	net_timeout = 1;
249 	NetLoop(NETCONS);	/* kind of poll */
250 
251 	input_recursion = 0;
252 
253 	return input_size != 0;
254 }
255 
256 int drv_nc_init(void)
257 {
258 	struct stdio_dev dev;
259 	int rc;
260 
261 	memset(&dev, 0, sizeof(dev));
262 
263 	strcpy(dev.name, "nc");
264 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
265 	dev.start = nc_start;
266 	dev.putc = nc_putc;
267 	dev.puts = nc_puts;
268 	dev.getc = nc_getc;
269 	dev.tstc = nc_tstc;
270 
271 	rc = stdio_register(&dev);
272 
273 	return (rc == 0) ? 1 : rc;
274 }
275