target/xtensa: avoid IHI for writes to non-executable memory
[openocd.git] / contrib / remote_bitbang / remote_bitbang_sysfsgpio.c
index 5c717ce0ed0816f348bf05fd5d2833439a4e7794..1588eb9a3ca2f1c744699477164290692c59729b 100644 (file)
@@ -1,41 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
+ *   Copyright (C) 2021 by Manuel Wick <manuel@matronix.de>                *
  *   Copyright (C) 2013 Paul Fertser <fercerpav@gmail.com>                 *
  *   Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 /*
-  This is a test application to be used as a remote bitbang server for
-  the OpenOCD remote_bitbang interface driver.
-
-  To compile run:
-  gcc -Wall -ansi -pedantic -std=c99 -o remote_bitbang_sysfsgpio remote_bitbang_sysfsgpio.c
-
-
-  Usage example:
-
-  On Raspberry Pi run:
-  socat TCP6-LISTEN:7777,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
-
-  On host run:
 openocd -c "interface remote_bitbang; remote_bitbang_host raspberrypi; remote_bitbang_port 7777" \
        -f target/stm32f1x.cfg
-
-  Or if you want to test UNIX sockets, run both on Raspberry Pi:
-  socat UNIX-LISTEN:/tmp/remotebitbang-socket,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
 openocd -c "interface remote_bitbang; remote_bitbang_host /tmp/remotebitbang-socket" -f target/stm32f1x.cfg
* This is a test application to be used as a remote bitbang server for
* the OpenOCD remote_bitbang interface driver.
+ *
* To compile run:
* gcc -Wall -ansi -pedantic -std=c99 -o remote_bitbang_sysfsgpio remote_bitbang_sysfsgpio.c
+ *
+ *
* Usage example:
+ *
* On Raspberry Pi run:
* socat TCP6-LISTEN:7777,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
+ *
* On host run:
* openocd -c "adapter driver remote_bitbang; remote_bitbang host raspberrypi; remote_bitbang port 7777" \
*  -f target/stm32f1x.cfg
+ *
* Or if you want to test UNIX sockets, run both on Raspberry Pi:
* socat UNIX-LISTEN:/tmp/remotebitbang-socket,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
* openocd -c "adapter driver remote_bitbang; remote_bitbang host /tmp/remotebitbang-socket" -f target/stm32f1x.cfg
 */
 
 #include <sys/types.h>
@@ -108,11 +98,14 @@ static void unexport_sysfs_gpio(int gpio)
  * If the gpio is an output, it is initialized according to init_high,
  * otherwise it is ignored.
  *
+ * When open_rw is set, the file descriptor will be open as read and write,
+ * e.g. for SWDIO (TMS) that is used as input and output.
+ *
  * If the gpio is already exported we just show a warning and continue; if
  * openocd happened to crash (or was killed by user) then the gpios will not
  * have been cleaned up.
  */
-static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
+static int setup_sysfs_gpio(int gpio, int is_output, int init_high, int open_rw)
 {
        char buf[40];
        char gpiostr[4];
@@ -143,7 +136,9 @@ static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
        }
 
        snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
-       if (is_output)
+       if (open_rw)
+               ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
+       else if (is_output)
                ret = open(buf, O_WRONLY | O_NONBLOCK | O_SYNC);
        else
                ret = open(buf, O_RDONLY | O_NONBLOCK | O_SYNC);
@@ -154,6 +149,37 @@ static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
        return ret;
 }
 
+/*
+ * Change direction for gpio.
+ */
+static int change_dir_sysfs_gpio(int gpio, int is_output, int init_high)
+{
+       char buf[40];
+       int ret;
+
+       if (!is_gpio_valid(gpio))
+               return ERROR_OK;
+
+       snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
+       ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
+       if (ret < 0) {
+               LOG_ERROR("Couldn't set direction for gpio %d", gpio);
+               perror("sysfsgpio: ");
+               unexport_sysfs_gpio(gpio);
+               return ERROR_FAIL;
+       }
+
+       return ERROR_OK;
+}
+
+/* gpio numbers for each gpio. Negative values are invalid */
+static int tck_gpio = -1;
+static int tms_gpio = -1;
+static int tdi_gpio = -1;
+static int tdo_gpio = -1;
+static int trst_gpio = -1;
+static int srst_gpio = -1;
+
 /*
  * file descriptors for /sys/class/gpio/gpioXX/value
  * Set up during init.
@@ -165,6 +191,15 @@ static int tdo_fd = -1;
 static int trst_fd = -1;
 static int srst_fd = -1;
 
+/*
+ * GPIO state of /sys/class/gpio/gpioXX/value
+ */
+static int last_tck = -1;
+static int last_tms = -1;
+static int last_tms_drive = -1;
+static int last_tdi = -1;
+static int last_initialized = -1;
+
 /*
  * Bitbang interface read of TDO
  *
@@ -190,26 +225,22 @@ static int sysfsgpio_read(void)
 /*
  * Bitbang interface write of TCK, TMS, TDI
  *
- * Seeing as this is the only function where the outputs are changed,
- * we can cache the old value to avoid needlessly writing it.
+ * Output states are changed here and in sysfsgpio_write_swd,
+ * which are not used simultaneously, so we can cache the old
+ * value to avoid needlessly writing it.
  */
 static void sysfsgpio_write(int tck, int tms, int tdi)
 {
        const char one[] = "1";
        const char zero[] = "0";
 
-       static int last_tck;
-       static int last_tms;
-       static int last_tdi;
-
-       static int first_time;
        size_t bytes_written;
 
-       if (!first_time) {
+       if (!last_initialized) {
                last_tck = !tck;
                last_tms = !tms;
                last_tdi = !tdi;
-               first_time = 1;
+               last_initialized = 1;
        }
 
        if (tdi != last_tdi) {
@@ -262,13 +293,81 @@ static void sysfsgpio_reset(int trst, int srst)
        }
 }
 
-/* gpio numbers for each gpio. Negative values are invalid */
-static int tck_gpio = -1;
-static int tms_gpio = -1;
-static int tdi_gpio = -1;
-static int tdo_gpio = -1;
-static int trst_gpio = -1;
-static int srst_gpio = -1;
+/*
+ * Bitbang interface set direction of SWDIO (TMS)
+ */
+static void sysfsgpio_swdio_drive(int is_output)
+{
+       int ret;
+
+       if (is_output != 0 && last_tms == -1)
+               last_tms = 0;
+
+       ret = change_dir_sysfs_gpio(tms_gpio, (is_output != 0) ? 1 : 0, last_tms);
+       if (ret != ERROR_OK)
+               LOG_WARNING("Failed to change SWDIO (TMS) direction to output");
+       else
+               last_tms_drive = (is_output != 0) ? 1 : 0;
+}
+
+/*
+ * Bitbang interface read of SWDIO (TMS)
+ *
+ * The sysfs value will read back either '0' or '1'. The trick here is to call
+ * lseek to bypass buffering in the sysfs kernel driver.
+ */
+static int sysfsgpio_swdio_read(void)
+{
+       char buf[1];
+
+       /* important to seek to signal sysfs of new read */
+       lseek(tms_fd, 0, SEEK_SET);
+       int ret = read(tms_fd, &buf, sizeof(buf));
+
+       if (ret < 0) {
+               LOG_WARNING("reading swdio (tms) failed");
+               return 0;
+       }
+
+       return buf[0];
+}
+
+/*
+ * Bitbang interface write of SWCLK (TCK) and SWDIO (TMS)
+ *
+ * Output states are changed here and in sysfsgpio_write, which
+ * are not used simultaneously, so we can cache the old value
+ * to avoid needlessly writing it.
+ */
+static void sysfsgpio_swd_write(int swclk, int swdio)
+{
+       static const char one[] = "1";
+       static const char zero[] = "0";
+
+       size_t bytes_written;
+
+       if (!last_initialized) {
+               last_tck = !swclk;
+               last_tms = !swdio;
+               last_initialized = 1;
+       }
+
+       if (last_tms_drive == 1 && swdio != last_tms) {
+               bytes_written = write(tms_fd, swdio ? &one : &zero, 1);
+               if (bytes_written != 1)
+                       LOG_WARNING("writing swdio (tms) failed");
+       }
+
+       /* write clk last */
+       if (swclk != last_tck) {
+               bytes_written = write(tck_fd, swclk ? &one : &zero, 1);
+               if (bytes_written != 1)
+                       LOG_WARNING("writing swclk (tck) failed");
+       }
+
+       last_tms = swdio;
+       last_tck = swclk;
+}
 
 /* helper func to close and cleanup files only if they were valid/ used */
 static void cleanup_fd(int fd, int gpio)
@@ -304,13 +403,21 @@ static void process_remote_protocol(void)
                        char d = c - 'r';
                        sysfsgpio_reset(!!(d & 2),
                                        (d & 1));
-               } else if (c >= '0' && c <= '0' + 7) {/* Write */
+               } else if (c >= '0' && c <= '0' + 7) { /* Write */
                        char d = c - '0';
                        sysfsgpio_write(!!(d & 4),
                                        !!(d & 2),
                                        (d & 1));
                } else if (c == 'R')
                        putchar(sysfsgpio_read());
+               else if (c == 'c') /* SWDIO read */
+                       putchar(sysfsgpio_swdio_read());
+               else if (c == 'o' || c == 'O') /* SWDIO drive */
+                       sysfsgpio_swdio_drive(c == 'o' ? 0 : 1);
+               else if (c >= 'd' && c <= 'g') { /* SWD write */
+                       char d = c - 'd';
+                       sysfsgpio_swd_write((d & 2), (d & 1));
+               }
                else
                        LOG_ERROR("Unknown command '%c' received", c);
        }
@@ -318,7 +425,7 @@ static void process_remote_protocol(void)
 
 int main(int argc, char *argv[])
 {
-       LOG_WARNING("SysfsGPIO remote_bitbang JTAG driver\n");
+       LOG_WARNING("SysfsGPIO remote_bitbang JTAG+SWD driver\n");
 
        for (int i = 1; i < argc; i++) {
                if (!strcmp(argv[i], "tck"))
@@ -360,36 +467,39 @@ int main(int argc, char *argv[])
         * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
         * as outputs.  Drive TDI and TCK low, and TMS/TRST/SRST high.
         */
-       tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
+       tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0, 0);
        if (tck_fd < 0)
                goto out_error;
 
-       tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
+       tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1, 1);
        if (tms_fd < 0)
                goto out_error;
+       last_tms_drive = 0;
 
-       tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
+       tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0, 0);
        if (tdi_fd < 0)
                goto out_error;
 
-       tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
+       tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0, 0);
        if (tdo_fd < 0)
                goto out_error;
 
        /* assume active low */
        if (trst_gpio > 0) {
-               trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
+               trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1, 0);
                if (trst_fd < 0)
                        goto out_error;
        }
 
        /* assume active low */
        if (srst_gpio > 0) {
-               srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
+               srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1, 0);
                if (srst_fd < 0)
                        goto out_error;
        }
 
+       last_initialized = 0;
+
        LOG_WARNING("SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
                 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
        LOG_WARNING("SysfsGPIO num: srst = %d", srst_gpio);

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)