Merge pull request #313 from schodet/f2-memory
[fw/stlink] / gdbserver / gdb-server.c
index 71ab440163f352b7f9bdb4888f58ede9eabefc68..393474dd3396b2971f2588fc1b6e922d107a4e1a 100644 (file)
@@ -1,4 +1,3 @@
-#define DEBUG 0
 /*
  * Copyright (C)  2011 Peter Zotov <whitequark@whitequark.org>
  * Use of this source code is governed by a BSD-style
 #endif
 
 #include <stlink-common.h>
+#include <uglylogging.h>
 
 #include "gdb-remote.h"
-
-#define DEFAULT_LOGGING_LEVEL 50
-#define DEFAULT_GDB_LISTEN_PORT 4242
-
-#define STRINGIFY_inner(name) #name
-#define STRINGIFY(name) STRINGIFY_inner(name)
+#include "gdb-server.h"
 
 #define FLASH_BASE 0x08000000
 
@@ -195,11 +190,11 @@ int main(int argc, char** argv) {
         stlink_reset(sl);
     }
 
-    printf("Chip ID is %08x, Core ID is  %08x.\n", sl->chip_id, sl->core_id);
+    ILOG("Chip ID is %08x, Core ID is  %08x.\n", sl->chip_id, sl->core_id);
 
     voltage = stlink_target_voltage(sl);
     if (voltage != -1) {
-        printf("Target voltage is %d mV.\n", voltage);
+        ILOG("Target voltage is %d mV.\n", voltage);
     }
 
     sl->verbose=0;
@@ -214,7 +209,9 @@ int main(int argc, char** argv) {
 #endif
 
     do {
-        serve(sl, &state);
+        if (serve(sl, &state)) {
+         sleep (1); // don't go bezurk if serve returns with error
+       }
 
         /* Continue */
         stlink_run(sl);
@@ -311,15 +308,65 @@ static const char* const memory_map_template_F4 =
     "  <memory type=\"flash\" start=\"0x08010000\" length=\"0x10000\">"     //Sector 4
     "    <property name=\"blocksize\">0x10000</property>"                   //64kB
     "  </memory>"
-    "  <memory type=\"flash\" start=\"0x08020000\" length=\"0x70000\">"     //Sectors 5..11
+    "  <memory type=\"flash\" start=\"0x08020000\" length=\"0xE0000\">"     //Sectors 5..11
     "    <property name=\"blocksize\">0x20000</property>"                   //128kB
     "  </memory>"
     "  <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>"   // peripheral regs
+    "  <memory type=\"ram\" start=\"0x60000000\" length=\"0x7fffffff\"/>"   // AHB3 Peripherals
     "  <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>"   // cortex regs
     "  <memory type=\"rom\" start=\"0x1fff0000\" length=\"0x7800\"/>"       // bootrom
     "  <memory type=\"rom\" start=\"0x1fffc000\" length=\"0x10\"/>"         // option byte area
     "</memory-map>";
 
+static const char* const memory_map_template_F4_HD =
+    "<?xml version=\"1.0\"?>"
+    "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
+    "     \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
+    "<memory-map>"
+    "  <memory type=\"rom\" start=\"0x00000000\" length=\"0x100000\"/>"     // code = sram, bootrom or flash; flash is bigger
+    "  <memory type=\"ram\" start=\"0x10000000\" length=\"0x10000\"/>"      // ccm ram
+    "  <memory type=\"ram\" start=\"0x20000000\" length=\"0x40000\"/>"      // sram
+    "  <memory type=\"ram\" start=\"0x60000000\" length=\"0x10000000\"/>"   // fmc bank 1 (nor/psram/sram)
+    "  <memory type=\"ram\" start=\"0x70000000\" length=\"0x20000000\"/>"   // fmc bank 2 & 3 (nand flash)
+    "  <memory type=\"ram\" start=\"0x90000000\" length=\"0x10000000\"/>"   // fmc bank 4 (pc card)
+    "  <memory type=\"ram\" start=\"0xC0000000\" length=\"0x20000000\"/>"   // fmc sdram bank 1 & 2
+    "  <memory type=\"flash\" start=\"0x08000000\" length=\"0x10000\">"     //Sectors 0..3
+    "    <property name=\"blocksize\">0x4000</property>"                    //16kB
+    "  </memory>"
+    "  <memory type=\"flash\" start=\"0x08010000\" length=\"0x10000\">"     //Sector 4
+    "    <property name=\"blocksize\">0x10000</property>"                   //64kB
+    "  </memory>"
+    "  <memory type=\"flash\" start=\"0x08020000\" length=\"0xE0000\">"     //Sectors 5..11
+    "    <property name=\"blocksize\">0x20000</property>"                   //128kB
+    "  </memory>"
+    "  <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>"   // peripheral regs
+    "  <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>"   // cortex regs
+    "  <memory type=\"rom\" start=\"0x1fff0000\" length=\"0x7800\"/>"       // bootrom
+    "  <memory type=\"rom\" start=\"0x1fffc000\" length=\"0x10\"/>"         // option byte area
+    "</memory-map>";
+
+static const char* const memory_map_template_F2 =
+    "<?xml version=\"1.0\"?>"
+    "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
+    "     \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
+    "<memory-map>"
+    "  <memory type=\"rom\" start=\"0x00000000\" length=\"0x%zx\"/>"        // code = sram, bootrom or flash; flash is bigger
+    "  <memory type=\"ram\" start=\"0x20000000\" length=\"0x%zx\"/>"        // sram
+    "  <memory type=\"flash\" start=\"0x08000000\" length=\"0x10000\">"     //Sectors 0..3
+    "    <property name=\"blocksize\">0x4000</property>"                    //16kB
+    "  </memory>"
+    "  <memory type=\"flash\" start=\"0x08010000\" length=\"0x10000\">"     //Sector 4
+    "    <property name=\"blocksize\">0x10000</property>"                   //64kB
+    "  </memory>"
+    "  <memory type=\"flash\" start=\"0x08020000\" length=\"0x%zx\">"       //Sectors 5..
+    "    <property name=\"blocksize\">0x20000</property>"                   //128kB
+    "  </memory>"
+    "  <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>"   // peripheral regs
+    "  <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>"   // cortex regs
+    "  <memory type=\"rom\" start=\"0x%08x\" length=\"0x%zx\"/>"            // bootrom
+    "  <memory type=\"rom\" start=\"0x1fffc000\" length=\"0x10\"/>"         // option byte area
+    "</memory-map>";
+
 static const char* const memory_map_template =
     "<?xml version=\"1.0\"?>"
     "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
@@ -343,6 +390,14 @@ char* make_memory_map(stlink_t *sl) {
 
     if(sl->chip_id==STM32_CHIPID_F4) {
         strcpy(map, memory_map_template_F4);
+    } else if(sl->chip_id==STM32_CHIPID_F4_HD) {
+        strcpy(map, memory_map_template_F4_HD);
+    } else if(sl->chip_id==STM32_CHIPID_F2) {
+        snprintf(map, 4096, memory_map_template_F2,
+                sl->flash_size,
+                sl->sram_size,
+                sl->flash_size - 0x20000,
+                sl->sys_base, sl->sys_size);
     } else {
         snprintf(map, 4096, memory_map_template,
                 sl->flash_size,
@@ -382,9 +437,7 @@ struct code_hw_watchpoint {
 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
 
 static void init_data_watchpoints(stlink_t *sl) {
-#if DEBUG
-    printf("init watchpoints\n");
-#endif
+    DLOG("init watchpoints\n");
 
     // set trcena in debug command to turn on dwt unit
     stlink_write_debug32(sl, 0xE000EDFC,
@@ -397,8 +450,8 @@ static void init_data_watchpoints(stlink_t *sl) {
     }
 }
 
-static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
-{
+static int add_data_watchpoint(stlink_t *sl, enum watchfun wf,
+                               stm32_addr_t addr, unsigned int len) {
     int i = 0;
     uint32_t mask;
 
@@ -417,9 +470,7 @@ static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr
         for(i = 0; i < DATA_WATCH_NUM; i++) {
             // is this an empty slot ?
             if(data_watches[i].fun == WATCHDISABLED) {
-#if DEBUG
-                printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
-#endif
+                DLOG("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
 
                 data_watches[i].fun = wf;
                 data_watches[i].addr = addr;
@@ -441,9 +492,7 @@ static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr
         }
     }
 
-#if DEBUG
-    printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
-#endif
+    DLOG("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
     return -1;
 }
 
@@ -453,9 +502,7 @@ static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
 
     for(i = 0 ; i < DATA_WATCH_NUM; i++) {
         if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
-#if DEBUG
-            printf("delete watchpoint %d addr %x\n", i, addr);
-#endif
+            DLOG("delete watchpoint %d addr %x\n", i, addr);
 
             data_watches[i].fun = WATCHDISABLED;
             stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
@@ -464,9 +511,7 @@ static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
         }
     }
 
-#if DEBUG
-    printf("failure: delete watchpoint addr %x\n", addr);
-#endif
+    DLOG("failure: delete watchpoint addr %x\n", addr);
 
     return -1;
 }
@@ -490,7 +535,7 @@ static void init_code_breakpoints(stlink_t *sl) {
     if (((val & 3) != 1) ||
             ((((val >> 8) & 0x70) | ((val >> 4) & 0xf)) != CODE_BREAK_NUM) ||
             (((val >> 8) & 0xf) != CODE_LIT_NUM)){
-        fprintf(stderr, "[FP_CTRL] = 0x%08x expecting 0x%08x\n", val,
+        ELOG("[FP_CTRL] = 0x%08x expecting 0x%08x\n", val,
                 ((CODE_BREAK_NUM & 0x70) << 8) | (CODE_LIT_NUM << 8) |  ((CODE_BREAK_NUM & 0xf) << 4) | 1);
     }
 
@@ -506,7 +551,7 @@ static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
     int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
 
     if(addr & 1) {
-        fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
+        ELOG("update_code_breakpoint: unaligned address %08x\n", addr);
         return -1;
     }
 
@@ -532,20 +577,16 @@ static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
     else       brk->type &= ~type;
 
     if(brk->type == 0) {
-#if DEBUG
-        printf("clearing hw break %d\n", id);
-#endif
+        DLOG("clearing hw break %d\n", id);
 
         stlink_write_debug32(sl, 0xe0002008 + id * 4, 0);
     } else {
         uint32_t mask = (brk->addr) | 1 | (brk->type << 30);
 
-#if DEBUG
-        printf("setting hw break %d at %08x (%d)\n",
-                id, brk->addr, brk->type);
-        printf("reg %08x \n",
-                mask);
-#endif
+        DLOG("setting hw break %d at %08x (%d)\n",
+                    id, brk->addr, brk->type);
+        DLOG("reg %08x \n",
+                    mask);
 
         stlink_write_debug32(sl, 0xe0002008 + id * 4, mask);
     }
@@ -567,13 +608,13 @@ static struct flash_block* flash_root;
 static int flash_add_block(stm32_addr_t addr, unsigned length, stlink_t *sl) {
 
     if(addr < FLASH_BASE || addr + length > FLASH_BASE + sl->flash_size) {
-        fprintf(stderr, "flash_add_block: incorrect bounds\n");
+        ELOG("flash_add_block: incorrect bounds\n");
         return -1;
     }
 
     stlink_calculate_pagesize(sl, addr);
     if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
-        fprintf(stderr, "flash_add_block: unaligned block\n");
+        ELOG("flash_add_block: unaligned block\n");
         return -1;
     }
 
@@ -616,14 +657,14 @@ static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
     }
 
     if(fit_blocks == 0) {
-        fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
+        ELOG("Unfit data block %08x -> %04x\n", addr, length);
         return -1;
     }
 
     if(fit_length != length) {
-        fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
+        WLOG("data block %08x -> %04x truncated to %04x\n",
                 addr, length, fit_length);
-        fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
+        WLOG("(this is not an error, just a GDB glitch)\n");
     }
 
     return 0;
@@ -636,9 +677,7 @@ static int flash_go(stlink_t *sl) {
     stlink_reset(sl);
 
     for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
-#if DEBUG
-        printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
-#endif
+        DLOG("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
 
         unsigned length = fb->length;
         for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
@@ -646,9 +685,7 @@ static int flash_go(stlink_t *sl) {
             //Update FLASH_PAGE
             stlink_calculate_pagesize(sl, page);
 
-#if DEBUG
-            printf("flash_do: page %08x\n", page);
-#endif
+            DLOG("flash_do: page %08x\n", page);
 
             if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
                         length > FLASH_PAGE ? FLASH_PAGE : length) < 0)
@@ -698,7 +735,7 @@ int serve(stlink_t *sl, st_state_t *st) {
         return 1;
     }
 
-    printf("Listening at *:%d...\n", st->listen_port);
+    ILOG("Listening at *:%d...\n", st->listen_port);
 
     int client = accept(sock, NULL, NULL);
     //signal (SIGINT, SIG_DFL);
@@ -716,7 +753,7 @@ int serve(stlink_t *sl, st_state_t *st) {
     init_code_breakpoints(sl);
     init_data_watchpoints(sl);
 
-    printf("GDB connected.\n");
+    ILOG("GDB connected.\n");
 
     /*
      * To allow resetting the chip from GDB it is required to
@@ -729,13 +766,14 @@ int serve(stlink_t *sl, st_state_t *st) {
 
         int status = gdb_recv_packet(client, &packet);
         if(status < 0) {
-            fprintf(stderr, "cannot recv: %d\n", status);
+            ELOG("cannot recv: %d\n", status);
+#ifdef __MINGW32__
+            win32_close_socket(sock);
+#endif
             return 1;
         }
 
-#if DEBUG
-        printf("recv: %s\n", packet);
-#endif
+        DLOG("recv: %s\n", packet);
 
         char* reply = NULL;
         reg regp;
@@ -758,12 +796,10 @@ int serve(stlink_t *sl, st_state_t *st) {
                 char* queryName = calloc(queryNameLength + 1, 1);
                 strncpy(queryName, &packet[1], queryNameLength);
 
-#if DEBUG
-                printf("query: %s;%s\n", queryName, params);
-#endif
+                DLOG("query: %s;%s\n", queryName, params);
 
                 if(!strcmp(queryName, "Supported")) {
-                    if(sl->chip_id==STM32_CHIPID_F4) {
+                    if(sl->chip_id==STM32_CHIPID_F4 || sl->chip_id==STM32_CHIPID_F4_HD) {
                         reply = strdup("PacketSize=3fff;qXfer:memory-map:read+;qXfer:features:read+");
                     }
                     else {
@@ -783,10 +819,8 @@ int serve(stlink_t *sl, st_state_t *st) {
                     unsigned addr = strtoul(__s_addr, NULL, 16),
                              length = strtoul(s_length, NULL, 16);
 
-#if DEBUG
-                    printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
-                            type, op, annex, addr, length);
-#endif
+                    DLOG("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
+                                type, op, annex, addr, length);
 
                     const char* data = NULL;
 
@@ -820,9 +854,7 @@ int serve(stlink_t *sl, st_state_t *st) {
 
 
                     if (!strncmp(params,"726573756d65",12)) {// resume
-#if DEBUG
-                        printf("Rcmd: resume\n");
-#endif
+                        DLOG("Rcmd: resume\n");
                         stlink_run(sl);
 
                         reply = strdup("OK");
@@ -831,19 +863,15 @@ int serve(stlink_t *sl, st_state_t *st) {
 
                         stlink_force_debug(sl);
 
-#if DEBUG
-                        printf("Rcmd: halt\n");
-#endif
+                        DLOG("Rcmd: halt\n");
                     } else if (!strncmp(params,"6a7461675f7265736574",20)) { //jtag_reset
                         reply = strdup("OK");
 
-                        stlink_jtag_reset(sl, 1);
                         stlink_jtag_reset(sl, 0);
+                        stlink_jtag_reset(sl, 1);
                         stlink_force_debug(sl);
 
-#if DEBUG
-                        printf("Rcmd: jtag_reset\n");
-#endif
+                        DLOG("Rcmd: jtag_reset\n");
                     } else if (!strncmp(params,"7265736574",10)) { //reset
                         reply = strdup("OK");
 
@@ -852,14 +880,9 @@ int serve(stlink_t *sl, st_state_t *st) {
                         init_code_breakpoints(sl);
                         init_data_watchpoints(sl);
 
-#if DEBUG
-                        printf("Rcmd: reset\n");
-#endif
+                        DLOG("Rcmd: reset\n");
                     } else {
-#if DEBUG
-                        printf("Rcmd: %s\n", params);
-#endif
-
+                        DLOG("Rcmd: %s\n", params);
                     }
 
                 }
@@ -888,10 +911,8 @@ int serve(stlink_t *sl, st_state_t *st) {
                     unsigned addr = strtoul(__s_addr, NULL, 16),
                              length = strtoul(s_length, NULL, 16);
 
-#if DEBUG
-                    printf("FlashErase: addr:%08x,len:%04x\n",
-                            addr, length);
-#endif
+                    DLOG("FlashErase: addr:%08x,len:%04x\n",
+                                addr, length);
 
                     if(flash_add_block(addr, length, sl) < 0) {
                         reply = strdup("E00");
@@ -926,9 +947,7 @@ int serve(stlink_t *sl, st_state_t *st) {
                     if(dec_index % 2 != 0)
                         dec_index++;
 
-#if DEBUG
-                    printf("binary packet %d -> %d\n", data_length, dec_index);
-#endif
+                    DLOG("binary packet %d -> %d\n", data_length, dec_index);
 
                     if(flash_populate(addr, decoded, dec_index) < 0) {
                         reply = strdup("E00");
@@ -959,7 +978,10 @@ int serve(stlink_t *sl, st_state_t *st) {
                 while(1) {
                     int status = gdb_check_for_interrupt(client);
                     if(status < 0) {
-                        fprintf(stderr, "cannot check for int: %d\n", status);
+                        ELOG("cannot check for int: %d\n", status);
+#ifdef __MINGW32__
+                        win32_close_socket(sock);
+#endif
                         return 1;
                     }
 
@@ -1267,15 +1289,16 @@ int serve(stlink_t *sl, st_state_t *st) {
         }
 
         if(reply) {
-#if DEBUG
-            printf("send: %s\n", reply);
-#endif
+            DLOG("send: %s\n", reply);
 
             int result = gdb_send_packet(client, reply);
             if(result != 0) {
-                fprintf(stderr, "cannot send: %d\n", result);
+                ELOG("cannot send: %d\n", result);
                 free(reply);
                 free(packet);
+#ifdef __MINGW32__
+                win32_close_socket(sock);
+#endif
                 return 1;
             }
 
@@ -1285,5 +1308,9 @@ int serve(stlink_t *sl, st_state_t *st) {
         free(packet);
     }
 
+#ifdef __MINGW32__
+    win32_close_socket(sock);
+#endif
+
     return 0;
 }