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,
25 #ifdef CONFIG_NETCONSOLE
30 DECLARE_GLOBAL_DATA_PTR;
32 static char input_buffer[512];
33 static int input_size = 0; /* char count in input buffer */
34 static int input_offset = 0; /* offset to valid chars in input buffer */
35 static int input_recursion = 0;
36 static int output_recursion = 0;
37 static int net_timeout;
38 static uchar nc_ether[6]; /* server enet address */
39 static IPaddr_t nc_ip; /* server ip */
40 static short nc_port; /* source/target port */
41 static const char *output_packet; /* used by first send udp */
42 static int output_packet_len = 0;
45 * Start with a default last protocol.
46 * We are only interested in NETCONS or not.
48 proto_t net_loop_last_protocol = BOOTP;
50 static void nc_wait_arp_handler(uchar * pkt, unsigned dest, unsigned src, unsigned len){
51 NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
54 static void nc_handler(uchar * pkt, unsigned dest, unsigned src, unsigned len){
55 if(input_size){ /* got input - quit net loop */
56 NetState = NETLOOP_SUCCESS;
60 static void nc_timeout(void){
61 NetState = NETLOOP_SUCCESS;
65 if(!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)){
66 /* going to check for input packet */
67 NetSetHandler(nc_handler);
68 NetSetTimeout(net_timeout, nc_timeout);
70 /* send arp request */
72 NetSetHandler(nc_wait_arp_handler);
73 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
74 memcpy(pkt, output_packet, output_packet_len);
75 NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, output_packet_len);
79 int nc_input_packet(uchar * pkt, unsigned dest, unsigned src, unsigned len){
82 if(dest != nc_port || !len){ /* not for us */
86 if(input_size == sizeof(input_buffer)){ /* no space */
90 if(len > sizeof input_buffer - input_size){
91 len = sizeof input_buffer - input_size;
94 end = input_offset + input_size;
96 if(end > sizeof input_buffer){
97 end -= sizeof input_buffer;
102 if(end + len > sizeof input_buffer){
103 chunk = sizeof input_buffer - end;
104 memcpy(input_buffer, pkt + chunk, len - chunk);
107 memcpy(input_buffer + end, pkt, chunk);
114 static void nc_send_packet(const char *buf, int len){
115 struct eth_device *eth;
121 if((eth = eth_get_dev()) == NULL){
125 if(!memcmp(nc_ether, NetEtherNullAddr, 6)){
126 if(eth->state == ETH_STATE_ACTIVE){
131 output_packet_len = len;
132 NetLoop(NETCONS); /* wait for arp reply and send packet */
133 output_packet_len = 0;
138 if(eth->state != ETH_STATE_ACTIVE){
139 if(eth_is_on_demand_init()){
140 if(eth_init(gd->bd) < 0){
143 eth_set_last_protocol(NETCONS);
145 eth_init_state_only(gd->bd);
151 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
152 memcpy(pkt, buf, len);
155 NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
158 if(eth_is_on_demand_init()){
161 eth_halt_state_only();
170 p = getenv("ncport");
171 nc_port = p ? (short)simple_strtol(p, NULL, 10) : 6666; /* default NetConsole port */
173 if(getenv("serverip")){
175 nc_ip = getenv_IPaddr("serverip");
182 nc_ip = ~0; /* ncip is not set */
185 our_ip = getenv_IPaddr("ipaddr");
186 netmask = getenv_IPaddr("netmask");
188 if(nc_ip == ~0 || /* 255.255.255.255 */
189 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
190 (netmask | nc_ip) == ~0)) /* broadcast to our net */{
191 memset(nc_ether, 0xff, sizeof nc_ether);
193 memset(nc_ether, 0, sizeof nc_ether); /* force arp request */
199 void nc_putc(char c){
200 if(output_recursion){
204 output_recursion = 1;
206 nc_send_packet(&c, 1);
208 output_recursion = 0;
211 void nc_puts(const char *s){
214 if(output_recursion){
218 output_recursion = 1;
220 if((len = strlen(s)) > 512){
224 nc_send_packet(s, len);
226 output_recursion = 0;
234 net_timeout = 0; /* no timeout */
241 c = input_buffer[input_offset++];
243 if(input_offset >= sizeof input_buffer){
244 input_offset -= sizeof input_buffer;
253 struct eth_device *eth;
265 if(eth && eth->state == ETH_STATE_ACTIVE){
266 return(0); /* inside net loop */
272 NetLoop(NETCONS); /* kind of poll */
276 return(input_size != 0);
279 int drv_nc_init(void){
283 memset(&dev, 0, sizeof(dev));
285 strcpy(dev.name, "nc");
286 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
287 dev.start = nc_start;
293 rc = device_register(&dev);
295 return((rc == 0) ? 1 : rc);
298 #endif /* CONFIG_NETCONSOLE */