netconsole.c 7.0 KB

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