netconsole.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. static char input_buffer[512];
  29. static int input_size; /* char count in input buffer */
  30. static int input_offset; /* offset to valid chars in input buffer */
  31. static int input_recursion;
  32. static int output_recursion;
  33. static int net_timeout;
  34. static uchar nc_ether[6]; /* server enet address */
  35. static IPaddr_t nc_ip; /* server ip */
  36. static short nc_out_port; /* target output port */
  37. static short nc_in_port; /* source input port */
  38. static const char *output_packet; /* used by first send udp */
  39. static int output_packet_len;
  40. static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
  41. IPaddr_t sip, unsigned src,
  42. unsigned len)
  43. {
  44. net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
  45. }
  46. static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  47. unsigned len)
  48. {
  49. if (input_size)
  50. net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
  51. }
  52. static void nc_timeout(void)
  53. {
  54. net_set_state(NETLOOP_SUCCESS);
  55. }
  56. void NcStart(void)
  57. {
  58. if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
  59. /* going to check for input packet */
  60. net_set_udp_handler(nc_handler);
  61. NetSetTimeout(net_timeout, nc_timeout);
  62. } else {
  63. /* send arp request */
  64. uchar *pkt;
  65. net_set_arp_handler(nc_wait_arp_handler);
  66. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  67. memcpy(pkt, output_packet, output_packet_len);
  68. NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
  69. output_packet_len);
  70. }
  71. }
  72. int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  73. {
  74. int end, chunk;
  75. if (dest != nc_in_port || !len)
  76. return 0; /* not for us */
  77. debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
  78. if (input_size == sizeof(input_buffer))
  79. return 1; /* no space */
  80. if (len > sizeof(input_buffer) - input_size)
  81. len = sizeof(input_buffer) - input_size;
  82. end = input_offset + input_size;
  83. if (end > sizeof(input_buffer))
  84. end -= sizeof(input_buffer);
  85. chunk = len;
  86. if (end + len > sizeof(input_buffer)) {
  87. chunk = sizeof(input_buffer) - end;
  88. memcpy(input_buffer, pkt + chunk, len - chunk);
  89. }
  90. memcpy(input_buffer + end, pkt, chunk);
  91. input_size += len;
  92. return 1;
  93. }
  94. static void nc_send_packet(const char *buf, int len)
  95. {
  96. struct eth_device *eth;
  97. int inited = 0;
  98. uchar *pkt;
  99. uchar *ether;
  100. IPaddr_t ip;
  101. debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
  102. eth = eth_get_dev();
  103. if (eth == NULL)
  104. return;
  105. if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
  106. if (eth->state == ETH_STATE_ACTIVE)
  107. return; /* inside net loop */
  108. output_packet = buf;
  109. output_packet_len = len;
  110. NetLoop(NETCONS); /* wait for arp reply and send packet */
  111. output_packet_len = 0;
  112. return;
  113. }
  114. if (eth->state != ETH_STATE_ACTIVE) {
  115. if (eth_init(gd->bd) < 0)
  116. return;
  117. inited = 1;
  118. }
  119. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  120. memcpy(pkt, buf, len);
  121. ether = nc_ether;
  122. ip = nc_ip;
  123. NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
  124. if (inited)
  125. eth_halt();
  126. }
  127. static int nc_start(void)
  128. {
  129. int netmask, our_ip;
  130. char *p;
  131. nc_out_port = 6666; /* default port */
  132. nc_in_port = nc_out_port;
  133. if (getenv("ncip")) {
  134. nc_ip = getenv_IPaddr("ncip");
  135. if (!nc_ip)
  136. return -1; /* ncip is 0.0.0.0 */
  137. p = strchr(getenv("ncip"), ':');
  138. if (p != NULL) {
  139. nc_out_port = simple_strtoul(p + 1, NULL, 10);
  140. nc_in_port = nc_out_port;
  141. }
  142. } else
  143. nc_ip = ~0; /* ncip is not set, so broadcast */
  144. p = getenv("ncoutport");
  145. if (p != NULL)
  146. nc_out_port = simple_strtoul(p, NULL, 10);
  147. p = getenv("ncinport");
  148. if (p != NULL)
  149. nc_in_port = simple_strtoul(p, NULL, 10);
  150. our_ip = getenv_IPaddr("ipaddr");
  151. netmask = getenv_IPaddr("netmask");
  152. if (nc_ip == ~0 || /* 255.255.255.255 */
  153. ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
  154. (netmask | nc_ip) == ~0)) /* broadcast to our net */
  155. memset(nc_ether, 0xff, sizeof(nc_ether));
  156. else
  157. memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
  158. /*
  159. * Initialize the static IP settings and buffer pointers
  160. * incase we call NetSendUDPPacket before NetLoop
  161. */
  162. net_init();
  163. return 0;
  164. }
  165. static void nc_putc(char c)
  166. {
  167. if (output_recursion)
  168. return;
  169. output_recursion = 1;
  170. nc_send_packet(&c, 1);
  171. output_recursion = 0;
  172. }
  173. static void nc_puts(const char *s)
  174. {
  175. int len;
  176. if (output_recursion)
  177. return;
  178. output_recursion = 1;
  179. len = strlen(s);
  180. while (len) {
  181. int send_len = min(len, 512);
  182. nc_send_packet(s, send_len);
  183. len -= send_len;
  184. s += send_len;
  185. }
  186. output_recursion = 0;
  187. }
  188. static int nc_getc(void)
  189. {
  190. uchar c;
  191. input_recursion = 1;
  192. net_timeout = 0; /* no timeout */
  193. while (!input_size)
  194. NetLoop(NETCONS);
  195. input_recursion = 0;
  196. c = input_buffer[input_offset++];
  197. if (input_offset >= sizeof(input_buffer))
  198. input_offset -= sizeof(input_buffer);
  199. input_size--;
  200. return c;
  201. }
  202. static int nc_tstc(void)
  203. {
  204. struct eth_device *eth;
  205. if (input_recursion)
  206. return 0;
  207. if (input_size)
  208. return 1;
  209. eth = eth_get_dev();
  210. if (eth && eth->state == ETH_STATE_ACTIVE)
  211. return 0; /* inside net loop */
  212. input_recursion = 1;
  213. net_timeout = 1;
  214. NetLoop(NETCONS); /* kind of poll */
  215. input_recursion = 0;
  216. return input_size != 0;
  217. }
  218. int drv_nc_init(void)
  219. {
  220. struct stdio_dev dev;
  221. int rc;
  222. memset(&dev, 0, sizeof(dev));
  223. strcpy(dev.name, "nc");
  224. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  225. dev.start = nc_start;
  226. dev.putc = nc_putc;
  227. dev.puts = nc_puts;
  228. dev.getc = nc_getc;
  229. dev.tstc = nc_tstc;
  230. rc = stdio_register(&dev);
  231. return (rc == 0) ? 1 : rc;
  232. }