blob: 56ba64fbb030121e0e6f1ec2d2613fe93e6cc235 [file] [log] [blame]
wdenkb8fb6192004-08-02 21:11:11 +00001/*
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>
wdenkb8fb6192004-08-02 21:11:11 +000025#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020026#include <stdio_dev.h>
wdenkb8fb6192004-08-02 21:11:11 +000027#include <net.h>
28
Wolfgang Denk6405a152006-03-31 18:32:53 +020029DECLARE_GLOBAL_DATA_PTR;
30
wdenka0ebde52004-09-08 22:03:11 +000031static char input_buffer[512];
32static int input_size = 0; /* char count in input buffer */
33static int input_offset = 0; /* offset to valid chars in input buffer */
wdenkb8fb6192004-08-02 21:11:11 +000034static int input_recursion = 0;
35static int output_recursion = 0;
36static int net_timeout;
wdenka0ebde52004-09-08 22:03:11 +000037static uchar nc_ether[6]; /* server enet address */
38static IPaddr_t nc_ip; /* server ip */
39static short nc_port; /* source/target port */
40static const char *output_packet; /* used by first send udp */
41static int output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +000042
Luca Ceresoli428ab362011-04-18 06:19:50 +000043static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
44 IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000045 unsigned len)
46{
47 NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
48}
49
Luca Ceresoli428ab362011-04-18 06:19:50 +000050static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
wdenkb8fb6192004-08-02 21:11:11 +000051 unsigned len)
52{
wdenka0ebde52004-09-08 22:03:11 +000053 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +000054 NetState = NETLOOP_SUCCESS; /* got input - quit net loop */
55}
56
57static void nc_timeout (void)
58{
59 NetState = NETLOOP_SUCCESS;
60}
61
62void NcStart (void)
63{
wdenka0ebde52004-09-08 22:03:11 +000064 if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) {
wdenkb8fb6192004-08-02 21:11:11 +000065 /* going to check for input packet */
66 NetSetHandler (nc_handler);
67 NetSetTimeout (net_timeout, nc_timeout);
68 } else {
69 /* send arp request */
wdenka0ebde52004-09-08 22:03:11 +000070 uchar *pkt;
wdenkb8fb6192004-08-02 21:11:11 +000071 NetSetHandler (nc_wait_arp_handler);
wdenka0ebde52004-09-08 22:03:11 +000072 pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE;
73 memcpy (pkt, output_packet, output_packet_len);
74 NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, output_packet_len);
wdenkb8fb6192004-08-02 21:11:11 +000075 }
76}
77
78int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len)
79{
wdenka0ebde52004-09-08 22:03:11 +000080 int end, chunk;
81
82 if (dest != nc_port || !len)
wdenkb8fb6192004-08-02 21:11:11 +000083 return 0; /* not for us */
84
wdenka0ebde52004-09-08 22:03:11 +000085 if (input_size == sizeof input_buffer)
86 return 1; /* no space */
87 if (len > sizeof input_buffer - input_size)
88 len = sizeof input_buffer - input_size;
89
90 end = input_offset + input_size;
91 if (end > sizeof input_buffer)
92 end -= sizeof input_buffer;
93
94 chunk = len;
95 if (end + len > sizeof input_buffer) {
96 chunk = sizeof input_buffer - end;
97 memcpy(input_buffer, pkt + chunk, len - chunk);
98 }
99 memcpy (input_buffer + end, pkt, chunk);
100
101 input_size += len;
102
wdenkb8fb6192004-08-02 21:11:11 +0000103 return 1;
104}
105
106static void nc_send_packet (const char *buf, int len)
107{
wdenkb8fb6192004-08-02 21:11:11 +0000108 struct eth_device *eth;
109 int inited = 0;
110 uchar *pkt;
wdenka0ebde52004-09-08 22:03:11 +0000111 uchar *ether;
112 IPaddr_t ip;
wdenkb8fb6192004-08-02 21:11:11 +0000113
wdenka0ebde52004-09-08 22:03:11 +0000114 if ((eth = eth_get_dev ()) == NULL) {
wdenkb8fb6192004-08-02 21:11:11 +0000115 return;
wdenka0ebde52004-09-08 22:03:11 +0000116 }
wdenkb8fb6192004-08-02 21:11:11 +0000117
wdenka0ebde52004-09-08 22:03:11 +0000118 if (!memcmp (nc_ether, NetEtherNullAddr, 6)) {
119 if (eth->state == ETH_STATE_ACTIVE)
120 return; /* inside net loop */
121 output_packet = buf;
122 output_packet_len = len;
123 NetLoop (NETCONS); /* wait for arp reply and send packet */
124 output_packet_len = 0;
wdenkb8fb6192004-08-02 21:11:11 +0000125 return;
126 }
127
128 if (eth->state != ETH_STATE_ACTIVE) {
129 if (eth_init (gd->bd) < 0)
130 return;
131 inited = 1;
132 }
133 pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE;
134 memcpy (pkt, buf, len);
wdenka0ebde52004-09-08 22:03:11 +0000135 ether = nc_ether;
136 ip = nc_ip;
137 NetSendUDPPacket (ether, ip, nc_port, nc_port, len);
wdenkb8fb6192004-08-02 21:11:11 +0000138
139 if (inited)
140 eth_halt ();
141}
142
Mike Frysinger31da77e2009-12-21 14:19:12 -0500143static int nc_start(void)
wdenkb8fb6192004-08-02 21:11:11 +0000144{
wdenka0ebde52004-09-08 22:03:11 +0000145 int netmask, our_ip;
146
147 nc_port = 6666; /* default port */
wdenkb8fb6192004-08-02 21:11:11 +0000148
wdenka0ebde52004-09-08 22:03:11 +0000149 if (getenv ("ncip")) {
wdenkb21608e2005-04-20 09:28:54 +0000150 char *p;
151
wdenka0ebde52004-09-08 22:03:11 +0000152 nc_ip = getenv_IPaddr ("ncip");
153 if (!nc_ip)
154 return -1; /* ncip is 0.0.0.0 */
wdenkb21608e2005-04-20 09:28:54 +0000155 if ((p = strchr (getenv ("ncip"), ':')) != NULL)
wdenka0ebde52004-09-08 22:03:11 +0000156 nc_port = simple_strtoul (p + 1, NULL, 10);
157 } else
158 nc_ip = ~0; /* ncip is not set */
159
160 our_ip = getenv_IPaddr ("ipaddr");
161 netmask = getenv_IPaddr ("netmask");
162
163 if (nc_ip == ~0 || /* 255.255.255.255 */
164 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
165 (netmask | nc_ip) == ~0)) /* broadcast to our net */
166 memset (nc_ether, 0xff, sizeof nc_ether);
167 else
168 memset (nc_ether, 0, sizeof nc_ether); /* force arp request */
169
170 return 0;
wdenkb8fb6192004-08-02 21:11:11 +0000171}
172
Mike Frysinger31da77e2009-12-21 14:19:12 -0500173static void nc_putc(char c)
wdenkb8fb6192004-08-02 21:11:11 +0000174{
175 if (output_recursion)
176 return;
177 output_recursion = 1;
178
179 nc_send_packet (&c, 1);
180
181 output_recursion = 0;
182}
183
Mike Frysinger31da77e2009-12-21 14:19:12 -0500184static void nc_puts(const char *s)
wdenkb8fb6192004-08-02 21:11:11 +0000185{
wdenkb21608e2005-04-20 09:28:54 +0000186 int len;
187
wdenkb8fb6192004-08-02 21:11:11 +0000188 if (output_recursion)
189 return;
190 output_recursion = 1;
191
Michael Walle9227aed2011-10-07 12:27:50 +0000192 len = strlen(s);
193 while (len) {
194 int send_len = min(len, 512);
195 nc_send_packet(s, send_len);
196 len -= send_len;
197 s += send_len;
198 }
wdenkb8fb6192004-08-02 21:11:11 +0000199
200 output_recursion = 0;
201}
202
Mike Frysinger31da77e2009-12-21 14:19:12 -0500203static int nc_getc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000204{
wdenkb21608e2005-04-20 09:28:54 +0000205 uchar c;
206
wdenkb8fb6192004-08-02 21:11:11 +0000207 input_recursion = 1;
208
209 net_timeout = 0; /* no timeout */
wdenka0ebde52004-09-08 22:03:11 +0000210 while (!input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000211 NetLoop (NETCONS);
212
213 input_recursion = 0;
214
wdenkb21608e2005-04-20 09:28:54 +0000215 c = input_buffer[input_offset++];
216
wdenka0ebde52004-09-08 22:03:11 +0000217 if (input_offset >= sizeof input_buffer)
218 input_offset -= sizeof input_buffer;
219 input_size--;
wdenkb8fb6192004-08-02 21:11:11 +0000220
wdenka0ebde52004-09-08 22:03:11 +0000221 return c;
wdenkb8fb6192004-08-02 21:11:11 +0000222}
223
Mike Frysinger31da77e2009-12-21 14:19:12 -0500224static int nc_tstc(void)
wdenkb8fb6192004-08-02 21:11:11 +0000225{
226 struct eth_device *eth;
227
228 if (input_recursion)
229 return 0;
230
wdenka0ebde52004-09-08 22:03:11 +0000231 if (input_size)
wdenkb8fb6192004-08-02 21:11:11 +0000232 return 1;
233
234 eth = eth_get_dev ();
235 if (eth && eth->state == ETH_STATE_ACTIVE)
236 return 0; /* inside net loop */
237
238 input_recursion = 1;
239
240 net_timeout = 1;
wdenka0ebde52004-09-08 22:03:11 +0000241 NetLoop (NETCONS); /* kind of poll */
wdenkb8fb6192004-08-02 21:11:11 +0000242
243 input_recursion = 0;
244
wdenka0ebde52004-09-08 22:03:11 +0000245 return input_size != 0;
wdenkb8fb6192004-08-02 21:11:11 +0000246}
247
248int drv_nc_init (void)
249{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200250 struct stdio_dev dev;
wdenkb8fb6192004-08-02 21:11:11 +0000251 int rc;
252
253 memset (&dev, 0, sizeof (dev));
254
255 strcpy (dev.name, "nc");
256 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
257 dev.start = nc_start;
258 dev.putc = nc_putc;
259 dev.puts = nc_puts;
260 dev.getc = nc_getc;
261 dev.tstc = nc_tstc;
262
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200263 rc = stdio_register (&dev);
wdenkb8fb6192004-08-02 21:11:11 +0000264
265 return (rc == 0) ? 1 : rc;
266}