Allowed CONFIG_USE_LIBSG=0 to suppress STLink/V1 compilation
[fw/stlink] / gdbserver / gdb-server.c
index 1cce453ee7547fc09b1630b1489d603d15b94383..67f0be0e61d4edd42992b72865b393c0ff3c186a 100644 (file)
@@ -14,6 +14,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <signal.h>
 
 #include <stlink-common.h>
 
 #define FLASH_PAGE_MASK (~((1 << 10) - 1))
 #define FLASH_SIZE (FLASH_PAGE * 128)
 
+volatile int do_exit = 0;
+void ctrl_c(int sig)
+{
+  do_exit = 1;
+}
+
 static const char hex[] = "0123456789abcdef";
 
 static const char* current_memory_map = NULL;
@@ -32,6 +39,11 @@ static const char* current_memory_map = NULL;
  * Chip IDs are explained in the appropriate programming manual for the
  * DBGMCU_IDCODE register (0xE0042000)
  */
+
+#define CORE_M3_R1 0x1BA00477
+#define CORE_M3_R2 0x4BA00477
+#define CORE_M4_R0 0x2BA01477
+
 struct chip_params {
        uint32_t chip_id;
        char* description;
@@ -43,11 +55,11 @@ struct chip_params {
        { 0x410, "F1 Medium-density device", 0x1ffff7e0,
          0x20000,  0x400, 0x5000,  0x1ffff000, 0x800  }, // table 2, pm0063
        { 0x411, "F2 device", 0, /* No flash size register found in the docs*/
-         0x100000,   0x20000, 0x20000, 0x1ff00000, 0x7800  }, // table 1, pm0059
+         0x100000,   0x20000, 0x20000, 0x1fff0000, 0x7800  }, // table 1, pm0059
        { 0x412, "F1 Low-density device", 0x1ffff7e0,
          0x8000,   0x400, 0x2800,  0x1ffff000, 0x800  }, // table 1, pm0063
        { 0x413, "F4 device", 0x1FFF7A10,
-         0x100000,   0x20000, 0x20000,  0x1ff00000, 0x7800  }, // table 1, pm0081
+         0x100000,   0x20000, 0x30000,  0x1fff0000, 0x7800  }, // table 1, pm0081
        { 0x414, "F1 High-density device", 0x1ffff7e0,
          0x80000,  0x800, 0x10000, 0x1ffff000, 0x800  },  // table 3 pm0063 
           // This ignores the EEPROM! (and uses the page erase size,
@@ -69,21 +81,95 @@ int serve(stlink_t *sl, int port);
 char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
 
 int main(int argc, char** argv) {
-       if(argc != 3) {
-               fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
-               return 1;
+
+       stlink_t *sl = NULL;
+
+       const char * HelpStr =  "Usage:\n"
+                                                               "\t st-util port [/dev/sgX]\n"
+                                                               "\t st-util [port]\n"
+                                                               "\t st-util --help\n";
+
+       switch(argc) {
+
+               case 3 : {
+                       //sl = stlink_quirk_open(argv[2], 0);
+                       // FIXME - hardcoded to usb....
+                       sl = stlink_open_usb(10);
+                       if(sl == NULL) return 1;
+                       break;
+               }
+
+               case 2 : {
+                       if (strcmp(argv[1], "--help") == 0) {
+                               fprintf(stdout, HelpStr, NULL);
+                               return 1;
+                       }
+               }
+
+#if CONFIG_USE_LIBSG
+               case 1 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99)
+                       const int DevNumMax = 99;
+                       int ExistDevCount = 0;
+
+                       for(int DevNum = 0; DevNum <= DevNumMax; DevNum++)
+                       {
+                               if(DevNum < 10) {
+                                       char DevName[] = "/dev/sgX";
+                                       const int X_index = 7;
+                                       DevName[X_index] = DevNum + '0';
+                                       if ( !access(DevName, F_OK) ) {
+                                               sl = stlink_quirk_open(DevName, 0);
+                                               ExistDevCount++;
+                                       }
+                               }
+                               else if(DevNum < 100) {
+                                       char DevName[] = "/dev/sgXY";
+                                       const int X_index = 7;
+                                       const int Y_index = 8;
+                                       DevName[X_index] = DevNum/10 + '0';
+                                       DevName[Y_index] = DevNum%10 + '0';
+                                       if ( !access(DevName, F_OK) ) {
+                                               sl = stlink_quirk_open(DevName, 0);
+                                               ExistDevCount++;
+                                       }
+                               }
+                               if(sl != NULL) break;
+                       }
+
+                       if(sl == NULL) {
+                               fprintf(stdout, "\nNumber of /dev/sgX devices found: %i \n",
+                                               ExistDevCount);
+                               fprintf(stderr, "ST-LINK not found\n");
+                               return 1;
+                       }
+                       break;
+               }
+#endif
+
+               default: {
+                       fprintf(stderr, HelpStr, NULL);
+                       return 1;
+               }
        }
 
-        // FIXME - hardcoded to usb....
-        stlink_t *sl =stlink_open_usb(argv[2], 10);
-       if (sl == NULL)
-               return 1;
+       if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
+               stlink_exit_dfu_mode(sl);
+       }
 
-       if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
-               stlink_enter_swd_mode(sl);
+       if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
+         stlink_enter_swd_mode(sl);
+       }
 
        uint32_t chip_id = stlink_chip_id(sl);
-       printf("Chip ID is %08x.\n", chip_id);
+       uint32_t core_id = stlink_core_id(sl);
+
+       /* Fix chip_id for F4 */
+       if (((chip_id & 0xFFF) == 0x411) && (core_id == CORE_M4_R0)) {
+         printf("Fixing wrong chip_id for STM32F4 Rev A errata\n");
+         chip_id = 0x413;
+       }
+
+       printf("Chip ID is %08x, Core ID is  %08x.\n", chip_id, core_id);
 
        const struct chip_params* params = NULL;
 
@@ -114,10 +200,13 @@ int main(int argc, char** argv) {
        // memory map is in 1k blocks.
        current_memory_map = make_memory_map(params, flash_size * 0x400);
 
-       int port = atoi(argv[1]);
+       int port = 4242;
 
        while(serve(sl, port) == 0);
 
+       /* Switch back to mass storage mode before closing. */
+       stlink_run(sl);
+       stlink_exit_debug_mode(sl);
        stlink_close(sl);
 
        return 0;
@@ -297,12 +386,14 @@ struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
 static void init_code_breakpoints(stlink_t *sl) {
        memset(sl->q_buf, 0, 4);
        sl->q_buf[0] = 0x03; // KEY | ENABLE
-       stlink_write_mem32(sl, 0xe0002000, 4);
+       stlink_write_mem32(sl, CM3_REG_FP_CTRL, 4);
+        printf("KARL - should read back as 0x03, not 60 02 00 00\n");
+        stlink_read_mem32(sl, CM3_REG_FP_CTRL, 4);
 
        memset(sl->q_buf, 0, 4);
        for(int i = 0; i < CODE_BREAK_NUM; i++) {
                code_breaks[i].type = 0;
-               stlink_write_mem32(sl, 0xe0002008 + i * 4, 4);
+               stlink_write_mem32(sl, CM3_REG_FP_COMP0 + i * 4, 4);
        }
 }
 
@@ -512,7 +603,9 @@ int serve(stlink_t *sl, int port) {
 
        printf("Listening at *:%d...\n", port);
 
+       (void) signal (SIGINT, ctrl_c);
        int client = accept(sock, NULL, NULL);
+       signal (SIGINT, SIG_DFL);
        if(client < 0) {
                perror("accept");
                return 1;
@@ -569,8 +662,9 @@ int serve(stlink_t *sl, int port) {
                        if(!strcmp(queryName, "Supported")) {
                                reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
                        } else if(!strcmp(queryName, "Xfer")) {
-                               char *type, *op, *annex, *s_addr, *s_length;
+                               char *type, *op, *s_addr, *s_length;
                                char *tok = params;
+                               char *annex __attribute__((unused));
 
                                type     = strsep(&tok, ":");
                                op       = strsep(&tok, ":");