bcm2835gpio: Fix incorrect GPIO validation 38/6938/4
authorSteve Marple <stevemarple@googlemail.com>
Tue, 19 Apr 2022 22:22:31 +0000 (23:22 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 14 May 2022 08:59:03 +0000 (08:59 +0000)
Incorrect validation prevented GPIO0 from controlling the direction of
the SWDIO buffer or operating TRST/SRST.

Have all GPIO number validation checks performed by is_gpio_valid().

Change-Id: Ib8fb704ab588a618ac41c111f6168d658891d92c
Signed-off-by: Steve Marple <stevemarple@googlemail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/6938
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
src/jtag/drivers/bcm2835gpio.c

index b7a4d998cddfb1e5b010571a8e6600015e9ee1e1..22d237f22081d6a04edeedca2cb87920e5164cee 100644 (file)
@@ -94,6 +94,11 @@ static int speed_coeff = 113714;
 static int speed_offset = 28;
 static unsigned int jtag_delay;
 
 static int speed_offset = 28;
 static unsigned int jtag_delay;
 
+static int is_gpio_valid(int gpio)
+{
+       return gpio >= 0 && gpio <= 31;
+}
+
 static bb_value_t bcm2835gpio_read(void)
 {
        return (GPIO_LEV & 1<<tdo_gpio) ? BB_HIGH : BB_LOW;
 static bb_value_t bcm2835gpio_read(void)
 {
        return (GPIO_LEV & 1<<tdo_gpio) ? BB_HIGH : BB_LOW;
@@ -133,12 +138,12 @@ static int bcm2835gpio_reset(int trst, int srst)
        uint32_t set = 0;
        uint32_t clear = 0;
 
        uint32_t set = 0;
        uint32_t clear = 0;
 
-       if (trst_gpio > 0) {
+       if (is_gpio_valid(trst_gpio)) {
                set |= !trst<<trst_gpio;
                clear |= trst<<trst_gpio;
        }
 
                set |= !trst<<trst_gpio;
                clear |= trst<<trst_gpio;
        }
 
-       if (srst_gpio > 0) {
+       if (is_gpio_valid(srst_gpio)) {
                set |= !srst<<srst_gpio;
                clear |= srst<<srst_gpio;
        }
                set |= !srst<<srst_gpio;
                clear |= srst<<srst_gpio;
        }
@@ -151,7 +156,7 @@ static int bcm2835gpio_reset(int trst, int srst)
 
 static void bcm2835_swdio_drive(bool is_output)
 {
 
 static void bcm2835_swdio_drive(bool is_output)
 {
-       if (swdio_dir_gpio > 0) {
+       if (is_gpio_valid(swdio_dir_gpio)) {
                if (is_output) {
                        GPIO_SET = 1 << swdio_dir_gpio;
                        OUT_GPIO(swdio_gpio);
                if (is_output) {
                        GPIO_SET = 1 << swdio_dir_gpio;
                        OUT_GPIO(swdio_gpio);
@@ -196,11 +201,6 @@ static int bcm2835gpio_speed(int speed)
        return ERROR_OK;
 }
 
        return ERROR_OK;
 }
 
-static int is_gpio_valid(int gpio)
-{
-       return gpio >= 0 && gpio <= 31;
-}
-
 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionums)
 {
        if (CMD_ARGC == 4) {
 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionums)
 {
        if (CMD_ARGC == 4) {
@@ -557,7 +557,7 @@ static int bcm2835gpio_init(void)
                OUT_GPIO(tck_gpio);
                OUT_GPIO(tms_gpio);
 
                OUT_GPIO(tck_gpio);
                OUT_GPIO(tms_gpio);
 
-               if (trst_gpio != -1) {
+               if (is_gpio_valid(trst_gpio)) {
                        trst_gpio_mode = MODE_GPIO(trst_gpio);
                        GPIO_SET = 1 << trst_gpio;
                        OUT_GPIO(trst_gpio);
                        trst_gpio_mode = MODE_GPIO(trst_gpio);
                        GPIO_SET = 1 << trst_gpio;
                        OUT_GPIO(trst_gpio);
@@ -566,7 +566,7 @@ static int bcm2835gpio_init(void)
 
        if (transport_is_swd()) {
                /* Make buffer an output before the GPIO connected to it */
 
        if (transport_is_swd()) {
                /* Make buffer an output before the GPIO connected to it */
-               if (swdio_dir_gpio != -1) {
+               if (is_gpio_valid(swdio_dir_gpio)) {
                        swdio_dir_gpio_mode = MODE_GPIO(swdio_dir_gpio);
                        GPIO_SET = 1 << swdio_dir_gpio;
                        OUT_GPIO(swdio_dir_gpio);
                        swdio_dir_gpio_mode = MODE_GPIO(swdio_dir_gpio);
                        GPIO_SET = 1 << swdio_dir_gpio;
                        OUT_GPIO(swdio_dir_gpio);
@@ -581,7 +581,7 @@ static int bcm2835gpio_init(void)
                OUT_GPIO(swdio_gpio);
        }
 
                OUT_GPIO(swdio_gpio);
        }
 
-       if (srst_gpio != -1) {
+       if (is_gpio_valid(srst_gpio)) {
                srst_gpio_mode = MODE_GPIO(srst_gpio);
                GPIO_SET = 1 << srst_gpio;
                OUT_GPIO(srst_gpio);
                srst_gpio_mode = MODE_GPIO(srst_gpio);
                GPIO_SET = 1 << srst_gpio;
                OUT_GPIO(srst_gpio);
@@ -601,7 +601,7 @@ static int bcm2835gpio_quit(void)
                SET_MODE_GPIO(tdi_gpio, tdi_gpio_mode);
                SET_MODE_GPIO(tck_gpio, tck_gpio_mode);
                SET_MODE_GPIO(tms_gpio, tms_gpio_mode);
                SET_MODE_GPIO(tdi_gpio, tdi_gpio_mode);
                SET_MODE_GPIO(tck_gpio, tck_gpio_mode);
                SET_MODE_GPIO(tms_gpio, tms_gpio_mode);
-               if (trst_gpio != -1)
+               if (is_gpio_valid(trst_gpio))
                        SET_MODE_GPIO(trst_gpio, trst_gpio_mode);
        }
 
                        SET_MODE_GPIO(trst_gpio, trst_gpio_mode);
        }
 
@@ -610,10 +610,10 @@ static int bcm2835gpio_quit(void)
                SET_MODE_GPIO(swdio_gpio, swdio_gpio_mode);
        }
 
                SET_MODE_GPIO(swdio_gpio, swdio_gpio_mode);
        }
 
-       if (srst_gpio != -1)
+       if (is_gpio_valid(srst_gpio))
                SET_MODE_GPIO(srst_gpio, srst_gpio_mode);
 
                SET_MODE_GPIO(srst_gpio, srst_gpio_mode);
 
-       if (swdio_dir_gpio != -1)
+       if (is_gpio_valid(swdio_dir_gpio))
                SET_MODE_GPIO(swdio_dir_gpio, swdio_dir_gpio_mode);
 
        return ERROR_OK;
                SET_MODE_GPIO(swdio_dir_gpio, swdio_dir_gpio_mode);
 
        return ERROR_OK;

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)