wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 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 | |
| 26 | #ifdef CONFIG_NETCONSOLE |
| 27 | |
| 28 | #include <command.h> |
| 29 | #include <devices.h> |
| 30 | #include <net.h> |
| 31 | |
| 32 | #ifndef CONFIG_NET_MULTI |
| 33 | #error define CONFIG_NET_MULTI to use netconsole |
| 34 | #endif |
| 35 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 36 | static char input_buffer[512]; |
| 37 | static int input_size = 0; /* char count in input buffer */ |
| 38 | static int input_offset = 0; /* offset to valid chars in input buffer */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 39 | static int input_recursion = 0; |
| 40 | static int output_recursion = 0; |
| 41 | static int net_timeout; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 42 | static uchar nc_ether[6]; /* server enet address */ |
| 43 | static IPaddr_t nc_ip; /* server ip */ |
| 44 | static short nc_port; /* source/target port */ |
| 45 | static const char *output_packet; /* used by first send udp */ |
| 46 | static int output_packet_len = 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 47 | |
| 48 | static void nc_wait_arp_handler (uchar * pkt, unsigned dest, unsigned src, |
| 49 | unsigned len) |
| 50 | { |
| 51 | NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ |
| 52 | } |
| 53 | |
| 54 | static void nc_handler (uchar * pkt, unsigned dest, unsigned src, |
| 55 | unsigned len) |
| 56 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 57 | if (input_size) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 58 | NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ |
| 59 | } |
| 60 | |
| 61 | static void nc_timeout (void) |
| 62 | { |
| 63 | NetState = NETLOOP_SUCCESS; |
| 64 | } |
| 65 | |
| 66 | void NcStart (void) |
| 67 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 68 | if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) { |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 69 | /* going to check for input packet */ |
| 70 | NetSetHandler (nc_handler); |
| 71 | NetSetTimeout (net_timeout, nc_timeout); |
| 72 | } else { |
| 73 | /* send arp request */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 74 | uchar *pkt; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 75 | NetSetHandler (nc_wait_arp_handler); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 76 | pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; |
| 77 | memcpy (pkt, output_packet, output_packet_len); |
| 78 | NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, output_packet_len); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 83 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 84 | int end, chunk; |
| 85 | |
| 86 | if (dest != nc_port || !len) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 87 | return 0; /* not for us */ |
| 88 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 89 | if (input_size == sizeof input_buffer) |
| 90 | return 1; /* no space */ |
| 91 | if (len > sizeof input_buffer - input_size) |
| 92 | len = sizeof input_buffer - input_size; |
| 93 | |
| 94 | end = input_offset + input_size; |
| 95 | if (end > sizeof input_buffer) |
| 96 | end -= sizeof input_buffer; |
| 97 | |
| 98 | chunk = len; |
| 99 | if (end + len > sizeof input_buffer) { |
| 100 | chunk = sizeof input_buffer - end; |
| 101 | memcpy(input_buffer, pkt + chunk, len - chunk); |
| 102 | } |
| 103 | memcpy (input_buffer + end, pkt, chunk); |
| 104 | |
| 105 | input_size += len; |
| 106 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | static void nc_send_packet (const char *buf, int len) |
| 111 | { |
| 112 | DECLARE_GLOBAL_DATA_PTR; |
| 113 | |
| 114 | struct eth_device *eth; |
| 115 | int inited = 0; |
| 116 | uchar *pkt; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 117 | uchar *ether; |
| 118 | IPaddr_t ip; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 119 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 120 | if ((eth = eth_get_dev ()) == NULL) { |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 121 | return; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 122 | } |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 123 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 124 | if (!memcmp (nc_ether, NetEtherNullAddr, 6)) { |
| 125 | if (eth->state == ETH_STATE_ACTIVE) |
| 126 | return; /* inside net loop */ |
| 127 | output_packet = buf; |
| 128 | output_packet_len = len; |
| 129 | NetLoop (NETCONS); /* wait for arp reply and send packet */ |
| 130 | output_packet_len = 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (eth->state != ETH_STATE_ACTIVE) { |
| 135 | if (eth_init (gd->bd) < 0) |
| 136 | return; |
| 137 | inited = 1; |
| 138 | } |
| 139 | pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; |
| 140 | memcpy (pkt, buf, len); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 141 | ether = nc_ether; |
| 142 | ip = nc_ip; |
| 143 | NetSendUDPPacket (ether, ip, nc_port, nc_port, len); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 144 | |
| 145 | if (inited) |
| 146 | eth_halt (); |
| 147 | } |
| 148 | |
| 149 | int nc_start (void) |
| 150 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 151 | int netmask, our_ip; |
| 152 | |
| 153 | nc_port = 6666; /* default port */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 154 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 155 | if (getenv ("ncip")) { |
| 156 | nc_ip = getenv_IPaddr ("ncip"); |
| 157 | if (!nc_ip) |
| 158 | return -1; /* ncip is 0.0.0.0 */ |
| 159 | char *p = strchr (getenv ("ncip"), ':'); |
| 160 | if (p) |
| 161 | nc_port = simple_strtoul (p + 1, NULL, 10); |
| 162 | } else |
| 163 | nc_ip = ~0; /* ncip is not set */ |
| 164 | |
| 165 | our_ip = getenv_IPaddr ("ipaddr"); |
| 166 | netmask = getenv_IPaddr ("netmask"); |
| 167 | |
| 168 | if (nc_ip == ~0 || /* 255.255.255.255 */ |
| 169 | ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */ |
| 170 | (netmask | nc_ip) == ~0)) /* broadcast to our net */ |
| 171 | memset (nc_ether, 0xff, sizeof nc_ether); |
| 172 | else |
| 173 | memset (nc_ether, 0, sizeof nc_ether); /* force arp request */ |
| 174 | |
| 175 | return 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void nc_putc (char c) |
| 179 | { |
| 180 | if (output_recursion) |
| 181 | return; |
| 182 | output_recursion = 1; |
| 183 | |
| 184 | nc_send_packet (&c, 1); |
| 185 | |
| 186 | output_recursion = 0; |
| 187 | } |
| 188 | |
| 189 | void nc_puts (const char *s) |
| 190 | { |
| 191 | if (output_recursion) |
| 192 | return; |
| 193 | output_recursion = 1; |
| 194 | |
| 195 | int len = strlen (s); |
| 196 | |
| 197 | if (len > 512) |
| 198 | len = 512; |
| 199 | |
| 200 | nc_send_packet (s, len); |
| 201 | |
| 202 | output_recursion = 0; |
| 203 | } |
| 204 | |
| 205 | int nc_getc (void) |
| 206 | { |
| 207 | input_recursion = 1; |
| 208 | |
| 209 | net_timeout = 0; /* no timeout */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 210 | while (!input_size) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 211 | NetLoop (NETCONS); |
| 212 | |
| 213 | input_recursion = 0; |
| 214 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 215 | uchar c = input_buffer[input_offset]; |
| 216 | input_offset++; |
| 217 | if (input_offset >= sizeof input_buffer) |
| 218 | input_offset -= sizeof input_buffer; |
| 219 | input_size--; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 220 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 221 | return c; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | int nc_tstc (void) |
| 225 | { |
| 226 | struct eth_device *eth; |
| 227 | |
| 228 | if (input_recursion) |
| 229 | return 0; |
| 230 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 231 | if (input_size) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 232 | 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; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 241 | NetLoop (NETCONS); /* kind of poll */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 242 | |
| 243 | input_recursion = 0; |
| 244 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 245 | return input_size != 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int drv_nc_init (void) |
| 249 | { |
| 250 | device_t dev; |
| 251 | 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 | |
| 263 | rc = device_register (&dev); |
| 264 | |
| 265 | return (rc == 0) ? 1 : rc; |
| 266 | } |
| 267 | |
| 268 | #endif /* CONFIG_NETCONSOLE */ |