netconsole.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2004
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <stdio_dev.h>
  9. #include <net.h>
  10. #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
  11. #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
  12. #endif
  13. static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
  14. static int input_size; /* char count in input buffer */
  15. static int input_offset; /* offset to valid chars in input buffer */
  16. static int input_recursion;
  17. static int output_recursion;
  18. static int net_timeout;
  19. static uchar nc_ether[6]; /* server enet address */
  20. static struct in_addr nc_ip; /* server ip */
  21. static short nc_out_port; /* target output port */
  22. static short nc_in_port; /* source input port */
  23. static const char *output_packet; /* used by first send udp */
  24. static int output_packet_len;
  25. /*
  26. * Start with a default last protocol.
  27. * We are only interested in NETCONS or not.
  28. */
  29. enum proto_t net_loop_last_protocol = BOOTP;
  30. static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
  31. struct in_addr sip, unsigned src,
  32. unsigned len)
  33. {
  34. net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
  35. }
  36. static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  37. unsigned src, unsigned len)
  38. {
  39. if (input_size)
  40. net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
  41. }
  42. static void nc_timeout_handler(void)
  43. {
  44. net_set_state(NETLOOP_SUCCESS);
  45. }
  46. static int is_broadcast(struct in_addr ip)
  47. {
  48. static struct in_addr netmask;
  49. static struct in_addr our_ip;
  50. static int env_changed_id;
  51. int env_id = get_env_id();
  52. /* update only when the environment has changed */
  53. if (env_changed_id != env_id) {
  54. netmask = env_get_ip("netmask");
  55. our_ip = env_get_ip("ipaddr");
  56. env_changed_id = env_id;
  57. }
  58. return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */
  59. ((netmask.s_addr & our_ip.s_addr) ==
  60. (netmask.s_addr & ip.s_addr) && /* on the same net and */
  61. (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */
  62. }
  63. static int refresh_settings_from_env(void)
  64. {
  65. const char *p;
  66. static int env_changed_id;
  67. int env_id = get_env_id();
  68. /* update only when the environment has changed */
  69. if (env_changed_id != env_id) {
  70. if (env_get("ncip")) {
  71. nc_ip = env_get_ip("ncip");
  72. if (!nc_ip.s_addr)
  73. return -1; /* ncip is 0.0.0.0 */
  74. p = strchr(env_get("ncip"), ':');
  75. if (p != NULL) {
  76. nc_out_port = simple_strtoul(p + 1, NULL, 10);
  77. nc_in_port = nc_out_port;
  78. }
  79. } else {
  80. nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
  81. }
  82. p = env_get("ncoutport");
  83. if (p != NULL)
  84. nc_out_port = simple_strtoul(p, NULL, 10);
  85. p = env_get("ncinport");
  86. if (p != NULL)
  87. nc_in_port = simple_strtoul(p, NULL, 10);
  88. if (is_broadcast(nc_ip))
  89. /* broadcast MAC address */
  90. memset(nc_ether, 0xff, sizeof(nc_ether));
  91. else
  92. /* force arp request */
  93. memset(nc_ether, 0, sizeof(nc_ether));
  94. }
  95. return 0;
  96. }
  97. /**
  98. * Called from net_loop in net/net.c before each packet
  99. */
  100. void nc_start(void)
  101. {
  102. refresh_settings_from_env();
  103. if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
  104. /* going to check for input packet */
  105. net_set_udp_handler(nc_handler);
  106. net_set_timeout_handler(net_timeout, nc_timeout_handler);
  107. } else {
  108. /* send arp request */
  109. uchar *pkt;
  110. net_set_arp_handler(nc_wait_arp_handler);
  111. pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
  112. IP_UDP_HDR_SIZE;
  113. memcpy(pkt, output_packet, output_packet_len);
  114. net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
  115. output_packet_len);
  116. }
  117. }
  118. int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
  119. unsigned src_port, unsigned len)
  120. {
  121. int end, chunk;
  122. if (dest_port != nc_in_port || !len)
  123. return 0; /* not for us */
  124. if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
  125. return 0; /* not from our client */
  126. debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
  127. if (input_size == sizeof(input_buffer))
  128. return 1; /* no space */
  129. if (len > sizeof(input_buffer) - input_size)
  130. len = sizeof(input_buffer) - input_size;
  131. end = input_offset + input_size;
  132. if (end >= sizeof(input_buffer))
  133. end -= sizeof(input_buffer);
  134. chunk = len;
  135. /* Check if packet will wrap in input_buffer */
  136. if (end + len >= sizeof(input_buffer)) {
  137. chunk = sizeof(input_buffer) - end;
  138. /* Copy the second part of the pkt to start of input_buffer */
  139. memcpy(input_buffer, pkt + chunk, len - chunk);
  140. }
  141. /* Copy first (or only) part of pkt after end of current valid input*/
  142. memcpy(input_buffer + end, pkt, chunk);
  143. input_size += len;
  144. return 1;
  145. }
  146. static void nc_send_packet(const char *buf, int len)
  147. {
  148. #ifdef CONFIG_DM_ETH
  149. struct udevice *eth;
  150. #else
  151. struct eth_device *eth;
  152. #endif
  153. int inited = 0;
  154. uchar *pkt;
  155. uchar *ether;
  156. struct in_addr ip;
  157. debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
  158. eth = eth_get_dev();
  159. if (eth == NULL)
  160. return;
  161. if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
  162. if (eth_is_active(eth))
  163. return; /* inside net loop */
  164. output_packet = buf;
  165. output_packet_len = len;
  166. input_recursion = 1;
  167. net_loop(NETCONS); /* wait for arp reply and send packet */
  168. input_recursion = 0;
  169. output_packet_len = 0;
  170. return;
  171. }
  172. if (!eth_is_active(eth)) {
  173. if (eth_is_on_demand_init()) {
  174. if (eth_init() < 0)
  175. return;
  176. eth_set_last_protocol(NETCONS);
  177. } else {
  178. eth_init_state_only();
  179. }
  180. inited = 1;
  181. }
  182. pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  183. memcpy(pkt, buf, len);
  184. ether = nc_ether;
  185. ip = nc_ip;
  186. net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
  187. if (inited) {
  188. if (eth_is_on_demand_init())
  189. eth_halt();
  190. else
  191. eth_halt_state_only();
  192. }
  193. }
  194. static int nc_stdio_start(struct stdio_dev *dev)
  195. {
  196. int retval;
  197. nc_out_port = 6666; /* default port */
  198. nc_in_port = nc_out_port;
  199. retval = refresh_settings_from_env();
  200. if (retval != 0)
  201. return retval;
  202. /*
  203. * Initialize the static IP settings and buffer pointers
  204. * incase we call net_send_udp_packet before net_loop
  205. */
  206. net_init();
  207. return 0;
  208. }
  209. static void nc_stdio_putc(struct stdio_dev *dev, char c)
  210. {
  211. if (output_recursion)
  212. return;
  213. output_recursion = 1;
  214. nc_send_packet(&c, 1);
  215. output_recursion = 0;
  216. }
  217. static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
  218. {
  219. int len;
  220. if (output_recursion)
  221. return;
  222. output_recursion = 1;
  223. len = strlen(s);
  224. while (len) {
  225. int send_len = min(len, (int)sizeof(input_buffer));
  226. nc_send_packet(s, send_len);
  227. len -= send_len;
  228. s += send_len;
  229. }
  230. output_recursion = 0;
  231. }
  232. static int nc_stdio_getc(struct stdio_dev *dev)
  233. {
  234. uchar c;
  235. input_recursion = 1;
  236. net_timeout = 0; /* no timeout */
  237. while (!input_size)
  238. net_loop(NETCONS);
  239. input_recursion = 0;
  240. c = input_buffer[input_offset++];
  241. if (input_offset >= sizeof(input_buffer))
  242. input_offset -= sizeof(input_buffer);
  243. input_size--;
  244. return c;
  245. }
  246. static int nc_stdio_tstc(struct stdio_dev *dev)
  247. {
  248. #ifdef CONFIG_DM_ETH
  249. struct udevice *eth;
  250. #else
  251. struct eth_device *eth;
  252. #endif
  253. if (input_recursion)
  254. return 0;
  255. if (input_size)
  256. return 1;
  257. eth = eth_get_dev();
  258. if (eth_is_active(eth))
  259. return 0; /* inside net loop */
  260. input_recursion = 1;
  261. net_timeout = 1;
  262. net_loop(NETCONS); /* kind of poll */
  263. input_recursion = 0;
  264. return input_size != 0;
  265. }
  266. int drv_nc_init(void)
  267. {
  268. struct stdio_dev dev;
  269. int rc;
  270. memset(&dev, 0, sizeof(dev));
  271. strcpy(dev.name, "nc");
  272. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  273. dev.start = nc_stdio_start;
  274. dev.putc = nc_stdio_putc;
  275. dev.puts = nc_stdio_puts;
  276. dev.getc = nc_stdio_getc;
  277. dev.tstc = nc_stdio_tstc;
  278. rc = stdio_register(&dev);
  279. return (rc == 0) ? 1 : rc;
  280. }