netconsole.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static int is_broadcast(IPaddr_t ip)
  65. {
  66. static IPaddr_t netmask;
  67. static IPaddr_t our_ip;
  68. static int env_changed_id;
  69. int env_id = get_env_id();
  70. /* update only when the environment has changed */
  71. if (env_changed_id != env_id) {
  72. netmask = getenv_IPaddr("netmask");
  73. our_ip = getenv_IPaddr("ipaddr");
  74. env_changed_id = env_id;
  75. }
  76. return (ip == ~0 || /* 255.255.255.255 */
  77. ((netmask & our_ip) == (netmask & ip) && /* on the same net */
  78. (netmask | ip) == ~0)); /* broadcast to our net */
  79. }
  80. static int refresh_settings_from_env(void)
  81. {
  82. const char *p;
  83. static int env_changed_id;
  84. int env_id = get_env_id();
  85. /* update only when the environment has changed */
  86. if (env_changed_id != env_id) {
  87. if (getenv("ncip")) {
  88. nc_ip = getenv_IPaddr("ncip");
  89. if (!nc_ip)
  90. return -1; /* ncip is 0.0.0.0 */
  91. p = strchr(getenv("ncip"), ':');
  92. if (p != NULL) {
  93. nc_out_port = simple_strtoul(p + 1, NULL, 10);
  94. nc_in_port = nc_out_port;
  95. }
  96. } else
  97. nc_ip = ~0; /* ncip is not set, so broadcast */
  98. p = getenv("ncoutport");
  99. if (p != NULL)
  100. nc_out_port = simple_strtoul(p, NULL, 10);
  101. p = getenv("ncinport");
  102. if (p != NULL)
  103. nc_in_port = simple_strtoul(p, NULL, 10);
  104. if (is_broadcast(nc_ip))
  105. /* broadcast MAC address */
  106. memset(nc_ether, 0xff, sizeof(nc_ether));
  107. else
  108. /* force arp request */
  109. memset(nc_ether, 0, sizeof(nc_ether));
  110. }
  111. return 0;
  112. }
  113. /**
  114. * Called from NetLoop in net/net.c before each packet
  115. */
  116. void NcStart(void)
  117. {
  118. refresh_settings_from_env();
  119. if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
  120. /* going to check for input packet */
  121. net_set_udp_handler(nc_handler);
  122. NetSetTimeout(net_timeout, nc_timeout);
  123. } else {
  124. /* send arp request */
  125. uchar *pkt;
  126. net_set_arp_handler(nc_wait_arp_handler);
  127. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  128. memcpy(pkt, output_packet, output_packet_len);
  129. NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
  130. output_packet_len);
  131. }
  132. }
  133. int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
  134. unsigned src_port, unsigned len)
  135. {
  136. int end, chunk;
  137. if (dest_port != nc_in_port || !len)
  138. return 0; /* not for us */
  139. if (src_ip != nc_ip && !is_broadcast(nc_ip))
  140. return 0; /* not from our client */
  141. debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
  142. if (input_size == sizeof(input_buffer))
  143. return 1; /* no space */
  144. if (len > sizeof(input_buffer) - input_size)
  145. len = sizeof(input_buffer) - input_size;
  146. end = input_offset + input_size;
  147. if (end > sizeof(input_buffer))
  148. end -= sizeof(input_buffer);
  149. chunk = len;
  150. if (end + len > sizeof(input_buffer)) {
  151. chunk = sizeof(input_buffer) - end;
  152. memcpy(input_buffer, pkt + chunk, len - chunk);
  153. }
  154. memcpy(input_buffer + end, pkt, chunk);
  155. input_size += len;
  156. return 1;
  157. }
  158. static void nc_send_packet(const char *buf, int len)
  159. {
  160. struct eth_device *eth;
  161. int inited = 0;
  162. uchar *pkt;
  163. uchar *ether;
  164. IPaddr_t ip;
  165. debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
  166. eth = eth_get_dev();
  167. if (eth == NULL)
  168. return;
  169. if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
  170. if (eth->state == ETH_STATE_ACTIVE)
  171. return; /* inside net loop */
  172. output_packet = buf;
  173. output_packet_len = len;
  174. NetLoop(NETCONS); /* wait for arp reply and send packet */
  175. output_packet_len = 0;
  176. return;
  177. }
  178. if (eth->state != ETH_STATE_ACTIVE) {
  179. if (eth_is_on_demand_init()) {
  180. if (eth_init(gd->bd) < 0)
  181. return;
  182. eth_set_last_protocol(NETCONS);
  183. } else
  184. eth_init_state_only(gd->bd);
  185. inited = 1;
  186. }
  187. pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  188. memcpy(pkt, buf, len);
  189. ether = nc_ether;
  190. ip = nc_ip;
  191. NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
  192. if (inited) {
  193. if (eth_is_on_demand_init())
  194. eth_halt();
  195. else
  196. eth_halt_state_only();
  197. }
  198. }
  199. static int nc_start(void)
  200. {
  201. int retval;
  202. nc_out_port = 6666; /* default port */
  203. nc_in_port = nc_out_port;
  204. retval = refresh_settings_from_env();
  205. if (retval != 0)
  206. return retval;
  207. /*
  208. * Initialize the static IP settings and buffer pointers
  209. * incase we call NetSendUDPPacket before NetLoop
  210. */
  211. net_init();
  212. return 0;
  213. }
  214. static void nc_putc(char c)
  215. {
  216. if (output_recursion)
  217. return;
  218. output_recursion = 1;
  219. nc_send_packet(&c, 1);
  220. output_recursion = 0;
  221. }
  222. static void nc_puts(const char *s)
  223. {
  224. int len;
  225. if (output_recursion)
  226. return;
  227. output_recursion = 1;
  228. len = strlen(s);
  229. while (len) {
  230. int send_len = min(len, sizeof(input_buffer));
  231. nc_send_packet(s, send_len);
  232. len -= send_len;
  233. s += send_len;
  234. }
  235. output_recursion = 0;
  236. }
  237. static int nc_getc(void)
  238. {
  239. uchar c;
  240. input_recursion = 1;
  241. net_timeout = 0; /* no timeout */
  242. while (!input_size)
  243. NetLoop(NETCONS);
  244. input_recursion = 0;
  245. c = input_buffer[input_offset++];
  246. if (input_offset >= sizeof(input_buffer))
  247. input_offset -= sizeof(input_buffer);
  248. input_size--;
  249. return c;
  250. }
  251. static int nc_tstc(void)
  252. {
  253. struct eth_device *eth;
  254. if (input_recursion)
  255. return 0;
  256. if (input_size)
  257. return 1;
  258. eth = eth_get_dev();
  259. if (eth && eth->state == ETH_STATE_ACTIVE)
  260. return 0; /* inside net loop */
  261. input_recursion = 1;
  262. net_timeout = 1;
  263. NetLoop(NETCONS); /* kind of poll */
  264. input_recursion = 0;
  265. return input_size != 0;
  266. }
  267. int drv_nc_init(void)
  268. {
  269. struct stdio_dev dev;
  270. int rc;
  271. memset(&dev, 0, sizeof(dev));
  272. strcpy(dev.name, "nc");
  273. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  274. dev.start = nc_start;
  275. dev.putc = nc_putc;
  276. dev.puts = nc_puts;
  277. dev.getc = nc_getc;
  278. dev.tstc = nc_tstc;
  279. rc = stdio_register(&dev);
  280. return (rc == 0) ? 1 : rc;
  281. }