3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <stdio_dev.h>
29 DECLARE_GLOBAL_DATA_PTR
;
31 #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
32 #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
35 static char input_buffer
[CONFIG_NETCONSOLE_BUFFER_SIZE
];
36 static int input_size
; /* char count in input buffer */
37 static int input_offset
; /* offset to valid chars in input buffer */
38 static int input_recursion
;
39 static int output_recursion
;
40 static int net_timeout
;
41 static uchar nc_ether
[6]; /* server enet address */
42 static IPaddr_t nc_ip
; /* server ip */
43 static short nc_out_port
; /* target output port */
44 static short nc_in_port
; /* source input port */
45 static const char *output_packet
; /* used by first send udp */
46 static int output_packet_len
;
48 * Start with a default last protocol.
49 * We are only interested in NETCONS or not.
51 enum proto_t net_loop_last_protocol
= BOOTP
;
53 static void nc_wait_arp_handler(uchar
*pkt
, unsigned dest
,
54 IPaddr_t sip
, unsigned src
,
57 net_set_state(NETLOOP_SUCCESS
); /* got arp reply - quit net loop */
60 static void nc_handler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
64 net_set_state(NETLOOP_SUCCESS
); /* got input - quit net loop */
67 static void nc_timeout(void)
69 net_set_state(NETLOOP_SUCCESS
);
72 static int is_broadcast(IPaddr_t ip
)
74 static IPaddr_t netmask
;
75 static IPaddr_t our_ip
;
76 static int env_changed_id
;
77 int env_id
= get_env_id();
79 /* update only when the environment has changed */
80 if (env_changed_id
!= env_id
) {
81 netmask
= getenv_IPaddr("netmask");
82 our_ip
= getenv_IPaddr("ipaddr");
84 env_changed_id
= env_id
;
87 return (ip
== ~0 || /* 255.255.255.255 */
88 ((netmask
& our_ip
) == (netmask
& ip
) && /* on the same net */
89 (netmask
| ip
) == ~0)); /* broadcast to our net */
92 static int refresh_settings_from_env(void)
95 static int env_changed_id
;
96 int env_id
= get_env_id();
98 /* update only when the environment has changed */
99 if (env_changed_id
!= env_id
) {
100 if (getenv("ncip")) {
101 nc_ip
= getenv_IPaddr("ncip");
103 return -1; /* ncip is 0.0.0.0 */
104 p
= strchr(getenv("ncip"), ':');
106 nc_out_port
= simple_strtoul(p
+ 1, NULL
, 10);
107 nc_in_port
= nc_out_port
;
110 nc_ip
= ~0; /* ncip is not set, so broadcast */
112 p
= getenv("ncoutport");
114 nc_out_port
= simple_strtoul(p
, NULL
, 10);
115 p
= getenv("ncinport");
117 nc_in_port
= simple_strtoul(p
, NULL
, 10);
119 if (is_broadcast(nc_ip
))
120 /* broadcast MAC address */
121 memset(nc_ether
, 0xff, sizeof(nc_ether
));
123 /* force arp request */
124 memset(nc_ether
, 0, sizeof(nc_ether
));
130 * Called from NetLoop in net/net.c before each packet
134 refresh_settings_from_env();
135 if (!output_packet_len
|| memcmp(nc_ether
, NetEtherNullAddr
, 6)) {
136 /* going to check for input packet */
137 net_set_udp_handler(nc_handler
);
138 NetSetTimeout(net_timeout
, nc_timeout
);
140 /* send arp request */
142 net_set_arp_handler(nc_wait_arp_handler
);
143 pkt
= (uchar
*)NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
;
144 memcpy(pkt
, output_packet
, output_packet_len
);
145 NetSendUDPPacket(nc_ether
, nc_ip
, nc_out_port
, nc_in_port
,
150 int nc_input_packet(uchar
*pkt
, unsigned dest
, unsigned src
, unsigned len
)
154 if (dest
!= nc_in_port
|| !len
)
155 return 0; /* not for us */
157 debug_cond(DEBUG_DEV_PKT
, "input: \"%*.*s\"\n", len
, len
, pkt
);
159 if (input_size
== sizeof(input_buffer
))
160 return 1; /* no space */
161 if (len
> sizeof(input_buffer
) - input_size
)
162 len
= sizeof(input_buffer
) - input_size
;
164 end
= input_offset
+ input_size
;
165 if (end
> sizeof(input_buffer
))
166 end
-= sizeof(input_buffer
);
169 if (end
+ len
> sizeof(input_buffer
)) {
170 chunk
= sizeof(input_buffer
) - end
;
171 memcpy(input_buffer
, pkt
+ chunk
, len
- chunk
);
173 memcpy(input_buffer
+ end
, pkt
, chunk
);
180 static void nc_send_packet(const char *buf
, int len
)
182 struct eth_device
*eth
;
188 debug_cond(DEBUG_DEV_PKT
, "output: \"%*.*s\"\n", len
, len
, buf
);
194 if (!memcmp(nc_ether
, NetEtherNullAddr
, 6)) {
195 if (eth
->state
== ETH_STATE_ACTIVE
)
196 return; /* inside net loop */
198 output_packet_len
= len
;
199 NetLoop(NETCONS
); /* wait for arp reply and send packet */
200 output_packet_len
= 0;
204 if (eth
->state
!= ETH_STATE_ACTIVE
) {
205 if (eth_is_on_demand_init()) {
206 if (eth_init(gd
->bd
) < 0)
208 eth_set_last_protocol(NETCONS
);
210 eth_init_state_only(gd
->bd
);
214 pkt
= (uchar
*)NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
;
215 memcpy(pkt
, buf
, len
);
218 NetSendUDPPacket(ether
, ip
, nc_out_port
, nc_in_port
, len
);
221 if (eth_is_on_demand_init())
224 eth_halt_state_only();
228 static int nc_start(void)
232 nc_out_port
= 6666; /* default port */
233 nc_in_port
= nc_out_port
;
235 retval
= refresh_settings_from_env();
240 * Initialize the static IP settings and buffer pointers
241 * incase we call NetSendUDPPacket before NetLoop
248 static void nc_putc(char c
)
250 if (output_recursion
)
252 output_recursion
= 1;
254 nc_send_packet(&c
, 1);
256 output_recursion
= 0;
259 static void nc_puts(const char *s
)
263 if (output_recursion
)
265 output_recursion
= 1;
269 int send_len
= min(len
, sizeof(input_buffer
));
270 nc_send_packet(s
, send_len
);
275 output_recursion
= 0;
278 static int nc_getc(void)
284 net_timeout
= 0; /* no timeout */
290 c
= input_buffer
[input_offset
++];
292 if (input_offset
>= sizeof(input_buffer
))
293 input_offset
-= sizeof(input_buffer
);
299 static int nc_tstc(void)
301 struct eth_device
*eth
;
310 if (eth
&& eth
->state
== ETH_STATE_ACTIVE
)
311 return 0; /* inside net loop */
316 NetLoop(NETCONS
); /* kind of poll */
320 return input_size
!= 0;
323 int drv_nc_init(void)
325 struct stdio_dev dev
;
328 memset(&dev
, 0, sizeof(dev
));
330 strcpy(dev
.name
, "nc");
331 dev
.flags
= DEV_FLAGS_OUTPUT
| DEV_FLAGS_INPUT
| DEV_FLAGS_SYSTEM
;
332 dev
.start
= nc_start
;
338 rc
= stdio_register(&dev
);
340 return (rc
== 0) ? 1 : rc
;