2012-06-24 04:53:13 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Black Magic Debug project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Black Sphere Technologies Ltd.
|
|
|
|
* Written by Gareth McMullin <gareth@blacksphere.co.nz>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "general.h"
|
|
|
|
#include "target.h"
|
|
|
|
|
|
|
|
target *target_list = NULL;
|
2013-05-04 20:46:47 +00:00
|
|
|
bool connect_assert_srst;
|
2012-06-24 04:53:13 +00:00
|
|
|
|
|
|
|
target *target_new(unsigned size)
|
|
|
|
{
|
|
|
|
target *t = (void*)calloc(1, size);
|
|
|
|
t->next = target_list;
|
|
|
|
target_list = t;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void target_list_free(void)
|
|
|
|
{
|
2012-06-24 09:37:10 +00:00
|
|
|
struct target_command_s *tc;
|
|
|
|
|
2012-06-24 04:53:13 +00:00
|
|
|
while(target_list) {
|
|
|
|
target *t = target_list->next;
|
2012-06-27 09:26:08 +00:00
|
|
|
if (target_list->destroy_callback)
|
|
|
|
target_list->destroy_callback(target_list);
|
2012-07-05 07:23:28 +00:00
|
|
|
if (target_list->priv)
|
|
|
|
target_list->priv_free(target_list->priv);
|
2012-06-24 09:37:10 +00:00
|
|
|
while (target_list->commands) {
|
|
|
|
tc = target_list->commands->next;
|
|
|
|
free(target_list->commands);
|
|
|
|
target_list->commands = tc;
|
|
|
|
}
|
2015-03-25 03:45:05 +00:00
|
|
|
if (target_list->dyn_mem_map)
|
|
|
|
free(target_list->dyn_mem_map);
|
|
|
|
while (target_list->ram) {
|
|
|
|
void * next = target_list->ram->next;
|
|
|
|
free(target_list->ram);
|
|
|
|
target_list->ram = next;
|
|
|
|
}
|
|
|
|
while (target_list->flash) {
|
|
|
|
void * next = target_list->flash->next;
|
|
|
|
free(target_list->flash);
|
|
|
|
target_list->flash = next;
|
|
|
|
}
|
2012-06-24 04:53:13 +00:00
|
|
|
free(target_list);
|
|
|
|
target_list = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-24 09:37:10 +00:00
|
|
|
void target_add_commands(target *t, const struct command_s *cmds, const char *name)
|
|
|
|
{
|
|
|
|
struct target_command_s *tc;
|
|
|
|
if (t->commands) {
|
|
|
|
for (tc = t->commands; tc->next; tc = tc->next);
|
|
|
|
tc = tc->next = malloc(sizeof(*tc));
|
|
|
|
} else {
|
|
|
|
t->commands = tc = malloc(sizeof(*tc));
|
|
|
|
}
|
|
|
|
tc->specific_name = name;
|
|
|
|
tc->cmds = cmds;
|
|
|
|
tc->next = NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-27 09:26:08 +00:00
|
|
|
target *target_attach(target *t, target_destroy_callback destroy_cb)
|
|
|
|
{
|
|
|
|
if (t->destroy_callback)
|
|
|
|
t->destroy_callback(t);
|
|
|
|
|
|
|
|
t->destroy_callback = destroy_cb;
|
|
|
|
|
2012-11-03 03:42:46 +00:00
|
|
|
if (!t->attach(t))
|
|
|
|
return NULL;
|
2012-06-27 09:26:08 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-03-23 05:59:04 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-03-25 03:45:05 +00:00
|
|
|
/* Deprecated static const memory map */
|
2015-03-23 05:59:04 +00:00
|
|
|
if (t->xml_mem_map)
|
|
|
|
return t->xml_mem_map;
|
|
|
|
|
2015-03-25 03:45:05 +00:00
|
|
|
if (t->dyn_mem_map)
|
|
|
|
return t->dyn_mem_map;
|
|
|
|
|
2015-03-23 05:59:04 +00:00
|
|
|
/* 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>");
|
|
|
|
|
2015-03-25 03:45:05 +00:00
|
|
|
t->dyn_mem_map = tmp;
|
2015-03-23 05:59:04 +00:00
|
|
|
|
2015-03-25 03:45:05 +00:00
|
|
|
return t->dyn_mem_map;
|
2015-03-23 05:59:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 03:45:59 +00:00
|
|
|
static struct target_flash *flash_for_addr(target *t, uint32_t addr)
|
|
|
|
{
|
|
|
|
for (struct target_flash *f = t->flash; f; f = f->next)
|
|
|
|
if ((f->start <= addr) &&
|
|
|
|
(addr < (f->start + f->length)))
|
|
|
|
return f;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int target_flash_erase(target *t, uint32_t addr, size_t len)
|
|
|
|
{
|
|
|
|
if (t->flash_write)
|
|
|
|
return t->flash_erase(t, addr, len);
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
while (len) {
|
|
|
|
struct target_flash *f = flash_for_addr(t, addr);
|
|
|
|
size_t tmplen = MIN(len, f->length - (addr % f->length));
|
|
|
|
ret |= f->erase(f, addr, tmplen);
|
|
|
|
addr += tmplen;
|
|
|
|
len -= tmplen;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int target_flash_write(target *t,
|
|
|
|
uint32_t dest, const void *src, size_t len)
|
|
|
|
{
|
|
|
|
if (t->flash_write)
|
|
|
|
return t->flash_write(t, dest, src, len);
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
while (len) {
|
|
|
|
struct target_flash *f = flash_for_addr(t, dest);
|
|
|
|
size_t tmplen = MIN(len, f->length - (dest % f->length));
|
|
|
|
ret |= f->write(f, dest, src, tmplen);
|
|
|
|
src += tmplen;
|
|
|
|
len -= tmplen;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int target_flash_done(target *t)
|
|
|
|
{
|
|
|
|
for (struct target_flash *f = t->flash; f; f = f->next) {
|
|
|
|
if (f->done) {
|
|
|
|
int tmp = f->done(f);
|
|
|
|
if (tmp)
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|