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