netconsole.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <stdio_dev.h>
  26. #include <net.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
  29. #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
  30. #endif
  31. static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
  32. static int input_size; /* char count in input buffer */
  33. static int input_offset; /* offset to valid chars in input buffer */
  34. static int input_recursion;
  35. static int output_recursion;
  36. static int net_timeout;
  37. static uchar nc_ether[6]; /* server enet address */
  38. static IPaddr_t nc_ip; /* server ip */
  39. static short nc_out_port; /* target output port */
  40. static short nc_in_port; /* source input port */
  41. static const char *output_packet; /* used by first send udp */
  42. static int output_packet_len;
  43. static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
  44. IPaddr_t sip, unsigned src,
  45. unsigned len)
  46. {
  47. net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
  48. }
  49. static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  50. unsigned len)
  51. {
  52. if (input_size)
  53. net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
  54. }
  55. static void nc_timeout(void)
  56. {
  57. net_set_state(NETLOOP_SUCCESS);
  58. }
  59. void NcStart(void)
  60. {
  61. if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
  62. /* going to check for input packet */
  63. net_set_udp_handler(nc_handler);
  64. NetSetTimeout(net_timeout, nc_timeout);
  65. } else {
  66. /* send arp request */
  67. uchar *pkt;
  68. net_set_arp_handler(nc_wait_arp_handler);
  69. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  70. memcpy(pkt, output_packet, output_packet_len);
  71. NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
  72. output_packet_len);
  73. }
  74. }
  75. int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  76. {
  77. int end, chunk;
  78. if (dest != nc_in_port || !len)
  79. return 0; /* not for us */
  80. debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
  81. if (input_size == sizeof(input_buffer))
  82. return 1; /* no space */
  83. if (len > sizeof(input_buffer) - input_size)
  84. len = sizeof(input_buffer) - input_size;
  85. end = input_offset + input_size;
  86. if (end > sizeof(input_buffer))
  87. end -= sizeof(input_buffer);
  88. chunk = len;
  89. if (end + len > sizeof(input_buffer)) {
  90. chunk = sizeof(input_buffer) - end;
  91. memcpy(input_buffer, pkt + chunk, len - chunk);
  92. }
  93. memcpy(input_buffer + end, pkt, chunk);
  94. input_size += len;
  95. return 1;
  96. }
  97. static void nc_send_packet(const char *buf, int len)
  98. {
  99. struct eth_device *eth;
  100. int inited = 0;
  101. uchar *pkt;
  102. uchar *ether;
  103. IPaddr_t ip;
  104. debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
  105. eth = eth_get_dev();
  106. if (eth == NULL)
  107. return;
  108. if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
  109. if (eth->state == ETH_STATE_ACTIVE)
  110. return; /* inside net loop */
  111. output_packet = buf;
  112. output_packet_len = len;
  113. NetLoop(NETCONS); /* wait for arp reply and send packet */
  114. output_packet_len = 0;
  115. return;
  116. }
  117. if (eth->state != ETH_STATE_ACTIVE) {
  118. if (eth_init(gd->bd) < 0)
  119. return;
  120. inited = 1;
  121. }
  122. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  123. memcpy(pkt, buf, len);
  124. ether = nc_ether;
  125. ip = nc_ip;
  126. NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
  127. if (inited)
  128. eth_halt();
  129. }
  130. static int nc_start(void)
  131. {
  132. int netmask, our_ip;
  133. char *p;
  134. nc_out_port = 6666; /* default port */
  135. nc_in_port = nc_out_port;
  136. if (getenv("ncip")) {
  137. nc_ip = getenv_IPaddr("ncip");
  138. if (!nc_ip)
  139. return -1; /* ncip is 0.0.0.0 */
  140. p = strchr(getenv("ncip"), ':');
  141. if (p != NULL) {
  142. nc_out_port = simple_strtoul(p + 1, NULL, 10);
  143. nc_in_port = nc_out_port;
  144. }
  145. } else
  146. nc_ip = ~0; /* ncip is not set, so broadcast */
  147. p = getenv("ncoutport");
  148. if (p != NULL)
  149. nc_out_port = simple_strtoul(p, NULL, 10);
  150. p = getenv("ncinport");
  151. if (p != NULL)
  152. nc_in_port = simple_strtoul(p, NULL, 10);
  153. our_ip = getenv_IPaddr("ipaddr");
  154. netmask = getenv_IPaddr("netmask");
  155. if (nc_ip == ~0 || /* 255.255.255.255 */
  156. ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
  157. (netmask | nc_ip) == ~0)) /* broadcast to our net */
  158. memset(nc_ether, 0xff, sizeof(nc_ether));
  159. else
  160. memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
  161. /*
  162. * Initialize the static IP settings and buffer pointers
  163. * incase we call NetSendUDPPacket before NetLoop
  164. */
  165. net_init();
  166. return 0;
  167. }
  168. static void nc_putc(char c)
  169. {
  170. if (output_recursion)
  171. return;
  172. output_recursion = 1;
  173. nc_send_packet(&c, 1);
  174. output_recursion = 0;
  175. }
  176. static void nc_puts(const char *s)
  177. {
  178. int len;
  179. if (output_recursion)
  180. return;
  181. output_recursion = 1;
  182. len = strlen(s);
  183. while (len) {
  184. int send_len = min(len, sizeof(input_buffer));
  185. nc_send_packet(s, send_len);
  186. len -= send_len;
  187. s += send_len;
  188. }
  189. output_recursion = 0;
  190. }
  191. static int nc_getc(void)
  192. {
  193. uchar c;
  194. input_recursion = 1;
  195. net_timeout = 0; /* no timeout */
  196. while (!input_size)
  197. NetLoop(NETCONS);
  198. input_recursion = 0;
  199. c = input_buffer[input_offset++];
  200. if (input_offset >= sizeof(input_buffer))
  201. input_offset -= sizeof(input_buffer);
  202. input_size--;
  203. return c;
  204. }
  205. static int nc_tstc(void)
  206. {
  207. struct eth_device *eth;
  208. if (input_recursion)
  209. return 0;
  210. if (input_size)
  211. return 1;
  212. eth = eth_get_dev();
  213. if (eth && eth->state == ETH_STATE_ACTIVE)
  214. return 0; /* inside net loop */
  215. input_recursion = 1;
  216. net_timeout = 1;
  217. NetLoop(NETCONS); /* kind of poll */
  218. input_recursion = 0;
  219. return input_size != 0;
  220. }
  221. int drv_nc_init(void)
  222. {
  223. struct stdio_dev dev;
  224. int rc;
  225. memset(&dev, 0, sizeof(dev));
  226. strcpy(dev.name, "nc");
  227. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  228. dev.start = nc_start;
  229. dev.putc = nc_putc;
  230. dev.puts = nc_puts;
  231. dev.getc = nc_getc;
  232. dev.tstc = nc_tstc;
  233. rc = stdio_register(&dev);
  234. return (rc == 0) ? 1 : rc;
  235. }