netconsole.c 5.9 KB

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