gdbserver: use common hexify/unhexify routines
[openocd.git] / src / server / gdb_server.c
index 047a9d098f421c99659ba876a9c333f90c3058ac..5e570f5d5302df3903bb81f05e347a7b4e5bb336 100644 (file)
@@ -662,20 +662,17 @@ static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
 static int gdb_output_con(struct connection *connection, const char *line)
 {
        char *hex_buffer;
-       int i, bin_size;
+       int bin_size;
 
        bin_size = strlen(line);
 
-       hex_buffer = malloc(bin_size*2 + 2);
+       hex_buffer = malloc(bin_size * 2 + 2);
        if (hex_buffer == NULL)
                return ERROR_GDB_BUFFER_TOO_SMALL;
 
        hex_buffer[0] = 'O';
-       for (i = 0; i < bin_size; i++)
-               snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
-       hex_buffer[bin_size*2 + 1] = 0;
-
-       int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
+       int pkt_len = hexify(hex_buffer + 1, line, bin_size, bin_size * 2 + 1);
+       int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
 
        free(hex_buffer);
        return retval;
@@ -1231,14 +1228,9 @@ static int gdb_read_memory_packet(struct connection *connection,
        if (retval == ERROR_OK) {
                hex_buffer = malloc(len * 2 + 1);
 
-               uint32_t i;
-               for (i = 0; i < len; i++) {
-                       uint8_t t = buffer[i];
-                       hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
-                       hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
-               }
+               int pkt_len = hexify(hex_buffer, (char *)buffer, len, len * 2 + 1);
 
-               gdb_put_packet(connection, hex_buffer, len * 2);
+               gdb_put_packet(connection, hex_buffer, pkt_len);
 
                free(hex_buffer);
        } else
@@ -1258,8 +1250,6 @@ static int gdb_write_memory_packet(struct connection *connection,
        uint32_t len = 0;
 
        uint8_t *buffer;
-
-       uint32_t i;
        int retval;
 
        /* skip command character */
@@ -1283,11 +1273,8 @@ static int gdb_write_memory_packet(struct connection *connection,
 
        LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
 
-       for (i = 0; i < len; i++) {
-               uint32_t tmp;
-               sscanf(separator + 2*i, "%2" SCNx32, &tmp);
-               buffer[i] = tmp;
-       }
+       if (unhexify((char *)buffer, separator + 2, len) != (int)len)
+               LOG_ERROR("unable to decode memory packet");
 
        retval = target_write_buffer(target, addr, len, buffer);
 
@@ -1705,17 +1692,12 @@ static int gdb_query_packet(struct connection *connection,
        struct gdb_connection *gdb_connection = connection->priv;
        struct target *target = get_target_from_connection(connection);
 
-       if (strstr(packet, "qRcmd,")) {
+       if (strncmp(packet, "qRcmd,", 6) == 0) {
                if (packet_size > 6) {
                        char *cmd;
-                       int i;
-                       cmd = malloc((packet_size - 6)/2 + 1);
-                       for (i = 0; i < (packet_size - 6)/2; i++) {
-                               uint32_t tmp;
-                               sscanf(packet + 6 + 2*i, "%2" SCNx32, &tmp);
-                               cmd[i] = tmp;
-                       }
-                       cmd[(packet_size - 6)/2] = 0x0;
+                       cmd = malloc((packet_size - 6) / 2 + 1);
+                       int len = unhexify(cmd, packet + 6, (packet_size - 6) / 2);
+                       cmd[len] = 0;
 
                        /* We want to print all debug output to GDB connection */
                        log_add_callback(gdb_log_callback, connection);
@@ -1731,7 +1713,7 @@ static int gdb_query_packet(struct connection *connection,
                }
                gdb_put_packet(connection, "OK", 2);
                return ERROR_OK;
-       } else if (strstr(packet, "qCRC:")) {
+       } else if (strncmp(packet, "qCRC:", 5) == 0) {
                if (packet_size > 5) {
                        int retval;
                        char gdb_reply[10];
@@ -1765,7 +1747,7 @@ static int gdb_query_packet(struct connection *connection,
 
                        return ERROR_OK;
                }
-       } else if (strstr(packet, "qSupported")) {
+       } else if (strncmp(packet, "qSupported", 10) == 0) {
                /* we currently support packet size and qXfer:memory-map:read (if enabled)
                 * disable qXfer:features:read for the moment */
                int retval = ERROR_OK;
@@ -1790,10 +1772,10 @@ static int gdb_query_packet(struct connection *connection,
                free(buffer);
 
                return ERROR_OK;
-       } else if (strstr(packet, "qXfer:memory-map:read::")
+       } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
                   && (flash_get_bank_count() > 0))
                return gdb_memory_map(connection, packet, packet_size);
-       else if (strstr(packet, "qXfer:features:read:")) {
+       else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
                char *xml = NULL;
                int size = 0;
                int pos = 0;
@@ -1831,7 +1813,7 @@ static int gdb_query_packet(struct connection *connection,
 
                free(xml);
                return ERROR_OK;
-       } else if (strstr(packet, "QStartNoAckMode")) {
+       } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
                gdb_connection->noack_mode = 1;
                gdb_put_packet(connection, "OK", 2);
                return ERROR_OK;
@@ -1855,7 +1837,7 @@ static int gdb_v_packet(struct connection *connection,
                return ERROR_OK;
        }
 
-       if (strstr(packet, "vFlashErase:")) {
+       if (strncmp(packet, "vFlashErase:", 12) == 0) {
                unsigned long addr;
                unsigned long length;
 
@@ -1911,7 +1893,7 @@ static int gdb_v_packet(struct connection *connection,
                return ERROR_OK;
        }
 
-       if (strstr(packet, "vFlashWrite:")) {
+       if (strncmp(packet, "vFlashWrite:", 12) == 0) {
                int retval;
                unsigned long addr;
                unsigned long length;
@@ -1945,7 +1927,7 @@ static int gdb_v_packet(struct connection *connection,
                return ERROR_OK;
        }
 
-       if (!strcmp(packet, "vFlashDone")) {
+       if (strncmp(packet, "vFlashDone", 10) == 0) {
                uint32_t written;
 
                /* process the flashing buffer. No need to erase as GDB
@@ -2195,7 +2177,6 @@ static int gdb_input_inner(struct connection *connection)
                                        gdb_con->attached = true;
                                        /*  info rtos parts */
                                        gdb_thread_packet(connection, packet, packet_size);
-                                       gdb_put_packet(connection, "OK", 2);
                                        break;
 
                                case 'j':

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)