netconsole.c 5.7 KB

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