aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGareth McMullin2015-03-22 22:59:04 -0700
committerGareth McMullin2015-04-03 21:18:16 -0700
commit0fc635b3f8a961d09a7ac784d7846c785a3345dc (patch)
tree787ad3167e2e1a5d15c315389d6818eb5f767a72
parent482070c91b0cc5a5f16c02a30e26e306685566bb (diff)
Add functions for dynamically generating the XML memory map.
-rw-r--r--src/include/target.h30
-rw-r--r--src/platforms/native/platform.h1
-rw-r--r--src/target.c58
3 files changed, 85 insertions, 4 deletions
diff --git a/src/include/target.h b/src/include/target.h
index 940a360..5a9a94a 100644
--- a/src/include/target.h
+++ b/src/include/target.h
@@ -112,12 +112,27 @@ target *target_attach(target *t, target_destroy_callback destroy_cb);
#define target_regs_size(target) \
((target)->regs_size)
-#define target_mem_map(target) \
- ((target)->xml_mem_map ? (target)->xml_mem_map : "")
-
#define target_tdesc(target) \
((target)->tdesc ? (target)->tdesc : "")
+struct target_ram {
+ uint32_t start;
+ uint32_t length;
+ struct target_ram *next;
+};
+
+struct target_flash {
+ uint32_t start;
+ uint32_t length;
+ uint32_t blocksize;
+ int (*erase)(struct target_flash *f, uint32_t addr, size_t len);
+ int (*write)(struct target_flash *f, uint32_t dest,
+ const uint8_t *src, size_t len);
+ int (*done)(struct target_flash *t);
+ target *t;
+ struct target_flash *next;
+};
+
struct target_s {
/* Notify controlling debugger if target is lost */
target_destroy_callback destroy_callback;
@@ -158,8 +173,12 @@ struct target_s {
unsigned target_options;
uint32_t idcode;
- /* Flash memory access functions */
+ /* Target memory map */
const char *xml_mem_map;
+ struct target_ram *ram;
+ struct target_flash *flash;
+
+ /* DEPRECATED: Flash memory access functions */
int (*flash_erase)(target *t, uint32_t addr, size_t len);
int (*flash_write)(target *t, uint32_t dest,
const uint8_t *src, size_t len);
@@ -190,6 +209,9 @@ extern bool connect_assert_srst;
target *target_new(unsigned size);
void target_list_free(void);
void target_add_commands(target *t, const struct command_s *cmds, const char *name);
+void target_add_ram(target *t, uint32_t start, uint32_t len);
+void target_add_flash(target *t, struct target_flash *f);
+const char *target_mem_map(target *t);
static inline uint32_t target_mem_read32(target *t, uint32_t addr)
{
diff --git a/src/platforms/native/platform.h b/src/platforms/native/platform.h
index 6958585..96d86be 100644
--- a/src/platforms/native/platform.h
+++ b/src/platforms/native/platform.h
@@ -150,6 +150,7 @@
/* Use newlib provided integer only stdio functions */
#define sscanf siscanf
#define sprintf siprintf
+#define snprintf sniprintf
#define vasprintf vasiprintf
#endif
diff --git a/src/target.c b/src/target.c
index 4299a45..4e10e72 100644
--- a/src/target.c
+++ b/src/target.c
@@ -80,3 +80,61 @@ target *target_attach(target *t, target_destroy_callback destroy_cb)
return t;
}
+void target_add_ram(target *t, uint32_t start, uint32_t len)
+{
+ struct target_ram *ram = malloc(sizeof(*ram));
+ ram->start = start;
+ ram->length = len;
+ ram->next = t->ram;
+ t->ram = ram;
+}
+
+void target_add_flash(target *t, struct target_flash *f)
+{
+ f->t = t;
+ f->next = t->flash;
+ t->flash = f;
+}
+
+static ssize_t map_ram(char *buf, size_t len, struct target_ram *ram)
+{
+ return snprintf(buf, len, "<memory type=\"ram\" start=\"0x%08"PRIx32
+ "\" length=\"0x%08"PRIx32"\"/>",
+ ram->start, ram->length);
+}
+
+static ssize_t map_flash(char *buf, size_t len, struct target_flash *f)
+{
+ int i = 0;
+ i += snprintf(&buf[i], len - i, "<memory type=\"flash\" start=\"0x%08"PRIx32
+ "\" length=\"0x%08"PRIx32"\">",
+ f->start, f->length);
+ i += snprintf(&buf[i], len - i, "<property name=\"blocksize\">0x%08"PRIx32
+ "</property></memory>",
+ f->blocksize);
+ return i;
+}
+
+const char *target_mem_map(target *t)
+{
+ if (t->xml_mem_map)
+ return t->xml_mem_map;
+
+ /* FIXME size buffer */
+ size_t len = 1024;
+ char *tmp = malloc(len);
+ size_t i = 0;
+ i = snprintf(&tmp[i], len - i, "<memory-map>");
+ /* Map each defined RAM */
+ for (struct target_ram *r = t->ram; r; r = r->next)
+ i += map_ram(&tmp[i], len - i, r);
+ /* Map each defined Flash */
+ for (struct target_flash *f = t->flash; f; f = f->next)
+ i += map_flash(&tmp[i], len - i, f);
+ i += snprintf(&tmp[i], len - i, "</memory-map>");
+
+ t->xml_mem_map = tmp;
+
+ return t->xml_mem_map;
+}
+