Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 9 | #include <env.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 10 | #include <stdio_dev.h> |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 11 | #include <net.h> |
| 12 | |
Joe Hershberger | 2c8fe51 | 2012-07-31 06:09:17 +0000 | [diff] [blame] | 13 | #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE |
| 14 | #define CONFIG_NETCONSOLE_BUFFER_SIZE 512 |
| 15 | #endif |
| 16 | |
| 17 | static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE]; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 18 | static int input_size; /* char count in input buffer */ |
| 19 | static int input_offset; /* offset to valid chars in input buffer */ |
| 20 | static int input_recursion; |
| 21 | static int output_recursion; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 22 | static int net_timeout; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 23 | static uchar nc_ether[6]; /* server enet address */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 24 | static struct in_addr nc_ip; /* server ip */ |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 25 | static short nc_out_port; /* target output port */ |
| 26 | static short nc_in_port; /* source input port */ |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 27 | static const char *output_packet; /* used by first send udp */ |
| 28 | static int output_packet_len; |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 29 | /* |
| 30 | * Start with a default last protocol. |
| 31 | * We are only interested in NETCONS or not. |
| 32 | */ |
| 33 | enum proto_t net_loop_last_protocol = BOOTP; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 34 | |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 35 | static void nc_wait_arp_handler(uchar *pkt, unsigned dest, |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 36 | struct in_addr sip, unsigned src, |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 37 | unsigned len) |
| 38 | { |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 39 | net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 42 | static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 43 | unsigned src, unsigned len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 44 | { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 45 | if (input_size) |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 46 | net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 49 | static void nc_timeout_handler(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 50 | { |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 51 | net_set_state(NETLOOP_SUCCESS); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 54 | static int is_broadcast(struct in_addr ip) |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 55 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 56 | static struct in_addr netmask; |
| 57 | static struct in_addr our_ip; |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 58 | static int env_changed_id; |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 59 | int env_id = env_get_id(); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 60 | |
| 61 | /* update only when the environment has changed */ |
| 62 | if (env_changed_id != env_id) { |
Simon Glass | 723806c | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 63 | netmask = env_get_ip("netmask"); |
| 64 | our_ip = env_get_ip("ipaddr"); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 65 | |
| 66 | env_changed_id = env_id; |
| 67 | } |
| 68 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 69 | return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */ |
| 70 | ((netmask.s_addr & our_ip.s_addr) == |
| 71 | (netmask.s_addr & ip.s_addr) && /* on the same net and */ |
| 72 | (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */ |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int refresh_settings_from_env(void) |
| 76 | { |
| 77 | const char *p; |
| 78 | static int env_changed_id; |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 79 | int env_id = env_get_id(); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 80 | |
| 81 | /* update only when the environment has changed */ |
| 82 | if (env_changed_id != env_id) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 83 | if (env_get("ncip")) { |
Simon Glass | 723806c | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 84 | nc_ip = env_get_ip("ncip"); |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 85 | if (!nc_ip.s_addr) |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 86 | return -1; /* ncip is 0.0.0.0 */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 87 | p = strchr(env_get("ncip"), ':'); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 88 | if (p != NULL) { |
| 89 | nc_out_port = simple_strtoul(p + 1, NULL, 10); |
| 90 | nc_in_port = nc_out_port; |
| 91 | } |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 92 | } else { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 93 | nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */ |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 94 | } |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 95 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 96 | p = env_get("ncoutport"); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 97 | if (p != NULL) |
| 98 | nc_out_port = simple_strtoul(p, NULL, 10); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 99 | p = env_get("ncinport"); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 100 | if (p != NULL) |
| 101 | nc_in_port = simple_strtoul(p, NULL, 10); |
| 102 | |
| 103 | if (is_broadcast(nc_ip)) |
| 104 | /* broadcast MAC address */ |
| 105 | memset(nc_ether, 0xff, sizeof(nc_ether)); |
| 106 | else |
| 107 | /* force arp request */ |
| 108 | memset(nc_ether, 0, sizeof(nc_ether)); |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /** |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 114 | * Called from net_loop in net/net.c before each packet |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 115 | */ |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 116 | void nc_start(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 117 | { |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 118 | refresh_settings_from_env(); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 119 | if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) { |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 120 | /* going to check for input packet */ |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 121 | net_set_udp_handler(nc_handler); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 122 | net_set_timeout_handler(net_timeout, nc_timeout_handler); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 123 | } else { |
| 124 | /* send arp request */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 125 | uchar *pkt; |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 126 | net_set_arp_handler(nc_wait_arp_handler); |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 127 | pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + |
| 128 | IP_UDP_HDR_SIZE; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 129 | memcpy(pkt, output_packet, output_packet_len); |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 130 | net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port, |
| 131 | output_packet_len); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Joe Hershberger | 049a95a | 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 | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 136 | unsigned src_port, unsigned len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 137 | { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 138 | int end, chunk; |
| 139 | |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 140 | if (dest_port != nc_in_port || !len) |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 141 | return 0; /* not for us */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 142 | |
Joe Hershberger | 049a95a | 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 | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 144 | return 0; /* not from our client */ |
| 145 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 146 | debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt); |
| 147 | |
Joe Hershberger | e1902ac | 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 | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 152 | |
| 153 | end = input_offset + input_size; |
Joe Hershberger | 3cacc6a | 2017-08-30 17:32:31 -0500 | [diff] [blame] | 154 | if (end >= sizeof(input_buffer)) |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 155 | end -= sizeof(input_buffer); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 156 | |
| 157 | chunk = len; |
Joe Hershberger | 3cacc6a | 2017-08-30 17:32:31 -0500 | [diff] [blame] | 158 | /* Check if packet will wrap in input_buffer */ |
| 159 | if (end + len >= sizeof(input_buffer)) { |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 160 | chunk = sizeof(input_buffer) - end; |
Joe Hershberger | 3cacc6a | 2017-08-30 17:32:31 -0500 | [diff] [blame] | 161 | /* Copy the second part of the pkt to start of input_buffer */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 162 | memcpy(input_buffer, pkt + chunk, len - chunk); |
| 163 | } |
Joe Hershberger | 3cacc6a | 2017-08-30 17:32:31 -0500 | [diff] [blame] | 164 | /* Copy first (or only) part of pkt after end of current valid input*/ |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 165 | memcpy(input_buffer + end, pkt, chunk); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 166 | |
| 167 | input_size += len; |
| 168 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | } |
| 171 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 172 | static void nc_send_packet(const char *buf, int len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 173 | { |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 174 | #ifdef CONFIG_DM_ETH |
| 175 | struct udevice *eth; |
| 176 | #else |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 177 | struct eth_device *eth; |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 178 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 179 | int inited = 0; |
| 180 | uchar *pkt; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 181 | uchar *ether; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 182 | struct in_addr ip; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 183 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 184 | debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf); |
| 185 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 186 | eth = eth_get_dev(); |
| 187 | if (eth == NULL) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 188 | return; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 189 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 190 | if (!memcmp(nc_ether, net_null_ethaddr, 6)) { |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 191 | if (eth_is_active(eth)) |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 192 | return; /* inside net loop */ |
| 193 | output_packet = buf; |
| 194 | output_packet_len = len; |
Suriyan Ramasami | efd9bb9 | 2013-10-16 09:54:24 -0700 | [diff] [blame] | 195 | input_recursion = 1; |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 196 | net_loop(NETCONS); /* wait for arp reply and send packet */ |
Suriyan Ramasami | efd9bb9 | 2013-10-16 09:54:24 -0700 | [diff] [blame] | 197 | input_recursion = 0; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 198 | output_packet_len = 0; |
| 199 | return; |
| 200 | } |
| 201 | |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 202 | if (!eth_is_active(eth)) { |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 203 | if (eth_is_on_demand_init()) { |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 204 | if (eth_init() < 0) |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 205 | return; |
| 206 | eth_set_last_protocol(NETCONS); |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 207 | } else { |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 208 | eth_init_state_only(); |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 209 | } |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 210 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 211 | inited = 1; |
| 212 | } |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 213 | pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 214 | memcpy(pkt, buf, len); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 215 | ether = nc_ether; |
| 216 | ip = nc_ip; |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 217 | net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 218 | |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 219 | if (inited) { |
| 220 | if (eth_is_on_demand_init()) |
| 221 | eth_halt(); |
| 222 | else |
| 223 | eth_halt_state_only(); |
| 224 | } |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 227 | static int nc_stdio_start(struct stdio_dev *dev) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 228 | { |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 229 | int retval; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 230 | |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 231 | nc_out_port = 6666; /* default port */ |
| 232 | nc_in_port = nc_out_port; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 233 | |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 234 | retval = refresh_settings_from_env(); |
| 235 | if (retval != 0) |
| 236 | return retval; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 237 | |
Joe Hershberger | d7310c7 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Initialize the static IP settings and buffer pointers |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 240 | * incase we call net_send_udp_packet before net_loop |
Joe Hershberger | d7310c7 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 241 | */ |
| 242 | net_init(); |
| 243 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 244 | return 0; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 247 | static void nc_stdio_putc(struct stdio_dev *dev, char c) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 248 | { |
| 249 | if (output_recursion) |
| 250 | return; |
| 251 | output_recursion = 1; |
| 252 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 253 | nc_send_packet(&c, 1); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 254 | |
| 255 | output_recursion = 0; |
| 256 | } |
| 257 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 258 | static void nc_stdio_puts(struct stdio_dev *dev, const char *s) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 259 | { |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 260 | int len; |
| 261 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 262 | if (output_recursion) |
| 263 | return; |
| 264 | output_recursion = 1; |
| 265 | |
Michael Walle | 1a9845b | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 266 | len = strlen(s); |
| 267 | while (len) { |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 268 | int send_len = min(len, (int)sizeof(input_buffer)); |
Michael Walle | 1a9845b | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 269 | nc_send_packet(s, send_len); |
| 270 | len -= send_len; |
| 271 | s += send_len; |
| 272 | } |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 273 | |
| 274 | output_recursion = 0; |
| 275 | } |
| 276 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 277 | static int nc_stdio_getc(struct stdio_dev *dev) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 278 | { |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 279 | uchar c; |
| 280 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 281 | input_recursion = 1; |
| 282 | |
| 283 | net_timeout = 0; /* no timeout */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 284 | while (!input_size) |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 285 | net_loop(NETCONS); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 286 | |
| 287 | input_recursion = 0; |
| 288 | |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 289 | c = input_buffer[input_offset++]; |
| 290 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 291 | if (input_offset >= sizeof(input_buffer)) |
| 292 | input_offset -= sizeof(input_buffer); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 293 | input_size--; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 294 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 295 | return c; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 298 | static int nc_stdio_tstc(struct stdio_dev *dev) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 299 | { |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 300 | #ifdef CONFIG_DM_ETH |
| 301 | struct udevice *eth; |
| 302 | #else |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 303 | struct eth_device *eth; |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 304 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 305 | |
| 306 | if (input_recursion) |
| 307 | return 0; |
| 308 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 309 | if (input_size) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 310 | return 1; |
| 311 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 312 | eth = eth_get_dev(); |
Bernhard Nortmann | c163e43 | 2015-09-14 15:29:44 +0200 | [diff] [blame] | 313 | if (eth_is_active(eth)) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 314 | return 0; /* inside net loop */ |
| 315 | |
| 316 | input_recursion = 1; |
| 317 | |
| 318 | net_timeout = 1; |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 319 | net_loop(NETCONS); /* kind of poll */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 320 | |
| 321 | input_recursion = 0; |
| 322 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 323 | return input_size != 0; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 326 | int drv_nc_init(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 327 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 328 | struct stdio_dev dev; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 329 | int rc; |
| 330 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 331 | memset(&dev, 0, sizeof(dev)); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 332 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 333 | strcpy(dev.name, "nc"); |
Bin Meng | 1caf934 | 2015-11-03 23:23:37 -0800 | [diff] [blame] | 334 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 335 | dev.start = nc_stdio_start; |
| 336 | dev.putc = nc_stdio_putc; |
| 337 | dev.puts = nc_stdio_puts; |
| 338 | dev.getc = nc_stdio_getc; |
| 339 | dev.tstc = nc_stdio_tstc; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 340 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 341 | rc = stdio_register(&dev); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 342 | |
| 343 | return (rc == 0) ? 1 : rc; |
| 344 | } |