netconsole.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
  29. #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
  30. #endif
  31. static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
  32. static int input_size; /* char count in input buffer */
  33. static int input_offset; /* offset to valid chars in input buffer */
  34. static int input_recursion;
  35. static int output_recursion;
  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_out_port; /* target output port */
  40. static short nc_in_port; /* source input port */
  41. static const char *output_packet; /* used by first send udp */
  42. static int output_packet_len;
  43. /*
  44. * Start with a default last protocol.
  45. * We are only interested in NETCONS or not.
  46. */
  47. enum proto_t net_loop_last_protocol = BOOTP;
  48. static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
  49. IPaddr_t sip, unsigned src,
  50. unsigned len)
  51. {
  52. net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
  53. }
  54. static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  55. unsigned len)
  56. {
  57. if (input_size)
  58. net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
  59. }
  60. static void nc_timeout(void)
  61. {
  62. net_set_state(NETLOOP_SUCCESS);
  63. }
  64. void NcStart(void)
  65. {
  66. if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
  67. /* going to check for input packet */
  68. net_set_udp_handler(nc_handler);
  69. NetSetTimeout(net_timeout, nc_timeout);
  70. } else {
  71. /* send arp request */
  72. uchar *pkt;
  73. net_set_arp_handler(nc_wait_arp_handler);
  74. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  75. memcpy(pkt, output_packet, output_packet_len);
  76. NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
  77. output_packet_len);
  78. }
  79. }
  80. int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  81. {
  82. int end, chunk;
  83. if (dest != nc_in_port || !len)
  84. return 0; /* not for us */
  85. debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
  86. if (input_size == sizeof(input_buffer))
  87. return 1; /* no space */
  88. if (len > sizeof(input_buffer) - input_size)
  89. len = sizeof(input_buffer) - input_size;
  90. end = input_offset + input_size;
  91. if (end > sizeof(input_buffer))
  92. end -= sizeof(input_buffer);
  93. chunk = len;
  94. if (end + len > sizeof(input_buffer)) {
  95. chunk = sizeof(input_buffer) - end;
  96. memcpy(input_buffer, pkt + chunk, len - chunk);
  97. }
  98. memcpy(input_buffer + end, pkt, chunk);
  99. input_size += len;
  100. return 1;
  101. }
  102. static void nc_send_packet(const char *buf, int len)
  103. {
  104. struct eth_device *eth;
  105. int inited = 0;
  106. uchar *pkt;
  107. uchar *ether;
  108. IPaddr_t ip;
  109. debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
  110. eth = eth_get_dev();
  111. if (eth == NULL)
  112. return;
  113. if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
  114. if (eth->state == ETH_STATE_ACTIVE)
  115. return; /* inside net loop */
  116. output_packet = buf;
  117. output_packet_len = len;
  118. NetLoop(NETCONS); /* wait for arp reply and send packet */
  119. output_packet_len = 0;
  120. return;
  121. }
  122. if (eth->state != ETH_STATE_ACTIVE) {
  123. if (eth_is_on_demand_init()) {
  124. if (eth_init(gd->bd) < 0)
  125. return;
  126. eth_set_last_protocol(NETCONS);
  127. } else
  128. eth_init_state_only(gd->bd);
  129. inited = 1;
  130. }
  131. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  132. memcpy(pkt, buf, len);
  133. ether = nc_ether;
  134. ip = nc_ip;
  135. NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
  136. if (inited) {
  137. if (eth_is_on_demand_init())
  138. eth_halt();
  139. else
  140. eth_halt_state_only();
  141. }
  142. }
  143. static int nc_start(void)
  144. {
  145. int netmask, our_ip;
  146. char *p;
  147. nc_out_port = 6666; /* default port */
  148. nc_in_port = nc_out_port;
  149. if (getenv("ncip")) {
  150. nc_ip = getenv_IPaddr("ncip");
  151. if (!nc_ip)
  152. return -1; /* ncip is 0.0.0.0 */
  153. p = strchr(getenv("ncip"), ':');
  154. if (p != NULL) {
  155. nc_out_port = simple_strtoul(p + 1, NULL, 10);
  156. nc_in_port = nc_out_port;
  157. }
  158. } else
  159. nc_ip = ~0; /* ncip is not set, so broadcast */
  160. p = getenv("ncoutport");
  161. if (p != NULL)
  162. nc_out_port = simple_strtoul(p, NULL, 10);
  163. p = getenv("ncinport");
  164. if (p != NULL)
  165. nc_in_port = simple_strtoul(p, NULL, 10);
  166. our_ip = getenv_IPaddr("ipaddr");
  167. netmask = getenv_IPaddr("netmask");
  168. if (nc_ip == ~0 || /* 255.255.255.255 */
  169. ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
  170. (netmask | nc_ip) == ~0)) /* broadcast to our net */
  171. memset(nc_ether, 0xff, sizeof(nc_ether));
  172. else
  173. memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
  174. /*
  175. * Initialize the static IP settings and buffer pointers
  176. * incase we call NetSendUDPPacket before NetLoop
  177. */
  178. net_init();
  179. return 0;
  180. }
  181. static void nc_putc(char c)
  182. {
  183. if (output_recursion)
  184. return;
  185. output_recursion = 1;
  186. nc_send_packet(&c, 1);
  187. output_recursion = 0;
  188. }
  189. static void nc_puts(const char *s)
  190. {
  191. int len;
  192. if (output_recursion)
  193. return;
  194. output_recursion = 1;
  195. len = strlen(s);
  196. while (len) {
  197. int send_len = min(len, sizeof(input_buffer));
  198. nc_send_packet(s, send_len);
  199. len -= send_len;
  200. s += send_len;
  201. }
  202. output_recursion = 0;
  203. }
  204. static int nc_getc(void)
  205. {
  206. uchar c;
  207. input_recursion = 1;
  208. net_timeout = 0; /* no timeout */
  209. while (!input_size)
  210. NetLoop(NETCONS);
  211. input_recursion = 0;
  212. c = input_buffer[input_offset++];
  213. if (input_offset >= sizeof(input_buffer))
  214. input_offset -= sizeof(input_buffer);
  215. input_size--;
  216. return c;
  217. }
  218. static int nc_tstc(void)
  219. {
  220. struct eth_device *eth;
  221. if (input_recursion)
  222. return 0;
  223. if (input_size)
  224. return 1;
  225. eth = eth_get_dev();
  226. if (eth && eth->state == ETH_STATE_ACTIVE)
  227. return 0; /* inside net loop */
  228. input_recursion = 1;
  229. net_timeout = 1;
  230. NetLoop(NETCONS); /* kind of poll */
  231. input_recursion = 0;
  232. return input_size != 0;
  233. }
  234. int drv_nc_init(void)
  235. {
  236. struct stdio_dev dev;
  237. int rc;
  238. memset(&dev, 0, sizeof(dev));
  239. strcpy(dev.name, "nc");
  240. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  241. dev.start = nc_start;
  242. dev.putc = nc_putc;
  243. dev.puts = nc_puts;
  244. dev.getc = nc_getc;
  245. dev.tstc = nc_tstc;
  246. rc = stdio_register(&dev);
  247. return (rc == 0) ? 1 : rc;
  248. }