netconsole.c 7.5 KB

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