Allowed CONFIG_USE_LIBSG=0 to suppress STLink/V1 compilation
[fw/stlink] / gdbserver / gdb-server.c
index 97288014965768b3a1e5169f819d08e69459e3ee..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;
@@ -52,7 +59,7 @@ struct chip_params {
        { 0x412, "F1 Low-density device", 0x1ffff7e0,
          0x8000,   0x400, 0x2800,  0x1ffff000, 0x800  }, // table 1, pm0063
        { 0x413, "F4 device", 0x1FFF7A10,
-         0x100000,   0x20000, 0x20000,  0x1fff0000, 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,
@@ -84,15 +91,10 @@ int main(int argc, char** argv) {
 
        switch(argc) {
 
-               default: {
-                       fprintf(stderr, HelpStr, NULL);
-                       return 1;
-               }
-
                case 3 : {
                        //sl = stlink_quirk_open(argv[2], 0);
-                        // FIXME - hardcoded to usb....
-                        sl = stlink_open_usb(10);
+                       // FIXME - hardcoded to usb....
+                       sl = stlink_open_usb(10);
                        if(sl == NULL) return 1;
                        break;
                }
@@ -104,6 +106,7 @@ int main(int argc, char** argv) {
                        }
                }
 
+#if CONFIG_USE_LIBSG
                case 1 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99)
                        const int DevNumMax = 99;
                        int ExistDevCount = 0;
@@ -141,11 +144,17 @@ int main(int argc, char** argv) {
                        }
                        break;
                }
+#endif
+
+               default: {
+                       fprintf(stderr, HelpStr, NULL);
+                       return 1;
+               }
        }
 
-        if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
-            stlink_exit_dfu_mode(sl);
-        }
+       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);
@@ -195,6 +204,9 @@ int main(int argc, char** argv) {
 
        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;
@@ -591,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;