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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 9 | #include <command.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 10 | #include <stdio_dev.h> |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 11 | #include <net.h> |
| 12 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
Joe Hershberger | 45f4a23 | 2012-07-31 06:09:17 +0000 | [diff] [blame] | 15 | #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE |
| 16 | #define CONFIG_NETCONSOLE_BUFFER_SIZE 512 |
| 17 | #endif |
| 18 | |
| 19 | static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE]; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 20 | static int input_size; /* char count in input buffer */ |
| 21 | static int input_offset; /* offset to valid chars in input buffer */ |
| 22 | static int input_recursion; |
| 23 | static int output_recursion; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 24 | static int net_timeout; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 25 | static uchar nc_ether[6]; /* server enet address */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 26 | static struct in_addr nc_ip; /* server ip */ |
Joe Hershberger | d0f5396 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 27 | static short nc_out_port; /* target output port */ |
| 28 | static short nc_in_port; /* source input port */ |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 29 | static const char *output_packet; /* used by first send udp */ |
| 30 | static int output_packet_len; |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Start with a default last protocol. |
| 33 | * We are only interested in NETCONS or not. |
| 34 | */ |
| 35 | enum proto_t net_loop_last_protocol = BOOTP; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 36 | |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 37 | static void nc_wait_arp_handler(uchar *pkt, unsigned dest, |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 38 | struct in_addr sip, unsigned src, |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 39 | unsigned len) |
| 40 | { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 41 | net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 44 | static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 45 | unsigned src, unsigned len) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 46 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 47 | if (input_size) |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 48 | net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 51 | static void nc_timeout(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 52 | { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 53 | net_set_state(NETLOOP_SUCCESS); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 56 | static int is_broadcast(struct in_addr ip) |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 57 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 58 | static struct in_addr netmask; |
| 59 | static struct in_addr our_ip; |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 60 | static int env_changed_id; |
| 61 | int env_id = get_env_id(); |
| 62 | |
| 63 | /* update only when the environment has changed */ |
| 64 | if (env_changed_id != env_id) { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 65 | netmask = getenv_ip("netmask"); |
| 66 | our_ip = getenv_ip("ipaddr"); |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 67 | |
| 68 | env_changed_id = env_id; |
| 69 | } |
| 70 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 71 | return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */ |
| 72 | ((netmask.s_addr & our_ip.s_addr) == |
| 73 | (netmask.s_addr & ip.s_addr) && /* on the same net and */ |
| 74 | (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */ |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int refresh_settings_from_env(void) |
| 78 | { |
| 79 | const char *p; |
| 80 | static int env_changed_id; |
| 81 | int env_id = get_env_id(); |
| 82 | |
| 83 | /* update only when the environment has changed */ |
| 84 | if (env_changed_id != env_id) { |
| 85 | if (getenv("ncip")) { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 86 | nc_ip = getenv_ip("ncip"); |
| 87 | if (!nc_ip.s_addr) |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 88 | return -1; /* ncip is 0.0.0.0 */ |
| 89 | p = strchr(getenv("ncip"), ':'); |
| 90 | if (p != NULL) { |
| 91 | nc_out_port = simple_strtoul(p + 1, NULL, 10); |
| 92 | nc_in_port = nc_out_port; |
| 93 | } |
| 94 | } else |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 95 | nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */ |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 96 | |
| 97 | p = getenv("ncoutport"); |
| 98 | if (p != NULL) |
| 99 | nc_out_port = simple_strtoul(p, NULL, 10); |
| 100 | p = getenv("ncinport"); |
| 101 | if (p != NULL) |
| 102 | nc_in_port = simple_strtoul(p, NULL, 10); |
| 103 | |
| 104 | if (is_broadcast(nc_ip)) |
| 105 | /* broadcast MAC address */ |
| 106 | memset(nc_ether, 0xff, sizeof(nc_ether)); |
| 107 | else |
| 108 | /* force arp request */ |
| 109 | memset(nc_ether, 0, sizeof(nc_ether)); |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Called from NetLoop in net/net.c before each packet |
| 116 | */ |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 117 | void NcStart(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 118 | { |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 119 | refresh_settings_from_env(); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame^] | 120 | if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) { |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 121 | /* going to check for input packet */ |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 122 | net_set_udp_handler(nc_handler); |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 123 | NetSetTimeout(net_timeout, nc_timeout); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 124 | } else { |
| 125 | /* send arp request */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 126 | uchar *pkt; |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 127 | net_set_arp_handler(nc_wait_arp_handler); |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 128 | pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 129 | memcpy(pkt, output_packet, output_packet_len); |
Joe Hershberger | d0f5396 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 130 | NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port, |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 131 | output_packet_len); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 135 | int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port, |
Joe Hershberger | 58cd218 | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 136 | unsigned src_port, unsigned len) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 137 | { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 138 | int end, chunk; |
| 139 | |
Joe Hershberger | 58cd218 | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 140 | if (dest_port != nc_in_port || !len) |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 141 | return 0; /* not for us */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 142 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 143 | if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip)) |
Joe Hershberger | 58cd218 | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 144 | return 0; /* not from our client */ |
| 145 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 146 | debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt); |
| 147 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 148 | if (input_size == sizeof(input_buffer)) |
| 149 | return 1; /* no space */ |
| 150 | if (len > sizeof(input_buffer) - input_size) |
| 151 | len = sizeof(input_buffer) - input_size; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 152 | |
| 153 | end = input_offset + input_size; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 154 | if (end > sizeof(input_buffer)) |
| 155 | end -= sizeof(input_buffer); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 156 | |
| 157 | chunk = len; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 158 | if (end + len > sizeof(input_buffer)) { |
| 159 | chunk = sizeof(input_buffer) - end; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 160 | memcpy(input_buffer, pkt + chunk, len - chunk); |
| 161 | } |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 162 | memcpy(input_buffer + end, pkt, chunk); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 163 | |
| 164 | input_size += len; |
| 165 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 169 | static void nc_send_packet(const char *buf, int len) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 170 | { |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 171 | struct eth_device *eth; |
| 172 | int inited = 0; |
| 173 | uchar *pkt; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 174 | uchar *ether; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 175 | struct in_addr ip; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 176 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 177 | debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf); |
| 178 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 179 | eth = eth_get_dev(); |
| 180 | if (eth == NULL) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 181 | return; |
| 182 | |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame^] | 183 | if (!memcmp(nc_ether, net_null_ethaddr, 6)) { |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 184 | if (eth->state == ETH_STATE_ACTIVE) |
| 185 | return; /* inside net loop */ |
| 186 | output_packet = buf; |
| 187 | output_packet_len = len; |
Suriyan Ramasami | 7f37f22 | 2013-10-16 09:54:24 -0700 | [diff] [blame] | 188 | input_recursion = 1; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 189 | NetLoop(NETCONS); /* wait for arp reply and send packet */ |
Suriyan Ramasami | 7f37f22 | 2013-10-16 09:54:24 -0700 | [diff] [blame] | 190 | input_recursion = 0; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 191 | output_packet_len = 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
| 195 | if (eth->state != ETH_STATE_ACTIVE) { |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 196 | if (eth_is_on_demand_init()) { |
Joe Hershberger | 3dbe17e | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 197 | if (eth_init() < 0) |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 198 | return; |
| 199 | eth_set_last_protocol(NETCONS); |
| 200 | } else |
Joe Hershberger | 3dbe17e | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 201 | eth_init_state_only(); |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 202 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 203 | inited = 1; |
| 204 | } |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 205 | pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 206 | memcpy(pkt, buf, len); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 207 | ether = nc_ether; |
| 208 | ip = nc_ip; |
Joe Hershberger | d0f5396 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 209 | NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 210 | |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 211 | if (inited) { |
| 212 | if (eth_is_on_demand_init()) |
| 213 | eth_halt(); |
| 214 | else |
| 215 | eth_halt_state_only(); |
| 216 | } |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 219 | static int nc_start(struct stdio_dev *dev) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 220 | { |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 221 | int retval; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 222 | |
Joe Hershberger | d0f5396 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 223 | nc_out_port = 6666; /* default port */ |
| 224 | nc_in_port = nc_out_port; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 225 | |
Joe Hershberger | 6625780 | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 226 | retval = refresh_settings_from_env(); |
| 227 | if (retval != 0) |
| 228 | return retval; |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 229 | |
Joe Hershberger | 6a9ed16 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 230 | /* |
| 231 | * Initialize the static IP settings and buffer pointers |
| 232 | * incase we call NetSendUDPPacket before NetLoop |
| 233 | */ |
| 234 | net_init(); |
| 235 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 236 | return 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 239 | static void nc_putc(struct stdio_dev *dev, char c) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 240 | { |
| 241 | if (output_recursion) |
| 242 | return; |
| 243 | output_recursion = 1; |
| 244 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 245 | nc_send_packet(&c, 1); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 246 | |
| 247 | output_recursion = 0; |
| 248 | } |
| 249 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 250 | static void nc_puts(struct stdio_dev *dev, const char *s) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 251 | { |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 252 | int len; |
| 253 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 254 | if (output_recursion) |
| 255 | return; |
| 256 | output_recursion = 1; |
| 257 | |
Michael Walle | 9227aed | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 258 | len = strlen(s); |
| 259 | while (len) { |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 260 | int send_len = min(len, (int)sizeof(input_buffer)); |
Michael Walle | 9227aed | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 261 | nc_send_packet(s, send_len); |
| 262 | len -= send_len; |
| 263 | s += send_len; |
| 264 | } |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 265 | |
| 266 | output_recursion = 0; |
| 267 | } |
| 268 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 269 | static int nc_getc(struct stdio_dev *dev) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 270 | { |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 271 | uchar c; |
| 272 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 273 | input_recursion = 1; |
| 274 | |
| 275 | net_timeout = 0; /* no timeout */ |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 276 | while (!input_size) |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 277 | NetLoop(NETCONS); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 278 | |
| 279 | input_recursion = 0; |
| 280 | |
wdenk | b21608e | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 281 | c = input_buffer[input_offset++]; |
| 282 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 283 | if (input_offset >= sizeof(input_buffer)) |
| 284 | input_offset -= sizeof(input_buffer); |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 285 | input_size--; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 286 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 287 | return c; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 290 | static int nc_tstc(struct stdio_dev *dev) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 291 | { |
| 292 | struct eth_device *eth; |
| 293 | |
| 294 | if (input_recursion) |
| 295 | return 0; |
| 296 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 297 | if (input_size) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 298 | return 1; |
| 299 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 300 | eth = eth_get_dev(); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 301 | if (eth && eth->state == ETH_STATE_ACTIVE) |
| 302 | return 0; /* inside net loop */ |
| 303 | |
| 304 | input_recursion = 1; |
| 305 | |
| 306 | net_timeout = 1; |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 307 | NetLoop(NETCONS); /* kind of poll */ |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 308 | |
| 309 | input_recursion = 0; |
| 310 | |
wdenk | a0ebde5 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 311 | return input_size != 0; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 314 | int drv_nc_init(void) |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 315 | { |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 316 | struct stdio_dev dev; |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 317 | int rc; |
| 318 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 319 | memset(&dev, 0, sizeof(dev)); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 320 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 321 | strcpy(dev.name, "nc"); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 322 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 323 | dev.start = nc_start; |
| 324 | dev.putc = nc_putc; |
| 325 | dev.puts = nc_puts; |
| 326 | dev.getc = nc_getc; |
| 327 | dev.tstc = nc_tstc; |
| 328 | |
Joe Hershberger | 107b3d0 | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 329 | rc = stdio_register(&dev); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 330 | |
| 331 | return (rc == 0) ? 1 : rc; |
| 332 | } |