netconsole.c 5.7 KB

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