netconsole.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <stdio_dev.h>
  10. #include <net.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  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(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 = get_env_id();
  54. /* update only when the environment has changed */
  55. if (env_changed_id != env_id) {
  56. netmask = getenv_ip("netmask");
  57. our_ip = getenv_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 = get_env_id();
  70. /* update only when the environment has changed */
  71. if (env_changed_id != env_id) {
  72. if (getenv("ncip")) {
  73. nc_ip = getenv_ip("ncip");
  74. if (!nc_ip.s_addr)
  75. return -1; /* ncip is 0.0.0.0 */
  76. p = strchr(getenv("ncip"), ':');
  77. if (p != NULL) {
  78. nc_out_port = simple_strtoul(p + 1, NULL, 10);
  79. nc_in_port = nc_out_port;
  80. }
  81. } else
  82. nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
  83. p = getenv("ncoutport");
  84. if (p != NULL)
  85. nc_out_port = simple_strtoul(p, NULL, 10);
  86. p = getenv("ncinport");
  87. if (p != NULL)
  88. nc_in_port = simple_strtoul(p, NULL, 10);
  89. if (is_broadcast(nc_ip))
  90. /* broadcast MAC address */
  91. memset(nc_ether, 0xff, sizeof(nc_ether));
  92. else
  93. /* force arp request */
  94. memset(nc_ether, 0, sizeof(nc_ether));
  95. }
  96. return 0;
  97. }
  98. /**
  99. * Called from NetLoop in net/net.c before each packet
  100. */
  101. void NcStart(void)
  102. {
  103. refresh_settings_from_env();
  104. if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
  105. /* going to check for input packet */
  106. net_set_udp_handler(nc_handler);
  107. NetSetTimeout(net_timeout, nc_timeout);
  108. } else {
  109. /* send arp request */
  110. uchar *pkt;
  111. net_set_arp_handler(nc_wait_arp_handler);
  112. pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
  113. IP_UDP_HDR_SIZE;
  114. memcpy(pkt, output_packet, output_packet_len);
  115. net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
  116. output_packet_len);
  117. }
  118. }
  119. int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
  120. unsigned src_port, unsigned len)
  121. {
  122. int end, chunk;
  123. if (dest_port != nc_in_port || !len)
  124. return 0; /* not for us */
  125. if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
  126. return 0; /* not from our client */
  127. debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
  128. if (input_size == sizeof(input_buffer))
  129. return 1; /* no space */
  130. if (len > sizeof(input_buffer) - input_size)
  131. len = sizeof(input_buffer) - input_size;
  132. end = input_offset + input_size;
  133. if (end > sizeof(input_buffer))
  134. end -= sizeof(input_buffer);
  135. chunk = len;
  136. if (end + len > sizeof(input_buffer)) {
  137. chunk = sizeof(input_buffer) - end;
  138. memcpy(input_buffer, pkt + chunk, len - chunk);
  139. }
  140. memcpy(input_buffer + end, pkt, chunk);
  141. input_size += len;
  142. return 1;
  143. }
  144. static void nc_send_packet(const char *buf, int len)
  145. {
  146. struct eth_device *eth;
  147. int inited = 0;
  148. uchar *pkt;
  149. uchar *ether;
  150. struct in_addr ip;
  151. debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
  152. eth = eth_get_dev();
  153. if (eth == NULL)
  154. return;
  155. if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
  156. if (eth->state == ETH_STATE_ACTIVE)
  157. return; /* inside net loop */
  158. output_packet = buf;
  159. output_packet_len = len;
  160. input_recursion = 1;
  161. NetLoop(NETCONS); /* wait for arp reply and send packet */
  162. input_recursion = 0;
  163. output_packet_len = 0;
  164. return;
  165. }
  166. if (eth->state != ETH_STATE_ACTIVE) {
  167. if (eth_is_on_demand_init()) {
  168. if (eth_init() < 0)
  169. return;
  170. eth_set_last_protocol(NETCONS);
  171. } else
  172. eth_init_state_only();
  173. inited = 1;
  174. }
  175. pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  176. memcpy(pkt, buf, len);
  177. ether = nc_ether;
  178. ip = nc_ip;
  179. net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
  180. if (inited) {
  181. if (eth_is_on_demand_init())
  182. eth_halt();
  183. else
  184. eth_halt_state_only();
  185. }
  186. }
  187. static int nc_start(struct stdio_dev *dev)
  188. {
  189. int retval;
  190. nc_out_port = 6666; /* default port */
  191. nc_in_port = nc_out_port;
  192. retval = refresh_settings_from_env();
  193. if (retval != 0)
  194. return retval;
  195. /*
  196. * Initialize the static IP settings and buffer pointers
  197. * incase we call net_send_udp_packet before NetLoop
  198. */
  199. net_init();
  200. return 0;
  201. }
  202. static void nc_putc(struct stdio_dev *dev, char c)
  203. {
  204. if (output_recursion)
  205. return;
  206. output_recursion = 1;
  207. nc_send_packet(&c, 1);
  208. output_recursion = 0;
  209. }
  210. static void nc_puts(struct stdio_dev *dev, const char *s)
  211. {
  212. int len;
  213. if (output_recursion)
  214. return;
  215. output_recursion = 1;
  216. len = strlen(s);
  217. while (len) {
  218. int send_len = min(len, (int)sizeof(input_buffer));
  219. nc_send_packet(s, send_len);
  220. len -= send_len;
  221. s += send_len;
  222. }
  223. output_recursion = 0;
  224. }
  225. static int nc_getc(struct stdio_dev *dev)
  226. {
  227. uchar c;
  228. input_recursion = 1;
  229. net_timeout = 0; /* no timeout */
  230. while (!input_size)
  231. NetLoop(NETCONS);
  232. input_recursion = 0;
  233. c = input_buffer[input_offset++];
  234. if (input_offset >= sizeof(input_buffer))
  235. input_offset -= sizeof(input_buffer);
  236. input_size--;
  237. return c;
  238. }
  239. static int nc_tstc(struct stdio_dev *dev)
  240. {
  241. struct eth_device *eth;
  242. if (input_recursion)
  243. return 0;
  244. if (input_size)
  245. return 1;
  246. eth = eth_get_dev();
  247. if (eth && eth->state == ETH_STATE_ACTIVE)
  248. return 0; /* inside net loop */
  249. input_recursion = 1;
  250. net_timeout = 1;
  251. NetLoop(NETCONS); /* kind of poll */
  252. input_recursion = 0;
  253. return input_size != 0;
  254. }
  255. int drv_nc_init(void)
  256. {
  257. struct stdio_dev dev;
  258. int rc;
  259. memset(&dev, 0, sizeof(dev));
  260. strcpy(dev.name, "nc");
  261. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  262. dev.start = nc_start;
  263. dev.putc = nc_putc;
  264. dev.puts = nc_puts;
  265. dev.getc = nc_getc;
  266. dev.tstc = nc_tstc;
  267. rc = stdio_register(&dev);
  268. return (rc == 0) ? 1 : rc;
  269. }