From f84cb403e460baf39def6635624a8c1239c1cfe0 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sun, 13 Jun 2021 11:38:30 -0700 Subject: [PATCH 1/3] dirtyJtag optimizations to cut the number of USB requests --- src/dirtyJtag.cpp | 111 ++++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 39 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index b0a2e18..de1bc64 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -179,14 +179,43 @@ int DirtyJtag::setClkFreq(uint32_t clkHZ) int DirtyJtag::writeTMS(uint8_t *tms, int len, bool flush_buffer) { (void) flush_buffer; + int actual_length; if (len == 0) return 0; - for (int i = 0; i < len; i++) { - bool val = (tms[i >> 3] & (1 << (i & 0x07))); - sendBitBang(SIG_TMS, ((val)?SIG_TMS : 0), NULL, (i == len-1)); + uint8_t mask= SIG_TCK | SIG_TMS; + uint8_t buf[64]; + u_int buffer_idx = 0; + for (int i = 0; i < len; i++) + { + uint8_t val = (tms[i >> 3] & (1 << (i & 0x07))) ? SIG_TMS : 0; + buf[buffer_idx++] = CMD_SETSIG; + buf[buffer_idx++] = mask; + buf[buffer_idx++] = val; + buf[buffer_idx++] = CMD_SETSIG; + buf[buffer_idx++] = mask; + buf[buffer_idx++] = val | SIG_TCK; + if ((buffer_idx + 7) >= sizeof(buf) || (i == len - 1)) { + //flush the buffer + if (i == len - 1) { + //insert tck falling edge + buf[buffer_idx++] = CMD_SETSIG; + buf[buffer_idx++] = mask; + buf[buffer_idx++] = val; + } + buf[buffer_idx++] = CMD_STOP; + int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, buffer_idx, &actual_length, 1000); + if (ret < 0) + { + cerr << "toggleClk: usb bulk write failed " << ret << endl; + return -EXIT_FAILURE; + } + buffer_idx = 0; + } } + assert(buffer_idx == 0); return len; } @@ -316,48 +345,52 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) int DirtyJtag::sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last) { - int actual_length; - mask |= SIG_TCK; - uint8_t buf[] = { CMD_SETSIG, - static_cast(mask), - static_cast(val), - CMD_SETSIG, - static_cast(mask), - static_cast(val | SIG_TCK), - CMD_STOP}; - - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 7, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk write failed 1" << endl; - return -EXIT_FAILURE; - } - - if (read) { - uint8_t rd_buf[] = {CMD_GETSIG, CMD_STOP}; + if (read || !((mask & SIG_TDI) && (mask & SIG_TMS))) { + int actual_length; + mask |= SIG_TCK; + uint8_t buf[] = {CMD_SETSIG, + static_cast(mask), + static_cast(val), + CMD_SETSIG, + static_cast(mask), + static_cast(val | SIG_TCK), + CMD_GETSIG, //<---Read instruction + CMD_STOP, + }; + if (!read){ + //remove the read instruction + buf[6] = CMD_STOP; + } if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - rd_buf, 2, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk write failed 3" << endl; + buf, sizeof(buf), &actual_length, 1000) < 0) { + cerr << "sendBitBang: usb bulk write failed 1" << endl; return -EXIT_FAILURE; } - do { - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - read, 1, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk read failed 4" << endl; + if (read) { + do + { + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + read, 1, &actual_length, 1000) < 0) + { + cerr << "sendBitBang: usb bulk read failed 4" << endl; + return -EXIT_FAILURE; + } + } while (actual_length == 0); + } + + if (last) { + buf[2] &= ~SIG_TCK; + buf[3] = CMD_STOP; + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 4, &actual_length, 1000) < 0) { + cerr << "sendBitBang usb bulk write failed" << endl; return -EXIT_FAILURE; } - } while (actual_length == 0); - } - - if (last) { - buf[2] &= ~SIG_TCK; - buf[3] = CMD_STOP; - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 4, &actual_length, 1000) < 0) { - cerr << "sendBitBang usb bulk write failed" << endl; - return -EXIT_FAILURE; } - } - return EXIT_SUCCESS; + return EXIT_SUCCESS; + } else { + return toggleClk(SIG_TMS & val, SIG_TDI & val, 1); + } } From cbbac0bff8dd435e1777d152130e5f38dd1e2394 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 19 Jun 2021 09:40:33 -0700 Subject: [PATCH 2/3] Changes per code review. --- src/dirtyJtag.cpp | 112 +++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 61 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index de1bc64..9f9796c 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -215,7 +215,6 @@ int DirtyJtag::writeTMS(uint8_t *tms, int len, bool flush_buffer) buffer_idx = 0; } } - assert(buffer_idx == 0); return len; } @@ -327,70 +326,61 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) unsigned char last_bit = (tx_cpy[pos >> 3] & (1 << (pos & 0x07))) ? SIG_TDI: 0; - if (sendBitBang(SIG_TMS | SIG_TDI, - SIG_TMS | (last_bit), (rx ? &sig : 0), true) != 0) { - cerr << "writeTDI: last bit error" << endl; - return -EXIT_FAILURE; - } + uint8_t mask = SIG_TMS | SIG_TDI; + uint8_t val = SIG_TMS | (last_bit); - if (rx) { + if (rx) + { + int actual_length; + mask |= SIG_TCK; + uint8_t buf[] = { + CMD_SETSIG, + static_cast(mask), + static_cast(val), + CMD_SETSIG, + static_cast(mask), + static_cast(val | SIG_TCK), + CMD_GETSIG, //<---Read instruction + CMD_STOP, + }; + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, sizeof(buf), &actual_length, 1000) < 0) + { + cerr << "writeTDI: last bit error: usb bulk write failed 1" << endl; + return -EXIT_FAILURE; + } + do + { + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + &sig, 1, &actual_length, 1000) < 0) + { + cerr << "writeTDI: last bit error: usb bulk read failed" << endl; + return -EXIT_FAILURE; + } + } while (actual_length == 0); rx[pos >> 3] >>= 1; - if (sig & SIG_TDO) { - rx[pos >> 3] |= (1 << (pos & 0x07));; + if (sig & SIG_TDO) + { + rx[pos >> 3] |= (1 << (pos & 0x07)); + } + buf[2] &= ~SIG_TCK; + buf[3] = CMD_STOP; + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 4, &actual_length, 1000) < 0) + { + cerr << "writeTDI: last bit error: usb bulk write failed 2" << endl; + return -EXIT_FAILURE; + } + + } + else + { + if (toggleClk(SIG_TMS & val, SIG_TDI & val, 1)) + { + cerr << "writeTDI: last bit error" << endl; + return -EXIT_FAILURE; } } } return EXIT_SUCCESS; } - -int DirtyJtag::sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last) -{ - if (read || !((mask & SIG_TDI) && (mask & SIG_TMS))) { - int actual_length; - mask |= SIG_TCK; - uint8_t buf[] = {CMD_SETSIG, - static_cast(mask), - static_cast(val), - CMD_SETSIG, - static_cast(mask), - static_cast(val | SIG_TCK), - CMD_GETSIG, //<---Read instruction - CMD_STOP, - }; - if (!read){ - //remove the read instruction - buf[6] = CMD_STOP; - } - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, sizeof(buf), &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk write failed 1" << endl; - return -EXIT_FAILURE; - } - - if (read) { - do - { - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - read, 1, &actual_length, 1000) < 0) - { - cerr << "sendBitBang: usb bulk read failed 4" << endl; - return -EXIT_FAILURE; - } - } while (actual_length == 0); - } - - if (last) { - buf[2] &= ~SIG_TCK; - buf[3] = CMD_STOP; - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 4, &actual_length, 1000) < 0) { - cerr << "sendBitBang usb bulk write failed" << endl; - return -EXIT_FAILURE; - } - } - - return EXIT_SUCCESS; - } else { - return toggleClk(SIG_TMS & val, SIG_TDI & val, 1); - } -} From 80f642a0a4d373aeb958cb1d06b5b8fd102282d7 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 19 Jun 2021 12:39:25 -0700 Subject: [PATCH 3/3] Fix a buffer overflow per code review --- src/dirtyJtag.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index 9f9796c..74c562f 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -196,7 +196,7 @@ int DirtyJtag::writeTMS(uint8_t *tms, int len, bool flush_buffer) buf[buffer_idx++] = CMD_SETSIG; buf[buffer_idx++] = mask; buf[buffer_idx++] = val | SIG_TCK; - if ((buffer_idx + 7) >= sizeof(buf) || (i == len - 1)) { + if ((buffer_idx + 9) >= sizeof(buf) || (i == len - 1)) { //flush the buffer if (i == len - 1) { //insert tck falling edge @@ -209,7 +209,7 @@ int DirtyJtag::writeTMS(uint8_t *tms, int len, bool flush_buffer) buf, buffer_idx, &actual_length, 1000); if (ret < 0) { - cerr << "toggleClk: usb bulk write failed " << ret << endl; + cerr << "writeTMS: usb bulk write failed " << ret << endl; return -EXIT_FAILURE; } buffer_idx = 0;