netconsole.c 6.0 KB

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