netconsole.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 uchar nc_buf = 0; /* input buffer */
  32. static int input_recursion = 0;
  33. static int output_recursion = 0;
  34. static int net_timeout;
  35. static void nc_wait_arp_handler (uchar * pkt, unsigned dest, unsigned src,
  36. unsigned len)
  37. {
  38. NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
  39. }
  40. static void nc_handler (uchar * pkt, unsigned dest, unsigned src,
  41. unsigned len)
  42. {
  43. if (nc_buf)
  44. NetState = NETLOOP_SUCCESS; /* got input - quit net loop */
  45. }
  46. static void nc_timeout (void)
  47. {
  48. NetState = NETLOOP_SUCCESS;
  49. }
  50. void NcStart (void)
  51. {
  52. if (memcmp (NetServerEther, NetEtherNullAddr, 6)) {
  53. /* going to check for input packet */
  54. NetSetHandler (nc_handler);
  55. NetSetTimeout (net_timeout, nc_timeout);
  56. } else {
  57. /* send arp request */
  58. NetSetHandler (nc_wait_arp_handler);
  59. NetSendUDPPacket (NetServerEther, NetServerIP, 6665, 6666, 0);
  60. }
  61. }
  62. int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  63. {
  64. if (dest != 6666 || !len)
  65. return 0; /* not for us */
  66. nc_buf = *pkt;
  67. return 1;
  68. }
  69. static void nc_send_packet (const char *buf, int len)
  70. {
  71. DECLARE_GLOBAL_DATA_PTR;
  72. struct eth_device *eth;
  73. int inited = 0;
  74. uchar *pkt;
  75. if (!memcmp (NetServerEther, NetEtherNullAddr, 6))
  76. return;
  77. if ((eth = eth_get_dev ()) == NULL) {
  78. return;
  79. }
  80. if (eth->state != ETH_STATE_ACTIVE) {
  81. if (eth_init (gd->bd) < 0)
  82. return;
  83. inited = 1;
  84. }
  85. pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE;
  86. memcpy (pkt, buf, len);
  87. NetSendUDPPacket (NetServerEther, NetServerIP, 6666, 6665, len);
  88. if (inited)
  89. eth_halt ();
  90. }
  91. int nc_start (void)
  92. {
  93. if (memcmp (NetServerEther, NetEtherNullAddr, 6))
  94. return 0;
  95. return NetLoop (NETCONS); /* wait for arp reply */
  96. }
  97. void nc_putc (char c)
  98. {
  99. if (output_recursion)
  100. return;
  101. output_recursion = 1;
  102. nc_send_packet (&c, 1);
  103. output_recursion = 0;
  104. }
  105. void nc_puts (const char *s)
  106. {
  107. if (output_recursion)
  108. return;
  109. output_recursion = 1;
  110. int len = strlen (s);
  111. if (len > 512)
  112. len = 512;
  113. nc_send_packet (s, len);
  114. output_recursion = 0;
  115. }
  116. int nc_getc (void)
  117. {
  118. input_recursion = 1;
  119. net_timeout = 0; /* no timeout */
  120. while (!nc_buf)
  121. NetLoop (NETCONS);
  122. input_recursion = 0;
  123. uchar tmp = nc_buf;
  124. nc_buf = 0;
  125. return tmp;
  126. }
  127. int nc_tstc (void)
  128. {
  129. struct eth_device *eth;
  130. if (input_recursion)
  131. return 0;
  132. if (nc_buf)
  133. return 1;
  134. eth = eth_get_dev ();
  135. if (eth && eth->state == ETH_STATE_ACTIVE)
  136. return 0; /* inside net loop */
  137. input_recursion = 1;
  138. net_timeout = 1;
  139. NetLoop (NETCONS); /* kind of poll */
  140. input_recursion = 0;
  141. return nc_buf != 0;
  142. }
  143. int drv_nc_init (void)
  144. {
  145. device_t dev;
  146. int rc;
  147. memset (&dev, 0, sizeof (dev));
  148. strcpy (dev.name, "nc");
  149. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  150. dev.start = nc_start;
  151. dev.putc = nc_putc;
  152. dev.puts = nc_puts;
  153. dev.getc = nc_getc;
  154. dev.tstc = nc_tstc;
  155. rc = device_register (&dev);
  156. return (rc == 0) ? 1 : rc;
  157. }
  158. #endif /* CONFIG_NETCONSOLE */