jtag_vpi: ensured constant packet size & endianness
[openocd.git] / src / jtag / drivers / jtag_vpi.c
index 3e39420fb39b80bebb8e1cdc924c49222b50fe69..27b9da116187adaf0de4bea6415e119975826414 100644 (file)
@@ -53,16 +53,34 @@ char *server_address;
 int sockfd;
 struct sockaddr_in serv_addr;
 
+/* One jtag_vpi "packet" as sent over a TCP channel. */
 struct vpi_cmd {
-       int cmd;
+       union {
+               uint32_t cmd;
+               unsigned char cmd_buf[4];
+       };
        unsigned char buffer_out[XFERT_MAX_SIZE];
        unsigned char buffer_in[XFERT_MAX_SIZE];
-       int length;
-       int nb_bits;
+       union {
+               uint32_t length;
+               unsigned char length_buf[4];
+       };
+       union {
+               uint32_t nb_bits;
+               unsigned char nb_bits_buf[4];
+       };
 };
 
 static int jtag_vpi_send_cmd(struct vpi_cmd *vpi)
 {
+       /* Use little endian when transmitting/receiving jtag_vpi cmds.
+          The choice of little endian goes against usual networking conventions
+          but is intentional to remain compatible with most older OpenOCD builds
+          (i.e. builds on little-endian platforms). */
+       h_u32_to_le(vpi->cmd_buf, vpi->cmd);
+       h_u32_to_le(vpi->length_buf, vpi->length);
+       h_u32_to_le(vpi->nb_bits_buf, vpi->nb_bits);
+
        int retval = write_socket(sockfd, vpi, sizeof(struct vpi_cmd));
        if (retval <= 0)
                return ERROR_FAIL;
@@ -76,6 +94,11 @@ static int jtag_vpi_receive_cmd(struct vpi_cmd *vpi)
        if (retval < (int)sizeof(struct vpi_cmd))
                return ERROR_FAIL;
 
+       /* Use little endian when transmitting/receiving jtag_vpi cmds. */
+       vpi->cmd = le_to_h_u32(vpi->cmd_buf);
+       vpi->length = le_to_h_u32(vpi->length_buf);
+       vpi->nb_bits = le_to_h_u32(vpi->nb_bits_buf);
+
        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)