You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

393 lines
11 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Richard Uhler *
  3. * ruhler@mit.edu *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #ifndef _WIN32
  22. #include <sys/un.h>
  23. #include <netdb.h>
  24. #endif
  25. #include "helper/system.h"
  26. #include "helper/replacements.h"
  27. #include <jtag/interface.h>
  28. #include "bitbang.h"
  29. /* arbitrary limit on host name length: */
  30. #define REMOTE_BITBANG_HOST_MAX 255
  31. static char *remote_bitbang_host;
  32. static char *remote_bitbang_port;
  33. static int remote_bitbang_fd;
  34. static uint8_t remote_bitbang_send_buf[512];
  35. static unsigned int remote_bitbang_send_buf_used;
  36. /* Circular buffer. When start == end, the buffer is empty. */
  37. static char remote_bitbang_recv_buf[64];
  38. static unsigned int remote_bitbang_recv_buf_start;
  39. static unsigned int remote_bitbang_recv_buf_end;
  40. static bool remote_bitbang_buf_full(void)
  41. {
  42. return remote_bitbang_recv_buf_end ==
  43. ((remote_bitbang_recv_buf_start + sizeof(remote_bitbang_recv_buf) - 1) %
  44. sizeof(remote_bitbang_recv_buf));
  45. }
  46. /* Read any incoming data, placing it into the buffer. */
  47. static int remote_bitbang_fill_buf(void)
  48. {
  49. socket_nonblock(remote_bitbang_fd);
  50. while (!remote_bitbang_buf_full()) {
  51. unsigned int contiguous_available_space;
  52. if (remote_bitbang_recv_buf_end >= remote_bitbang_recv_buf_start) {
  53. contiguous_available_space = sizeof(remote_bitbang_recv_buf) -
  54. remote_bitbang_recv_buf_end;
  55. if (remote_bitbang_recv_buf_start == 0)
  56. contiguous_available_space -= 1;
  57. } else {
  58. contiguous_available_space = remote_bitbang_recv_buf_start -
  59. remote_bitbang_recv_buf_end - 1;
  60. }
  61. ssize_t count = read_socket(remote_bitbang_fd,
  62. remote_bitbang_recv_buf + remote_bitbang_recv_buf_end,
  63. contiguous_available_space);
  64. if (count > 0) {
  65. remote_bitbang_recv_buf_end += count;
  66. if (remote_bitbang_recv_buf_end == sizeof(remote_bitbang_recv_buf))
  67. remote_bitbang_recv_buf_end = 0;
  68. } else if (count == 0) {
  69. return ERROR_OK;
  70. } else if (count < 0) {
  71. #ifdef _WIN32
  72. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  73. #else
  74. if (errno == EAGAIN) {
  75. #endif
  76. return ERROR_OK;
  77. } else {
  78. log_socket_error("remote_bitbang_fill_buf");
  79. return ERROR_FAIL;
  80. }
  81. }
  82. }
  83. return ERROR_OK;
  84. }
  85. static int remote_bitbang_flush(void)
  86. {
  87. if (remote_bitbang_send_buf_used <= 0)
  88. return ERROR_OK;
  89. unsigned int offset = 0;
  90. while (offset < remote_bitbang_send_buf_used) {
  91. ssize_t written = write_socket(remote_bitbang_fd, remote_bitbang_send_buf + offset,
  92. remote_bitbang_send_buf_used - offset);
  93. if (written < 0) {
  94. log_socket_error("remote_bitbang_putc");
  95. remote_bitbang_send_buf_used = 0;
  96. return ERROR_FAIL;
  97. }
  98. offset += written;
  99. }
  100. remote_bitbang_send_buf_used = 0;
  101. return ERROR_OK;
  102. }
  103. typedef enum {
  104. NO_FLUSH,
  105. FLUSH_SEND_BUF
  106. } flush_bool_t;
  107. static int remote_bitbang_queue(int c, flush_bool_t flush)
  108. {
  109. remote_bitbang_send_buf[remote_bitbang_send_buf_used++] = c;
  110. if (flush == FLUSH_SEND_BUF ||
  111. remote_bitbang_send_buf_used >= ARRAY_SIZE(remote_bitbang_send_buf))
  112. return remote_bitbang_flush();
  113. return ERROR_OK;
  114. }
  115. static int remote_bitbang_quit(void)
  116. {
  117. if (remote_bitbang_queue('Q', FLUSH_SEND_BUF) == ERROR_FAIL)
  118. return ERROR_FAIL;
  119. if (close_socket(remote_bitbang_fd) != 0) {
  120. log_socket_error("close_socket");
  121. return ERROR_FAIL;
  122. }
  123. free(remote_bitbang_host);
  124. free(remote_bitbang_port);
  125. LOG_INFO("remote_bitbang interface quit");
  126. return ERROR_OK;
  127. }
  128. static bb_value_t char_to_int(int c)
  129. {
  130. switch (c) {
  131. case '0':
  132. return BB_LOW;
  133. case '1':
  134. return BB_HIGH;
  135. default:
  136. remote_bitbang_quit();
  137. LOG_ERROR("remote_bitbang: invalid read response: %c(%i)", c, c);
  138. return BB_ERROR;
  139. }
  140. }
  141. /* Get the next read response. */
  142. static bb_value_t remote_bitbang_rread(void)
  143. {
  144. if (remote_bitbang_flush() != ERROR_OK)
  145. return ERROR_FAIL;
  146. /* Enable blocking access. */
  147. socket_block(remote_bitbang_fd);
  148. char c;
  149. ssize_t count = read_socket(remote_bitbang_fd, &c, 1);
  150. if (count == 1) {
  151. return char_to_int(c);
  152. } else {
  153. remote_bitbang_quit();
  154. LOG_ERROR("read_socket: count=%d", (int) count);
  155. log_socket_error("read_socket");
  156. return BB_ERROR;
  157. }
  158. }
  159. static int remote_bitbang_sample(void)
  160. {
  161. if (remote_bitbang_fill_buf() != ERROR_OK)
  162. return ERROR_FAIL;
  163. assert(!remote_bitbang_buf_full());
  164. return remote_bitbang_queue('R', NO_FLUSH);
  165. }
  166. static bb_value_t remote_bitbang_read_sample(void)
  167. {
  168. if (remote_bitbang_recv_buf_start == remote_bitbang_recv_buf_end) {
  169. if (remote_bitbang_fill_buf() != ERROR_OK)
  170. return ERROR_FAIL;
  171. }
  172. if (remote_bitbang_recv_buf_start != remote_bitbang_recv_buf_end) {
  173. int c = remote_bitbang_recv_buf[remote_bitbang_recv_buf_start];
  174. remote_bitbang_recv_buf_start =
  175. (remote_bitbang_recv_buf_start + 1) % sizeof(remote_bitbang_recv_buf);
  176. return char_to_int(c);
  177. }
  178. return remote_bitbang_rread();
  179. }
  180. static int remote_bitbang_write(int tck, int tms, int tdi)
  181. {
  182. char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
  183. return remote_bitbang_queue(c, NO_FLUSH);
  184. }
  185. static int remote_bitbang_reset(int trst, int srst)
  186. {
  187. char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
  188. /* Always flush the send buffer on reset, because the reset call need not be
  189. * followed by jtag_execute_queue(). */
  190. return remote_bitbang_queue(c, FLUSH_SEND_BUF);
  191. }
  192. static int remote_bitbang_blink(int on)
  193. {
  194. char c = on ? 'B' : 'b';
  195. return remote_bitbang_queue(c, FLUSH_SEND_BUF);
  196. }
  197. static struct bitbang_interface remote_bitbang_bitbang = {
  198. .buf_size = sizeof(remote_bitbang_recv_buf) - 1,
  199. .sample = &remote_bitbang_sample,
  200. .read_sample = &remote_bitbang_read_sample,
  201. .write = &remote_bitbang_write,
  202. .blink = &remote_bitbang_blink,
  203. };
  204. static int remote_bitbang_init_tcp(void)
  205. {
  206. struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
  207. struct addrinfo *result, *rp;
  208. int fd = 0;
  209. LOG_INFO("Connecting to %s:%s",
  210. remote_bitbang_host ? remote_bitbang_host : "localhost",
  211. remote_bitbang_port);
  212. /* Obtain address(es) matching host/port */
  213. int s = getaddrinfo(remote_bitbang_host, remote_bitbang_port, &hints, &result);
  214. if (s != 0) {
  215. LOG_ERROR("getaddrinfo: %s\n", gai_strerror(s));
  216. return ERROR_FAIL;
  217. }
  218. /* getaddrinfo() returns a list of address structures.
  219. Try each address until we successfully connect(2).
  220. If socket(2) (or connect(2)) fails, we (close the socket
  221. and) try the next address. */
  222. for (rp = result; rp ; rp = rp->ai_next) {
  223. fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  224. if (fd == -1)
  225. continue;
  226. if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
  227. break; /* Success */
  228. close(fd);
  229. }
  230. freeaddrinfo(result); /* No longer needed */
  231. if (!rp) { /* No address succeeded */
  232. log_socket_error("Failed to connect");
  233. return ERROR_FAIL;
  234. }
  235. return fd;
  236. }
  237. static int remote_bitbang_init_unix(void)
  238. {
  239. if (!remote_bitbang_host) {
  240. LOG_ERROR("host/socket not specified");
  241. return ERROR_FAIL;
  242. }
  243. LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
  244. int fd = socket(PF_UNIX, SOCK_STREAM, 0);
  245. if (fd < 0) {
  246. log_socket_error("socket");
  247. return ERROR_FAIL;
  248. }
  249. struct sockaddr_un addr;
  250. addr.sun_family = AF_UNIX;
  251. strncpy(addr.sun_path, remote_bitbang_host, sizeof(addr.sun_path));
  252. addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
  253. if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
  254. log_socket_error("connect");
  255. return ERROR_FAIL;
  256. }
  257. return fd;
  258. }
  259. static int remote_bitbang_init(void)
  260. {
  261. bitbang_interface = &remote_bitbang_bitbang;
  262. remote_bitbang_recv_buf_start = 0;
  263. remote_bitbang_recv_buf_end = 0;
  264. LOG_INFO("Initializing remote_bitbang driver");
  265. if (!remote_bitbang_port)
  266. remote_bitbang_fd = remote_bitbang_init_unix();
  267. else
  268. remote_bitbang_fd = remote_bitbang_init_tcp();
  269. if (remote_bitbang_fd < 0)
  270. return remote_bitbang_fd;
  271. LOG_INFO("remote_bitbang driver initialized");
  272. return ERROR_OK;
  273. }
  274. COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
  275. {
  276. if (CMD_ARGC == 1) {
  277. uint16_t port;
  278. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
  279. free(remote_bitbang_port);
  280. remote_bitbang_port = port == 0 ? NULL : strdup(CMD_ARGV[0]);
  281. return ERROR_OK;
  282. }
  283. return ERROR_COMMAND_SYNTAX_ERROR;
  284. }
  285. COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
  286. {
  287. if (CMD_ARGC == 1) {
  288. free(remote_bitbang_host);
  289. remote_bitbang_host = strdup(CMD_ARGV[0]);
  290. return ERROR_OK;
  291. }
  292. return ERROR_COMMAND_SYNTAX_ERROR;
  293. }
  294. static const struct command_registration remote_bitbang_command_handlers[] = {
  295. {
  296. .name = "remote_bitbang_port",
  297. .handler = remote_bitbang_handle_remote_bitbang_port_command,
  298. .mode = COMMAND_CONFIG,
  299. .help = "Set the port to use to connect to the remote jtag.\n"
  300. " if 0 or unset, use unix sockets to connect to the remote jtag.",
  301. .usage = "port_number",
  302. },
  303. {
  304. .name = "remote_bitbang_host",
  305. .handler = remote_bitbang_handle_remote_bitbang_host_command,
  306. .mode = COMMAND_CONFIG,
  307. .help = "Set the host to use to connect to the remote jtag.\n"
  308. " if port is 0 or unset, this is the name of the unix socket to use.",
  309. .usage = "host_name",
  310. },
  311. COMMAND_REGISTRATION_DONE,
  312. };
  313. static int remote_bitbang_execute_queue(void)
  314. {
  315. /* safety: the send buffer must be empty, no leftover characters from
  316. * previous transactions */
  317. assert(remote_bitbang_send_buf_used == 0);
  318. /* process the JTAG command queue */
  319. int ret = bitbang_execute_queue();
  320. if (ret != ERROR_OK)
  321. return ret;
  322. /* flush not-yet-sent characters, if any */
  323. return remote_bitbang_flush();
  324. }
  325. static struct jtag_interface remote_bitbang_interface = {
  326. .execute_queue = &remote_bitbang_execute_queue,
  327. };
  328. struct adapter_driver remote_bitbang_adapter_driver = {
  329. .name = "remote_bitbang",
  330. .transports = jtag_only,
  331. .commands = remote_bitbang_command_handlers,
  332. .init = &remote_bitbang_init,
  333. .quit = &remote_bitbang_quit,
  334. .reset = &remote_bitbang_reset,
  335. .jtag_ops = &remote_bitbang_interface,
  336. };