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.
 
 
 
 
 
 

739 lines
18 KiB

  1. /***************************************************************************
  2. * *
  3. * Copyright (C) 2012 by Spencer Oliver *
  4. * spen@spen-soft.co.uk *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. /* project specific includes */
  25. #include <helper/binarybuffer.h>
  26. #include <jtag/interface.h>
  27. #include <jtag/hla/hla_layout.h>
  28. #include <jtag/hla/hla_transport.h>
  29. #include <jtag/hla/hla_interface.h>
  30. #include <target/target.h>
  31. #include <target/cortex_m.h>
  32. #include <libusb-1.0/libusb.h>
  33. #define ICDI_WRITE_ENDPOINT 0x02
  34. #define ICDI_READ_ENDPOINT 0x83
  35. #define ICDI_WRITE_TIMEOUT 1000
  36. #define ICDI_READ_TIMEOUT 1000
  37. #define ICDI_PACKET_SIZE 2048
  38. #define PACKET_START "$"
  39. #define PACKET_END "#"
  40. struct icdi_usb_handle_s {
  41. libusb_context *usb_ctx;
  42. libusb_device_handle *usb_dev;
  43. char *read_buffer;
  44. char *write_buffer;
  45. int max_packet;
  46. int read_count;
  47. };
  48. static int icdi_usb_read_mem32(void *handle, uint32_t addr, uint16_t len, uint8_t *buffer);
  49. static int icdi_usb_write_mem32(void *handle, uint32_t addr, uint16_t len, const uint8_t *buffer);
  50. static int remote_escape_output(const char *buffer, int len, char *out_buf, int *out_len, int out_maxlen)
  51. {
  52. int input_index, output_index;
  53. output_index = 0;
  54. for (input_index = 0; input_index < len; input_index++) {
  55. char b = buffer[input_index];
  56. if (b == '$' || b == '#' || b == '}' || b == '*') {
  57. /* These must be escaped. */
  58. if (output_index + 2 > out_maxlen)
  59. break;
  60. out_buf[output_index++] = '}';
  61. out_buf[output_index++] = b ^ 0x20;
  62. } else {
  63. if (output_index + 1 > out_maxlen)
  64. break;
  65. out_buf[output_index++] = b;
  66. }
  67. }
  68. *out_len = input_index;
  69. return output_index;
  70. }
  71. static int remote_unescape_input(const char *buffer, int len, char *out_buf, int out_maxlen)
  72. {
  73. int input_index, output_index;
  74. int escaped;
  75. output_index = 0;
  76. escaped = 0;
  77. for (input_index = 0; input_index < len; input_index++) {
  78. char b = buffer[input_index];
  79. if (output_index + 1 > out_maxlen)
  80. LOG_ERROR("Received too much data from the target.");
  81. if (escaped) {
  82. out_buf[output_index++] = b ^ 0x20;
  83. escaped = 0;
  84. } else if (b == '}')
  85. escaped = 1;
  86. else
  87. out_buf[output_index++] = b;
  88. }
  89. if (escaped)
  90. LOG_ERROR("Unmatched escape character in target response.");
  91. return output_index;
  92. }
  93. static int icdi_send_packet(void *handle, int len)
  94. {
  95. unsigned char cksum = 0;
  96. struct icdi_usb_handle_s *h;
  97. int result, retry = 0;
  98. int transferred = 0;
  99. assert(handle != NULL);
  100. h = (struct icdi_usb_handle_s *)handle;
  101. /* check we have a large enough buffer for checksum "#00" */
  102. if (len + 3 > h->max_packet) {
  103. LOG_ERROR("packet buffer too small");
  104. return ERROR_FAIL;
  105. }
  106. /* calculate checksum - offset start of packet */
  107. for (int i = 1; i < len; i++)
  108. cksum += h->write_buffer[i];
  109. len += sprintf(&h->write_buffer[len], PACKET_END "%02x", cksum);
  110. #ifdef _DEBUG_USB_COMMS_
  111. char buffer[50];
  112. char ch = h->write_buffer[1];
  113. if (ch == 'x' || ch == 'X')
  114. LOG_DEBUG("writing packet: <binary>");
  115. else {
  116. memcpy(buffer, h->write_buffer, len >= 50 ? 50-1 : len);
  117. buffer[len] = 0;
  118. LOG_DEBUG("writing packet: %s", buffer);
  119. }
  120. #endif
  121. while (1) {
  122. result = libusb_bulk_transfer(h->usb_dev, ICDI_WRITE_ENDPOINT, (unsigned char *)h->write_buffer, len,
  123. &transferred, ICDI_WRITE_TIMEOUT);
  124. if (result != 0 || transferred != len) {
  125. LOG_DEBUG("Error TX Data %d", result);
  126. return ERROR_FAIL;
  127. }
  128. /* check that the client got the message ok, or shall we resend */
  129. result = libusb_bulk_transfer(h->usb_dev, ICDI_READ_ENDPOINT, (unsigned char *)h->read_buffer, h->max_packet,
  130. &transferred, ICDI_READ_TIMEOUT);
  131. if (result != 0 || transferred < 1) {
  132. LOG_DEBUG("Error RX Data %d", result);
  133. return ERROR_FAIL;
  134. }
  135. #ifdef _DEBUG_USB_COMMS_
  136. LOG_DEBUG("received reply: '%c' : count %d", h->read_buffer[0], transferred);
  137. #endif
  138. if (h->read_buffer[0] == '-') {
  139. LOG_DEBUG("Resending packet %d", ++retry);
  140. } else {
  141. if (h->read_buffer[0] != '+')
  142. LOG_DEBUG("Unexpected Reply from ICDI: %c", h->read_buffer[0]);
  143. break;
  144. }
  145. if (retry == 3) {
  146. LOG_DEBUG("maximum nack retries attempted");
  147. return ERROR_FAIL;
  148. }
  149. }
  150. retry = 0;
  151. h->read_count = transferred;
  152. while (1) {
  153. /* read reply from icdi */
  154. result = libusb_bulk_transfer(h->usb_dev, ICDI_READ_ENDPOINT, (unsigned char *)h->read_buffer + h->read_count,
  155. h->max_packet - h->read_count, &transferred, ICDI_READ_TIMEOUT);
  156. #ifdef _DEBUG_USB_COMMS_
  157. LOG_DEBUG("received data: count %d", transferred);
  158. #endif
  159. /* check for errors but retry for timeout */
  160. if (result != 0) {
  161. if (result == LIBUSB_ERROR_TIMEOUT) {
  162. LOG_DEBUG("Error RX timeout %d", result);
  163. } else {
  164. LOG_DEBUG("Error RX Data %d", result);
  165. return ERROR_FAIL;
  166. }
  167. }
  168. h->read_count += transferred;
  169. /* we need to make sure we have a full packet, including checksum */
  170. if (h->read_count > 5) {
  171. /* check that we have received an packet delimiter
  172. * we do not validate the checksum
  173. * reply should contain $...#AA - so we check for # */
  174. if (h->read_buffer[h->read_count - 3] == '#')
  175. return ERROR_OK;
  176. }
  177. if (retry++ == 3) {
  178. LOG_DEBUG("maximum data retries attempted");
  179. break;
  180. }
  181. }
  182. return ERROR_FAIL;
  183. }
  184. static int icdi_send_cmd(void *handle, const char *cmd)
  185. {
  186. struct icdi_usb_handle_s *h;
  187. h = (struct icdi_usb_handle_s *)handle;
  188. int cmd_len = snprintf(h->write_buffer, h->max_packet, PACKET_START "%s", cmd);
  189. return icdi_send_packet(handle, cmd_len);
  190. }
  191. static int icdi_send_remote_cmd(void *handle, const char *data)
  192. {
  193. struct icdi_usb_handle_s *h;
  194. h = (struct icdi_usb_handle_s *)handle;
  195. size_t cmd_len = sprintf(h->write_buffer, PACKET_START "qRcmd,");
  196. cmd_len += hexify(h->write_buffer + cmd_len, data, 0, h->max_packet - cmd_len);
  197. return icdi_send_packet(handle, cmd_len);
  198. }
  199. static int icdi_get_cmd_result(void *handle)
  200. {
  201. struct icdi_usb_handle_s *h;
  202. int offset = 0;
  203. char ch;
  204. assert(handle != NULL);
  205. h = (struct icdi_usb_handle_s *)handle;
  206. do {
  207. ch = h->read_buffer[offset++];
  208. if (offset > h->read_count)
  209. return ERROR_FAIL;
  210. } while (ch != '$');
  211. if (memcmp("OK", h->read_buffer + offset, 2) == 0)
  212. return ERROR_OK;
  213. if (h->read_buffer[offset] == 'E') {
  214. /* get error code */
  215. char result;
  216. if (unhexify(&result, h->read_buffer + offset + 1, 1) != 1)
  217. return ERROR_FAIL;
  218. return result;
  219. }
  220. /* for now we assume everything else is ok */
  221. return ERROR_OK;
  222. }
  223. static int icdi_usb_idcode(void *handle, uint32_t *idcode)
  224. {
  225. return ERROR_OK;
  226. }
  227. static int icdi_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
  228. {
  229. return icdi_usb_write_mem32(handle, addr, 1, (uint8_t *)&val);
  230. }
  231. static enum target_state icdi_usb_state(void *handle)
  232. {
  233. int result;
  234. struct icdi_usb_handle_s *h;
  235. uint32_t dhcsr;
  236. h = (struct icdi_usb_handle_s *)handle;
  237. result = icdi_usb_read_mem32(h, DCB_DHCSR, 1, (uint8_t *)&dhcsr);
  238. if (result != ERROR_OK)
  239. return TARGET_UNKNOWN;
  240. if (dhcsr & S_HALT)
  241. return TARGET_HALTED;
  242. return TARGET_RUNNING;
  243. }
  244. static int icdi_usb_version(void *handle)
  245. {
  246. struct icdi_usb_handle_s *h;
  247. h = (struct icdi_usb_handle_s *)handle;
  248. char version[20];
  249. /* get info about icdi */
  250. int result = icdi_send_remote_cmd(handle, "version");
  251. if (result != ERROR_OK)
  252. return result;
  253. if (h->read_count < 8) {
  254. LOG_ERROR("Invalid Reply Received");
  255. return ERROR_FAIL;
  256. }
  257. /* convert reply */
  258. if (unhexify(version, h->read_buffer + 2, 4) != 4) {
  259. LOG_WARNING("unable to get ICDI version");
  260. return ERROR_OK;
  261. }
  262. /* null terminate and print info */
  263. version[4] = 0;
  264. LOG_INFO("ICDI Firmware version: %s", version);
  265. return ERROR_OK;
  266. }
  267. static int icdi_usb_query(void *handle)
  268. {
  269. int result;
  270. struct icdi_usb_handle_s *h;
  271. h = (struct icdi_usb_handle_s *)handle;
  272. result = icdi_send_cmd(handle, "qSupported");
  273. if (result != ERROR_OK)
  274. return result;
  275. /* check result */
  276. result = icdi_get_cmd_result(handle);
  277. if (result != ERROR_OK) {
  278. LOG_ERROR("query supported failed: 0x%x", result);
  279. return ERROR_FAIL;
  280. }
  281. /* from this we can get the max packet supported */
  282. /* query packet buffer size */
  283. char *offset = strstr(h->read_buffer, "PacketSize");
  284. if (offset) {
  285. char *separator;
  286. int max_packet;
  287. max_packet = strtoul(offset + 11, &separator, 16);
  288. if (!max_packet)
  289. LOG_ERROR("invalid max packet, using defaults");
  290. else
  291. h->max_packet = max_packet;
  292. LOG_DEBUG("max packet supported : %" PRIu32 " bytes", h->max_packet);
  293. }
  294. /* if required re allocate packet buffer */
  295. if (h->max_packet != ICDI_PACKET_SIZE) {
  296. h->read_buffer = realloc(h->read_buffer, h->max_packet);
  297. h->write_buffer = realloc(h->write_buffer, h->max_packet);
  298. if (h->read_buffer == 0 || h->write_buffer == 0) {
  299. LOG_ERROR("unable to reallocate memory");
  300. return ERROR_FAIL;
  301. }
  302. }
  303. /* set extended mode */
  304. result = icdi_send_cmd(handle, "!");
  305. /* check result */
  306. result = icdi_get_cmd_result(handle);
  307. if (result != ERROR_OK) {
  308. LOG_ERROR("unable to enable extended mode: 0x%x", result);
  309. return ERROR_FAIL;
  310. }
  311. return ERROR_OK;
  312. }
  313. static int icdi_usb_reset(void *handle)
  314. {
  315. /* we do this in hla_target.c */
  316. return ERROR_OK;
  317. }
  318. static int icdi_usb_assert_srst(void *handle, int srst)
  319. {
  320. /* TODO not supported yet */
  321. return ERROR_COMMAND_NOTFOUND;
  322. }
  323. static int icdi_usb_run(void *handle)
  324. {
  325. int result;
  326. /* resume target at current address */
  327. result = icdi_send_cmd(handle, "c");
  328. if (result != ERROR_OK)
  329. return result;
  330. /* check result */
  331. result = icdi_get_cmd_result(handle);
  332. if (result != ERROR_OK) {
  333. LOG_ERROR("continue failed: 0x%x", result);
  334. return ERROR_FAIL;
  335. }
  336. return result;
  337. }
  338. static int icdi_usb_halt(void *handle)
  339. {
  340. int result;
  341. /* this query halts the target ?? */
  342. result = icdi_send_cmd(handle, "?");
  343. if (result != ERROR_OK)
  344. return result;
  345. /* check result */
  346. result = icdi_get_cmd_result(handle);
  347. if (result != ERROR_OK) {
  348. LOG_ERROR("halt failed: 0x%x", result);
  349. return ERROR_FAIL;
  350. }
  351. return result;
  352. }
  353. static int icdi_usb_step(void *handle)
  354. {
  355. int result;
  356. /* step target at current address */
  357. result = icdi_send_cmd(handle, "s");
  358. if (result != ERROR_OK)
  359. return result;
  360. /* check result */
  361. result = icdi_get_cmd_result(handle);
  362. if (result != ERROR_OK) {
  363. LOG_ERROR("step failed: 0x%x", result);
  364. return ERROR_FAIL;
  365. }
  366. return result;
  367. }
  368. static int icdi_usb_read_regs(void *handle)
  369. {
  370. /* currently unsupported */
  371. return ERROR_OK;
  372. }
  373. static int icdi_usb_read_reg(void *handle, int num, uint32_t *val)
  374. {
  375. int result;
  376. struct icdi_usb_handle_s *h;
  377. char cmd[10];
  378. h = (struct icdi_usb_handle_s *)handle;
  379. snprintf(cmd, sizeof(cmd), "p%x", num);
  380. result = icdi_send_cmd(handle, cmd);
  381. if (result != ERROR_OK)
  382. return result;
  383. /* check result */
  384. result = icdi_get_cmd_result(handle);
  385. if (result != ERROR_OK) {
  386. LOG_ERROR("register read failed: 0x%x", result);
  387. return ERROR_FAIL;
  388. }
  389. /* convert result */
  390. if (unhexify((char *)val, h->read_buffer + 2, 4) != 4) {
  391. LOG_ERROR("failed to convert result");
  392. return ERROR_FAIL;
  393. }
  394. return result;
  395. }
  396. static int icdi_usb_write_reg(void *handle, int num, uint32_t val)
  397. {
  398. int result;
  399. char cmd[20];
  400. int cmd_len = snprintf(cmd, sizeof(cmd), "P%x=", num);
  401. hexify(cmd + cmd_len, (char *)&val, 4, sizeof(cmd));
  402. result = icdi_send_cmd(handle, cmd);
  403. if (result != ERROR_OK)
  404. return result;
  405. /* check result */
  406. result = icdi_get_cmd_result(handle);
  407. if (result != ERROR_OK) {
  408. LOG_ERROR("register write failed: 0x%x", result);
  409. return ERROR_FAIL;
  410. }
  411. return result;
  412. }
  413. static int icdi_usb_read_mem(void *handle, uint32_t addr, uint32_t len, uint8_t *buffer)
  414. {
  415. int result;
  416. struct icdi_usb_handle_s *h;
  417. char cmd[20];
  418. h = (struct icdi_usb_handle_s *)handle;
  419. snprintf(cmd, sizeof(cmd), "x%x,%x", addr, len);
  420. result = icdi_send_cmd(handle, cmd);
  421. if (result != ERROR_OK)
  422. return result;
  423. /* check result */
  424. result = icdi_get_cmd_result(handle);
  425. if (result != ERROR_OK) {
  426. LOG_ERROR("memory read failed: 0x%x", result);
  427. return ERROR_FAIL;
  428. }
  429. /* unescape input */
  430. int read_len = remote_unescape_input(h->read_buffer + 5, h->read_count - 8, (char *)buffer, len);
  431. if (read_len != (int)len) {
  432. LOG_ERROR("read more bytes than expected: actual 0x%" PRIx32 " expected 0x%" PRIx32, read_len, len);
  433. return ERROR_FAIL;
  434. }
  435. return ERROR_OK;
  436. }
  437. static int icdi_usb_write_mem(void *handle, uint32_t addr, uint32_t len, const uint8_t *buffer)
  438. {
  439. int result;
  440. struct icdi_usb_handle_s *h;
  441. h = (struct icdi_usb_handle_s *)handle;
  442. size_t cmd_len = snprintf(h->write_buffer, h->max_packet, PACKET_START "X%x,%x:", addr, len);
  443. int out_len;
  444. cmd_len += remote_escape_output((char *)buffer, len, h->write_buffer + cmd_len,
  445. &out_len, h->max_packet - cmd_len);
  446. if (out_len < (int)len) {
  447. /* for now issue a error as we have no way of allocating a larger buffer */
  448. LOG_ERROR("memory buffer too small: requires 0x%" PRIx32 " actual 0x%" PRIx32, out_len, len);
  449. return ERROR_FAIL;
  450. }
  451. result = icdi_send_packet(handle, cmd_len);
  452. if (result != ERROR_OK)
  453. return result;
  454. /* check result */
  455. result = icdi_get_cmd_result(handle);
  456. if (result != ERROR_OK) {
  457. LOG_ERROR("memory write failed: 0x%x", result);
  458. return ERROR_FAIL;
  459. }
  460. return ERROR_OK;
  461. }
  462. static int icdi_usb_read_mem8(void *handle, uint32_t addr, uint16_t len, uint8_t *buffer)
  463. {
  464. return icdi_usb_read_mem(handle, addr, len, buffer);
  465. }
  466. static int icdi_usb_write_mem8(void *handle, uint32_t addr, uint16_t len, const uint8_t *buffer)
  467. {
  468. return icdi_usb_write_mem(handle, addr, len, buffer);
  469. }
  470. static int icdi_usb_read_mem32(void *handle, uint32_t addr, uint16_t len, uint8_t *buffer)
  471. {
  472. return icdi_usb_read_mem(handle, addr, len * 4, buffer);
  473. }
  474. static int icdi_usb_write_mem32(void *handle, uint32_t addr, uint16_t len, const uint8_t *buffer)
  475. {
  476. return icdi_usb_write_mem(handle, addr, len * 4, buffer);
  477. }
  478. static int icdi_usb_close(void *handle)
  479. {
  480. struct icdi_usb_handle_s *h;
  481. h = (struct icdi_usb_handle_s *)handle;
  482. if (h->usb_dev)
  483. libusb_close(h->usb_dev);
  484. if (h->usb_ctx)
  485. libusb_exit(h->usb_ctx);
  486. if (h->read_buffer)
  487. free(h->read_buffer);
  488. if (h->write_buffer)
  489. free(h->write_buffer);
  490. free(handle);
  491. return ERROR_OK;
  492. }
  493. static int icdi_usb_open(struct hl_interface_param_s *param, void **fd)
  494. {
  495. int retval;
  496. struct icdi_usb_handle_s *h;
  497. LOG_DEBUG("icdi_usb_open");
  498. h = calloc(1, sizeof(struct icdi_usb_handle_s));
  499. if (h == 0) {
  500. LOG_ERROR("unable to allocate memory");
  501. return ERROR_FAIL;
  502. }
  503. LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
  504. param->vid, param->pid);
  505. if (libusb_init(&h->usb_ctx) != 0) {
  506. LOG_ERROR("libusb init failed");
  507. goto error_open;
  508. }
  509. h->usb_dev = libusb_open_device_with_vid_pid(h->usb_ctx, param->vid, param->pid);
  510. if (!h->usb_dev) {
  511. LOG_ERROR("open failed");
  512. goto error_open;
  513. }
  514. if (libusb_claim_interface(h->usb_dev, 2)) {
  515. LOG_DEBUG("claim interface failed");
  516. goto error_open;
  517. }
  518. /* check if mode is supported */
  519. retval = ERROR_OK;
  520. switch (param->transport) {
  521. #if 0
  522. /* TODO place holder as swd is not currently supported */
  523. case HL_TRANSPORT_SWD:
  524. #endif
  525. case HL_TRANSPORT_JTAG:
  526. break;
  527. default:
  528. retval = ERROR_FAIL;
  529. break;
  530. }
  531. if (retval != ERROR_OK) {
  532. LOG_ERROR("mode (transport) not supported by device");
  533. goto error_open;
  534. }
  535. /* allocate buffer */
  536. h->read_buffer = malloc(ICDI_PACKET_SIZE);
  537. h->write_buffer = malloc(ICDI_PACKET_SIZE);
  538. h->max_packet = ICDI_PACKET_SIZE;
  539. if (h->read_buffer == 0 || h->write_buffer == 0) {
  540. LOG_DEBUG("malloc failed");
  541. goto error_open;
  542. }
  543. /* query icdi version etc */
  544. retval = icdi_usb_version(h);
  545. if (retval != ERROR_OK)
  546. goto error_open;
  547. /* query icdi support */
  548. retval = icdi_usb_query(h);
  549. if (retval != ERROR_OK)
  550. goto error_open;
  551. *fd = h;
  552. /* set the max target read/write buffer in bytes
  553. * as we are using gdb binary packets to transfer memory we have to
  554. * reserve half the buffer for any possible escape chars plus
  555. * at least 64 bytes for the gdb packet header */
  556. param->max_buffer = (((h->max_packet - 64) / 4) * 4) / 2;
  557. return ERROR_OK;
  558. error_open:
  559. icdi_usb_close(h);
  560. return ERROR_FAIL;
  561. }
  562. struct hl_layout_api_s icdi_usb_layout_api = {
  563. .open = icdi_usb_open,
  564. .close = icdi_usb_close,
  565. .idcode = icdi_usb_idcode,
  566. .state = icdi_usb_state,
  567. .reset = icdi_usb_reset,
  568. .assert_srst = icdi_usb_assert_srst,
  569. .run = icdi_usb_run,
  570. .halt = icdi_usb_halt,
  571. .step = icdi_usb_step,
  572. .read_regs = icdi_usb_read_regs,
  573. .read_reg = icdi_usb_read_reg,
  574. .write_reg = icdi_usb_write_reg,
  575. .read_mem8 = icdi_usb_read_mem8,
  576. .write_mem8 = icdi_usb_write_mem8,
  577. .read_mem32 = icdi_usb_read_mem32,
  578. .write_mem32 = icdi_usb_write_mem32,
  579. .write_debug_reg = icdi_usb_write_debug_reg
  580. };