netconsole.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #ifndef CONFIG_NET_MULTI
  29. #error define CONFIG_NET_MULTI to use netconsole
  30. #endif
  31. static char input_buffer[512];
  32. static int input_size = 0; /* char count in input buffer */
  33. static int input_offset = 0; /* offset to valid chars in input buffer */
  34. static int input_recursion = 0;
  35. static int output_recursion = 0;
  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_port; /* source/target port */
  40. static const char *output_packet; /* used by first send udp */
  41. static int output_packet_len = 0;
  42. static void nc_wait_arp_handler (uchar * pkt, unsigned dest, unsigned src,
  43. unsigned len)
  44. {
  45. NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
  46. }
  47. static void nc_handler (uchar * pkt, unsigned dest, unsigned src,
  48. unsigned len)
  49. {
  50. if (input_size)
  51. NetState = NETLOOP_SUCCESS; /* got input - quit net loop */
  52. }
  53. static void nc_timeout (void)
  54. {
  55. NetState = NETLOOP_SUCCESS;
  56. }
  57. void NcStart (void)
  58. {
  59. if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) {
  60. /* going to check for input packet */
  61. NetSetHandler (nc_handler);
  62. NetSetTimeout (net_timeout, nc_timeout);
  63. } else {
  64. /* send arp request */
  65. uchar *pkt;
  66. NetSetHandler (nc_wait_arp_handler);
  67. pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE;
  68. memcpy (pkt, output_packet, output_packet_len);
  69. NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, 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_port || !len)
  76. return 0; /* not for us */
  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. DECLARE_GLOBAL_DATA_PTR;
  96. struct eth_device *eth;
  97. int inited = 0;
  98. uchar *pkt;
  99. uchar *ether;
  100. IPaddr_t ip;
  101. if ((eth = eth_get_dev ()) == NULL) {
  102. return;
  103. }
  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_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. 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. if ((p = strchr (getenv ("ncip"), ':')) != NULL)
  136. nc_port = simple_strtoul (p + 1, NULL, 10);
  137. } else
  138. nc_ip = ~0; /* ncip is not set */
  139. our_ip = getenv_IPaddr ("ipaddr");
  140. netmask = getenv_IPaddr ("netmask");
  141. if (nc_ip == ~0 || /* 255.255.255.255 */
  142. ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
  143. (netmask | nc_ip) == ~0)) /* broadcast to our net */
  144. memset (nc_ether, 0xff, sizeof nc_ether);
  145. else
  146. memset (nc_ether, 0, sizeof nc_ether); /* force arp request */
  147. return 0;
  148. }
  149. void nc_putc (char c)
  150. {
  151. if (output_recursion)
  152. return;
  153. output_recursion = 1;
  154. nc_send_packet (&c, 1);
  155. output_recursion = 0;
  156. }
  157. void nc_puts (const char *s)
  158. {
  159. int len;
  160. if (output_recursion)
  161. return;
  162. output_recursion = 1;
  163. if ((len = strlen (s)) > 512)
  164. len = 512;
  165. nc_send_packet (s, len);
  166. output_recursion = 0;
  167. }
  168. int nc_getc (void)
  169. {
  170. uchar c;
  171. input_recursion = 1;
  172. net_timeout = 0; /* no timeout */
  173. while (!input_size)
  174. NetLoop (NETCONS);
  175. input_recursion = 0;
  176. c = input_buffer[input_offset++];
  177. if (input_offset >= sizeof input_buffer)
  178. input_offset -= sizeof input_buffer;
  179. input_size--;
  180. return c;
  181. }
  182. int nc_tstc (void)
  183. {
  184. struct eth_device *eth;
  185. if (input_recursion)
  186. return 0;
  187. if (input_size)
  188. return 1;
  189. eth = eth_get_dev ();
  190. if (eth && eth->state == ETH_STATE_ACTIVE)
  191. return 0; /* inside net loop */
  192. input_recursion = 1;
  193. net_timeout = 1;
  194. NetLoop (NETCONS); /* kind of poll */
  195. input_recursion = 0;
  196. return input_size != 0;
  197. }
  198. int drv_nc_init (void)
  199. {
  200. device_t dev;
  201. int rc;
  202. memset (&dev, 0, sizeof (dev));
  203. strcpy (dev.name, "nc");
  204. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  205. dev.start = nc_start;
  206. dev.putc = nc_putc;
  207. dev.puts = nc_puts;
  208. dev.getc = nc_getc;
  209. dev.tstc = nc_tstc;
  210. rc = device_register (&dev);
  211. return (rc == 0) ? 1 : rc;
  212. }
  213. #endif /* CONFIG_NETCONSOLE */